SIGNALS file not being generated

Posted By: JamesHH

SIGNALS file not being generated - 07/03/19 21:36

When I set PEEK, and define a series that peeks ahead (for the target variable of course), then in some cases adviseLong with SIGNALS does not generate the signals file.

For example, in the following script (also attached) the median return one bar ahead over all assets is calculated in the variable cross_section_median. No signal files are output. However,
if I comment out the definition of cross_section_median then I get a signals file for each asset.

Is this a bug?


/// m-period simple return, at t steps back in time
var simple_returns(int t, int m) {
return priceClose(t) / priceClose(t + m) - 1;
}

/// cross-section median of m-period simple returns, at s steps ahead
var median_cross_section_returns(int s, int m) {
vars cross_section = malloc(NumAssetsListed * sizeof(var));

int i = 0;
while(asset(loop(Assets))) {
cross_section[i++] = simple_returns(-s, m);
}

var result = Median(cross_section, NumAssetsListed);

free(cross_section);

return result;
}

function run()
{
StartDate = 19900101;
EndDate = 19931231;
BarPeriod = 1440;
LookBack = 240;

NumWFOCycles = 1;
DataSplit = 75;

assetList("Assets.2.1993.csv");
set(RULES+PEEK);
Spread = RollLong = RollShort = Commission = Slippage = 0;
LifeTime = 1;
if(Train) Hedge = 2;

//vars next_return = series(simple_returns(-1, 1));
vars cross_section_median = series(median_cross_section_returns(1, 1));

char* symbol;
while(asset(symbol = loop(Assets))) {
adviseLong(SIGNALS, 0, simple_returns(1, 1), simple_returns(2, 1), simple_returns(3, 1));
enterLong();
}
}

Attached File
script.c  (0 downloads)
Posted By: jcl

Re: SIGNALS file not being generated - 07/04/19 06:39

This code will probably eat all available memory and then crash.
Posted By: JamesHH

Re: SIGNALS file not being generated - 07/04/19 14:22

No, it won't even start. If I run it with NEURAL instead of SIGNALS, the code exits before neural.init() is called.

So, what is the proper way to calculate the cross section median of the returns one bar ahead?
Posted By: jcl

Re: SIGNALS file not being generated - 07/04/19 14:55

I don't know, but run it in test mode and check the log - there you can proably see why it does not train. Also, better remove the malloc. Functions like malloc are resource heavy. You normally use them only at initialization, not at any bar.
Posted By: Grat

Re: SIGNALS file not being generated - 07/04/19 18:21

you are have data for symbol between 1990 and 1993?

StartDate = 19900101;
EndDate = 19931231;
Posted By: JamesHH

Re: SIGNALS file not being generated - 07/05/19 21:16

Originally Posted by Grat
you are have data for symbol between 1990 and 1993?

StartDate = 19900101;
EndDate = 19931231;


Yes. Of course, backtests on that data are not very meaningful for future trading laugh
Posted By: JamesHH

Re: SIGNALS file not being generated - 07/05/19 21:25

Originally Posted by jcl
I don't know, but run it in test mode and check the log - there you can proably see why it does not train. Also, better remove the malloc. Functions like malloc are resource heavy. You normally use them only at initialization, not at any bar.


Thanks for the tip on malloc.

It turns out the problem was the way I was using the loop function. I was looping once to calculate the indicator, and then trying to loop again. However, following the manual which suggests using either for(listed_assets), for(used_assets) or a for-loop, I get some unexpected results:

1. for(listed_assets) produces the SIGNALS files correctly (one for each symbol), but it give me the error: "Error 015: invalid advise signal[0] at bar 251"
2. for(used_assets) gives the same SIGNALS files and no error.
3. the for-loop results in each signal repeated twice in the SIGNALS files.

Any insights on why I am seeing this, or what the correct approach is here? Here is the test code for this issue:

/// 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()
{
StartDate = 19900101;
EndDate = 19931231;
BarPeriod = 1440;
LookBack = 240;

NumWFOCycles = 1;
DataSplit = 75;

assetList("Assets.2.1993.csv");
set(RULES+PEEK);
Spread = RollLong = RollShort = Commission = Slippage = 0;
LifeTime = 1;
if(Train) Hedge = 2;

// This initialize the assets
while(asset(loop(Assets))) {
}

char* symbol;
int i;
// #1:
//for(listed_assets) {
// #2:
for(used_assets) {
// #3:
//for (i = 0; symbol = Assets[i]; i++) {
adviseLong(SIGNALS, 0, simple_returns(1, 1), simple_returns(2, 1), simple_returns(3, 1));
enterLong();
}
}
Posted By: Spirit

Re: SIGNALS file not being generated - 07/06/19 07:32

I think its the other way round, you must use loop() for the advise calls. I dont understand why you use 2 loops. Invalid signal probably means you divide by zero.

https://manual.zorro-project.com/loop.htm
Posted By: JamesHH

Re: SIGNALS file not being generated - 07/06/19 19:24

Originally Posted by Spirit
I think its the other way round, you must use loop() for the advise calls. I dont understand why you use 2 loops. Invalid signal probably means you divide by zero.

https://manual.zorro-project.com/loop.htm


I will try the other way around. Of course I read the manual (as I mentioned previously), but these function calls are not fully defined.

As for two loops, I don't see how else this could possibly be done? I need an indicator which is calculated over all assets, so presumably I need a loop to define the indicator. The indicator must be defined before the advise calls which are done in a second loop.

Thanks, I will check if divide by zero could be happening, but I still don't understand the difference between for(listed_assets) and for(used_assets) after reading the manual.
Posted By: JamesHH

Re: SIGNALS file not being generated - 07/07/19 06:09

After making the corrections shown below everything is fine.

I used loop() for the advise calls as Spirit suggested. Though I'm not sure it's necessary, it is obviously better code. The main thing though was that I needed to explicitly call asset() in the for(listed_assets) loop:


vars cross_section;

/// m-period simple return, at t steps back in time
var simple_returns(int t, int m) {
return priceClose(t) / priceClose(t + m) - 1;
}

/// cross-section median of m-period simple returns, at s steps ahead
var median_cross_section_returns(int s, int m) {
int i = 0;
string current = Asset; // save the current asset
for(listed_assets) {
asset(Asset);
cross_section[i++] = simple_returns(-s, m);
}
asset(current); // restore the current asset

return Median(cross_section, NumAssetsListed);
}

function run()
{
StartDate = 19900101;
EndDate = 19931231;
BarPeriod = 1440;
LookBack = 240;

NumWFOCycles = 1;
DataSplit = 75;

set(RULES+PEEK);
Spread = RollLong = RollShort = Commission = Slippage = 0;
LifeTime = 1;
if(Train) Hedge = 2;

char* symbol;
while(asset(symbol = loop(Assets))) {
adviseLong(SIGNALS, median_cross_section_returns(1, 1), simple_returns(0, 1), simple_returns(1, 1), simple_returns(2, 1));
enterLong();
}
}

function main()
{
assetList("Assets.2.1993.csv");
cross_section = malloc(NumAssetsListed * sizeof(var));
}
Posted By: Grat

Re: SIGNALS file not being generated - 07/07/19 10:01

You have a function:
Code
function run()
{...}

and

function main()..


try remove main and use this:
Code
if (is(INITRUN)){
   assetList("Assets.2.1993.csv");
   cross_section = malloc(NumAssetsListed * sizeof(var));
}

inside run function
Posted By: JamesHH

Re: SIGNALS file not being generated - 07/08/19 01:40

Good point. Thanks for the tip.
© 2024 lite-C Forums