同花顺Supermind量化交易 经典量化策略基础-动量策略与均值回归策略

我们的基础策略教程首先教授动量策略与均值回归策略。接下来我们将为大家展示相应策略的策略逻辑,策略信号以及策略表现等

基础策略教程

作者:邱吉尔

一、动量策略

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-0510.88
2015-01-0610.72
2015-01-0710.51
2015-01-0810.14
2015-01-0910.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]:

pricereturnpositionstrategy
2015-01-0510.88NaNNaNNaN
2015-01-0610.72-0.014815-1.0NaN
2015-01-0710.51-0.019784-1.00.019784
2015-01-0810.14-0.035839-1.00.035839
2015-01-0910.220.0078591.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]:

pricereturnreturn_cumposition_5dstrategy_5dsty_cumr_5dposition_10dstrategy_10dsty_cumr_10dposition_20dstrategy_20dsty_cumr_20dposition_50dstrategy_50dsty_cumr_50d
2015-03-2410.42-0.0047870.942549-1.0-0.0047870.8507731.0-0.0047870.9534021.0-0.0047871.009770-1.00.0047871.004787
2015-03-2510.11-0.0302020.914082-1.00.0302020.8764681.0-0.0302020.9246071.0-0.0302020.979273-1.00.0302021.035134
2015-03-2610.250.0137530.926653-1.0-0.0137530.8644141.00.0137530.9373231.00.0137530.9927411.0-0.0137531.020898
2015-03-2710.22-0.0029310.923937-1.00.0029310.8669481.0-0.0029310.9345761.0-0.0029310.989831-1.0-0.0029311.017905
2015-03-3010.630.0393340.9602791.0-0.0393340.8328481.00.0393340.9713361.00.0393341.0287651.0-0.0393340.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-0422.92
2016-01-0522.92
2016-01-0622.92
2016-01-0722.92
2016-01-0822.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量化交易官网查看:经典量化策略基础-动量策略与均值回归策略

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值