How to limit strategy to only trade during market hours?

Posted By: Dalla

How to limit strategy to only trade during market hours? - 11/04/17 12:44

When building a system for e.g. DAX, I want to limit the strategy to only trade during market hours. Reading about AssetMarket in the manual it says:

"Time zone of the currently selected asset, used for setting AssetFrame to a TimeFrame that skips all bars outside market hours in local time, but follows the BarPeriod inside market hours. The market hours are given by StartMarket and EndMarket."

Great, this looks just like what I want: Skip bars outside market hours.

So I add to the top of my strategy

Code:
set(LOGFILE+PARAMETERS);
	StartDate = 2004;
	EndDate = 2017;
	LookBack = 600;
	BarPeriod = 240;
	AssetMarket = UTC;
	StartMarket = 0800;
	EndMarket = 1645;
	
	vars close = series(priceClose());
	vars open = series(priceOpen());
	vars high = series(priceHigh());
	vars low = series(priceLow());
	vars price = series(price());

        ....



But still, after backtesting and looking at the trade log, I can see entries, stop losses and time stops being executed outside market hours.

So how can I limit the strategy to only execute entries and stops inside market hours?
Posted By: jcl

Re: How to limit strategy to only trade during market hours? - 11/06/17 09:12

I do not see where your script skips bars or skips trading outside market hours. You can find examples of skipping bars in the manual. The simplest way is just checking the time, f.i. if(between(ltod(0),930,1530)) enterLong();.
Posted By: Dalla

Re: How to limit strategy to only trade during market hours? - 11/06/17 10:43

The manual suggests that setting AssetMarket together with StartMarket and EndMarket will handle skipping bars for me.

Also, if(between(ltod(0),930,1530)) enterLong();. will help not with Stop and Lifetime I guess?
Posted By: jcl

Re: How to limit strategy to only trade during market hours? - 11/06/17 14:45

TimeFrame is used for skipping bars. My one line example did indeed not do Stop and lifetime - I know you as a seasoned script writer and did not assume that you needed help with that wink.
Posted By: Dalla

Re: How to limit strategy to only trade during market hours? - 11/06/17 20:32

Yeah yeah ;-)
Stop and LifeTime aside, there is clearly something here that I fail to understand.

Repeating myself; When reading about AssetMarket in the manual, it states:

"Time zone of the currently selected asset, used for setting AssetFrame to a TimeFrame that skips all bars outside market hours in local time, but follows the BarPeriod inside market hours. The market hours are given by StartMarket and EndMarket."

Like you said, there is an example of how to skip bars in the manual. At least I can find one here: http://www.zorro-trader.com/manual/en/barperiod.htm

This is what it says
Code:
// skip bars outside market hours (equivalent to AssetMarket) //////
// equivalent to AssetZone
static int SkippedBars = 0;
if(!market(ET,0)) {
   TimeFrame = 0;
   SkippedBars--; // count negative number of bars outside market hours
} else if(TimeFrame == 0) {
   TimeFrame = SkippedBars;
   SkippedBars = 0;
} else
  TimeFrame = 1;
vars PriceInMarketHours = series(price());



I don't understand how what this snippet and AssetMarket have in common?
Posted By: jcl

Re: How to limit strategy to only trade during market hours? - 11/07/17 09:18

You must set TimeFrame to 0 for skipping bars outside trading hours. Then, at trading start, set TimeFrame to the negative number of skipped bars. AssetFrame can be used to do that automatically. There are examples in the manual how to set TimeFrame from AssetFrame.

http://manual.zorro-project.com/assetzone.htm
Posted By: Dalla

Re: How to limit strategy to only trade during market hours? - 11/07/17 09:48

OK. One more question on this.

Can bar skipping not be used together with for example the ATR indicator?

Manual says "The function internally creates series when TimeFrame is > 1, and must then be called in a fixed order in the script."

If I use the snippet above together with something simple like
Code:
if(  price[0] > ATR(20 ))	{
    enterLong();
}



my script stops execution with a message along these lines
Bar 1212: 4 - Bar 1213: 1

I guess ATR does not like skipping bars?
Posted By: jcl

Re: How to limit strategy to only trade during market hours? - 11/07/17 12:41

No, the error message looks like inconsistent series. This has nothing to do with skipping bars, but could mean the ATR call itself is somewhere skipped.
Posted By: Dalla

Re: How to limit strategy to only trade during market hours? - 11/07/17 13:11

I don't think so. This simple reproducer also gives that same error message:

Code:
function run()
{
	set(LOGFILE+PARAMETERS);
	StartDate = 2016;
	EndDate = 2017;
	LookBack =600;
	BarPeriod = 60;
	AssetMarket = UTC;
	StartMarket = 0800;
	EndMarket = 1800;
	
	static int SkippedBars = 0;
	if(!market(UTC,0)) {
		TimeFrame = 0;
		SkippedBars--; // count negative number of bars outside market hours
	} else if(TimeFrame == 0) {
		TimeFrame = SkippedBars;
		SkippedBars = 0;
	} else
		TimeFrame = 1;

	vars prices = series(priceClose());
	LifeTime = 10;

	if (prices[0] > ATR(200))
		enterLong();

}

Posted By: jcl

Re: How to limit strategy to only trade during market hours? - 11/07/17 14:58

That's inconsistent series, but I must admit that it is a bit tricky.

ATR internally generates a series only when TimeFrame is different to 1. So, we have an inconsistent number of series, but the problem is indeed related to skipping bars.

This should fix it:

Code:
...
vars O = series(priceOpen()),
	H = series(priceHigh()),
	L = series(priceLow()),
	prices = series(priceClose());
if(prices[0] > ATR(O,H,L,prices,200))
	enterLong();
}



I have to mention this in the manual because a normal user has indeed no chance to see the reason of the problem.
Posted By: Dalla

Re: How to limit strategy to only trade during market hours? - 11/07/17 19:18

Thanks, that fixed the problem!
Posted By: 3dgamelight

Re: How to limit strategy to only trade during market hours? - 09/12/18 07:14

What is the recommended way to set market hours when the market reopen on the same day?
© 2024 lite-C Forums