Exemplo n.º 1
0
def test_model():
    model.StockSelector()
    model.SimpleCostModel()
    model.AlphaContext()
    model.FactorRiskModel()
    model.FactorSignalModel()
    
Exemplo n.º 2
0
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)
Exemplo n.º 3
0
def test_alpha_strategy_dataview():
    dv_subfolder_name = 'test_dataview'
    save_dataview(sub_folder=dv_subfolder_name)

    dv = DataView()

    fullpath = fileio.join_relative_path('../output/prepared',
                                         dv_subfolder_name)
    dv.load_dataview(folder=fullpath)

    props = {
        "benchmark": "000300.SH",
        # "symbol": ','.join(dv.symbol),
        "universe": ','.join(dv.symbol),
        "start_date": dv.start_date,
        "end_date": dv.end_date,
        "period": "month",
        "days_delay": 0,
        "init_balance": 1e9,
        "position_ratio": 0.7,
    }

    gateway = DailyStockSimGateway()
    gateway.init_from_config(props)

    context = model.Context()
    context.register_gateway(gateway)
    context.register_trade_api(gateway)
    context.register_dataview(dv)

    risk_model = model.FactorRiskModel()
    signal_model = model.FactorRevenueModel_dv()
    cost_model = model.SimpleCostModel()

    risk_model.register_context(context)
    signal_model.register_context(context)
    cost_model.register_context(context)

    signal_model.register_func('my_factor', my_factor)
    signal_model.activate_func({'my_factor': {}})
    cost_model.register_func('my_commission', my_commission)
    cost_model.activate_func({'my_commission': {'myrate': 1e-2}})

    strategy = DemoAlphaStrategy(risk_model, signal_model, cost_model)
    # strategy.active_pc_method = 'equal_weight'
    # strategy.active_pc_method = 'mc'
    strategy.active_pc_method = 'factor_value_weight'

    bt = AlphaBacktestInstance_dv()
    bt.init_from_config(props, strategy, context=context)

    bt.run_alpha()

    bt.save_results(fileio.join_relative_path('../output/'))