Introduction
The AutoLength Moving Average is a dynamic adaptation of the traditional moving average that adjusts its length based on market conditions (author Zeiierman). Unlike standard moving averages that rely on fixed periods, this indicator intelligently modifies its length using a counter-based approach, allowing it to adapt to varying market trends. This feature makes it especially useful for traders who need a responsive yet smooth trend-following tool.
Concept and Functionality
The AutoLength Moving Average is designed to dynamically adjust its smoothing period, ensuring that it reacts appropriately to market fluctuations. Here’s how it works:
- It starts with a base length for the moving average.
- A counter tracks market conditions and increases or resets based on predefined reset conditions.
- The length of the moving average dynamically extends based on the counter’s value, up to a maximum limit.
- This results in a moving average that expands during strong trends and contracts during periods of uncertainty, optimizing its responsiveness to price action.
Key Components
- Dynamic Length Calculation: The moving average length expands progressively up to a maximum dynamic length, depending on market conditions.
- Counter Mechanism: A counter increases or resets based on the chosen reset condition, affecting the adaptive length of the moving average.
- Trend Detection: The indicator determines trend direction by analyzing moving average crosses and confirming trends over a given period.
This approach allows the AutoLength Moving Average to smooth price movements while being flexible enough to capture changes in trend direction efficiently.
Reset Conditions
A critical aspect of this indicator is its ability to reset or reverse the counter based on different market conditions. The trader can select from eight different reset conditions:
- Moving Average Slope: The counter resets when the slope of the moving average changes direction.
- RSI Overbought/Oversold: A reset occurs when the RSI exceeds overbought or drops below oversold levels.
- Volume SMA: The counter resets when the volume exceeds its moving average, indicating an increase in trading activity.
- Bollinger Bands Breakout: A reset happens when the price crosses above or below the Bollinger Bands.
- MACD Crossover: The reset is triggered when the MACD line crosses above or below the signal line.
- Stochastic Overbought/Oversold: A reset occurs based on the stochastic oscillator exceeding thresholds.
- CCI Overbought/Oversold: The Commodity Channel Index is used to detect extreme market conditions.
- Momentum (Rate of Change): The counter resets when the rate of change of price momentum turns positive or negative.
By selecting the appropriate reset condition, traders can fine-tune the indicator to align with their trading style, whether they prefer trend-following or mean-reversion strategies.
Visualization and Signals
To enhance usability, the AutoLength Moving Average includes visual elements to indicate key trading signals:
- Trend Confirmation:
- A strong uptrend is confirmed when the indicator detects a consistent upward trend over a defined period.
- A downtrend is confirmed similarly for downward movements.
- Trend Color Coding:
- Green (Confirmed Uptrend): When the uptrend is validated.
- Red (Confirmed Downtrend): When the downtrend is validated.
- Yellow (Uncertain Trend): If the trend is not confirmed.
- Reversal Signals:
- An orange marker appears when an uptrend starts.
- A lime marker appears when a downtrend starts.
These signals help traders quickly identify trend shifts and adjust their strategies accordingly.
Configuration and Customization
To optimize the indicator for different market conditions, traders can adjust several parameters:
Primary Inputs:
- Base Length (Default: 50): Initial period for the moving average.
- Max Dynamic Length (Default: 350): The maximum expansion allowed for the adaptive length.
- Counter Threshold (Default: 70): The limit before the counter stops increasing.
- Reverse Counter (Default: False): Enables or disables counter reversal after reaching the threshold.
- Trending Confirmation (Default: 10): The number of periods required to confirm a trend.
Reset Condition Settings:
- RSI Length, Overbought & Oversold Levels
- Volume SMA Length
- Bollinger Bands Length & Multiplier
- MACD Fast, Slow & Signal Periods
- Stochastic Length, Overbought & Oversold Levels
- CCI Length, Overbought & Oversold Levels
- Momentum Length
Each of these settings allows users to adapt the AutoLength Moving Average to their preferred trading methodology.
ProBuilder Code Implementation
Below is the full ProBuilder code for the AutoLength Moving Average, which can be copied and used directly in ProRealTime:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
//---------------------------------------------// //PRC_AutoLength Moving Average (Zeiierman) //version = 0 //10.03.2025 //Iván González @ www.prorealcode.com //Sharing ProRealTime knowledge //---------------------------------------------// // Inputs //---------------------------------------------// src = close // Source price used for calculations baseLen = 50 // Base length for the Moving Average maxDynLen = 350 // Maximum dynamic length for MA counterbreak = 70 // Threshold for counter reversal reverseCounter = 0 // 1=true, 0=false -> Reverses counter after break trendingCol = 10 // Length of trend confirmation // Selection of Reset Condition Type resetConditiontype = 1 // 1 = Based on Moving Average Slope // 2 = Based on RSI Overbought/Oversold levels // 3 = Based on Volume SMA // 4 = Based on Bollinger Bands (Price Breakout) // 5 = Based on MACD Line and Signal Line Crossover // 6 = Based on Stochastic Overbought/Oversold levels // 7 = Based on CCI Overbought/Oversold levels // 8 = Based on Momentum (Rate of Change) // Ma slopeOB = 50 // Length for MA slope calculation //RSI rsiLength = 14 // RSI period length rsiOB = 60 // RSI Overbought level rsiOS = 40 // RSI Oversold level //Volume volSmaLength = 20 // Volume SMA length //Bollinger bblength = 200 // Bollinger Bands length bbMult = 1.1 // Bollinger Bands multiplier //Macd macdfastLength = 50 // Fast length for MACD calculation macdSlowLength = 100 // Slow length for MACD calculation macdSignalLength = 25 // Signal length for MACD //Stochastic stochLength = 50 // Stochastic period length stochOB = 60 // Stochastic Overbought level stochOS = 40 // Stochastic Oversold level //CCI cciLength = 100 // Commodity Channel Index (CCI) length cciOB = 10 // CCI Overbought threshold cciOS = -10 // CCI Oversold threshold //Momentum momentumLength = 140 // Momentum period length //Trend length = 20 // Trend smoothing length trendSmoothing = 0.8 // Smoothing weight for trend adaptation trending = 20 // Trending periods for confirmation //---------------------------------------------// // Calculate indicators //---------------------------------------------// // RSI rsivalue=rsi[rsilength](src) // Volume SMA volSMA = average[volSmaLength,2](volume) // MACD mymacdline=MACDline[macdfastLength,macdSlowLength,macdSignalLength](src) mysignalline=MACDSignal[macdfastLength,macdSlowLength,macdSignalLength](src) // Stochastic stochK=Stochastic[stochLength,3](close) // CCI ccivalue=cci[cciLength](src) // Momentum (rate of change) mymomentum=roc[momentumLength](src) // Bollinger bands basisBB=average[bblength](src) bbStd=std[bblength](src) bbupper=basisBB+bbMult*bbStd bblower=basisBB-bbMult*bbStd //---------------------------------------------// // Dynamic MA and counter variables //---------------------------------------------// once counter=1 once prevState=0 dynLen=min(baseLen+(counter-1),maxDynLen) ma=average[dynLen](src) //---------------------------------------------// // Get the current condition state //---------------------------------------------// once state=0 if resetConditiontype=1 then if ma>ma[slopeOB] then state=1 elsif ma<ma[slopeOB] then state=-1 else state=0 endif elsif resetConditiontype=2 then if rsiValue>rsiOB then state=1 elsif rsiValue<rsiOS then state=-1 else state=0 endif elsif resetConditiontype=3 then if average[20](volume)>volSMA then state=1 elsif average[20](volume)<volSMA then state=-1 else state=0 endif elsif resetConditiontype=4 then if close>bbupper then state=1 elsif close<bblower then state=-1 else state=0 endif elsif resetConditiontype=5 then if mymacdline>mysignalline then state=1 elsif mymacdline<mysignalline then state=-1 else state=0 endif elsif resetConditiontype=6 then if stochk>stochOB then state=1 elsif stochk<stochOS then state=-1 else state=0 endif elsif resetConditiontype=7 then if ccivalue>cciOB then state=1 elsif ccivalue<cciOS then state=-1 else state=0 endif elsif resetConditiontype=8 then if mymomentum>0 then state=1 elsif mymomentum<0 then state=-1 else state=0 endif endif currentState = state //---------------------------------------------// // Update the counter with reverse counting option //---------------------------------------------// once counterIncreasing=1 if currentState<>0 then if currentState<>prevState then counter=1 counterIncreasing=1 else if reverseCounter then if counterIncreasing then if counter<counterbreak then counter=1+counter else counterIncreasing=0 counter=counter-1 endif else if counter>1 then counter=counter-1 else counterIncreasing=1 counter=1+counter endif endif else counter=1+counter endif endif else counter=1 counterIncreasing=1 endif prevState=currentState //---------------------------------------------// // Recalculate th dynamic MA based on the updated counter //---------------------------------------------// dynLen=min(baseLen+(counter-1),maxDynLen) ma=average[dynLen](src) //---------------------------------------------// // Trend Direction //---------------------------------------------// once trend=0 if close crosses over ma then trend=1 elsif close crosses under ma then trend =0 endif trendChange=trend<>trend[1] //---------------------------------------------// // Adaptative Trend calculations //---------------------------------------------// atrThreshold=averagetruerange[200](close) freq=2*3.1416/length smoothfactor=trendSmoothing*freq responseFactor=exp(-smoothfactor) if barindex<counter+baseLen then primaryFilter=0 secondaryFilter=0 else primaryFilter=primaryfilter[1]+smoothFactor*(ma-primaryFilter[1]) secondaryFilter=secondaryfilter[1]*responseFactor+(1-responseFactor)*primaryfilter endif filteredTrend=(primaryFilter+secondaryFilter)/2 //---------------------------------------------// // Track Trend stregth changes //---------------------------------------------// once trendUp=0 once trendDown=0 isUptrend=filteredTrend>filteredTrend[2] isDownTrend=filteredTrend<filteredTrend[2] //reset trend counters when direction changes if isUptrend then trendUp=trendUp+1 trendDown=0 elsif isDowntrend then trendUp=0 trendDown=trendDown+1 endif //---------------------------------------------// // Colours and Signals //---------------------------------------------// ConfirmedUptrend=trendDown>=trending ConfirmedDownTrend=trendUp>=trending ConfirmedUptrendCol=trendUp>=trendingCol ConfirmedDowntrendCol=trenddown>=trendingCol PosTrendStart=ConfirmedUptrend and ConfirmedUptrend[1]=0 NegTrendStart=ConfirmedDownTrend and ConfirmedDownTrend[1]=0 circleUp=filteredTrend+atrThreshold circleDn=filteredTrend-atrThreshold if ConfirmedUptrendCol then r=0 g=230 b=118 elsif ConfirmedDowntrendCol then r=255 g=82 b=82 else r=212 g=218 b=44 alphaDn=0 alphaUp=0 endif if PosTrendStart then drawtext("▼",barindex,circleUp)coloured("orange",255) drawpoint(barindex,circleUp,5)coloured("orange",30) alphaDn=0 alphaUp=125 elsif NegTrendStart then drawtext("▲",barindex,circleDn)coloured("lime",255) drawpoint(barindex,circleDn,5)coloured("lime",30) alphaDn=125 alphaUp=0 endif //---------------------------------------------// return ma as "Dynamic MA" coloured(r,g,b)style(line,2), circleUp style(point,2)coloured("orange",alphaUp), circleDn style(point,2)coloured("Lime",alphaDn) |
Conclusion
The AutoLength Moving Average is a powerful and flexible moving average designed for traders who require dynamic adaptation to market conditions. With its intelligent counter-based length adjustment and customizable reset conditions, it provides an edge over traditional moving averages by offering both smoothness and responsiveness.
This indicator is especially beneficial for trend traders, swing traders, and those who prefer adaptive technical analysis tools to improve their decision-making process.
Share this
No information on this site is investment advice or a solicitation to buy or sell any financial instrument. Past performance is not indicative of future results. Trading may expose you to risk of loss greater than your deposits and is only suitable for experienced investors who have sufficient financial means to bear such risk.
ProRealTime ITF files and other attachments :PRC is also on YouTube, subscribe to our channel for exclusive content and tutorials