我们的基础策略教程首先教授动量策略与均值回归策略。接下来我们将为大家展示相应策略的策略逻辑,策略信号以及策略表现等
基础策略教程
作者:邱吉尔
一、动量策略
1.导入库包
In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
import pandas as pd
plt.style.use('seaborn')
2.获取数据
In [2]:
data = get_price(['000001.SZ'], '20150101', '20170101', '1d', ['close'])['000001.SZ']
data.rename(columns={'close':'price'},inplace=True)
data.head()
Out[2]:
price | |
---|---|
2015-01-05 | 10.88 |
2015-01-06 | 10.72 |
2015-01-07 | 10.51 |
2015-01-08 | 10.14 |
2015-01-09 | 10.22 |
3.策略逻辑
In [3]:
data['return']=np.log(data['price']/data['price'].shift(1))
data['position']=np.sign(data['return'])
data['strategy']=data['position'].shift(1)*data['return']
data.head()
Out[3]:
price | return | position | strategy | |
---|---|---|---|---|
2015-01-05 | 10.88 | NaN | NaN | NaN |
2015-01-06 | 10.72 | -0.014815 | -1.0 | NaN |
2015-01-07 | 10.51 | -0.019784 | -1.0 | 0.019784 |
2015-01-08 | 10.14 | -0.035839 | -1.0 | 0.035839 |
2015-01-09 | 10.22 | 0.007859 | 1.0 | -0.007859 |
4.策略表现可视化
In [4]:
data[['return','strategy']].cumsum().apply(np.exp).plot(figsize=(10,8))
Out[4]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f003b733d30>
5.基于10天的收益的动量信号
In [5]:
data['position_10']=np.sign(data['return'].rolling(10).mean())
data['strategy_10']=data['position_10'].shift(1)*data['return']
data[['return','strategy_10']].cumsum().apply(np.exp).plot(figsize=(10,8))
Out[5]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f008a3f0320>
6.基于不同天数的动量策略展示
In [6]:
data = get_price(['000001.SZ'], '20150101', '20170101', '1d', ['close'])['000001.SZ']
data.rename(columns={'close':'price'},inplace=True)
data['return']=np.log(data['price']/data['price'].shift(1))
data['return_cum'] = (data['return']+1).cumprod()
price_plot = ['return_cum']
for days in [5,10,20,50]:
price_plot.append('sty_cumr_%dd' % days)
data['position_%dd' % days] = np.sign(data['return'].rolling(days).mean())
data['strategy_%dd' % days] = data['position_%dd' % days].shift(1) * data['return']
data['sty_cumr_%dd' % days] = (data['strategy_%dd' % days]+1).cumprod()
data.dropna(inplace=True)
data.head()
Out[6]:
price | return | return_cum | position_5d | strategy_5d | sty_cumr_5d | position_10d | strategy_10d | sty_cumr_10d | position_20d | strategy_20d | sty_cumr_20d | position_50d | strategy_50d | sty_cumr_50d | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
2015-03-24 | 10.42 | -0.004787 | 0.942549 | -1.0 | -0.004787 | 0.850773 | 1.0 | -0.004787 | 0.953402 | 1.0 | -0.004787 | 1.009770 | -1.0 | 0.004787 | 1.004787 |
2015-03-25 | 10.11 | -0.030202 | 0.914082 | -1.0 | 0.030202 | 0.876468 | 1.0 | -0.030202 | 0.924607 | 1.0 | -0.030202 | 0.979273 | -1.0 | 0.030202 | 1.035134 |
2015-03-26 | 10.25 | 0.013753 | 0.926653 | -1.0 | -0.013753 | 0.864414 | 1.0 | 0.013753 | 0.937323 | 1.0 | 0.013753 | 0.992741 | 1.0 | -0.013753 | 1.020898 |
2015-03-27 | 10.22 | -0.002931 | 0.923937 | -1.0 | 0.002931 | 0.866948 | 1.0 | -0.002931 | 0.934576 | 1.0 | -0.002931 | 0.989831 | -1.0 | -0.002931 | 1.017905 |
2015-03-30 | 10.63 | 0.039334 | 0.960279 | 1.0 | -0.039334 | 0.832848 | 1.0 | 0.039334 | 0.971336 | 1.0 | 0.039334 | 1.028765 | 1.0 | -0.039334 | 0.977867 |
In [7]:
data[price_plot].plot(figsize=(10,8))
Out[7]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f008a428e80>
二、均值回归策略
1.获取数据
In [8]:
data2 = get_price(['000002.SZ'], '20160101', '20180101', '1d', ['close'])['000002.SZ']
data2.rename(columns={'close':'price'},inplace=True)
data2.head()
Out[8]:
price | |
---|---|
2016-01-04 | 22.92 |
2016-01-05 | 22.92 |
2016-01-06 | 22.92 |
2016-01-07 | 22.92 |
2016-01-08 | 22.92 |
2.策略逻辑
In [9]:
days=50
data2['return'] = np.log(data2['price'] / data2['price'].shift(1))
data2['price_sma_%dd'%days]=data2['price'].rolling(days).mean() #使用50天作为移动平均值
data2['distance']=data2['price']-data2['price_sma_%dd'%days]
In [10]:
threshold=data2['distance'].std() #标准差作通道
#print(threshold)
In [11]:
data2['distance'].plot(figsize=(8,6),legend=True)
plt.axhline(threshold*1, color='r')
plt.axhline(-threshold*1, color='r')
plt.axhline(0, color='r')
Out[11]:
<matplotlib.lines.Line2D at 0x7f008a42f400>
策略逻辑:
1.当距离高于一倍标准差,则做空
2.当距离低于负一倍标准差,则做多
3.当出现价格与移动平均价格交叉时,则仓位归0
In [12]:
data2['position'] = np.where(data2['distance'] > threshold, -1, np.nan)
data2['position'] = np.where(data2['distance'] < -threshold, 1, data2['position'])
data2['position'] = np.where(data2['distance'] *data2['distance'].shift(1) < 0, 0, data2['position'])
data2['position'] = data2['position'].ffill().fillna(0)
3.策略信号
In [13]:
data2['position'][days:].plot(ylim=[-1.1,1.1],figsize=(10,8))
Out[13]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f008a4bdba8>
4.策略展示
In [14]:
data2['strategy']=data2['position'].shift(1)*data2['return']
data2[['return','strategy']].cumsum().apply(np.exp).plot(figsize=(10,8))
Out[14]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f008a3d5898>
查看以上策略详细请 到 supermind量化交易官网查看:经典量化策略基础-动量策略与均值回归策略