Sometimes, in several trading platforms, it is not possible to get value from different kind of average functions (like simple/exponential moving average or ATR or whatever) before the current bar number reaches the value of Length parameter.
For example, if you have ATR(15) and you add that on chart, till 15th bar, the built-in function wouldn’t return any value (NAN). That is expected behavior, because the platform need enough data to aggregate to make correct calculation, and before that, it doesn’t give you the “estimations”.

However, there are cases, when people need “estimated/approximated” value, because something is better than nothing. For that purposes, we use the following formula to calculate the values before current-bar reaches to the Length value:

    correct_value_ = atr(length)
    tr_ = bar_index==0? high - low : max(high - low, max(abs(high - close[1]), abs(low - close[1])))
    NOT_STARTED_ = bar_index<length-1
    approx_value_ = 0.0, approx_value_ := NOT_STARTED_ ? ( bar_index==0 ? tr_ : (approx_value_[1]*bar_index + tr_) /(bar_index+1) ) : na
    result_ = NOT_STARTED_ ? approx_value_ : correct_value_

(Note: this is formula for Pinescript, however you can adopt for any other language)

Leave a Reply

Your email address will not be published. Required fields are marked *