BarPeriod + TimeFrame issue

Posted By: trenki2

BarPeriod + TimeFrame issue - 08/18/16 21:40

I developed a script with a BarPeriod of 1440 and then wanted to integrate that one into a portfolio script that used a BarPeriod of 60.

For this to work I set the TimeFrame for the code of this script to 24 so that again it would result in daily trade signals.

I expected the simulation results to be the same but they weren't. With BarPeriod 60 and TimeFrame 24 I got a much lower AR.

Running the following script with different settings shows what I mean. Not only the AR but also the number of trades is different.

Code:
function run()
{
	BarPeriod = 1440;
	TimeFrame = 1;
	
	//BarPeriod = 60;
	//TimeFrame = 24;
	
	StartDate = 2015;
	EndDate = 2016;
	
	asset("EUR/USD");
	
	Stop = 20 * PIP;
	if (NumOpenTotal == 0)
		enterLong();
}



From the documentation about TimeFrame I assumed this should be the same. What am I missing?
Posted By: jcl

Re: BarPeriod + TimeFrame issue - 08/19/16 09:46

It is not the same, but very different. Use the frameSync function for synchronizing time frames. Details are explained in the manual under "BarPeriod" with several examples.

Posted By: trenki2

Re: BarPeriod + TimeFrame issue - 08/19/16 10:05

Ok, I tried to use frameSync() as in the examples but I still get very different results. What else might be wrong:

Code:
#define H24 (1440/BarPeriod)	

function run()
{
	//BarPeriod = 1440;
	//TimeFrame = 1;
	
	BarPeriod = 60;
	TimeFrame = frameSync(H24);
	
	StartDate = 2015;
	EndDate = 2016;
	
	asset("EUR/USD");
	
	Stop = 20 * PIP;
	if (NumOpenTotal == 0)
		enterLong();
}

Posted By: jcl

Re: BarPeriod + TimeFrame issue - 08/19/16 11:04

Probably because your 1440 version ignored intrabar prices for the stop. Use the TICKS flag.

You can find out yourself why your strategy does what it does by looking into the log. Compare the logs and you see where any differences come from.
Posted By: trenki2

Re: BarPeriod + TimeFrame issue - 08/19/16 11:18

I tried with set(TICKS). It just made the simulation slower, but the results were almost exactly the same.

Will have to take a look at the logs ...
Posted By: trenki2

Re: BarPeriod + TimeFrame issue - 08/19/16 11:50

I looked at the logs and found some differences:

* The logs start at different dates:
60: [1: Fri 02.01.15 02:00] 1.20660
1440: [1: Tue 06.01.15 00:00] 1.19460

* The first trade happens at different times.
60:
[19: Sun 04.01.15 23:00] 1.20090
[EUR/USD::L1901] Long 1@1.2000 Risk 2$ at 23:00

1440:
[1: Tue 06.01.15 00:00] 1.19460
[EUR/USD::L0101] Long 1@1.1936 Risk 2$ at 00:00

* Sunday trades don't happen at midnight but at 23:00.
So frameSync() does not help that trades get opened at daily bar starts?

[19: Sun 04.01.15 23:00] 1.20090
[EUR/USD::L1901] Long 1@1.2000 Risk 2$ at 23:00

* The number of trades is different
60: 39
1440: 19

I tried to set Lookback = 0 as i suspected it might have something to do with the logs starting at different times but that was not it.

Here is the code that generated my logs:

Code:
function run()
{
	set(TICKS + LOGFILE);
	LookBack = 0;
	
	if (0)
	{
		BarPeriod = 1440;
		TimeFrame = 1;		
	}
	else
	{
		BarPeriod = 60;
		TimeFrame = frameSync(24);		
	}
	
	StartDate = 2015;
	EndDate = 2016;
	
	asset("EUR/USD");
	
	Stop = 20 * PIP;
	if (NumOpenTotal == 0)
		enterLong();
}



Attached File
Testtest60.txt  (2 downloads)
Attached File
Testtest1440.txt  (1 downloads)
Posted By: jcl

Re: BarPeriod + TimeFrame issue - 08/19/16 15:18

For starting at a certain date, use a 8-digit start date such as 20150102 (must not be a weekend). And for trading at Monday 00:00, set StartWeek = 10000.
Posted By: trenki2

Re: BarPeriod + TimeFrame issue - 08/19/16 16:17

When I set StartDate = 20150101 and StartWeek the logs still start at different times:

Code:
60:
Test: Test EUR/USD 2015..2016
[1: Fri 02.01.15 02:00]  1.20660

1440:
Test: Test EUR/USD 2015..2016
[1: Tue 06.01.15 00:00]  1.19460



The results differ less but the number of trades is still different and the times at which they are entered do not match either.

Since 20150101 is actually a holliday i tried with StartDate = 20150105.

The log with 60 now starts at the expected time. The log with 1440 has the first line with Tue 06.01.15.

The initial trades now seem to be entered at the same time and the stop loss is also triggered at the same time. But the entry and exit price is slightly different between both logs:

Code:
60:
[47: Wed 07.01.15 00:00]  -2 +0 0/1
[EUR/USD::L4702] Long 1@1.1872 Risk 2$  at 00:00
...
[EUR/USD::L4702] Stop 1@1.1852: -1.83 at 09:59

1440:
[2: Wed 07.01.15 00:00]  -2 +0 0/1
[EUR/USD::L0202] Long 1@1.1870 Risk 2$  at 00:00
[EUR/USD::L0202] Stop 1@1.1850: -1.82 at 09:59



The number of trades is also off by 1.

The trade that is entered on Monday 19.01.15 is stopped out at a different time:

Code:
60:
[231: Mon 19.01.15 00:00]  -17 +0 0/9
[EUR/USD::L3110] Long 1@1.1561 Risk 2$  at 00:00
...
[273: Tue 20.01.15 18:00]  -17 -1 0/10
[EUR/USD::L3110] Stop 1@1.1541: -1.83 at 18:14

1440:
[10: Mon 19.01.15 00:00]  -16 +0 0/9
[EUR/USD::L1010] Long 1@1.1559 Risk 2$  at 00:00

[11: Tue 20.01.15 00:00]  -16 +3 1/9
[12: Wed 21.01.15 00:00]  -16 -1 0/10
[13: Thu 22.01.15 00:00]  -16 +5 1/9
[EUR/USD::L1010] Stop 1@1.1538: -1.85 at 13:41



Trade L7911 from the 60 log does not happen in the 1440 log.

Trade L1512 from 1440 and L4713 from 60 are started at the same time but the stop time does not match. The same goes for L4918 from 1440 and L1619 from 60.

These seem to be all the differences. Is this a bug?

Current code:
Code:
function run()
{
	set(TICKS + LOGFILE);
	LookBack = 0;
	
	StartDate = 20150105;
	EndDate = 20160105;
	StartWeek = 10000;
	
	if (0)
	{
		BarPeriod = 1440;
		TimeFrame = 1;		
	}
	else
	{
		BarPeriod = 60;
		TimeFrame = frameSync(24);		
	}
	
	asset("EUR/USD");
	
	Stop = 20 * PIP;
	if (NumOpenTotal == 0)
		enterLong();
}




Attached File
Testtest60.txt  (1 downloads)
Attached File
Testtest1440.txt  (1 downloads)
Posted By: jcl

Re: BarPeriod + TimeFrame issue - 08/19/16 17:08

100% identical trades are unlikely due to random slippage. You can simulate no slippage by setting Slippage = 0. This should then eliminate the differences, at least all that I can think of.
Posted By: trenki2

Re: BarPeriod + TimeFrame issue - 08/19/16 17:33

Even setting Slippage = 0 does not eliminate the differences. The two trades from the logs above that started on 19.01.15 did indeed result in almost the same loss but one was stopped out two days earlier than the other. I don't think that can be because of slippage.

I've modified the example. Now I count the number of trades opened. When TradeNum is even I go long, otherwise I go short.

With BarPeriod 1440 i get 7 Trades. With BarPeriod 60 i get 18 Trades. This is a big difference.

Maybe that is because even though the entry time is the same the logs report a different entry price between the 1440 and 60 version. Why is that?

Code:
function run()
{
	Slippage = 0;
	
	set(TICKS + LOGFILE);
	LookBack = 0;
	
	StartDate = 20150105;
	EndDate = 20160105;
	StartWeek = 10000;
	
	if (1)
	{
		BarPeriod = 1440;
		TimeFrame = 1;		
	}
	else
	{
		BarPeriod = 60;
		TimeFrame = frameSync(24);		
	}
	
	asset("EUR/USD");
	
	static int TradeNum = 0;
	
	Stop = 20 * PIP;
	if (NumOpenTotal == 0)
	{
		if (TradeNum++ % 2 == 0)
			enterLong();
		else
			enterShort();
	}
}

Posted By: jcl

Re: BarPeriod + TimeFrame issue - 08/19/16 18:28

I leave this as a little brain-teaser over the weekend. Who can see why this code still does not get the same number of trades on 1-day and 1-hour bars? Solution Monday. Have a nice weekend!
Posted By: jcl

Re: BarPeriod + TimeFrame issue - 08/22/16 08:27

Ok, no takers, so here's the solution. One version of the script increments the counter 24 times faster than the other version.
Posted By: trenki2

Re: BarPeriod + TimeFrame issue - 08/22/16 10:52

Still this does not explain why the number of trades in the former example without the TradeNum counter are different. I applied every hint you gave and they are still different. It was not because of Slippage either.

An the other question was why the entry price for the same time and date is different between the 1440 and 60 log. Shouldn't it be the same?
Posted By: jcl

Re: BarPeriod + TimeFrame issue - 08/22/16 11:27

Almost. There can be minimal differences due to interpolation of gaps, and thus also minimal differences in entry and exit prices. Aside from that the trading behavior and the result should be identical. Otherwise check out the log - there you should see the reason of any different trade.
© 2024 lite-C Forums