True Strength Index with Symbol Input

APOT

New member
Platform
  1. Thinkorswim
can this be converted to be able to add any ticker? Thank you. @barbaros

Code:
# True Strength Index
declare lower;
input longLength = 25;
input shortLength = 13;
input signalLength = 8;
input averageType = AverageType.EXPONENTIAL;
def diff = close - close[1];
def doubleSmoothedAbsDiff = MovingAverage(averageType, MovingAverage(averageType, AbsValue(diff), longLength), shortLength);
plot TSI;
plot Signal;
TSI = if doubleSmoothedAbsDiff == 0 then 0
      else 100 * (MovingAverage(averageType, MovingAverage(averageType, diff, longLength), shortLength)) / doubleSmoothedAbsDiff;
Signal = MovingAverage(averageType, TSI, signalLength);
plot ZeroLine = 0;
TSI.SetDefaultColor(GetColor(1));
Signal.SetDefaultColor(GetColor(8));
Signal.Hide();
ZeroLine.SetDefaultColor(GetColor(5));

TSI.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
TSI.SetLineWeight(3);
TSI.DefineColor("Positive and Up", Color.GREEN);
TSI.DefineColor("Positive and Down",(CreateColor(153, 0, 242)));
TSI.DefineColor("Negative and Down", Color.RED);
TSI.DefineColor("Negative and Up", Color.YELLOW);
TSI.AssignValueColor(if TSI >= 0 then if TSI > TSI[1] then TSI.Color("Positive and Up") else TSI.Color("Positive and Down") else if TSI < TSI[1] then TSI.Color("Negative and Down") else TSI.Color("Negative and Up"));

TSI.AssignValueColor(
if TSI >= 0 then if TSI > TSI[1] then TSI.Color("Positive and Up") else TSI.Color("Positive and Down") else
if TSI < TSI[1] then TSI.Color("Negative and Down") else TSI.Color("Negative and Up"));
input n = 20;
def bar = BarNumber();
def Currh = if tsi > zeroline
                then fold i = 1 to Floor(n / 2)
                with p = 1
                while p
                do tsi > getValue(tsi, -i)
                else 0;
def CurrPivotH = if (bar > n and
                         tsi == highest(tsi, Floor(n/2)) and
                         Currh)
                     then tsi
                     else double.NaN;
def Currl = if tsi < zeroline
                then fold j = 1 to Floor(n / 2)
                with q = 1
                while q
                do tsi < getValue(tsi, -j)
                else 0;
def CurrPivotL = if (bar > n and
                         tsi == lowest(tsi, Floor(n/2)) and
                         Currl)
                     then tsi
                     else double.NaN;
def CurrPHBar = if !isNaN(CurrPivotH)
                then bar
                else CurrPHBar[1];
def CurrPLBar = if !isNaN(CurrPivotL)
                then bar
                else CurrPLBar[1];
def PHpoint = if !isNaN(CurrPivotH)
              then CurrPivotH
              else PHpoint[1];
def priorPHBar = if PHpoint != PHpoint[1]
                 then CurrPHBar[1]
                 else priorPHBar[1];
def PLpoint = if !isNaN(CurrPivotL)
              then CurrPivotL
              else PLpoint[1];
def priorPLBar = if PLpoint != PLpoint[1]
                 then CurrPLBar[1]
                 else priorPLBar[1];
def HighPivots = bar >= highestAll(priorPHBar);
def LowPivots = bar >= highestAll(priorPLBar);
def pivotHigh = if HighPivots
                then CurrPivotH
                else double.NaN;
plot PlotHline = pivotHigh;
    PlotHline.enableApproximation();
    PlotHline.SetDefaultColor(GetColor(5));
    PlotHline.SetStyle(Curve.LONG_DASH);
    PlotHline.SetLineWeight(5);
plot pivotLow = if LowPivots
                then CurrPivotL
                else double.NaN;
    pivotLow.enableApproximation();
    pivotLow.SetDefaultColor(GetColor(6));
    pivotLow.SetStyle(Curve.LONG_DASH);
    pivotLow.SetLineWeight(3);
plot PivotDot = if !isNaN(pivotHigh)
                then pivotHigh
                else if !isNaN(pivotLow)
                     then pivotLow
                     else double.NaN;
    pivotDot.SetDefaultColor(GetColor(3));
    pivotDot.SetPaintingStrategy(PaintingStrategy.POINTS);
    pivotDot.SetLineWeight(3);
 
Last edited:
Top