Download MetaTrader 5
To add comments, please log in or register
Publish your article. We will translate it into other languages!
d.bignotti
71
d.bignotti 2016.10.14 09:54 
void OnStart()
  {

   double Lots=((1860.03*1.0/100)/(114.542-114.511))/(0.87/0.001);
   double NormLotsRound=MathRound(Lots/0.01)*0.01;
   double NormLotsDouble=NormalizeDouble(Lots,2);

   Print("Lots is, ",Lots);

   Print("MathRound(Lots/0.01) is, ",MathRound(Lots/0.01));

   Print("Lots with MathRound, ",NormLotsRound);
   
   Print("Lots with NormalizeDouble, ",NormLotsDouble);

  }

 

I'm running this script to test a function in my EA that auto calculate lot based on % and a Delta, i wrote the revelant code only with numeric value, it's the first time i get this kind of problem running it for several week.

 

If you run the Script i'm getting a Lots that is 0.6896662958841845,

 

when i'm rounding it with Lot step of my broker (0.01) i get 69.0 that is right.

 

But when then, i'm multiplying it for lot step (again 0.01) i get 0.6900000000000001.  Why this is happening?

 

Tried also using NormalizeDouble and i get wrong value, what should i do? The correct lot should be 0.69!

whroeder1
13602
whroeder1 2016.10.14 12:06  
d_bignotti: i get 0.6900000000000001.  Why this is happening?
  1. Floating point has infinite number of decimals, it's your 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 forum
  2. Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong
  3. You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
    • Account Balance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the SPREAD, and DeltaPerLot is usually around $10/pip)
    • Do NOT use TickValue by itself - DeltaPerLot
    • You must normalize lots properly and check against min and max.
    • You must also check FreeMargin to avoid stop out
d.bignotti
71
d.bignotti 2016.10.14 12:39  
WHRoeder:
  1. Floating point has infinite number of decimals, it's your 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 forum
  2. Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong
  3. You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
    • Account Balance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the SPREAD, and DeltaPerLot is usually around $10/pip)
    • Do NOT use TickValue by itself - DeltaPerLot
    • You must normalize lots properly and check against min and max.
    • You must also check FreeMargin to avoid stop out

I don not use normalizedouble in EA code, this script was made just to test why i get wrong number, lot is beign adjusted with lotstep, max lot and min lot from broker and i check for freemargin also.

 I'm going to read everything you psoted, i hope i can get everything working, i have no coding background, so a lot of "easy" thing are out of my knowledge and English is not my native language.

 Thank you for helping. I'll write back if i need more practical examples.

d.bignotti
71
d.bignotti 2016.10.14 14:08  
WHRoeder:
  1. Floating point has infinite number of decimals, it's your 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 forum
  2. Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong
  3. You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
    • Account Balance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the SPREAD, and DeltaPerLot is usually around $10/pip)
    • Do NOT use TickValue by itself - DeltaPerLot
    • You must normalize lots properly and check against min and max.
    • You must also check FreeMargin to avoid stop out

I readed everything, point 2. and 3. are fully implemented in my code, the script i posted is just with plain number i get from ea with no "safety" logic just to test why i get this strange numbers.

I readed Can price != price ? - MQL4 forum, but i do not understand what i should do in my case, i don't need to compare 2 double, it's clear how to do it the topic. 

I have a double, 69.0 in this case, multiplied by *0.01, and i should get 0.69, isntead i get 0.6900000000000001.

What kind of function or calculation should i use to turn this double with 16th decimal in something that my OrderModify function can accept?   

whroeder1
13602
whroeder1 2016.10.14 18:02  
d_bignotti: I have a double, 69.0 in this case, multiplied by *0.01, and i should get 0.69, isntead i get 0.6900000000000001.
No you shouldn't. Read and understand #1
/
To add comments, please log in or register