コード例 #1
0
ファイル: first_example.py プロジェクト: gglive/JAQS
def do_backtest():
    # Load local data file that we just stored.
    dv = DataView()
    dv.load_dataview(folder_path=dataview_store_folder)

    backtest_props = {
        "start_date": dv.start_date,  # start and end date of back-test
        "end_date": dv.end_date,
        "period": "month",  # re-balance period length
        "benchmark": dv.benchmark,  # benchmark and universe
        "universe": dv.universe,
        "init_balance": 1e8,  # Amount of money at the start of back-test
        "position_ratio": 1.0,  # Amount of money at the start of back-test
    }
    backtest_props.update(data_config)
    backtest_props.update(trade_config)

    # Create model context using AlphaTradeApi, AlphaStrategy, PortfolioManager and AlphaBacktestInstance.
    # We can store anything, e.g., public variables in context.

    trade_api = AlphaTradeApi()
    strategy = AlphaStrategy(pc_method='market_value_weight')
    pm = PortfolioManager()
    bt = AlphaBacktestInstance()
    context = model.Context(dataview=dv,
                            instance=bt,
                            strategy=strategy,
                            trade_api=trade_api,
                            pm=pm)

    bt.init_from_config(backtest_props)
    bt.run_alpha()

    # After finishing back-test, we save trade results into a folder
    bt.save_results(folder_path=backtest_result_folder)
コード例 #2
0
ファイル: first_example.py プロジェクト: gglive/JAQS
def do_livetrade():
    dv = DataView()
    dv.load_dataview(folder_path=dataview_store_folder)

    props = {"period": "day", "strategy_no": 1044, "init_balance": 1e6}
    props.update(data_config)
    props.update(trade_config)

    strategy = AlphaStrategy(pc_method='market_value_weight')
    pm = PortfolioManager()

    bt = AlphaLiveTradeInstance()
    trade_api = RealTimeTradeApi(props)
    ds = RemoteDataService()

    context = model.Context(dataview=dv,
                            instance=bt,
                            strategy=strategy,
                            trade_api=trade_api,
                            pm=pm,
                            data_api=ds)

    bt.init_from_config(props)
    bt.run_alpha()

    goal_positions = strategy.goal_positions
    print("Length of goal positions:", len(goal_positions))
    task_id, msg = trade_api.goal_portfolio(goal_positions)
    print(task_id, msg)
コード例 #3
0
ファイル: test_backtest.py プロジェクト: liuleicode/JAQS
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)
コード例 #4
0
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,
    }
    props.update(data_config)
    props.update(trade_config)

    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 >= 5) & (pe_ttm <= 80)

    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)
コード例 #5
0
def test_alpha_strategy_dataview():
    dv = DataView()
    dv.load_dataview(folder_path=dataview_dir_path)

    props = {
        "benchmark": BENCHMARK,
        "universe": ','.join(dv.symbol),
        "start_date": dv.start_date,
        "end_date": dv.end_date,
        "period": "day",
        "days_delay": 0,
        "init_balance": 1e8,
        "position_ratio": 1.0,
        "strategy_no": 44
    }
    props.update(data_config)
    props.update(trade_config)

    stock_selector = model.StockSelector()
    stock_selector.add_filter(name='rank_ret_top10', func=my_selector)

    strategy = AlphaStrategy(stock_selector=stock_selector,
                             pc_method='equal_weight')
    pm = PortfolioManager()

    if is_backtest:
        bt = AlphaBacktestInstance()
        trade_api = AlphaTradeApi()
        ds = None
    else:
        bt = AlphaLiveTradeInstance()
        trade_api = RealTimeTradeApi(props)
        ds = RemoteDataService()

    context = model.Context(dataview=dv,
                            instance=bt,
                            strategy=strategy,
                            trade_api=trade_api,
                            pm=pm,
                            data_api=ds)
    stock_selector.register_context(context)

    bt.init_from_config(props)
    bt.run_alpha()

    if is_backtest:
        bt.save_results(folder_path=backtest_result_dir_path)
    else:
        goal_positions = strategy.goal_positions
        print(goal_positions)
コード例 #6
0
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)
コード例 #7
0
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)
コード例 #8
0
ファイル: ICCombine.py プロジェクト: zorro430/JAQS
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,
        'commission_rate': 0.0
    }
    props.update(data_config)
    props.update(trade_config)

    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 = jutil.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)
コード例 #9
0
def do_backtest():
    # Load local data file that we just stored.
    dv = DataView()
    dv.load_dataview(folder_path=dataview_store_folder)

    backtest_props = {# start and end date of back-test
                      "start_date": dv.start_date,
                      "end_date": dv.end_date,
                      # re-balance period length
                      "period": "month",
                      # benchmark and universe
                      "benchmark": dv.benchmark,
                      "universe": dv.universe,
                      # Amount of money at the start of back-test
                      "init_balance": 1e8,
                      # Amount of money at the start of back-test
                      "position_ratio": 1.0,
                      }
    backtest_props.update(data_config)
    backtest_props.update(trade_config)

    # We use trade_api to send orders
    trade_api = AlphaTradeApi()
    # This is our strategy
    strategy = AlphaStrategy(pc_method='market_value_weight')
    # PortfolioManager helps us to manage tasks, orders and calculate positions
    pm = PortfolioManager()
    # BacktestInstance is in charge of running the back-test
    bt = AlphaBacktestInstance()

    # Public variables are stored in context. We can also store anything in it
    context = model.Context(dataview=dv,
                            instance=bt,
                            strategy=strategy,
                            trade_api=trade_api,
                            pm=pm)

    bt.init_from_config(backtest_props)
    bt.run_alpha()

    # After finishing back-test, we save trade results into a folder
    bt.save_results(folder_path=backtest_result_folder)
コード例 #10
0
ファイル: Graham.py プロジェクト: liuleicode/JAQS
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)
コード例 #11
0
ファイル: alphaSimple.py プロジェクト: baqiang/datatest
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)
コード例 #12
0
ファイル: case7.py プロジェクト: zhaidi266/LearnJaqsByExample
def alpha_strategy_backtest():
    dv = DataView()

    dv.load_dataview(folder_path=dataview_folder)
    
    props = {
        "benchmark": benchmark,
        "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,
        }
    props.update(data_config)
    props.update(trade_config)

    trade_api = AlphaTradeApi()
    trade_api.init_from_config(props)

    stock_selector = model.StockSelector()
    stock_selector.add_filter(name='selector_roe_roa_not_new', func=selector_roe_roa_not_new)

    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_folder)
コード例 #13
0
def do_livetrade():
    dv = DataView()
    dv.load_dataview(folder_path=dataview_store_folder)

    # print("total_mv", dv.get_ts('total_mv'))
    # print("float_mv", dv.get_ts('float_mv'))

    props = {"period": "day", "strategy_no": 1683, "init_balance": 1e6}
    props.update(data_config)
    props.update(trade_config)

    strategy = AlphaStrategy(pc_method='market_value_weight')
    pm = PortfolioManager()

    bt = AlphaLiveTradeInstance()
    trade_api = RealTimeTradeApi(props)
    ds = RemoteDataService()

    context = model.Context(dataview=dv,
                            instance=bt,
                            strategy=strategy,
                            trade_api=trade_api,
                            pm=pm,
                            data_api=ds)

    trade_api.set_ordstatus_callback(on_orderstatus)
    trade_api.set_trade_callback(on_trade)
    trade_api.set_task_callback(on_taskstatus)
    bt.init_from_config(props)
    bt.run_alpha()

    goal_positions = strategy.goal_positions
    # print("strategy.weights", strategy.weights)
    # print("Length of goal positions:", len(goal_positions))
    # print(goal_positions)
    task_id, msg = trade_api.goal_portfolio(goal_positions)
    print(task_id, msg)