Calculated prices need Math.Round(x + y, Symbol.Digits) when using <= to prevent .9999999999

Created at 13 Oct 2020, 18:00
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
PR

prosteel1

Joined 04.07.2018

Calculated prices need Math.Round(x + y, Symbol.Digits) when using <= to prevent .9999999999
13 Oct 2020, 18:00


I've recently resolved an issue in my code where I was using a <= operator to check the prices of a bar with a calculated price.

While I was rounding the initial values using Symbol.Digits, when I added them together, the double they were written to, had .99999999 and so prevented the = part of the <= operator from being true. >= seems to work fine.

I was not expecting this and am now going through my code looking for <= and making sure any calculated price is rounded.

It would take a fair bit of work to write an example.

While cTrader showed the values rounded using a print statement, the = part of the operator wasn't being triggered - after debugging in visual studio I could see the calculated value had the 999999999 at the end.

Any idea why adding two prices such as 1500.05 + 375.55 would result in 1875.599999999 ?

 


@prosteel1
Replies

PanagiotisCharalampous
14 Oct 2020, 07:37

Hi prosteel1,

Maybe one of the added values had these decimals as well. We cannot know the exact reason, if we do not have an example.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous