In this blog we will share three TOS Scripts that will enable you to identify stocks that are outperforming the market. You will find stocks that are in the top percentile or the lowest percentile to go long or short. You will be able to plot the number in a column code that ranges from 1-99 with 99 being the strongest or in the top one percentile. Likewise for the bottom. This also comes with a lower study where you can track RS over a period of time. Finally a script to scan for stocks.
http://tos.mx/P70ehp9 This script will add a column ranking the stocks from top to bottom percentile. Here's the code if you want to cut/paste in the column code.
####################### Start of Code ##########
input CorrelationWithSecurity = "SPX";
input CorrelationWithSecurity = "SPX";
#Calculation TimeFrame
def aggregationperiod = AggregationPeriod.DAY;
def indexclose = close(CorrelationWithSecurity, aggregationperiod);
def RSclose = close / indexclose;
def barCount = IF !IsNaN(close) THEN IF IsNaN(barCount[1]) THEN 1 ELSE barCount[1] + 1 ELSE barCount[1];
#Normalize Relative Strength
def newRngMax = 99; #Maximum normalized value
def newRngMin = 1; #Minimum normalized value
def HHDataclose = Highest(RSclose, 252);
def LLDataclose = Lowest(RSclose, 252);
def normalizeRSclose = ((( newRngMax - newRngMin ) * ( RSclose - LLDataclose )) / ( HHDataclose - LLDataclose )) + newRngMin;
Plot IBDRSI = Round(normalizeRSclose,0);
AssignBackgroundColor(if IBDRSI > 89 then color.dark_green else if IBDRSI <9 then color.DARK_RED else color.gray);
#IBDRSI.assignValueColor(if IBDRSI < 11 then color.white else if IBDRSI > 89 then createcolor(255,255,255) else createcolor(0,0,0));
#End Script
2. Lower Study for IBD Strength. This is written by another author and ported to TOS by us. Credits in the header file.
http://tos.mx/XiPqdZx Import and save this link. The code is posted as well.
######### Start of Code#############################################
# Plot the Relative Strength Line in ThinkorSwim
# Written by: @Diamond_Stocks @RayTL_ - TLScripts
# Site: https://www.traderlion.com/ https://www.traderlion.com/tl-scripts/
declare lower;
# User Inputs
input show_RSNHBP = yes; #Hint show_RSNHBP: Set to "Yes" to display RS New High Before Price (RSNHBP). The Pink Dot
input show_RSNH = yes; #Hint show_RSNH: Set to "Yes" to display RS New High. The Green Dot.
#Relative Strength Type - Candlestick to be added later.
input graphStyle = {default "Line"}; #Hint graphStyle: Only available format is Line.
#3 Month, 6 Month, 1 Year RS
input TimeFrame = {default "Three_Months", "Six_Months", "1_Year"}; #Hint TimeFrame: Select the appropriate timeframe to calculate Relative Strength.
#Index SymbolRelation
input CorrelationWithSecurity = "SPX"; #Hint CorrelationWithSecurity: Select appropriate Index against which Relative Strength will be calculated.
#Calculation TimeFrame
def aggregationperiod = AggregationPeriod.DAY;
#Chart Normalized Relative Strength Rating on Last Bar
def isLastBar = BarNumber() == HighestAll(if !IsNaN(close) then BarNumber() else Double.NaN);
#Turn on or off Alerts when cycling charts
input Alerts_On = yes; #Hint Alerts_On: Set to "Yes" to receive sound alerts when triggered.
#Add Chart Bubbble to Graph
input RS_Rating_ChartBubble = yes; #Hint RS_Rating_ChartBubble: Set to "Yes" to display current Normalized RS Rating (not the same as MarketSmith).
#Establish look back length:
def Length = if TimeFrame == TimeFrame.Three_Months then 63 else if TimeFrame == TimeFrame.Six_Months then 126 else 252;
#Get Index Close/Open/High/Low - Prep for Candlestick RS.
def indexopen = open(CorrelationWithSecurity, aggregationperiod);
def indexhigh = high(CorrelationWithSecurity, aggregationperiod);
def indexlow = low(CorrelationWithSecurity, aggregationperiod);
def indexclose = close(CorrelationWithSecurity, aggregationperiod);
#Get Relative Strength - Prep for Candlestick RS.
def RSopen = open / indexopen;
def RShigh = high / indexhigh;
def RSlow = low / indexlow;
def RSclose = close / indexclose;
def barCount = IF !IsNaN(close) THEN IF IsNaN(barCount[1]) THEN 1 ELSE barCount[1] + 1 ELSE barCount[1];
#Normalize Relative Strength
def newRngMax = 99; #Maximum normalized value
def newRngMin = 1; #Minimum normalized value
def HHDataclose = HighestAll(RSclose);
def LLDataclose = LowestAll(RSclose);
def HHDataopen = HighestAll(RSopen);
def LLDataopen = LowestAll(RSopen);
def HHDatalow = HighestAll(RSlow);
def LLDatalow = LowestAll(RSlow);
def HHDatahigh = HighestAll(RShigh);
def LLDatahigh = LowestAll(RShigh);
def normalizeRSclose = ((( newRngMax - newRngMin ) * ( RSclose - LLDataclose )) / ( HHDataclose - LLDataclose )) + newRngMin;
def normalizeRSopen = ((( newRngMax - newRngMin ) * ( RSopen - LLDataopen )) / ( HHDataopen - LLDataopen )) + newRngMin;
def normalizeRShigh = ((( newRngMax - newRngMin ) * ( RShigh - LLDatahigh )) / ( HHDatahigh - LLDatahigh )) + newRngMin;
def normalizeRSlow = ((( newRngMax - newRngMin ) * ( RSlow - LLDatalow )) / ( HHDatalow - LLDatalow )) + newRngMin;
#Chart RS Line and set appearence:
plot RSLine = RSclose;
RSLine.DefineColor("Positive", Color.UPTICK);
RSLine.DefineColor("Negative", Color.DOWNTICK);
RSLine.SetLineWeight(1);
RSLine.AssignValueColor(if RSLine[0] > RSLine[1] then CreateColor(43, 152, 242) else CreateColor(227, 88, 251));
#Get Highest RS Value
def highestRS = Highest(RSclose, Length);
def RSNHBPcondition = if RSclose >= highestRS and close < Highest(close, Length) then highestRS else no;
#Plot RSNHBP Condition
plot RSNHBP = if show_RSNHBP and RSNHBPcondition == highestRS then highestRS else Double.NaN;
#Plot RSNH Condition
plot RSNH = if show_RSNH and RSNHBPcondition == no and RSclose == highestRS and isLastBar then highestRS else Double.NaN;
#Appearance Settings
RSNHBP.SetPaintingStrategy(PaintingStrategy.POINTS);
RSNHBP.SetLineWeight(2);
RSNHBP.SetDefaultColor(CreateColor(196, 94, 225));
RSNHBP.HideBubble();
RSNH.SetPaintingStrategy(PaintingStrategy.POINTS);
RSNH.SetDefaultColor(Color.GREEN);
RSNH.SetLineWeight(2);
RSNH.HideBubble();
#Add Chart Bubble for RS Rating
AddChartBubble(RS_Rating_ChartBubble and isLastBar, RSclose, "RS: " + Round(normalizeRSclose, 0), Color.WHITE, no);
#Add Label
AddLabel(yes, Concat("Relative Strength: ", Round(normalizeRSclose, 0)), CreateColor(43, 152, 242));
#Alert Capability
Alert( if Alerts_On and RSNHBP then yes else no, "Relative Strength Line New Highs Before Price", Alert.BAR, Sound.Ding);
Alert(if Alerts_On and RSNH then yes else no, "Relative Strength Line New Highs", Alert.BAR, Sound.Chimes);
########### End of Code##################################################
3. Scan for Stocks.
http://tos.mx/AFcWbVo Scan for Stocks with high Relative Strength. In the code we are looking for stocks with RS > 98, however, you can adjust that number to your taste.
##### start of code please look at the bottom and adjust scan as needed##########
input CorrelationWithSecurity = "SPX";
def indexclose = close(CorrelationWithSecurity);
def RSclose = close / indexclose;
#Normalize Relative Strength
def newRngMax = 99; #Maximum normalized value
def newRngMin = 1; #Minimum normalized value
def HHDataclose = Highest(RSClose, 252);
def LLDataclose = Lowest(RSclose, 252);
plot normalizedRSclose = ((( newRngMax - newRngMin ) * ( RSclose - LLDataclose )) / ( HHDataclose - LLDataclose )) + newRngMin > 98;
Amazing work...thank you very much for your sharing.