def test_model(): model.StockSelector() model.SimpleCostModel() model.AlphaContext() model.FactorRiskModel() model.FactorSignalModel()
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.FactorSignalModel() 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(signal_model=signal_model, stock_selector=stock_selector, cost_model=cost_model, risk_model=risk_model, pc_method='factor_value_weight') pm = PortfolioManager() # strategy = AlphaStrategy(signal_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": "000905.SH", "universe": ','.join(dv.symbol), "start_date": 20170605, "end_date": 20180807, "period": "day", "days_delay": 0, "init_balance": 1e9, "position_ratio": 1.0, "commission_rate": 0.0015, #手续费 "n_periods": 2 } props.update(data_config) props.update(trade_config) trade_api = AlphaTradeApi() signal_model = model.FactorSignalModel() #添加信号 signal_model.add_signal('alpha3', alpha) #在使用新因子时,alpha3应改为新因子的名称 stock_selector = model.StockSelector() stock_selector.add_filter(name='myselector', func=my_selector) strategy = AlphaStrategy(stock_selector=stock_selector, signal_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) 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": 20170131, "end_date": dv.end_date, "period": "month", "days_delay": 0, "init_balance": 1e9, "position_ratio": 1.0, } props.update(data_config) props.update(trade_config) 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.FactorSignalModel() signal_model.add_signal('signal_gq30', singal_gq30) strategy = AlphaStrategy(signal_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, } props.update(data_config) props.update(trade_config) trade_api = AlphaTradeApi() stock_selector = model.StockSelector() stock_selector.add_filter(name='myselector', func=my_selector) signal_model = model.FactorSignalModel() signal_model.add_signal(name='signalsize', func=signal_size) strategy = AlphaStrategy(stock_selector=stock_selector, pc_method='factor_value_weight', signal_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 = { "symbol": dv.symbol, "universe": ','.join(dv.symbol), "start_date": dv.start_date, "end_date": dv.end_date, "period": "week", "days_delay": 0, "init_balance": 1e7, "position_ratio": 1.0, "commission_rate": 2E-4 # 手续费万2 } props.update(data_config) props.update(trade_config) trade_api = AlphaTradeApi() signal_model = model.FactorSignalModel() signal_model.add_signal('stockWeight', stockWeight) strategy = AlphaStrategy(signal_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)