Gamestudio Links
Zorro Links
Newest Posts
Votes for future features
by 7th_zorro
Today at 01:57
Open WMB file
by Superku
Yesterday at 23:37
Stopping and resuming sessions without interrupting trade statis
by jcl
Yesterday at 08:14
Best Devlopment Experience
by j45p41
03/20/19 23:04
Blog Post: Continuous Futures Data in Zorro
by chsmac85
03/20/19 15:59
Error message : Closing Order Quantity
by OptimusPrime
03/20/19 12:49
script can work but no result(Profit and loss)
by jcl
03/20/19 09:12
AUM Magazine
Latest Screens
Pogostuck: Rage With Your Friends
Worst Case Z
AckCon'18 - Lotter vs the World 2 - Preview Release
Evonet Beta v1.0 : Online Project Development Template
Who's Online
3 registered (AndrewAMD, 7th_zorro, 1 invisible), 658 Guests and 8 Spiders online.
Key: Admin, Global Mod, Mod
Newest Members
FrankFred, QuartzLoop, Conchip, danatrader, ilikezorro
18190 Registered Users
Topic Options
Rate This Topic
#475390 - 12/05/18 20:48 How to exclude individual asset in Lookback period from trading?
Dalla Offline
Senior Member

Registered: 02/24/17
Posts: 345
I have a strategy that trades momentum over a stock universe of some ~200 stocks.
It trades daily bars and has a one year lookback. Some of the stocks have been listed longer than others, e.g. if I run a backtest from 2000, some of the stocks was not listed until 2005, 2010 etc.

My current problem is that sometimes after sorting stocks by momentum, trades are skipped because the one or more of the selected stocks are in a lookback. That is , they have a high momentum, but they were listed less than a year ago.

I have tried adding some
Code:
if(is(LOOKBACK))

logic to the script, but it doesn´t seem to help. I guess this LOOKBACK variable is for the entire script, and not per asset? Can I somehow exclude assets that are still in their lookback period from my momentum calculations?

Code:
//Find composite momentum
		while(loop(Assets))
		{
			asset(Loop1);
			exitLong();
			
                        //This is my attempt to exclude stocks in lookback
			if(is(LOOKBACK)) {
				printf("nCurrently in lookback %s", Asset);
				asset_num++;
				continue;
			}
			
			var mom12 = (priceClose(TRADING_DAYS_1_MONTH) - priceClose(TRADING_DAYS_12_MONTH)) / priceClose(TRADING_DAYS_12_MONTH);
			var mom6 =  (priceClose(TRADING_DAYS_1_MONTH) - priceClose(TRADING_DAYS_6_MONTH )) / priceClose(TRADING_DAYS_6_MONTH);
			var mom3 =  (priceClose(TRADING_DAYS_1_MONTH) - priceClose(TRADING_DAYS_3_MONTH )) / priceClose(TRADING_DAYS_3_MONTH);
			
			var compositeMomentum = (mom12+mom6+mom3)/3.;
			Returns[asset_num] = compositeMomentum;
			asset_num++;
		}
		
		
		//Capital = 100000;		
		//Margin = Capital+WinTotal-LossTotal;
		
		// sort returns lowest to highest
		int* idx = sortIdx(Returns, -asset_num);
		sortData(Returns, -asset_num);
		
		int i, j = 0;
		for(; j < 10; i++)
		{
			asset(Assets[idx[i]]);
			if (priceClose(0) == 0) {
				printf("nPrice is 0 for %s, skipping", Asset);
				continue;
			}

			var size = round(10000.0/priceClose(0));
			printf("nBuying %f %s at %f", size, Asset, priceClose(0));
			enterLong(size);
			j++;
		}
		printf("n--------------------------");


Top
#475392 - 12/05/18 22:26 Re: How to exclude individual asset in Lookback period from trading? [Re: Dalla]
DdlV Offline
Serious User

Registered: 06/10/13
Posts: 1403
Hi Dalla. You may be having some confusion regarding LOOKBACK? LOOKBACK means the current iteration of the script is still in the lookback phase - i.e., in a lookback bar before active bars where trading can happen.

When you say an Asset is "in lookback", meaning there isn't enough price data yet, you could look at priceClose(<OffsetAsFarBackAsYouNeed>) and if 0 exclude the Asset that way?

HTH.

Top
#475396 - 12/06/18 07:43 Re: How to exclude individual asset in Lookback period from trading? [Re: DdlV]
Dalla Offline
Senior Member

Registered: 02/24/17
Posts: 345
I don't think I'm confused. The message I'm seeing in the log occationally when entering a trade is
Code:
[ABB::L] Skipped (outside bars 523..3073)



Looking through the manual regarding these messages
"The trade was not entered because trading was disabled on that bar, for instance due to SKIP flags, in the LookBack period, inside a TimeFrame, or during weekends or holidays." The only thing that applies here is the lookback.

I tested your solution since it would make sense, but unfortunatly it doesn´t help.

Top
#475402 - 12/06/18 18:18 Re: How to exclude individual asset in Lookback period from trading? [Re: Dalla]
DdlV Offline
Serious User

Registered: 06/10/13
Posts: 1403
Well... LOOKBACK applies to the Bar, not the Asset(s). run() is executed every Bar. If the current Bar is in the Lookback period, LOOKBACK will be set, and trade entry attempts will fail with that message. This is all correct operation - trading doesn't start until after the Lookback period. Correct Zorro operation - your script's operation is a differnt issue laugh - you may not want your script to try to enter trades during the Lookback period.

But if you're seeing that message on a Bar after the Lookback period, then something else is causing it...

HTH.

Top
#475424 - 12/07/18 14:44 Re: How to exclude individual asset in Lookback period from trading? [Re: DdlV]
Dalla Offline
Senior Member

Registered: 02/24/17
Posts: 345
The problem is indeed that I'm seeing this message only for certain assets, after the lookback period, when other assets are succesfully traded.

Top
#475429 - 12/07/18 15:43 Re: How to exclude individual asset in Lookback period from trading? [Re: Dalla]
jcl Offline

Chief Engineer

Registered: 07/22/00
Posts: 26788
Loc: Frankfurt
Check the bar number. You can see in the log at which bar the lookback period ends, so you can verify if the skipping is due to the lookback period, or due to TimeFrame or other reasons.

Top
#476569 - 03/10/19 20:46 Re: How to exclude individual asset in Lookback period from trading? [Re: jcl]
Dalla Offline
Senior Member

Registered: 02/24/17
Posts: 345
Reviving this old thread, since I'm still having this issue and still not sure how to solve it. But I've found out why it happens:
I have a universe of ~ 300 equities. I have history from 1995 or so, but some of the stocks in the universe have been listed between 1995 and now.

I have Lookback = 252 in my script.

Let's take the example of ABB which I used above.
The strategy starts at bar 0, naturally.
But he history of the ABB asset itself starts at bar 271.

Let's say that my strategy gives me the signal to buy ABB at bar 400.
We are now outside the strategys Lookback period, this will still give me the message
[ABB::L] Skipped (outside bars 523..3073)
Makes sense I guess.

So the question is, how can I determine that I'm outside the valid bar range for a specific asset?

Top
#476571 - 03/11/19 07:19 Re: How to exclude individual asset in Lookback period from trading? [Re: Dalla]
jcl Offline

Chief Engineer

Registered: 07/22/00
Posts: 26788
Loc: Frankfurt
You can use the suspended() function.

Top
#476578 - 03/11/19 12:40 Re: How to exclude individual asset in Lookback period from trading? [Re: jcl]
Dalla Offline
Senior Member

Registered: 02/24/17
Posts: 345
Exactly what I was looking for, thanks!

Top



Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de