def test_alpha_strategy_dataview(): save_dataview() dv = DataView() dv.load_dataview(folder_path=dataview_dir_path) props = { "start_date": dv.start_date, "end_date": dv.end_date, "period": "week", "days_delay": 0, "init_balance": 1e8, "position_ratio": 0.7, 'commission_rate': 0.0 } trade_api = AlphaTradeApi() bt = AlphaBacktestInstance() risk_model = model.FactorRiskModel() signal_model = model.FactorRevenueModel() cost_model = model.SimpleCostModel() stock_selector = model.StockSelector() signal_model.add_signal(name='my_factor', func=my_factor) cost_model.consider_cost(name='my_commission', func=my_commission, options={'myrate': 1e-2}) stock_selector.add_filter(name='total_profit_growth', func=my_selector) stock_selector.add_filter(name='no_new_stocks', func=my_selector_no_new_stocks) strategy = AlphaStrategy(revenue_model=signal_model, stock_selector=stock_selector, cost_model=cost_model, risk_model=risk_model, pc_method='factor_value_weight') pm = PortfolioManager() # strategy = AlphaStrategy(revenue_model=signal_model, pc_method='factor_value_weight') # strategy = AlphaStrategy(stock_selector=stock_selector, pc_method='market_value_weight') # strategy = AlphaStrategy() context = model.AlphaContext(dataview=dv, trade_api=trade_api, instance=bt, strategy=strategy, pm=pm) for mdl in [risk_model, signal_model, cost_model, stock_selector]: mdl.register_context(context) bt.init_from_config(props) bt.run_alpha() bt.save_results(folder_path=backtest_result_dir_path)
def test_alpha_strategy_dataview(): dv = DataView() dv.load_dataview(folder_path=dataview_dir_path) props = { "benchmark": "000300.SH", "universe": ','.join(dv.symbol), "start_date": dv.start_date, "end_date": dv.end_date, "period": "month", "days_delay": 0, "init_balance": 1e8, "position_ratio": 1.0, } trade_api = AlphaTradeApi() trade_api.init_from_config(props) def selector_growth(context, user_options=None): growth_rate = context.snapshot['net_profit_growth'] return (growth_rate >= 0.2) & (growth_rate <= 4) def selector_pe(context, user_options=None): pe_ttm = context.snapshot['pe_ttm'] return (pe_ttm >= 10) & (pe_ttm <= 20) stock_selector = model.StockSelector() stock_selector.add_filter(name='net_profit_growth', func=selector_growth) stock_selector.add_filter(name='pe', func=selector_pe) strategy = AlphaStrategy(stock_selector=stock_selector, pc_method='equal_weight') pm = PortfolioManager() bt = AlphaBacktestInstance() context = model.Context(dataview=dv, instance=bt, strategy=strategy, trade_api=trade_api, pm=pm) stock_selector.register_context(context) bt.init_from_config(props) bt.run_alpha() bt.save_results(folder_path=backtest_result_dir_path)
def test_alpha_strategy_dataview(): dv = DataView() dv.load_dataview(folder_path=dataview_dir_path) props = { "start_date": dv.start_date, "end_date": dv.end_date, "period": "week", "days_delay": 0, "init_balance": 1e8, "position_ratio": 0.7, 'commission_rate': 0.0 } trade_api = AlphaTradeApi() bt = AlphaBacktestInstance() stock_selector = model.StockSelector() stock_selector.add_filter(name='myselector', func=my_selector) strategy = AlphaStrategy(stock_selector=stock_selector, pc_method='equal_weight') pm = PortfolioManager() context = model.AlphaContext(dataview=dv, trade_api=trade_api, instance=bt, strategy=strategy, pm=pm) store = pd.HDFStore(ic_weight_hd5_path) factorList = fileio.read_json(custom_data_path) context.ic_weight = store['ic_weight'] context.factorList = factorList store.close() for mdl in [stock_selector]: mdl.register_context(context) bt.init_from_config(props) bt.run_alpha() bt.save_results(folder_path=backtest_result_dir_path)
def test_alpha_strategy_dataview(): dv = DataView() dv.load_dataview(folder_path=dataview_dir_path) props = { "benchmark": "000300.SH", "universe": ','.join(dv.symbol), "start_date": 20170131, "end_date": dv.end_date, "period": "month", "days_delay": 0, "init_balance": 1e9, "position_ratio": 1.0, } trade_api = AlphaTradeApi() def singal_gq30(context, user_options=None): import numpy as np res = np.power(context.snapshot['gq30'], 8) return res signal_model = model.FactorRevenueModel() signal_model.add_signal('signal_gq30', singal_gq30) strategy = AlphaStrategy(revenue_model=signal_model, pc_method='factor_value_weight') pm = PortfolioManager() bt = AlphaBacktestInstance() context = model.Context(dataview=dv, instance=bt, strategy=strategy, trade_api=trade_api, pm=pm) signal_model.register_context(context) bt.init_from_config(props) bt.run_alpha() bt.save_results(folder_path=backtest_result_dir_path)
def test_alpha_strategy_dataview(): dv = DataView() dv.load_dataview(folder_path=dataview_dir_path) props = { "start_date": dv.start_date, "end_date": dv.end_date, "period": "week", "days_delay": 0, "init_balance": 1e8, "position_ratio": 1.0, } gateway = AlphaTradeApi() gateway.init_from_config(props) context = model.Context(dataview=dv, gateway=gateway) stock_selector = model.StockSelector() stock_selector.add_filter(name='myselector', func=my_selector) signal_model = model.FactorRevenueModel() signal_model.add_signal(name='signalsize', func=signal_size) strategy = AlphaStrategy(stock_selector=stock_selector, pc_method='factor_value_weight', revenue_model=signal_model) pm = PortfolioManager() bt = AlphaBacktestInstance() context = model.Context(dataview=dv, instance=bt, strategy=strategy, trade_api=trade_api, pm=pm) for mdl in [signal_model, stock_selector]: mdl.register_context(context) bt.init_from_config(props) bt.run_alpha() bt.save_results(folder_path=backtest_result_dir_path)
def test_alpha_strategy_dataview(): dv = DataView() dv.load_dataview(folder_path=dataview_dir_path) props = { "benchmark": "000300.SH", "universe": ','.join(dv.symbol), "start_date": dv.start_date, "end_date": dv.end_date, "period": "month", "days_delay": 0, "init_balance": 1e8, "position_ratio": 1.0, } trade_api = AlphaTradeApi() context = model.Context(dataview=dv, gateway=trade_api) stock_selector = model.StockSelector(context) stock_selector.add_filter(name='myrank', func=my_selector) strategy = AlphaStrategy(stock_selector=stock_selector, pc_method='equal_weight') pm = PortfolioManager() bt = AlphaBacktestInstance() context = model.Context(dataview=dv, instance=bt, strategy=strategy, trade_api=trade_api, pm=pm) stock_selector.register_context(context) bt.init_from_config(props) bt.run_alpha() bt.save_results(folder_path=backtest_result_dir_path)