Advertisement
xmd79

Adaptive Schaff Trend Cycle (STC) [AlgoAlpha]

Mar 25th, 2024
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. //@version=5
  2. indicator(title='Adaptive Schaff Trend Cycle (STC) [AlgoAlpha]', shorttitle='AlgoAlpha - Adaptive 𝓢𝓣𝓒', overlay=false)
  3. length = input.int(55, 'Adaptive Logic Length', minval = 1)
  4. lengthInput = input.int(12, 'STC Length', minval = 1)
  5. smoothingFactor = input.float(0.45, 'STC Sensitivity', minval = 0.0001, maxval = 1)
  6. fastLength = input.int(26, 'MACD Fast Length', minval = 1)
  7. slowLength = input.int(50, 'MACD Slow Length', minval = 1)
  8. upColor = input.color(#6f6f6f, "Up Color")
  9. downColor = input.color(#ff0000, "Down Color")
  10.  
  11. calculateMACD(source, fastLength, slowLength) =>
  12. var macd = 0.
  13. var lag = (9 - 1) / 2
  14. var a1 = 2 / (fastLength + 1)
  15. var a2 = 2 / (slowLength + 1)
  16. r2 = .5 * math.pow(ta.correlation(close, bar_index, length), 2) + .5
  17. K = r2 * ((1 - a1) * (1 - a2)) + (1 - r2) * ((1 - a1) / (1 - a2))
  18. macd := (close - close[1]) * (a1 - a2) + (-a2 - a1 + 2) * nz(macd[1]) - K * nz(macd[2])
  19. ema = ta.ema(macd, 9)
  20. macd
  21.  
  22. calculateSTC(lengthInput, fastLength, slowLength) =>
  23. var normalizedMACD = 0.0
  24. var smoothedMACD = 0.0
  25. var smoothedNormalizedMACD = 0.0
  26. var STCValue = 0.0
  27. MACDValue = calculateMACD(close, fastLength, slowLength)
  28. lowestMACD = ta.lowest(MACDValue, lengthInput)
  29. highestMACD = ta.highest(MACDValue, lengthInput) - lowestMACD
  30. normalizedMACD := highestMACD > 0 ? (MACDValue - lowestMACD) / highestMACD * 100 : nz(normalizedMACD[1])
  31. smoothedMACD := na(smoothedMACD[1]) ? normalizedMACD : smoothedMACD[1] + smoothingFactor * (normalizedMACD - smoothedMACD[1])
  32. lowestSmoothedMACD = ta.lowest(smoothedMACD, lengthInput)
  33. highestSmoothedMACD = ta.highest(smoothedMACD, lengthInput) - lowestSmoothedMACD
  34. smoothedNormalizedMACD := highestSmoothedMACD > 0 ? (smoothedMACD - lowestSmoothedMACD) / highestSmoothedMACD * 100 : nz(smoothedNormalizedMACD[1])
  35. STCValue := na(STCValue[1]) ? smoothedNormalizedMACD : STCValue[1] + smoothingFactor * (smoothedNormalizedMACD - STCValue[1])
  36. [STCValue-50, MACDValue]
  37.  
  38. [STCValue, MACDValue] = calculateSTC(lengthInput, fastLength, slowLength)
  39.  
  40. MACDValue := (MACDValue) / (ta.ema(high - low, slowLength)) * 100
  41.  
  42. MACDValue := ((MACDValue) - ta.ema(MACDValue, 9))/2
  43.  
  44. tv = 80
  45. STCColor = STCValue > STCValue[1] ? color.from_gradient(STCValue, 0, 50, color.new(upColor, tv), color.new(upColor, 0)) : color.from_gradient(STCValue, -50, 0, color.new(downColor, 0), color.new(downColor, tv))
  46.  
  47. p1 = plot(STCValue, color=STCColor, title='STC', linewidth=2, style = plot.style_linebr) //STCValue > STCValue[1] and STCValue > 0 ? STCValue : STCValue < STCValue[1] and STCValue < 0 ? STCValue : na
  48. p2 = plot(STCValue[1], display = display.none)
  49. plot(MACDValue, title='Histogram', style = plot.style_columns, color = (MACDValue > MACDValue[1] and MACDValue > 0) ? color.new(upColor, 50) : (MACDValue < MACDValue[1] and MACDValue < 0) ? color.new(downColor, 50) : MACDValue < 0 ? color.new(downColor, 80) : color.new(upColor, 80))
  50. midLine = plot(0, color=color.new(color.gray, 70))
  51. upperLine = plot(-25, color=color.new(color.gray, 70))
  52. lowerLine = plot(25, color=color.new(color.gray, 70))
  53. overboughtLine = plot(60, color=color.new(color.gray, 70))
  54. oversoldLine = plot(-60, color=color.new(color.gray, 70))
  55. plotchar(ta.cross(STCValue, STCValue[1]) ? STCValue[1] : na, "STC Trend Shift", "●", location.absolute, STCValue > STCValue[1] ? upColor : downColor)
  56. fill(p1, p2, STCValue > STCValue[1] ? color.new(upColor, 90) : color.new(downColor, 90))
  57. fill(upperLine, lowerLine, color=color.new(color.gray, 96))
  58. fill(midLine, overboughtLine, top_value = 60, bottom_value = 30, bottom_color = na, top_color = color.from_gradient(STCValue, -50, 50, color.new(color.new(downColor, 50),100), color.new(color.new(downColor, 50),40)))
  59. fill(midLine, oversoldLine, top_value = -30, bottom_value = -60, bottom_color = color.from_gradient(STCValue, -50, 50, color.new(color.new(upColor, 50),40), color.new(color.new(upColor, 50),100)), top_color = na)
  60.  
  61. ///////////////Alerts
  62. alertcondition(ta.crossover(STCValue, STCValue[1]), "STC Moving Up")
  63. alertcondition(ta.crossunder(STCValue, STCValue[1]), "STC Moving Down")
  64.  
  65. alertcondition(ta.crossover(STCValue, 0), "STC Crossover Zero")
  66. alertcondition(ta.crossunder(STCValue, 0), "STC Crossunder Zero")
  67.  
  68. alertcondition(ta.crossover(MACDValue, 0), "Histogram Crossover Zero")
  69. alertcondition(ta.crossunder(MACDValue, 0), "Histogram Crossunder Zero")
  70.  
  71. alertcondition(ta.crossover(MACDValue, MACDValue[1]), "Histogram Moving Up")
  72. alertcondition(ta.crossunder(MACDValue, MACDValue[1]), "Histogram Moving Down")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement