Pine coders can now use dynamically calculated lengths in the following functions:
alma() change() highest() highestbars() linreg() lowest() lowestbars() mom() sma() sum() vwma() wma()
Series lengths will make it easier to create adaptive Pine indicators using calculations based on variable periods. See here, for example, how we use volatility to shorten or lengthen the ALMA period on the thick line and compare it to the static ALMA shown with the thin line:
//@version=4 study("Adaptive ALMA", "AALMA", true) i_minLen = input(10, minval = 1) i_maxLen = input(60, minval = 1) i_adaptPct = input(3., minval = 0, maxval = 100) / 100.0 startingLen = avg(i_minLen, i_maxLen) var float dynamicLen = startingLen highVolatility = atr(10) > atr(40) if highVolatility dynamicLen := max(i_minLen, dynamicLen * (1 - i_adaptPct)) else dynamicLen := min(i_maxLen, dynamicLen * (1 + i_adaptPct)) dynamicAlma = alma(close, int(dynamicLen), 0.85, 6) staticAlma = alma(close, int(startingLen), 0.85, 6) maColor(_ma) => close > _ma ? highVolatility ? #40FF00ff : #40FF0060 : highVolatility ? #FF0080ff : #FF008060 plot(dynamicAlma, "Dynamic Alma", maColor(dynamicAlma), 3) plot(staticAlma, "Static Alma", maColor(staticAlma), 1) // Follow the calculated Length in the Data Window. plotchar(dynamicLen, "dynamicLength", "")
Although it may be handy to use floats to calculate lengths dynamically, please remember to cast them to an “int” before using them in function calls, as we do in our example. Also keep in mind that, as usual, these functions require evaluation on every bar, so should be used outside `for` loops, and `if` blocks not executed on every bar.
We hope this new feature you requested is useful. Please continue sending us your feedback for improvement. We build TradingView for you and love hearing what you think of these highly-requested updates to our platform.