I added a check to my trailing stop logic to set it to the MODE_STOPLEVEL if I set it too close. It's giving me the error zero in the title above, but it's actually setting the trailing stop correctly and trailing the position when it moves. Do you see anything in this code that would be a problem?
for (i = OrdersTotal(); i>=0; i-- )
if ( OrderSelect(i,SELECT_BY_POS) == true )
if ( OrderSymbol() == _Symbol )
if ( Trailing_Stop > 0 ) // if a trailing stop is specified by the user
{
if ( OrderType() == OP_BUY )
{
This_TS = NormalizeDouble(MathMin(Bid-(Trailing_Stop*Pips_Factor),MarketInfo(Symbol(),MODE_STOPLEVEL)),Digits); // calculated TS must not pass STOPLEVEL; use the min for a buy
if ( OrderStopLoss() < This_TS ) // if the calculated TS is closer to the price then the current one
{
retval = OrderModify(OrderTicket(),OrderOpenPrice(),This_TS,OrderTakeProfit(),0,Blue);
if ( retval && Write_To_Log == 2 ) WriteToLog(StringConcatenate("Order StopLoss modified successfully to ",OrderStopLoss()));
else { err = GetLastError(); Print("Error ",err, " in StopLoss modify of ticket #", OrderTicket(), ": ",ErrorDescription(err)); }
//if ( Valid_SL(stoploss,OP_BUY) != stoploss ) WriteToLog("********* Research Valid_SL BUY order modification warning: "+Valid_SL(stoploss,OP_BUY)+"<>"+stoploss);
}
}
if ( OrderType() == OP_SELL )
{
This_TS = NormalizeDouble(MathMax(Ask+(Trailing_Stop*Pips_Factor),MarketInfo(Symbol(),MODE_STOPLEVEL)),Digits); // calculated TS must not pass STOPLEVEL; use the max for a sell
if ( OrderStopLoss() > This_TS ) // if the calculated TS is closer to the price then the current one
{
retval = OrderModify(OrderTicket(),OrderOpenPrice(),This_TS,OrderTakeProfit(),0,Blue);
if ( retval && Write_To_Log == 2 ) WriteToLog(StringConcatenate("Order StopLoss modified successfully to ",OrderStopLoss()));
else { err = GetLastError(); Print("Error ",err, " in StopLoss modify of ticket #", OrderTicket(), ": ",ErrorDescription(err)); }
//if ( Valid_SL(stoploss,OP_SELL) != stoploss ) WriteToLog("********* Research Valid_SL SELL order modification warning: "+Valid_SL(stoploss,OP_SELL)+"<>"+stoploss);
}
}
}