如何在Z-Score回测中填补政权?

时间:2017-04-16 22:06:14

标签: python finance trading back-testing

我目前正在尝试实施单一资产回溯测试,当zscore低于某个阈值时会生成买入信号,当高于阈值时卖出。

df1 = pd.read_csv('XBT.csv', index_col = 0)
df1 = df1.drop(['ADJUSTED','VOLUME'], axis = 1)
df1.head()

                        OPEN        HIGH         LOW       CLOSE
Date                
2016-09-25 22:00:00 600.1650    600.2850    599.3190    599.4575
2016-09-25 22:01:00 599.4550    600.1605    599.2980    599.5125
2016-09-25 22:02:00 599.5101    601.0850    599.2945    600.1450
2016-09-25 22:03:00 600.2950    600.6150    599.3290    599.3350
2016-09-25 22:04:00 599.3350    600.1400    599.3350    599.6972

此外,我计算了滚动的zscore,阈值和累积回报。

window = 300 #HYPERPARAMETER 5 hours lookback
df1['zScore'] = (df1['CLOSE']  - df1['CLOSE'] .rolling(60).mean())/df1['CLOSE'] .rolling(60).std()
df1 = df1.dropna()
entryZscore = 4 #HYPERPARMETER arbitrary naumber based on visual observations
exitZscore = 0 #HYPER PARAMETER
df1['cumret'] = df1['CLOSE'].divide(df1['CLOSE'].ix[0])


                        OPEN    HIGH         LOW        CLOSE    zScore cumret
Date                        
2016-09-25 22:59:00 599.8950    600.1239    599.5101    599.5376    -0.068662   1.000000
2016-09-25 23:00:00 599.5626    600.0743    599.5500    599.5775    0.001960    1.000067
2016-09-25 23:01:00 599.5750    600.0372    599.2800    599.9372    0.656922    1.000667
2016-09-25 23:02:00 600.3245    600.3245    598.5426    599.8000    0.416065    1.000438
2016-09-25 23:03:00 599.8000    599.9950    599.8000    599.9901    0.749235    1.000755

对我来说,问题是创建一个Regime值,它将指示我们是长(1),短(-1)还是中性(0)以便稍后创建策略返回值。我试图通过装箱进出点来做到这一点,但是从这里开始坚持下去。任何帮助将不胜感激!

df1['long entry'] = np.where((df1.zScore < - entryZscore) & ( df1.zScore.shift(1) > - entryZscore), 1, 0)
df1['long exit'] = np.where((df1.zScore > - exitZscore) & (df1.zScore.shift(1) < - exitZscore),1, 0) 
df1['short entry'] = np.where((df1.zScore >  entryZscore) & ( df1.zScore.shift(1) <  entryZscore),1, 0) 
df1['short exit'] = np.where((df1.zScore < exitZscore) & (df1.zScore.shift(1) > exitZscore),1, 0)
df1['close pct ch'] = df1['CLOSE'].pct_change()
df1 = df1.dropna()

1 个答案:

答案 0 :(得分:0)

要跟踪您的职位,您需要实施整个职位管理系统。你应该使用一个支持这些东西的平台,这样你就可以专注于交易算法。

我看过Quantopian的一家公司遇险研究: https://www.quantopian.com/posts/campbell-hilscher-szilagyi-chs-model-probability-of-corporate-failure笔记本可能已经过时而且不再运行,但整个想法都在那里。