//+------------------------------------------------------------------+ //| CloseWhenAccBal.mq4 | //| | //| | //+------------------------------------------------------------------+ #property copyright "TradeForexFx" #property link "http://www." extern int My_Acc_Equity_Target=75000; extern double DoNotCloseQty=0.00; extern int No_Trade_Buffer = 500; extern bool Show_Comments= true; //int UseTradingHours = 1; //int TradeSession1 = 1; int Slippage=5; void start() { if ( GlobalVariableGet("NoTradeEquity") != (My_Acc_Equity_Target - No_Trade_Buffer)) GlobalVariableSet("NoTradeEquity", My_Acc_Equity_Target - No_Trade_Buffer) ; if(Show_Comments) DoComments(); if (My_Acc_Equity_Target !=0 && AccountEquity() >= My_Acc_Equity_Target) { CloseAll(); } return(0); } //+------------------------------------------------------------------+ void DoComments() { double TotOrder,Op_Equity,Closing_Equity,HighestEq, CurrentEq,LowestEq, Equity_Target,NoTradeEq,MaxProf,Maxprofage,MaxLoss,MaxLossage; Op_Equity = GlobalVariableGet("Start_Of_Cycle_Equity"); CurrentEq = AccountEquity(); NoTradeEq = GlobalVariableGet("NoTradeEquity"); HighestEq = GlobalVariableGet("Highest_AccEquity"); LowestEq = GlobalVariableGet("Lowest_AccEquity"); TotOrder = OrdersTotal(); Equity_Target= My_Acc_Equity_Target; MaxProf = HighestEq - Op_Equity; Maxprofage = 0; MaxLoss = 0; MaxLoss = Op_Equity - LowestEq; if (Op_Equity!=0) { Maxprofage = (MaxProf/Op_Equity) *100; MaxLossage = (MaxLoss/Op_Equity)*100; } double mpb=0; if (TotOrder>0) { mpb=AccountMargin()/TotOrder; } Comment( "Total Orders ", TotOrder," Margin per pair ", mpb, "\n", "Start of Cycle Amount: ",Op_Equity, "\n", "No Trade Above " , NoTradeEq , "\n", "Highest Equity " , HighestEq, "\n", "Lowest Equity " , LowestEq, "\n", "Curren Equity: ",CurrentEq, "\n", "Max Profit & Percentage ",MaxProf, " ", Maxprofage, "\n", "Max loss & Percentage ",MaxProf, " ", MaxLossage ); return(0); } void CloseAll() { int total = OrdersTotal(); int cnt = 0; RefreshRates(); for (cnt = 0 ; cnt <= total ; cnt++) { OrderSelect(0,SELECT_BY_POS,MODE_TRADES); if(OrderType()==OP_BUY && OrderLots() > DoNotCloseQty) OrderClose(OrderTicket(),OrderLots()-DoNotCloseQty,MarketInfo(OrderSymbol(),MODE_BID),5,Violet); if(OrderType()==OP_SELL && OrderLots() > DoNotCloseQty) OrderClose(OrderTicket(),OrderLots()-DoNotCloseQty,MarketInfo(OrderSymbol(),MODE_ASK),5,Violet); if(OrderType()>OP_SELL) //pending orders OrderDelete(OrderTicket()); } return(0); }