Optimize in a ranked asset portfolio

Posted By: dBc

Optimize in a ranked asset portfolio - 10/20/20 18:09

I'm trying to implement in Zorro this R code provided in this site: IGM strategy

The code looks like this:

Code
vars Returns[6];
var  cumRets[6];
int  days = 0;	

function run()
{
//	set(PARAMETERS+LOGFILE+PLOTNOW);
	set(LOGFILE+PLOTNOW);
	BarPeriod    = 1440;
	LookBack     = 240;
	Verbose      = 0;
	NumWFOCycles = 5;
	
	
	
	int i;
	for(i=0;i<6;i++)
	{
		asset(Assets[i]);
		Returns[i] = series((price(0)-price(1))/price(1));
	}
	
	days++;
	
	if (days%20 == 0 && days>240) { // once a month do
		exitTrade();
		while(asset(loop("MDY", "TLT", "EEM", "ILF", "EPP", "FEZ")))
		{
			vars retSubset = Returns[Itor1];
			cumRets[Itor1] = Sum(retSubset,optimize(60,20,240,20));
		}
		
		int * Idx = sortIdx (cumRets, -6);
//		printf("\n %i", Idx[0]);
		asset(Assets[Idx[0]]);
		enterLong();
	}

}


The TEST phase works correctly, but the TRAIN phase optimize the last asset only, then the TEST issue an error that no .par file is available for each asset.
The manual explains that the TRAIN runs each asset with all available parameters value, then continue to the next asset.
In this specific strategy the position is taken only after all assets are ranked by their returns, in my understanding the current TEST approach can't handle such situation.
Am I correct or there is some hack to overcome this situation?
Posted By: danatrader

Re: Optimize in a ranked asset portfolio - 10/21/20 18:34

Is there a specific reason in this script to not use the assetloop?
Posted By: dBc

Re: Optimize in a ranked asset portfolio - 10/22/20 06:25

I'm using while(asset(loop... optimize( ...
If I use asset - loop to calculate the returns, I got the same results, no optimization is made.
Posted By: Zheka

Re: Optimize in a ranked asset portfolio - 10/24/20 08:32

The manual suggests using an while (asset(loop()) - while (algo(loop())) structure for optimizing params per component. Try adding the "algo" loop.
Posted By: dBc

Re: Optimize in a ranked asset portfolio - 10/24/20 11:54

Hi Zheka, I don't understand how to add a while(algo(loop when there is only one algorithm in this strategy.

I'm not sure that Zorro can optimize a rotation strategy. It seems that Zorro's optimization algorithm optimizes each asset separately.
Posted By: danatrader

Re: Optimize in a ranked asset portfolio - 10/24/20 12:03

Well, Zorro can optimize a rotation strategy, but then if you don't want to optimize every asset seperate, you'll need to move out your optimize calls from the assetcalls.
You need to manually define the optimize steps and values to optimize for.

Also looking at the newest beta, offers you plenty of options to optimize, at least latest with the version >=2.33 you can optimize everything.
Posted By: dBc

Re: Optimize in a ranked asset portfolio - 10/24/20 17:34

Thanks danatrader.
I do not agree with you, I think Zorro (at least in the free version), uses in its optimization algorithm a "vertical" approach.
When optimizing a portfolio of assets, Zorro looks at each asset separately, and no asset influences the optimization of other one.
In a rotation strategy one might optimize in a sense that there are influences in the optimization process between the asset ("horizontal" optimization).
In a horizontal optimization one would end with different parameter values for each asset.

If I would proceed with the R code provided in the attached link, and add an optimization code, I could easily get a different "lookback" period for each of the 6 assets candidates.
I'm not saying that optimizing each asset look-back would be the best approach for this strategy, but at least it's dame a trail.
Posted By: danatrader

Re: Optimize in a ranked asset portfolio - 10/24/20 18:15

I am sorry, may you explain in different words?

You say, "It seems that Zorro's optimization algorithm optimizes each asset separately."

Then you state, "In a horizontal optimization one would end with different parameter values for each asset."

Isn't that contradicting?
Then again, with beta 2.33 you can use R-Code for optimization.
Posted By: dBc

Re: Optimize in a ranked asset portfolio - 10/24/20 18:25

danatrader thanks for your feedback.

Yes it's contradicting. Zorro in my poor opinion optimizes in a "vertical" sense. And I'm looking to optimize in a "horizontal" sense.
I'll look into beta 2.33 to see if it can help me.
Posted By: danatrader

Re: Optimize in a ranked asset portfolio - 10/24/20 19:03

You maybe want to create a synthetic asset, optimize it and then select based on the optimize timeframe of the synthetic asset choose the best performing, the one with the most weight within the synthetic.
Posted By: dBc

Re: Optimize in a ranked asset portfolio - 10/24/20 19:20

danatrader thanks again for your reply

I looked at "Petra on Programming: Four Dimensions of Strength" and I got the response I looked for.

Here is the updated code:
Code
function run()
{
	set(PARAMETERS+LOGFILE+PLOTNOW);
//	set(LOGFILE+PLOTNOW);
	BarPeriod    = 1440;
	LookBack     = 120;
	Verbose      = 0;
	var Weights[6];
	string Stocks[6];
        Capital = 10000;
//	NumWFOCycles = 5;
	
	
	
	while(asset(loop(""MDY", "TLT", "EEM", "ILF", "EPP", "FEZ"")))
	{
		Returns[Itor1] = RET(optimize(60,20,120,20));
		Stocks[Itor1]  = Loop1;
	}
	
	
	if (month(0) != month(1)) { // once a month do
		int i;
		distribute(Weights,Returns,6,1,0);

		for(i=0;i<6;i++) {

			asset(Stocks[i]);
			int NewShares = Weights[i]*Balance/priceClose(0) - LotsPool;

			if(NewShares > 0)
				enterLong(NewShares);
			else if(NewShares < 0)
			        exitLong("",-NewShares);
		}
	}

}




This code optimizes each asset separately as expected
Posted By: kalmar

Re: Optimize in a ranked asset portfolio - 10/26/20 14:34

Hi dBc,

And what is RET() in your "Returns[Itor1] = RET(optimize(60,20,120,20));" means? Is your self-made function for return calculation?

Thx
Posted By: danatrader

Re: Optimize in a ranked asset portfolio - 10/26/20 15:08

https://zorro-trader.com/manual/en/ta.htm

RET(int TimePeriod): var
Return of the current asset: (Close(0)-Close(TimePeriod))/Close(TimePeriod). Source code in indicators.c
Posted By: kalmar

Re: Optimize in a ranked asset portfolio - 10/26/20 16:50

Sorry, Thx danatrader laugh I was searching for "RET(" and got nothing
Posted By: kalmar

Re: Optimize in a ranked asset portfolio - 10/26/20 17:18

Hi, dBc,

I guess this line "if (month(0) != month(1)) { // once a month do" could be also changed to "if(tdm() == 1 && !is(LOOKBACK)){ // 1st trading day of the month"
Posted By: dBc

Re: Optimize in a ranked asset portfolio - 10/27/20 06:33

Hi Kalmar,

I copied the code from "Petra on programing", but probably you can use if(tdm()==1 .... as well. I'm very new user of this amazing software.
© 2024 lite-C Forums