Initially, when I started with MQL, I would manipulate prices as "doubles". Nowadays, especially in the more complex EA's, I manipulate prices as "ints". At the very first opportunity, I convert a price into ticks:
int priceTicks = (int) round( price / tickSize );
From then on, all my calculations and manipulations are done with "ints". Not only is it more memory compact and much faster, but comparisons are much easier to handle. Doing a "priceA == priceB" for "doubles" is quite problematic, but not for "ints" because it gives exact matches. Not to mention, that in this way prices, stop sizes, etc. are ALWAYS aligned.
Then, just before I have to place or modify an order, I then convert it back:
priceTicks * tickSize
EDIT: I do the same for volume/lots by using the broker's Lot-Step.
Lot size must also be adjusted to a multiple of LotStep and check against
min and max. If that is not a power of 1/10 then
NormalizeDouble is wrong. Do it
right.
Initially, when I started with MQL, I would manipulate prices as "doubles". Nowadays, especially in the more complex EA's, I manipulate prices as "ints". At the very first opportunity, I convert a price into ticks:
From then on, all my calculations and manipulations are done with "ints". Not only is it more memory compact and much faster, but comparisons are much easier to handle. Doing a "priceA == priceB" for "doubles" is quite problematic, but not for "ints" because it gives exact matches. Not to mention, that in this way prices, stop sizes, etc. are ALWAYS aligned.
Then, just before I have to place or modify an order, I then convert it back:
EDIT: I do the same for volume/lots by using the broker's Lot-Step.
It's not only about price, though.. there are various other indicating fractions, besides the price, that one wants to follow.. and its a pain in the ***, cause formatting them via string manipulation is costly in terms of calculations
MarketInfo() is for mql4 which is the mql5 alternative??
---
Nvm: found it here SYMBOL_TRADE_TICK_SIZE
This is valid for both MQL4 and MQL5:
Why are you still insisting on using NormalizeDouble()? It is a kludge! It is useless! If you do things correctly, you don't need it!
Yes, that is unfortunately that MetaQuotes sets the wrong example for the coders!
Just as a side note.
MT4:NormalizeDouble - General - MQL5 programming forum
How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum
Just as a side note.
Good idea. :thumbsup: