def get_computed_strategy(self, backtest_settings): ''' return a time series (net asset value) @param backtest_settings: a backtest settings @return: the computed strategy @raise ServiceError: ''' # initialize strategy strategy = Strategy(backtest_settings['name']) # set dates strategy.first_date = datetime.strptime(backtest_settings['first_date'].value, '%Y%m%dT%H:%M:%S').date() strategy.last_date = datetime.strptime(backtest_settings['last_date'].value, '%Y%m%dT%H:%M:%S').date() # set portfolio, first cash flow and currency strategy.portfolio = Portfolio(backtest_settings['currency']) initial_cash_flow = CashFlow(strategy.first_date, backtest_settings['amount'], backtest_settings['currency']) strategy.portfolio.add_transaction(initial_cash_flow) # set rolling strategy.rolling = backtest_settings['rolling'] # add instruments instruments_cache = {} for ticker in backtest_settings['tickers']: first_data_date = strategy.calendar_util.get_n_previous_business_date(strategy.first_date, backtest_settings['needed_depth']) instrument = self.instrument_service.get_by_ticker(ticker, first_data_date, strategy.last_date) financial_instrument = self.get_financial_instrument(instrument) strategy.add_instrument(financial_instrument) # store in cache instruments_cache[ticker] = financial_instrument # add quantity computers computers_cache = {} for computer_type in backtest_settings['quantity_computers']: quantity_computer = self.get_quantity_computer(computer_type, strategy) strategy.add_quantity_computer(quantity_computer) # store in cache computers_cache[computer_type] = quantity_computer # add indicators indicators_cache = {} for indicator_setting in backtest_settings['indicator_settings']: financial_instrument = instruments_cache[indicator_setting['ticker']] indicator = self.get_indicator(indicator_setting, backtest_settings, financial_instrument) indicator.calendar_util = strategy.calendar_util strategy.add_indicator(indicator) # store in cache indicators_cache[indicator.id] = indicator # add trading blocks blocks_cache = {} for trading_block_setting in backtest_settings['trading_block_settings']: block = TradingBlock(trading_block_setting['name']) block.order_type = trading_block_setting['order_type'] block.instrument = instruments_cache[trading_block_setting['ticker']] # store in cache blocks_cache[block.id] = block strategy.add_trading_block(block) # add condition bundle for condition_bundle_setting in backtest_settings['condition_bundle_settings']: current_block = blocks_cache[condition_bundle_setting['trading_block_name']] current_indicator = indicators_cache[condition_bundle_setting['indicator_name'] + current_block.instrument.ticker] strategy.add_condition_bundle_to_trading_block(current_block, condition_bundle_setting['regime_type'], current_indicator) # add quantity computer to blocks for quantity_setting in backtest_settings['quantity_settings']: current_block = blocks_cache[quantity_setting['trading_block_name']] allocation = quantity_setting['allocation'] current_computer = computers_cache[quantity_setting['quantity_computer_type']] strategy.add_quantity_computer_to_trading_block(current_block, current_computer, allocation) try: strategy.compute() except Exception, e: raise ServerError(e.message)