I’m going to backtest this Gold futures strategy from @Dburgh:
here is the 2nd #FreeFriday strategy. It is for #GC_F and holds for 3 days. Close[0] means current bar's close and [2] means 2 bars ago. pic.twitter.com/5Z3Uc3FCOP
— Dave Bergstrom (@Dburgh) 25 de noviembre de 2016
First we import the basic packages: pandas and numpy for time series manipulation and quandl for retrieving data:
import numpy as np import pandas as pd import quandl
Then we need to obtain daily data for ES futures from Quandl:
gold_daily = quandl.get("CHRIS/CME_GC1", start_date="2001-5-14", end_date="2016-11-23") # use Last column as Close gold_daily.columns=['Open', 'High', 'Low', 'Close', 'Change', 'Settle', 'Volume', 'Open Interest']
Since the algorithm logic is pretty simple, we can backtest using a vectored approach instead of an event driven loop:
multiplier=100 signal=( (gold_daily.Low > gold_daily.shift(5).Close) & (gold_daily.shift(3).Open > gold_daily.shift(9).Low) & (gold_daily.shift(7).Open > gold_daily.shift(8).Close) & (gold_daily.index.dayofweek == 2) ) # hold time 3 days profits=(signal*multiplier*(gold_daily.shift(-3).Close-gold_daily.Close)) returns=(signal*(-1+gold_daily.shift(-3).Close/gold_daily.Close))
Now we can plot the cumulative returns:
profits.cumsum().plot()
And this is what we get:
Want to know the hitrate of this strategy? Around 65%
len(profits[profits>0])/signal.sum()
Sharpe ratio? 0.89
np.sqrt(252) * (np.mean(returns)) / np.std(returns)