예제 #1
0
    def load_strategy(self):
        for s in self._config_client['strategy']:
            strategyClass = strategy_list.get(s, None)
            if not strategyClass:
                print(u'can not find strategy:%s' % s)
            else:
                strategy = strategyClass(self._outgoing_request_event_engine, self._data_board)
                strategy.id = self._strategy_id
                strategy.name = s          # assign class name to the strategy

                # init strategy
                strategy.on_init(self._config_client['strategy'][s])
                for sym in strategy.symbols:
                    if sym in self._tick_strategy_dict:
                        self._tick_strategy_dict[sym].append(self._strategy_id)
                    else:
                        self._tick_strategy_dict[sym] = [self._strategy_id]

                strategy.active = False
                self._strategy_dict[self._strategy_id] = strategy
                self._strategy_id = self._strategy_id+1
예제 #2
0
    def __init__(self, config):
        """
        1. read in configs
        2. Set up backtest event engine
        """
        self._current_time = Timestamp('1900-01-01')

        ## 0. read in configs
        self._initial_cash = config['cash']
        self._symbols = config['tickers']
        self._benchmark = config['benchmark']
        #self.start_date = datetime.datetime.strptime(config['start_date'], "%Y-%m-%d")
        #self.end_date = datetime.datetime.strptime(config['end_date'], "%Y-%m-%d")
        start_date = config['start_date']
        send_date = config['end_date']
        strategy_name = config['strategy']
        datasource = str(config['datasource'])
        self._hist_dir = config['hist_dir']
        self._output_dir = config['output_dir']

        ## 1. data_feed
        symbols_all = self._symbols[:]  # copy
        if self._benchmark is not None:
            symbols_all.append(self._benchmark)
        self._symbols = [str(s) for s in self._symbols]
        symbols_all = [str(s) for s in symbols_all]

        if (datasource.upper() == 'LOCAL'):
            self._data_feed = BacktestDataFeedLocal(hist_dir=self._hist_dir,
                                                    start_date=start_date,
                                                    end_date=send_date)
        elif (datasource.upper() == 'TUSHARE'):
            self._data_feed = BacktestDataFeedTushare(start_date=start_date,
                                                      end_date=send_date)
        else:
            self._data_feed = BacktestDataFeedQuandl(start_date=start_date,
                                                     end_date=send_date)

        self._data_feed.subscribe_market_data(symbols_all)

        ## 2. event engine
        self._events_engine = BacktestEventEngine(self._data_feed)

        ## 3. brokerage
        self._data_board = DataBoard()
        self._backtest_brokerage = BacktestBrokerage(self._events_engine,
                                                     self._data_board)

        ## 4. portfolio_manager
        self._portfolio_manager = PortfolioManager(self._initial_cash)

        ## 5. performance_manager
        self._performance_manager = PerformanceManager(symbols_all)

        ## 6. risk_manager
        self._risk_manager = PassThroughRiskManager()

        ## 7. load all strategies
        strategyClass = strategy_list.get(strategy_name, None)
        if not strategyClass:
            print(u'can not find strategy:%s' % strategy_name)
            return
        self._strategy = strategyClass(self._symbols, self._events_engine)
        self._strategy.on_init()
        self._strategy.on_start()

        ## 8. trade recorder
        #self._trade_recorder = ExampleTradeRecorder(output_dir)

        ## 9. wire up event handlers
        self._events_engine.register_handler(EventType.TICK,
                                             self._tick_event_handler)
        self._events_engine.register_handler(EventType.BAR,
                                             self._bar_event_handler)
        self._events_engine.register_handler(EventType.ORDER,
                                             self._order_event_handler)
        self._events_engine.register_handler(EventType.FILL,
                                             self._fill_event_handler)
    def __init__(self, config):
        """
        1. read in configs
        2. Set up backtest event engine
        """
        self._current_time = None

        ## 0. read in configs
        self._initial_cash = config['cash']
        self._symbols = config['symbols']
        self._benchmark = config['benchmark']
        #self.start_date = datetime.datetime.strptime(config['start_date'], "%Y-%m-%d")
        #self.end_date = datetime.datetime.strptime(config['end_date'], "%Y-%m-%d")
        start_date = config['start_date']
        send_date = config['end_date']
        params = config['params']
        strategy_name = config['strategy']
        datasource = str(config['datasource'])
        batch_tag = config['batch_tag']
        root_multiplier = config['root_multiplier']
        self._hist_dir = config['hist_dir']
        self._fvp_file = config['fvp_file']
        self._output_dir = config['output_dir']

        ## 1. data_feed
        symbols_all = self._symbols[:]   # copy
        if self._benchmark is not None:
            symbols_all.append(self._benchmark)
        self._symbols = [str(s) for s in self._symbols]
        symbols_all = set([str(s) for s in symbols_all])  # remove duplicates

        if (datasource.upper() == 'LOCAL'):
            print('Using local single symbol data feed')
            self._data_feed = BacktestDataFeedLocalSingleSymbol(
                hist_dir=self._hist_dir,
                start_date=start_date, end_date=send_date
            )
        elif (datasource.upper() == 'MULTI_LOCAL'):
            print('Using local multiple symbol data feed')
            self._data_feed = BacktestDataFeedLocalMultipleSymbols(
                hist_dir=self._hist_dir,
                start_date=start_date, end_date=send_date
            )
        else:
            print('Using Quandl data feed')
            self._data_feed = BacktestDataFeedQuandl(
                start_date=start_date, end_date=send_date
            )

        self._data_feed.subscribe_market_data(self._symbols)         # not symbols_all

        ## 2. event engine
        self._events_engine = BacktestEventEngine(self._data_feed)

        ## 3. brokerage
        self._data_board = DataBoard(hist_dir=self._hist_dir, syms=symbols_all)
        self._backtest_brokerage = BacktestBrokerage(
            self._events_engine, self._data_board
        )

        ## 4. portfolio_manager
        self._df_fvp = None
        if self._fvp_file is not None:
            self._df_fvp = pd.read_csv(self._hist_dir+self._fvp_file, index_col=0)

        self._portfolio_manager = PortfolioManager(self._initial_cash, self._df_fvp)

        ## 5. performance_manager
        self._performance_manager = PerformanceManager(self._symbols, self._benchmark, batch_tag, root_multiplier, self._df_fvp)

        ## 6. risk_manager
        self._risk_manager = PassThroughRiskManager()

        ## 7. load all strategies
        strategyClass = strategy_list.get(strategy_name, None)
        if not strategyClass:
            print(u'can not find strategy:%s' % strategy_name)
            return
        else:
            print(u'backtesting strategy:%s' % strategy_name)
        self._strategy = strategyClass(self._events_engine, self._data_board)
        self._strategy.set_symbols(self._symbols)
        self._strategy.set_capital(self._initial_cash)
        self._strategy.on_init(params)
        self._strategy.on_start()

        ## 8. trade recorder
        #self._trade_recorder = ExampleTradeRecorder(output_dir)

        ## 9. wire up event handlers
        self._events_engine.register_handler(EventType.TICK, self._tick_event_handler)
        self._events_engine.register_handler(EventType.BAR, self._bar_event_handler)
        self._events_engine.register_handler(EventType.ORDER, self._order_event_handler)
        self._events_engine.register_handler(EventType.FILL, self._fill_event_handler)
예제 #4
0
    def __init__(self, config):
        """
        1. read in configs
        2. Set up backtest event engine
        """
        self._current_time = None

        ## 0. read in configs
        self._initial_cash = config['cash']
        self._symbols = config['symbols']
        self._benchmark = config['benchmark']
        #self.start_date = datetime.datetime.strptime(config['start_date'], "%Y-%m-%d")
        #self.end_date = datetime.datetime.strptime(config['end_date'], "%Y-%m-%d")
        start_date = config['start_date']
        send_date = config['end_date']
        params = config['params']
        strategy_name = config['strategy']
        datasource = str(config['datasource'])
        batch_tag = config['batch_tag']
        root_multiplier = config['root_multiplier']
        self._hist_dir = config['hist_dir']
        self._fvp_file = config['fvp_file']
        self._output_dir = config['output_dir']

        ## 1. data_feed
        symbols_all = self._symbols[:]  # copy
        if self._benchmark is not None:
            symbols_all.append(self._benchmark)
        self._symbols = [str(s) for s in self._symbols]
        symbols_all = set([str(s) for s in symbols_all])  # remove duplicates

        if (datasource.upper() == 'LOCAL'):
            print('Using local single symbol data feed')
            self._data_feed = BacktestDataFeedLocalSingleSymbol(
                hist_dir=self._hist_dir,
                start_date=start_date,
                end_date=send_date)
        elif (datasource.upper() == 'MULTI_LOCAL'):
            print('Using local multiple symbol data feed')
            self._data_feed = BacktestDataFeedLocalMultipleSymbols(
                hist_dir=self._hist_dir,
                start_date=start_date,
                end_date=send_date)
        else:
            print('Using Quandl data feed')
            self._data_feed = BacktestDataFeedQuandl(start_date=start_date,
                                                     end_date=send_date)

        self._data_feed.subscribe_market_data(self._symbols)  # not symbols_all

        ## 2. event engine
        self._events_engine = BacktestEventEngine(self._data_feed)

        ## 3. brokerage
        self._data_board = DataBoard(hist_dir=self._hist_dir, syms=symbols_all)
        self._backtest_brokerage = BacktestBrokerage(self._events_engine,
                                                     self._data_board)

        ## 4. portfolio_manager
        self._df_fvp = None
        if self._fvp_file is not None:
            self._df_fvp = pd.read_csv(self._hist_dir + self._fvp_file,
                                       index_col=0)

        self._portfolio_manager = PortfolioManager(self._initial_cash,
                                                   self._df_fvp)

        ## 5. performance_manager
        self._performance_manager = PerformanceManager(self._symbols,
                                                       self._benchmark,
                                                       batch_tag,
                                                       root_multiplier,
                                                       self._df_fvp)

        ## 6. risk_manager
        self._risk_manager = PassThroughRiskManager()

        ## 7. load all strategies
        strategyClass = strategy_list.get(strategy_name, None)
        if not strategyClass:
            print(u'can not find strategy:%s' % strategy_name)
            return
        else:
            print(u'backtesting strategy:%s' % strategy_name)
        self._strategy = strategyClass(self._events_engine, self._data_board)
        self._strategy.set_symbols(self._symbols)
        self._strategy.set_capital(self._initial_cash)
        self._strategy.on_init(params)
        self._strategy.on_start()

        ## 8. trade recorder
        #self._trade_recorder = ExampleTradeRecorder(output_dir)

        ## 9. wire up event handlers
        self._events_engine.register_handler(EventType.TICK,
                                             self._tick_event_handler)
        self._events_engine.register_handler(EventType.BAR,
                                             self._bar_event_handler)
        self._events_engine.register_handler(EventType.ORDER,
                                             self._order_event_handler)
        self._events_engine.register_handler(EventType.FILL,
                                             self._fill_event_handler)