Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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/)
- // with the following additional requirements:
- //
- // 1. Citations for sources and references must be maintained and updated when appropriate
- // 2. Links to strategy references included in indicator tooptips and/or alerts should be retained to give credit to the original source
- // while also providing a freely available source of information on proper use and interpretation
- //
- // Author: SamAccountX
- //
- // I didn't like the poor way of setting alerts with the original version, nor the way the original settings were set up,
- // so I decided to make a copy and do some tweaks to help improve upon some of these gaps.
- // In addition to that, I added the ability to plot a customizable moving average of the TSI values.
- //
- // 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
- // Vid Ref: https://youtu.be/oMPvVbfnXSk
- //
- // Original author: ZakariaBouguira
- // Source reference: https://www.tradingview.com/script/YlRZqh9x-True-Strength-Indicator-with-histogram
- //
- //@version=5
- indicator('TSI with histogram and MA - SamX', shorttitle='TSI Hist & MA')
- g_TSISettings = 'TSI Settings'
- long = input.int(title='Long-Preiod Length', defval=13, group=g_TSISettings, tooltip='Original default: 25')
- short = input.int(title='Short-Period Length', defval=6, group=g_TSISettings, tooltip='Original default: 13')
- signal = input.int(title='Signal Preiod Length', defval=4, group=g_TSISettings, tooltip='Original default: 13')
- priceSource = input.source(title='Source for price', defval=close, group=g_TSISettings, tooltip='Source for price information. Default: Close')
- showHist = input.bool(title='Show TSI Histogram', defval=true, group=g_TSISettings, tooltip='Show the TSI Histogram along the 0 line. Default value: checked')
- 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 ' +
- 'green if the TSI is above the signal line, and red if below the signal line. \n\n' +
- 'Default value: checked')
- g_MASettings = "TSI Moving Average Settings"
- 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. ')
- 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' +
- 'Only relevant when "Show floating TSI Moving Average" above is enabled.')
- 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.")
- g_ALMASettings = 'ALMA Additional Settings'
- 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')
- 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')
- 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' +
- '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 ' +
- 'that will then be dynamically populated and evaluated by the indicator script itself. \n\n' +
- '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 ' +
- 'will select "Any alert() function call"')
- 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")
- // Function to calculate smoothed moving average...
- smoothedMovingAvg(src, len) =>
- smma = 0.0
- // TV will complain about the use of the ta.sma function use inside a function saying that it should be called on each calculation,
- // 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)
- // and using the previous smma value for each subsequent iteration, this can be safely ignored
- smma := na(smma[1]) ? ta.sma(src, len) : (smma[1] * (len - 1) + src) / len
- smma
- price = priceSource
- double_smooth(src, long, short) =>
- fist_smooth = ta.ema(src, long)
- ta.ema(fist_smooth, short)
- pc = ta.change(price)
- double_smoothed_pc = double_smooth(pc, long, short)
- double_smoothed_abs_pc = double_smooth(math.abs(pc), long, short)
- tsi_value = 100 * (double_smoothed_pc / double_smoothed_abs_pc)
- signal_line = ta.ema(tsi_value, signal)
- hist = tsi_value - signal_line
- // Identify if TSI is above or below signal line
- tsiBullish = tsi_value > signal_line
- tsiBearish = tsi_value < signal_line
- // Plot the histogram first so it will be layered behind the TSI and TSI Signal lines
- plot(showHist == true ? hist : na, title='TSI Histogram)', color=color.rgb(207, 80, 207, 70), style=plot.style_area)
- // mid-line
- hline(price=0, title='Zero (Mid Line)', color=color.rgb(127, 127, 127, 0), linestyle=hline.style_dashed)
- // Define the colors for the TSI so we can re-use them in the fill zone as well...
- tsiBullColor = color.rgb(0, 255, 0, 0)
- tsiBearColor = color.rgb(255, 0, 0, 0)
- // Plot the TSI and TSI Signal lines
- pSignal = plot(signal_line, title='TSI Signal Line', color=color.rgb(0, 127, 255, 0))
- pTSI = plot(tsi_value, title='TSI', color=tsiBullish ? tsiBullColor : tsiBearish ? tsiBearColor : color.rgb(127, 127, 127, 0), display=display.all)
- // TSI MA fun
- 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
- pFloatingMA = plot(showFloatingTSIMA ? floatingMA : na, title="TSI Floating MA", color=color.rgb(255, 255, 0, 0))
- // An added bonus - area fill between the TSI and the signal line, shaded to
- // indicate whether the TSI is above or below the signal line...
- //
- // Similar to the histogram, plot this first so that the lines end up on top of it...
- fillColor = tsiBullish ? color.new(tsiBullColor, 63) : tsiBearish ? color.new(tsiBearColor, 63) : na
- fill(pTSI, pSignal, color=showTSIFill ? fillColor : na, title='Fill Color')
- // Traditional AlertConditions
- isCrossAbove = tsiBearish[1] and tsiBullish
- isCrossBelow = tsiBullish[1] and tsiBearish
- alertcondition(isCrossAbove and barstate.isconfirmed, title='TSI Cross Above Signal Line', message='TSI Crossed Above Signal line')
- alertcondition(isCrossBelow and barstate.isconfirmed, title='TSI Cross Below Signal Line', message='TSI Crossed Below Signal line')
- if (useNewAlerts)
- // Get date/time for use in alert...
- // So there is unfortunately no way to resolve this to the user's display timezone, so nixing it to reduce confusion...
- // var now = timenow
- // 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)))
- //
- // 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
- // the "standard" output format of "Exchange:Symbol" for use in the alerts
- var sym = (syminfo.prefix + ':' + syminfo.ticker)
- if (isCrossBelow and barstate.isconfirmed)
- 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).'
- alert(str.tostring(m), alert.freq_once_per_bar_close)
- if (isCrossAbove and barstate.isconfirmed)
- 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).'
- alert(str.tostring(m), alert.freq_once_per_bar_close)
Advertisement
Add Comment
Please, Sign In to add comment