Gamestudio Links
Zorro Links
Newest Posts
Blobsculptor tools and objects download here
by NeoDumont. 03/28/24 03:01
Issue with Multi-Core WFO Training
by aliswee. 03/24/24 20:20
Why Zorro supports up to 72 cores?
by Edgar_Herrera. 03/23/24 21:41
Zorro Trader GPT
by TipmyPip. 03/06/24 09:27
VSCode instead of SED
by 3run. 03/01/24 19:06
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
5 registered members (Quad, TipmyPip, degenerate_762, AndrewAMD, Nymphodora), 997 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
sakolin, rajesh7827, juergen_wue, NITRO_FOREVER, jack0roses
19043 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Correlation of asset pairs #480509
06/12/20 02:05
06/12/20 02:05
Joined: Mar 2020
Posts: 41
J
jbhunter Offline OP
Newbie
jbhunter  Offline OP
Newbie
J

Joined: Mar 2020
Posts: 41
Looking for some feedback on this. The script just crashes. Too many series? Is there a better way to do this? Or should this work I am just doing it wrong?

Looking for best correlated pairs.

I would imagine the best answer is use R, matlab, or python. However there must be a way in Zorro.

Code
#define ASSET_NUM 5
function run() 
{
	set(LOGFILE);
	BarPeriod = 1440;
	LookBack = 100;
	StartDate = 2018;
	EndDate = 2019;
	
	assetList("AssetsIB_ETF");
	
	string a1;
	string a2;
	int cnt = 0;
	int i = 0;
	int j = 0;
	for(i = 0; i < ASSET_NUM; i++)
	{
		a1 = Assets[i];
		asset(a1);
		vars p1 = series(priceClose());
		for(j = 0; j < ASSET_NUM; j++)
		{
			a2 = Assets[j];
			cnt++;
			asset(a2);
			vars p2 = series(priceClose());
			var corr = Correlation(p1,p2,30);
			
			string pair = strcat(strcat(a1,"-"),a2);
			plot(pair,corr,LINE,BLACK);
		}
	}
	
}

Re: Correlation of asset pairs [Re: jbhunter] #480510
06/12/20 02:26
06/12/20 02:26
Joined: Feb 2017
Posts: 1,718
Chicago
AndrewAMD Online
Serious User
AndrewAMD  Online
Serious User

Joined: Feb 2017
Posts: 1,718
Chicago
Your strcat calls are very wrong. What are you even trying to do?

Re: Correlation of asset pairs [Re: AndrewAMD] #480519
06/12/20 11:22
06/12/20 11:22
Joined: Mar 2020
Posts: 41
J
jbhunter Offline OP
Newbie
jbhunter  Offline OP
Newbie
J

Joined: Mar 2020
Posts: 41
Ahh yes. The strcat calls were not correct. I was wanting to get a name for the plot such as, "a1-a2" for the plot. A simple strf("%s-%s",a1,a2) works.

I would like to get a rolling correlation between asset pairs. The script runs now. I have changed the series to returns, vars p1 = series((priceClose()-priceClose(1)/priceClose(1)));

Not plotting correctly at this point, but thanks for the help!

Re: Correlation of asset pairs [Re: jbhunter] #480529
06/12/20 22:22
06/12/20 22:22
Joined: Mar 2020
Posts: 41
J
jbhunter Offline OP
Newbie
jbhunter  Offline OP
Newbie
J

Joined: Mar 2020
Posts: 41
I eventually want to use a rolling correlation like above. Not sure how to actually do it yet.

However I have started with some random pairs currently. I would like to optimize on minimum hurst ratio. I am curious if they way I am defining assets and looping through them, including setting the Algo is messing up the optimization...

How might I ignore the asset for the optimization, and later OptimalF parameters?

When I uncomment either optimization line I get.

Error 011: optimize invalid number/range

Code
// Strategy template ///////////////////////


#define ASSET_NUM 10
void tradeLong(string a1, string a2)
{
	print(TO_LOG,"\ntradeLong, %s-%s",a1,a2);
	asset(a1);
	enterLong();
	asset(a2);
	enterShort();
}
void tradeShort(string a1, string a2)
{
	print(TO_LOG,"\ntradeShort, %s-%s",a1,a2);
	asset(a1);
	enterShort();
	asset(a2);
	enterLong();
}
void closeTrades(string a1, string a2)
{
	string algoName = strf("%s%s",a1,a2);
	Algo = algoName;
	

	print(TO_LOG,"\nCloseTrades",a1,a2);
	asset(a1);
	for (current_trades)
	{
		print(TO_LOG,"\n%i",TradeID);
		exitTrade(ThisTrade);
	}
	
	asset(a2);
	for (current_trades)
	{
		print(TO_LOG,"\n%i",TradeID);
		exitTrade(ThisTrade);
	}
}
void tradePair(string a1, string a2)
{
	string algoName = strf("%s%s",a1,a2);
	Algo = algoName;
	
	MaxLong = MaxShort = 1;
	var thH = 50;//optimize(30,30,60,5);
	var thL = -50;
	
	int hurstPer = 50;
	var minHurst = 0.5;//optimize(0.5,0.5,0.9,0.1);
	
	asset(a1);
	vars p1 = series(priceClose());
	vars ret1 = series(diff(priceClose()));
	var h1 = Hurst(p1, hurstPer);
	//vars s1 = series(StochEhlers(p1,14,0,0)*100);
	Stoch(14,3,MAType_EMA,3,MAType_EMA);
	vars s1 = series(rSlowK);

	asset(a2);
	vars p2 = series(priceClose());
	vars ret2 = series(diff(priceClose()));
	var h2 = Hurst(p2, hurstPer);
	//vars s2 = series(StochEhlers(p2,14,0,0)*100);
	Stoch(14,3,MAType_EMA,3,MAType_EMA);
	vars s2 = series(rSlowK);
	
	vars sd = series(s1[0]-s2[0]);
	
	plot("ret1",ret1[0],NEW+BARS,BLACK);
	plot("ret2",ret2[0],NEW+BARS,BLUE);
	
	plot("s1",s1[0],NEW,BLACK);
	plot("s2",s2[0],0,BLUE);
	plot("sd",sd[0],NEW,BLACK);
	
	plot("thH",thH,LINE,RED);
	plot("thL",thL,LINE,RED);
	plot("zero",0,LINE,RED);
	
	if (crossOver(sd,0) || crossUnder(sd,0))
		closeTrades(a1,a2);
	
	
	var hurstRatio = (h1+h2)/2;
	
	if (hurstRatio > minHurst)
	{
		if (crossUnder(sd,thL))
			tradeLong(a1,a2);
		
		if (crossOver(sd,thH))
			tradeShort(a1,a2);
	}
	
	
}

function run() 
{
	set(LOGFILE);
	setf(PlotMode,PL_ALL);
	BarPeriod = 1440;
	LookBack = 100;
	StartDate = 2015;
	EndDate = 2020;
	
	set(PARAMETERS);
	//set(FACTORS);
	
	//NumWFOCycles = 3;
	
	assetList("AssetsIB_ETF");
	
	Hedge = 5;
	
	int i;
	int j;
	string a1;
	string a2;
	
	for(i = 0; i < ASSET_NUM; i++)
	{
		a1 = Assets[i];
		for(j = i+1; j < ASSET_NUM; j++)
		{
			a2 = Assets[j];			
			tradePair(a1,a2);
		}
	}
	
}

Last edited by jbhunter; 06/12/20 22:23.
Re: Correlation of asset pairs [Re: jbhunter] #480530
06/13/20 01:36
06/13/20 01:36
Joined: Mar 2019
Posts: 357
D
danatrader Offline
Senior Member
danatrader  Offline
Senior Member
D

Joined: Mar 2019
Posts: 357
I am not sure if you can ignore the asset, since optimization is done basd on the asset returns.
But you can write your own optimize logic, if that is what you mean.

So returns of your strategy are is the optimize target.
You would need to give it an alternate target.

I did try with number of trades, worked fine.

But you can optimize outside the asset loop / algo loop, I think then it is global.

Last edited by danatrader; 06/13/20 01:37.
Re: Correlation of asset pairs [Re: danatrader] #480539
06/13/20 21:52
06/13/20 21:52
Joined: Mar 2020
Posts: 41
J
jbhunter Offline OP
Newbie
jbhunter  Offline OP
Newbie
J

Joined: Mar 2020
Posts: 41
I was able to do global optimization bringing the optimize() call prior to any for loops. However I am still struggling with how to optimize "per pair".

I have now tried this... For instance the PAIRLOOP contains "BBHFAZ","BBHGDX".... Then the string is split into the two asset symbols a1/a2.
Code
	string pair;
	while(pair = loop(PAIRLOOP))
	{
		Algo = pair;
		//string pair = dataStr(10,i,1);
		
		string a1 = strmid(pair,0,3);
		string a2 = strmid(pair,3,3);	
		
		tradePair(a1,a2);
	}



Code
void tradePair(string a1, string a2)
{
	int _stochPer = optimize(8,8,20,2);
	int _stochPer = optimize(8,8,20,2);
	var _th = optimize(50,35,95,5);
	int _hurstPer = 60;//round(optimize(40,20,70,10));
	var _hurstTh = 0.5;//optimize(0.5,0.3,0.8,0.1);
	int _atrPer = 20;//round(optimize(10,5,50,5));
	var _minCorr = 0;//optimize(0,0,0.5,0.1);
	int _corrPer = 100;
	
	SetMargin();
	MaxLong = MaxShort = 1;
	var thH = _th;
	var thL = -thH;
	
	asset(a1);
	vars p1 = series(priceClose());

	asset(a2);
	vars p2 = series(priceClose());
        .........
}



This seems to work but still has problems. I get stuff like this...

BBH:BBHFAZ 12.00 11.95 45.3=> 3.416
BBH:BBHGDX 12.00 16.27 44.2=> 2.132
BBH:BBHGLD 12 10.19 55.0=> 1.526
BBH:BBHSDS 12 13.76 54.3=> 5.327
BBH:BBHTLT 12 10.32 59.9=> 1.957
BBH:BBHTZA 12 10.21 60.2=> 3.145
BBH:BBHVXX 12.00 9.93 45.0=> 1.147
BBH:BBHXLU 12 16.11 65.0=> 6.489
BBH:DIAFAZ 8 8 50=> 0.000
BBH:DIAGDX 12 16.13 44.7=> 0.982
BBH:DIASDS 8 8 50=> 0.000
BBH:DIATZA 8 8 50=> 0.000



I have thought of setting Asset="BBHFAZ"; and listing them on the AssetList as dummies. But I am thinking the individual Asset="a1" calls later defeat the purpose.

Last edited by jbhunter; 06/14/20 03:29.
Re: Correlation of asset pairs [Re: jbhunter] #480547
06/14/20 09:54
06/14/20 09:54
Joined: Mar 2019
Posts: 357
D
danatrader Offline
Senior Member
danatrader  Offline
Senior Member
D

Joined: Mar 2019
Posts: 357
Not sure I understand you, but what about using a dummy asset?


Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1