xmd79

ATR [KL]

Nov 21st, 2021
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © DojiEmoji
  3.  
  4. //@version=5
  5. indicator(title="ATR [KL]", overlay=false, timeframe="", timeframe_gaps=true)
  6.  
  7. // Calculations of ATRs: {
  8. length_fast = input.int(title="Length of lookback (fast)", defval=14, minval=1)
  9. length_slow = input.int(title="Length of lookback (slow)", defval=50, minval=1)
  10.  
  11. atr_fast = ta.sma(ta.tr(true), length_fast)
  12. atr_slow = ta.sma(ta.tr(true), length_slow)
  13. // }
  14.  
  15. // Hypothesis testing (2-tailed) {
  16. // Null Hypothesis H0 : atr_fast equals atr_slow
  17. // Alternative hypothesis Ha : atr_fast not equals to atr_slow; implies atr_fast is either too low or too high
  18.  
  19. // Calculate test statistic (test_stat)
  20. _std_error = ta.stdev(ta.tr, length_fast) / math.pow(length_fast, 0.5) // Standard Error (SE) = std / sq root(sample size)
  21. test_stat = (atr_fast - atr_slow) / _std_error
  22.  
  23. // Compare test_stat against critical value defined by user in settings
  24. 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.")
  25. critical_value = conf_interval == "90%" ? 1.645 : conf_interval == "95%" ? 1.96 : 2.58
  26. reject_H0 = math.abs(test_stat) > critical_value
  27. // }
  28.  
  29. var color_normal = input.color(#7b1fa2, title="ATR Color (Normal)")
  30. var color_high = input.color(color.red, title="ATR Color (Extreme high)")
  31. var color_low = input.color(color.green, title="ATR Color (Extremely low)")
  32.  
  33. atr_extremely_high = test_stat > math.abs(critical_value)
  34. atr_extremely_low = test_stat < -math.abs(critical_value)
  35. color_length_fast = atr_extremely_high ? color_high : atr_extremely_low ? color_low : color_normal
  36.  
  37. // Plotting {
  38. plot(atr_fast, title = "ATR fast", color=color.new(color_length_fast, 0), linewidth=2)
  39. //plot(atr_slow, title = "ATR slow", color=color.new(color.gray, 75))
  40.  
  41. plot_arrows = input(true, title="Plot arrows", tooltip="Arrows at top/bottom of ATR panel to indicate extreme high/low periods")
  42. plotshape(plot_arrows and atr_extremely_high, style=shape.triangleup, location=location.top, color=color.red)
  43. plotshape(plot_arrows and atr_extremely_low, style=shape.triangledown, location=location.bottom, color=color.green)
  44.  
  45. // }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment