Esempio n. 1
0
    def __init__(self, strategies, data_dir, start_automatically=True):

        if not strategies:
            raise ValueError("Empty strategies list in your algo file.")

        # Single market object will be used for all backtesting instances
        self.market = BacktestMarketData(
            data_dir,  #should come from script loader
            strategies[0].SYMBOL_LIST,
            date_from=getattr(strategies[0], 'DATE_FROM',
                              datetime.now() - timedelta(weeks=52)),
            date_to=getattr(strategies[0], 'DATE_TO', datetime.now()),
            normalize_prices=getattr(strategies[0], 'NORMALIZE_PRICES',
                                     settings.NORMALIZE_PRICES),
            normalize_volume=getattr(strategies[0], 'NORMALIZE_VOLUME',
                                     settings.NORMALIZE_VOLUME),
            round_decimals=getattr(strategies[0], 'ROUND_DECIMALS',
                                   settings.ROUND_DECIMALS),
        )

        self.portfolios = []

        for strategy in strategies:
            port = BacktestPortfolio(self.market, strategy)
            self.portfolios.append(port)

        logging.info("Backtesting {} {} with {} symbols from {} to {}".format(
            len(self.portfolios),
            'strategies' if len(self.portfolios) > 1 else 'strategy',
            len(self.market.symbol_list),
            self.market.date_from.strftime('%Y-%m-%d'),
            self.market.date_to.strftime('%Y-%m-%d'),
        ))
        logging.info("Data load took {}".format(str(self.market.load_time)))

        if start_automatically:
            self.start()
            self.calc_performance()