Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
- // © DojiEmoji
- //@version=5
- indicator(title="ATR [KL]", overlay=false, timeframe="", timeframe_gaps=true)
- // Calculations of ATRs: {
- length_fast = input.int(title="Length of lookback (fast)", defval=14, minval=1)
- length_slow = input.int(title="Length of lookback (slow)", defval=50, minval=1)
- atr_fast = ta.sma(ta.tr(true), length_fast)
- atr_slow = ta.sma(ta.tr(true), length_slow)
- // }
- // Hypothesis testing (2-tailed) {
- // Null Hypothesis H0 : atr_fast equals atr_slow
- // Alternative hypothesis Ha : atr_fast not equals to atr_slow; implies atr_fast is either too low or too high
- // Calculate test statistic (test_stat)
- _std_error = ta.stdev(ta.tr, length_fast) / math.pow(length_fast, 0.5) // Standard Error (SE) = std / sq root(sample size)
- test_stat = (atr_fast - atr_slow) / _std_error
- // Compare test_stat against critical value defined by user in settings
- conf_interval = input.string(title="Confidence Interval", defval="99%", options=["90%","95%","99%"], tooltip="Critical values of 1.645, 1.96, 2.58, for CI=90%/95%/99%, respectively; Under 2-tailed test to compare atr_fast vs atr_slow. Null hypothesis (H0) is that both should equal. Based on the computed test statistic value, if absolute value of it is +/- critical value, then H0 will be rejected.")
- critical_value = conf_interval == "90%" ? 1.645 : conf_interval == "95%" ? 1.96 : 2.58
- reject_H0 = math.abs(test_stat) > critical_value
- // }
- var color_normal = input.color(#7b1fa2, title="ATR Color (Normal)")
- var color_high = input.color(color.red, title="ATR Color (Extreme high)")
- var color_low = input.color(color.green, title="ATR Color (Extremely low)")
- atr_extremely_high = test_stat > math.abs(critical_value)
- atr_extremely_low = test_stat < -math.abs(critical_value)
- color_length_fast = atr_extremely_high ? color_high : atr_extremely_low ? color_low : color_normal
- // Plotting {
- plot(atr_fast, title = "ATR fast", color=color.new(color_length_fast, 0), linewidth=2)
- //plot(atr_slow, title = "ATR slow", color=color.new(color.gray, 75))
- plot_arrows = input(true, title="Plot arrows", tooltip="Arrows at top/bottom of ATR panel to indicate extreme high/low periods")
- plotshape(plot_arrows and atr_extremely_high, style=shape.triangleup, location=location.top, color=color.red)
- plotshape(plot_arrows and atr_extremely_low, style=shape.triangledown, location=location.bottom, color=color.green)
- // }
Advertisement
Add Comment
Please, Sign In to add comment