[关闭]
@Channelchan 2018-01-11T19:24:29.000000Z 字数 4400 阅读 25149

事件驱动

  1. 读取本地数据
  2. 添加金叉事件
  3. 计算binary事件
  4. 查看绩效

1_读取本地数据

  1. import numpy as np
  2. from jaqs.data import DataView
  3. from jaqs.research import SignalDigger
  4. import warnings
  5. warnings.filterwarnings("ignore")
  6. dataview_folder = 'JAQS_Data/hs300'
  7. dv = DataView()
  8. dv.load_dataview(dataview_folder)
D:\Anaconda3\lib\site-packages\statsmodels\compat\pandas.py:56: FutureWarning: The pandas.core.datetools module is deprecated and will be removed in a future version. Please use the pandas.tseries module instead.
  from pandas.core import datetools


Dataview loaded successfully.
  1. mask = dv.get_ts('mask_index_member')
  2. can_enter = dv.get_ts('can_enter')
  3. can_exit = dv.get_ts('can_exit')

2_添加金叉事件

  1. dv.remove_field('above')
  2. dv.remove_field('delay_above')
  3. dv.remove_field('sig_cross')
Field name [above] does not exist.
Field name [delay_above] does not exist.
Field name [sig_cross] does not exist.
  1. def add_event():
  2. dv.add_formula('above', ' Ts_Mean(close_adj, 10) >= Ts_Mean(close_adj, 40)', is_quarterly=False)
  3. dv.add_formula('delay_above', 'Delay(above, 1)', is_quarterly=False)
  4. dv.add_formula('sig_cross', 'above&&(!delay_above)', is_quarterly=False)
  5. add_event()

3_计算binary事件

  1. import matplotlib.pyplot as plt
  2. def cal_obj(signal, name, period):
  3. price = dv.get_ts('close_adj').loc[20160105:]
  4. # price_bench = dv.data_benchmark.loc[20160105:]
  5. # Step.4 analyze!
  6. obj = SignalDigger()
  7. df_all, df_events, df_stats = obj.create_binary_event_report(signal.loc[20160105:], price, mask.loc[20160105:], can_enter.loc[20160105:], can_exit.loc[20160105:],periods=[period,period*2, period*3])
  8. res = obj.create_full_report()
  9. print(df_stats)
  10. return obj
  11. def plot_pfm(signal, name, period=5):
  12. obj = cal_obj(signal, name, period)
  13. obj.fig_objs
  14. plt.show()
  15. def signal_data(signal, name, period=5):
  16. obj = cal_obj(signal, name, period)
  17. return obj.signal_data
  1. signal = dv.get_ts('sig_cross')

4_查看事件绩效

  1. 统计信息
  2. 回测绩效
  3. 事件表格
  1. #统计信息
  2. #回测绩效
  3. plot_pfm(signal,'cross_sig', period=4)
Nan Data Count (should be zero) : 0;  Percentage of effective data: 53%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 53%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 53%

       Calendar Distribution    (3696 occurance from 2016-01-05 to 2017-12-22):
Figure saved: C:\Users\small\OneDrive\notebook\Internet_Course\Selection\JAQS\event_report.pdf


Value of signals of Different Quantiles Statistics
          min  max      mean       std   count  count %
quantile                                               
1         0.0  1.0  0.015204  0.122365  136475    100.0
Figure saved: C:\Users\small\OneDrive\notebook\Internet_Course\Selection\JAQS\returns_report.pdf
Information Analysis
                ic
IC Mean     -0.002
IC Std.      0.066
t-stat(IC)     NaN
p-value(IC)    NaN
IC Skew        NaN
IC Kurtosis    NaN
Ann. IR     -0.037
Figure saved: C:\Users\small\OneDrive\notebook\Internet_Course\Selection\JAQS\information_report.pdf
                   Annu. Ret.  Annu. Vol.    t-stat  p-value  skewness  \
trade_date Period                                                        
all_sample 4         0.195337    0.297228  3.848801  0.00012  0.660572   
           8         0.252103    0.279498  7.470432  0.00000  0.742530   
           12        0.234839    0.272293  8.748348  0.00000  0.567819   

                   kurtosis  occurance  Annu. Ret. (all samp)  \
trade_date Period                                               
all_sample 4       4.367479       2075               0.050040   
           8       5.040717       2075               0.067030   
           12      2.933731       2075               0.073625   

                   Annu. Vol. (all samp)  
trade_date Period                         
all_sample 4                    0.311621  
           8                    0.297248  
           12                   0.291717  

output_12_1.png-99.1kB

output_12_2.png-255.2kB

output_12_3.png-138.8kB

  1. signal_data = signal_data(signal,'cross_sig', period=4)
Nan Data Count (should be zero) : 0;  Percentage of effective data: 53%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 53%
Nan Data Count (should be zero) : 0;  Percentage of effective data: 53%

       Calendar Distribution    (3696 occurance from 2016-01-05 to 2017-12-22):
Figure saved: C:\Users\small\OneDrive\notebook\Internet_Course\Selection\JAQS\event_report.pdf


Value of signals of Different Quantiles Statistics
          min  max      mean       std   count  count %
quantile                                               
1         0.0  1.0  0.015204  0.122365  136475    100.0
Figure saved: C:\Users\small\OneDrive\notebook\Internet_Course\Selection\JAQS\returns_report.pdf
Information Analysis
                ic
IC Mean     -0.002
IC Std.      0.066
t-stat(IC)     NaN
p-value(IC)    NaN
IC Skew        NaN
IC Kurtosis    NaN
Ann. IR     -0.037
Figure saved: C:\Users\small\OneDrive\notebook\Internet_Course\Selection\JAQS\information_report.pdf
                   Annu. Ret.  Annu. Vol.    t-stat  p-value  skewness  \
trade_date Period                                                        
all_sample 4         0.195337    0.297228  3.848801  0.00012  0.660572   
           8         0.252103    0.279498  7.470432  0.00000  0.742530   
           12        0.234839    0.272293  8.748348  0.00000  0.567819   

                   kurtosis  occurance  Annu. Ret. (all samp)  \
trade_date Period                                               
all_sample 4       4.367479       2075               0.050040   
           8       5.040717       2075               0.067030   
           12      2.933731       2075               0.073625   

                   Annu. Vol. (all samp)  
trade_date Period                         
all_sample 4                    0.311621  
           8                    0.297248  
           12                   0.291717  
  1. signal_df = signal_data.signal[signal_data.signal==1].unstack()
  1. # 事件表格
  2. print(signal_df.tail().stack())
trade_date  symbol   
20171218    002008.SZ    1.0
            600519.SH    1.0
20171219    600009.SH    1.0
20171220    002608.SZ    1.0
            601117.SH    1.0
            601933.SH    1.0
20171221    000568.SZ    1.0
            002304.SZ    1.0
            002508.SZ    1.0
            002555.SZ    1.0
            600276.SH    1.0
            600660.SH    1.0
20171222    601939.SH    1.0
            603833.SH    1.0
dtype: float64
  1. # 保存成excel
  2. signal_df.to_excel('signal_df.xlsx')
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注