Top 5 Ticker Divergence Indicator For ThinkOrSwim

FutureTony

Well-known member
VIP
Hi again,

I'm back with a twist on a concept I have seen mentioned on Twitter a few times. The idea is to track the percentage change of the 5 biggest stocks in the market as to provide a modified view of market internals. In theory, the movement of the biggest stocks will define the movement of the market and when trending strongly in one direction, the market will follow suit. When divergences occur, one should take note.
This study has two 'modes'. One that visualizes as a histogram that plots the movement of the underlying 'top 5' for each candle:
CsfzR3a.png


In the image above, I have the divergence activated to paint the candles when divergence shows up. The other mode is as a moving average:
1kixthz.png

Using the average might be a bit easier to visually identify divergence as new highs/lows are made. Crossovers might also be useful in identifying trend exhaustion. Both versions will default to adding a label showing the current 'trend'. This can be switched off or can show the actual averages and current trend for more detailed information.
A few notes. The divergence calculation for candle coloring is based on an 'extreme' reading. I have not come up with a great way of standardizing these levels across all timeframes. I have found this works best of a 1 or 2 minute chart where the value is set to 2 or 3. You may have to experiment to come up with a suitable level on different timeframes. In addition, this study references those other 5 tickers so cannot be run on tick or range charts. And finally, because this is using stock tickers, I find it best to run against /ES or /NQ on time charts that do have extended hours turned OFF. As a result, the average portion of the study requires a few candles at the start of the session before it starts printing:
K3Yklv9.png

Give it a whirl and let me know if anyone has thoughts on standardizing the extreme levels or other suggestions for improvement.

Ruby:
# created by @tony_futures to identify trend and divergence between the futures and the top 5 tickers

declare lower;
input TICKER = "AAPL";
input TICKER2 = "MSFT";
input TICKER3 = "AMZN";
input TICKER4 = "TSLA";
input TICKER5 = "GOOGL";

def stock1 = close(symbol=TICKER);
def stock2 = close(symbol=TICKER2);
def stock3 = close(symbol=TICKER3);
def stock4 = close(symbol=TICKER4);
def stock5 = close(symbol=TICKER5);

def percentChange = (((stock1 - stock1[1]) + (stock2 - stock2[1]) + (stock3 - stock3[1]) + (stock4 - stock4[1]) + (stock5 - stock5[1])) / (stock1[1] + stock2[1] + stock3[1] + stock4[1] + stock5[1])) * 1000;

input showHisto = yes;
def rth   = secondsfromTime(0930)>=0 and secondsfromTime(1600)<0;
plot change = if showHisto and rth then percentChange else Double.NaN;
change.setPaintingStrategy(PaintingStrategy.HISTOGRAM);
DefineGlobalColor("bullish", CreateColor(51,107,135));
DefineGlobalColor("extrabullish", CreateColor(51,107,255));
DefineGlobalColor("bearish", CreateColor(208,150,131));
DefineGlobalColor("extrabearish", CreateColor(255,150,131));
DefineGlobalColor("neutral", Color.GRAY);

plot zero = if showHisto then 0 else Double.NaN;
zero.setDefaultColor(GlobalColor("neutral"));

input extremeLevel = 2;
plot bullish = if showHisto then extremeLevel else Double.NaN;
bullish.setDefaultColor(GlobalColor("neutral"));
bullish.hideBubble();

plot bearish = if showHisto then -extremeLevel else Double.NaN;
bearish.setDefaultColor(GlobalColor("neutral"));
bearish.hideBubble();

change.DefineColor("Up", GlobalColor("bullish"));
change.DefineColor("ExtraUp", GlobalColor("extrabullish"));
change.DefineColor("Down", GlobalColor("bearish"));
change.DefineColor("ExtraDown", GlobalColor("extrabearish"));
change.AssignValueColor(if percentChange > bullish then change.color("ExtraUp") else if percentChange >= 0 then change.color("Up") else if percentChange < bearish then change.color("ExtraDown") else change.color("Down"));
change.hideBubble();

def NYOpen = 0930;
def isNYOpen = secondsFromTime(NYOpen) == 0;

def upAvg = if isNYOpen then 0 else if percentChange > 0 then upAvg[1] + percentChange else upAvg[1];
def downAvg = if isNYOpen then 0 else if percentChange < 0 then downAvg[1] + percentChange else downAvg[1];
def totalAvg = upAvg + downAvg;
def totalAvgChange = totalAvg - totalAvg[5];
def bearishAvg = (totalAvg < (-extremeLevel * 10));
def bullishAvg = (totalAvg > (extremeLevel * 10));

input showLabels = yes;
input showAvgs = no;
AddLabel(showLabels and showAvgs,"Avg: " + Round(totalAvg,1) + " | " + Round((totalAvgChange),1), if bearishAvg then GlobalColor("bearish") else if bullishAvg then GlobalColor("bullish") else GlobalColor("neutral"));
AddLabel(showLabels and !showAvgs and bullishAvg, "Momo Up",GlobalColor("bullish"));
AddLabel(showLabels and !showAvgs and bearishAvg, "Momo Down",GlobalColor("bearish"));
AddLabel(showLabels and !showAvgs and !bullishAvg and !bearishAvg, "Neutral",GlobalColor("neutral"));

bullish.DefineColor("Up", GlobalColor("bullish"));
bullish.DefineColor("Down", GlobalColor("bearish"));
bullish.AssignValueColor(if bullishAvg then bullish.color("Up") else if bearishAvg then bullish.color("Down") else Color.CURRENT);
bearish.DefineColor("Up", GlobalColor("bullish"));
bearish.DefineColor("Down", GlobalColor("bearish"));
bearish.AssignValueColor(if bullishAvg then bullish.color("Up") else if bearishAvg then bullish.color("Down") else Color.CURRENT);

plot avg = if rth and !showHisto then totalAvg else Double.NaN;
avg.setDefaultColor(GlobalColor("bearish"));

plot avgAVG = if !showHisto then Average(totalAvg,10) else Double.NaN;
avgAVG.setDefaultColor(GlobalColor("neutral"));

def isUp = close > open;
def isDown = close < open;
input showArrows = no;
def bullDiv = isDown[2] and isDown[1] and (percentChange[1] > 0) and (percentChange > (extremeLevel/2));
def bearDiv = isUp[2] and isUp[1] and (percentChange[1] < 0) and (percentChange < (-extremeLevel/2));
plot divUp = if showArrows and bullDiv and showHisto then zero else if showArrows and bullDiv then avg else Double.NaN;
divUp.setPaintingStrategy(PaintingStrategy.ARROW_UP);
divUp.setDefaultColor(GlobalColor("neutral"));

plot divDown = if showArrows and bearDiv and showHisto then zero else if showArrows and bearDiv then avg else Double.NaN;
divDown.setPaintingStrategy(PaintingStrategy.ARROW_DOWN);
divDown.setDefaultColor(GlobalColor("neutral"));

input colorCandles = no;
AssignPriceColor(if colorCandles and bullDiv then Color.GREEN else Color.CURRENT);
AssignPriceColor(if colorCandles and bearDiv then Color.RED else Color.CURRENT);
 
Last edited:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Hi again,

I'm back with a twist on a concept I have seen mentioned on Twitter a few times. The idea is to track the percentage change of the 5 biggest stocks in the market as to provide a modified view of market internals. In theory, the movement of the biggest stocks will define the movement of the market and when trending strongly in one direction, the market will follow suit. When divergences occur, one should take note.
This study has two 'modes'. One that visualizes as a histogram that plots the movement of the underlying 'top 5' for each candle:
CsfzR3a.png


In the image above, I have the divergence activated to paint the candles when divergence shows up. The other mode is as a moving average:
1kixthz.png

Using the average might be a bit easier to visually identify divergence as new highs/lows are made. Crossovers might also be useful in identifying trend exhaustion. Both versions will default to adding a label showing the current 'trend'. This can be switched off or can show the actual averages and current trend for more detailed information.
A few notes. The divergence calculation for candle coloring is based on an 'extreme' reading. I have not come up with a great way of standardizing these levels across all timeframes. I have found this works best of a 1 or 2 minute chart where the value is set to 2 or 3. You may have to experiment to come up with a suitable level on different timeframes. In addition, this study references those other 5 tickers so cannot be run on tick or range charts. And finally, because this is using stock tickers, I find it best to run against /ES or /NQ on time charts that do have extended hours turned OFF. As a result, the average portion of the study requires a few candles at the start of the session before it starts printing:
K3Yklv9.png

Give it a whirl and let me know if anyone has thoughts on standardizing the extreme levels or other suggestions for improvement.

Ruby:
# created by @tony_futures to identify trend and divergence between the futures and the top 5 tickers

declare lower;
input TICKER = "AAPL";
input TICKER2 = "MSFT";
input TICKER3 = "AMZN";
input TICKER4 = "TSLA";
input TICKER5 = "GOOGL";

def stock1 = close(symbol=TICKER);
def stock2 = close(symbol=TICKER2);
def stock3 = close(symbol=TICKER3);
def stock4 = close(symbol=TICKER4);
def stock5 = close(symbol=TICKER5);

def percentChange = (((stock1 - stock1[1]) + (stock2 - stock2[1]) + (stock3 - stock3[1]) + (stock4 - stock4[1]) + (stock5 - stock5[1])) / (stock1[1] + stock2[1] + stock3[1] + stock4[1] + stock5[1])) * 1000;

input showHisto = yes;
def rth   = secondsfromTime(0930)>=0 and secondsfromTime(1600)<0;
plot change = if showHisto and rth then percentChange else Double.NaN;
change.setPaintingStrategy(PaintingStrategy.HISTOGRAM);
DefineGlobalColor("bullish", CreateColor(51,107,135));
DefineGlobalColor("extrabullish", CreateColor(51,107,255));
DefineGlobalColor("bearish", CreateColor(208,150,131));
DefineGlobalColor("extrabearish", CreateColor(255,150,131));
DefineGlobalColor("neutral", Color.GRAY);

plot zero = if showHisto then 0 else Double.NaN;
zero.setDefaultColor(GlobalColor("neutral"));

input extremeLevel = 2;
plot bullish = if showHisto then extremeLevel else Double.NaN;
bullish.setDefaultColor(GlobalColor("neutral"));
bullish.hideBubble();

plot bearish = if showHisto then -extremeLevel else Double.NaN;
bearish.setDefaultColor(GlobalColor("neutral"));
bearish.hideBubble();

change.DefineColor("Up", GlobalColor("bullish"));
change.DefineColor("ExtraUp", GlobalColor("extrabullish"));
change.DefineColor("Down", GlobalColor("bearish"));
change.DefineColor("ExtraDown", GlobalColor("extrabearish"));
change.AssignValueColor(if percentChange > bullish then change.color("ExtraUp") else if percentChange >= 0 then change.color("Up") else if percentChange < bearish then change.color("ExtraDown") else change.color("Down"));
change.hideBubble();

def NYOpen = 0930;
def isNYOpen = secondsFromTime(NYOpen) == 0;

def upAvg = if isNYOpen then 0 else if percentChange > 0 then upAvg[1] + percentChange else upAvg[1];
def downAvg = if isNYOpen then 0 else if percentChange < 0 then downAvg[1] + percentChange else downAvg[1];
def totalAvg = upAvg + downAvg;
def totalAvgChange = totalAvg - totalAvg[5];
def bearishAvg = (totalAvg < (-extremeLevel * 10));
def bullishAvg = (totalAvg > (extremeLevel * 10));

input showLabels = yes;
input showAvgs = no;
AddLabel(showLabels and showAvgs,"Avg: " + Round(totalAvg,1) + " | " + Round((totalAvgChange),1), if bearishAvg then GlobalColor("bearish") else if bullishAvg then GlobalColor("bullish") else GlobalColor("neutral"));
AddLabel(showLabels and !showAvgs and bullishAvg, "Momo Up",GlobalColor("bullish"));
AddLabel(showLabels and !showAvgs and bearishAvg, "Momo Down",GlobalColor("bearish"));
AddLabel(showLabels and !showAvgs and !bullishAvg and !bearishAvg, "Neutral",GlobalColor("neutral"));

bullish.DefineColor("Up", GlobalColor("bullish"));
bullish.DefineColor("Down", GlobalColor("bearish"));
bullish.AssignValueColor(if bullishAvg then bullish.color("Up") else if bearishAvg then bullish.color("Down") else Color.CURRENT);
bearish.DefineColor("Up", GlobalColor("bullish"));
bearish.DefineColor("Down", GlobalColor("bearish"));
bearish.AssignValueColor(if bullishAvg then bullish.color("Up") else if bearishAvg then bullish.color("Down") else Color.CURRENT);

plot avg = if rth and !showHisto then totalAvg else Double.NaN;
avg.setDefaultColor(GlobalColor("bearish"));

plot avgAVG = if !showHisto then Average(totalAvg,10) else Double.NaN;
avgAVG.setDefaultColor(GlobalColor("neutral"));

def isUp = close > open;
def isDown = close < open;
input showArrows = no;
def bullDiv = isDown[2] and isDown[1] and (percentChange[1] > 0) and (percentChange > (extremeLevel/2));
def bearDiv = isUp[2] and isUp[1] and (percentChange[1] < 0) and (percentChange < (-extremeLevel/2));
plot divUp = if showArrows and bullDiv and showHisto then zero else if showArrows and bullDiv then avg else Double.NaN;
divUp.setPaintingStrategy(PaintingStrategy.ARROW_UP);
divUp.setDefaultColor(GlobalColor("neutral"));

plot divDown = if showArrows and bearDiv and showHisto then zero else if showArrows and bearDiv then avg else Double.NaN;
divDown.setPaintingStrategy(PaintingStrategy.ARROW_DOWN);
divDown.setDefaultColor(GlobalColor("neutral"));

input colorCandles = no;
AssignPriceColor(if colorCandles and bullDiv then Color.GREEN else Color.CURRENT);
AssignPriceColor(if colorCandles and bearDiv then Color.RED else Color.CURRENT);
hey @FutureTony thank you for great concept, i will try it, please can you share your chart with us? i like the way your chart look. thanks in advance. keep up the good job
 
hey @FutureTony thank you for great concept, i will try it, please can you share your chart with us? i like the way your chart look. thanks in advance. keep up the good job
Sorry, my TOS doesn't let me export charts. I have no idea why the feature is disabled but it is. The setup is super simple - just using the default gray:
KcPZCdP.png
 
I think my original description on this might have been too brief. In general, when a histogram bar prints above or below the 'extreme' level, there is a good chance we are either beginning a new trend or ending the existing one. Basically signs of impulsive moves. Yesterday we had an exhaustion move up quickly followed by the divergence signal. We also happened to be at a key area on the higher timeframe and lead to a strong reversal down. Worth watching IMO.
DZQ3o6v.png


Notice how after the divergence signal we never have another impulsive bar to the upside. We drift downwards with a couple of strong pushes down.
 
One more for you based on today's action. The print at 9:36 is the most bullish print of the week. We rally higher and hit a divergent down candle. We then drop 35+ points. Then chop a bit, print a divergent up candle and rally 30 points and counting. Hope some of you are finding this helpful.

4fAnrOl.png
 
Hi again,

I'm back with a twist on a concept I have seen mentioned on Twitter a few times. The idea is to track the percentage change of the 5 biggest stocks in the market as to provide a modified view of market internals. In theory, the movement of the biggest stocks will define the movement of the market and when trending strongly in one direction, the market will follow suit. When divergences occur, one should take note.
This study has two 'modes'. One that visualizes as a histogram that plots the movement of the underlying 'top 5' for each candle:
CsfzR3a.png


In the image above, I have the divergence activated to paint the candles when divergence shows up. The other mode is as a moving average:
1kixthz.png

Using the average might be a bit easier to visually identify divergence as new highs/lows are made. Crossovers might also be useful in identifying trend exhaustion. Both versions will default to adding a label showing the current 'trend'. This can be switched off or can show the actual averages and current trend for more detailed information.
A few notes. The divergence calculation for candle coloring is based on an 'extreme' reading. I have not come up with a great way of standardizing these levels across all timeframes. I have found this works best of a 1 or 2 minute chart where the value is set to 2 or 3. You may have to experiment to come up with a suitable level on different timeframes. In addition, this study references those other 5 tickers so cannot be run on tick or range charts. And finally, because this is using stock tickers, I find it best to run against /ES or /NQ on time charts that do have extended hours turned OFF. As a result, the average portion of the study requires a few candles at the start of the session before it starts printing:
K3Yklv9.png

Give it a whirl and let me know if anyone has thoughts on standardizing the extreme levels or other suggestions for improvement.

Ruby:
# created by @tony_futures to identify trend and divergence between the futures and the top 5 tickers

declare lower;
input TICKER = "AAPL";
input TICKER2 = "MSFT";
input TICKER3 = "AMZN";
input TICKER4 = "TSLA";
input TICKER5 = "GOOGL";

def stock1 = close(symbol=TICKER);
def stock2 = close(symbol=TICKER2);
def stock3 = close(symbol=TICKER3);
def stock4 = close(symbol=TICKER4);
def stock5 = close(symbol=TICKER5);

def percentChange = (((stock1 - stock1[1]) + (stock2 - stock2[1]) + (stock3 - stock3[1]) + (stock4 - stock4[1]) + (stock5 - stock5[1])) / (stock1[1] + stock2[1] + stock3[1] + stock4[1] + stock5[1])) * 1000;

input showHisto = yes;
def rth   = secondsfromTime(0930)>=0 and secondsfromTime(1600)<0;
plot change = if showHisto and rth then percentChange else Double.NaN;
change.setPaintingStrategy(PaintingStrategy.HISTOGRAM);
DefineGlobalColor("bullish", CreateColor(51,107,135));
DefineGlobalColor("extrabullish", CreateColor(51,107,255));
DefineGlobalColor("bearish", CreateColor(208,150,131));
DefineGlobalColor("extrabearish", CreateColor(255,150,131));
DefineGlobalColor("neutral", Color.GRAY);

plot zero = if showHisto then 0 else Double.NaN;
zero.setDefaultColor(GlobalColor("neutral"));

input extremeLevel = 2;
plot bullish = if showHisto then extremeLevel else Double.NaN;
bullish.setDefaultColor(GlobalColor("neutral"));
bullish.hideBubble();

plot bearish = if showHisto then -extremeLevel else Double.NaN;
bearish.setDefaultColor(GlobalColor("neutral"));
bearish.hideBubble();

change.DefineColor("Up", GlobalColor("bullish"));
change.DefineColor("ExtraUp", GlobalColor("extrabullish"));
change.DefineColor("Down", GlobalColor("bearish"));
change.DefineColor("ExtraDown", GlobalColor("extrabearish"));
change.AssignValueColor(if percentChange > bullish then change.color("ExtraUp") else if percentChange >= 0 then change.color("Up") else if percentChange < bearish then change.color("ExtraDown") else change.color("Down"));
change.hideBubble();

def NYOpen = 0930;
def isNYOpen = secondsFromTime(NYOpen) == 0;

def upAvg = if isNYOpen then 0 else if percentChange > 0 then upAvg[1] + percentChange else upAvg[1];
def downAvg = if isNYOpen then 0 else if percentChange < 0 then downAvg[1] + percentChange else downAvg[1];
def totalAvg = upAvg + downAvg;
def totalAvgChange = totalAvg - totalAvg[5];
def bearishAvg = (totalAvg < (-extremeLevel * 10));
def bullishAvg = (totalAvg > (extremeLevel * 10));

input showLabels = yes;
input showAvgs = no;
AddLabel(showLabels and showAvgs,"Avg: " + Round(totalAvg,1) + " | " + Round((totalAvgChange),1), if bearishAvg then GlobalColor("bearish") else if bullishAvg then GlobalColor("bullish") else GlobalColor("neutral"));
AddLabel(showLabels and !showAvgs and bullishAvg, "Momo Up",GlobalColor("bullish"));
AddLabel(showLabels and !showAvgs and bearishAvg, "Momo Down",GlobalColor("bearish"));
AddLabel(showLabels and !showAvgs and !bullishAvg and !bearishAvg, "Neutral",GlobalColor("neutral"));

bullish.DefineColor("Up", GlobalColor("bullish"));
bullish.DefineColor("Down", GlobalColor("bearish"));
bullish.AssignValueColor(if bullishAvg then bullish.color("Up") else if bearishAvg then bullish.color("Down") else Color.CURRENT);
bearish.DefineColor("Up", GlobalColor("bullish"));
bearish.DefineColor("Down", GlobalColor("bearish"));
bearish.AssignValueColor(if bullishAvg then bullish.color("Up") else if bearishAvg then bullish.color("Down") else Color.CURRENT);

plot avg = if rth and !showHisto then totalAvg else Double.NaN;
avg.setDefaultColor(GlobalColor("bearish"));

plot avgAVG = if !showHisto then Average(totalAvg,10) else Double.NaN;
avgAVG.setDefaultColor(GlobalColor("neutral"));

def isUp = close > open;
def isDown = close < open;
input showArrows = no;
def bullDiv = isDown[2] and isDown[1] and (percentChange[1] > 0) and (percentChange > (extremeLevel/2));
def bearDiv = isUp[2] and isUp[1] and (percentChange[1] < 0) and (percentChange < (-extremeLevel/2));
plot divUp = if showArrows and bullDiv and showHisto then zero else if showArrows and bullDiv then avg else Double.NaN;
divUp.setPaintingStrategy(PaintingStrategy.ARROW_UP);
divUp.setDefaultColor(GlobalColor("neutral"));

plot divDown = if showArrows and bearDiv and showHisto then zero else if showArrows and bearDiv then avg else Double.NaN;
divDown.setPaintingStrategy(PaintingStrategy.ARROW_DOWN);
divDown.setDefaultColor(GlobalColor("neutral"));

input colorCandles = no;
AssignPriceColor(if colorCandles and bullDiv then Color.GREEN else Color.CURRENT);
AssignPriceColor(if colorCandles and bearDiv then Color.RED else Color.CURRENT);
Thanks so much for this! So if I got it correctly, we take a position when a red candle forms? What direction? Looks really nice.
 
Thanks so much for this! So if I got it correctly, we take a position when a red candle forms? What direction? Looks really nice.
The red and green candles represent potential shifts in market direction. Price of the futures contract is still going up but the top 5 are starting to go down. I would look for something else to confirm an entry pattern if you are looking to use it that way (ex. MACD or moving average cross).
 
@FutureTony , I uploaded this as a lower indicator in /ES, but nothing populate in the indicator. All plots are on. Not sure what I'm doing wrong?
Try it on a 1 min or 2 min chart. It might not work properly on the daily chart. If that's still not working, please post a screenshot.
 
This is a very interesting study, thanks for making it @FutureTony ! Since I already have my lower studies set up how I like and I'm very particular, I took the liberty of changing this to an upper study with vertical lines instead.

3PahK0X.png


It actually works quite well in my experience, especially when using other momentum/trend indicators for confluence. I just changed the "showArrows" input to "showLines" and removed much of the oscillator logic. In your opinion, do we begin to see diminishing returns on this study if we were to include more tickers, such as top 10 instead of top 5?

Code:
# created by @tony_futures to identify trend and divergence between the futures and the top 5 tickers
## Edited by Chemmy to be an upper study instead of an oscillator


input TICKER = "AAPL";
input TICKER2 = "MSFT";
input TICKER3 = "AMZN";
input TICKER4 = "TSLA";
input TICKER5 = "GOOGL";

def stock1 = close(symbol=TICKER);
def stock2 = close(symbol=TICKER2);
def stock3 = close(symbol=TICKER3);
def stock4 = close(symbol=TICKER4);
def stock5 = close(symbol=TICKER5);

def percentChange = (((stock1 - stock1[1]) + (stock2 - stock2[1]) + (stock3 - stock3[1]) + (stock4 - stock4[1]) + (stock5 - stock5[1])) / (stock1[1] + stock2[1] + stock3[1] + stock4[1] + stock5[1])) * 1000;

input showHisto = yes;
def rth   = secondsfromTime(0930)>=0 and secondsfromTime(1600)<0;
def change = if showHisto and rth then percentChange else Double.NaN;
#change.setPaintingStrategy(PaintingStrategy.HISTOGRAM);
DefineGlobalColor("bullish", CreateColor(51,107,135));
DefineGlobalColor("extrabullish", CreateColor(51,107,255));
DefineGlobalColor("bearish", CreateColor(208,150,131));
DefineGlobalColor("extrabearish", CreateColor(255,150,131));
DefineGlobalColor("neutral", Color.GRAY);

def zero = if showHisto then 0 else Double.NaN;
#zero.setDefaultColor(GlobalColor("neutral"));

input extremeLevel = 2;
def bullish = if showHisto then extremeLevel else Double.NaN;
#bullish.setDefaultColor(GlobalColor("neutral"));
#bullish.hideBubble();

def bearish = if showHisto then -extremeLevel else Double.NaN;
#bearish.setDefaultColor(GlobalColor("neutral"));
#bearish.hideBubble();

#change.DefineColor("Up", GlobalColor("bullish"));
#change.DefineColor("ExtraUp", GlobalColor("extrabullish"));
#change.DefineColor("Down", GlobalColor("bearish"));
#change.DefineColor("ExtraDown", GlobalColor("extrabearish"));
#change.AssignValueColor(if percentChange > bullish then change.color("ExtraUp") else if percentChange >= 0 then #change.color("Up") else if percentChange < bearish then change.color("ExtraDown") else change.color("Down"));
#change.hideBubble();

def NYOpen = 0930;
def isNYOpen = secondsFromTime(NYOpen) == 0;

def upAvg = if isNYOpen then 0 else if percentChange > 0 then upAvg[1] + percentChange else upAvg[1];
def downAvg = if isNYOpen then 0 else if percentChange < 0 then downAvg[1] + percentChange else downAvg[1];
def totalAvg = upAvg + downAvg;
def totalAvgChange = totalAvg - totalAvg[5];
def bearishAvg = (totalAvg < (-extremeLevel * 10));
def bullishAvg = (totalAvg > (extremeLevel * 10));

input showLabels = yes;
input showAvgs = no;
AddLabel(showLabels and showAvgs,"Avg: " + Round(totalAvg,1) + " | " + Round((totalAvgChange),1), if bearishAvg then GlobalColor("bearish") else if bullishAvg then GlobalColor("bullish") else GlobalColor("neutral"));
AddLabel(showLabels and !showAvgs and bullishAvg, "Momo Up",GlobalColor("bullish"));
AddLabel(showLabels and !showAvgs and bearishAvg, "Momo Down",GlobalColor("bearish"));
AddLabel(showLabels and !showAvgs and !bullishAvg and !bearishAvg, "Neutral",GlobalColor("neutral"));

#bullish.DefineColor("Up", GlobalColor("bullish"));
#bullish.DefineColor("Down", GlobalColor("bearish"));
#bullish.AssignValueColor(if bullishAvg then bullish.color("Up") else if bearishAvg then bullish.color("Down") else Color.CURRENT);
#bearish.DefineColor("Up", GlobalColor("bullish"));
#bearish.DefineColor("Down", GlobalColor("bearish"));
#bearish.AssignValueColor(if bullishAvg then bullish.color("Up") else if bearishAvg then bullish.color("Down") else Color.CURRENT);

def avg = if rth and !showHisto then totalAvg else Double.NaN;
#avg.setDefaultColor(GlobalColor("bearish"));

def avgAVG = if !showHisto then Average(totalAvg,10) else Double.NaN;
#avgAVG.setDefaultColor(GlobalColor("neutral"));

def isUp = close > open;
def isDown = close < open;
input showLines = yes;
def bullDiv = isDown[2] and isDown[1] and (percentChange[1] > 0) and (percentChange > (extremeLevel/2));
def bearDiv = isUp[2] and isUp[1] and (percentChange[1] < 0) and (percentChange < (-extremeLevel/2));

#plot divUp = if showArrows and bullDiv and showHisto then zero else if showArrows and bullDiv then avg else Double.NaN;
#divUp.setPaintingStrategy(PaintingStrategy.ARROW_UP);
#divUp.setDefaultColor(GlobalColor("neutral"));
addverticalLine(showLines and bullDiv, "TopUp", Color.GREEN);

#plot divDown = if showArrows and bearDiv and showHisto then zero else if showArrows and bearDiv then avg else Double.NaN;
#divDown.setPaintingStrategy(PaintingStrategy.ARROW_DOWN);
#divDown.setDefaultColor(GlobalColor("neutral"));
addverticalLine(showLines and bearDiv, "TopDown", Color.RED);

input colorCandles = no;
AssignPriceColor(if colorCandles and bullDiv then Color.GREEN else Color.CURRENT);
AssignPriceColor(if colorCandles and bearDiv then Color.RED else Color.CURRENT);
 
This is a very interesting study, thanks for making it @FutureTony ! Since I already have my lower studies set up how I like and I'm very particular, I took the liberty of changing this to an upper study with vertical lines instead.

3PahK0X.png


It actually works quite well in my experience, especially when using other momentum/trend indicators for confluence. I just changed the "showArrows" input to "showLines" and removed much of the oscillator logic. In your opinion, do we begin to see diminishing returns on this study if we were to include more tickers, such as top 10 instead of top 5?

Code:
# created by @tony_futures to identify trend and divergence between the futures and the top 5 tickers
## Edited by Chemmy to be an upper study instead of an oscillator


input TICKER = "AAPL";
input TICKER2 = "MSFT";
input TICKER3 = "AMZN";
input TICKER4 = "TSLA";
input TICKER5 = "GOOGL";

def stock1 = close(symbol=TICKER);
def stock2 = close(symbol=TICKER2);
def stock3 = close(symbol=TICKER3);
def stock4 = close(symbol=TICKER4);
def stock5 = close(symbol=TICKER5);

def percentChange = (((stock1 - stock1[1]) + (stock2 - stock2[1]) + (stock3 - stock3[1]) + (stock4 - stock4[1]) + (stock5 - stock5[1])) / (stock1[1] + stock2[1] + stock3[1] + stock4[1] + stock5[1])) * 1000;

input showHisto = yes;
def rth   = secondsfromTime(0930)>=0 and secondsfromTime(1600)<0;
def change = if showHisto and rth then percentChange else Double.NaN;
#change.setPaintingStrategy(PaintingStrategy.HISTOGRAM);
DefineGlobalColor("bullish", CreateColor(51,107,135));
DefineGlobalColor("extrabullish", CreateColor(51,107,255));
DefineGlobalColor("bearish", CreateColor(208,150,131));
DefineGlobalColor("extrabearish", CreateColor(255,150,131));
DefineGlobalColor("neutral", Color.GRAY);

def zero = if showHisto then 0 else Double.NaN;
#zero.setDefaultColor(GlobalColor("neutral"));

input extremeLevel = 2;
def bullish = if showHisto then extremeLevel else Double.NaN;
#bullish.setDefaultColor(GlobalColor("neutral"));
#bullish.hideBubble();

def bearish = if showHisto then -extremeLevel else Double.NaN;
#bearish.setDefaultColor(GlobalColor("neutral"));
#bearish.hideBubble();

#change.DefineColor("Up", GlobalColor("bullish"));
#change.DefineColor("ExtraUp", GlobalColor("extrabullish"));
#change.DefineColor("Down", GlobalColor("bearish"));
#change.DefineColor("ExtraDown", GlobalColor("extrabearish"));
#change.AssignValueColor(if percentChange > bullish then change.color("ExtraUp") else if percentChange >= 0 then #change.color("Up") else if percentChange < bearish then change.color("ExtraDown") else change.color("Down"));
#change.hideBubble();

def NYOpen = 0930;
def isNYOpen = secondsFromTime(NYOpen) == 0;

def upAvg = if isNYOpen then 0 else if percentChange > 0 then upAvg[1] + percentChange else upAvg[1];
def downAvg = if isNYOpen then 0 else if percentChange < 0 then downAvg[1] + percentChange else downAvg[1];
def totalAvg = upAvg + downAvg;
def totalAvgChange = totalAvg - totalAvg[5];
def bearishAvg = (totalAvg < (-extremeLevel * 10));
def bullishAvg = (totalAvg > (extremeLevel * 10));

input showLabels = yes;
input showAvgs = no;
AddLabel(showLabels and showAvgs,"Avg: " + Round(totalAvg,1) + " | " + Round((totalAvgChange),1), if bearishAvg then GlobalColor("bearish") else if bullishAvg then GlobalColor("bullish") else GlobalColor("neutral"));
AddLabel(showLabels and !showAvgs and bullishAvg, "Momo Up",GlobalColor("bullish"));
AddLabel(showLabels and !showAvgs and bearishAvg, "Momo Down",GlobalColor("bearish"));
AddLabel(showLabels and !showAvgs and !bullishAvg and !bearishAvg, "Neutral",GlobalColor("neutral"));

#bullish.DefineColor("Up", GlobalColor("bullish"));
#bullish.DefineColor("Down", GlobalColor("bearish"));
#bullish.AssignValueColor(if bullishAvg then bullish.color("Up") else if bearishAvg then bullish.color("Down") else Color.CURRENT);
#bearish.DefineColor("Up", GlobalColor("bullish"));
#bearish.DefineColor("Down", GlobalColor("bearish"));
#bearish.AssignValueColor(if bullishAvg then bullish.color("Up") else if bearishAvg then bullish.color("Down") else Color.CURRENT);

def avg = if rth and !showHisto then totalAvg else Double.NaN;
#avg.setDefaultColor(GlobalColor("bearish"));

def avgAVG = if !showHisto then Average(totalAvg,10) else Double.NaN;
#avgAVG.setDefaultColor(GlobalColor("neutral"));

def isUp = close > open;
def isDown = close < open;
input showLines = yes;
def bullDiv = isDown[2] and isDown[1] and (percentChange[1] > 0) and (percentChange > (extremeLevel/2));
def bearDiv = isUp[2] and isUp[1] and (percentChange[1] < 0) and (percentChange < (-extremeLevel/2));

#plot divUp = if showArrows and bullDiv and showHisto then zero else if showArrows and bullDiv then avg else Double.NaN;
#divUp.setPaintingStrategy(PaintingStrategy.ARROW_UP);
#divUp.setDefaultColor(GlobalColor("neutral"));
addverticalLine(showLines and bullDiv, "TopUp", Color.GREEN);

#plot divDown = if showArrows and bearDiv and showHisto then zero else if showArrows and bearDiv then avg else Double.NaN;
#divDown.setPaintingStrategy(PaintingStrategy.ARROW_DOWN);
#divDown.setDefaultColor(GlobalColor("neutral"));
addverticalLine(showLines and bearDiv, "TopDown", Color.RED);

input colorCandles = no;
AssignPriceColor(if colorCandles and bullDiv then Color.GREEN else Color.CURRENT);
AssignPriceColor(if colorCandles and bearDiv then Color.RED else Color.CURRENT);
I like this. I'm not sure you would get much benefit extending past 5 tickers. I suppose in theory it might be even more interesting to do less. If you think AAPL is really driving the market, watching for divergence there might work very well. BUT...I do think you are losing something without the 'readings' over and under the threshold lines. I was thinking about tweaking this to track the largest spikes up/down for the day/week and seeing when we exceed them. Use it kind of like the 1k/-1k $TICK readings. Hopefully will post a new version sometime this week.
 
I like this. I'm not sure you would get much benefit extending past 5 tickers. I suppose in theory it might be even more interesting to do less. If you think AAPL is really driving the market, watching for divergence there might work very well. BUT...I do think you are losing something without the 'readings' over and under the threshold lines. I was thinking about tweaking this to track the largest spikes up/down for the day/week and seeing when we exceed them. Use it kind of like the 1k/-1k $TICK readings. Hopefully will post a new version sometime this week.
Yeah I think you're right, I added a few extra tickers and didn't really see much benefit -- it definitely seems like less may be better in regards to this. I do agree that the oscillator is by far the most informative version of this, unfortunately that adds one too many lower indicators for me. In other news, I wondered how this would work using VIX/UVXY as the main divergences and it actually ends up being somewhat similar to a "VIX fix" type study:

mcao5y2.png


Where the signals coincide almost perfectly the signals from a study like that. Pretty interesting, in my opinion! Very cool code. Also very excited to see your new ideas for this going forward.
 
Hi again,

I'm back with a twist on a concept I have seen mentioned on Twitter a few times. The idea is to track the percentage change of the 5 biggest stocks in the market as to provide a modified view of market internals. In theory, the movement of the biggest stocks will define the movement of the market and when trending strongly in one direction, the market will follow suit. When divergences occur, one should take note.
This study has two 'modes'. One that visualizes as a histogram that plots the movement of the underlying 'top 5' for each candle:
CsfzR3a.png


In the image above, I have the divergence activated to paint the candles when divergence shows up. The other mode is as a moving average:
1kixthz.png

Using the average might be a bit easier to visually identify divergence as new highs/lows are made. Crossovers might also be useful in identifying trend exhaustion. Both versions will default to adding a label showing the current 'trend'. This can be switched off or can show the actual averages and current trend for more detailed information.
A few notes. The divergence calculation for candle coloring is based on an 'extreme' reading. I have not come up with a great way of standardizing these levels across all timeframes. I have found this works best of a 1 or 2 minute chart where the value is set to 2 or 3. You may have to experiment to come up with a suitable level on different timeframes. In addition, this study references those other 5 tickers so cannot be run on tick or range charts. And finally, because this is using stock tickers, I find it best to run against /ES or /NQ on time charts that do have extended hours turned OFF. As a result, the average portion of the study requires a few candles at the start of the session before it starts printing:
K3Yklv9.png

Give it a whirl and let me know if anyone has thoughts on standardizing the extreme levels or other suggestions for improvement.

Ruby:
# created by @tony_futures to identify trend and divergence between the futures and the top 5 tickers

declare lower;
input TICKER = "AAPL";
input TICKER2 = "MSFT";
input TICKER3 = "AMZN";
input TICKER4 = "TSLA";
input TICKER5 = "GOOGL";

def stock1 = close(symbol=TICKER);
def stock2 = close(symbol=TICKER2);
def stock3 = close(symbol=TICKER3);
def stock4 = close(symbol=TICKER4);
def stock5 = close(symbol=TICKER5);

def percentChange = (((stock1 - stock1[1]) + (stock2 - stock2[1]) + (stock3 - stock3[1]) + (stock4 - stock4[1]) + (stock5 - stock5[1])) / (stock1[1] + stock2[1] + stock3[1] + stock4[1] + stock5[1])) * 1000;

input showHisto = yes;
def rth   = secondsfromTime(0930)>=0 and secondsfromTime(1600)<0;
plot change = if showHisto and rth then percentChange else Double.NaN;
change.setPaintingStrategy(PaintingStrategy.HISTOGRAM);
DefineGlobalColor("bullish", CreateColor(51,107,135));
DefineGlobalColor("extrabullish", CreateColor(51,107,255));
DefineGlobalColor("bearish", CreateColor(208,150,131));
DefineGlobalColor("extrabearish", CreateColor(255,150,131));
DefineGlobalColor("neutral", Color.GRAY);

plot zero = if showHisto then 0 else Double.NaN;
zero.setDefaultColor(GlobalColor("neutral"));

input extremeLevel = 2;
plot bullish = if showHisto then extremeLevel else Double.NaN;
bullish.setDefaultColor(GlobalColor("neutral"));
bullish.hideBubble();

plot bearish = if showHisto then -extremeLevel else Double.NaN;
bearish.setDefaultColor(GlobalColor("neutral"));
bearish.hideBubble();

change.DefineColor("Up", GlobalColor("bullish"));
change.DefineColor("ExtraUp", GlobalColor("extrabullish"));
change.DefineColor("Down", GlobalColor("bearish"));
change.DefineColor("ExtraDown", GlobalColor("extrabearish"));
change.AssignValueColor(if percentChange > bullish then change.color("ExtraUp") else if percentChange >= 0 then change.color("Up") else if percentChange < bearish then change.color("ExtraDown") else change.color("Down"));
change.hideBubble();

def NYOpen = 0930;
def isNYOpen = secondsFromTime(NYOpen) == 0;

def upAvg = if isNYOpen then 0 else if percentChange > 0 then upAvg[1] + percentChange else upAvg[1];
def downAvg = if isNYOpen then 0 else if percentChange < 0 then downAvg[1] + percentChange else downAvg[1];
def totalAvg = upAvg + downAvg;
def totalAvgChange = totalAvg - totalAvg[5];
def bearishAvg = (totalAvg < (-extremeLevel * 10));
def bullishAvg = (totalAvg > (extremeLevel * 10));

input showLabels = yes;
input showAvgs = no;
AddLabel(showLabels and showAvgs,"Avg: " + Round(totalAvg,1) + " | " + Round((totalAvgChange),1), if bearishAvg then GlobalColor("bearish") else if bullishAvg then GlobalColor("bullish") else GlobalColor("neutral"));
AddLabel(showLabels and !showAvgs and bullishAvg, "Momo Up",GlobalColor("bullish"));
AddLabel(showLabels and !showAvgs and bearishAvg, "Momo Down",GlobalColor("bearish"));
AddLabel(showLabels and !showAvgs and !bullishAvg and !bearishAvg, "Neutral",GlobalColor("neutral"));

bullish.DefineColor("Up", GlobalColor("bullish"));
bullish.DefineColor("Down", GlobalColor("bearish"));
bullish.AssignValueColor(if bullishAvg then bullish.color("Up") else if bearishAvg then bullish.color("Down") else Color.CURRENT);
bearish.DefineColor("Up", GlobalColor("bullish"));
bearish.DefineColor("Down", GlobalColor("bearish"));
bearish.AssignValueColor(if bullishAvg then bullish.color("Up") else if bearishAvg then bullish.color("Down") else Color.CURRENT);

plot avg = if rth and !showHisto then totalAvg else Double.NaN;
avg.setDefaultColor(GlobalColor("bearish"));

plot avgAVG = if !showHisto then Average(totalAvg,10) else Double.NaN;
avgAVG.setDefaultColor(GlobalColor("neutral"));

def isUp = close > open;
def isDown = close < open;
input showArrows = no;
def bullDiv = isDown[2] and isDown[1] and (percentChange[1] > 0) and (percentChange > (extremeLevel/2));
def bearDiv = isUp[2] and isUp[1] and (percentChange[1] < 0) and (percentChange < (-extremeLevel/2));
plot divUp = if showArrows and bullDiv and showHisto then zero else if showArrows and bullDiv then avg else Double.NaN;
divUp.setPaintingStrategy(PaintingStrategy.ARROW_UP);
divUp.setDefaultColor(GlobalColor("neutral"));

plot divDown = if showArrows and bearDiv and showHisto then zero else if showArrows and bearDiv then avg else Double.NaN;
divDown.setPaintingStrategy(PaintingStrategy.ARROW_DOWN);
divDown.setDefaultColor(GlobalColor("neutral"));

input colorCandles = no;
AssignPriceColor(if colorCandles and bullDiv then Color.GREEN else Color.CURRENT);
AssignPriceColor(if colorCandles and bearDiv then Color.RED else Color.CURRENT);
Both forms of this interesting indicator (the histogram and non-histogram versions) show worthwhile information in usefully different contexts. Both run fine on minute or hourly charts and I greatly appreciate the effort and willingness to share this approach. Unfortunately, I can't get the indicator to run on a daily chart --is it possible to run the indicator(s) on a daily chart and if so, how?
 
Last edited:
Both forms of this interesting indicator (the histogram and non-histogram versions both show useful information in usefully different contexts) run fine on minute or hourly charts and I greatly appreciate the effort and willingness to share this approach. Unfortunately, I can't get the indicator to run on a daily chart --is it possible to run on a daily chart and if so, how?
Hi bro,

I think you can remove the "RTH" and "isNYOpen" to run on daily.

In addition, you might want to adjust the "extremeLevel".

Hope this helps.
 
One more for you based on today's action. The print at 9:36 is the most bullish print of the week. We rally higher and hit a divergent down candle. We then drop 35+ points. Then chop a bit, print a divergent up candle and rally 30 points and counting. Hope some of you are finding this helpful.

4fAnrOl.png
Sort of unrelated, but I like the labels you have on this chart. Can you dive into them a bit?
 
Sort of unrelated, but I like the labels you have on this chart. Can you dive into them a bit?
The labels are the implied range based on the VIX and ES opening prices. That study was written by a friend so not mine to share but if you google the VIX implied move calculation...you will find out how to do it.
 
Yeah I think you're right, I added a few extra tickers and didn't really see much benefit -- it definitely seems like less may be better in regards to this. I do agree that the oscillator is by far the most informative version of this, unfortunately that adds one too many lower indicators for me. In other news, I wondered how this would work using VIX/UVXY as the main divergences and it actually ends up being somewhat similar to a "VIX fix" type study:

mcao5y2.png


Where the signals coincide almost perfectly the signals from a study like that. Pretty interesting, in my opinion! Very cool code. Also very excited to see your new ideas for this going forward.
This gave me an idea to plot reverse UVXY as a lower indicator vs the upper as ES. Going to work on this a bit and post it as a separate thread:
JXO2eoB.png
 

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
469 Online
Create Post

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top