Indicator · Pine Script v5 · Own pane

Average Bars Before Reversion

by jawauntb

This indicator estimates how many bars typically pass before a custom skew measure falls back below its exponential moving average.

View on GitHub

GitHub stars3on the repository
Pine versionv5runs as is
Lines of code64
Settings0inputs
LicenceGPL-3.0
Last update2 years ago22 May 2024

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

64 lines of Pine Script v5 from average-skew-reversal-time.pine, published under GPL-3.0. Keep the author's credit and licence lines when you reuse it.

average-skew-reversal-time.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © jawauntb

//@version=5
indicator("Average Bars Before Reversion", shorttitle="ABSR", overlay=false)

// Define lengths for EMAs and other parameters
lenSuperFast = 2
lenFast = 8
lenAverage = 14
lenMedium = 30
lenSlow = 110
SkewLength = 30
alpha = 2.0/(1.0+SkewLength)
deviation_max = 1.
deviation_min = 1.

trueRange = math.max(math.max(high - low, math.abs(high - close[1])), math.abs(low - close[1]))/close[1]
deviation_max := alpha*(close > close[1] ? trueRange:0) + (1.0-alpha)*nz(deviation_max[1])
deviation_min := alpha*(close < close[1] ? trueRange:0) + (1.0-alpha)*nz(deviation_min[1])

// Calculate skew
skew = deviation_max/deviation_min

// Calculate the EMA of the skew for the defined lengths
emaSuperFast = ta.ema(skew, lenSuperFast)
emaFast = ta.ema(skew, lenFast)
emaAverage = ta.ema(skew, lenAverage)
emaMedium = ta.ema(skew, lenMedium)
emaSlow = ta.ema(skew, lenSlow)

// Initialize an array for each EMA to store the number of bars before reversion
barsBeforeReversionSuperFast = array.new_float(0)
barsBeforeReversionFast = array.new_float(0)
barsBeforeReversionAverage = array.new_float(0)
barsBeforeReversionMedium = array.new_float(0)
barsBeforeReversionSlow = array.new_float(0)

// Iterate over each bar
for i = 0 to lenSlow
    if (skew[i] > emaSuperFast[i] and skew[i+1] < emaSuperFast[i+1])
        array.push(barsBeforeReversionSuperFast, i)
    if (skew[i] > emaFast[i] and skew[i+1] < emaFast[i+1])
        array.push(barsBeforeReversionFast, i)
    if (skew[i] > emaAverage[i] and skew[i+1] < emaAverage[i+1])
        array.push(barsBeforeReversionAverage, i)
    if (skew[i] > emaMedium[i] and skew[i+1] < emaMedium[i+1])
        array.push(barsBeforeReversionMedium, i)
    if (skew[i] > emaSlow[i] and skew[i+1] < emaSlow[i+1])
        array.push(barsBeforeReversionSlow, i)

// Calculate the average number of bars before reversion for each EMA
avgBarsBeforeReversionSuperFast = array.avg(barsBeforeReversionSuperFast)
avgBarsBeforeReversionFast = array.avg(barsBeforeReversionFast)
avgBarsBeforeReversionAverage = array.avg(barsBeforeReversionAverage)
avgBarsBeforeReversionMedium = array.avg(barsBeforeReversionMedium)
avgBarsBeforeReversionSlow = array.avg(barsBeforeReversionSlow)

// Plot the average number of bars before reversion for each EMA
plot(avgBarsBeforeReversionSuperFast, color=color.rgb(75, 234, 252), title="Average Bars Before Reversion Super Fast", linewidth = 2)
plot(avgBarsBeforeReversionFast, color=color.rgb(10, 239, 18), title="Average Bars Before Reversion Fast", linewidth = 2)
plot(avgBarsBeforeReversionAverage, color=color.rgb(255, 165, 0), title="Average Bars Before Reversion Average", linewidth = 2)
plot(avgBarsBeforeReversionMedium, color=color.rgb(31, 34, 134), title="Average Bars Before Reversion Medium", linewidth = 2)
plot(avgBarsBeforeReversionSlow, color=color.rgb(198, 45, 45), title="Average Bars Before Reversion Slow", linewidth = 2)

Overview

Average Bars Before Reversion builds the skew from smoothed upside and downside true-range deviations, then applies five fixed EMA lengths from 2 to 110. On each bar it scans the last 110 bars for downward crosses of the skew through each EMA and plots the average bar offset of those crosses in a separate pane. All lengths are hardcoded and the script does not generate trade signals or position rules.

Details

Published by

jawauntb on GitHub

Draws

Separate pane below the chart

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

This indicator builds a channel from a Hull moving average of the source and an ATR scaled by a band multiplier, then normalizes price inside that channel into a 0 to 100 oscillator. It smooths the result with a weighted moving average and plots the line in a

Indicator v6 Mean reversionVolatility & bands
Updated 3 months ago · None stated GitHub repository of Radiant Mean Reversion Channels [Pineify]
2 stars

Calculates custom exponential moving averages and standard deviation envelopes based on price offsets and lookback windows. It plots dynamic channel bands, mean reversion zones, and squeeze indicators in a separate pane. Users can adjust the moving average len

Indicator v5 Moving averagesVolatility & bands
Updated 3 years ago · None stated GitHub repository of [CBB] Rising Falling Color Band

This indicator plots trade entry and exit signals on the price chart by combining Bollinger Bands with optional filter conditions. It calculates standard Bollinger Bands along with RSI, Aroon, and moving average filters. Reversal signals trigger when price bre

Indicator v4 Volatility & bandsOscillators & momentum
Updated 2 months ago · None stated GitHub repository of Bollinger Bands Winner IND [Alorse]

Kalman Percentile Bands

by NQ-stats-MX
15 stars

Kalman Percentile Bands plots a volatility-adaptive Kalman mean along with three empirical percentile bands on each side of the price chart. It calculates a local-level Kalman filter as the fair value mean, normalizes price deviations by an EWMA of volatility,

Indicator v6 Mean reversionVolatility & bands
Updated 2 weeks ago · MIT GitHub repository of Kalman Percentile Bands
6 stars

This indicator classifies price action into four distinct market regimes: Trend, Expansion, Compression, or Mean Reversion. It determines states using a simplified Hurst exponent estimate, ADX slope thresholds, and realized volatility measurements. Based on th

Indicator v6 TrendVolatility & bands
Updated 2 months ago · MIT GitHub repository of Capital41 Regime Matrix v3

More Mean reversion scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers