NormalizePrice() any suggestions on how to do this better

 

Hello,

I have a function which I use often in my code. I would like to know if there is a better/more efficient way of doing this.

Any suggestions are welcomed.

//+------------------------------------------------------------------+
//| NormalizePrice()                                                 |
//+------------------------------------------------------------------+
double NormalizePrice(const double _price)
{ 
    // Precache init

        const static int _digits        = (int)SymbolInfoInteger(Symbol(), SYMBOL_DIGITS);
        const static int _stepsize  = (int)(SymbolInfoDouble(Symbol(), SYMBOL_TRADE_TICK_SIZE) * MathPow(10, _digits));

    // Return
    return(NormalizePrice(_price, _digits, _stepsize));
}


//+------------------------------------------------------------------+
//| NormalizePrice()                                                 |
//+------------------------------------------------------------------+
double NormalizePrice(const string symbol, const double _price)
{
    // Precache init

        static int      c_step_size     = NULL;
        static int      c_digits        = NULL;
        static string   c_symbol        = NULL;


    // Cache update
    
        if(c_symbol != symbol)
        { 
            c_symbol    = symbol;
            c_digits    = (int)SymbolInfoInteger(symbol, SYMBOL_DIGITS);
            c_step_size = (int)(SymbolInfoDouble(symbol, SYMBOL_TRADE_TICK_SIZE) * MathPow(10, c_digits));
        }
        
    // Return
    return(NormalizePrice(_price, c_digits, c_step_size));
}


//+------------------------------------------------------------------+
//| NormalizePrice()                                                 |
//+------------------------------------------------------------------+
double NormalizePrice(const double _price, const int digits, const int stepsize = 1)
{
    // Precache init

        static  double  price_div       = NULL;
        static  int     c_digits        = NULL;
        static  int     c_stepsize      = NULL;
        static  int     price_factor    = NULL;


    // Cache update
    
        if( (c_digits != digits) 
         || (c_stepsize != stepsize) )
        { 
            c_digits        = digits;
            c_stepsize      = stepsize;
            price_factor    = MathPow(10, digits + 1);
            price_div       = 1.0 / (double)price_factor;
        }


    // Calculate corrected price

        const int _ticks    = (int)MathRound(_price * price_factor);
        const int _round    = (_ticks % 10);
        const int ticks     = (_round >= 5) ? _ticks + (10 - _round) : _ticks - _round;
        const int diff      = (ticks % (c_stepsize * 10));
    
    // Return
    return((ticks - diff) * price_div);
}


 

#include <Trade\SymbolInfo.mqh>

CSymbolInfo         m_symbol;

m_symbol.NormalizePrice(your_price);

 
Roberto Jacobs #:

#include <Trade\SymbolInfo.mqh>

CSymbolInfo         m_symbol;

m_symbol.NormalizePrice(your_price);

Oh yes. - Thank you.

Very good hint to take a look at the std-lib.

would have saved me from asking.... :-)

 
Dominik Christian Egert #:

Oh yes. - Thank you.

Very good hint to take a look at the std-lib.

would have saved me from asking.... :-)

You like complication so the "Standard lib" is of no help.
 
Alain Verleyen #:
You like complication so the "Standard lib" is of no help.

Yes. - So true. I use it as my learning process, do it on my own, then see if there has been an attempt made to solve the task. 

Its to keep up creativity. While you figure out a path to solve something, sometimes, without a prebiased view, you come up wiht good solutions, nobody has done before. So, thats why I (mostly) go the hard way. Always choose the path least walked.

In fact, I am no big fan of std-libs, in general. Two main reasons, one mentioned oabove, learning, and the other, because I can change the code, if its not as I like it to be.

 
Dominik Christian Egert #:

Yes. - So true. I use it as my learning process, do it on my own, then see if there has been an attempt made to solve the task. 

Its to keep up creativity. While you figure out a path to solve something, sometimes, without a prebiased view, you come up wiht good solutions, nobody has done before. So, thats why I (mostly) go the hard way. Always choose the path least walked.

In fact, I am no big fan of std-libs, in general. Two main reasons, one mentioned oabove, learning, and the other, because I can change the code, if its not as I like it to be.

Understandable.

The "standard" library has nothing standard in my opinion, a lot of bad code and bugs in there, mostly I don't use it. The Generic library is pretty good though.

 

Dominik, you can take a look at my library

https://www.mql5.com/en/code/20822 

 
Alain Verleyen #:

Understandable.

The "standard" library has nothing standard in my opinion, a lot of bad code and bugs in there, mostly I don't use it. The Generic library is pretty good though.

Hmm... Any idea why they don't fix at least the bugs?

(don't they see the threat of e.g. TradingView when they would start to enable a reasonable automatic trading?
Which Broker would then spend a fortune for a aging product/language like MTx when even the quality leaves much to be desired?)

 
Thomas S. #:

Hmm... Any idea why they don't fix at least the bugs?

(don't they see the threat of e.g. TradingView when they would start to enable a reasonable automatic trading?
Which Broker would then spend a fortune for a aging product/language like MTx when even the quality leaves much to be desired?)

Aging? MQL's is way more capable than the TradingView's language, even with the bugs, the only better alternative is to just
write directly in C++.

The standard library serves only to show the capabilities of the language, but most experienced programmers are either using
alternative open source ones or private solutions.

It is not a language aimed at novices and that's good, it gives power to those who know how to code instead of restricting them
because feature X would be too complex for a novice to understand.

 
Yes, I fully agree. Still missing the feature 'return a reference". If that's even a deal for a pine coder...
 
amrali #:

Dominik, you can take a look at my library

https://www.mql5.com/en/code/20822 

TThank you. Real quality code.


Reason: