Optimization

Posted By: MatPed

Optimization - 12/12/16 14:40

Hi,
I was experimenting a script found in the manual
Code:
// Currency Strength Strategy /////////////////////
// Exploits price shocks f.i. by CHF cap and Brexit

function run()
{
	set(PARAMETERS);  // Not as original
 	NumYears = 7; 		// Not as original
  	BarPeriod = 60;
  	Margin = 50;  		// Not as original
  	ccyReset();  // reset strengths at begin of any bar
  
  	string Name;
  	while(Name = (loop(Assets)))
  	{
    	if(assetType(Name) != FOREX) 
      continue; // Currency pairs only
    	asset(Name);
    	vars Prices = series(priceClose());
    	ccySet(ROC(Prices, 1)); // store price change as strength
  	}
  
// get currency pairs with highest and lowest strength difference
  	string Best = ccyMax(), Worst = ccyMin();
  	var Threshold = optimize(1.0, 1.0, 2.0, 0.1);  // Not as original

  	static char OldBest[10], OldWorst[10];  // static for keeping contents between runs
  	if(*OldBest && !strstr(Best,OldBest)) { // new strongest asset?
  		asset(OldBest);
   	exitLong();
  		if(ccyStrength(Best) > Threshold) {
   		asset(Best);
     		enterLong();
   	}
  	} 
  
 	if(*OldWorst && !strstr(Worst,OldWorst)) { // new weakest asset?
   	asset(OldWorst);
   	exitShort();
   	if(ccyStrength(Worst) < -Threshold) {
      	asset(Worst);
      	enterShort();
   	}
  	}



The only modification was few lines in order to optimize the main parameter: Threshold . Unfortunately it does not work.
What Am I missing?

Thank you in advance
Posted By: jcl

Re: Optimization - 12/13/16 11:43

The asset is missing. Portfolio strategies optimize the parameters for every asset separately. This won't work in your script since you're optimizing the parameter outside the loop() while no asset is selected.

The solution is simply not to use the loop() function, which indicates a portfolio strategy, but a simple for(..) loop to enumerate the assets. Then Zorro thinks it's no portfolio and you can optimize the threshold regardless of the asset.

int i;
for(i=0; Name=Assets[i]; i++)
...

Posted By: MatPed

Re: Optimization - 12/13/16 16:58

Thank you JCL. It works!
© 2023 lite-C Forums