Error converting String to Double for OrderSend! - page 2

 
ducarpit #:

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

No. What he meant is, don't have your SL as string in the first place.

Why are you storing it as a string?
 
Dominik Egert #:
No. What he meant is, don't have your SL as string in the first place.

Why are you storing it as a string?

Oh sorry, at the first i didin't understand the question, i try to explain why i have the SL parameter as a string....

I open the order with Telegram, i made a simple bot (for me) that is connected to my MetaTrader Demo account;

to open an order, i have to send a command to the bot followed by the OrderSend parameters separated by ';', this is an example:

/sendorder AUDCAD;OP_BUY;0.01;Ask;1.08395;1.08475;

So i receive all the command and then i need to split the parameters with the StringSplit function in an array of string.

This is why i have all the parameters (include the SL) as a string...

Tell me if i explained the reason in a clear way..

 
ducarpit #:

Oh sorry, at the first i didin't understand the question, i try to explain why i have the SL parameter as a string....

I open the order with Telegram, i made a simple bot (for me) that is connected to my MetaTrader Demo account;

to open an order, i have to send a command to the bot followed by the OrderSend parameters separated by ';', this is an example:

So i receive all the command and then i need to split the parameters with the StringSplit function in an array of string.

This is why i have all the parameters (include the SL) as a string...

Tell me if i explained the reason in a clear way..

Yes. it's clear.

You should do sanity checks on the inputs from telegram, so that you can catch faulty inputs.

After filtering your received input, prepare the data to send it to OrderSend, then call order send with ready made data.
 
Dominik Egert #:
Yes. it's clear.

You should do sanity checks on the inputs from telegram, so that you can catch faulty inputs.

After filtering your received input, prepare the data to send it to OrderSend, then call order send with ready made data.
Something like StringIsNumeric() may help to check the string before conversion.

 
amrali #:
Something like StringIsNumeric() may help to check the string before conversion.

Unfortunatly this library is for MT5 (i use MT4) but the ideaa is good :)

 

Unfortunatly, it doens't work always :(

Despite i adjusted the stoploss value with the MarketInfo function and MODE_TICKET parameter, in some cases during the moltiplication with tickSize, the double value for stoploss return with more than five decimal number.

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.

Why the StringToDouble function do this? I think all the problems start from here. Is there a way to delete all the decimal numbers over the fifth value?

I also tried with MthFloor and MathRound, without success...

 
ducarpit #:

Unfortunatly, it doens't work always :(

Despite i adjusted the stoploss value with the MarketInfo function and MODE_TICKET parameter, in some cases during the moltiplication with tickSize, the double value for stoploss return with more than five decimal number.

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.

Why the StringToDouble function do this? I think all the problems start from here. Is there a way to delete all the decimal numbers over the fifth value?

I also tried with MthFloor and MathRound, without success...

Actually, yes, all problems start from there. But if you look deeper into it, you will begin to understand the true nature of this problem.

It is technically unsolvable, because double has an infinite amount of numbers between 0.0 and 1.0, but your computer only has 64bit to store these values.

Please read about floating point representation in computers, and begin to understand the things William has posted about your problem.

You cannot cut off the decimals you ask for. It's impossible. And it is ok, not an error.
 
Dominik Egert #:
Actually, yes, all problems start from there. But if you look deeper into it, you will begin to understand the true nature of this problem.

It is technically unsolvable, because double has an infinite amount of numbers between 0.0 and 1.0, but your computer only has 64bit to store these values.

Please read about floating point representation in computers, and begin to understand the things William has posted about your problem.

You cannot cut off the decimals you ask for. It's impossible. And it is ok, not an error.

I read and re-read the article more than one time, i'm not a developer  but with lot effort i think i understand the concept.

Basically, i am interesting to know if there are a solution to convert a string in a double and clean it from the decimal values that cause the error.

Because if the asnwer is YES then i can try to study how, but if the answer is NO anyway the i interrupt the project because i think it's stupid studying for something impossible and unsolvable.

So, if you can, do you think is there a possible solution or it is unsolvable? In onor to the community spirit, today this is an informatoin for me, tomorrow it will be an informations for others.

Thnks you so much

Christian

 

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.

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

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

If you want to see the correct number of digits, convert it to a string with the correct/wanted accuracy.
          question about decima of marketinfo() - MQL4 programming forum (2016)

 
ducarpit #:

I read and re-read the article more than one time, i'm not a developer  but with lot effort i think i understand the concept.

Basically, i am interesting to know if there are a solution to convert a string in a double and clean it from the decimal values that cause the error.

Because if the asnwer is YES then i can try to study how, but if the answer is NO anyway the i interrupt the project because i think it's stupid studying for something impossible and unsolvable.

So, if you can, do you think is there a possible solution or it is unsolvable? In onor to the community spirit, today this is an informatoin for me, tomorrow it will be an informations for others.

Thnks you so much

Christian

Well, it is not possible to remove those fractions, but it is very possible to make double values that are valid for sending them to OrderSend.

Believe it or not, it has been done hundreds of times before.

As a hint, maybe it helps studying some EAs from code base...
Reason: