# True Strength Index Indicator for ThinkorSwim

#### APOT7

##### Member
VIP
Can anyone help is changing this indicator with color? Its called True Strength Index. Its a built in indicator for TOS.

I would like True Strength Index colors to be:
• Green is its moving upward above zero line
• red if it moving downward below zero line
• yellow color if it moving from negative value to positive value under zero line.
• Purple color if it moving positive value to negative value above zero line.
Code:
``````#
# TD Ameritrade IP Company, Inc. (c) 2007-2021
#

declare lower;

input longLength = 25;
input shortLength = 13;
input signalLength = 8;
input averageType = AverageType.EXPONENTIAL;

def diff = close - close;
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));``````

Last edited:

#### BenTen

Staff
VIP
Green is its moving upward above zero line

How do you define "moving upward"?

red if it moving downward below zero line

Define "moving downward"?

yellow color if it moving from negative to positive under zero line.

How do we get a positive value under 0?

Purple color if it moving positive to negative above zero line.

Same as above. How can the value go from positive to negative if it's still above 0?

Here is the best I can do. Green if above 0 and red if below .

Code:
``````# True Strength Index

declare lower;

input longLength = 25;
input shortLength = 13;
input signalLength = 8;
input averageType = AverageType.EXPONENTIAL;

def diff = close - close;
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.AssignValueColor(if TSI > 0 then color.green else color.red);``````

• ##### Member
Can anyone help is changing this indicator with color? Its called True Strength Index. Its a built in indicator for TOS.

I would like True Strength Index colors to be:
• Green is its moving upward above zero line
• red if it moving downward below zero line
• yellow color if it moving from negative to positive under zero line.
• Purple color if it moving positive to negative above zero line.
Code:
``````#
# TD Ameritrade IP Company, Inc. (c) 2007-2021
#

declare lower;

input longLength = 25;
input shortLength = 13;
input signalLength = 8;
input averageType = AverageType.EXPONENTIAL;

def diff = close - close;
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));``````
Enjoy, if this is not quite what you want let me know and I will try to fix it.

Code:
``````input longLength = 25;
input shortLength = 13;
input signalLength = 8;
input averageType = AverageType.EXPONENTIAL;

def diff = close - close;
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.LINE);
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 then TSI.Color("Positive and Up") else TSI.Color("Positive and Down") else if TSI < TSI then TSI.Color("Negative and Down") else TSI.Color("Negative and Up"));``````

Last edited:
• APOT7

##### Member
@APOT7 Found this one as well it follows a 2 line cross instead of a ZeroLine cross. This is supposed to make the TSI more accurate. I put the same color scheme and line/histogram option in here but it is called Hist instead of TSI.

Code:
``````##ONE NOTE
#Wednesday, May 15, 2019 - archived 1
def Data = close;
def NA = Double.NaN;
#NA.SetDefaultColor(Color.BLACK);
#NA.HideTitle();
declare lower;
input IndicatorLine = 32;
input Smooth = 5;
input SignalLine = 5;
plot TSIFast = TrueStrengthIndex(LongLength = IndicatorLine, ShortLength = Smooth);
plot TSISlow = ExpAverage(TSIFast, SignalLine);
plot zero = 0;
zero.SetDefaultColor(Color.WHITE);
zero.HideBubble();
zero.HideTitle();
plot Fast = TSIFast;
Fast.SetDefaultColor(Color.BLUE);
plot Slow = TSISlow;
Slow.SetDefaultColor(Color.WHITE);
Slow.SetStyle(Curve.SHORT_DASH);
plot HighLvl = 25;
HighLvl.SetDefaultColor(Color.LIGHT_GREEN);
HighLvl.HideBubble();
HighLvl.HideTitle();
plot LowLvl = -25;
LowLvl.SetDefaultColor(Color.RED);
LowLvl.HideBubble();
LowLvl.HideTitle();

plot Hist = (TSIFast - TSISlow) * 2;
#TSI.SetPaintingStrategy(PaintingStrategy.LINE);
Hist.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Hist.SetLineWeight(3);
Hist.DefineColor("Positive and Up", Color.GREEN);
Hist.DefineColor("Positive and Down", (CreateColor(153, 0, 242)));
Hist.DefineColor("Negative and Down", Color.RED);
Hist.DefineColor("Negative and Up", Color.YELLOW);
Hist.AssignValueColor(if Hist >= 0 then if Hist > Hist then Hist.Color("Positive and Up") else Hist.Color("Positive and Down") else if Hist < Hist then Hist.Color("Negative and Down") else Hist.Color("Negative and Up"));

#Arrows
input usearrows = yes;
plot arrowup = if usearrows and Hist crosses above 0 then 0 else Double.NaN;
arrowup.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
arrowup.SetDefaultColor(Color.GREEN);
arrowup.SetLineWeight(3);
plot arrowdn = if usearrows and Hist crosses below 0 then 0 else Double.NaN;
arrowdn.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
arrowdn.SetDefaultColor(Color.RED);
arrowdn.SetLineWeight(3);
#Clouds
def cond1 = if Hist > 0
then Double.POSITIVE_INFINITY
else Double.NEGATIVE_INFINITY;
def cond2 = if Hist < 0
then Double.POSITIVE_INFINITY
else Double.NEGATIVE_INFINITY;
input showclouds = yes;
then cond1
else Double.NaN,
cond2,
Color.WHITE, Color.GRAY);

• APOT7

#### APOT7

##### Member
VIP
@Bladeof300 thank bro!... this is what I wanted. I just changed histogram to line in plot setting as well as changed colors. ...Thank u so much!

This seems to be more accurate! thanks.

• #### joe362

##### Member
How do you define "moving upward"?

Define "moving downward"?

How do we get a positive value under 0?

Same as above. How can the value go from positive to negative if it's still above 0?

Here is the best I can do. Green if above 0 and red if below .

Code:
``````# True Strength Index

declare lower;

input longLength = 25;
input shortLength = 13;
input signalLength = 8;
input averageType = AverageType.EXPONENTIAL;

def diff = close - close;
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.AssignValueColor(if TSI > 0 then color.green else color.red);``````
Hi Benten,
Your scrip is amazing. Can you create another code for the watchlist column?
Thanks

#### BenTen

Staff
VIP
@joe362 Sure. try this:

Code:
``````# True Strength Index
input longLength = 25;
input shortLength = 13;
input signalLength = 8;
input averageType = AverageType.EXPONENTIAL;

def diff = close - close;
def doubleSmoothedAbsDiff = MovingAverage(averageType, MovingAverage(averageType, AbsValue(diff), longLength), shortLength);
def TSI = if doubleSmoothedAbsDiff == 0 then 0 else 100 * (MovingAverage(averageType, MovingAverage(averageType, diff, longLength), shortLength)) / doubleSmoothedAbsDiff;
plot value = TSI;``````

• ##### New member
VIP
@joe362 Sure. try this:

Code:
``````# True Strength Index
input longLength = 25;
input shortLength = 13;
input signalLength = 8;
input averageType = AverageType.EXPONENTIAL;

def diff = close - close;
def doubleSmoothedAbsDiff = MovingAverage(averageType, MovingAverage(averageType, AbsValue(diff), longLength), shortLength);
def TSI = if doubleSmoothedAbsDiff == 0 then 0 else 100 * (MovingAverage(averageType, MovingAverage(averageType, diff, longLength), shortLength)) / doubleSmoothedAbsDiff;
plot value = TSI;``````
Once again... for the forgetful.. how does one set a Alert, on a watch-list, using only this True Strength Index, for crossover from negative to positive.
I find a very significant different ranging on charting due to 'Average type settings and time frames on the TOS main chart and the number for the watchlist True strength index number... this difference between 'watch-list and main TOS charting has always eluded me where numbers are never the same in my setups. Perhaps a builtin divergence unavoidable?
2 questions, so I might be confusing.

#### BenTen

Staff
VIP
• #### joe362

##### Member
@joe362 Sure. try this:

Code:
``````# True Strength Index
input longLength = 25;
input shortLength = 13;
input signalLength = 8;
input averageType = AverageType.EXPONENTIAL;

def diff = close - close;
def doubleSmoothedAbsDiff = MovingAverage(averageType, MovingAverage(averageType, AbsValue(diff), longLength), shortLength);
def TSI = if doubleSmoothedAbsDiff == 0 then 0 else 100 * (MovingAverage(averageType, MovingAverage(averageType, diff, longLength), shortLength)) / doubleSmoothedAbsDiff;
plot value = TSI;``````
thanks so much

#### Mrq77

##### New member
@APOT7 Found this one as well it follows a 2 line cross instead of a ZeroLine cross. This is supposed to make the TSI more accurate. I put the same color scheme and line/histogram option in here but it is called Hist instead of TSI.

Code:
``````##ONE NOTE
#Wednesday, May 15, 2019 - archived 1
def Data = close;
def NA = Double.NaN;
#NA.SetDefaultColor(Color.BLACK);
#NA.HideTitle();
declare lower;
input IndicatorLine = 32;
input Smooth = 5;
input SignalLine = 5;
plot TSIFast = TrueStrengthIndex(LongLength = IndicatorLine, ShortLength = Smooth);
plot TSISlow = ExpAverage(TSIFast, SignalLine);
plot zero = 0;
zero.SetDefaultColor(Color.WHITE);
zero.HideBubble();
zero.HideTitle();
plot Fast = TSIFast;
Fast.SetDefaultColor(Color.BLUE);
plot Slow = TSISlow;
Slow.SetDefaultColor(Color.WHITE);
Slow.SetStyle(Curve.SHORT_DASH);
plot HighLvl = 25;
HighLvl.SetDefaultColor(Color.LIGHT_GREEN);
HighLvl.HideBubble();
HighLvl.HideTitle();
plot LowLvl = -25;
LowLvl.SetDefaultColor(Color.RED);
LowLvl.HideBubble();
LowLvl.HideTitle();

plot Hist = (TSIFast - TSISlow) * 2;
#TSI.SetPaintingStrategy(PaintingStrategy.LINE);
Hist.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Hist.SetLineWeight(3);
Hist.DefineColor("Positive and Up", Color.GREEN);
Hist.DefineColor("Positive and Down", (CreateColor(153, 0, 242)));
Hist.DefineColor("Negative and Down", Color.RED);
Hist.DefineColor("Negative and Up", Color.YELLOW);
Hist.AssignValueColor(if Hist >= 0 then if Hist > Hist then Hist.Color("Positive and Up") else Hist.Color("Positive and Down") else if Hist < Hist then Hist.Color("Negative and Down") else Hist.Color("Negative and Up"));

#Arrows
input usearrows = yes;
plot arrowup = if usearrows and Hist crosses above 0 then 0 else Double.NaN;
arrowup.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
arrowup.SetDefaultColor(Color.GREEN);
arrowup.SetLineWeight(3);
plot arrowdn = if usearrows and Hist crosses below 0 then 0 else Double.NaN;
arrowdn.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
arrowdn.SetDefaultColor(Color.RED);
arrowdn.SetLineWeight(3);
#Clouds
def cond1 = if Hist > 0
then Double.POSITIVE_INFINITY
else Double.NEGATIVE_INFINITY;
def cond2 = if Hist < 0
then Double.POSITIVE_INFINITY
else Double.NEGATIVE_INFINITY;
input showclouds = yes;
then cond1
else Double.NaN,
cond2,
Color.WHITE, Color.GRAY);
This indicator Is excellent. Can anyone help me with a scan for when the chart shows the arrows? In the condition wizard I set it to show when "arrow down is true" but doesn't seem to be working.

##### Well-known member
VIP
@Mrq77 Welcome to the usethinkscript forums... The easiest way to perform the scan would be to save one of the scripts above that has the arrows and then reference that Study from within the scanner and then select the plot that represents the arrow you are scanning for...

#### MerryDay

##### Well-known member
VIP
@Mrq77 The way the arrow statements are written could be messing up your ability to scan for them because they are set to print on the 0 line and the scanner says that 0 means false so it might not be returning results???

My Work-A-Round: Arrows plot when Hist crosses above 0 (bullish) or crosses below 0 (bear).
Set your scanner to look for Hist crosses above 0. When I ran it on a 15min agg, it got over 1000 equities that fit that condition • Mrq77

#### Mrq77

##### New member
@Mrq77 The way the arrow statements are written could be messing up your ability to scan for them because they are set to print on the 0 line and the scanner says that 0 means false so it might not be returning results???

My Work-A-Round: Arrows plot when Hist crosses above 0 (bullish) or crosses below 0 (bear).
Set your scanner to look for Hist crosses above 0. When I ran it on a 15min agg, it got over 1000 equities that fit that condition Thank you very much this is exactly what I was looking for. I was conditioning the histogram against the zero line in the indicator instead just crossing over a value of 0.0.

• MerryDay