Beta Covariance / Correlation For ThinkOrSwim

Charlie.mp3

New member
Hey everyone,

I'm just trying to free up some screen space and want to put the correlation indicator in a badge as opposed to a plot. Anyone have anything like that already scripted?

I just want it as a label that reads the daily correlation to the SPX.
Sorry by Badge I meant "Label"

Thanks.
 
Last edited by a moderator:

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

Not sure if this is what you're looking for.

Code:
# Beta with a Correlation label
# Mobius
# Chat Room discussion
# V01.01.11.2015

declare lower;

input length = 60;
input returnLength = 1;
input index = {default SPX, "Nasdaq Composite", NDX, Dow30, "Russell 2000", "/GC","/CL"};
input price = FundamentalType.CLOSE;
Assert(returnLength > 0, "'return length' must be positive: " + returnLength);

def logic;
switch (index) {
case SPX:
    logic = close("SPX");
case "Nasdaq Composite":
    logic = close("COMP");
case NDX:
    logic = close("NDX");
case "Dow30":
    logic = close("$DJI");
case "Russell 2000":
    logic = close("RUT");
case "/GC":
    logic = close("/GC");
case "/CL":
    logic = close("/CL");
}
def c1 = close(period = AggregationPeriod.DAY);
def c2 = close(symbol = index, period = AggregationPeriod.DAY);
def primary = if c1[returnLength] == 0 then 0 else (c1 - c1[returnLength]) / c1[returnLength] * 100;
def secondary = if c2[returnLength] == 0 then 0 else (c2 - c2[returnLength]) / c2[returnLength] * 100;

plot Beta = Covariance(primary, secondary, length) / Sqr(StDev(secondary, length));
Beta.SetDefaultColor(GetColor(9));

plot corr = Correlation(c1, c2, length);
AddLabel(1, "Correlation: " + corr);#, if corr > .75 then color.green else if between(corr, 0, .75) then color.yellow else color.red);

# End Study
 
Close, I do see the Correlation Label displayed in the study. The "declare lower;" part puts the study at the bottom of the chart, I just wanted the label at the top displaying the current daily correlation to the SPX. But I think I'll play around with this code and see if I can Frankenstein something with it.

Thanks.
 
Hi Guys,

TOS supplies this as a lower study, is it possible to have this as a label to save screen real estate instead of a lower study plot?

Code:
#
# TD Ameritrade IP Company, Inc. (c) 2007-2021
#

declare lower;

input length = 21;
input returnLength = 1;
input index = {Default SPX, "Nasdaq Composite", NDX, Dow30, "Russell 2000"};

assert(returnLength > 0, "'return length' must be positive: " + returnLength);

def primary = if close[returnLength] == 0 then 0 else (close - close[returnLength]) / close[returnLength] * 100;

def logic;
switch(index) {
case SPX:
logic = close("SPX");
case "Nasdaq Composite":
logic = close("COMP");
case NDX:
logic = close("NDX");
case "Dow30":
logic = close("$DJI");
case "Russell 2000":
logic = close("RUT");
}

def secondary = if logic[returnLength] == 0 then 0 else (logic - logic[returnLength]) / logic[returnLength] * 100;

plot Beta = covariance(primary, secondary, length) / Sqr(stdev(secondary, length));
Beta.SetDefaultColor(GetColor(9));
 
Sure...

Ruby:
#
# TD Ameritrade IP Company, Inc. (c) 2007-2021
#
# Modified 2021-02-09 by rad14733 to display as label in upper studies

declare upper;

input length = 21;
input returnLength = 1;
input index = {Default SPX, "Nasdaq Composite", NDX, Dow30, "Russell 2000"};

assert(returnLength > 0, "'return length' must be positive: " + returnLength);

def primary = if close[returnLength] == 0 then 0 else (close - close[returnLength]) / close[returnLength] * 100;

def logic;
switch(index) {
case SPX:
logic = close("SPX");
case "Nasdaq Composite":
logic = close("COMP");
case NDX:
logic = close("NDX");
case "Dow30":
logic = close("$DJI");
case "Russell 2000":
logic = close("RUT");
}

def secondary = if logic[returnLength] == 0 then 0 else (logic - logic[returnLength]) / logic[returnLength] * 100;

def Beta = covariance(primary, secondary, length) / Sqr(stdev(secondary, length));

AddLabel(yes, index + " Beta = " + Beta, Color.WHITE);
 
Allow "Beta" length to include previous day's data?

I'd like to use Beta with a labels and bubbles. However, it seems the length only looks back to the current day's market open.

So on a 10 minute chart and a length of 21, it won't display data until 210 minutes into the session.

Is there any way to use data from a previous day - whether combined with today's data or simply only the previous day's data?

Mainly, I'm looking to use Beta as soon as the market opens, so the recency of the beta calculation isn't too important to me. For instance, TSLA will generally be much higher beta than SPX, JNJ much lower. This is the rough number I'm looking to use beginning at the open.

Here's code for a label I'm currently working with:

Code:
declare upper;

input length = 21;
input returnLength = 1;

assert(returnLength > 0, "'return length' must be positive: " + returnLength);

def primary = if close[returnLength] == 0 then 0 else (close - close[returnLength]) / close[returnLength] * 100;

def SPX = close("SPX");

def secondary = if SPX[returnLength] == 0 then 0 else (SPX - SPX[returnLength]) / SPX[returnLength] * 100;

def Beta = covariance(primary, secondary, length) / Sqr(stdev(secondary, length));

AddLabel(yes, "Beta = " + Beta, Color.WHITE);

Thanks in advance - you all have been very helpful!
 
Last edited by a moderator:
Allow "Beta" length to include previous day's data?

I'd like to use Beta with a labels and bubbles. However, it seems the length only looks back to the current day's market open.

So on a 10 minute chart and a length of 21, it won't display data until 210 minutes into the session.

Is there any way to use data from a previous day - whether combined with today's data or simply only the previous day's data?

Mainly, I'm looking to use Beta as soon as the market opens, so the recency of the beta calculation isn't too important to me. For instance, TSLA will generally be much higher beta than SPX, JNJ much lower. This is the rough number I'm looking to use beginning at the open.

Here's code for a label I'm currently working with:

Code:
declare upper;

input length = 21;
input returnLength = 1;

assert(returnLength > 0, "'return length' must be positive: " + returnLength);

def primary = if close[returnLength] == 0 then 0 else (close - close[returnLength]) / close[returnLength] * 100;

def SPX = close("SPX");

def secondary = if SPX[returnLength] == 0 then 0 else (SPX - SPX[returnLength]) / SPX[returnLength] * 100;

def Beta = covariance(primary, secondary, length) / Sqr(stdev(secondary, length));

AddLabel(yes, "Beta = " + Beta, Color.WHITE);

Thanks in advance - you all have been very helpful!
You could try changing the returnLength to 2
 
Matt Caruso CARS
you can choose the banchmark index in t settings. for more information about this indicator.
https://www.carusoinsights.com/caruso-adaptive-relative-strength/

HI,
im actually working on a script that emulates matt caruso cars.
I created this script that actually does a really good work, but need upgrades
I will leave the code below if someone is interested in helping me.
thanks.

declare lower;
INPUT Betalength = 40;
input Index = "SPY";
DEF avgfast = 50;
DEF avgfast1 = 10;
DEF avgslow = 100;
DEF avgslow1 = 10;
DEF avgs40 =40;

def logic = close(Index);


#RETORNOS DEL ACTIVO Y DEL ACTIVO Y COMPARABLE
def ret1 = (close - close[1]) / close[1];
def ret2 = (logic - logic[1]) / logic[1];

#BETA
def Beta = Covariance(ret1, ret2, Betalength) / Sqr(StDev(ret2, Betalength));

#ALPHA
plot alpha = ret1 - (ret2 * Beta);
alpha.AssignValueColor(if alpha >= 0 then Color.UPTICK else Color.DOWNTICK);
alpha.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
alpha.SetLineWeight(3);


#LABELS
AddLabel(yes, "Beta : " + Round(Beta, 3), Color.LIGHT_GRAY);
AddLabel(yes, "IndexChange% : " + Round(ret2, 3), Color.LIGHT_GRAY);
AddLabel(yes, "Change% : " + Round(ret1, 3), Color.LIGHT_GRAY);


#Media movil rapida + otra que la suavice

def avg1 = expaverage(alpha, avgfast);
DEF avg11 = expaverage(avg1, avgfast1);

#Media movil lenta + otra que la suavice
DEF avg2 = average(alpha, avgslow);
DEF avg22 = expaverage(avg2, avgslow1);


#SM50

DEF SM50 = WMA(alpha, avgs40);





#conditions
#def conditions1 = if ( (avg11 < avg22) and (avg11 > 0 and avg22 > 0)and (ratio>1) ) then 1 else Double.NaN;
def conditions2 = if ( (avg11 > 0 and avg22 > 0) and (SM50 > 0)) then 1 else Double.NaN;




#plot
#plot histograma1 = conditions1;
#histograma1.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
#histograma1.AssignValueColor (Color.GRAY);
#histograma1.SetLineWeight(3);

plot histograma2 = conditions2;
histograma2.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
histograma2.AssignValueColor (Color.YELLOW);
histograma2.SetLineWeight(3);


DEF line = 0;
plot zeroline = line;
 
Last edited:
Matt Caruso CARS

HI,
im actually working on a script that emulates matt caruso cars.
I created this script that actually does a really good work, but need upgrades
I will leave the code below if someone is interested in helping me.
thanks.
Ruby:
declare lower;
input Betalength = 60;
input Index = "SPY";
input AVGfast = 40;
input avgsignal1 = 10;
input AVGslow = 20;
input avgsignal2 = 100;


def logic = close(Index);


#RETORNOS DEL ACTIVO Y DEL ACTIVO Y COMPARABLE
def ret1 = (close / close[1]) - 1 ;
def ret2 = (logic / logic[1]) - 1
;

#BETA
def Beta = Covariance(ret1, ret2, Betalength) / Sqr(StDev(ret2, Betalength));

#ALPHA
#plot alpha = ret1 - (ret2 * Beta);
#alpha.AssignValueColor(if alpha >= 0 then Color.UPTICK else Color.DOWNTICK);
#alpha.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
#alpha.SetLineWeight(3);

def alpha = ret1 - (ret2 * Beta);


#LABELS
AddLabel(yes, "Beta : " + Round(Beta, 3), Color.LIGHT_GRAY);
AddLabel(yes, "IndexChange% : " + Round(ret2, 3), Color.LIGHT_GRAY);
AddLabel(yes, "Change% : " + Round(ret1, 3), Color.LIGHT_GRAY);



#Media movil rapida + otra que la suavice

def avg1 = expaverage(alpha, avgfast);
plot Macdgraph = expaverage(avg1, avgsignal1);

#Media movil lenta + otra que la suavice
def avg2 = ExpAverage(alpha, AVGslow);
plot Macdgrap2 = ExpAverage(avg2, avgsignal2);

#plot
plot histograma = if (macdgraph > 0 and Macdgrap2 > 0) then 1 else Double.NaN;
histograma.SetPaintingStrategy(PaintingStrategy.histoGRAM);
histograma.AssignValueColor (Color.yellow);
histograma.SetLineWeight(3);

#input line = 0;
#plot zeroline = line;
Thank you. Is this only for SPY (I trade QQQ), and what timeframes should be used? Could you help explain this indicator and how to use it in further detail?
Thanks
 
Sure...

Ruby:
#
# TD Ameritrade IP Company, Inc. (c) 2007-2021
#
# Modified 2021-02-09 by rad14733 to display as label in upper studies

declare upper;

input length = 21;
input returnLength = 1;
input index = {Default SPX, "Nasdaq Composite", NDX, Dow30, "Russell 2000"};

assert(returnLength > 0, "'return length' must be positive: " + returnLength);

def primary = if close[returnLength] == 0 then 0 else (close - close[returnLength]) / close[returnLength] * 100;

def logic;
switch(index) {
case SPX:
logic = close("SPX");
case "Nasdaq Composite":
logic = close("COMP");
case NDX:
logic = close("NDX");
case "Dow30":
logic = close("$DJI");
case "Russell 2000":
logic = close("RUT");
}

def secondary = if logic[returnLength] == 0 then 0 else (logic - logic[returnLength]) / logic[returnLength] * 100;

def Beta = covariance(primary, secondary, length) / Sqr(stdev(secondary, length));

AddLabel(yes, index + " Beta = " + Beta, Color.WHITE);
this is really good - but how to make the ticker input user-defined customizeable, like in native TOS study ticker input parameter field?
 
Thread starter Similar threads Forum Replies Date
samer800 Put to Call Ratio Correlation for ThinkOrSwim Custom 6
samer800 Multiple Frequency Volatility Correlation for ThinkOrSwim Custom 0

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
453 Online
Create Post

Similar threads

Similar threads

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