xmd79

TSI with histogram and MA - SamX

Nov 25th, 2021
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.09 KB | None | 0 0
  1. // This source code provided free and open-source as defined by the terms of the Mozilla Public License 2.0 (https://mozilla.org/MPL/2.0/)
  2. // with the following additional requirements:
  3. //
  4. // 1. Citations for sources and references must be maintained and updated when appropriate
  5. // 2. Links to strategy references included in indicator tooptips and/or alerts should be retained to give credit to the original source
  6. // while also providing a freely available source of information on proper use and interpretation
  7. //
  8. // Author: SamAccountX
  9. //
  10. // I didn't like the poor way of setting alerts with the original version, nor the way the original settings were set up,
  11. // so I decided to make a copy and do some tweaks to help improve upon some of these gaps.
  12. // In addition to that, I added the ability to plot a customizable moving average of the TSI values.
  13. //
  14. // I also took this opportunity to update it to Pinescript v5, and updated the default input value to match the settings for Hawk's Strategy
  15. // Vid Ref: https://youtu.be/oMPvVbfnXSk
  16. //
  17. // Original author: ZakariaBouguira
  18. // Source reference: https://www.tradingview.com/script/YlRZqh9x-True-Strength-Indicator-with-histogram
  19. //
  20. //@version=5
  21. indicator('TSI with histogram and MA - SamX', shorttitle='TSI Hist & MA')
  22. g_TSISettings = 'TSI Settings'
  23. long = input.int(title='Long-Preiod Length', defval=13, group=g_TSISettings, tooltip='Original default: 25')
  24. short = input.int(title='Short-Period Length', defval=6, group=g_TSISettings, tooltip='Original default: 13')
  25. signal = input.int(title='Signal Preiod Length', defval=4, group=g_TSISettings, tooltip='Original default: 13')
  26. priceSource = input.source(title='Source for price', defval=close, group=g_TSISettings, tooltip='Source for price information. Default: Close')
  27. showHist = input.bool(title='Show TSI Histogram', defval=true, group=g_TSISettings, tooltip='Show the TSI Histogram along the 0 line. Default value: checked')
  28. showTSIFill = input.bool(title='Show TSI colored fill', defval=true, group=g_TSISettings, tooltip='Fill the area between the TSI and the TSI Signal. Default fill color will be ' +
  29. 'green if the TSI is above the signal line, and red if below the signal line. \n\n' +
  30. 'Default value: checked')
  31.  
  32. g_MASettings = "TSI Moving Average Settings"
  33. showFloatingTSIMA = input.bool(title='Show floating TSI Moving Average', defval=true, group=g_MASettings, tooltip='Show a floating moving average line for the TSI value. ')
  34. maType = input.string(title='TSI Moving Average Calculation', group=g_MASettings, options=['Exponential', 'Simple', 'Smoothed', 'Weighted', 'Linear', 'Hull', 'Arnaud Legoux'], defval='Exponential', tooltip='Type of moving average calculation to use (default is Exponential (EMA)). \n\n' +
  35. 'Only relevant when "Show floating TSI Moving Average" above is enabled.')
  36. maLength = input.int(title="TSI MA Length", defval=50, minval=1, step=1, group=g_MASettings, tooltip="Number of historical TSI values to use for calculating the MA value. Lower numbers will react more quickly, while higher numbers will better depict longer trends.")
  37.  
  38. g_ALMASettings = 'ALMA Additional Settings'
  39. almaSigma = input.float(title="Sigma", defval=6, minval=0, maxval=100, step=0.1, group=g_ALMASettings, tooltip='Standard deviation applied to the MA. Higher values tend to make the line smoother. Default: 6')
  40. almaOffset = input.float(title="Offset", defval=0.85, minval=0, maxval=1, step=0.01, group=g_ALMASettings, tooltip='Standard deviation applied to the MA. Higher values tend to make the line smoother, while lower values make it more responsive. Default: 0.85')
  41.  
  42. useNewAlerts = input.bool(title='Use Indicator-native alerts', defval=true, group="Alert Settings", tooltip='If enabled (default), alerting will be handled natively by the indicator code instead of the general TradingView alerts. \n\n' +
  43. 'This is helpful for users with the free version of TradingView as it has a limition of a single traditional alert by allowing the user to configure a single alert in Tradingview ' +
  44. 'that will then be dynamically populated and evaluated by the indicator script itself. \n\n' +
  45. 'To configure the TV alert for this, go to Create Alert. In the Conditions section, select the name of the indicator for the first drop-down box, and in the second drop-down box you ' +
  46. 'will select "Any alert() function call"')
  47. unusedInput = input(title='Additional info on TSI and how it might be used: https://www.investopedia.com/terms/t/tsi.asp', defval=true, group="Other")
  48.  
  49. // Function to calculate smoothed moving average...
  50. smoothedMovingAvg(src, len) =>
  51. smma = 0.0
  52. // TV will complain about the use of the ta.sma function use inside a function saying that it should be called on each calculation,
  53. // but since we're only using it once to set the initial value for the smoothed MA (when the previous smma value is NaN - Not a Number)
  54. // and using the previous smma value for each subsequent iteration, this can be safely ignored
  55. smma := na(smma[1]) ? ta.sma(src, len) : (smma[1] * (len - 1) + src) / len
  56. smma
  57.  
  58.  
  59. price = priceSource
  60. double_smooth(src, long, short) =>
  61. fist_smooth = ta.ema(src, long)
  62. ta.ema(fist_smooth, short)
  63. pc = ta.change(price)
  64. double_smoothed_pc = double_smooth(pc, long, short)
  65. double_smoothed_abs_pc = double_smooth(math.abs(pc), long, short)
  66. tsi_value = 100 * (double_smoothed_pc / double_smoothed_abs_pc)
  67. signal_line = ta.ema(tsi_value, signal)
  68.  
  69. hist = tsi_value - signal_line
  70.  
  71. // Identify if TSI is above or below signal line
  72. tsiBullish = tsi_value > signal_line
  73. tsiBearish = tsi_value < signal_line
  74.  
  75. // Plot the histogram first so it will be layered behind the TSI and TSI Signal lines
  76. plot(showHist == true ? hist : na, title='TSI Histogram)', color=color.rgb(207, 80, 207, 70), style=plot.style_area)
  77.  
  78. // mid-line
  79. hline(price=0, title='Zero (Mid Line)', color=color.rgb(127, 127, 127, 0), linestyle=hline.style_dashed)
  80.  
  81. // Define the colors for the TSI so we can re-use them in the fill zone as well...
  82. tsiBullColor = color.rgb(0, 255, 0, 0)
  83. tsiBearColor = color.rgb(255, 0, 0, 0)
  84. // Plot the TSI and TSI Signal lines
  85. pSignal = plot(signal_line, title='TSI Signal Line', color=color.rgb(0, 127, 255, 0))
  86. pTSI = plot(tsi_value, title='TSI', color=tsiBullish ? tsiBullColor : tsiBearish ? tsiBearColor : color.rgb(127, 127, 127, 0), display=display.all)
  87.  
  88. // TSI MA fun
  89. floatingMA = maType == 'Exponential' ? ta.ema(tsi_value, maLength) : maType == 'Simple' ? ta.sma(tsi_value, maLength) : maType == 'Weighted' ? ta.wma(tsi_value, maLength) : maType == 'Linear' ? ta.linreg(tsi_value, maLength, 0) : maType == 'Hull' ? ta.hma(tsi_value, maLength) : maType == 'Smoothed' ? smoothedMovingAvg(tsi_value, maLength) : maType == 'Arnaud Legoux' ? ta.alma(tsi_value, maLength, almaOffset, almaSigma) : na
  90. pFloatingMA = plot(showFloatingTSIMA ? floatingMA : na, title="TSI Floating MA", color=color.rgb(255, 255, 0, 0))
  91.  
  92. // An added bonus - area fill between the TSI and the signal line, shaded to
  93. // indicate whether the TSI is above or below the signal line...
  94. //
  95. // Similar to the histogram, plot this first so that the lines end up on top of it...
  96. fillColor = tsiBullish ? color.new(tsiBullColor, 63) : tsiBearish ? color.new(tsiBearColor, 63) : na
  97. fill(pTSI, pSignal, color=showTSIFill ? fillColor : na, title='Fill Color')
  98.  
  99.  
  100.  
  101. // Traditional AlertConditions
  102. isCrossAbove = tsiBearish[1] and tsiBullish
  103. isCrossBelow = tsiBullish[1] and tsiBearish
  104. alertcondition(isCrossAbove and barstate.isconfirmed, title='TSI Cross Above Signal Line', message='TSI Crossed Above Signal line')
  105. alertcondition(isCrossBelow and barstate.isconfirmed, title='TSI Cross Below Signal Line', message='TSI Crossed Below Signal line')
  106.  
  107. if (useNewAlerts)
  108. // Get date/time for use in alert...
  109. // So there is unfortunately no way to resolve this to the user's display timezone, so nixing it to reduce confusion...
  110. // var now = timenow
  111. // var date = str.tostring(str.tostring(dayofmonth(now)) + '/' + str.tostring(month(now)) + '/' + str.tostring(year(now)) + ' - ' + str.tostring(hour(now))+ ':' + str.tostring(minute(now)) + ':' + str.tostring(second(now)))
  112. //
  113. // syminfo.tickerid works well for candlestick charts, but returns some odd additional data on other chart types like HA candles. Therefore, we're going to explicitly construct
  114. // the "standard" output format of "Exchange:Symbol" for use in the alerts
  115. var sym = (syminfo.prefix + ':' + syminfo.ticker)
  116. if (isCrossBelow and barstate.isconfirmed)
  117. var m = 'TSI Crossed Below Signal Line on ' + str.tostring(sym) + ', ' + timeframe.period + ' timeframe. Check for additional confluences like being above the mid line (overbought).'
  118. alert(str.tostring(m), alert.freq_once_per_bar_close)
  119. if (isCrossAbove and barstate.isconfirmed)
  120. var m = 'TSI Crossed Above Signal Line on ' + str.tostring(sym) + ', ' + timeframe.period + ' timeframe. Check for additional confluences like being below the mid line (oversold).'
  121. alert(str.tostring(m), alert.freq_once_per_bar_close)
  122.  
  123.  
  124.  
Advertisement
Add Comment
Please, Sign In to add comment