Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
def test_livetrade():
    props = {'symbol': 'rb1801.SHF', 'strategy_no': 46}
    tapi = RealTimeTradeApi(trade_config)
    ins = EventLiveTradeInstance()

    props.update(data_config)
    props.update(trade_config)

    ds = RemoteDataService()
    strat = DoubleMaStrategy()
    pm = PortfolioManager()

    context = model.Context(data_api=ds,
                            trade_api=tapi,
                            instance=ins,
                            strategy=strat,
                            pm=pm)

    ins.init_from_config(props)
    ds.subscribe(props['symbol'])

    ins.run()
    time.sleep(3)
    ins.stop()
    ins.save_results(result_dir_path)
    do_analyze()
Ejemplo n.º 4
0
def run_strategy():
    if is_backtest:
        """
        回测模式
        """
        props = {"symbol": '600519.SH',
                 "start_date": 20170101,
                 "end_date": 20171104,
                 "fast_ma_length": 5,
                 "slow_ma_length": 15,
                 "bar_type": "1d",  # '1d'
                 "init_balance": 50000}

        tapi = BacktestTradeApi()
        ins = EventBacktestInstance()
        
    else:
        """
        实盘/仿真模式
        """
        props = {'symbol': '600519.SH',
                 "fast_ma_length": 5,
                 "slow_ma_length": 15,
                 'strategy.no': 1062}
        tapi = RealTimeTradeApi(trade_config)
        ins = EventLiveTradeInstance()
Ejemplo n.º 5
0
def run_strategy():
    '''
    universe可以自己定义吗,就是从我筛选的一组股票里面挑选
    把universe改成symbol,然后值设置成一系列代码的字符串,用逗号隔开
    :return:
    '''
    if is_backtest:
        """
        回测模式
        """
        props = {
            "symbol": '600519.SH',
            # "symbol": '002050.SZ',
            # "benchmark": '002050.SZ',
            "benchmark": '000300.SH',
            "start_date": 20170101,
            "end_date": 20171219,
            "fast_ma_length": 3,
            "slow_ma_length": 8,
            "live_ma_length": 34,
            "bar_type": "1d",  # '1d'
            "init_balance": 50000}

        tapi = BacktestTradeApi()
        ins = EventBacktestInstance()

    else:
        """
        实盘/仿真模式
        """
        props = {'symbol': '600519.SH',
                 "fast_ma_length": 3,
                 "slow_ma_length": 5,
                 "live_ma_length": 34,
                 'strategy.no': 1062}
        tapi = RealTimeTradeApi(trade_config)
        ins = EventLiveTradeInstance()

    props.update(data_config)
    props.update(trade_config)

    ds = RemoteDataService()
    strat = TripleMaStrategy()
    pm = PortfolioManager()

    context = model.Context(data_api=ds, trade_api=tapi, instance=ins,
                            strategy=strat, pm=pm)

    ins.init_from_config(props)
    if not is_backtest:
        ds.subscribe(props['symbol'])

    ins.run()
    if not is_backtest:
        time.sleep(9999)
    ins.save_results(folder_path=result_dir_path)
Ejemplo n.º 6
0
def run_strategy():
    if is_backtest:
        """
        回测模式
        """
        props = {
            "symbol": '600519.SH',
            "start_date": 20170101,
            "end_date": 20171104,
            "fast_ma_length": 5,
            "slow_ma_length": 15,
            "bar_type": "1d",  # '1d'
            "init_balance": 50000
        }

        tapi = BacktestTradeApi()
        ins = EventBacktestInstance()

    else:
        """
        实盘/仿真模式
        """
        props = {
            'symbol': '600519.SH',
            "fast_ma_length": 5,
            "slow_ma_length": 15,
            'strategy.no': 1062
        }
        tapi = RealTimeTradeApi(trade_config)
        ins = EventLiveTradeInstance()

    props.update(data_config)
    props.update(trade_config)

    ds = RemoteDataService()
    strat = DoubleMaStrategy()
    pm = PortfolioManager()

    context = model.Context(data_api=ds,
                            trade_api=tapi,
                            instance=ins,
                            strategy=strat,
                            pm=pm)

    ins.init_from_config(props)
    if not is_backtest:
        ds.subscribe(props['symbol'])

    ins.run()
    if not is_backtest:
        time.sleep(9999)
    ins.save_results(folder_path=result_dir_path)
Ejemplo n.º 7
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)
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)
Ejemplo n.º 9
0
def run_strategy():
    if is_backtest:
        props = {
            "symbol": "rb1710.SHF",
            "start_date": 20170510,
            "end_date": 20170930,
            "bar_type": "1M",  # '1d'
            "init_balance": 2e4
        }

        tapi = BacktestTradeApi()
        ins = EventBacktestInstance()

    else:
        props = {'symbol': 'rb1801.SHF', 'strategy.no': 46}
        tapi = RealTimeTradeApi(trade_config)
        ins = EventLiveTradeInstance()

    props.update(data_config)
    props.update(trade_config)

    ds = RemoteDataService()
    strat = DoubleMaStrategy()
    pm = PortfolioManager()

    context = model.Context(data_api=ds,
                            trade_api=tapi,
                            instance=ins,
                            strategy=strat,
                            pm=pm)

    ins.init_from_config(props)
    if not is_backtest:
        ds.subscribe(props['symbol'])

    ins.run()
    if not is_backtest:
        time.sleep(9999)
    ins.save_results(folder_path=result_dir_path)
Ejemplo n.º 10
0
def run_strategy():
    if is_backtest:
        """
        回测模式
        """

        ds = RemoteDataService()
        ds.init_from_config(data_config)
        symbol_list = ds.query_index_member(index, start_date, end_date)
        # symbol_list = ['600887.SH']
        # symbol_list = sample(symbol_list, 20)
        print(symbol_list)

        # add the benchmark index to the last position of symbol_list
        symbol_list.append(index)
        props = {"symbol": ','.join(symbol_list),
                 "holding_Count": 15,
                 "start_date": start_date,
                 "end_date": end_date,
                 "bar_type": "1d",  # '1d'
                 "init_balance": 300000,
                 "commission_rate": 2E-4}

        tapi = BacktestTradeApi()
        ins = EventBacktestInstance()

    else:
        """
        实盘/仿真模式
        """
        props = {'symbol': '600519.SH',
                 "fast_ma_length": 5,
                 "slow_ma_length": 15,
                 'strategy.no': 1062}
        tapi = RealTimeTradeApi(trade_config)
        ins = EventLiveTradeInstance()

    props.update(data_config)
    props.update(trade_config)

    ds = RemoteDataService()
    strat = RNNStrategy()
    pm = PortfolioManager()

    context = model.Context(data_api=ds, trade_api=tapi, instance=ins,
                            strategy=strat, pm=pm)

    ins.init_from_config(props)
    if not is_backtest:
        ds.subscribe(props['symbol'])

    ins.run()
    if not is_backtest:
        time.sleep(9999)
    ins.save_results(folder_path=result_dir_path)

    ta = ana.EventAnalyzer()

    ds = RemoteDataService()
    ds.init_from_config(data_config)

    ta.initialize(data_server_=ds, file_folder=result_dir_path)
    df_bench, _ = ds.daily(index, start_date=start_date, end_date=end_date)
    ta.data_benchmark = df_bench.set_index('trade_date').loc[:, ['close']]

    temp = pd.read_csv(result_dir_path + '/trades.csv')
    symbols = set(temp['symbol'].unique())
    print(symbols)

    ta.do_analyze(result_dir=result_dir_path, selected_sec=list(symbols))