0 registered members (),
677
guests, and 5
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Find best currency pair to trade
#484091
09/05/21 18:41
09/05/21 18:41
|
Joined: Dec 2014
Posts: 206 Germany
Smon
OP
Member
|
OP
Member
Joined: Dec 2014
Posts: 206
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 while(Name = (loop(Assets))) , because I don't want optimize individual parameters for individual currency pairs.
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: Grant]
#484093
09/06/21 13:08
09/06/21 13:08
|
Joined: Dec 2014
Posts: 206 Germany
Smon
OP
Member
|
OP
Member
Joined: Dec 2014
Posts: 206
Germany
|
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,731 Chicago
AndrewAMD
Serious User
|
Serious User
Joined: Feb 2017
Posts: 1,731
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.htmThe 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: 206 Germany
Smon
OP
Member
|
OP
Member
Joined: Dec 2014
Posts: 206
Germany
|
Thanks Andrew, that worked! Solution: 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: 206 Germany
Smon
OP
Member
|
OP
Member
Joined: Dec 2014
Posts: 206
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: ...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:
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: Grant]
#484418
10/24/21 07:09
10/24/21 07:09
|
Joined: Dec 2014
Posts: 206 Germany
Smon
OP
Member
|
OP
Member
Joined: Dec 2014
Posts: 206
Germany
|
'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: 296 Netherlands
Grant
Member
|
Member
Joined: Aug 2017
Posts: 296
Netherlands
|
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).
|
|
|
|