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
Find best currency pair to trade #484091
09/05/21 18:41
09/05/21 18:41
Joined: Dec 2014
Posts: 204
Germany
Smon Offline OP
Member
Smon  Offline OP
Member

Joined: Dec 2014
Posts: 204
Germany
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));		
	}	
}



Re: Find best currency pair to trade [Re: Smon] #484092
09/05/21 21:27
09/05/21 21:27
Joined: Aug 2017
Posts: 294
Netherlands
G
Grant Offline
Member
Grant  Offline
Member
G

Joined: Aug 2017
Posts: 294
Netherlands
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++)

Re: Find best currency pair to trade [Re: Grant] #484093
09/06/21 13:08
09/06/21 13:08
Joined: Dec 2014
Posts: 204
Germany
Smon Offline OP
Member
Smon  Offline OP
Member

Joined: Dec 2014
Posts: 204
Germany
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!

Re: Find best currency pair to trade [Re: Smon] #484094
09/06/21 17:04
09/06/21 17:04
Joined: Feb 2017
Posts: 1,718
Chicago
AndrewAMD Online
Serious User
AndrewAMD  Online
Serious User

Joined: Feb 2017
Posts: 1,718
Chicago
"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.

Re: Find best currency pair to trade [Re: AndrewAMD] #484095
09/06/21 19:26
09/06/21 19:26
Joined: Dec 2014
Posts: 204
Germany
Smon Offline OP
Member
Smon  Offline OP
Member

Joined: Dec 2014
Posts: 204
Germany
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));
}
}

Re: Find best currency pair to trade [Re: Smon] #484416
10/23/21 20:48
10/23/21 20:48
Joined: Dec 2014
Posts: 204
Germany
Smon Offline OP
Member
Smon  Offline OP
Member

Joined: Dec 2014
Posts: 204
Germany
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
		}
	}

Re: Find best currency pair to trade [Re: Smon] #484417
10/23/21 22:56
10/23/21 22:56
Joined: Aug 2017
Posts: 294
Netherlands
G
Grant Offline
Member
Grant  Offline
Member
G

Joined: Aug 2017
Posts: 294
Netherlands
'rt:2' indicates a missing file.

Re: Find best currency pair to trade [Re: Grant] #484418
10/24/21 07:09
10/24/21 07:09
Joined: Dec 2014
Posts: 204
Germany
Smon Offline OP
Member
Smon  Offline OP
Member

Joined: Dec 2014
Posts: 204
Germany
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.

Last edited by Smon; 10/24/21 07:50.
Re: Find best currency pair to trade [Re: Smon] #484422
10/24/21 10:08
10/24/21 10:08
Joined: Aug 2017
Posts: 294
Netherlands
G
Grant Offline
Member
Grant  Offline
Member
G

Joined: Aug 2017
Posts: 294
Netherlands
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).

Re: Find best currency pair to trade [Re: Smon] #486751
09/28/22 09:23
09/28/22 09:23
Joined: Apr 2021
Posts: 10
Southampton
B
Bennycjones Offline
Newbie
Bennycjones  Offline
Newbie
B

Joined: Apr 2021
Posts: 10
Southampton
I believe I had a similar problem before andthat calling asset(Asset); before my asset loop fixed it whilst still enabling global optimisation.


Moderated by  Petra 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1