OrderModify - page 2

 
I have not worked it out yet but a long time ago a teacher told me that the representation of 0.1 in floating point representation is difficult. Recall is a bit fuzzy as to the exact 0.XX number as it was in the 1970's era.
 

A math teacher once told me that 0.9 recurring = 1

That has never made sense to me either.

 
I think he meant that when you divide 1 by 9 the answer is 0.111111111111 and as many ones as you care to type. I think a similar thing happens with floating point representation of 0.1.
 
Ickyrus:
I think he meant that when you divide 1 by 9 the answer is 0.111111111111 and as many ones as you care to type. I think a similar thing happens with floating point representation of 0.1.

Yep,

1

_ x 9 = 1 so, 1/9 = 0.111111 recurring, 0.111111 recurring x 9 = 0.9999999 recurring, hence 0.999999 recurring = 1

9

 

The same thing happens with 1/10. A floating point number a sum of 1/2 1/4 1/8 1/16 etc. The closest you can come to 1/10 in 24 bits is 0.100000001490116119384765625

See Floating point - Wikipedia, the free encyclopedia

 
dineshydv:
May be normalizedouble can help you.


You were right, I fixed the problem like this:

double sl = NormalizeDouble(OrderStopLoss(),Digits);
double openprice = NormalizeDouble(OrderOpenPrice(),Digits);

Now it works properly with

if(sl > openprice+5*Point)

20:52:51 2012.06.01 00:00 NewEA EURUSD,H1: open #1 sell 0.05 EURUSD at 1.2364 sl: 1.3372 tp: 1.2262 ok
20:52:52 2012.06.01 12:20 NewEA EURUSD,H1: Alert: Modify
20:52:52 2012.06.01 12:20 NewEA EURUSD,H1: modify #1 sell 0.05 EURUSD at 1.2364 sl: 1.2369 tp: 1.2263 ok
20:52:52 2012.06.01 16:21 Tester: stop loss #1 at 1.2369 (1.2366 / 1.2369)

I have used if(sl == openprice) before lots of times without normalizing the doubles, I didnt notice a problem then so I really dont know why it happend this time.

Having said that I don't remember using such comparison with sl and openprice+n*Point before though. I think somehow a tiny error was generated, smaller than 8dp, I tried to reveal it with DoubleToStr(value,8) but it just showed zeros after the expected 4dp price values.

Whatever it was, NormalizeDouble() must have removed it.

 
Can price != price ? - MQL4 forum
Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 forum
if ( sl > MathRound(openprice+5*Point)) that didnt work order still modified multiple times

Of course. price = 1.xxxx so MathRound(1.xxxx+0.0005) = MathRound(1.xxx5) = 1 and SL > 1 for your pair.

To round to the nearest 5 points use MathRound(value/(5*Points))*(5*Points)

To the nearest tick

double NormalizePrice(double p, string pair=""){
    // https://forum.mql4.com/43064#515262 zzuegg reports for non-currency DE30:
    // MarketInfo(analyze.pair,MODE_TICKSIZE) returns 0.5
    // MarketInfo(analyze.pair,MODE_DIGITS) return 1
    // Point = 0.1
    // Prices to open must be a multiple of ticksize
    if (pair == "") pair = Symbol();
    double ts = MarketInfo(pair, MODE_TICKSIZE)
    return( MathRound(p/ts) * ts );
}
double NormalizeLots(double lots, string pair=""){
    if (pair == "") pair = Symbol();
    double  lotStep     = MarketInfo(pair, MODE_LOTSTEP),
            minLot      = MarketInfo(pair, MODE_MINLOT);
    lots    = MathRound(lots/ls) * ls;
    if (lots < minLot) lots = 0;    // or minLot
    return(lots);
}