我目前正在尝试实施单个资产回测,其中当 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()