def test_order_add_future(self): portfolio = Portfolio() order = SellOrder('usd', 50.0) portfolio.add_order( datetime.datetime.now() + datetime.timedelta(minutes=5), order) orders = portfolio.get_orders_to_process() self.assertEqual(len(orders), 0) self.assertEqual(len(portfolio.orders), 1)
def test_order_add(self): portfolio = Portfolio() order = SellOrder('usd', 50.0) portfolio.add_order(datetime.datetime.now(), order) orders = portfolio.get_orders_to_process() self.assertEqual(len(orders), 1) self.assertEqual(orders[0], order) self.assertEqual(len(portfolio.orders), 0)
def test_execute_order_multiple(self): portfolio = Portfolio({'usd': 100}) portfolio.execute_order(BuyOrder('ltc_usd'), 25.0) self.assertEqual(portfolio.amount_available('ltc'), 4.0) self.assertEqual(portfolio.amount_available('usd'), 0.0) portfolio.execute_order(SellOrder('ltc_usd'), 30.0) self.assertEqual(portfolio.amount_available('usd'), 120.0) self.assertEqual(portfolio.amount_available('ltc'), 0.0)
def test_order_add_multiple(self): portfolio = Portfolio() order_1 = SellOrder('usd', 50.0) order_2 = BuyOrder('usd', 100.0) time_1 = datetime.datetime.now() portfolio.add_order(time_1, order_1) time_2 = datetime.datetime.now() portfolio.add_order(time_2, order_2) orders = portfolio.get_orders_to_process() self.assertEqual(len(orders), 2) self.assertIn(order_1, orders) self.assertIn(order_2, orders) self.assertEqual(len(portfolio.orders), 0)
def __init__(self, pairs, initial_portfolio=None): self.pairs = pairs self.db = database.MarketDatabase() self.analyzer = analysis.analysis.Analyzer() self.portfolio = Portfolio(initial_portfolio) self.last_prices = {}
class Simulator: def __init__(self, pairs, initial_portfolio=None): self.pairs = pairs self.db = database.MarketDatabase() self.analyzer = analysis.analysis.Analyzer() self.portfolio = Portfolio(initial_portfolio) self.last_prices = {} def simulate(self): data = {} for pair in self.pairs: logger.info("loading data for %s" % pair) ticks = self.db.retrieve_ticks(pair, datetime.datetime(2000, 1, 1), datetime.datetime.now()) ticks = analysis.analysis.filter_repeating_ticks(ticks) data[pair] = ticks logger.info("all data loaded") timestamps = merge_timestamps(data) logger.info("timestamps merged") for (i, t) in enumerate(timestamps): for pair in self.pairs: if not t in [tick['time'] for tick in data[pair]]: continue start = 0 if i > 1000: start = i - 1000 ticks = data[pair][start:i] signals = self.analyzer.analyze(ticks, pair) base, counter = pair.split('_') self.last_prices[pair] = data[pair][i]['last'] if signals: for signal in signals: if isinstance(signal, BuySignal): if self.portfolio.amount_available(counter): self.portfolio.add_order(t, BuyOrder(pair)) self.portfolio.add_order( t + datetime.timedelta(minutes=5), SellOrder(pair)) for order in self.portfolio.get_orders_to_process(t): try: order_base, order_counter = order.pair.split('_') self.portfolio.execute_order( order, self.last_prices[order.pair]) logger.info( "processed %s order %s @ %s - %s and %s now at portfolio for %s" % (order.get_type(), order.pair, self.last_prices[order.pair], self.portfolio.amount_available(order_base), self.portfolio.amount_available(order_counter), order.pair)) except NoFundsException: logger.debug("can't process %s order for %s" % (order.get_type(), pair))
def test_execute_order_no_money_2(self): portfolio = Portfolio({'usd': 100}) self.assertRaises( NoFundsException, lambda: portfolio.execute_order(SellOrder('ltc_usd'), 25.0))
def test_execute_order_no_money(self): portfolio = Portfolio() self.assertRaises( NoFundsException, lambda: portfolio.execute_order(BuyOrder('ltc_usd'), 25.0))
def test_execute_order(self): portfolio = Portfolio({'usd': 100}) portfolio.execute_order(BuyOrder('ltc_usd'), 25.0) self.assertEqual(portfolio.amount_available('ltc'), 4)
def test_portfolio_create(self): portfolio = Portfolio({'usd': 100.0}) self.assertEqual(portfolio.amount_available('usd'), 100.0)
def test_portfolio_subtract(self): portfolio = Portfolio({'usd': 100.0}) portfolio.add_currency('usd', -50.0) self.assertEqual(portfolio.amount_available('usd'), 50.0)
def test_portfolio_add_new(self): portfolio = Portfolio({'usd': 100.0}) portfolio.add_currency('eur', 50.0) self.assertEqual(portfolio.amount_available('eur'), 50.0)