Error converting String to Double for OrderSend! - page 3

 

I think i understood the double variable and the floating-point and now i think i have the right value for SL and TP, in fact i can open some order successfully!

The problem is that for some order i still have the 130 error from the ordersend, but the thing more strange is that i get the same error if i set the SL and TP value direcly from code and with the same values :(

Instead, if i open the order from the smartphone app with the same parameters it works!

This could still be a problem with the correct SL value fit? 

 
ducarpit #:

I think i understood the double variable and the floating-point and now i think i have the right value for SL and TP, in fact i can open some order successfully!

The problem is that for some order i still have the 130 error from the ordersend, but the thing more strange is that i get the same error if i set the SL and TP value direcly from code and with the same values :(

Instead, if i open the order from the smartphone app with the same parameters it works!

This could still be a problem with the correct SL value fit? 

Please provide your current code, so we can see and eventually help.
 
ducarpit #:

By debugging the code, i see that during the conversion from String to Double the function add some decimal value (and i don't know why), so for example 1.07866 in string become 1.07866000000001 in double.

You can try this solution:

double RoundPriceToStep(const double price, const double step)
  {
   #define AdjustValue(value) StringToDouble(StringFormat("%.15g", value))

   double result = MathRound(AdjustValue(price / step)) * step;

   return AdjustValue(result);
  }

And use it to round the SL

   double  tickSize = MarketInfo(Symbol(), MODE_TICKSIZE);

 //double stoploss = MathRound(StringToDouble(order_params[4])/tickSize)*tickSize;
   double stoploss = RoundPriceToStep(StringToDouble(order_params[4]), tickSize);


Finally, you need to check you SL for your broker's stop level.

see the graph here https://www.mql5.com/en/forum/461774#comment_52124875 ,

and also the MQL4 book here https://book.mql4.com/appendix/limits

For buy: stoploss must be <= Bid - MarketInfo(Symbol(), MODE_STOPLEVEL) * Point()

For sell: stoploss must be >= Ask + MarketInfo(Symbol(), MODE_STOPLEVEL) * Point()
 
amrali #:

You can try this solution:

And use it to round the SL


Finally, you need to check you SL for your broker's stop level.

see the graph here https://www.mql5.com/en/forum/461774#comment_52124875 ,

and also the MQL4 book here https://book.mql4.com/appendix/limits

Thank you for you suggestion, but unfortunalty i get the same error 130 with your code :(

These are my SL, TP and TICKSIZE parameters (calculated by your function):


stoploss: 1.0744400000000001
takeprofit: 1.0734300000000001
ticksize:0.00001

And these are the Ask price, the MODE_STOPLEVEL, the Point and the Digits parameters (if they can help you):

Ask: 0.64869
Point: 0.00001
Digits: 5
MODE_STOPLEVEL: 0.0

Christian

 
ducarpit #:

Thank you for you suggestion, but unfortunalty i get the same error 130 with your code :(

These are my SL, TP and TICKSIZE parameters (calculated by your function):


And these are the Ask price, the MODE_STOPLEVEL, the Point and the Digits parameters (if they can help you):

Christian

SL and TP are both above your market price.

That cannot work.

EDIT:
ERR_INVALID_STOPS 130 Invalid stops.
 
Dominik Egert #:
SL and TP are both above your market price.

That cannot work.

EDIT:
ERR_INVALID_STOPS 130 Invalid stops.

Sorry, i wrote the wrong values (a little confusion due to the multiple test), these are the parameters about a failed order retrieved by the MetaEditor debugger:

stoploss: 0.64839
takeprofit: 0.64751
ticksize:0.00001

Ask: 0.64801
Point: 0.00001
Digits: 5
MODE_STOPLEVEL: 0.0

These are the OrderSend parameters:

Symbol: AUDUSD
Type: OP_SELL
Volume: 0.01
Price: Bid
SL: 0.64839
TP: 0.64751
Slippage: 5
 
ducarpit #:

Sorry, i wrote the wrong values (a little confusion due to the multiple test), these are the parameters about a failed order retrieved by the MetaEditor debugger:

Could you please provide all relevant code, such that we can actually reproduce the issue.

Maybe we can find a solution to your problem this way.

Edit:
Hard-nose the response from Telegram, so that we actually have the string
 
Dominik Egert #:
Could you please provide all relevant code, such that we can actually reproduce the issue.

Maybe we can find a solution to your problem this way.

Edit:
Hard-nose the response from Telegram, so that we actually have the string

This is the code to open the order:

      int slippage = 10;
      
      // get the broker TICKSIZE to correcly round the StopLoss and the TakeProfit value
      double tickSize = MarketInfo(Symbol(), MODE_TICKSIZE);
      
      double stoploss = RoundPriceToStep(StringToDouble(order_params[4]), tickSize);
      double takeprofit = RoundPriceToStep(StringToDouble(order_params[5]), tickSize);

      RefreshRates();
      
      //double ask = Ask;
      //double mode_stoplevel = MarketInfo(order_params[0], MODE_STOPLEVEL);
      //double point = Point;
      //int digits = Digits;

      // send the order and check the result
      int order_result = OrderSend(order_params[0],
         order_params[1],
         StringToDouble(order_params[2]),
         order_params[3],
         slippage,
         stoploss,
         takeprofit
      );
      

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

The parameters are in a string array (called order_params) where each row is a parameter.

In Telegram i send a command like this:

/ordersend AUDUSD;OP_SELL;0.01;Bid;0.64839;0.64751

So the parameters are separeted by ';' and then they are splittend in the string array.

The sintax is:

/ordersend <symbol>;<cmd>;<volume>;<price>;<sl>;<tp>

and the function to split:

string order_params[10];

// if the string with the parameters is empy, the BOT will show the help message
if (StringCompare(parameters,"") == 0)
        return BotOrderSendHelp();

// i need to split the parameters to elaborate them
        int i = StringSplit(parameters, StringGetCharacter(";",0), order_params);

Christian


PS: the function RoundPriceToStep was suggested by the user @amrali above

 
ducarpit #:

This is the code to open the order:

The parameters are in a string array (called order_params) where each row is a parameter.

In Telegram i send a command like this:

So the parameters are separeted by ';' and then they are splittend in the string array.

The sintax is:

and the function to split:

Christian


PS: the function RoundPriceToStep was suggested by the user @amrali above

could you make it be compilable, or at least include all involved code.

This is not enough.

Edit:

Please all relevant code involved, hardcode the telegram string as well.
 
ducarpit #:

This is the code to open the order:

The parameters are in a string array (called order_params) where each row is a parameter.

In Telegram i send a command like this:

So the parameters are separeted by ';' and then they are splittend in the string array.

The sintax is:

and the function to split:

Christian


PS: the function RoundPriceToStep was suggested by the user @amrali above

double RoundPriceToStep(const double price, const double step)
  {
   #define AdjustValue(value) StringToDouble(StringFormat("%.15g", value))
   double result = MathRound(AdjustValue(price / step)) * step;
   return AdjustValue(result);
  }

bool CheckSLTP(const string symbol, const int cmd, const double sl, const double tp)
  {
   double bid      = MarketInfo(symbol, MODE_BID);
   double ask      = MarketInfo(symbol, MODE_ASK);
   double point    = MarketInfo(symbol, MODE_POINT);
   double stoplvl  = MarketInfo(symbol, MODE_STOPLEVEL);

//--- check Stops levels
   /*
      Buying is done at the Ask price                 |  Selling is done at the Bid price
      ------------------------------------------------|----------------------------------
      TakeProfit        >= Bid                        |  TakeProfit        <= Ask
      StopLoss          <= Bid                        |  StopLoss          >= Ask
      TakeProfit - Bid  >= SYMBOL_TRADE_STOPS_LEVEL   |  Ask - TakeProfit  >= SYMBOL_TRADE_STOPS_LEVEL
      Bid - StopLoss    >= SYMBOL_TRADE_STOPS_LEVEL   |  StopLoss - Ask    >= SYMBOL_TRADE_STOPS_LEVEL
   */
   if(tp > 0)
     {
      if((cmd == OP_BUY) && tp - bid < stoplvl * point)
         //Print("ERROR: wrong buy tp detected ", DoubleToString(tp,8));
         return(false);

      if((cmd == OP_SELL) && ask - tp < stoplvl * point)
         //Print("ERROR: wrong sell tp detected ", DoubleToString(tp,8));
         return(false);
     }

   if(sl > 0)
     {
      if((cmd == OP_BUY) && bid - sl < stoplvl * point)
         //Print("ERROR: wrong buy sl detected ", DoubleToString(sl,8));
         return(false);

      if((cmd == OP_SELL) && sl - ask < stoplvl * point)
         // Print("ERROR: wrong sell sl detected ", DoubleToString(sl,8));
         return(false);
     }

   return(true);
  }

string do_trade()
  {
   //string parameters = "/ordersend <symbol>;<cmd>;<volume>;<price>;<sl>;<tp>"
   string parameters = "/ordersend AUDUSD;OP_SELL;0.01;Bid;0.64839;0.64751";

   string order_params[10];

// if the string with the parameters is empy, the BOT will show the help message
   if(StringCompare(parameters,"") == 0)
      return BotOrderSendHelp();

   int replaced = StringReplace(parameters, "/ordersend ", NULL);
   if(replaced != 1)
      return BotOrderSendHelp();

// i need to split the parameters to elaborate them
   int i = StringSplit(parameters, StringGetCharacter(";",0), order_params);
   if(i != 6)
      return BotOrderSendHelp();

   string symbol     = order_params[0];
   int cmd           = order_params[1] == "OP_BUY" ? OP_BUY : order_params[1] == "OP_SELL" ? OP_SELL : -1;
   double volume     = StringToDouble(order_params[2]);
   double price      = StringToDouble(order_params[3]);
   double stoploss   = StringToDouble(order_params[4]);
   double takeprofit = StringToDouble(order_params[5]);

   int slippage = 10;

   double tickSize = MarketInfo(symbol, MODE_TICKSIZE);

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

   if(!CheckSLTP(symbol, 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);

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