2 registered members (dr_panther, 7th_zorro),
1,203
guests, and 2
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Unexpected behaviour with two loops over multiple assets
#477900
08/12/19 23:18
08/12/19 23:18
|
Joined: Oct 2018
Posts: 72
JamesHH
OP
Junior Member
|
OP
Junior Member
Joined: Oct 2018
Posts: 72
|
I need to loop twice over my assets to code a particular strategy, because the decision to enter short or long depends on the ranking of signals over the assets.
So, in the first loop an ML model is called to get estimated probabilities:
char* symbol; int i = 0; while(asset(symbol = loop(Assets))) { Spread = RollLong = RollShort = Commission = Slippage = 0;
var signal = adviseLong(NEURAL, target[0], features, LookBack); signals[i++] = signal; }
Here I found that loop(Assets) and for(listed_assets) have different behaviour (I guess this is an example of "special parameter handling by loop"?), so I stick with the "standard" choice of loop(Assets).
Then the signal rankings are determined:
sortRank(ranks_01, signals, NumAssetsListed);
And a second loop decides whether to go long or short depending on the ranking:
i = 0; for(listed_assets) { asset(Asset); int rank = ranks[i++];
... enterLong() or enterShort() if the rank is high or low enough, respectively ... }
This works perfectly when I tried it with up to 50 assets. However, when I test with my full asset list of 448 symbols, the second loop does not work anymore. It makes calls to enterShort() but no short trades are taken.
Am I doing something wrong here?
Last edited by JamesHH; 08/12/19 23:29.
|
|
|
Re: Unexpected behaviour with two loops over multiple assets
[Re: JamesHH]
#477916
08/13/19 20:56
08/13/19 20:56
|
Joined: Oct 2018
Posts: 72
JamesHH
OP
Junior Member
|
OP
Junior Member
Joined: Oct 2018
Posts: 72
|
Below is a complete MRE, except for the asset data. When run with just the first 50 assets, everything works fine. But when the full asset list is used only long positions are entered. The printf output confirms that enterShort() is being called even though no short positions are actually taken. Is there a problem with the MRE script?
#include <r.h>
vars signals, ranks_01;
/// m-period simple return, at t steps back in time var simple_returns(int t, int m) { return priceClose(t) / priceClose(t + m) - 1; }
function run() { Script = "DeepLearn"; StartDate = 19930101; EndDate = 19931231; BarPeriod = 1440; LookBack = 240;
NumWFOCycles = 1; DataSplit = 75;
set(RULES+PEEK+LOGFILE); LifeTime = 1; if(Train) Hedge = 2; if(is(INITRUN)) { // All 448 assets assetList("Assets.KDH.1993.csv"); // First 50 assets only //assetList("Assets.50.1993.csv"); printf("\nNumAssetsListed: %d", NumAssetsListed); signals = malloc(NumAssetsListed * sizeof(var)); ranks_01 = malloc(NumAssetsListed * sizeof(var)); } vars target_returns = series(simple_returns(-1, 1));
char* symbol; int i = 0; while(asset(symbol = loop(Assets))) { var signal = adviseLong(NEURAL, target_returns[0], simple_returns(0, 1), simple_returns(1, 1), simple_returns(2, 1)); signals[i++] = signal; } sortRank(ranks_01, signals, NumAssetsListed);
i = 0; for(listed_assets) { asset(Asset); // Convert to an integer rank between 1 and NumAssetsListed int rank = round(ranks_01[i++] * (NumAssetsListed - 1)) + 1; //printf("\nrank: %d", rank); if(rank == 1) { printf("\nGoing short..."); enterShort(); } if(rank == NumAssetsListed) { printf("\nGoing long..."); enterLong(); } } }
Last edited by JamesHH; 08/13/19 20:57. Reason: typo
|
|
|
Re: Unexpected behaviour with two loops over multiple assets
[Re: JamesHH]
#477930
08/15/19 12:59
08/15/19 12:59
|
Joined: Jan 2019
Posts: 73 berlin
laz
Junior Member
|
Junior Member
Joined: Jan 2019
Posts: 73
berlin
|
This is just a guess, I have read something here, that could possibly fit. You transfer here 448 parameters to a function (loop(Assets)), maybe jcl can clarify on whether this is still problematic or not? I can not test your script, unfortunately I do not have so many assets or data from 1993. Set Verbose higher, enable DIAG and debug deeper, use stepping for debugging with watch("!blabla"), look into the logfiles... https://zorro-trader.com/manual/en/verbose.htmhttp://manual.zorro-trader.com/watch.htmhttps://zorro-trader.com/manual/en/trouble.htmZorro "normally" gives you a hint what goes wrong...
Last edited by laz; 08/15/19 13:15.
|
|
|
Re: Unexpected behaviour with two loops over multiple assets
[Re: JamesHH]
#478064
08/31/19 01:13
08/31/19 01:13
|
Joined: Oct 2018
Posts: 72
JamesHH
OP
Junior Member
|
OP
Junior Member
Joined: Oct 2018
Posts: 72
|
For now, I was just going to generate .t6 files with prices set to 0 when the data runs out, since price(0) == 0 can be checked from the script.
Actually, that doesn't work because Zorro gives Warning 035: Price gap, and uses the last real price ignoring the zeros. In case someone is interested in the kludge I used to detect the end of the historical data then I am happy to share it here.
|
|
|
|