Multi asset correlation

Posted By: Paul_der_Zweite

Multi asset correlation - 02/18/19 17:23

Hi,
I want to check a list of assets for correlation. Each asset in the list should be checked with each asset in the list.

I don't want to write a time for every possibility, of course, but by while(asset(loop())

Unfortunately I can't get it to work yet wink
I would like a list, on which the following is shown to me:
asset a to asset b: Correlation x
asset a to asset c: Correlation x
asset a to asset d: Correlation x
and so on.

I tried the following, which doesn't work yet:

Code:
function run()
{
	BarPeriod = 240;
	LookBack = 300;
	StartDate = 2017;
	//EndDate = 2019;
		
	while(asset(loop("AUD/CAD","AUD/JPY","AUD/NZD","AUD/USD","CAD/CHF","CAD/JPY","CHF/JPY","Cocoa","Coffee","Copper","Corn","Cotton","Esp35","EuBund","EUR/AUD","EUR/CAD","EUR/CHF","EUR/GBP","EUR/HUF","EUR/JPY","EUR/NOK","EUR/NZD","Euro50","EUR/PLN","EUR/SEK","EUR/SGD","EUR/TRY","EUR/USD","Fra40","Gasol","GBP/AUD","GBP/CAD","GBP/CHF","GBP/JPY","GBP/NZD","GBP/USD","GER30","GerTec","GOLD","Jp225","LCrude","Neth25","NGas","NZD/CAD","NZD/CHF","NZD/JPY","NZD/USD","OJ","SGD/JPY","SILVER","Soybns","Sugar","Swi20","TRY/JPY","UK100","Usa500","UsaInd","UsaRus","UsaTB","UsaTec","UsaVix","USD/BRL","USD/CAD","USD/CHF","USD/CNH","USD/HUF","USD/JPY","USD/MXN","USD/NOK","USD/PLN","USD/RUB","USD/SEK","USD/SGD","USD/TRY","USD/ZAR","Wheat")))
	
	vars price_asset_1 = series(price());
	
	while(asset(loop("AUD/CAD","AUD/JPY","AUD/NZD","AUD/USD","CAD/CHF","CAD/JPY","CHF/JPY","Cocoa","Coffee","Copper","Corn","Cotton","Esp35","EuBund","EUR/AUD","EUR/CAD","EUR/CHF","EUR/GBP","EUR/HUF","EUR/JPY","EUR/NOK","EUR/NZD","Euro50","EUR/PLN","EUR/SEK","EUR/SGD","EUR/TRY","EUR/USD","Fra40","Gasol","GBP/AUD","GBP/CAD","GBP/CHF","GBP/JPY","GBP/NZD","GBP/USD","GER30","GerTec","GOLD","Jp225","LCrude","Neth25","NGas","NZD/CAD","NZD/CHF","NZD/JPY","NZD/USD","OJ","SGD/JPY","SILVER","Soybns","Sugar","Swi20","TRY/JPY","UK100","Usa500","UsaInd","UsaRus","UsaTB","UsaTec","UsaVix","USD/BRL","USD/CAD","USD/CHF","USD/CNH","USD/HUF","USD/JPY","USD/MXN","USD/NOK","USD/PLN","USD/RUB","USD/SEK","USD/SGD","USD/TRY","USD/ZAR","Wheat")))
	
	vars price_asset_2 = series(price());
	
	var corr = Correlation(price_asset_1, price_asset_2, 240);
	
	if(!is(LOOKBACK))
		printf("n %f", corr);

}



In my beginner version only ones appear (1.000000). *head scratch*

Does anyone have a tip?
Posted By: OptimusPrime

Re: Multi asset correlation - 02/18/19 20:00

You are lacking brackets to make the sub-loop work. Right now, you are making price_asset_1 equal to price_asset_2. and both contain the price for the last asset you have in the while loop.
Posted By: Paul_der_Zweite

Re: Multi asset correlation - 02/21/19 20:27

Hi OptimusPrime,
Thank you very much for your help.

This is now the current version of the script:
Code:
function run()
{
	//BarPeriod = 240;
	LookBack = 300;
	StartDate = 2018;
	//EndDate = 2019;
		
	vars price_asset_1, price_asset_2;
	var corr;
	
	while(asset(loop("AUD/CAD","AUD/JPY","AUD/NZD","AUD/USD","CAD/CHF","CAD/JPY","CHF/JPY","Cocoa","Coffee","Copper","Corn","Cotton","Esp35","EuBund","EUR/AUD","EUR/CAD","EUR/CHF","EUR/GBP","EUR/HUF","EUR/JPY","EUR/NOK","EUR/NZD","Euro50","EUR/PLN","EUR/SEK","EUR/SGD","EUR/TRY","EUR/USD","Fra40","Gasol","GBP/AUD","GBP/CAD","GBP/CHF","GBP/JPY","GBP/NZD","GBP/USD","GER30","GerTec","GOLD","Jp225","LCrude","Neth25","NGas","NZD/CAD","NZD/CHF","NZD/JPY","NZD/USD","OJ","SGD/JPY","SILVER","Soybns","Sugar","Swi20","TRY/JPY","UK100","Usa500","UsaInd","UsaRus","UsaTB","UsaTec","UsaVix","USD/BRL","USD/CAD","USD/CHF","USD/CNH","USD/HUF","USD/JPY","USD/MXN","USD/NOK","USD/PLN","USD/RUB","USD/SEK","USD/SGD","USD/TRY","USD/ZAR","Wheat")))
	{
		price_asset_1 = series(price());
		printf("n %s to", Asset);
	
	
		while(asset(loop("AUD/CAD","AUD/JPY","AUD/NZD","AUD/USD","CAD/CHF","CAD/JPY","CHF/JPY","Cocoa","Coffee","Copper","Corn","Cotton","Esp35","EuBund","EUR/AUD","EUR/CAD","EUR/CHF","EUR/GBP","EUR/HUF","EUR/JPY","EUR/NOK","EUR/NZD","Euro50","EUR/PLN","EUR/SEK","EUR/SGD","EUR/TRY","EUR/USD","Fra40","Gasol","GBP/AUD","GBP/CAD","GBP/CHF","GBP/JPY","GBP/NZD","GBP/USD","GER30","GerTec","GOLD","Jp225","LCrude","Neth25","NGas","NZD/CAD","NZD/CHF","NZD/JPY","NZD/USD","OJ","SGD/JPY","SILVER","Soybns","Sugar","Swi20","TRY/JPY","UK100","Usa500","UsaInd","UsaRus","UsaTB","UsaTec","UsaVix","USD/BRL","USD/CAD","USD/CHF","USD/CNH","USD/HUF","USD/JPY","USD/MXN","USD/NOK","USD/PLN","USD/RUB","USD/SEK","USD/SGD","USD/TRY","USD/ZAR","Wheat")))
		{
			price_asset_2 = series(price());
	
			corr = Correlation(price_asset_1, price_asset_2, 240);
	
			if(!is(LOOKBACK))
			printf("n %s = %f", Asset, corr);
		}
	}
}



It contains 2 nested while loops but unfortunately hangs.
And I don't understand the explanatory text output of the script yet.

I would like to output something like
"Asset_1 to Asset_2 = Correlation X"
"Asset_1 to Asset_3 = Correlation X"

and so on.

Any hint?
Posted By: jcl

Re: Multi asset correlation - 02/22/19 08:57

This won't work either: Due to the special loop handling, the run function can call the loop() function once with assets and once with algos. Not two times with assets.

So, replace at least the second loop() call with a normal for() or while() loop, like this:

for(used_assets) { ...
© 2024 lite-C Forums