Gamestudio Links
Zorro Links
Newest Posts
Data from CSV not parsed correctly
by EternallyCurious. 04/18/24 10:45
StartWeek not working as it should
by Zheka. 04/18/24 10:11
folder management functions
by VoroneTZ. 04/17/24 06:52
lookback setting performance issue
by 7th_zorro. 04/16/24 03:08
zorro 64bit command line support
by 7th_zorro. 04/15/24 09:36
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:48
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:46
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
0 registered members (), 668 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
EternallyCurious, 11honza11, ccorrea, sakolin, rajesh7827
19046 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
Question on bar generation, why the "extra" bar? #469192
11/07/17 19:42
11/07/17 19:42
Joined: Feb 2017
Posts: 369
D
Dalla Offline OP
Senior Member
Dalla  Offline OP
Senior Member
D

Joined: Feb 2017
Posts: 369
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.

Re: Question on bar generation, why the "extra" bar? [Re: Dalla] #469200
11/08/17 07:43
11/08/17 07:43
Joined: Jul 2000
Posts: 27,978
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,978
Frankfurt
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.

Re: Question on bar generation, why the "extra" bar? [Re: jcl] #469205
11/08/17 10:50
11/08/17 10:50
Joined: Feb 2017
Posts: 369
D
Dalla Offline OP
Senior Member
Dalla  Offline OP
Senior Member
D

Joined: Feb 2017
Posts: 369
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.

Re: Question on bar generation, why the "extra" bar? [Re: Dalla] #469206
11/08/17 13:05
11/08/17 13:05
Joined: Jul 2000
Posts: 27,978
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,978
Frankfurt
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.

Re: Question on bar generation, why the "extra" bar? [Re: jcl] #469212
11/08/17 16:20
11/08/17 16:20
Joined: Jul 2017
Posts: 784
Z
Zheka Offline
User
Zheka  Offline
User
Z

Joined: Jul 2017
Posts: 784
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?

Re: Question on bar generation, why the "extra" bar? [Re: Zheka] #469220
11/08/17 17:08
11/08/17 17:08
Joined: Jul 2000
Posts: 27,978
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,978
Frankfurt
If you want a bar start and end time divisible by 15 minutes, you need no FrameOffset, but 15 min bars or less.

Re: Question on bar generation, why the "extra" bar? [Re: jcl] #469223
11/08/17 19:21
11/08/17 19:21
Joined: Jul 2017
Posts: 784
Z
Zheka Offline
User
Zheka  Offline
User
Z

Joined: Jul 2017
Posts: 784
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?

Last edited by Zheka; 11/08/17 19:22.
Re: Question on bar generation, why the "extra" bar? [Re: Zheka] #469224
11/08/17 20:25
11/08/17 20:25
Joined: Feb 2017
Posts: 369
D
Dalla Offline OP
Senior Member
Dalla  Offline OP
Senior Member
D

Joined: Feb 2017
Posts: 369
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;


Re: Question on bar generation, why the "extra" bar? [Re: Dalla] #469227
11/09/17 07:21
11/09/17 07:21
Joined: Jul 2000
Posts: 27,978
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,978
Frankfurt
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.

Re: Question on bar generation, why the "extra" bar? [Re: jcl] #469254
11/09/17 19:00
11/09/17 19:00
Joined: Feb 2017
Posts: 369
D
Dalla Offline OP
Senior Member
Dalla  Offline OP
Senior Member
D

Joined: Feb 2017
Posts: 369
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 😀

Page 1 of 2 1 2

Moderated by  Petra 

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