Need Help: Automatically Comparing Sectors to Individuals Stock Beta!

Hey Everyone,
Iv been recently trying to find out how i can automatically compare an individuals stocks beta to its sectors performance E.G S&P/ASX 200 Materials sector with a ticker of XMJ. I Am struggling to find a code that is able to find the sector code from the individual stock , to put replace the "default" in the beta code.

An example for this would be me looking up "BHP" and the beta indicator would automatically assign "XMJ" as the indices to be compared to.

I am utilising Norgate Premium data which returns this data in symbol.

Cheers Chris .

Hi,

There might be other effective solutions for your query. However, below is the result of my hit-and-trial and would be extremely grateful if others further improvise or modify the code:

_SECTION_BEGIN( "Beta Calc" );
	 per = Param( "Look-back Period", 21, 3, 252, 1 ); //"per" variable is crucial - do play with it
	 
	 SetForeign( "E.G S&P/ASX 200 Materials sector" ); //Put the symbol of the stock or index in-between the double-quotes
		 ForSymC = C;
		 ForSymArr = Avg; //Instead of using the Typical price, you can also use Close
	 RestorePriceArrays();
	 
	 ThisSymArr = Avg; //or Close
	 
	 //Improvised version of https://www.investopedia.com/ask/answers/102714/how-do-you-calculate-beta-excel.asp
	 Beta = ( ( per * Sum( ROC( ThisSymArr, 1 ) * ROC( ForSymArr, 1 ), per ) ) - ( Sum( ROC( ThisSymArr, 1 ), per ) * Sum( ROC( ForSymArr, 1 ), per ) ) ) / ( ( per * Sum( ( ROC( ForSymArr, 1 ) ^ 2 ), per ) ) - ( Sum( ROC( ForSymArr, 1 ), per ) ^ 2 ) );
	 
	 Title = "Beta: " + Beta;
	 Plot( ForSymC, "", colorGrey50, styleLine | styleOwnScale | styleNoLabel, Null, Null, 0, 0, 1 );
	 Plot( C, "", colorBlue, styleLine | styleOwnScale | styleThick | styleNoLabel, Null, Null, 0, 0, 1 );
_SECTION_END();

Cheers!

2 Likes

Hey mate, Finally got back to this problem after doing a lot of other work. Now that i know more about coding heres a solution which automatically finds the sector or index associated with the stock, as long as your categories are set up and linked each stock to the appropriate sector or index.

_SECTION_BEGIN("Beta");

function Beta( BaseSymbol, N )
{
Chg = ROC( C, 1 );
ChgBase = ROC( Foreign( BaseSymbol, "C" ), 1 );

return ( N * Sum( Chg * ChgBase , N ) - Sum( Chg, N) * Sum( ChgBase, N ) ) /
( N * Sum( ChgBase ^ 2 , N ) - Sum( ChgBase, N ) ^ 2 );
}

param8 = ParamToggle("Xao","Xao");
param7 = ParamToggle("Sector","Sector",1);

if(NOT(Param7)){
symbol = GetBaseIndex();

}

if(NOT(Param8)){
symbol = "$XAO-ASX";

}

Plot( Beta( symbol, Param("Periods", 21, 2, 100 ) ), _DEFAULT_NAME(),colorLime,styleThick );
_SECTION_END();

1 Like

How to add multiple symbols to above code