请 [注册] 或 [登录]  | 返回主站

量化交易吧 /  量化平台 帖子:3365810 新帖:17

事件驱动策略基础模板

萨达撒撒发表于:5 月 10 日 00:21回复(1)
  • 事件驱动策略是指一种以某种事件的发生为核心逻辑的投资策略,其中事件可以是业绩预告、分析师评级、去st、政策发布、川普发推等,也可以是股价突然上扬、K线形成某种形态、某指标达到某阈值等。
  • 而在编写事件驱动策略过程中,最基本最常见的流程是对事件发生后,相应的股票股价如何变化做出统计,从而对该事件是否可用做出初步判断。
  • 本文将以红三兵事件为例,分享一个事件驱动策略分析基础模板,用以统计股票触发事件后股价的变化情况。该框架附有注释,可以套用,希望能给大家带来一些帮助。
  • 重点在于分享的方法,红三兵策略本身价值可能并不大。

具体内容见分享的研究文件。

事件驱动策略基础模板¶

  • 事件驱动策略是指一种以某种事件的发生为核心逻辑的投资策略,其中事件可以是业绩预告、分析师评级、去st、政策发布、川普发推等,也可以是股价突然上扬、K线形成某种形态、某指标达到某阈值等。
  • 而在编写事件驱动策略过程中,最基本最常见的流程是对事件发生后,相应的股票股价如何变化做出统计,从而对该事件是否可用做出初步判断。
  • 本文将以红三兵事件为例,分享一个事件驱动策略分析基础框架,用以统计股票触发事件后股价的变化情况。该框架附有注释,可以套用,希望能给大家带来一些价值。
  • 重点在于分享的方法与框架,红三兵策略本身价值可能并不大。

导入库与函数¶

  • 以下这部分内容是加载各种用到的库,以及自定义的函数,需要先运行过一次才能正确运行后续的内容
from jqdata import *
import jqdata
import pandas as pd
import datetime 
import pickle

# 全局变量
# 交易日期列表
all_trade_days=[i.strftime('%Y-%m-%d') for i in list(get_all_trade_days())]

## 获取高开低收的涨跌幅持续上涨的股票
def get_pct_change_up(scu,n_days,date,field='close',up_limit=0.01):
    if len(scu)==0:
        return []
    df=get_price(security=scu,count=n_days+1,end_date=date,fields=[field])[field,:,:].pct_change().iloc[1:,:]
    if len(df)==0:
        df=df.T
        df['0000-00-00']=[]
        df=df.T
    return list(df[df>up_limit].T.dropna().index)

# 获取相对baseday的ix个交易日量价涨跌幅
def get_pct_change(scu,ix,date,fqy='1d',field='close'):
    if ix<=0:
        df=get_price(security=scu,count=-ix+2,end_date=date,fields=[field],frequency=fqy)[field,:,:].pct_change().iloc[1:,:]

    else:
        df=get_price(security=scu,count=ix+2,end_date=all_trade_days[all_trade_days.index(date)+ix],fields=[field],frequency=fqy)[field,:,:].pct_change().iloc[1:,:]
    if df.index.empty==True:
        df=df.T
        df['0000-00-00']=[]
        df=df.T
    return df
# 获取相对baseday的ix个交易日量价累计涨跌幅
def get_pct_change_accum(scu,ix,date,fqy='1d',field='close'):
    if ix<=0:
        df=get_price(security=scu,count=-ix+2,end_date=date,fields=[field],frequency=fqy)[field,:,:].iloc[1:,:]

    else:
        df=get_price(security=scu,count=ix+2,end_date=all_trade_days[all_trade_days.index(date)+ix],fields=[field],frequency=fqy)[field,:,:].iloc[1:,:]
    if df.index.empty==True:
        df=df.T
        df['0000-00-00']=[]
        df=df.T
        
    return df/df.iloc[0]
# 筛去新股
def new_stk_filter(scu,date,n_days):
    if len(scu)==0:
        return []
    df=get_all_securities(types=['stock'], date=date)[['start_date']]
    df=df.loc[scu]
    date=datetime.datetime.strptime(all_trade_days[all_trade_days.index(date)-n_days],"%Y-%m-%d").date()
    return list(df[df['start_date']< date].index)
# 筛去涨跌停股
def tenper_stk_filter(scu,date,ix=0):
    if len(scu)==0:
        return []
    df=get_pct_change(scu,0,date,fqy='1d',field='close').T
    df=df[df<0.099].dropna()
    df=df[df>-0.099].dropna()
    return list(df.index)
# 筛去收盘价低于开盘价的股票
def clop_stk_filter(scu,date,ix=1):
    if len(scu)==0:
        return []
    df=get_price(security=scu,count=ix,end_date=date,fields=['open','close'])
    df['cl-op']=df['close']/df['open']-1
    df=df['cl-op'].T
    df=df[df>0].dropna()
    return list(df.index)

获取事件数据¶

  • 获取事件数据没有通法。不同的事件数据来源可能不同,导致格式不同,甚至有些数据需要自己生成与整理,所以换成其他事件时,主要改的地方也就是这里。
  • 此处以红三兵k线形态为例,所以是自己获取的股票行情数据自己生成并整理的事件数据。
  • 红三兵的采用的定义是,股票最高价、最低价、开盘价、收盘价都连续3天上涨超过1%,并且连续3天收盘价大于开盘价,此外还剔除了近期新股与涨跌停股。
# 选择统计红三兵事件出现情况的起止时间
start='2018-01-09'
end='2018-10-29'


# 选择要统计的形态是红几兵
n_days=3
# 初始化存储统计结果的变量
totalLog=dict()
# 获取起止时间内的交易日
days=[i.strftime('%Y-%m-%d') for i in list(get_trade_days(start,end))]

# 循环统计起止时间内的交易日
for baseDay in days:
    # 打印当前计算到哪天,用来观察计算进度
    print(baseDay)
    # 获取全市场的股票
    scu = get_index_stocks('000001.XSHG',baseDay)+get_index_stocks('399106.XSHE',baseDay)
    stk_list=scu
    date=baseDay
    
    # 筛选出stk_list中收盘价连续3天上涨幅度大于1%的股票
    stk_list=get_pct_change_up(stk_list,n_days,date,'close',up_limit=0.01)
    # 进一步筛选出stk_list中开盘价连续3天上涨幅度大于1%的股票
    stk_list=get_pct_change_up(stk_list,n_days,date,'open',up_limit=0.01)
    # 进一步筛选出stk_list中最低价价续3天上涨幅度大于1%的股票
    stk_list=get_pct_change_up(stk_list,n_days,date,'low',up_limit=0.01)
    # 进一步筛选出stk_list中最高价连续3天上涨幅度大于1%的股票
    stk_list=get_pct_change_up(stk_list,n_days,date,'high',up_limit=0.01)
    # 进一步筛选出stk_list中连续3天收盘价大于开盘价的股票
    stk_list=clop_stk_filter(stk_list,date,3)
    # 进一步筛掉stk_list中最近20个交易日的新股
    stk_list=new_stk_filter(stk_list,date,20)
    # 进一步筛掉stk_list中当天涨跌停的股票
    stk_list=tenper_stk_filter(stk_list,date,0)
    # 记录触发红三兵事件的股票,存在totalLog中
    if stk_list!=[]:
            totalLog[baseDay]=stk_list
            
2018-01-09
2018-01-10
2018-01-11
2018-01-12
2018-01-15
2018-01-16
2018-01-17
2018-01-18
2018-01-19
2018-01-22
2018-01-23
2018-01-24
2018-01-25
2018-01-26
2018-01-29
2018-01-30
2018-01-31
2018-02-01
2018-02-02
2018-02-05
2018-02-06
2018-02-07
2018-02-08
2018-02-09
2018-02-12
2018-02-13
2018-02-14
2018-02-22
2018-02-23
2018-02-26
2018-02-27
2018-02-28
2018-03-01
2018-03-02
2018-03-05
2018-03-06
2018-03-07
2018-03-08
2018-03-09
2018-03-12
2018-03-13
2018-03-14
2018-03-15
2018-03-16
2018-03-19
2018-03-20
2018-03-21
2018-03-22
2018-03-23
2018-03-26
2018-03-27
2018-03-28
2018-03-29
2018-03-30
2018-04-02
2018-04-03
2018-04-04
2018-04-09
2018-04-10
2018-04-11
2018-04-12
2018-04-13
2018-04-16
2018-04-17
2018-04-18
2018-04-19
2018-04-20
2018-04-23
2018-04-24
2018-04-25
2018-04-26
2018-04-27
2018-05-02
2018-05-03
2018-05-04
2018-05-07
2018-05-08
2018-05-09
2018-05-10
2018-05-11
2018-05-14
2018-05-15
2018-05-16
2018-05-17
2018-05-18
2018-05-21
2018-05-22
2018-05-23
2018-05-24
2018-05-25
2018-05-28
2018-05-29
2018-05-30
2018-05-31
2018-06-01
2018-06-04
2018-06-05
2018-06-06
2018-06-07
2018-06-08
2018-06-11
2018-06-12
2018-06-13
2018-06-14
2018-06-15
2018-06-19
2018-06-20
2018-06-21
2018-06-22
2018-06-25
2018-06-26
2018-06-27
2018-06-28
2018-06-29
2018-07-02
2018-07-03
2018-07-04
2018-07-05
2018-07-06
2018-07-09
2018-07-10
2018-07-11
2018-07-12
2018-07-13
2018-07-16
2018-07-17
2018-07-18
2018-07-19
2018-07-20
2018-07-23
2018-07-24
2018-07-25
2018-07-26
2018-07-27
2018-07-30
2018-07-31
2018-08-01
2018-08-02
2018-08-03
2018-08-06
2018-08-07
2018-08-08
2018-08-09
2018-08-10
2018-08-13
2018-08-14
2018-08-15
2018-08-16
2018-08-17
2018-08-20
2018-08-21
2018-08-22
2018-08-23
2018-08-24
2018-08-27
2018-08-28
2018-08-29
2018-08-30
2018-08-31
2018-09-03
2018-09-04
2018-09-05
2018-09-06
2018-09-07
2018-09-10
2018-09-11
2018-09-12
2018-09-13
2018-09-14
2018-09-17
2018-09-18
2018-09-19
2018-09-20
2018-09-21
2018-09-25
2018-09-26
2018-09-27
2018-09-28
2018-10-08
2018-10-09
2018-10-10
2018-10-11
2018-10-12
2018-10-15
2018-10-16
2018-10-17
2018-10-18
2018-10-19
2018-10-22
2018-10-23
2018-10-24
2018-10-25
2018-10-26
2018-10-29
# 展示totalLog,即每天触发事件的股票
totalLog      
{'2018-01-09': ['603676.XSHG', '002422.XSHE'],
 '2018-01-10': ['600299.XSHG'],
 '2018-01-11': ['600690.XSHG', '000517.XSHE'],
 '2018-01-12': ['600515.XSHG', '600690.XSHG', '000517.XSHE'],
 '2018-01-15': ['600690.XSHG', '601998.XSHG'],
 '2018-01-16': ['603289.XSHG', '300715.XSHE'],
 '2018-01-17': ['600048.XSHG'],
 '2018-01-18': ['601398.XSHG', '601939.XSHG'],
 '2018-01-19': ['600506.XSHG',
  '601288.XSHG',
  '601939.XSHG',
  '603458.XSHG',
  '000711.XSHE',
  '002408.XSHE',
  '002559.XSHE',
  '002736.XSHE',
  '002822.XSHE',
  '002915.XSHE',
  '300635.XSHE'],
 '2018-01-22': ['600782.XSHG', '601225.XSHG', '000711.XSHE'],
 '2018-01-23': ['000801.XSHE', '002124.XSHE'],
 '2018-01-25': ['600107.XSHG', '600583.XSHG', '002617.XSHE', '300253.XSHE'],
 '2018-01-29': ['002707.XSHE'],
 '2018-01-30': ['600643.XSHG'],
 '2018-01-31': ['603877.XSHG'],
 '2018-02-12': ['002176.XSHE', '300323.XSHE'],
 '2018-02-13': ['600645.XSHG', '002248.XSHE'],
 '2018-02-14': ['600265.XSHG',
  '600525.XSHG',
  '600645.XSHG',
  '600841.XSHG',
  '600860.XSHG',
  '600986.XSHG',
  '603180.XSHG',
  '603369.XSHG',
  '603678.XSHG',
  '603738.XSHG',
  '603801.XSHG',
  '000611.XSHE',
  '002134.XSHE',
  '002414.XSHE',
  '002664.XSHE',
  '002798.XSHE',
  '002812.XSHE',
  '002829.XSHE',
  '300200.XSHE'],
 '2018-02-22': ['600196.XSHG',
  '600507.XSHG',
  '600525.XSHG',
  '600581.XSHG',
  '600727.XSHG',
  '600741.XSHG',
  '600742.XSHG',
  '600841.XSHG',
  '600986.XSHG',
  '603069.XSHG',
  '603369.XSHG',
  '603801.XSHG',
  '603833.XSHG',
  '000612.XSHE',
  '000636.XSHE',
  '000830.XSHE',
  '000963.XSHE',
  '002110.XSHE',
  '002190.XSHE',
  '002242.XSHE',
  '002377.XSHE'],
 '2018-02-23': ['600538.XSHG', '603228.XSHG'],
 '2018-02-26': ['600022.XSHG',
  '600247.XSHG',
  '600461.XSHG',
  '600538.XSHG',
  '600596.XSHG',
  '600604.XSHG',
  '600740.XSHG',
  '600789.XSHG',
  '600895.XSHG',
  '600985.XSHG',
  '601601.XSHG',
  '601678.XSHG',
  '603228.XSHG',
  '603507.XSHG',
  '603639.XSHG',
  '603848.XSHG',
  '000488.XSHE',
  '000507.XSHE',
  '000959.XSHE',
  '002009.XSHE',
  '002042.XSHE',
  '002078.XSHE',
  '002092.XSHE',
  '002396.XSHE',
  '002415.XSHE',
  '002799.XSHE',
  '002908.XSHE',
  '300077.XSHE',
  '300203.XSHE',
  '300308.XSHE',
  '300514.XSHE',
  '300602.XSHE',
  '300689.XSHE'],
 '2018-02-27': ['600118.XSHG',
  '600439.XSHG',
  '600523.XSHG',
  '600623.XSHG',
  '603006.XSHG',
  '603083.XSHG',
  '603177.XSHG',
  '603286.XSHG',
  '603329.XSHG',
  '603676.XSHG',
  '603707.XSHG',
  '002054.XSHE',
  '002234.XSHE',
  '002544.XSHE',
  '002771.XSHE',
  '002799.XSHE',
  '002864.XSHE',
  '002879.XSHE',
  '002893.XSHE',
  '300323.XSHE',
  '300394.XSHE',
  '300461.XSHE',
  '300664.XSHE',
  '300688.XSHE',
  '300691.XSHE',
  '300692.XSHE',
  '300696.XSHE',
  '300722.XSHE',
  '300726.XSHE'],
 '2018-02-28': ['600416.XSHG',
  '600732.XSHG',
  '002222.XSHE',
  '002448.XSHE',
  '300152.XSHE',
  '300306.XSHE',
  '300663.XSHE',
  '300687.XSHE'],
 '2018-03-01': ['600175.XSHG',
  '600732.XSHG',
  '000633.XSHE',
  '000738.XSHE',
  '000821.XSHE',
  '002120.XSHE',
  '002439.XSHE',
  '002552.XSHE',
  '300034.XSHE',
  '300378.XSHE',
  '300427.XSHE',
  '300554.XSHE',
  '300687.XSHE'],
 '2018-03-02': ['000613.XSHE', '300687.XSHE'],
 '2018-03-05': ['600843.XSHG', '000837.XSHE', '002680.XSHE'],
 '2018-03-06': ['603081.XSHG',
  '002680.XSHE',
  '002850.XSHE',
  '002925.XSHE',
  '300462.XSHE'],
 '2018-03-07': ['600436.XSHG', '300688.XSHE'],
 '2018-03-08': ['600436.XSHG',
  '603477.XSHG',
  '603676.XSHG',
  '000790.XSHE',
  '002422.XSHE',
  '002893.XSHE',
  '002923.XSHE',
  '300347.XSHE',
  '300559.XSHE',
  '300645.XSHE',
  '300688.XSHE'],
 '2018-03-09': ['603616.XSHG', '000020.XSHE', '002863.XSHE', '300495.XSHE'],
 '2018-03-12': ['600146.XSHG',
  '603208.XSHG',
  '603677.XSHG',
  '603683.XSHG',
  '002585.XSHE',
  '300551.XSHE',
  '300602.XSHE',
  '300690.XSHE',
  '300706.XSHE',
  '300707.XSHE'],
 '2018-03-13': ['600060.XSHG',
  '600139.XSHG',
  '600303.XSHG',
  '600346.XSHG',
  '000628.XSHE',
  '000806.XSHE',
  '002200.XSHE',
  '002503.XSHE',
  '002759.XSHE',
  '300023.XSHE',
  '300167.XSHE',
  '300221.XSHE',
  '300486.XSHE',
  '300572.XSHE'],
 '2018-03-14': ['000546.XSHE', '000585.XSHE'],
 '2018-03-15': ['002742.XSHE'],
 '2018-03-16': ['000953.XSHE'],
 '2018-03-19': ['002208.XSHE', '300497.XSHE'],
 '2018-03-21': ['300669.XSHE'],
 '2018-03-23': ['600662.XSHG'],
 '2018-03-26': ['002265.XSHE'],
 '2018-03-28': ['002025.XSHE', '300600.XSHE'],
 '2018-03-29': ['600587.XSHG',
  '600860.XSHG',
  '603139.XSHG',
  '603189.XSHG',
  '603607.XSHG',
  '000736.XSHE',
  '000938.XSHE',
  '002544.XSHE',
  '002802.XSHE',
  '002843.XSHE',
  '002846.XSHE',
  '300042.XSHE',
  '300249.XSHE',
  '300297.XSHE',
  '300333.XSHE',
  '300350.XSHE',
  '300453.XSHE',
  '300550.XSHE',
  '300553.XSHE',
  '300636.XSHE'],
 '2018-03-30': ['600860.XSHG',
  '603189.XSHG',
  '603266.XSHG',
  '000938.XSHE',
  '002802.XSHE'],
 '2018-04-02': ['600109.XSHG',
  '600171.XSHG',
  '600571.XSHG',
  '600602.XSHG',
  '603196.XSHG',
  '603277.XSHG',
  '603528.XSHG',
  '603819.XSHG',
  '000605.XSHE',
  '000676.XSHE',
  '002279.XSHE',
  '002421.XSHE',
  '002886.XSHE',
  '002913.XSHE',
  '002916.XSHE',
  '300024.XSHE',
  '300033.XSHE',
  '300047.XSHE',
  '300085.XSHE',
  '300139.XSHE',
  '300263.XSHE',
  '300359.XSHE',
  '300422.XSHE',
  '300537.XSHE',
  '300566.XSHE',
  '300580.XSHE',
  '300591.XSHE'],
 '2018-04-03': ['600211.XSHG',
  '603305.XSHG',
  '603936.XSHG',
  '000692.XSHE',
  '002696.XSHE',
  '002912.XSHE',
  '300085.XSHE',
  '300507.XSHE',
  '300603.XSHE'],
 '2018-04-04': ['600764.XSHG', '002910.XSHE'],
 '2018-04-09': ['603985.XSHG', '002910.XSHE'],
 '2018-04-10': ['603985.XSHG'],
 '2018-04-11': ['603985.XSHG'],
 '2018-04-12': ['601019.XSHG',
  '603637.XSHG',
  '002670.XSHE',
  '002840.XSHE',
  '300551.XSHE'],
 '2018-04-13': ['603757.XSHG', '002010.XSHE', '300525.XSHE'],
 '2018-04-16': ['603677.XSHG', '603757.XSHG'],
 '2018-04-18': ['300311.XSHE'],
 '2018-04-23': ['600680.XSHG', '603958.XSHG'],
 '2018-04-25': ['300122.XSHE'],
 '2018-04-26': ['002234.XSHE', '002691.XSHE', '300357.XSHE'],
 '2018-04-27': ['000650.XSHE', '002458.XSHE', '002691.XSHE', '300444.XSHE'],
 '2018-05-02': ['000650.XSHE', '002382.XSHE', '002563.XSHE', '002691.XSHE'],
 '2018-05-03': ['603589.XSHG', '002691.XSHE'],
 '2018-05-04': ['600027.XSHG',
  '603088.XSHG',
  '603589.XSHG',
  '603816.XSHG',
  '000703.XSHE',
  '002269.XSHE',
  '002899.XSHE',
  '300319.XSHE',
  '300519.XSHE'],
 '2018-05-07': ['600130.XSHG',
  '600132.XSHG',
  '600426.XSHG',
  '600859.XSHG',
  '603589.XSHG',
  '000703.XSHE',
  '000822.XSHE',
  '002042.XSHE',
  '002050.XSHE',
  '002304.XSHE',
  '002899.XSHE',
  '300121.XSHE',
  '300568.XSHE'],
 '2018-05-08': ['600114.XSHG',
  '600130.XSHG',
  '600588.XSHG',
  '600645.XSHG',
  '600655.XSHG',
  '600702.XSHG',
  '600872.XSHG',
  '603159.XSHG',
  '603369.XSHG',
  '603877.XSHG',
  '000501.XSHE',
  '000739.XSHE',
  '000822.XSHE',
  '000830.XSHE',
  '002050.XSHE',
  '002411.XSHE',
  '002853.XSHE',
  '300453.XSHE',
  '300489.XSHE',
  '300535.XSHE',
  '300568.XSHE',
  '300571.XSHE'],
 '2018-05-09': ['601918.XSHG',
  '603028.XSHG',
  '603159.XSHG',
  '603577.XSHG',
  '000720.XSHE',
  '002341.XSHE',
  '002411.XSHE',
  '300062.XSHE',
  '300402.XSHE',
  '300489.XSHE',
  '300490.XSHE'],
 '2018-05-10': ['600239.XSHG',
  '600290.XSHG',
  '603085.XSHG',
  '603189.XSHG',
  '603286.XSHG',
  '603626.XSHG',
  '603996.XSHG',
  '000502.XSHE',
  '002395.XSHE',
  '002715.XSHE',
  '002805.XSHE',
  '002824.XSHE',
  '300062.XSHE',
  '300192.XSHE',
  '300257.XSHE',
  '300560.XSHE'],
 '2018-05-11': ['603928.XSHG', '300192.XSHE'],
 '2018-05-14': ['600884.XSHG', '300037.XSHE'],
 '2018-05-16': ['600298.XSHG', '600623.XSHG'],
 '2018-05-17': ['002890.XSHE'],
 '2018-05-18': ['600075.XSHG', '600409.XSHG', '002890.XSHE'],
 '2018-05-21': ['600209.XSHG',
  '600409.XSHG',
  '000968.XSHE',
  '002087.XSHE',
  '002353.XSHE'],
 '2018-05-22': ['600209.XSHG',
  '603912.XSHG',
  '002306.XSHE',
  '300517.XSHE',
  '300720.XSHE'],
 '2018-05-23': ['600209.XSHG', '002568.XSHE', '002903.XSHE', '300703.XSHE'],
 '2018-05-24': ['603615.XSHG', '002751.XSHE', '300673.XSHE', '300739.XSHE'],
 '2018-05-25': ['603078.XSHG', '300584.XSHE'],
 '2018-05-28': ['603886.XSHG', '300584.XSHE'],
 '2018-05-29': ['002178.XSHE'],
 '2018-06-01': ['601069.XSHG'],
 '2018-06-04': ['002032.XSHE'],
 '2018-06-05': ['600983.XSHG', '000910.XSHE'],
 '2018-06-06': ['002897.XSHE'],
 '2018-06-07': ['002913.XSHE', '300505.XSHE', '300730.XSHE'],
 '2018-06-08': ['002364.XSHE'],
 '2018-06-13': ['002885.XSHE'],
 '2018-06-20': ['600121.XSHG'],
 '2018-06-25': ['600101.XSHG', '600252.XSHG', '300686.XSHE'],
 '2018-06-26': ['600319.XSHG'],
 '2018-06-27': ['600321.XSHG',
  '603499.XSHG',
  '603977.XSHG',
  '002799.XSHE',
  '002806.XSHE',
  '300689.XSHE'],
 '2018-06-29': ['600081.XSHG',
  '600228.XSHG',
  '300476.XSHE',
  '300604.XSHE',
  '300739.XSHE'],
 '2018-07-02': ['600081.XSHG',
  '600228.XSHG',
  '002199.XSHE',
  '300383.XSHE',
  '300476.XSHE'],
 '2018-07-03': ['600081.XSHG'],
 '2018-07-04': ['000738.XSHE', '002013.XSHE', '002228.XSHE', '002465.XSHE'],
 '2018-07-06': ['002231.XSHE'],
 '2018-07-10': ['000046.XSHE', '000401.XSHE', '002278.XSHE'],
 '2018-07-11': ['603596.XSHG',
  '603876.XSHG',
  '603993.XSHG',
  '002012.XSHE',
  '002302.XSHE',
  '002714.XSHE'],
 '2018-07-12': ['603866.XSHG',
  '002160.XSHE',
  '002427.XSHE',
  '002737.XSHE',
  '300012.XSHE'],
 '2018-07-13': ['600661.XSHG'],
 '2018-07-16': ['600305.XSHG',
  '600572.XSHG',
  '601888.XSHG',
  '603429.XSHG',
  '000636.XSHE',
  '000650.XSHE',
  '000860.XSHE',
  '000918.XSHE',
  '000963.XSHE',
  '002112.XSHE',
  '002383.XSHE',
  '002633.XSHE',
  '002653.XSHE',
  '300015.XSHE',
  '300144.XSHE',
  '300572.XSHE',
  '300630.XSHE',
  '300637.XSHE',
  '300654.XSHE'],
 '2018-07-17': ['600552.XSHG',
  '600635.XSHG',
  '603496.XSHG',
  '000153.XSHE',
  '002058.XSHE',
  '002672.XSHE',
  '002770.XSHE',
  '002882.XSHE',
  '300062.XSHE',
  '300637.XSHE'],
 '2018-07-18': ['600234.XSHG'],
 '2018-07-20': ['600011.XSHG'],
 '2018-07-23': ['600019.XSHG', '000046.XSHE'],
 '2018-07-24': ['600019.XSHG',
  '600297.XSHG',
  '600734.XSHG',
  '002168.XSHE',
  '002857.XSHE'],
 '2018-07-25': ['600119.XSHG',
  '600125.XSHG',
  '600331.XSHG',
  '600460.XSHG',
  '600531.XSHG',
  '600620.XSHG',
  '600710.XSHG',
  '600727.XSHG',
  '600794.XSHG',
  '600822.XSHG',
  '600880.XSHG',
  '601388.XSHG',
  '603128.XSHG',
  '603160.XSHG',
  '603868.XSHG',
  '000401.XSHE',
  '000428.XSHE',
  '000554.XSHE',
  '000609.XSHE',
  '000622.XSHE',
  '000809.XSHE',
  '000862.XSHE',
  '000925.XSHE',
  '002373.XSHE',
  '002611.XSHE',
  '300051.XSHE',
  '300079.XSHE',
  '300083.XSHE',
  '300106.XSHE',
  '300160.XSHE'],
 '2018-07-26': ['600063.XSHG',
  '600169.XSHG',
  '600331.XSHG',
  '600393.XSHG',
  '600740.XSHG',
  '600880.XSHG',
  '601218.XSHG',
  '601588.XSHG',
  '000048.XSHE',
  '000428.XSHE',
  '000521.XSHE',
  '000622.XSHE',
  '000659.XSHE',
  '000958.XSHE',
  '002002.XSHE',
  '002122.XSHE',
  '002170.XSHE',
  '002312.XSHE',
  '002785.XSHE',
  '300063.XSHE',
  '300185.XSHE',
  '300269.XSHE',
  '300432.XSHE'],
 '2018-07-27': ['600365.XSHG', '000048.XSHE', '300116.XSHE'],
 '2018-08-01': ['000409.XSHE'],
 '2018-08-02': ['600608.XSHG'],
 '2018-08-03': ['600608.XSHG', '300517.XSHE'],
 '2018-08-07': ['000004.XSHE'],
 '2018-08-08': ['600277.XSHG', '600408.XSHG'],
 '2018-08-09': ['601952.XSHG', '002888.XSHE'],
 '2018-08-10': ['601952.XSHG',
  '002281.XSHE',
  '002368.XSHE',
  '002492.XSHE',
  '300329.XSHE'],
 '2018-08-13': ['002281.XSHE'],
 '2018-08-14': ['600273.XSHG',
  '600610.XSHG',
  '601228.XSHG',
  '603086.XSHG',
  '000022.XSHE',
  '000507.XSHE',
  '000703.XSHE',
  '300630.XSHE'],
 '2018-08-16': ['300561.XSHE', '300708.XSHE'],
 '2018-08-22': ['600789.XSHG'],
 '2018-08-23': ['600198.XSHG',
  '000778.XSHE',
  '002634.XSHE',
  '002767.XSHE',
  '300086.XSHE',
  '300107.XSHE'],
 '2018-08-24': ['000913.XSHE', '002427.XSHE'],
 '2018-08-27': ['000893.XSHE',
  '002188.XSHE',
  '002427.XSHE',
  '002745.XSHE',
  '300160.XSHE'],
 '2018-08-28': ['000811.XSHE',
  '000893.XSHE',
  '002188.XSHE',
  '002191.XSHE',
  '300482.XSHE',
  '300559.XSHE'],
 '2018-08-29': ['600612.XSHG', '603886.XSHG', '300219.XSHE', '300481.XSHE'],
 '2018-08-30': ['300219.XSHE'],
 '2018-09-14': ['000338.XSHE', '300037.XSHE'],
 '2018-09-19': ['002304.XSHE', '002557.XSHE'],
 '2018-09-20': ['600267.XSHG'],
 '2018-09-21': ['600225.XSHG',
  '600470.XSHG',
  '600754.XSHG',
  '603012.XSHG',
  '000017.XSHE',
  '000897.XSHE',
  '002213.XSHE',
  '002508.XSHE',
  '002586.XSHE',
  '002647.XSHE',
  '002677.XSHE',
  '300025.XSHE',
  '300379.XSHE'],
 '2018-09-25': ['002647.XSHE'],
 '2018-09-26': ['600426.XSHG', '002468.XSHE'],
 '2018-09-27': ['600161.XSHG', '002445.XSHE', '300601.XSHE'],
 '2018-09-28': ['002627.XSHE'],
 '2018-10-08': ['002219.XSHE'],
 '2018-10-10': ['600532.XSHG'],
 '2018-10-16': ['603718.XSHG'],
 '2018-10-17': ['603926.XSHG'],
 '2018-10-22': ['600271.XSHG',
  '600729.XSHG',
  '300136.XSHE',
  '300227.XSHE',
  '300413.XSHE'],
 '2018-10-23': ['603718.XSHG', '000546.XSHE'],
 '2018-10-24': ['600767.XSHG',
  '600774.XSHG',
  '603032.XSHG',
  '000918.XSHE',
  '002290.XSHE',
  '002445.XSHE',
  '300013.XSHE',
  '300104.XSHE',
  '300580.XSHE',
  '300692.XSHE'],
 '2018-10-29': ['600807.XSHG']}

统计事件触发后股价涨跌幅变化情况¶

  • 不管你的事件是什么,只要你的数据格式处理成和前文的totalLog一样就可以套用,即是dict类型,其中,key为日期,类型为字符串,value为股票代码,类型为聚宽平台API使用的股票代码。
# 把事件数据赋值给stk
stk=totalLog
# 初始化存储统计结果的变量
totalSum=pd.DataFrame()
# 选择基准指数
# 如果选择了基准指数则统计的股票涨跌幅是减去了该指数涨跌幅的相对涨跌幅
# 如果想看单纯股票涨跌幅,可以赋值为空字符串''
baseIndex='000300.XSHG'
# 选择要统计触发事件后多少天的股价变化情况
n_limt=25
# 从事件触发前多少天开始统计,0即当天
ahead_days=0

# 循环触发了事件的每一天
for i in stk.keys():
#     print(i)
    # 获得当天触发事件的股票
    stk_list=stk[i]
    # 如果选择了基准指数
    if baseIndex!='':
        # 获取当天触发事件后股票相对基准指数的涨跌幅
        t=get_pct_change_accum([baseIndex]+stk_list,n_limt,all_trade_days[all_trade_days.index(i)-ahead_days],fqy='1d')
        t.reset_index(drop=True,inplace=True)
        t=t.sub(t[baseIndex],axis=0)
        del t[baseIndex]
    # 如果没选基准指数
    else:
        # 获取当天触发事件后股票的涨跌幅
        t=get_pct_change_accum(stk_list,n_limt,all_trade_days[all_trade_days.index(i)-ahead_days],fqy='1d')
        t.reset_index(drop=True,inplace=True)
    # 记录当天触发事件后股票的涨跌幅,存储在totalSum
    totalSum=pd.concat([totalSum,t],axis=1)
# 展示统计结果
# 格式为dataframe,字段为股票代码,索引为事件后第几天,当天为0,值为涨跌幅
totalSum
.dataframe thead tr:only-child th { text-align: right; } .dataframe thead th { text-align: left; } .dataframe tbody tr th { vertical-align: top; }
002304.XSHE 002557.XSHE 600081.XSHG 600019.XSHG 000046.XSHE 600011.XSHG 600063.XSHG 600169.XSHG 600331.XSHG 600393.XSHG ... 600612.XSHG 603886.XSHG 300219.XSHE 300481.XSHE 000811.XSHE 000893.XSHE 002188.XSHE 002191.XSHE 300482.XSHE 300559.XSHE
0 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ... 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
1 -0.006064 0.028158 -0.015710 0.028138 -0.026136 -0.003030 -0.023434 -0.007471 -0.012876 -0.022943 ... 0.011352 0.005995 0.026445 -0.001718 -0.026710 -0.001076 -0.009477 -0.001953 -0.006774 0.018371
2 0.027143 -0.002140 -0.023991 0.039041 -0.076332 -0.063694 -0.045917 0.025518 -0.024835 -0.030267 ... 0.004579 0.001370 0.031344 -0.014706 -0.050825 0.001744 -0.017910 0.001339 -0.024976 -0.004488
3 0.011853 0.008997 -0.036093 0.022694 -0.063095 -0.057458 -0.032805 -0.002556 -0.032414 -0.018912 ... 0.008907 0.025842 0.005148 0.009364 -0.038268 0.011700 -0.050767 -0.004496 0.002869 0.002787
4 0.047968 0.014538 -0.052784 0.042847 -0.120419 -0.050679 -0.040616 -0.002215 -0.046789 -0.065506 ... 0.014865 0.019579 -0.014264 0.006694 -0.061410 0.017967 -0.065906 0.009948 -0.005310 0.050588
5 0.052681 0.031759 -0.069762 0.075153 -0.074686 -0.051481 -0.039764 0.015492 -0.055998 -0.049935 ... 0.017942 -0.005821 0.012140 0.001722 -0.066108 -0.009637 -0.072891 0.024953 -0.004643 0.029829
6 0.045675 0.038578 -0.117561 0.064642 -0.047559 -0.024210 -0.027478 0.011706 -0.043677 -0.043253 ... 0.051406 -0.014457 0.004588 0.007255 -0.056365 0.012241 -0.075114 0.032388 0.002505 0.041265
7 0.030647 0.096970 -0.102798 0.044230 -0.029095 -0.042806 -0.012138 0.019679 -0.045496 -0.037416 ... 0.053759 0.023202 0.007215 0.012653 -0.038317 0.025149 -0.083612 0.042758 -0.002719 0.077503
8 0.031737 0.118224 -0.118925 0.056128 -0.014748 -0.016310 0.005810 0.062958 -0.024585 -0.049344 ... 0.038333 0.045652 0.002300 -0.015577 -0.044578 0.010656 -0.079867 0.040798 0.000028 -0.009473
9 -0.029628 0.120988 -0.096534 0.073143 0.011308 0.048939 0.021182 0.066611 -0.012638 -0.040014 ... 0.089670 0.032816 0.003032 0.039812 -0.065136 -0.010887 -0.076658 0.052408 -0.022840 0.028231
10 -0.003252 0.143579 -0.105313 0.071610 -0.045692 0.015135 0.004626 0.050947 -0.008717 -0.060470 ... 0.062665 0.018080 0.002598 0.049617 -0.053816 -0.014239 -0.072239 0.039846 0.001566 0.049355
11 0.019677 0.184402 -0.124539 0.090950 -0.008290 0.008029 0.016349 0.041016 -0.014260 -0.059567 ... 0.094774 0.027486 0.002376 0.044293 -0.016580 0.002439 -0.060322 0.073753 -0.005517 0.024744
12 0.006795 0.150983 -0.117307 0.118593 0.012993 -0.026994 0.037801 0.045167 -0.027232 -0.067500 ... 0.109193 0.015212 -0.002248 0.037785 -0.026741 -0.005184 -0.073179 0.068353 -0.025885 0.055540
13 0.025664 0.121869 -0.125342 0.096269 -0.026683 -0.042043 0.028889 0.042284 -0.018878 -0.071635 ... 0.120461 0.011622 -0.003248 0.015427 -0.045639 0.041416 -0.082888 0.031016 -0.054160 0.021342
14 0.015113 0.100432 -0.121356 0.084365 -0.030274 -0.024995 0.041394 0.049545 -0.023390 -0.060833 ... 0.110166 -0.007427 -0.062217 0.002069 -0.048143 0.103109 -0.101606 0.047900 -0.035857 0.003771
15 0.008198 0.110253 -0.106889 0.103198 -0.055436 -0.089639 0.031866 0.046045 -0.032776 -0.062563 ... 0.101539 0.009553 -0.078921 0.003991 -0.053679 0.073984 -0.104406 0.027737 -0.078646 0.016415
16 0.035389 0.146545 -0.111177 0.077579 -0.049026 -0.066304 0.027887 0.043742 -0.033153 -0.070389 ... 0.080348 -0.008241 -0.077229 -0.007510 -0.056744 0.112084 -0.117067 0.019838 -0.080249 0.022437
17 0.042294 0.150609 -0.111720 0.072416 -0.029004 -0.061313 0.027626 0.044819 -0.033520 -0.102178 ... 0.069097 -0.061965 -0.101874 -0.043649 -0.061811 0.166075 -0.110985 0.000291 -0.080409 -0.008010
18 -0.030474 0.124423 -0.106406 0.058404 -0.046673 -0.068756 0.038511 0.039741 -0.040043 -0.115955 ... 0.066779 -0.092119 -0.109777 -0.034586 -0.075972 0.161939 -0.135111 -0.014940 -0.087526 -0.029143
19 -0.079755 0.117993 -0.088254 0.074211 -0.086097 -0.056737 0.043799 0.041122 -0.041604 -0.119730 ... 0.099785 -0.081885 -0.115843 -0.051333 -0.049671 0.229341 -0.139562 -0.020200 -0.086155 -0.011734
20 -0.107425 0.116148 -0.111717 0.094100 -0.105568 -0.029219 0.040383 0.033800 -0.041596 -0.117104 ... 0.082223 -0.108667 -0.119796 -0.086327 -0.064521 0.279244 -0.145182 -0.034782 -0.086676 -0.038074
21 -0.145215 0.119893 -0.092922 0.077253 -0.109219 -0.011931 0.025043 0.032301 -0.043095 -0.091412 ... 0.094568 -0.126365 -0.129213 -0.082107 -0.062426 0.288334 -0.168122 -0.048625 -0.086064 -0.130826
22 -0.197295 0.082351 -0.098660 0.080111 -0.102449 -0.031488 0.026291 0.021047 -0.048945 -0.093238 ... 0.112288 -0.117969 -0.094505 -0.080693 -0.044008 0.305880 -0.173104 -0.053046 -0.100973 -0.117008
23 -0.220917 0.173337 -0.096861 0.051013 -0.105875 -0.064399 0.017720 0.022856 -0.030012 -0.088407 ... 0.118777 -0.112834 -0.112814 -0.072724 -0.036961 0.415385 -0.153847 -0.036911 -0.117282 -0.080917
24 -0.203669 0.176407 -0.071823 0.043397 -0.126440 -0.023215 0.031947 -0.004548 -0.036440 -0.093624 ... 0.152385 -0.107940 -0.106729 -0.048679 0.002076 0.413497 -0.147806 -0.038642 -0.109544 -0.124339
25 -0.161141 0.170648 -0.089873 0.035025 -0.140684 -0.033661 0.028139 0.001579 -0.046955 -0.086612 ... 0.127982 -0.144348 -0.128089 -0.094044 -0.009274 0.347055 -0.143026 -0.055604 -0.100087 -0.126086

26 rows × 670 columns

事件触发后股价平均涨跌幅走势图¶

  • 为了能快速对事件触发后股价走势有个判断,我们画出事件触发后股价平均涨跌幅走势图。
  • 以红三兵事件为例,可见在18年,平均来看,第25天会有1%左右的超额收益。(该结果很可能因样本不够多而不可靠)
# 设置作图大小
figsize=(16,5)
# 生成事件触发后平均股价走势图
totalSum.T.mean().plot(figsize=figsize).grid()

转成策略¶

  • 为了把统计后的发现转成一个事件驱动策略,我们需要确定买卖方法。
  • 买卖方法没有通法,此处采用简单的等资金量的买入方法,与到期抛售的卖出方法,以红三兵为例具体如下。首先定的是,事件触发后第1天买入,第24天卖出。然后,因为事件是不断触发的,所以我们要统计出在24天的时间窗口内最多会有多少个股票触发事件,下文统计出是187个,所以我们把总资金分为187份,每次触发事件,拿出一份去买入该股票。

统计一段时间窗口内触发事件的最大次数¶

# 把事件数据赋值给stk
stk=totalLog
# 选择时间窗口为几天
period=24
# 初始化存储每天触发事件次数的变量
stk_num_days=[]
# 初始化存储时间窗口内触发事件的最大次数的变量
max_stk_num=0

# 循环统计起止时间内的交易日
for i in days:
    # 如果当天有事件触发
    if stk.has_key(i):
        t=len(stk[i])
    # 如果当天没有事件触发
    else:
        t=0
    # 记录当天事件触发数
    stk_num_days+=[t]
    # 计算当前时间窗口内触发事件总数
    t=sum(stk_num_days[-period:])
    # 更新时间窗口内触发事件最大总数
    max_stk_num=max(max_stk_num,t)
# 展示结果
max_stk_num
187
# 展示每天触发事件次数走势图
plt.figure(figsize=figsize)
plot(stk_num_days)
[<matplotlib.lines.Line2D at 0x7ff2081cbed0>]

策略回测结果¶

  • 策略18年的回测结果如下图。

事件驱动.png

问题与思考¶

  • 策略回测虽然跑赢了大盘但是似乎没有盈利能力。
  • 而且用2018年数据统计出的参数做2018年的回测,有过拟合问题。
  • 事件触发后股价平均涨跌幅走势图统计上多大程度上可信?可能只是随机波动的结果。
  • 事件触发后股价平均涨跌幅走势图如果可信,我们其实买点在事件触发后第4天买入,第25天卖出更好。
  • 由每天触发事件次数走势图可以看出,每天触发事件次数分布很不平均,采取最大值来平均分配资金降低了资金使用效率。
 

全部回复

0/140

达人推荐

量化课程

    移动端课程