Gamestudio Links
Zorro Links
Newest Posts
Instrument Price Zorro Chart
by felixfx. 10/19/19 20:48
Today real live account blown - my experience
by Clifford_B. 10/19/19 17:09
Building Diversified Portfolios - New Paper
by SnoopySniff. 10/18/19 16:23
Indices & metals easier to trade than FX???
by SnoopySniff. 10/18/19 10:08
What are you working on?
by txesmi. 10/17/19 07:00
Ducascopy exporter
by line7. 10/17/19 06:21
AUM Magazine
Latest Screens
The Space Between
Pogostuck: Rage With Your Friends
Worst Case Z
AckCon'18 - Lotter vs the World 2 - Preview Release
Who's Online Now
5 registered members (AndrewAMD, felixfx, Dalla, painkiller, ugofx), 658 guests, and 0 spiders.
Key: Admin, Global Mod, Mod
Newest Members
VillageTalkies, line7, Shutch, Clifford_B, Paulius
18310 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Unexpected behaviour with two loops over multiple assets #477900
08/12/19 23:18
08/12/19 23:18
Joined: Oct 2018
Posts: 40
J
JamesHH Offline OP
Newbie
JamesHH  Offline OP
Newbie
J

Joined: Oct 2018
Posts: 40
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] #477906
08/13/19 15:19
08/13/19 15:19
Joined: Jul 2000
Posts: 27,029
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,029
Frankfurt
Probably yes, but not with the loops. They look ok.

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: 40
J
JamesHH Offline OP
Newbie
JamesHH  Offline OP
Newbie
J

Joined: Oct 2018
Posts: 40
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: 71
berlin
L
laz Offline
Junior Member
laz  Offline
Junior Member
L

Joined: Jan 2019
Posts: 71
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.htm
http://manual.zorro-trader.com/watch.htm
https://zorro-trader.com/manual/en/trouble.htm

Zorro "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: laz] #477941
08/16/19 18:39
08/16/19 18:39
Joined: Oct 2018
Posts: 40
J
JamesHH Offline OP
Newbie
JamesHH  Offline OP
Newbie
J

Joined: Oct 2018
Posts: 40
Thanks for your input.

Yes, that is very interesting. If too many parameters for the "Chinese compiler" is still an issue, I wonder if using MSVC with Zorro S could fix the problem? But yes, we need clarification from jcl or one of the Zorro engineers.

I could include data if it would help, but probably I should be making a support request if it comes to that.

So I set Verbose = 7 | DIAG as in the first suggestion, and there is something strange in the logs:

-----
[480: Thu 93-09-30 15:40] 31.16/31.16\30.71/30.84 -0.0100
Going short...
[43851610::S] Skipped (outside bars 481..543)
Going long...
[46069010::L] Skipped (outside bars 481..543)
End of lookback period

[481: Fri 93-10-01 15:40] 30.84/31.03\30.77/30.90 -0.0100
Going short...
[74763310::S] Skipped (outside bars 481..480)
Going long...
TRD: 64 MB allocated
[23331110::L8101] Long 1@30.24 at 15:40:00
Com 0.0100 Mrg 15.13 Net 0
Units 1.0000 MTotal 0.00 MCost 15.1250 PCost 0.01000 Opn 481
-----

In the first bar after the lookback period, bar 481, we can see that the short position is skipped because it is outside the range 481..480. This looks like a bug in Zorro to me, but I'm just a newbie at Zorro.

Re: Unexpected behaviour with two loops over multiple assets [Re: JamesHH] #478039
08/28/19 20:44
08/28/19 20:44
Joined: Oct 2018
Posts: 40
J
JamesHH Offline OP
Newbie
JamesHH  Offline OP
Newbie
J

Joined: Oct 2018
Posts: 40
It turns out the problem was that my strategy wasn't checking for delisted assets, so one of the assets had incomplete data for the test period.

Is there some way from Zorro to check when an asset timeseries "runs out"?

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.

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: 40
J
JamesHH Offline OP
Newbie
JamesHH  Offline OP
Newbie
J

Joined: Oct 2018
Posts: 40
Originally Posted by JamesHH

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.


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