== operation

 
Is it accurate to use the == operation to compare two doubles? For example, the average value (Moving Average) and the Open value of the Bar
 
No. See explanations
 
nguye235:
Is it accurate to use the == operation to compare two doubles? For example, the average value (Moving Average) and the Open value of the Bar.

Read this ? Can price != price ?
 

Interesting. In my code i have:

if(High[0] == Open[0] && Close[0] == Open[0]){
Alert("Message");
}

The condition satisfied when current Bar/Candle end, and new Bar/Candle just appear. I attach this to 11 charts and only few of them works. Among those, the time the alert appear are all different too. few appear right after the new candle appear. Some appear after 30 secs passed.... Those High[], Low[], Open[] are all predefined variables, so I thought it OK to compare those. Is there anyway to do this test more efficiently?

 
nguye235:

Interesting. In my code i have:

The condition satisfied when current Bar/Candle end, and new Bar/Candle just appear. I attach this to 11 charts and only few of them works. Among those, the time the alert appear are all different too. few appear right after the new candle appear. Some appear after 30 secs passed.... Those High[], Low[], Open[] are all predefined variables, so I thought it OK to compare those. Is there anyway to do this test more efficiently?


Sure. You could have more efficient test if you print more info than "Message". Try to add more values - open, low, high, close and volume.
 
nguye235:

Interesting. In my code i have:

The condition satisfied when current Bar/Candle end, and new Bar/Candle just appear. I attach this to 11 charts and only few of them works. Among those, the time the alert appear are all different too. few appear right after the new candle appear. Some appear after 30 secs passed.... Those High[], Low[], Open[] are all predefined variables, so I thought it OK to compare those. Is there anyway to do this test more efficiently?

Yes there is . . . read and understand this ? Can price != price ?
 
Well, comparing doubles is safe if not preceded by calculation, i.e. (Open[0] == Close[0]) or (Ask == 1.3232) is OK, while (Open[0] == Ask+ 0.0001 ) may fail.
 
Ovo:
Well, comparing doubles is safe if not preceded by calculation, i.e. (Open[0] == Close[0]) or (Ask == 1.3232) is OK, while (Open[0] == Ask+ 0.0001 ) may fail.

Even comparison with constants is risky... I'd also, personally, not risk doing Open[0] == Close[0] ..

After OP digests the link Raptor posted, My view is :


Generally I would advise the following (Others may have their own approach :) ).

- Avoid comparing doubles for equality or inequality, use < or > where feasible/logical in your code.

- However, Sometimes you do want >= or <= (rather than < or > ) to maximise pips etc, in which case ...

In the rare cases you specifically care about equality ( =, !=, >=, <= ) then compare using a tolerance, either

- round both numbers to nearest point and then compare with a small tolerance (eg Point/2 or even smaller)

- OR compare unrounded numbers with a bigger tolerance (which would be almost equal to your rounding size - eg just under 'Point' for prices) (ala Thirteen :) )

Eg if waiting for price to meet a MA, the MA is likely some arbitrary 'fluffy' number and unlikely to be a strict price (number of pips), so I would just use

if (price >= MA) ...

However if waiting for price to hit another specific price, eg previous candle OHLC or a Pivot line, then I think the Pivot Price is a better defined value than a MA (in terms of precise Pips/Points), I guess a lot of people may have pending orders sitting on them etc. and market may react at that price point, and in that case you may want to compare with a tolerance to be sure you dont miss the match, so in that case I might use:

if (ge(price,Pivot))...

Where ge() is a user defined 'greater or equal' function to compare price and Pivot within a tolerance. But then again I might still not care than much and just use (price >= Pivot). I would (in order of preference)

  • whenever possible, try to use < and > without a tolerance,
  • maybe a tolerance for ( >= . <= ) depending on what I'm comparing and if it might mean losing pips - eg if two very specific price points (eg other OHLC, Pivots?) I'd use a tolerance, if comparing a price against some fluffy number from an indicator series (eg MA) then probably not...
  • if strict equality is important then always use a tolerance for direct equality(or inequality) ( = / != ),
 
ydrol:

Even comparison with constants is risky... I'd also, personally, not risk doing Open[0] == Close[0] ..

I posted the text in reaction to the code block and Raptor's reaction. It is not a recommendation for coding. I wanted to point out, that the comparison would work in this case, at least with quotes sent by the broker.

if(High[0] == Open[0] && Close[0] == Open[0]){
Alert("Message");
}
 
Ovo:

I posted the text in reaction to the code block and Raptor's reaction. It is not a recommendation for coding. I wanted to point out, that the comparison would work in this case, at least with quotes sent by the broker.


If the last tick of the old bar comes in and the next tick comes very quickly, your expert may miss the first tick of the new bar and only calculate on the second. Especially if latency is poor. That is why indicators usually recount 2 bars.
 
GumRai:

If the last tick of the old bar comes in and the next tick comes very quickly, your expert may miss the first tick of the new bar and only calculate on the second. Especially if latency is poor. That is why indicators usually recount 2 bars.

Not sure why you are quoting me. The thread is about comparison of double precision numbers.
Reason: