def start(self): """Starts the replay of historical data against the user-defined strategy""" pairs = cfg.pairs() with open(self._prices_file) as file: while True: current_line = file.readline() if not current_line: break timestamp, prices = self.__class__.parse_entry(current_line) if len(prices) != len(pairs): raise RuntimeError( 'The number of prices in this entry ({}) does not match the number of pairs' .format(timestamp)) prices_dict = {pairs[i]: prices[i] for i in range(len(pairs))} results = self._account.auto_close_trades(prices_dict) for trade_id, outcome in results: logging.debug('{} Closed t{} with {}'.format( timestamp, trade_id, outcome)) self._strategy.execute(self._account, timestamp, prices_dict) logging.info('Result: %s' % self._account.net_balance_summary(prices_dict))
def __init__(self, units_per_trade, take_profit_percentage, stop_loss_percentage): super().__init__() self._units_per_trade = units_per_trade self._take_profit_percentage = take_profit_percentage self._stop_loss_percentage = stop_loss_percentage self._pairs = cfg.pairs() self._pair_index = 0
def merge_all_pairs(year, month=None): """Merges the prices of all pairs into one entry per timestamp.""" pairs = cfg.pairs() all_prices_dict = {} for i in range(len(pairs)): price_entries = get_prices(pairs[i], year, month) for entry in price_entries: parts = entry.split(';') timestamp = parts[0] close_price = parts[-2] if timestamp not in all_prices_dict: all_prices_dict[timestamp] = [None] * len(pairs) all_prices_dict[timestamp][i] = close_price timestamps = list(all_prices_dict.keys()) timestamps.sort() return [[timestamp] + all_prices_dict[timestamp] for timestamp in timestamps]
def test_pairs(self): with mock.patch('forex.config.CURRENCIES', [1, 2, 3]): self.assertEqual([(1, 2), (1, 3), (2, 3)], cfg.pairs())