Plot Beta as a label

rbark3r

New member
VIP
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));
 

rad14733

Well-known member
VIP
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);
 

serendipity2020

New member
I added below code to show Beta label but its not matching with values from Tipranks or Finviz.

Code:
def beta = round(Beta(), 2);
AddLabel(Show_Labels, Concat("Beta: ", beta), if beta > 1 then Color.RED else color.GREEN);

What length is used on Finviz when they calculate Beta?

I also tried to use above code given by @rad14733 to make it generic for any symbol by using GetSymbol() but it always calculates Beta as 1

Can someone help to correctly plot Beta values?
 

rad14733

Well-known member
VIP
I added below code to show Beta label but its not matching with values from Tipranks or Finviz.

Code:
def beta = round(Beta(), 2);
AddLabel(Show_Labels, Concat("Beta: ", beta), if beta > 1 then Color.RED else color.GREEN);

What length is used on Finviz when they calculate Beta?

I also tried to use above code given by @rad14733 to make it generic for any symbol by using GetSymbol() but it always calculates Beta as 1

Can someone help to correctly plot Beta values?

@serendipity2020 I assume that you're read the documentation on Beta() in the Thinkscript Learning Center...
 

Similar threads

Top