Beispiel #1
0
    def setUp(self):
        from dali.queue import sim_queue
        eq = sim_queue.SimQueue()

        from dali.price_handler import sim_bar_handler
        barObj = sim_bar_handler.SimBarHandler(
            '/Users/Hans/Desktop/Dali/data/zz500_15min',
            eq,
            start_date='2019-08-13',
            end_date='2019-08-16',
            look_back=10,
            store_path='/Users/Hans/Desktop/Dali/account/test/',
            fill_nan=True,
            init_tickers=[
                '000008', '000009', '000012', '000021', '000025', '000027',
                '000028', '000031', '000039', '000050', '000060', '000061',
                '000062', '000066', '000078', '000089', '000090', '000156',
                '000158'
            ])

        signal = Signal_test(eq, '/Users/Hans/Desktop/Dali/account/test/')

        from dali import position_handler
        self.posObj = position_handler.Position(
            price_handler=barObj,
            store_path='/Users/Hans/Desktop/Dali/account/test/')

        from dali.portfolio_handler import equal_wgts_portfolio_handler
        self.portfolioObj = equal_wgts_portfolio_handler.EqualWgtsPortfolioHandler(
            eq, ['Signal_test'],
            self.posObj,
            1e7,
            store_path='/Users/Hans/Desktop/Dali/account/test/')

        from dali.execution_handler import sim_exec_handler
        self.seObj = sim_exec_handler.SimExecHandler(
            eq,
            barObj,
            self.posObj,
            store_path='/Users/Hans/Desktop/Dali/account/test/')

        eq.register('BAR', signal)
        eq.register('SIGNAL', self.portfolioObj)
        eq.register('ORDER', self.seObj)
        eq.register('FILL', self.posObj)

        while not barObj.is_iteration_end():
            barObj.update_bar()
            eq.run()

        eq.unregister('BAR', signal)
        eq.unregister('SIGNAL', self.portfolioObj)
        eq.unregister('ORDER', self.seObj)
        eq.unregister('FILL', self.posObj)
Beispiel #2
0
    def setUp(self):
        init_cash = 1e7
        store_path = '/Users/Hans/Desktop/Dali/account/test/'
        account_path = store_path

        from dali.queue import sim_queue
        eq = sim_queue.SimQueue()

        from dali.price_handler import sim_bar_handler
        barObj = sim_bar_handler.SimBarHandler(
            '/Users/Hans/Desktop/Dali/data/zz500_15min',
            eq,
            start_date='2019-08-13 10:45:00',
            end_date='2019-08-15',
            look_back=10,
            store_path='/Users/Hans/Desktop/Dali/account/test/',
            fill_nan=True,
            init_tickers=['000008', '000009'])  #,'000012'])
        # ,'000021','000025',
        #                                         '000027','000028','000031','000039','000050','000060',
        #                                         '000061','000062','000066','000078','000089','000090',
        #                                         '000156','000158'])

        self.signal = Signal_test(eq, '/Users/Hans/Desktop/Dali/account/test/')

        from dali.position_handler import stock_position_handler
        posObj = stock_position_handler.PositionHandler(
            price_handler=barObj,
            init_cash=init_cash,
            store_path='/Users/Hans/Desktop/Dali/account/test/')

        from dali.portfolio_handler import equal_wgts_portfolio_handler
        self.port = equal_wgts_portfolio_handler.EqualWgtsPortfolioHandler(
            eq, ['Signal_test'],
            store_path='/Users/Hans/Desktop/Dali/account/test/')

        from dali.order_handler import order_handler
        self.orderObj = order_handler.OrderHandler(
            eq,
            price_handler=barObj,
            position_handler=posObj,
            init_cash=init_cash,
            store_path='/Users/Hans/Desktop/Dali/account/test/')

        from dali.execution_handler import sim_exec_handler
        self.seObj = sim_exec_handler.SimExecHandler(
            eq,
            barObj,
            posObj,
            store_path='/Users/Hans/Desktop/Dali/account/test/')

        eq.register('BAR', self.signal)
        eq.register('SIGNAL', self.port)
        eq.register('PORTFOLIO', self.orderObj)
        eq.register('ORDER', self.seObj)
        eq.register('FILL', posObj)

        while not barObj.is_iteration_end():
            barObj.update_bar()
            eq.run()

        eq.unregister('BAR', self.signal)
        eq.unregister('SIGNAL', self.port)
        eq.register('PORTFOLIO', self.orderObj)
        eq.unregister('ORDER', self.seObj)
        eq.unregister('FILL', posObj)

        from dali.statistics import books
        b = books.Books(account_path=account_path)
        b.run()
        from dali.statistics import benchmark
        b = benchmark.Benchmark(account_path=account_path)
        b.run()
        from dali.statistics import trades
        t = trades.Trades(account_path=account_path)
        t.run()
        from dali.statistics import profiling
        pro = profiling.Profiling(account_path=account_path,
                                  benchmark='equal_wgts_benchmark')
        pro.run()
def Dali_cal(config, expr_dict):
    print('-'*30 + time.strftime('%H:%M') + ' V' + version.__version__ + '-'*30)
    dali_dict = {}
    dali_dict['_id'] = str(expr_dict['_id'])
    dali_dict['expr'] = expr_dict['expr']


    # 删文件夹========================================================================
    store_path = os.path.join(config['output_dir'].replace('~', '/home/%s/' %getpass.getuser()), str(expr_dict['_id']))
    if not os.path.exists(store_path):
        os.mkdir(store_path)

    #try:
    # Queue========================================================================
    from dali.queue import sim_queue
    eq = sim_queue.SimQueue()

    # Signals========================================================================
    init_cash = config['init_cash']
    look_back = config['lookback']
    start_date = config['start_date']
    end_date = config['end_date']

    signal_name = str(expr_dict['_id'])
    signal_list = [signal_name]

    signal_obj = Signal(eq, store_path=store_path,
                                 signal_name=signal_name,
                                 expr=expr_dict['expr'],
                                 lookback=look_back)


    # BarHandler========================================================================
    from dali.price_handler import sim_bar_handler
    barObj = sim_bar_handler.SimBarHandler(config['data_path'].replace('~', '/home/%s/' %getpass.getuser()), eq,
                                start_date=start_date, end_date=end_date, look_back=look_back,
                                store_path=store_path,
                                fill_nan=True)

    # PositionHandler========================================================================
    from dali.position_handler import stock_position_handler
    posObj = stock_position_handler.PositionHandler(price_handler=barObj, init_cash=init_cash,
                               store_path=store_path)

    # Portf========================================================================
    from dali.portfolio_handler import equal_wgts_portfolio_handler
    port = equal_wgts_portfolio_handler.EqualWgtsPortfolioHandler(eq,
                                                signal_list,
                                                 store_path=store_path)

    # ========================================================================
    from dali.order_handler import order_handler
    orderObj = order_handler.OrderHandler(eq, price_handler=barObj, 
                                                position_handler=posObj,
                                                init_cash=init_cash, 
                                                store_path=store_path)

    # ========================================================================
    from dali.execution_handler import sim_exec_handler
    seObj = sim_exec_handler.SimExecHandler(eq, barObj, posObj, store_path=store_path)

    # ========================================================================
    eq.register('BAR', signal_obj)

    eq.register('SIGNAL', port)
    eq.register('PORTFOLIO', orderObj)
    eq.register('ORDER', seObj)
    eq.register('FILL', posObj)
    # ========================================================================
    while not barObj.is_iteration_end():
        barObj.update_bar()
        eq.run()
    # ========================================================================
    eq.unregister('BAR', signal_obj)
    eq.unregister('SIGNAL', port)
    eq.unregister('PORTFOLIO', orderObj)
    eq.unregister('ORDER', seObj)
    eq.unregister('FILL', posObj)



    # ========================================================================
    from dali.statistics import benchmark 
    b = benchmark.Benchmark(account_path=store_path)
    b.run()
    # ========================================================================
    from dali.statistics import trades 
    b = trades.Trades(account_path=store_path)
    b.run()
    # ========================================================================
    from dali.statistics import profiling
    pro = profiling.Profiling(account_path=store_path, benchmark='equal_wgts_benchmark')
    pro.run()
    dali_dict.update(pro.profiles)

    # except Exception as e:
    #     print('[kitkit_cal] raise Exception:%s' %e)
    #     dali_dict['dali_cal_error'] = str(e)

    # with open(os.path.join(store_path, 'dali_profiles.yaml'), 'w') as f:
    #     yaml.dump(dali_dict, f, encoding='unicode', sort_keys=False)

    return dali_dict