Intraday seasonality in FX market

Posted By: pcz

Intraday seasonality in FX market - 06/12/16 10:28

Very simple strategy based on this research paper: http://papers.ssrn.com/sol3/papers.cfm?abstract_id=2613592

I backtested it on EUR/USD M1 Dukascopy data, years 2004-2016. The results are interesting but there are some long drawdowns. Hopefuly you'll be able to improve the strategy in a way that is more suitable for deployment. Also note that I use my account params which is 0.5 spread and no commission. Works for GBP/USD too but the results are obviously very correlated and the return is lower.




Code:
var session1TZ = WET;
var session1Start = 8;
var session1End = 16;
var session2TZ = ET;
var session2Start = 11; // = 16 WET
var session2End = 17;

function tradeIS()
{
	if(dow() >= 1 && dow() <= 5) {
		if(NumOpenShort == 0 && lhour(session1TZ) == session1Start)
			enterShort();
		if(NumOpenShort > 0 && lhour(session1TZ) >= session1End)
			exitShort();
		if(NumOpenLong == 0 && lhour(session2TZ) == session2Start)
			enterLong();	
		if(NumOpenLong > 0 && lhour(session2TZ) >= session2End)
			exitLong();
	}
}

function run()
{
	StartDate = 2004;
	UnstablePeriod = 0;
	LookBack = 0;
	BarPeriod = 1;
	while(asset(loop("EUR/USD")))
		tradeIS();
	set(LOGFILE);
}



Code:
Test IS2 EUR/USD

Simulated account   AssetsFix 
Bar period          1 min (avg 1 min)
Test period         02.01.2004-17.05.2016 (4504144 bars)
Lookback period     0 bars (0 minutes)
Monte Carlo cycles  200
Assumed slippage    10.0 sec
Spread              0.5 pips (roll -0.42/0.06)
Contracts per lot   10000.0

Gross win/loss      125261$ / -111857$ (+13404p)
Average profit      1083$/year, 90$/month, 4.17$/day
Max drawdown        -1930$ 14% (MAE -1947$ 15%)
Total down time     94% (TAE 57%)
Max down time       117 weeks from Jul 2012
Max open margin     224$
Max open risk       160$
Trade volume        83921171$ (6781903$/year)
Transaction costs   -3211$ spr, -558$ slp, -266$ rol
Capital required    1174$

Number of trades    6423 (520/year, 10/week, 3/day)
Percent winning     51.6%
Max win/loss        347$ / -338$
Avg trade profit    2.09$ 2.1p (+37.8p / -36.0p)
Avg trade slippage  -0.09$ -0.1p (+0.6p / -0.8p)
Avg trade bars      414 (+415 / -414)
Max trade bars      479 (7 hours)
Time in market      59%
Max open trades     1
Max loss streak     11 (uncorrelated 13)

Annual return       92%
Profit factor       1.12 (PRR 1.08)
Sharpe ratio        0.88
Kelly criterion     0.84
R2 coefficient      0.946
Ulcer index         5.0%
Prediction error    59%

Confidence level     AR   DDMax  Capital

 10%                103%  1684$  1053$
 20%                 94%  1894$  1156$
 30%                 90%  1990$  1203$
 40%                 86%  2089$  1252$
 50%                 81%  2272$  1343$
 60%                 76%  2457$  1433$
 70%                 71%  2634$  1520$
 80%                 66%  2882$  1642$
 90%                 58%  3352$  1874$
 95%                 50%  3950$  2168$
100%                 32%  6360$  3355$

Portfolio analysis  OptF  ProF  Win/Loss  Wgt%

EUR/USD             .165  1.12  3315/3108  100.0  
EUR/USD:L           .225  1.13  1642/1569   39.1  
EUR/USD:S           .141  1.11  1673/1539   60.9

Posted By: MatPed

Re: Intraday seasonality in FX market - 06/12/16 17:31

Nice, I will have a look at this. Ciao
Posted By: boatman

Re: Intraday seasonality in FX market - 06/16/16 04:09

Thanks for sharing that strategy and your implementation, pcz.

Do you mind sharing who you trade with? Your transaction costs of 0.5 pips + 0 commission is very competitive. Is that an instiutional account?

My company is currently shopping around for brokers and I'm interested in talking to yours.

Many thanks!
Posted By: pcz

Re: Intraday seasonality in FX market - 06/17/16 09:13

I replied via PM. If anyone else is interested, send me a PM and I will copy/paste my reply to boatman laugh

Here I will only mention that the reason I don't count the commission into my backtests is because we pay fixed commission per month and have many many trades so the price per trade is negligible. Adding another strategy to our portfolio doesn't increase the cost (on the contrary it further decreases the average commission per trade).
Posted By: RTG

Re: Intraday seasonality in FX market - 07/25/16 14:48

Interesting
Posted By: RTG

Re: Intraday seasonality in FX market - 07/28/16 03:40

I've setup a demo account to run this script.

https://www.myfxbook.com/members/Sheffield/is-test/1724070
Posted By: trenki2

Re: Intraday seasonality in FX market - 08/27/16 17:38

Thanks for sharing your system. I took the idea and adapted the algorithm. I thought the same system might work for other currency pairs with different time settings.

The modified version uses Zorros optimize feature to automatically find profitable startHour and endHour pairs. The algorithm thus works using WFO optimization.
The only thing that is not really out of sample is the OptimalF factors.

Even though the drawdowns are less than in the original algorithm the average trade profit is reduced.

Here are my results:



Code:
Walk-Forward Test Test1 portfolio

Simulated account   AssetsFix 
Bar period          1 hour (avg 87 min)
Simulation period   05.04.2007-24.08.2016 (56590 bars)
Test period         16.09.2011-24.08.2016 (29952 bars)
Lookback period     80 bars (3 days)
WFO test cycles     5 x 5990 bars (51 weeks)
Training cycles     6 x 23960 bars (206 weeks)
Monte Carlo cycles  200
Assumed slippage    10.0 sec
Capital invested    10000$

Gross win/loss      127761$ / -112703$ (+9647p)
Average profit      3048$/year, 254$/month, 12$/day
Max drawdown        -1567$ 10% (MAE -1825$ 12%)
Total down time     90% (TAE 93%)
Max down time       14 weeks from Mar 2015
Max open margin     1239$
Max open risk       643$
Trade volume        140985019$ (28538185$/year)
Transaction costs   -7182$ spr, -118$ slp, -88$ rol, -10585$ com
Capital required    2460$

Number of trades    9615 (1947/year, 38/week, 8/day)
Percent winning     50.0%
Max win/loss        597$ / -172$
Avg trade profit    1.57$ 1.0p (+17.0p / -15.0p)
Avg trade slippage  -0.01$ -0.0p (+0.3p / -0.3p)
Avg trade bars      5 (+6 / -5)
Max trade bars      40 (40 hours)
Time in market      178%
Max open trades     10
Max loss streak     12 (uncorrelated 14)

Annual growth rate  20%
Profit factor       1.13 (PRR 1.10)
Sharpe ratio        0.96
Kelly criterion     3.02
R2 coefficient      0.842
Ulcer index         3.7%
Prediction error    124%

Confidence level     AR   DDMax  Capital

 10%                118%  1735$  2591$
 20%                112%  1911$  2728$
 30%                108%  2023$  2816$
 40%                101%  2278$  3014$
 50%                 98%  2382$  3095$
 60%                 95%  2511$  3195$
 70%                 91%  2711$  3352$
 80%                 86%  2963$  3548$
 90%                 79%  3354$  3852$
 95%                 74%  3725$  4142$
100%                 55%  5459$  5493$

Portfolio analysis  OptF  ProF  Win/Loss  Wgt%  Cycles

AUD/USD avg         .070  1.17  1181/1091   42.1  /X/X/
EUR/USD avg         .091  1.08  1244/1315   15.3  /\\\/
GBP/USD avg         .000  ----    0/0      0.0  .....
NZD/USD avg         .045  1.13  1129/1173   12.2  X\/XX
USD/CAD avg         .000  ----    0/0      0.0  .....
USD/CHF avg         .000  ----    0/0      0.0  .....
USD/JPY avg         .087  1.15  1258/1224   30.3  //XX/

ISL avg             .039  1.09  2478/2505   35.6  XXXX/
ISS avg             .054  1.17  2334/2298   64.4  /XXXX

AUD/USD:ISL         .130  1.08  616/606   11.7  /\/\/
AUD/USD:ISL:L       .130  1.08  616/606   11.7  /\/\/
AUD/USD:ISL:S       .000  ----    0/0      0.0  .....
AUD/USD:ISS         .292  1.30  565/485   30.4  /////
AUD/USD:ISS:L       .000  ----    0/0      0.0  .....
AUD/USD:ISS:S       .292  1.30  565/485   30.4  /////
EUR/USD:ISL         .304  1.07  638/641    9.7  /\\\/
EUR/USD:ISL:L       .304  1.07  638/641    9.7  /\\\/
EUR/USD:ISL:S       .000  ----    0/0      0.0  .....
EUR/USD:ISS         .061  1.09  606/674    5.6  /\\\/
EUR/USD:ISS:L       .000  ----    0/0      0.0  .....
EUR/USD:ISS:S       .061  1.09  606/674    5.6  /\\\/
NZD/USD:ISL         .005  0.98  613/666   -0.2  \\/\/
NZD/USD:ISL:L       .005  0.98  613/666   -0.2  \\/\/
NZD/USD:ISL:S       .000  ----    0/0      0.0  .....
NZD/USD:ISS         .176  1.15  516/507   12.4  /\//\
NZD/USD:ISS:L       .000  ----    0/0      0.0  .....
NZD/USD:ISS:S       .176  1.15  516/507   12.4  /\//\
USD/JPY:ISL         .119  1.18  611/592   14.4  /////
USD/JPY:ISL:L       .119  1.18  611/592   14.4  /////
USD/JPY:ISL:S       .000  ----    0/0      0.0  .....
USD/JPY:ISS         .232  1.13  647/632   16.0  //\\/
USD/JPY:ISS:L       .000  ----    0/0      0.0  .....
USD/JPY:ISS:S       .232  1.13  647/632   16.0  //\\/



And here is the code:

Code:
#define LONG	0
#define SHORT  1

bool Reinvest = false;
bool UseOptimalF = true;

var CalculateMargin(int side)
{
	var value = 0.1 * Capital;
	
	if (Reinvest)
	{
		value *= sqrt(Balance / Capital);
		//value *= sqrt(1 + max(0, ProfitClosed/Capital));
	}
	
	if (UseOptimalF)
		value *= ifelse(side == LONG, OptimalFLong, OptimalFShort);

	return value;
}

int hoursLookup[300];

function Initialize()
{
	int count = 0;
	int startHour, endHour;
	
	for (startHour = 0; startHour <= 23; ++startHour)
	for (endHour = startHour + 1; endHour <= 24; ++endHour)
		hoursLookup[count++] = (endHour % 24) * 24 + startHour;
}

int NumOpenTrades(int side)
{
	string currentAsset = Asset;
	string currentAlgo = Algo;	
	
	int count = 0;
	for (open_trades)
	{
		if (strcmp(TradeAsset, currentAsset) != 0 || strcmp(TradeAlgo, currentAlgo) != 0)
			continue;
		
		if ((TradeIsLong && side == LONG) || (TradeIsShort && side == SHORT))
			count++;
	}
	
	return count;
}

TRADE* EnterTrade(int side)
{
	if (side == LONG)
		return enterLong();
	else
		return enterShort();
}

void ExitTrade(int side)
{
	if (side == LONG)
		exitLong();
	else
		exitShort();	
}

function TradeIS(int side)
{
	int index = (int)(optimize(0, 0, 254, 1) / 254 * 299);
	int startHour = hoursLookup[index] % 24;
	int endHour = hoursLookup[index] / 24;
	
	// Exit over the weekend.
	if (dow() == FRIDAY && hour() >= 21)
	{ 
  		exitLong();
 	 	exitShort();
	}	
	
	Stop = 55 * PIP;
	Trail = 10 * PIP;
	
	// Don't trade from 00:00 to 00:00.
	if (startHour == endHour)
		return;
	
	// Trade during the week.
	if (dow() >= 1 && dow() <= 5) 
	{
		Margin = CalculateMargin(side);
		int numOpen = NumOpenTrades(side);
		if (numOpen == 0 && hour() == startHour)
			EnterTrade(side);	
		if (numOpen > 0 && hour() == endHour)
			ExitTrade(side);
	}
}

function run()
{
	set(PARAMETERS + FACTORS);
	
	StartDate = 2007;
	EndDate = 2016;
	BarPeriod = 60;
	Capital = 10000;
	DataSplit = 80;
	WFOPeriod = (312 * 24) * DataSplit / (100 - DataSplit);
	
	if (is(INITRUN))
		Initialize();

	while (asset(loop("AUD/USD", "EUR/USD", "GBP/USD", "NZD/USD", "USD/CAD",  "USD/CHF", "USD/JPY")))
	while (algo(loop("ISL", "ISS")))
	{
		if (Algo == "ISL")
			TradeIS(LONG);
		else if (Algo == "ISS")
			TradeIS(SHORT);
	}
}



Unfortunately the strategy is not really tradeable for the average trader. The average trade profit is just 1.0pip which is way too low. I backtested it also with variable spread and then the strategy fails.

But I think the strategy can work if one does not have to pay commissions.
Posted By: Grat

Re: Intraday seasonality in FX market - 08/27/16 18:21

So,

this is looks like the my idea. I'm publish two week before. Is better if optimize for the different days in week. In the monday is not optimal the same open hour like in the friday. But I'm not know, howto optimize for the each day. I get the error 40.

But i don't understant this:
Code:
int index = (int)(optimize(0, 0, 254, 1) / 254 * 299);

Posted By: trenki2

Re: Intraday seasonality in FX market - 08/27/16 18:56

Originally Posted By: Grat
So,

this is looks like the my idea. I'm publish two week before. Is better if optimize for the different days in week. In the monday is not optimal the same open hour like in the friday. But I'm not know, howto optimize for the each day. I get the error 40.

But i don't understant this:
Code:
int index = (int)(optimize(0, 0, 254, 1) / 254 * 299);



The optimize function computes an index into a lookup table. The lookup table stores the startHour and endHour combinations. There are 300 combinations per day. The optimize function picks an index into this lookup table. The expression is a bit complicated since it has to work around a limitation in the optimize function. It can only handle 255 steps, that is why it looks for a value between 0 and 254 and than scales that to [0, 299]. So not all entries in the lookup table can be picked but it is good enough.

If one would want to pick a different startHour/endHour depending on the weekday one possible solution for that would be to create one separate algorithm for each day f.i. by adding another parameter to the TradeIS() function and then adding more algos in the algo loop.
Posted By: Grat

Re: Intraday seasonality in FX market - 08/27/16 20:44

So,

after i make different open/close hour per day.
Quote:

Walk-Forward Test xcor4_TZD portfolio

Simulated account AssetsFix
Bar period 1 hour (avg 121 min)
Simulation period 01.05.2016-26.08.2016 (1439 bars)
Test period 10.08.2016-26.08.2016 (288 bars)
Lookback period 80 bars (3 days)
WFO test cycles 1 x 288 bars (17 days)
Training cycles 1151..0 bars (0 minutes, anchored)
Monte Carlo cycles 200
Assumed slippage 10.0 sec
Capital invested 10000$

Gross win/loss 4215$ / -777$ (+512p)
Average profit 77268$/year, 6439$/month, 297$/day
Max drawdown -250$ 7% (MAE -3558$ 104%)
Total down time 6% (TAE 25%)
Max down time 31 hours from Aug 2016
Max open margin 14243$
Max open risk 5808$
Trade volume 1697268$ (38149356$/year)
Transaction costs -103$ spr, 1.59$ slp, -14$ rol, -134$ com
Capital required 14994$

Number of trades 27 (607/year, 12/week, 3/day)
Percent winning 63.0%
Max win/loss 826$ / -160$
Avg trade profit 127$ 19.0p (+37.0p / -11.6p)
Avg trade slippage 0.06$ 0.0p (+0.2p / -0.4p)
Avg trade bars 21 (+30 / -8)
Max trade bars 86 (3 days)
Time in market 206%
Max open trades 19
Max loss streak 3 (uncorrelated 4)

Annual growth rate 76516%
Profit factor 5.42 (PRR 3.12)
Sharpe ratio 3.22
Kelly criterion 1.33
R2 coefficient 0.678
Ulcer index 44.3%
Prediction error 116%

Confidence level AR DDMax Capital

10% 539% 35$ 14347$
20% 538% 41$ 14366$
30% 537% 51$ 14397$
40% 536% 61$ 14427$
50% 535% 71$ 14455$
60% 532% 90$ 14512$
70% 528% 129$ 14631$
80% 525% 160$ 14722$
90% 521% 193$ 14822$
95% 520% 208$ 14868$
100% 514% 268$ 15047$

Portfolio analysis OptF ProF Win/Loss Wgt% Cycles

AUD/USD avg .283 0.96 1/2 -0.2 X
EUR/USD avg .406 5.47 2/2 6.9 X
GBP/USD avg .414 0.40 2/2 -3.5 X
NZD/USD avg .258 6.34 1/1 10.0 X
USD/CAD avg .542 3.74 3/3 24.9 X
USD/CHF avg .399 ++++ 3/0 4.8 /
USD/JPY avg .484 ++++ 5/0 56.9 /

1IS avg .469 0.84 3/4 -1.2 X
2IS avg .499 2.94 2/1 3.5 X
3IS avg .398 15.83 3/1 36.7 X
4IS avg .485 3.97 6/3 30.9 X
5IS avg .180 40.30 3/1 30.0 X

AUD/USD:2IS .999 0.00 0/1 -1.8 \
AUD/USD:2IS:L .999 0.00 0/1 -1.8 \
AUD/USD:2IS:S .999 ---- 0/0 0.0 .
AUD/USD:3IS .000 0.00 0/1 -2.5 \
AUD/USD:3IS:L .999 0.00 0/1 -2.5 \
AUD/USD:3IS:S .000 ---- 0/0 0.0 .
AUD/USD:4IS .000 ++++ 1/0 4.1 /
AUD/USD:4IS:L .000 ---- 0/0 0.0 .
AUD/USD:4IS:S .402 ++++ 1/0 4.1 /
EUR/USD:1IS .142 0.00 0/1 -0.8 \
EUR/USD:1IS:L .999 0.00 0/1 -0.8 \
EUR/USD:1IS:S .000 ---- 0/0 0.0 .
EUR/USD:4IS .786 ++++ 1/0 2.9 /
EUR/USD:4IS:L .999 ++++ 1/0 2.9 /
EUR/USD:4IS:S .000 ---- 0/0 0.0 .
EUR/USD:5IS .547 7.29 1/1 4.8 X
EUR/USD:5IS:L .547 ++++ 1/0 5.6 /
EUR/USD:5IS:S .524 0.00 0/1 -0.8 \
GBP/USD:1IS .999 ++++ 1/0 1.0 /
GBP/USD:1IS:L .999 ++++ 1/0 1.0 /
GBP/USD:1IS:S .000 ---- 0/0 0.0 .
GBP/USD:2IS .868 ++++ 1/0 1.3 /
GBP/USD:2IS:L .999 ++++ 1/0 1.3 /
GBP/USD:2IS:S .000 ---- 0/0 0.0 .
GBP/USD:4IS .344 0.00 0/2 -5.8 \
GBP/USD:4IS:L .152 0.00 0/1 -1.6 \
GBP/USD:4IS:S .999 0.00 0/1 -4.2 \
NZD/USD:1IS .000 0.00 0/1 -1.9 \
NZD/USD:1IS:L .999 0.00 0/1 -1.9 \
NZD/USD:1IS:S .000 ---- 0/0 0.0 .
NZD/USD:3IS .122 ++++ 1/0 11.9 /
NZD/USD:3IS:L .582 ++++ 1/0 11.9 /
NZD/USD:3IS:S .000 ---- 0/0 0.0 .
USD/CAD:1IS .999 0.00 0/2 -4.5 \
USD/CAD:1IS:L .580 0.00 0/1 -2.9 \
USD/CAD:1IS:S .999 0.00 0/1 -1.6 \
USD/CAD:3IS .999 ++++ 1/0 24.0 /
USD/CAD:3IS:L .000 ---- 0/0 0.0 .
USD/CAD:3IS:S .999 ++++ 1/0 24.0 /
USD/CAD:4IS .920 1.34 1/1 1.6 X
USD/CAD:4IS:L .402 0.00 0/1 -4.7 \
USD/CAD:4IS:S .999 ++++ 1/0 6.2 /
USD/CAD:5IS .227 ++++ 1/0 3.8 /
USD/CAD:5IS:L .000 ---- 0/0 0.0 .
USD/CAD:5IS:S .452 ++++ 1/0 3.8 /
USD/CHF:1IS .878 ++++ 1/0 1.2 /
USD/CHF:1IS:L .000 ---- 0/0 0.0 .
USD/CHF:1IS:S .999 ++++ 1/0 1.2 /
USD/CHF:3IS .999 ++++ 1/0 3.2 /
USD/CHF:3IS:L .999 ---- 0/0 0.0 .
USD/CHF:3IS:S .999 ++++ 1/0 3.2 /
USD/CHF:4IS .999 ++++ 1/0 0.4 /
USD/CHF:4IS:L .000 ---- 0/0 0.0 .
USD/CHF:4IS:S .999 ++++ 1/0 0.4 /
USD/JPY:1IS .039 ++++ 1/0 3.8 /
USD/JPY:1IS:L .000 ---- 0/0 0.0 .
USD/JPY:1IS:S .999 ++++ 1/0 3.8 /
USD/JPY:2IS .201 ++++ 1/0 4.1 /
USD/JPY:2IS:L .000 ---- 0/0 0.0 .
USD/JPY:2IS:S .999 ++++ 1/0 4.1 /
USD/JPY:4IS .952 ++++ 2/0 27.6 /
USD/JPY:4IS:L .999 ++++ 1/0 12.2 /
USD/JPY:4IS:S .847 ++++ 1/0 15.4 /
USD/JPY:5IS .999 ++++ 1/0 21.4 /
USD/JPY:5IS:L .000 ---- 0/0 0.0 .
USD/JPY:5IS:S .999 ++++ 1/0 21.4 /
Posted By: pcz

Re: Intraday seasonality in FX market - 09/02/16 15:50

I would be really careful with the random search for optimal values. It seems that there is (hopefully) a fundamental reason behind the strategy. When choosing different than working hours, the positive results might be purely by chance (data dredging). That said, besides EUR/USD I run it on USD/JPY too - but again using the working hours (US and Japanese), not optimized values.
Posted By: trenki2

Re: Intraday seasonality in FX market - 10/25/16 15:59

Could you share the exact session start end end time settings that you use for USD/JPY?
Posted By: pcz

Re: Intraday seasonality in FX market - 10/27/16 10:00

Originally Posted By: trenki2
Could you share the exact session start end end time settings that you use for USD/JPY?

Code:
session1TZ = JST;
session1Start = 9;
session1End = 16;
session2TZ = ET;
session2Start = 9;
session2End = 17;

Posted By: Repilado

Re: Intraday seasonality in FX market - 11/24/16 07:29

Hi pcz, thank you for sharing this information. Could you explain me how you imported the dukascopy data into Zorro? Thanks a lot!
Posted By: pcz

Re: Intraday seasonality in FX market - 11/27/16 19:07

(just linking the answer from another thread)
© 2024 lite-C Forums