Recovering From ERR_INVALID_STOPS

 

I wanted my AddStopProfit() below to keep attempting to adjust the stop till it becomes valid. But it doesn't seem to do that. I suspect it is because a new tick comes in before the for() loop is able to make enough adjustments.


bool AddStopProfit(int argTicket, double argStopLoss, double argTakeProfit)
{
        if (!OrderSelectAlert(argTicket,SELECT_BY_TICKET))   //order is now selected as a side effect
                return(0);

        double OpenPrice = OrderOpenPrice();

        while(IsTradeContextBusy()) Sleep(10);

        // Modify Order
        bool TicketMod = OrderModify(argTicket,OrderOpenPrice(),argStopLoss,argTakeProfit,0);

        // Error Handling
        if(TicketMod == false)
        {
                int ErrorCode = GetLastError();
                string ErrDesc = ErrorDescription(ErrorCode);

                string ErrAlert = StringConcatenate("Add Stop/Profit - Error ",ErrorCode,": ",ErrDesc);
                Alert(ErrAlert);

                string ErrLog = StringConcatenate("Bid: ",MarketInfo(OrderSymbol(),MODE_BID)," Ask: ",MarketInfo(OrderSymbol(),MODE_ASK),
                             " Ticket: ",argTicket," Stop: ",argStopLoss," Profit: ",argTakeProfit);
                Print(ErrLog);
        }
        if(ErrorCode == 130){     
                //130 is ERR_INVALID_STOPS. Must put a stop somehow! We will keep attempting to set it on pip away from requested price
                for(int i = 0;TicketMod == true;i++){
                        if (OrderType() == OP_BUY)
                                TicketMod = OrderModify(argTicket,OrderOpenPrice(),argStopLoss - i*PipPoint(OrderSymbol()),argTakeProfit,0);
                        else if(OrderType() == OP_SELL)
                                TicketMod = OrderModify(argTicket,OrderOpenPrice(),argStopLoss + i*PipPoint(OrderSymbol()),argTakeProfit,0);
                }
        }
        return(TicketMod);
}     


double PipPoint(string Currency)
{
	int CalcDigits = MarketInfo(Currency,MODE_DIGITS);
	if(CalcDigits == 2 || CalcDigits == 3) double CalcPoint = 0.01;
	else if(CalcDigits == 4 || CalcDigits == 5) CalcPoint = 0.0001;
	return(CalcPoint);
}



I would be grateful for any ideas about what is going wrong.

 

If you calculate the stops correctly in the first place you won't invalid stops errors. https://book.mql4.com/appendix/limits

 
BenLinus:


I wanted my AddStopProfit() below to keep attempting to adjust the stop till it becomes valid. But it doesn't seem to do that. I suspect it is because a new tick comes in before the for() loop is able to make enough adjustments.

When you get an error 130 this is what you should do . . .

"The attempt can be repeated only if the error occurred due to the price obsolescense. After 5-second (or more) delay, it is necessary to refresh data using the RefreshRates function and make a retry. If the error does not disappear, all attempts to trade must be stopped, the program logic must be changed."

. . . from here: Trading errors


So wait for 5 seconds, make one new attempt, then fix your code.

 
RaptorUK:

When you get an error 130 this is what you should do . . .

"The attempt can be repeated only if the error occurred due to the price obsolescense. After 5-second (or more) delay, it is necessary to refresh data using the RefreshRates function and make a retry. If the error does not disappear, all attempts to trade must be stopped, the program logic must be changed."

. . . from here: Trading errors


So wait for 5 seconds, make one new attempt, then fix your code.


Thanks. I did exactly what you and SDC said; I fixed my code. Now my EA seems to always manage to set stops.
 
BenLinus:

Thanks. I did exactly what you and SDC said; I fixed my code. Now my EA seems to always manage to set stops.
Excellent, well done
Reason: