//+------------------------------------------------------------------+ //| CloseByPL.mq4 | //| | //+------------------------------------------------------------------+ extern double Profit=10; extern double StopLossInPips=10; double myProfit=0; double myPips=0; bool EnableClose=false; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { //---- myProfit=CalcProfit(); // Get the current profit for the open trades, if it is greater than Proft=10, by defualt then close the trades. // myPips = Point * myProfit * 100; // Use this to set the dynamic Stop Loss ShowInit(); if (EnableClose && CntOrdAll(OP_BUY)==0 && CntOrdAll(OP_SELL)==0) { EnableClose=false; // Before Debug: EnableClose=1 } if ( (Profit>0 && myProfit>Profit) || (Profit<0 && myProfit= StopLossInPips then //if // EnableClose=true; CloseAll(); //---- return(0); } //+------------------------------------------------------------------+ void CloseAll () { if (!EnableClose) return; int _total=OrdersTotal(); int _ordertype; int _ticket; double _priceClose; if (_total==0) return; for(int _i=_total-1;_i>=0;_i--) { //# for loop if (OrderSelect(_i,SELECT_BY_POS)) { //# if if (OrderSymbol() != Symbol()) continue; _ordertype=OrderType(); _ticket=OrderTicket(); switch(_ordertype) { //# switch case OP_BUY: _priceClose=MarketInfo(OrderSymbol(),MODE_BID); //myPips = OrderOpenPrice() - Bid; Print("Close on ",_i," position order with ticket ¹",_ticket); OrderClose(_ticket,OrderLots(),_priceClose,3,Red); break; case OP_SELL: _priceClose=MarketInfo(OrderSymbol(),MODE_ASK); //myPips = OrderOpenPrice() - Ask; Print("Close on ",_i," position order with ticket ¹",_ticket); OrderClose(_ticket,OrderLots(),_priceClose,3,Red); break; default: // values from 1 to 5, deleting pending orders // Print("Delete on ",_i," position order with ticket ¹",_ticket); // OrderDelete(_ticket); break; } //# switch } // # if } // # for loop return; } void ShowInit() { string sComment = ""; string sp = "-----------------------------------------------------\n"; string NL = "\n"; sComment = sp; sComment = sComment + "CloseByPL will close all trades at: " + Profit + NL; sComment = sComment + sp; sComment = sComment + "Current Pips="+ myPips + NL; sComment = sComment + "Current P/L="+DoubleToStr(myProfit,0)+ NL; Comment(sComment); return; } double CalcProfit() { double _sum=0; int _total=OrdersTotal(); int _ordertype; int _ticket; double _priceClose; if (_total==0) {return (0);} for(int _i=_total-1;_i>=0;_i--) { if (OrderSelect(_i,SELECT_BY_POS)) { if (OrderSymbol() != Symbol()) continue; { _ordertype=OrderType(); _ticket=OrderTicket(); _sum =_sum + OrderProfit(); if (OrderType() == OP_BUY) { _priceClose=MarketInfo(OrderSymbol(),MODE_BID); myPips = OrderOpenPrice() - StopLossInPips * Point; if (myPips > Bid) { OrderClose(_ticket,OrderLots(),_priceClose,3,Red); } // Check for ask when to close out } if (OrderType() == OP_SELL) { _priceClose=MarketInfo(OrderSymbol(),MODE_ASK); myPips = OrderOpenPrice () + StopLossInPips * Point; if (myPips < Ask) { OrderClose(_ticket,OrderLots(),_priceClose,3,Red); } // Check for bid when to close out } } } } return(_sum); } int CntOrdAll(int Type) { //return number of orders with specific parameters for current symbol int _CntOrd; _CntOrd=0; for(int i=0;i