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, because I don't want optimize individual parameters for individual currency pairs.
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)))
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)); } }