[text] Better-Trend

Viewer

copydownloadembedprintName: Better-Trend
  1. //@version=5
  2.  
  3. indicator('Better-Trend', overlay=true)
  4.  
  5. amplitude = input(title='Amplitude', defval=2)
  6. channelDeviation = input(title='Channel Deviation', defval=2)
  7. showArrows = input(title='Show Arrows', defval=true)
  8. showChannels = input(title='Show Channels', defval=true)
  9.  
  10. var int trend = 0
  11. var int nextTrend = 0
  12. var float maxLowPrice = nz(low[1], low)
  13. var float minHighPrice = nz(high[1], high)
  14.  
  15. var float up = 0.0
  16. var float down = 0.0
  17. float atrHigh = 0.0
  18. float atrLow = 0.0
  19. float arrowUp = 1.5
  20. float arrowDown = 1.5
  21.  
  22. atr2 = ta.atr(125) / 1
  23. dev = channelDeviation * atr2
  24.  
  25. highPrice = high[math.abs(ta.highestbars(amplitude))]
  26. lowPrice = low[math.abs(ta.lowestbars(amplitude))]
  27. highma = ta.sma(high, amplitude)
  28. lowma = ta.sma(low, amplitude)
  29.  
  30. if nextTrend == 1
  31.     maxLowPrice := math.max(lowPrice, maxLowPrice)
  32.  
  33.     if highma < maxLowPrice and close < nz(low[1], low)
  34.         trend := 1
  35.         nextTrend := 0
  36.         minHighPrice := highPrice
  37.         minHighPrice
  38. else
  39.     minHighPrice := math.min(highPrice, minHighPrice)
  40.  
  41.     if lowma > minHighPrice and close > nz(high[1], high)
  42.         trend := 0
  43.         nextTrend := 1
  44.         maxLowPrice := lowPrice
  45.         maxLowPrice
  46.  
  47. if trend == 0
  48.     if not na(trend[1]) and trend[1] != 0
  49.         up := na(down[1]) ? down : down[1]
  50.         arrowUp := up - atr2
  51.         arrowUp
  52.     else
  53.         up := na(up[1]) ? maxLowPrice : math.max(maxLowPrice, up[1])
  54.         up
  55.     atrHigh := up + dev
  56.     atrLow := up - dev
  57.     atrLow
  58. else
  59.     if not na(trend[1]) and trend[1] != 1
  60.         down := na(up[1]) ? up : up[1]
  61.         arrowDown := down + atr2
  62.         arrowDown
  63.     else
  64.         down := na(down[1]) ? minHighPrice : math.min(minHighPrice, down[1])
  65.         down
  66.     atrHigh := down + dev
  67.     atrLow := down - dev
  68.     atrLow
  69.  
  70. ht = trend == 0 ? up : down
  71.  
  72. var color buyColor = color.blue
  73. var color sellColor = color.red
  74.  
  75. htColor = trend == 0 ? buyColor : sellColor
  76. htPlot = plot(ht, title='HalfTrend', linewidth=2, color=htColor)
  77.  
  78. atrHighPlot = plot(showChannels ? atrHigh : na, title='ATR High', style=plot.style_circles, color=color.new(sellColor, 0))
  79. atrLowPlot = plot(showChannels ? atrLow : na, title='ATR Low', style=plot.style_circles, color=color.new(buyColor, 0))
  80.  
  81. fill(htPlot, atrHighPlot, title='ATR High Ribbon', color=color.new(sellColor, 90))
  82. fill(htPlot, atrLowPlot, title='ATR Low Ribbon', color=color.new(buyColor, 90))
  83.  
  84. buySignal = not na(arrowUp) and trend == 0 and trend[1] == 1
  85. sellSignal = not na(arrowDown) and trend == 1 and trend[1] == 0
  86.  
  87. plotshape(showArrows and buySignal ? atrLow : na, title='Arrow Up', style=shape.triangleup, location=location.absolute, size=size.tiny, color=color.new(buyColor, 0))
  88. plotshape(showArrows and sellSignal ? atrHigh : na, title='Arrow Down', style=shape.triangledown, location=location.absolute, size=size.tiny, color=color.new(sellColor, 0))
  89.  
  90. alertcondition(buySignal, title='Alert: HalfTrend Buy', message='HalfTrend Buy')
  91. alertcondition(sellSignal, title='Alert: HalfTrend Sell', message='HalfTrend Sell')
  92.  
  93. //High-Close-Low
  94. High = input.int(125, minval=1, title='High')
  95. Low = input.int(125, minval=1, title='Low')
  96. Close = input.int(125, minval=1, title='Close')
  97.  
  98. //Regular MA's (Close)
  99. //SMA1 = input.int(20, minval=1, title='SMA1')
  100. //SMA2 = input.int(50, minval=1, title='SMA2')
  101. //SMA3 = input.int(100, minval=1, title='SMA3')
  102.  
  103. plot(ta.sma(high, High), color=color.new(color.green, 0), linewidth=2)
  104. plot(ta.sma(close, Low), color=color.new(color.orange, 0), linewidth=2)
  105. plot(ta.sma(low, Close), color=color.new(color.red, 0), linewidth=2)
  106.  
  107. //plot(ta.sma(close, SMA1), color=color.new(color.green, 0), linewidth=2)
  108. //plot(ta.sma(close, SMA2), color=color.new(color.orange, 0), linewidth=2)
  109. //plot(ta.sma(close, SMA3), color=color.new(color.red, 0), linewidth=2)
  110.  
  111. basePeriods = input.int(75, minval=25, title='Period')
  112. donchian(len) =>
  113.     math.avg(ta.lowest(len), ta.highest(len))
  114. baseLine = donchian(basePeriods)
  115. plot(baseLine, color=color.new(#991515, 0), title='Kijun-Sen')

Editor

You can edit this paste and save as new:


File Description
  • Better-Trend
  • Paste Code
  • 27 Jan-2023
  • 4 Kb
You can Share it: