from .helper import * @unittest.skipUnless("acceptance" in limits, "Acceptance skipped") class AcceptanceTest(WebMockTestCase): @unittest.expectedFailure def test_success_sell_only_necessary(self): # FIXME: catch stdout self.m.report.verbose_print = False fetch_balance = { "ETH": { "exchange_free": D("1.0"), "exchange_used": D("0.0"), "exchange_total": D("1.0"), "total": D("1.0"), }, "ETC": { "exchange_free": D("4.0"), "exchange_used": D("0.0"), "exchange_total": D("4.0"), "total": D("4.0"), }, "XVG": { "exchange_free": D("1000.0"), "exchange_used": D("0.0"), "exchange_total": D("1000.0"), "total": D("1000.0"), }, } repartition = { "ETH": (D("0.25"), "long"), "ETC": (D("0.25"), "long"), "BTC": (D("0.4"), "long"), "BTD": (D("0.01"), "short"), "B2X": (D("0.04"), "long"), "USDT": (D("0.05"), "long"), } def fetch_ticker(symbol): if symbol == "ETH/BTC": return { "symbol": "ETH/BTC", "bid": D("0.14"), "ask": D("0.16") } if symbol == "ETC/BTC": return { "symbol": "ETC/BTC", "bid": D("0.002"), "ask": D("0.003") } if symbol == "XVG/BTC": return { "symbol": "XVG/BTC", "bid": D("0.00003"), "ask": D("0.00005") } if symbol == "BTD/BTC": return { "symbol": "BTD/BTC", "bid": D("0.0008"), "ask": D("0.0012") } if symbol == "B2X/BTC": return { "symbol": "B2X/BTC", "bid": D("0.0008"), "ask": D("0.0012") } if symbol == "USDT/BTC": raise helper.ExchangeError if symbol == "BTC/USDT": return { "symbol": "BTC/USDT", "bid": D("14000"), "ask": D("16000") } self.fail("Shouldn't have been called with {}".format(symbol)) market = mock.Mock() market.fetch_all_balances.return_value = fetch_balance market.fetch_ticker.side_effect = fetch_ticker with mock.patch.object(market.Portfolio, "repartition", return_value=repartition): # Action 1 helper.prepare_trades(market) balances = portfolio.BalanceStore.all self.assertEqual(portfolio.Amount("ETH", 1), balances["ETH"].total) self.assertEqual(portfolio.Amount("ETC", 4), balances["ETC"].total) self.assertEqual(portfolio.Amount("XVG", 1000), balances["XVG"].total) trades = portfolio.TradeStore.all self.assertEqual(portfolio.Amount("BTC", D("0.15")), trades[0].value_from) self.assertEqual(portfolio.Amount("BTC", D("0.05")), trades[0].value_to) self.assertEqual("dispose", trades[0].action) self.assertEqual(portfolio.Amount("BTC", D("0.01")), trades[1].value_from) self.assertEqual(portfolio.Amount("BTC", D("0.05")), trades[1].value_to) self.assertEqual("acquire", trades[1].action) self.assertEqual(portfolio.Amount("BTC", D("0.04")), trades[2].value_from) self.assertEqual(portfolio.Amount("BTC", D("0.00")), trades[2].value_to) self.assertEqual("dispose", trades[2].action) self.assertEqual(portfolio.Amount("BTC", D("0.00")), trades[3].value_from) self.assertEqual(portfolio.Amount("BTC", D("-0.002")), trades[3].value_to) self.assertEqual("acquire", trades[3].action) self.assertEqual(portfolio.Amount("BTC", D("0.00")), trades[4].value_from) self.assertEqual(portfolio.Amount("BTC", D("0.008")), trades[4].value_to) self.assertEqual("acquire", trades[4].action) self.assertEqual(portfolio.Amount("BTC", D("0.00")), trades[5].value_from) self.assertEqual(portfolio.Amount("BTC", D("0.01")), trades[5].value_to) self.assertEqual("acquire", trades[5].action) # Action 2 portfolio.TradeStore.prepare_orders(only="dispose", compute_value=lambda x, y: x["bid"] * D("1.001")) all_orders = portfolio.TradeStore.all_orders(state="pending") self.assertEqual(2, len(all_orders)) self.assertEqual(2, 3*all_orders[0].amount.value) self.assertEqual(D("0.14014"), all_orders[0].rate) self.assertEqual(1000, all_orders[1].amount.value) self.assertEqual(D("0.00003003"), all_orders[1].rate) def create_order(symbol, type, action, amount, price=None, account="exchange"): self.assertEqual("limit", type) if symbol == "ETH/BTC": self.assertEqual("sell", action) self.assertEqual(D('0.66666666'), amount) self.assertEqual(D("0.14014"), price) elif symbol == "XVG/BTC": self.assertEqual("sell", action) self.assertEqual(1000, amount) self.assertEqual(D("0.00003003"), price) else: self.fail("I shouldn't have been called") return { "id": symbol, } market.create_order.side_effect = create_order market.order_precision.return_value = 8 # Action 3 portfolio.TradeStore.run_orders() self.assertEqual("open", all_orders[0].status) self.assertEqual("open", all_orders[1].status) market.fetch_order.return_value = { "status": "closed", "datetime": "2018-01-20 13:40:00" } market.privatePostReturnOrderTrades.return_value = [ { "tradeID": 42, "type": "buy", "fee": "0.0015", "date": "2017-12-30 12:00:12", "rate": "0.1", "amount": "10", "total": "1" } ] with mock.patch.object(market.time, "sleep") as sleep: # Action 4 helper.follow_orders(verbose=False) sleep.assert_called_with(30) for order in all_orders: self.assertEqual("closed", order.status) fetch_balance = { "ETH": { "exchange_free": D("1.0") / 3, "exchange_used": D("0.0"), "exchange_total": D("1.0") / 3, "margin_total": 0, "total": D("1.0") / 3, }, "BTC": { "exchange_free": D("0.134"), "exchange_used": D("0.0"), "exchange_total": D("0.134"), "margin_total": 0, "total": D("0.134"), }, "ETC": { "exchange_free": D("4.0"), "exchange_used": D("0.0"), "exchange_total": D("4.0"), "margin_total": 0, "total": D("4.0"), }, "XVG": { "exchange_free": D("0.0"), "exchange_used": D("0.0"), "exchange_total": D("0.0"), "margin_total": 0, "total": D("0.0"), }, } market.fetch_all_balances.return_value = fetch_balance with mock.patch.object(market.Portfolio, "repartition", return_value=repartition): # Action 5 helper.prepare_trades(market, only="acquire", compute_value="average") balances = portfolio.BalanceStore.all self.assertEqual(portfolio.Amount("ETH", 1 / D("3")), balances["ETH"].total) self.assertEqual(portfolio.Amount("ETC", 4), balances["ETC"].total) self.assertEqual(portfolio.Amount("BTC", D("0.134")), balances["BTC"].total) self.assertEqual(portfolio.Amount("XVG", 0), balances["XVG"].total) trades = portfolio.TradeStore.all self.assertEqual(portfolio.Amount("BTC", D("0.15")), trades[0].value_from) self.assertEqual(portfolio.Amount("BTC", D("0.05")), trades[0].value_to) self.assertEqual("dispose", trades[0].action) self.assertEqual(portfolio.Amount("BTC", D("0.01")), trades[1].value_from) self.assertEqual(portfolio.Amount("BTC", D("0.05")), trades[1].value_to) self.assertEqual("acquire", trades[1].action) self.assertNotIn("BTC", trades) self.assertEqual(portfolio.Amount("BTC", D("0.04")), trades[2].value_from) self.assertEqual(portfolio.Amount("BTC", D("0.00")), trades[2].value_to) self.assertEqual("dispose", trades[2].action) self.assertEqual(portfolio.Amount("BTC", D("0.00")), trades[3].value_from) self.assertEqual(portfolio.Amount("BTC", D("-0.002")), trades[3].value_to) self.assertEqual("acquire", trades[3].action) self.assertEqual(portfolio.Amount("BTC", D("0.00")), trades[4].value_from) self.assertEqual(portfolio.Amount("BTC", D("0.008")), trades[4].value_to) self.assertEqual("acquire", trades[4].action) self.assertEqual(portfolio.Amount("BTC", D("0.00")), trades[5].value_from) self.assertEqual(portfolio.Amount("BTC", D("0.01")), trades[5].value_to) self.assertEqual("acquire", trades[5].action) # Action 6 portfolio.TradeStore.prepare_orders(only="acquire", compute_value=lambda x, y: x["ask"]) all_orders = portfolio.TradeStore.all_orders(state="pending") self.assertEqual(4, len(all_orders)) self.assertEqual(portfolio.Amount("ETC", D("12.83333333")), round(all_orders[0].amount)) self.assertEqual(D("0.003"), all_orders[0].rate) self.assertEqual("buy", all_orders[0].action) self.assertEqual("long", all_orders[0].trade_type) self.assertEqual(portfolio.Amount("BTD", D("1.61666666")), round(all_orders[1].amount)) self.assertEqual(D("0.0012"), all_orders[1].rate) self.assertEqual("sell", all_orders[1].action) self.assertEqual("short", all_orders[1].trade_type) diff = portfolio.Amount("B2X", D("19.4")/3) - all_orders[2].amount self.assertAlmostEqual(0, diff.value) self.assertEqual(D("0.0012"), all_orders[2].rate) self.assertEqual("buy", all_orders[2].action) self.assertEqual("long", all_orders[2].trade_type) self.assertEqual(portfolio.Amount("BTC", D("0.0097")), all_orders[3].amount) self.assertEqual(D("16000"), all_orders[3].rate) self.assertEqual("sell", all_orders[3].action) self.assertEqual("long", all_orders[3].trade_type) # Action 6b # TODO: # Move balances to margin # Action 7 # TODO # portfolio.TradeStore.run_orders() with mock.patch.object(market.time, "sleep") as sleep: # Action 8 helper.follow_orders(verbose=False) sleep.assert_called_with(30)