How to keep zeros after converting from string type

 

Hello, I'm coding a basket indicator which alerts me when/if a pair from the list reaches a round number (000/500).

I've been able to obtain the last 3 digits from each pair as string (with StringLen and StringSubstr) but when i try to convert them to double or integer all the zeros got removed from the number, for example 015 becomes 15. I know this is supposed to be logical but i need those zero to be able to compare them with the round numbers. 

I also tried with NormalizeDouble and StringFormat but nothing seems to work in my case. How can i make it work?

Also if somebody has a different solution to reach my goal i'm all ears :)

Thanks.

 
Francesco Grassi:

Hello, I'm coding a basket indicator which alerts me when/if a pair from the list reaches a round number (000/500).

I've been able to obtain the last 3 digits from each pair as string (with StringLen and StringSubstr) but when i try to convert them to double or integer all the zeros got removed from the number, for example 015 becomes 15. I know this is supposed to be logical but i need those zero to be able to compare them with the round numbers. 

I also tried with NormalizeDouble and StringFormat but nothing seems to work in my case. How can i make it work?

Also if somebody has a different solution to reach my goal i'm all ears :)

Thanks.

You don't need the zero to compare numbers

015==15 will be true.

 
Keith Watford:

You don't need the zero to compare numbers

015==15 will be true.

But the problem is that it also removes the zeros after, for example 0.98100 becomes 981 and that number can't be compared with a round number.

 
Stop using strings. Compute your “round number” and just compare: double hrn = MathRoundUp(Bid, 500*_Point);
          MT4:NormalizeDouble - General - MQL5 programming forum 2017.01.04
          How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum 2017.05.19
 

I don't need to "roundup" prices, I just wanted to check when prices reach a round level like 1.90500 or 141.000.

Anyway i found a solution. I realized the problem is not happening when i convert from string to double/integer but simply MarketInfo(pair,MODE_BID) doesn't consider any zero after decimal numbers. For example now AUDCAD price is 0.98100 and if you Print its MarketInfo it just prints 0.981.

I find it a bit odd since those zeros can be used in a lot of different algorithm calculations. Maybe there is already a function to do that which i'm not aware.


Thanks anyway.

Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
Predefined Macro Substitutions - Named Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Francesco Grassi: I don't need to "roundup" prices, I just wanted to check when prices reach a round level like 1.90500 or 141.000.

Doubles are rarely equal. Understand the links in:
          The == operand. - MQL4 programming forum #2 2013.06.07

Prices can easily skip over your exact price. You need to round up to get the target price, remember it, and then wait for the market to cross (equal or above) it.
 

Yep i realized that when i was testing the indicator, infact i changed the code and now i compare the price in a range of numbers. Rather than "==" im using "(>=0 && <020) || (<=999 && >=980)".

But rounding up before the target price seems a better choice, the problem is: how i decide when to round up the number before the comparing? Should i still wait the price to reach a range before rounding up?

If you can write a small example for me it would help me understand as i'm not a professional coder.

Cheers.

Documentation on MQL5: Math Functions / MathFloor
Documentation on MQL5: Math Functions / MathFloor
  • www.mql5.com
MathFloor - Math Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Francesco Grassi:

Yep i realized that when i was testing the indicator, infact i changed the code and now i compare the price in a range of numbers. Rather than "==" im using "(>=0 && <020) || (<=999 && >=980)".

But rounding up before the target price seems a better choice, the problem is: how i decide when to round up the number before the comparing? Should i still wait the price to reach a range before rounding up?

If you can write a small example for me it would help me understand as i'm not a professional coder.

Cheers.

double x = round(price/rounding)*rounding;
then compare it to your nearest level