I created a script to place my order and modify its stoploss and takeprofit, but sometimes get "invalid stops" so added a While section to check if StopLoss == 0. I thought the script would run once and quit but when my trade management EA moved the SL to breakeven somthing (the script?) kept setting it back to the previous SL until the EA closed partial, and the original ticket number did not exist, then I got a billion lines in my log that the script could not execute OrderModify because the ticket did not exist, which then used up all 58 gigs of free space on that drive until I killed MT4 and restarted so the script wasn't running. How do I get the script to stop after doing it's job placing the order? I added the if(IsStopped()==true) return(0); because I read somewhere it might help but it didn't seem to (haven't run the script again since I restarted) and can't find any help on what makes IsStopped true or false. Here are the parts with OrderModify (yes it is ECN):
and
Inserted Code
while (Ticket <= 0 && number < tries) { if (ECN) { Ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, 0, 0, 0, MagicNumber, 0, Blue); Sleep(1000); if (Ticket > 0) ecnmodify(OP_BUY, Ticket, SL, TP); }// end if (ECN) else { Ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, SL, TP, 0, MagicNumber, 0, Blue); }// end else if (ECN) if (Ticket <= 0) number++; Sleep(1000); }// end while (Ticket <= 0 && number < tries) if (Ticket <= 0) { Alert("Error opening order"); Print("Error opening order! ", errordescription(GetLastError())); return(0); }// end if (Ticket <= 0) OrderSelect(Ticket,SELECT_BY_TICKET); while(OrderStopLoss()==0) { if(IsStopped()==true) return(0); OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, OrderExpiration(), CLR_NONE); }
and
Inserted Code
void ecnmodify(int _OrderType, int Ticket, double stoploss, double takeprofit) { RefreshRates(); if(_OrderType == OP_BUY) { while(IsTradeContextBusy()) Sleep(100); if(IsStopped()==true) return(0); int result = OrderModify(Ticket, OrderOpenPrice(), NormalizeDouble(Ask - (stoploss * Point), Digits), NormalizeDouble(Ask + (takeprofit * Point), Digits), 0, Red); if(!result) { int err = GetLastError(); Print(Symbol(), " SL/TP order modify failed with error(",err,"): ",errordescription(err)); }// end if(!result) } if(_OrderType == OP_SELL) { while(IsTradeContextBusy()) Sleep(100); if(IsStopped()==true) return(0); result = OrderModify(Ticket, OrderOpenPrice(), NormalizeDouble(Bid + (stoploss * Point), Digits), NormalizeDouble(Bid - (takeprofit * Point), Digits), 0, Red); if(!result) { err = GetLastError(); Print(Symbol(), " SL/TP order modify failed with error(",err,"): ",errordescription(err)); }// end if(!result) } }