示例#1
0
def run_strategy():

    start_date = 20150501
    end_date = 20171030
    index = '399975.SZ'

    ds = RemoteDataService()
    ds.init_from_config(data_config)
    symbol_list = ds.get_index_comp(index, start_date, start_date)

    # add the benchmark index to the last position of symbol_list
    symbol_list.append(index)
    props = {
        "symbol": ','.join(symbol_list),
        "start_date": start_date,
        "end_date": end_date,
        "bar_type": "1d",
        "init_balance": 1e7,
        "std multiplier": 1.5,
        "m": 10,
        "n": 60,
        "commission_rate": 2E-4
    }
    props.update(data_config)
    props.update(trade_config)

    tapi = BacktestTradeApi()
    ins = EventBacktestInstance()

    strat = SectorRolling()
    pm = PortfolioManager()

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

    ins.init_from_config(props)
    ins.run()
    ins.save_results(folder_path=result_dir_path)

    ta = ana.EventAnalyzer()

    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']]

    ta.do_analyze(result_dir=result_dir_path,
                  selected_sec=props['symbol'].split(',')[:2])
示例#2
0
def test_save_dataview():
    ds = RemoteDataService()
    ds.init_from_config(data_config)
    dv = DataView()

    props = {'start_date': start_date, 'end_date': end_date,
             'fields': 'sw1',
             'symbol': my_symbols,
             'freq': 1}

    dv.init_from_config(props, ds)
    dv.prepare_data()

    # set the benchmark
    res, _ = ds.daily(benchmark, start_date=dv.start_date, end_date=dv.end_date)
    dv._data_benchmark = res.set_index('trade_date').loc[:, ['close']]

    dv.save_dataview(folder_path=dataview_dir_path)
示例#3
0
def run_strategy():

    start_date = 20150501
    end_date = 20171030
    index = '399975.SZ'
    
    ds = RemoteDataService()
    ds.init_from_config(data_config)
    symbol_list = ds.query_index_member(index, start_date, start_date)

    # add the benchmark index to the last position of symbol_list
    symbol_list.append(index)
    props = {"symbol": ','.join(symbol_list),
             "start_date": start_date,
             "end_date": end_date,
             "bar_type": "1d",
             "init_balance": 1e7,
             "std multiplier": 1.5,
             "m": 10,
             "n": 60,
             "commission_rate": 2E-4}
    props.update(data_config)
    props.update(trade_config)

    tapi = BacktestTradeApi()
    ins = EventBacktestInstance()

    strat = SectorRolling()
    pm = PortfolioManager()

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

    ins.init_from_config(props)
    ins.run()
    ins.save_results(folder_path=result_dir_path)

    ta = ana.EventAnalyzer()

    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']]

    ta.do_analyze(result_dir=result_dir_path, selected_sec=props['symbol'].split(',')[:2])
示例#4
0
def my_globals(request):
    ds = RemoteDataService()
    ds.init_from_config(data_config)

    df, msg = ds.daily("000001.SH, 600030.SH, 000300.SH",
                       start_date=20170801,
                       end_date=20170820,
                       fields="open,high,low,close,vwap,preclose")

    multi_index_names = ['trade_date', 'symbol']
    df_multi = df.set_index(multi_index_names, drop=False)
    df_multi.sort_index(axis=0, level=multi_index_names, inplace=True)

    dfx = df_multi.loc[pd.IndexSlice[:, :], pd.IndexSlice['close']].unstack()
    dfy = df_multi.loc[pd.IndexSlice[:, :], pd.IndexSlice['open']].unstack()

    parser = Parser()
    request.function.__globals__.update({
        'parser': parser,
        'dfx': dfx,
        'dfy': dfy
    })
示例#5
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))
示例#6
0
    dfy = df_multi.loc[pd.IndexSlice[:, :], pd.IndexSlice['open']].unstack()

    parser = Parser()
    request.function.__globals__.update({
        'parser': parser,
        'dfx': dfx,
        'dfy': dfy
    })


if __name__ == "__main__":
    ds = RemoteDataService()
    ds.init_from_config(data_config)

    df, msg = ds.daily("000001.SH, 600030.SH, 000300.SH",
                       start_date=20170801,
                       end_date=20170820,
                       fields="open,high,low,close,vwap,preclose")
    ds.data_api.close()

    multi_index_names = ['trade_date', 'symbol']
    df_multi = df.set_index(multi_index_names, drop=False)
    df_multi.sort_index(axis=0, level=multi_index_names, inplace=True)

    dfx = df_multi.loc[pd.IndexSlice[:, :], pd.IndexSlice['close']].unstack()
    dfy = df_multi.loc[pd.IndexSlice[:, :], pd.IndexSlice['open']].unstack()

    parser = Parser()

    g = globals()
    g = {k: v for k, v in g.items() if k.startswith('test_') and callable(v)}