Error converting String to Double for OrderSend!

 

Hi,

i need to use the OrderSend function (MQL4 language) in my EA to open some orders and i have the stop loss and the take profit value in two different string, instead the OrderSend receive them as double value.

So i use this syntax:

      int order_result = OrderSend(order_params[0],
         order_params[1],
         StringToDouble(order_params[2]),
         order_params[3],
         slippage,
         StringToDouble(order_params[4]),
         StringToDouble(order_params[5])
      );

Unfortunatly, i always get the error 130, some errors with the stop loss and the take profit values.

I don't know why, i tried with the point and the comma character to insert the values (example 1.07637 and 1,07637).

If i run the same function by passing the parameters directly from the code it works:

      int order_result = OrderSend(order_params[0],
         order_params[1],
         StringToDouble(order_params[2]),
         order_params[3],
         slippage,
         1,07637,
         1,07637,
      );

Can anyone help me understanding why?

Thank you so much in advance

Christian

Documentation on MQL5: Trade Functions / OrderSend
Documentation on MQL5: Trade Functions / OrderSend
  • www.mql5.com
OrderSend - Trade Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Your topic has been moved to the section: MQL4 and MetaTrader 4
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
ducarpit:

Hi,

i need to use the OrderSend function (MQL4 language) in my EA to open some orders and i have the stop loss and the take profit value in two different string, instead the OrderSend receive them as double value.

So i use this syntax:

Unfortunatly, i always get the error 130, some errors with the stop loss and the take profit values.

I don't know why, i tried with the point and the comma character to insert the values (example 1.07637 and 1,07637).

If i run the same function by passing the parameters directly from the code it works:

Can anyone help me understanding why?

Thank you so much in advance

Christian

StringToDouble will return the double representation of the value stored in string.

It will not apply the required roundings and other "fittings" of a double to be a valid parameter for OrderSend.

Please do a search on why "NormalizeDouble" is usually wrong, and how to do it properly.
 
Dominik Egert #:
StringToDouble will return the double representation of the value stored in string.

It will not apply the required roundings and other "fittings" of a double to be a valid parameter for OrderSend.

Please do a search on why "NormalizeDouble" is usually wrong, and how to do it properly.

Hi, i tried to read something about this without success.

I'm a beginner with the EA and it's not very simple for me. I tried to stamp the variables value befor call the OrderSend with the Print functin but seems they are right (on the screen, i don't know if they are wrong then in the program variable).

I understood i need to use a value with 5 decimal digits (in the manually example above it works) but i didn't understand why the NormalizaDouble didn't work.

Christian

 
ducarpit #:

Hi, i tried to read something about this without success.

I'm a beginner with the EA and it's not very simple for me. I tried to stamp the variables value befor call the OrderSend with the Print functin but seems they are right (on the screen, i don't know if they are wrong then in the program variable).

I understood i need to use a value with 5 decimal digits (in the manually example above it works) but i didn't understand why the NormalizaDouble didn't work.

Christian

Try this post, it summarizes everything you need to know.

NormalizeDouble not working
NormalizeDouble not working
  • 2022.01.09
  • www.mql5.com
Hi everyone I'm not sure why NormalizeDouble is not working here. Can anyone suggest a fix to this? ***...
 
Dominik Egert #:
Try this post, it summarizes everything you need to know.

Hi, i tried to read all the article and made some tests:

my broker is a 5 digits broker and i verified that with 

MarketInfo(order_params[0],MODE_TICKSIZE)

with the result 0,00001.

So i tried this sintax to open an order with OrderSend by adding the NormalizeDouble with 5 digits:

      int order_result = OrderSend(order_params[0],
         order_params[1],
         StringToDouble(order_params[2]),
         order_params[3],
         slippage,
         NormalizeDouble(StringToDouble(order_params[4]),5),
         NormalizeDouble(StringToDouble(order_params[5]),5),
      );

I read (and i hope i understood correctly) i have to round the stop loss value according to the broker TICKSIZE, but in my case it is 0,00001 so it shouldn't be necessary.

Unfortunatly i get the same error 130 :(

What i didn't understand in your opinion? I think something happen during the conversion from string to double but i don't understand what...

Christian

 
ducarpit #: So i tried this sintax to open an order with OrderSend by adding the NormalizeDouble with 5 digits:
         NormalizeDouble(StringToDouble(order_params[4]),5),
         NormalizeDouble(StringToDouble(order_params[5]),5),

You used NormalizeDouble, It's use is usually wrong, as it is in your case.

  1. Floating point has an infinite number of decimals, it's you were not understanding floating point and that some numbers can't be represented exactly. (like 1/10.)
              Double-precision floating-point format - Wikipedia, the free encyclopedia

    See also The == operand. - MQL4 programming forum (2013)

  2. Print out your values to the precision you want with DoubleToString - Conversion Functions - MQL4 Reference.

  3. SL/TP (stops) need to be normalized to tick size (not Point) — code fails on non-currencies.
              On 5Digit Broker Stops are only allowed to be placed on full pip values. How to find out in mql? - MQL4 programming forum #10 (2011)

    And abide by the limits Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial and that requires understanding floating point equality Can price != price ? - MQL4 programming forum (2012)

  4. Open price for pending orders need to be adjusted. On Currencies, Point == TickSize, so you will get the same answer, but it won't work on non-currencies. So do it right.
              Trailing Bar Entry EA - MQL4 programming forum (2013)
              Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 programming forum (2012)

  5. Lot size must also be adjusted to a multiple of LotStep and check against min and max. If that is not a power of 1/10 then NormalizeDouble is wrong. Do it right.
              (MT4 2013)) (MT5 2022))

  6. MathRound() and NormalizeDouble() are rounding in a different way. Make it explicit.
              MT4:NormalizeDouble - MQL5 programming forum (2017)
              How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum (2017)

  7. Prices you get from the terminal are already correct (normalized).

  8. PIP, Point, or Tick are all different in general.
              What is a TICK? - MQL4 programming forum (2014)

 
William Roeder #:

You used NormalizeDouble, It's use is usually wrong, as it is in your case.

  1. Floating point has an infinite number of decimals, it's you were not understanding floating point and that some numbers can't be represented exactly. (like 1/10.)
              Double-precision floating-point format - Wikipedia, the free encyclopedia

    See also The == operand. - MQL4 programming forum (2013)

  2. Print out your values to the precision you want with DoubleToString - Conversion Functions - MQL4 Reference.

  3. SL/TP (stops) need to be normalized to tick size (not Point) — code fails on non-currencies.
              On 5Digit Broker Stops are only allowed to be placed on full pip values. How to find out in mql? - MQL4 programming forum #10 (2011)

    And abide by the limits Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial and that requires understanding floating point equality Can price != price ? - MQL4 programming forum (2012)

  4. Open price for pending orders need to be adjusted. On Currencies, Point == TickSize, so you will get the same answer, but it won't work on non-currencies. So do it right.
              Trailing Bar Entry EA - MQL4 programming forum (2013)
              Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 programming forum (2012)

  5. Lot size must also be adjusted to a multiple of LotStep and check against min and max. If that is not a power of 1/10 then NormalizeDouble is wrong. Do it right.
              (MT4 2013)) (MT5 2022))

  6. MathRound() and NormalizeDouble() are rounding in a different way. Make it explicit.
              MT4:NormalizeDouble - MQL5 programming forum (2017)
              How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum (2017)

  7. Prices you get from the terminal are already correct (normalized).

  8. PIP, Point, or Tick are all different in general.
              What is a TICK? - MQL4 programming forum (2014)

Maybe, (MAYBE), i solved the problem by reading your articles:

First i get the broker TICKSIZE and then i rounded the value with that:

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

Now, the OrderSend seems work! :)

 
ducarpit #:

Maybe, (MAYBE), i solved the problem by reading your articles:

First i get the broker TICKSIZE and then i rounded the value with that:

Now, the OrderSend seems work! :)


Isn't it nice when following people's advice and things begin to work.
 
Drop the double to string to stringToDouble or reread the links provided.
 
William Roeder #:
Drop the double to string to stringToDouble or reread the links provided.

I didn't understand your suggestion, sorry!

I have the stop loss value as a string so i need to convert it as a double to make the other adjustments;

if i drop the stringtodouble command then the compiler will do an implicit conversion.

Did you refer to this?

Christian