def analyze_signal(dv, signal_name, output_format='pdf'): # Step.2 calculate mask (to mask those ill data points) mask_sus = dv.get_ts('suspended') mask_index_member = dv.get_ts('not_index_member') mask_limit_reached = dv.get_ts('limit_reached') mask_all = np.logical_or( mask_sus, np.logical_or(mask_index_member, mask_limit_reached)) signal = dv.get_ts(signal_name) # avoid look-ahead bias price = dv.get_ts('close_adj') price_bench = dv.data_benchmark # Step.4 analyze! my_period = 5 obj = SignalDigger(output_folder='../../output/test_signal', output_format=output_format) obj.process_signal_before_analysis( signal, price=price, mask=mask_all, n_quantiles=5, period=my_period, benchmark_price=price_bench, ) res = obj.create_full_report()
def analyze_event(): # -------------------------------------------------------------------------------- # Step.1 load dataview dv = DataView() dv.load_dataview(dataview_folder) # -------------------------------------------------------------------------------- # Step.3 get signal, benchmark and price data target_symbol = '600519.SH' price = dv.get_ts('close_adj', symbol=target_symbol) dv.add_formula('in_', 'open_adj / Delay(close_adj, 1)', is_quarterly=False) signal = dv.get_ts('in_', symbol=target_symbol).shift(1, axis=0) # avoid look-ahead bias # Step.4 analyze! obj = SignalDigger(output_folder='../../output', output_format='pdf') obj.create_single_signal_report(signal, price, [1, 5, 9, 21], 6, mask=None, buy_condition={'cond1': {'column': 'quantile', 'filter': lambda x: x > 3, 'hold': 5}, 'cond2': {'column': 'quantile', 'filter': lambda x: x > 5, 'hold': 5}, 'cond3': {'column': 'quantile', 'filter': lambda x: x > 5, 'hold': 9}, })
def analyze_event(): # -------------------------------------------------------------------------------- # Step.1 load dataview dv = DataView() dv.load_dataview(dataview_folder) # -------------------------------------------------------------------------------- # Step.2 calculate mask (to mask those ill data points) mask_limit_reached = dv.get_ts('mask_limit_reached') mask_index_member = dv.get_ts('mask_index_member') mask_sus = dv.get_ts('mask_sus') mask_all = np.logical_or( mask_sus, np.logical_or(mask_index_member, mask_limit_reached)) # -------------------------------------------------------------------------------- # Step.3 get signal, benchmark and price data price = dv.get_ts('close_adj') price_bench = dv.data_benchmark dv.add_formula('in_', '(Delay(index_weight, 1) == 0) && (index_weight > 0)', is_quarterly=False) signal = dv.get_ts('in_').shift(1, axis=0) # avoid look-ahead bias # Step.4 analyze! obj = SignalDigger(output_folder='../../output', output_format='pdf') obj.create_binary_event_report(signal, price, mask_all, price_bench, periods=[20, 60, 121, 242], group_by=None)
def analyze_event(): # -------------------------------------------------------------------------------- # Step.1 load dataview dv = DataView() dv.load_dataview(dataview_folder) # -------------------------------------------------------------------------------- # Step.2 calculate mask (to mask those ill data points) mask_limit_reached = dv.get_ts('mask_limit_reached') mask_index_member = dv.get_ts('mask_index_member') mask_sus = dv.get_ts('mask_sus') mask_all = np.logical_or(mask_sus, np.logical_or(mask_index_member, mask_limit_reached)) # -------------------------------------------------------------------------------- # Step.3 get signal, benchmark and price data price = dv.get_ts('close_adj') price_bench = dv.data_benchmark dv.add_formula('in_', '(Delay(index_weight, 1) == 0) && (index_weight > 0)', is_quarterly=False) signal = dv.get_ts('in_').shift(1, axis=0) # avoid look-ahead bias # Step.4 analyze! obj = SignalDigger(output_folder='../../output', output_format='pdf') obj.create_binary_event_report(signal, price, mask_all, price_bench, periods=[20, 60, 121, 242], group_by=None)
def analyze_signal(): # -------------------------------------------------------------------------------- # Step.1 load dataview dv = DataView() dv.load_dataview(dataview_folder) # -------------------------------------------------------------------------------- # Step.2 calculate mask (to mask those ill data points) trade_status = dv.get_ts('trade_status') mask_sus = trade_status == u'停牌'.encode('utf-8') df_index_member = dv.get_ts('index_member') mask_index_member = ~(df_index_member > 0) dv.add_formula('limit_reached', 'Abs((open - Delay(close, 1)) / Delay(close, 1)) > 0.095', is_quarterly=False) df_limit_reached = dv.get_ts('limit_reached') mask_limit_reached = df_limit_reached > 0 mask_all = np.logical_or( mask_sus, np.logical_or(mask_index_member, mask_limit_reached)) # -------------------------------------------------------------------------------- # Step.3 get signal, benchmark and price data # dv.add_formula('illi_daily', '(high - low) * 1000000000 / turnover', is_quarterly=False) # dv.add_formula('illi', 'Ewma(illi_daily, 11)', is_quarterly=False) # dv.add_formula('size', 'Log(float_mv)', is_quarterly=False) # dv.add_formula('value', '-1.0/pb', is_quarterly=False) # dv.add_formula('liquidity', 'Ts_Mean(volume, 22) / float_mv', is_quarterly=False) dv.add_formula('divert', '- Correlation(vwap_adj, volume, 10)', is_quarterly=False) signal = dv.get_ts('divert').shift(1, axis=0) # avoid look-ahead bias price = dv.get_ts('close_adj') price_bench = dv.data_benchmark # Step.4 analyze! my_period = 5 obj = SignalDigger(output_folder='../../output/test_signal', output_format='pdf') obj.process_signal_before_analysis( signal, price=price, mask=mask_all, n_quantiles=5, period=my_period, benchmark_price=price_bench, ) res = obj.create_full_report()
def analyze_event(): # -------------------------------------------------------------------------------- # Step.1 load dataview dv = DataView() dv.load_dataview(dataview_folder) # -------------------------------------------------------------------------------- # Step.2 calculate mask (to mask those ill data points) trade_status = dv.get_ts('trade_status') mask_sus = trade_status == u'停牌'.encode('utf-8') df_index_member = dv.get_ts('index_member') mask_index_member = ~(df_index_member > 0) dv.add_formula('limit_reached', 'Abs((open - Delay(close, 1)) / Delay(close, 1)) > 0.095', is_quarterly=False) df_limit_reached = dv.get_ts('limit_reached') mask_limit_reached = df_limit_reached > 0 mask_all = np.logical_or( mask_sus, np.logical_or(mask_index_member, mask_limit_reached)) # -------------------------------------------------------------------------------- # Step.3 get signal, benchmark and price data dv.add_formula('new_high', 'close_adj >= Ts_Max(close_adj, 300)', is_quarterly=False) dv.add_formula('new_high_delay', 'Delay(Ts_Max(new_high, 300), 1)', is_quarterly=False) dv.add_formula('sig', 'new_high && (! new_high_delay)', is_quarterly=False) signal = dv.get_ts('sig').shift(0, axis=0) # avoid look-ahead bias price = dv.get_ts('close_adj') price_bench = dv.data_benchmark # Step.4 analyze! obj = SignalDigger(output_folder=jutil.join_relative_path('../output'), output_format='pdf') obj.create_binary_event_report(signal, price, mask_all, 5, price_bench, periods=[5, 20, 40])
def test_analyze_signal(): # -------------------------------------------------------------------------------- # Step.1 load dataview dv = DataView() dv.load_dataview(dataview_folder) mask = mask_index_member(dv) can_enter, can_exit = limit_up_down(dv) # -------------------------------------------------------------------------------- # Step.3 get signal, benchmark and price data dv.add_formula('divert', '- Correlation(vwap_adj, volume, 10)', is_quarterly=False, add_data=True) signal = dv.get_ts('divert') price = dv.get_ts('close_adj') price_bench = dv.data_benchmark # Step.4 analyze! my_period = 5 obj = SignalDigger(output_folder='../output/test_signal', output_format='pdf') obj.process_signal_before_analysis( signal=signal, price=price, high=dv.get_ts("high_adj"), # 可为空 low=dv.get_ts("low_adj"), # 可为空 group=dv.get_ts("sw1"), n_quantiles=5, # quantile分类数 mask=mask, # 过滤条件 can_enter=can_enter, # 是否能进场 can_exit=can_exit, # 是否能出场 period=my_period, # 持有期 benchmark_price=price_bench, # 基准价格 可不传入,持有期收益(return)计算为绝对收益 commission=0.0008, ) signal_data = obj.signal_data result = analysis(signal_data, is_event=False, period=my_period) ic = pfm.calc_signal_ic(signal_data, by_group=True) mean_ic_by_group = pfm.mean_information_coefficient(ic, by_group=True) plotting.plot_ic_by_group(mean_ic_by_group) res = obj.create_full_report()
def analyze_signal(dv, signal_name, output_format='pdf'): # Step.2 calculate mask (to mask those ill data points) mask_sus = dv.get_ts('suspended') mask_index_member = dv.get_ts('not_index_member') mask_limit_reached = dv.get_ts('limit_reached') mask_all = np.logical_or(mask_sus, np.logical_or(mask_index_member, mask_limit_reached)) signal = dv.get_ts(signal_name).shift(1, axis=0) # avoid look-ahead bias price = dv.get_ts('close_adj') price_bench = dv.data_benchmark # Step.4 analyze! my_period = 5 obj = SignalDigger(output_folder='../../output/test_signal', output_format=output_format) obj.process_signal_before_analysis(signal, price=price, mask=mask_all, n_quantiles=5, period=my_period, benchmark_price=price_bench, ) res = obj.create_full_report()
def test_analyze_signal(): # -------------------------------------------------------------------------------- # Step.1 load dataview dv = DataView() dv.load_dataview(dataview_folder) # -------------------------------------------------------------------------------- # Step.2 calculate mask (to mask those ill data points) trade_status = dv.get_ts('trade_status') mask_sus = trade_status == u'停牌' df_index_member = dv.get_ts('index_member') mask_index_member = ~(df_index_member > 0) dv.add_formula('limit_reached', 'Abs((open - Delay(close, 1)) / Delay(close, 1)) > 0.095', is_quarterly=False) df_limit_reached = dv.get_ts('limit_reached') mask_limit_reached = df_limit_reached > 0 mask_all = np.logical_or(mask_sus, np.logical_or(mask_index_member, mask_limit_reached)) # -------------------------------------------------------------------------------- # Step.3 get signal, benchmark and price data dv.add_formula('divert', '- Correlation(vwap_adj, volume, 10)', is_quarterly=False) signal = dv.get_ts('divert').shift(1, axis=0) # avoid look-ahead bias price = dv.get_ts('close_adj') price_bench = dv.data_benchmark # Step.4 analyze! my_period = 5 obj = SignalDigger(output_folder='../output/test_signal', output_format='pdf') obj.process_signal_before_analysis(signal, price=price, mask=mask_all, n_quantiles=5, period=my_period, benchmark_price=price_bench, ) res = obj.create_full_report()
def _generate_data(self, signal, period, mask=None, can_enter=None, can_exit=None, benchmark=None, commission=0.0008): ''' :param signal: :param period: :param mask: :param can_enter: :param can_exit: :param benchmark: :param commission: :return: ''' obj = SignalDigger() # 处理因子 计算目标股票池每只股票的持有期收益,和对应因子值的quantile分类 obj.process_signal_before_analysis( signal=signal, price=self.dv.get_ts("close_adj").reindex_like(signal), high=self.dv.get_ts("high_adj").reindex_like(signal), low=self.dv.get_ts("low_adj").reindex_like(signal), n_quantiles=5, mask=mask.reindex_like(signal) if mask is not None else None, can_enter=can_enter.reindex_like(signal) if can_enter is not None else None, # 是否能进场 can_exit=can_exit.reindex_like(signal) if can_exit is not None else None, # 是否能出场 period=period, # 持有期 benchmark_price=benchmark, # 基准价格 可不传入,持有期收益(return)计算为绝对收益 commission=commission, ) return obj.signal_data
def cal_obj(signal, name, period, quantile): obj = SignalDigger( ) #SignalDigger实体化,下面调用它的方法process_signal_before_analysis obj.process_signal_before_analysis( signal, #因子数据 price=price, #价格数据 n_quantiles=quantile, #百分位 period=period, #持有周期 can_enter=can_enter, #能买入 can_exit=can_exit, #能卖出 group=group, #行业分类表格 mask=mask #指数成分的过滤 ) obj.create_full_report() #创建一个完整的report return obj