Find best currency pair to trade

Posted By: Smon

Find best currency pair to trade - 09/05/21 18:41

I want to trade macroeconomic data. Let's say, non-farm payrolls just came out. So I'm looking for the best USD pair to take a trade. Either USD against the weakest or the strongest counter currency. The solution I came up with is this script:

However, it has two problems (Zorro 2.40.3): When I let the "findPair" function run via the print command, I get "Warning 048: asset loop recursion" and currency strength values off the scale or zero. Why is this? Does somebody know a better solution? I don't want to do
Code
while(Name = (loop(Assets)))
, because I don't want optimize individual parameters for individual currency pairs.

Code
string myCurrency(int idx)
{
	if(idx == 0) return ("AUD");
	if(idx == 1) return ("CAD");
	if(idx == 2) return ("CHF");
	if(idx == 3) return ("EUR");
	if(idx == 4) return ("GBP");
	if(idx == 5) return ("JPY");
	if(idx == 6) return ("NZD");
	if(idx == 7) return ("USD");
}

string findPair(string PrimCurrency, int bullbear)
{
	var foundStrength;
	var maxStrength;
	var minStrength;
	string Strongest;
	string Weakest;
	int initVal = 0; //
	int i;
	
	printf("\n\n.........");
	
	for(i=1; i<8; i++)
	{
		if(strcmp(myCurrency(i),PrimCurrency) == 0) continue;
		
		foundStrength = ccyStrength(myCurrency(i));
		//printf("\ni = %d, %s mit %.5f", i, myCurrency(i), foundStrength);
		if(foundStrength > maxStrength || initVal == 0)
		{
			
			maxStrength = foundStrength;
			Strongest = myCurrency(i);
			//printf("\...maxStrength set to %.5f", maxStrength);
			
		}
		if(foundStrength < minStrength || initVal == 0)
		{
			minStrength = foundStrength;
			Weakest = myCurrency(i);
			initVal = 1;
			//printf("\...minStrength set to %.5f", minStrength);
		}
	}
	printf("\nStrongest: %s: %.5f; Weakest: %s, %.5f", Strongest, maxStrength, Weakest, minStrength);
	
	string SekondaryCurrency = ifelse(bullbear == 0, Strongest, Weakest);
	string myAsset;
	int treffer = 0;
	
	for(listed_assets)
	{
		if(assetType(Asset) != FOREX) continue;
		myAsset = strf("%s/%s", PrimCurrency, SekondaryCurrency);
		printf("\nmyAsset = %s, Asset = %s", myAsset, Asset);
		if(strcmp(myAsset, Asset) == 0){
			printf(" <- chosen!");
			return(myAsset);
		}
		myAsset = strf("%s/%s", SekondaryCurrency, PrimCurrency);
		printf("\nmyAsset = %s, Asset = %s", myAsset, Asset);
		if(strcmp(myAsset, Asset) == 0){

			printf(" <- chosen!");
			return(myAsset);
		}
	}
	printf("\nSomething went wrong, you shouldn't be here! myAsset = %s", myAsset);	
}



function run()
{
	
	StartDate = 20120101;
	EndDate = 20210101;
	BarPeriod = 15;
	LookBack = 200*1440/BarPeriod; //200 Bars in D1
	set(STEPWISE);

	
	ccyReset();
	string Name;
	
	for(listed_assets)
	{
		Name = Asset;
		if(assetType(Name) != FOREX) continue;
		asset(Name);
		vars Prices = series(priceClose());
		
		var Strength = ROC(Prices,1);
 		ccySet(Strength);
	}

	static char OldBest[8], OldWorst[8];
	string Best = ccyMax(), Worst = ccyMin();
	
	string myAsset = "USD";
	
	if(!is(LOOKBACK))
	{
		printf("\nAUD %.4f, CAD %.4f, CHF %.4f, EUR %.4f GBP %.4f, JPY %.4f, NZD %.4f, USD %.4f", ccyStrength("AUD"),ccyStrength("CAD"),ccyStrength("CHF"),ccyStrength("EUR"),ccyStrength("GBP"),ccyStrength("JPY"),ccyStrength("NZD"),ccyStrength("USD"));	
		
		printf("\nChosen pair for USD bullish: %s", findPair(myAsset, 1));
		//printf("\nChosen pair for USD bearish: %s", findPair(myAsset, 0));		
	}	
}


Posted By: Grant

Re: Find best currency pair to trade - 09/05/21 21:27

I can't answer all your questions, but when you work with 8 pairs from 'idx' 0 to 7, then your for-loop should be: for(i=0; i<8; i++)
Posted By: Smon

Re: Find best currency pair to trade - 09/06/21 13:08

Originally Posted by Grant
I can't answer all your questions, but when you work with 8 pairs from 'idx' 0 to 7, then your for-loop should be: for(i=0; i<8; i++)


That was a bug indeed, but it didn't fix the problem. Thanks!
Posted By: AndrewAMD

Re: Find best currency pair to trade - 09/06/21 17:04

"Warning 048: asset loop recursion" is a very sneaky bug! You must fix this.

You are using a special enumeration loop macro, documented here:
https://zorro-project.com/manual/en/fortrades.htm

The problem is that you are breaking / returning out the loop without calling break_assets. Either finish the loop or break using break_assets, and then return the required value.
Posted By: Smon

Re: Find best currency pair to trade - 09/06/21 19:26

Thanks Andrew, that worked!


Solution:


Quote

string myCurrency(int idx)
{
if(idx == 0) return ("AUD");
if(idx == 1) return ("CAD");
if(idx == 2) return ("CHF");
if(idx == 3) return ("EUR");
if(idx == 4) return ("GBP");
if(idx == 5) return ("JPY");
if(idx == 6) return ("NZD");
if(idx == 7) return ("USD");
}

string findPair(string PrimCurrency, int bullbear)
{
var foundStrength;
var maxStrength;
var minStrength;
string Strongest;
string Weakest;
int initVal = 0; //don't break the first loop!
int i;

printf("\n\n.........");

for(i=0; i<8; i++)
{
if(strcmp(myCurrency(i),PrimCurrency) == 0) continue;

foundStrength = ccyStrength(myCurrency(i));

if(foundStrength > maxStrength || initVal == 0)
{
maxStrength = foundStrength;
Strongest = myCurrency(i);
}
if(foundStrength < minStrength || initVal == 0)
{
minStrength = foundStrength;
Weakest = myCurrency(i);
initVal = 1;
}
}
printf("\nStrongest: %s: %.5f; Weakest: %s, %.5f", Strongest, maxStrength, Weakest, minStrength);

string SekondaryCurrency = ifelse(bullbear == 0, Strongest, Weakest);
string myAsset;

for(listed_assets)
{
if(assetType(Asset) != FOREX) break_assets;

myAsset = strf("%s/%s", PrimCurrency, SekondaryCurrency);
printf("\nmyAsset = %s, Asset = %s", myAsset, Asset);
if(strcmp(myAsset, Asset) == 0){
printf(" <- chosen");
break_assets;

}
myAsset = strf("%s/%s", SekondaryCurrency, PrimCurrency);
printf("\nmyAsset = %s, Asset = %s", myAsset, Asset);
if(strcmp(myAsset, Asset) == 0){

printf(" <- chosen");
break_assets;
}
}
return(myAsset);
}




function run()
{

StartDate = 20120101;
EndDate = 20210101;
BarPeriod = 15;
LookBack = 200*1440/BarPeriod; //200 Bars in D1
set(STEPWISE);


ccyReset();
string Name;

for(listed_assets)
{
Name = Asset;
if(assetType(Name) != FOREX) continue;
asset(Name);
vars Prices = series(priceClose());

var Strength = ROC(Prices,1);
ccySet(Strength);
}

static char OldBest[8], OldWorst[8];
string Best = ccyMax(), Worst = ccyMin();

string myAsset = "USD";

if(!is(LOOKBACK))
{
printf("\nAUD %.4f, CAD %.4f, CHF %.4f, EUR %.4f GBP %.4f, JPY %.4f, NZD %.4f, USD %.4f", ccyStrength("AUD"),ccyStrength("CAD"),ccyStrength("CHF"),ccyStrength("EUR"),ccyStrength("GBP"),ccyStrength("JPY"),ccyStrength("NZD"),ccyStrength("USD"));

printf("\nChosen pair for USD bullish: %s", findPair(myAsset, 1));
//printf("\nChosen pair for USD bearish: %s", findPair(myAsset, 0));
}
}
Posted By: Smon

Re: Find best currency pair to trade - 10/23/21 20:48

Stuck again...

I trained the strategy with only global parameters (independent of any asset)

My error message:

myscript compiling.....................
Error 062: Can't open myscript_EURAUD_1.par (rt:2)
Error 044: Data\myscript_EURAUD_1.par not trained



The manual states:

Quote

...They are stored in Data\*.par. This file contains the parameters in the order they appear in the script. If the strategy does set up the asset itself, training generates different parameter files dependent on the asset selected in the [Asset] scrollbox.



This is my script stripped down to the crucial parts:

Code
        StartDate = 20120101;
	EndDate = 20210101;

	NumWFOCycles = 5;
	NumCores = 4;
	//set(STEPWISE);
	set(PARAMETERS);
	Spread = RollLong = RollShort = Commission = Slippage = 0;

	var boring = optimize(0.5, 0.1, 2);
	var atrfactor = optimize(1, 0.1, 20);
	
	ccyReset();
	for(listed_assets)
	{
		Name = Asset;
		if(assetType(Name) != FOREX) continue;
		asset(Name);
		vars Prices = series(priceClose());
		DominantCycle[str2int(Asset)] = 0.5*DominantPeriod(Prices,60);  //96*15 = 1440
		
		var Strength = ROC(Prices,1);
 		ccySet(Strength);
	}

	for(i=0; i<8; i++)
	{
		string Currency = myCurrency(i);
		//cycle through single currencies
		//determine, if I'm bullish or bearish the single currency
		//find best counter currency based on currency strength
		
		if(bullish) asset(findPair(Currency, 1));
		if(bearish) asset(findPair(Currency, 0));
		
		//check some other stuff
		
		if(I want to trade)
		{
			for(current_trades) if(TradeIsAsset) return;
			//don't trade if there is a trade running on this currency pair
			
			//other trade related stuff
		}
	}
Posted By: Grant

Re: Find best currency pair to trade - 10/23/21 22:56

'rt:2' indicates a missing file.
Posted By: Smon

Re: Find best currency pair to trade - 10/24/21 07:09

Originally Posted by Grant
'rt:2' indicates a missing file.


Sorry, I forgot to mention that. Yes this file wasn't created. Instead I got:

myscript.par
myscript_1.par
myscript_2.par
....

As I would expect, because the parameters aren't asset related. I think the next problem would arise if I mix global parameters with asset specific ones...


I tested what happens if I manually rename the files to myscript_USDJPY_1.par. (USDJPY is just an example. The Zorro looks for whatever asset I choose from the dropdown menu)

Error message:

Read myscript_USDJPY_1.par
Error 042: no USD/JPY parameters at walk 0

So the script somehow seems to think, that the parameters are asset specific.
Posted By: Grant

Re: Find best currency pair to trade - 10/24/21 10:08

Originally Posted by Smon
I think the next problem would arise if I mix global parameters with asset specific ones...


Maybe, I don't know. Just to be sure I would suggest to write your global vars to an AlgoVar or AssetVar (or a different type).
Posted By: Bennycjones

Re: Find best currency pair to trade - 09/28/22 09:23

I believe I had a similar problem before andthat calling asset(Asset); before my asset loop fixed it whilst still enabling global optimisation.
© 2024 lite-C Forums