我们的基础策略教程教授双均线策略。接下来我们将为大家展示相应策略的策略逻辑,策略信号以及策略表现等。
双均线策略
1.策略取数
In [15]:
data3 = get_price(['000905.SH'], '20150101', '20180101', '1d', ['close'])['000905.SH']
data3.rename(columns={'close':'price'},inplace=True)
data3.head()
Out[15]:
| price | |
|---|---|
| 2015-01-05 | 5417.017 |
| 2015-01-06 | 5479.864 |
| 2015-01-07 | 5488.242 |
| 2015-01-08 | 5444.037 |
| 2015-01-09 | 5409.058 |
In [16]:
data3['SMA_10'] = data3['price'].rolling(10).mean()
data3['SMA_60'] = data3['price'].rolling(60).mean()
In [17]:
data3[['price','SMA_10','SMA_60']].plot(figsize=(10,8))
Out[17]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f008a39a390>
2.策略信号
In [18]:
data3['position']=np.where(data3['SMA_10']>data3['SMA_60'],1,-1)
data3.dropna(inplace=True)
data3['position'].plot(ylim=[-1.1,1.1],figsize=(10,8))
Out[18]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f008a383fd0>
3.策略展示
In [19]:
data3['return']=np.log(data3['price']/data3['price'].shift(1))
data3['strategy'] = data3['position'].shift(1) * data3['return']
data3[['return','strategy']].cumsum().apply(np.exp).plot(figsize=(10, 6))
Out[19]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f008a29d438>
查看以上策略详情请到supermind量化交易官网查看:同花顺Supermind量化交易 经典量化策略基础-双均线策略