Stochastic Momentum Index for ThinkorSwim

GGC

New member
Stochastic momentum index and I would like to have a down arrow is SMI crosses below AVGSMI (with the crossing happening above the overbought line), and an up arrow if SMI crosses above AVGSMI (with the crossing happening below the oversold line). Thanks :)
 
Last edited by a moderator:

Pensar

Well-known member
VIP
Sure, here you go. Please verify that the crossing signals are plotting where you want.

Code:
# Stochastic Momentum Index
# Base code by TD Ameritrade IP Company, Inc. (c) 2008-2020
# Added arrows and alerts when SMI/AvgSMI cross above/below the oversold/overbought lines

declare lower;

input over_bought = 40.0;
input over_sold = -40.0;
input percentDLength = 3;
input percentKLength = 5;
input alerts = yes;

def min_low = lowest(low, percentKLength);
def max_high = highest(high, percentKLength);
def rel_diff = close - (max_high + min_low)/2;
def diff = max_high - min_low;

def avgrel = expaverage(expaverage(rel_diff, percentDLength), percentDLength);
def avgdiff = expaverage(expaverage(diff, percentDLength), percentDLength);

plot SMI = if avgdiff != 0 then avgrel / (avgdiff / 2) * 100 else 0;
smi.setDefaultColor(getColor(1));

plot AvgSMI = expaverage(smi, percentDLength);
avgsmi.setDefaultColor(getcolor(5));

plot overbought = over_bought;
overbought.setDefaultColor(getcolor(5));

plot oversold = over_sold;
oversold.setDefaultColor(getcolor(5));

def signalUp = AvgSMI[1] < oversold and SMI[1] < AvgSMI[1] and SMI > AvgSMI;
def signalDn = AvgSMI[1] > overbought and SMI[1] > AvgSMI[1] and SMI < AvgSMI;

plot arrowUp = if signalUp then AvgSMI else double.nan;
     arrowUp.SetPaintingStrategy(PaintingStrategy.Arrow_Up);
     arrowUp.SetDefaultColor(color.green);

plot arrowDn = if signalDn then AvgSMI else double.nan;
     arrowDn.SetPaintingStrategy(PaintingStrategy.Arrow_Down);
     arrowDn.SetDefaultColor(color.red);

Alert(alerts and signalUp, " Arrow Up! ", alert.bar, sound.ring);
Alert(alerts and signalDn, " Arrow Down! ", alert.bar, sound.ring);

#end code

Edit: Modified code to add alerts for the arrow signals.
 
Last edited:

rwfarrell

New member
Hey guys, I am on this board enough and I am only a beginner here with ThinkScript but here is my contribution to the group. For anyone who uses the Stochastic Momentum Index indicator. Here is a script that changes the candle colors when the SMI crosses the AvgSMI line. This can be used on any timeframe. Copy and paste the code. I call it SMICandles, make sure to drop the script in the volume section of your Studies and not in the price or chart area of Studies.

Code:
#Hint Violet candles = SMI 40 or greater
#Hint White candles = SMI -40 or less

plot SMI = StochasticMomentumIndex();
plot OverBought = 40;
plot OverSold = -40;

SMI.DefineColor("OverBought", Color.VIOLET);
SMI.DefineColor("OverSold", Color.yellow);

AssignPriceColor(if SMI >= OverBought then SMI.color("OverBought") else if SMI <= OverSold then SMI.color("OverSold") else Color.current);

def Condition1 = (StochasticMomentumIndex()."SMI" crosses below StochasticMomentumIndex()."AvgSMI");

def Condition2 = (StochasticMomentumIndex()."SMI" crosses above StochasticMomentumIndex()."AvgSMI");

               
AssignPriceColor
(if Condition1 then Color.plum
else Color.CURRENT);

AssignPriceColor
(if Condition2 then color.cyan
else Color.CURRENT);
 
Last edited by a moderator:

Duece

New member
I am looking to get an audio alert when the SMI (stochasticmomentumindex) likes cross each other either up or down, but have no clue how to make that happen.........any help really appreciated.....TY.
 

horserider

Well-known member
VIP
@Duece This might work , not tested.

Code:
input sound = yes;
Alert(sound and SMI crosses above avgsmi, "SMI Long",  Alert.BAR, Sound.Ring);
Alert(sound and SMI crosses below avgsmi, "SMI Short",  Alert.BAR, Sound.Ring);
 

rbark3r

New member
VIP
Hi Guys,

would appreciate it if you guys could make a simple customization to the code below.

Overbought and oversold are currently set to -50 and 50

I'm hoping you guys can insert a cloud within the following range -15 to 15.

I'd like to ignore any crosses inside of that range.

Thanks in advance
--------------------------------------------------------------------------------------------------------------

Code:
# Stochastic Momentum Index
# Base code by TD Ameritrade IP Company, Inc. (c) 2008-2020
# Added arrows and alerts when SMI/AvgSMI cross above/below the oversold/overbought lines

declare lower;

input over_bought = 50.0;
input over_sold = -50.0;
input percentDLength = 3;
input percentKLength = 5;
input alerts = yes;

def min_low = lowest(low, percentKLength);
def max_high = highest(high, percentKLength);
def rel_diff = close - (max_high + min_low)/2;
def diff = max_high - min_low;

def avgrel = expaverage(expaverage(rel_diff, percentDLength), percentDLength);
def avgdiff = expaverage(expaverage(diff, percentDLength), percentDLength);

plot SMI = if avgdiff != 0 then avgrel / (avgdiff / 2) * 100 else 0;
smi.setDefaultColor(getColor(1));

plot AvgSMI = expaverage(smi, percentDLength);
avgsmi.setDefaultColor(getcolor(5));

plot overbought = over_bought;
overbought.setDefaultColor(getcolor(5));

plot oversold = over_sold;
oversold.setDefaultColor(getcolor(5));

def signalUp = AvgSMI[1] < oversold and SMI[1] < AvgSMI[1] and SMI > AvgSMI;
def signalDn = AvgSMI[1] > overbought and SMI[1] > AvgSMI[1] and SMI < AvgSMI;

plot arrowUp = if signalUp then AvgSMI else double.nan;
     arrowUp.SetPaintingStrategy(PaintingStrategy.Arrow_Up);
     arrowUp.SetDefaultColor(color.green);

plot arrowDn = if signalDn then AvgSMI else double.nan;
     arrowDn.SetPaintingStrategy(PaintingStrategy.Arrow_Down);
     arrowDn.SetDefaultColor(color.red);

Alert(alerts and signalUp, " Arrow Up! ", alert.bar, sound.ring);
Alert(alerts and signalDn, " Arrow Down! ", alert.bar, sound.ring);

#end code
 

BenTen

Administrative
Staff member
Staff
VIP
@rbark3r To add the cloud, insert the following code to the bottom of your script:

Code:
plot range1 = 15;
plot range2 = -15;
AddCloud(range1, range2, color.yellow, color.yellow);
 

Optionsguy

New member
Hi All,

I was wondering if anyone knew how to create a scan that would find stocks that have an Stochastic Momentum Index “SMI” and “AvgSMI” within a value of 1 of each other?

For instance, a SMI value of 10 and an AvgSMI value of 11.

Any help is greatly appreciated!
 

XeoNoX

Well-known member
VIP
remember to thumbs up if you found this post useful @Optionsguy

“SMI” and “AvgSMI” Scan Within Value of 1​

Code:
#By XeoNoX Via Usethinkscript.com
#SMI and SMI Average within XYZ
input avg_length = 60;
def SMI = StochasticMomentumIndex()."SMI";
def SMIAverage = average(SMI,avg_length);
def diff = absValue(smi-SMIaverage);
plot scan = diff <1;
 

Optionsguy

New member
Thank you so much! What would be the average length for the default settings on TOS for the SMI? I can’t seem to find it, but I don’t think it is 60

I see:
  • D length=3
  • K length=5
 

XeoNoX

Well-known member
VIP
you stated:
find stocks that have an Stochastic Momentum Index “SMI” and “AvgSMI”

60 is the average of SMI Length... or what you reffered to as AvgSMI... you can change 60 to whatever average you want..... your K&D Length is whatever the default is built into thinkorswim.
 

Optionsguy

New member
you stated:
find stocks that have an Stochastic Momentum Index “SMI” and “AvgSMI”

60 is the average of SMI Length... or what you reffered to as AvgSMI... you can change 60 to whatever average you want..... your K&D Length is whatever the default is built into thinkorswim.
Hmm..okay, when I scan with this code, it brings up stocks that aren’t within 1 when lined up with the default settings.

I was looking to scan by the default settings, but I’m not sure what to put in for the length to match it up with the default.
 

XeoNoX

Well-known member
VIP
i tried it and it came up with 3 results on the daily. you appear to be confusing the average smi length with the averages that are part of the smi. study.

Think of it like this, what you requested was an average of 2 compared averages

personally i dont know what purpose it will serve, but maybe you might have stumbled upon something special 😆
 

cabe1332

Active member
I appreciate the detailed responses! Thank you for the help
@Optionsguy @XeoNoX I added this indicator with modifications like square points instead of arrows, cloud, and price bubble. It said to work much better on trending stocks, so I trend painter script with the stochasticmomoindex to have a better entry plot. To explains things short, the TrendReversal I found more accurate with Buy/Sell signals (screenshots below). It was worth a try. Good luck! @cabe1332

Unless @Optionsguy has better process or projections on how he uses the stochasticmomoindex.


tF7Ls50.png


6iJw411.png
 

analyst2345

New member
@Optionsguy @XeoNoX I added this indicator with modifications like square points instead of arrows, cloud, and price bubble. It said to work much better on trending stocks, so I trend painter script with the stochasticmomoindex to have a better entry plot. To explains things short, the TrendReversal I found more accurate with Buy/Sell signals (screenshots below). It was worth a try. Good luck! @cabe1332

Unless @Optionsguy has better process or projections on how he uses the stochasticmomoindex.


tF7Ls50.png


6iJw411.png
Can you please share the script/code for the Stochastic momentumidex_20210801 and the buy sell signal on chart. Thanks.
 

MerryDay

Administrative
Staff member
Staff
VIP
@analyst2345 Welcome to the Forum! The way the site works, is that someone will submit the script and any questions that they have and everybody then chimes in with their thoughts.

So the script/code being discussed is the basic built-in TOS StochasticMomentumIndex to which the original poster added things like arrows. When you are reading a thread, it is always safe to start w/ the study in post#1. You can add it to your charts, get to understand it, google both this forum and the internet for the pros and the caveats of using this indicator. Then you can adapt that code w/ what others have contributed if you want to.
@cabe1332 turned the arrows into squares. You can make them anything you want by going to settings. @cab1332 also made pretty with some cloud coloring and a price bubble. Just in case, you are not yet well-versed in this type of formatting, Below is a script of the TOS SMI w/ @cabe1332's pretty formatting features. Review the code and read up in the TOS library because you can use these formatting features on any TOS or Custom study.
a2.png

Ruby:
# ########################################################
declare lower;

plot SMI = StochasticMomentumIndex()."SMI";
plot AvgSMI = StochasticMomentumIndex()."AvgSMI";
plot OverBought = 40;
plot OverSold = -40;
plot zeroline = 0 ;
zeroline.SetPaintingStrategy(PaintingStrategy.DASHES);

def signalUp = AvgSMI[1] < oversold and SMI[1] < AvgSMI[1] and SMI > AvgSMI;
def signalDn = AvgSMI[1] > overbought and SMI[1] > AvgSMI[1] and SMI < AvgSMI;

plot Up = if signalUp then AvgSMI else double.NaN ;
Up.SetPaintingStrategy(PaintingStrategy.SQUARES);
Up.SetDefaultColor(color.green) ;
Up.SetLineWeight(5);
plot Down = if  signalDn then AvgSMI  else double.NaN ;
Down.SetPaintingStrategy(PaintingStrategy.SQUARES);
Down.SetDefaultColor(color.red) ;
Down.SetLineWeight(5);
AddCloud(SMI, AvgSMI, color.light_green, color.pink);
AddChartBubble(signalUp, 0, close, Color.light_orange, no);
# ########################################################
Shared Study Link: http://tos.mx/JY5P6yP Click here for --> Easiest way to load shared links
 

cabe1332

Active member
@analyst2345 Welcome to the Forum! The way the site works, is that someone will submit the script and any questions that they have and everybody then chimes in with their thoughts.

So the script/code being discussed is the basic built-in TOS StochasticMomentumIndex to which the original poster added things like arrows. When you are reading a thread, it is always safe to start w/ the study in post#1. You can add it to your charts, get to understand it, google both this forum and the internet for the pros and the caveats of using this indicator. Then you can adapt that code w/ what others have contributed if you want to.
@cabe1332 turned the arrows into squares. You can make them anything you want by going to settings. @cab1332 also made pretty with some cloud coloring and a price bubble. Just in case, you are not yet well-versed in this type of formatting, Below is a script of the TOS SMI w/ @cabe1332's pretty formatting features. Review the code and read up in the TOS library because you can use these formatting features on any TOS or Custom study.
View attachment 750
Ruby:
# ########################################################
declare lower;

plot SMI = StochasticMomentumIndex()."SMI";
plot AvgSMI = StochasticMomentumIndex()."AvgSMI";
plot OverBought = 40;
plot OverSold = -40;
plot zeroline = 0 ;
zeroline.SetPaintingStrategy(PaintingStrategy.DASHES);

def signalUp = AvgSMI[1] < oversold and SMI[1] < AvgSMI[1] and SMI > AvgSMI;
def signalDn = AvgSMI[1] > overbought and SMI[1] > AvgSMI[1] and SMI < AvgSMI;

plot Up = if signalUp then AvgSMI else double.NaN ;
Up.SetPaintingStrategy(PaintingStrategy.SQUARES);
Up.SetDefaultColor(color.green) ;
Up.SetLineWeight(5);
plot Down = if  signalDn then AvgSMI  else double.NaN ;
Down.SetPaintingStrategy(PaintingStrategy.SQUARES);
Down.SetDefaultColor(color.red) ;
Down.SetLineWeight(5);
AddCloud(SMI, AvgSMI, color.light_green, color.pink);
AddChartBubble(signalUp, 0, close, Color.light_orange, no);
# ########################################################
Shared Study Link: http://tos.mx/JY5P6yP Click here for --> Easiest way to load shared links
@MerryDay @analyst2345, I stopped using the Stochastic Momentum Index even the one I mod. It looked pretty and all, but I find it does not fit my strategy. The buy/sell signals were not accurate or too many. I revert back to what works best and profits and cut down on indicators I don't need. You can say "it gets better as you earn". Good luck! Thanks, @MerryDay for sharing the code. @cabe1332
 

Jered

New member
@MerryDay @analyst2345, I stopped using the Stochastic Momentum Index even the one I mod. It looked pretty and all, but I find it does not fit my strategy. The buy/sell signals were not accurate or too many. I revert back to what works best and profits and cut down on indicators I don't need. You can say "it gets better as you earn". Good luck! Thanks, @MerryDay for sharing the code. @cabe1332
While I have just signed up and noticed your use of the SMI, I have found that when the fast crosses the slow in a very defined angle, it works best when getting in on a trade early.
 

rbark3r

New member
VIP
@MerryDay @analyst2345, I stopped using the Stochastic Momentum Index even the one I mod. It looked pretty and all, but I find it does not fit my strategy. The buy/sell signals were not accurate or too many. I revert back to what works best and profits and cut down on indicators I don't need. You can say "it gets better as you earn". Good luck! Thanks, @MerryDay for sharing the code. @cabe1332
What do you use now/instead?
 

Similar threads

Top