Question on bar generation, why the "extra" bar?

Posted By: Dalla

Question on bar generation, why the "extra" bar? - 11/07/17 19:42

I have some M1 data for an asset that is open 0800 to 1645.
(some days actually only have data from 0802 or so, as we can see below)

Given that the M1 data ends at 1645 (market close), I was a bit surprised to see bars generated for the 2000 hour, since the market is not open by then.
A simple script like this

Code:
function run()
{
	set(LOGFILE+PARAMETERS);
	StartDate = 20170801;
	EndDate = 2017;
	LookBack = 600;
	BarPeriod = 240;
	StartWeek = 10800;
	EndWeek = 51645;

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

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

}



This generates a testlog that looks something like this:
Code:
[664: Mon 16-10-24 08:00] (10698.20)
[665: Mon 16-10-24 12:00] (10743.87)
[666: Mon 16-10-24 16:00] (10793.82)
[667: Mon 16-10-24 20:00] (10778.14)
[668: Tue 16-10-25 12:00] (10786.25)
[669: Tue 16-10-25 16:00] (10791.68)
[670: Tue 16-10-25 20:00] (10762.88)
[671: Wed 16-10-26 12:00] (10726.22)
[672: Wed 16-10-26 16:00] (10647.86)
[673: Wed 16-10-26 20:00] (10701.58)
[674: Thu 16-10-27 12:00] (10691.86)
[675: Thu 16-10-27 16:00] (10699.20)
[676: Thu 16-10-27 20:00] (10677.64)
[677: Fri 16-10-28 12:00] (10634.99)
[678: Fri 16-10-28 16:00] (10687.86)



Every day ends with a bar at 20:00, except Friday (since I set EndWeek to 51746).

I also get entries like this
Code:
[1421: Tue 11-03-15 16:00] -14053 +313 19/22  (6561.38)
[1422: Tue 11-03-15 20:00] -14053 +799 19/22  (6597.55)
[TEST:9:L2201] Long 1@6643.94 Risk 3103$ p at 20:00:00



Here a position is entered at 20:00. But like I said, the marked closed at 16:45, so in live trading this trade couldn't possibly happen. This is just one example, I get positions both opened and closed at 20:00.

Is this a bug, or am I missing something here? It just seems strange to me that a backtest can open/close trades when there is no historic data for the underlying asset.
Posted By: jcl

Re: Question on bar generation, why the "extra" bar? - 11/08/17 07:43

Looks ok to me. When the asset is traded between 16:00 and 20:00, then you also have an "extra" bar from 16:00 to 20:00.
Posted By: Dalla

Re: Question on bar generation, why the "extra" bar? - 11/08/17 10:50

OK, but this means that trades happen in the backtest that cannot possible happen in live trading. What is the point of that? In live trading the trades at 20:00 would generate an error, while they are successful in the backtest.
Posted By: jcl

Re: Question on bar generation, why the "extra" bar? - 11/08/17 13:05

The computer just does what you tell it to do. If you tell it to enter a trade, it will enter a trade. Limiting the trades to certain hours or other conditions is up to your script. The backtest doesn't know your broker's market hours.
Posted By: Zheka

Re: Question on bar generation, why the "extra" bar? - 11/08/17 16:20

It does look weird that despite there is no data in the database after 16:45, Zorro would still finish the 240-min bar at 20:00..

So, one has to use FrameOffset and framesync() to start off each day at 8 and end at 16:45, correct?
What would be the way to end the last bar each day on 16:45?
Posted By: jcl

Re: Question on bar generation, why the "extra" bar? - 11/08/17 17:08

If you want a bar start and end time divisible by 15 minutes, you need no FrameOffset, but 15 min bars or less.
Posted By: Zheka

Re: Question on bar generation, why the "extra" bar? - 11/08/17 19:21

So, in this case, to achieve a situation when the first 240min *period* in a day starts at 8:00 and the last ends at 16:45, one would do as follows:
...........
BarPeriod=15;
FrameOffset=8;
TimeFrame= framesync(24);
TimeFrame=16; //15*16=240
price=.....
if hour()=1600 and minute()=45 { TimeFrame=0}

enterLong()
....................

Correct?
Posted By: Dalla

Re: Question on bar generation, why the "extra" bar? - 11/08/17 20:25

I did this, which of course the last trade happens at 16

Code:
AssetMarket = UTC;
StartMarket = 0800;
EndMarket = 1600;
StartWeek = 10800;
EndWeek = 51745;
	
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;

Posted By: jcl

Re: Question on bar generation, why the "extra" bar? - 11/09/17 07:21

You need no TimeFrame with 15 min bars when the last bar ends at 16:45. No bars, no trading.

The TimeFrame mechanism is needed when you must skip bars outside trading hours. In that case it`s just TimeFrame = AssetFrame.
Posted By: Dalla

Re: Question on bar generation, why the "extra" bar? - 11/09/17 19:00

Yeah I know # but I never spoke about 15 min bars, that was Zheka 😀
My initial example was with 4h bars, and the above snippet solves that.

If you are saying that I could do just TimeFrame = AssetFrame instead of the above snippet, I'd be happy to reduce my code a bit 😀
Posted By: jcl

Re: Question on bar generation, why the "extra" bar? - 11/10/17 08:22

Yes, TimeFrame = AssetFrame is sufficient.
Posted By: Zheka

Re: Question on bar generation, why the "extra" bar? - 12/13/17 11:52

JCL,

I discovered that TimeFrame=AssetFrame works only in whole hours. E.g.:
Quote:
BarPeriod=20;
AssetZone=ET;
FrameOffset=51; //51=17hrs of 3x 20min bars;
TimeFrame=AssetFrame;

will end the Daily bar at 17:00 ET.
But with FrameOffset=50 (i.e. 1 bar less), the daily bar will end at 16:00 ET, not 16:40.

Can this be behaviour be changed to the expected?
Posted By: Zheka

Re: Question on bar generation, why the "extra" bar? - 12/13/17 14:08


Also, can AssetZone and TimeFrame be called in a function?
Posted By: jcl

Re: Question on bar generation, why the "extra" bar? - 12/13/17 19:50

Yes, as I understand the manual, AssetZone sets the time zone for hour bars. So for individual offsets and different bars you need to set up TimeFrame individually. You can use a function for that.
© 2024 lite-C Forums