Error converting String to Double for OrderSend! - page 4

 

In the case of erroneous or unnormalized stop levels, the error 130 (ERR_INVALID_STOPS) will be generated. This can occur if the order was opened with high slippage from the bot's requested open price, which causes the requested tp/sl prices to be invalid.

I suggest to set tp/sl in a separate step, and compensate for any slippage that may have occurred during  opening the order.

Here are my changes to the previous code: 

   //stoploss = RoundPriceToStep(stoploss, tickSize);
   //takeprofit = RoundPriceToStep(takeprofit, tickSize);

   //if(!CheckSLTP(symbol, (ENUM_ORDER_TYPE)cmd, stoploss, takeprofit))
   //   return("ERROR: wrong takeprofit or stoploss detected.");

// send the order and check the result
   //int order_result = OrderSend(symbol, cmd, volume, price, slippage, stoploss, takeprofit);
   int order_result = OrderSend(symbol, cmd, volume, price, slippage, 0.0, 0.0);

   if(order_result == -1)
      return "ERROR: error during order execution! Error code: " + IntegerToString(GetLastError());

   if(!OrderSelect(order_result, SELECT_BY_TICKET))
      return "ERROR: error during OrderSelect! Error code: " + IntegerToString(GetLastError());

   double to_move = OrderOpenPrice() - price;  // actual slippage from the bot's requested open price

   stoploss = RoundPriceToStep(stoploss + to_move, tickSize);
   takeprofit = RoundPriceToStep(takeprofit + to_move, tickSize);

   if(!CheckSLTP(symbol, cmd, stoploss, takeprofit))
      return("ERROR: wrong takeprofit or stoploss detected.");

   bool res = OrderModify(OrderTicket(), OrderOpenPrice(), stoploss, takeprofit, 0, CLR_NONE);

   if(!res)
      return "ERROR: error during OrderModify! Error code: " + IntegerToString(GetLastError());
 

Hi @ amrali,

maybe i found the problem this morning while i was trying your code.

While i was debugging, i noticed that the second parameter (the operation type, OP_BUY, OP_SELL, ecc) was always 0, i passed the type as order_params[1] that is a string containing "OP_BUY"/"OP_SELL" ecc and i thought that it was automatically convert in the correspective number correspondig to the right opetation: https://docs.mql4.com/constants/tradingconstants/orderproperties

Instead, maybe due to it is a string, it was always converted to 0 (BUY operation); Indeed i remember the BUY operation worked, i had problems with the other types.

I changed my code to right manage that parameter and to convert it in the right integer value and now it seems works! :)

At this point, maybe, the first problem was the wrong SL and TP fit value, but then the problem could have been this...

I hope this is the happy ending of this long topic :)

amrali
amrali
  • 2023.05.05
  • www.mql5.com
Trader's profile
 
Always pay attention to the compiler's warnings about implicit type conversions, it often leads into logical errors during the run time.
 
amrali #:
Always pay attention to the compiler's warnings about implicit type conversions, it often leads into logical errors during the run time.
Honestly, I am not sure the compiler warns about string content and success of conversion by StringToInteger, but maybe an error code in _LastError would have revealed the failed conversion.

Though, I am only guessing on the usage of StringToInteger in the first place.

Actually this thread is one of the prime examples why it is mandatory to post all relevant and involved code. We very likely would have spotted this error much earlier in the process.

One of the main distinctions between people beginning to code and experienced coders is the level of assumptions applied to the code.

An experienced coder will always first question what is actually happening, and verify that first...
 
ducarpit #:

While i was debugging, i noticed that the second parameter (the operation type, OP_BUY, OP_SELL, ecc) was always 0, i passed the type as order_params[1] that is a string containing "OP_BUY"/"OP_SELL" ecc 

@Dominik The compiler should had warned about an implicit typecast string to int parameter to OrderSend().
 
amrali #:
@Dominik The compiler should had warned about an implicit typecast string to int parameter to OrderSend().
Right, I missed that statement about the parameter being a string.

EDIT:
Actually we missed that in the very first post.
 
Dominik Egert #:
Right, I missed that statement about the parameter being a string.

EDIT:
Actually we missed that in the very first post.
But we succeeded to solve it finally ;)