//+--------------------------------------------------------------------------------------------------+ //| Sell StopLoss Target Price.mq4 | //+--------------------------------------------------------------------------------------------------+ #property show_confirm // comment out this line to eliminate the confirmation box //+------------------------------------------------------------------+ //| script program start function | //+------------------------------------------------------------------+ // edit these values as desired below: extern double Lots = 1.0; extern int stoploss = 400; extern int takeprofit = 100; extern int Slippage = 7; extern int MagicNumber = 0; int start() { //---- ////////////////////////////////////////////////////////////////// // Order Entry section ///////////////////////////////////////////////////////////////// int ticket =-1; datetime Exp = 0; int retries = 0; while(true) { while(IsTradeContextBusy()) Sleep(100); RefreshRates(); if(IsConnected() == true) ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0.0,0.0,"Sell_SL_TP",MagicNumber,0,Lime); if (ticket >=0) break; // valid order placement if (ticket == -1) { int err = GetLastError(); Print("Entry Error #: " + err ); if (err == 2 || err == 3 || err == 5 || err == 64 || err == 65 || err == 131 || err == 132 || err == 133 || err == 134 || err == 139 || err == 148 || err == 149 || err == 150) return(0); Sleep(3000); // take a short break then retry retries += 1; // increment retries if (retries >5) return(0); // end after 15 seconds of retries } } ////////////////////////////////////////////////////////////////// // Stop Loss and Profit Target / Take Profit Modification ///////////////////////////////////////////////////////////////// Sleep(3000); retries = 0; // now attempt to alter the stop loss and take profit bool selected = OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES); while(selected) { bool modified = OrderModify(ticket, OrderOpenPrice(), OrderOpenPrice() + stoploss * Point, OrderOpenPrice() - takeprofit * Point, 0, CLR_NONE); if (modified == true) return(0); err = GetLastError(); Print("Order Modification Error #: " + err ); if (err == 2 || err == 3 || err == 5 || err == 64 || err == 65 || err == 131 || err == 132 || err == 133 || err == 134 || err == 139 || err == 148 || err == 149 || err == 150) return(0); Sleep(3000); // take a short break then retry retries += 1; // increment retries if (retries >5) return(0); // end after 15 seconds of retries } //---- Print("Unable to select order for SL/TP modification"); return(0); }