X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=test.py;h=6e2747583b8c667f86e49c00932764c58aeb74c1;hb=80cdd672da2f0a4997a792bd1a2de19d4f516e5b;hp=1b8a109ce64d6286541c88a86b86ccc0d288638d;hpb=b83d489771c1c4f482a711cffc572f23a1599b4d;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FTrader.git diff --git a/test.py b/test.py index 1b8a109..6e27475 100644 --- a/test.py +++ b/test.py @@ -2,8 +2,140 @@ import portfolio import unittest from decimal import Decimal as D from unittest import mock +import requests +import requests_mock -class AmountTest(unittest.TestCase): +class WebMockTestCase(unittest.TestCase): + import time + + def setUp(self): + super(WebMockTestCase, self).setUp() + self.wm = requests_mock.Mocker() + self.wm.start() + + self.patchers = [ + mock.patch.multiple(portfolio.Balance, known_balances={}), + mock.patch.multiple(portfolio.Portfolio, data=None, liquidities={}), + mock.patch.multiple(portfolio.Trade, + ticker_cache={}, + ticker_cache_timestamp=self.time.time(), + fees_cache={}, + trades={}), + mock.patch.multiple(portfolio.Computation, + computations=portfolio.Computation.computations) + ] + for patcher in self.patchers: + patcher.start() + + + def tearDown(self): + for patcher in self.patchers: + patcher.stop() + self.wm.stop() + super(WebMockTestCase, self).tearDown() + +class PortfolioTest(WebMockTestCase): + def fill_data(self): + if self.json_response is not None: + portfolio.Portfolio.data = self.json_response + + def setUp(self): + super(PortfolioTest, self).setUp() + + with open("test_portfolio.json") as example: + self.json_response = example.read() + + self.wm.get(portfolio.Portfolio.URL, text=self.json_response) + + def test_get_cryptoportfolio(self): + self.wm.get(portfolio.Portfolio.URL, [ + {"text":'{ "foo": "bar" }', "status_code": 200}, + {"text": "System Error", "status_code": 500}, + {"exc": requests.exceptions.ConnectTimeout}, + ]) + portfolio.Portfolio.get_cryptoportfolio() + self.assertIn("foo", portfolio.Portfolio.data) + self.assertEqual("bar", portfolio.Portfolio.data["foo"]) + self.assertTrue(self.wm.called) + self.assertEqual(1, self.wm.call_count) + + portfolio.Portfolio.get_cryptoportfolio() + self.assertIsNone(portfolio.Portfolio.data) + self.assertEqual(2, self.wm.call_count) + + portfolio.Portfolio.data = "Foo" + portfolio.Portfolio.get_cryptoportfolio() + self.assertEqual("Foo", portfolio.Portfolio.data) + self.assertEqual(3, self.wm.call_count) + + def test_parse_cryptoportfolio(self): + portfolio.Portfolio.parse_cryptoportfolio() + + self.assertListEqual( + ["medium", "high"], + list(portfolio.Portfolio.liquidities.keys())) + + liquidities = portfolio.Portfolio.liquidities + self.assertEqual(10, len(liquidities["medium"].keys())) + self.assertEqual(10, len(liquidities["high"].keys())) + + expected = { + 'BTC': (D("0.2857"), "long"), + 'DGB': (D("0.1015"), "long"), + 'DOGE': (D("0.1805"), "long"), + 'SC': (D("0.0623"), "long"), + 'ZEC': (D("0.3701"), "long"), + } + self.assertDictEqual(expected, liquidities["high"]['2018-01-08']) + + expected = { + 'BTC': (D("1.1102e-16"), "long"), + 'ETC': (D("0.1"), "long"), + 'FCT': (D("0.1"), "long"), + 'GAS': (D("0.1"), "long"), + 'NAV': (D("0.1"), "long"), + 'OMG': (D("0.1"), "long"), + 'OMNI': (D("0.1"), "long"), + 'PPC': (D("0.1"), "long"), + 'RIC': (D("0.1"), "long"), + 'VIA': (D("0.1"), "long"), + 'XCP': (D("0.1"), "long"), + } + self.assertDictEqual(expected, liquidities["medium"]['2018-01-08']) + + # It doesn't refetch the data when available + portfolio.Portfolio.parse_cryptoportfolio() + + self.assertEqual(1, self.wm.call_count) + + def test_repartition(self): + expected_medium = { + 'BTC': (D("1.1102e-16"), "long"), + 'USDT': (D("0.1"), "long"), + 'ETC': (D("0.1"), "long"), + 'FCT': (D("0.1"), "long"), + 'OMG': (D("0.1"), "long"), + 'STEEM': (D("0.1"), "long"), + 'STRAT': (D("0.1"), "long"), + 'XEM': (D("0.1"), "long"), + 'XMR': (D("0.1"), "long"), + 'XVC': (D("0.1"), "long"), + 'ZRX': (D("0.1"), "long"), + } + expected_high = { + 'USDT': (D("0.1226"), "long"), + 'BTC': (D("0.1429"), "long"), + 'ETC': (D("0.1127"), "long"), + 'ETH': (D("0.1569"), "long"), + 'FCT': (D("0.3341"), "long"), + 'GAS': (D("0.1308"), "long"), + } + + self.assertEqual(expected_medium, portfolio.Portfolio.repartition()) + self.assertEqual(expected_medium, portfolio.Portfolio.repartition(liquidity="medium")) + self.assertEqual(expected_high, portfolio.Portfolio.repartition(liquidity="high")) + +class AmountTest(WebMockTestCase): def test_values(self): amount = portfolio.Amount("BTC", "0.65") self.assertEqual(D("0.65"), amount.value) @@ -43,6 +175,11 @@ class AmountTest(unittest.TestCase): converted_amount = amount.in_currency("ETH", None, rate=D("0.02")) self.assertEqual(D("0.2"), converted_amount.value) + def test__round(self): + amount = portfolio.Amount("BAR", portfolio.D("1.23456789876")) + self.assertEqual(D("1.23456789"), round(amount).value) + self.assertEqual(D("1.23"), round(amount, 2).value) + def test__abs(self): amount = portfolio.Amount("SC", -120) self.assertEqual(120, abs(amount).value) @@ -108,7 +245,7 @@ class AmountTest(unittest.TestCase): self.assertEqual(D("5.5"), (amount / 2).value) self.assertEqual(D("4.4"), (amount / D("2.5")).value) - def test__div(self): + def test__truediv(self): amount = portfolio.Amount("XEM", 11) self.assertEqual(D("5.5"), (amount / 2).value) @@ -126,6 +263,42 @@ class AmountTest(unittest.TestCase): with self.assertRaises(Exception): amount1 < amount3 + def test__le(self): + amount1 = portfolio.Amount("BTD", 11.3) + amount2 = portfolio.Amount("BTD", 13.1) + + self.assertTrue(amount1 <= amount2) + self.assertFalse(amount2 <= amount1) + self.assertTrue(amount1 <= amount1) + + amount3 = portfolio.Amount("BTC", 1.6) + with self.assertRaises(Exception): + amount1 <= amount3 + + def test__gt(self): + amount1 = portfolio.Amount("BTD", 11.3) + amount2 = portfolio.Amount("BTD", 13.1) + + self.assertTrue(amount2 > amount1) + self.assertFalse(amount1 > amount2) + self.assertFalse(amount1 > amount1) + + amount3 = portfolio.Amount("BTC", 1.6) + with self.assertRaises(Exception): + amount3 > amount1 + + def test__ge(self): + amount1 = portfolio.Amount("BTD", 11.3) + amount2 = portfolio.Amount("BTD", 13.1) + + self.assertTrue(amount2 >= amount1) + self.assertFalse(amount1 >= amount2) + self.assertTrue(amount1 >= amount1) + + amount3 = portfolio.Amount("BTC", 1.6) + with self.assertRaises(Exception): + amount3 >= amount1 + def test__eq(self): amount1 = portfolio.Amount("BTD", 11.3) amount2 = portfolio.Amount("BTD", 13.1) @@ -143,6 +316,28 @@ class AmountTest(unittest.TestCase): amount5 = portfolio.Amount("BTD", 0) self.assertTrue(amount5 == 0) + def test__ne(self): + amount1 = portfolio.Amount("BTD", 11.3) + amount2 = portfolio.Amount("BTD", 13.1) + amount3 = portfolio.Amount("BTD", 11.3) + + self.assertTrue(amount1 != amount2) + self.assertTrue(amount2 != amount1) + self.assertFalse(amount1 != amount3) + self.assertTrue(amount2 != 0) + + amount4 = portfolio.Amount("BTC", 1.6) + with self.assertRaises(Exception): + amount1 != amount4 + + amount5 = portfolio.Amount("BTD", 0) + self.assertFalse(amount5 != 0) + + def test__neg(self): + amount1 = portfolio.Amount("BTD", "11.3") + + self.assertEqual(portfolio.D("-11.3"), (-amount1).value) + def test__str(self): amount1 = portfolio.Amount("BTX", 32) self.assertEqual("32.00000000 BTX", str(amount1)) @@ -163,91 +358,7 @@ class AmountTest(unittest.TestCase): amount2.linked_to = amount3 self.assertEqual("Amount(32.00000000 BTX -> Amount(12000.00000000 USDT -> Amount(0.10000000 BTC)))", repr(amount1)) -class PortfolioTest(unittest.TestCase): - import urllib3 - def fill_data(self): - if self.json_response is not None: - portfolio.Portfolio.data = self.json_response - - def setUp(self): - super(PortfolioTest, self).setUp() - - with open("test_portfolio.json") as example: - import json - self.json_response = json.load(example) - - self.patcher = mock.patch.multiple(portfolio.Portfolio, data=None, liquidities={}) - self.patcher.start() - - @mock.patch.object(urllib3, "disable_warnings") - @mock.patch.object(urllib3.poolmanager.PoolManager, "request") - @mock.patch.object(portfolio.Portfolio, "URL", new="foo://bar") - def test_get_cryptoportfolio(self, request, disable_warnings): - request.side_effect = [ - type('', (), { "data": '{ "foo": "bar" }' }), - type('', (), { "data": 'System Error' }), - Exception("Connection error"), - ] - - portfolio.Portfolio.get_cryptoportfolio() - self.assertIn("foo", portfolio.Portfolio.data) - self.assertEqual("bar", portfolio.Portfolio.data["foo"]) - request.assert_called_with("GET", "foo://bar") - - request.reset_mock() - portfolio.Portfolio.get_cryptoportfolio() - self.assertIsNone(portfolio.Portfolio.data) - request.assert_called_with("GET", "foo://bar") - - request.reset_mock() - portfolio.Portfolio.data = "foo" - portfolio.Portfolio.get_cryptoportfolio() - request.assert_called_with("GET", "foo://bar") - self.assertEqual("foo", portfolio.Portfolio.data) - disable_warnings.assert_called_with() - - @mock.patch.object(portfolio.Portfolio, "get_cryptoportfolio") - def test_parse_cryptoportfolio(self, mock_get): - mock_get.side_effect = self.fill_data - - portfolio.Portfolio.parse_cryptoportfolio() - - self.assertListEqual( - ["medium", "high"], - list(portfolio.Portfolio.liquidities.keys())) - - liquidities = portfolio.Portfolio.liquidities - self.assertEqual(10, len(liquidities["medium"].keys())) - self.assertEqual(10, len(liquidities["high"].keys())) - - expected = {'BTC': 2857, 'DGB': 1015, 'DOGE': 1805, 'SC': 623, 'ZEC': 3701} - self.assertDictEqual(expected, liquidities["high"]['2018-01-08']) - - expected = {'ETC': 1000, 'FCT': 1000, 'GAS': 1000, 'NAV': 1000, 'OMG': 1000, 'OMNI': 1000, 'PPC': 1000, 'RIC': 1000, 'VIA': 1000, 'XCP': 1000} - self.assertDictEqual(expected, liquidities["medium"]['2018-01-08']) - - # It doesn't refetch the data when available - portfolio.Portfolio.parse_cryptoportfolio() - mock_get.assert_called_once_with() - - portfolio.Portfolio.data["portfolio_1"]["holding"]["direction"][3] = "short" - self.assertRaises(AssertionError, portfolio.Portfolio.parse_cryptoportfolio) - - @mock.patch.object(portfolio.Portfolio, "get_cryptoportfolio") - def test_repartition_pertenthousand(self, mock_get): - mock_get.side_effect = self.fill_data - - expected_medium = {'USDT': 1000, 'ETC': 1000, 'FCT': 1000, 'OMG': 1000, 'STEEM': 1000, 'STRAT': 1000, 'XEM': 1000, 'XMR': 1000, 'XVC': 1000, 'ZRX': 1000} - expected_high = {'USDT': 1226, 'BTC': 1429, 'ETC': 1127, 'ETH': 1569, 'FCT': 3341, 'GAS': 1308} - - self.assertEqual(expected_medium, portfolio.Portfolio.repartition_pertenthousand()) - self.assertEqual(expected_medium, portfolio.Portfolio.repartition_pertenthousand(liquidity="medium")) - self.assertEqual(expected_high, portfolio.Portfolio.repartition_pertenthousand(liquidity="high")) - - def tearDown(self): - self.patcher.stop() - -class BalanceTest(unittest.TestCase): +class BalanceTest(WebMockTestCase): def setUp(self): super(BalanceTest, self).setUp() @@ -277,27 +388,54 @@ class BalanceTest(unittest.TestCase): "total": 0.0 }, } - self.patcher = mock.patch.multiple(portfolio.Balance, known_balances={}) - self.patcher.start() def test_values(self): - balance = portfolio.Balance("BTC", 0.65, 0.35, 0.30) - self.assertEqual(0.65, balance.total.value) - self.assertEqual(0.35, balance.free.value) - self.assertEqual(0.30, balance.used.value) - self.assertEqual("BTC", balance.currency) + balance = portfolio.Balance("BTC", { + "exchange_total": "0.65", + "exchange_free": "0.35", + "exchange_used": "0.30", + "margin_total": "-10", + "margin_borrowed": "-10", + "margin_free": "0", + "margin_position_type": "short", + "margin_borrowed_base_currency": "USDT", + "margin_liquidation_price": "1.20", + "margin_pending_gain": "10", + "margin_lending_fees": "0.4", + "margin_borrowed_base_price": "0.15", + }) + self.assertEqual(portfolio.D("0.65"), balance.exchange_total.value) + self.assertEqual(portfolio.D("0.35"), balance.exchange_free.value) + self.assertEqual(portfolio.D("0.30"), balance.exchange_used.value) + self.assertEqual("BTC", balance.exchange_total.currency) + self.assertEqual("BTC", balance.exchange_free.currency) + self.assertEqual("BTC", balance.exchange_total.currency) + + self.assertEqual(portfolio.D("-10"), balance.margin_total.value) + self.assertEqual(portfolio.D("-10"), balance.margin_borrowed.value) + self.assertEqual(portfolio.D("0"), balance.margin_free.value) + self.assertEqual("BTC", balance.margin_total.currency) + self.assertEqual("BTC", balance.margin_borrowed.currency) + self.assertEqual("BTC", balance.margin_free.currency) - balance = portfolio.Balance.from_hash("BTC", { "total": 0.65, "free": 0.35, "used": 0.30}) - self.assertEqual(0.65, balance.total.value) - self.assertEqual(0.35, balance.free.value) - self.assertEqual(0.30, balance.used.value) self.assertEqual("BTC", balance.currency) + self.assertEqual(portfolio.D("0.4"), balance.margin_lending_fees.value) + self.assertEqual("USDT", balance.margin_lending_fees.currency) + @mock.patch.object(portfolio.Trade, "get_ticker") def test_in_currency(self, get_ticker): portfolio.Balance.known_balances = { - "BTC": portfolio.Balance("BTC", "0.65", "0.35", "0.30"), - "ETH": portfolio.Balance("ETH", 3, 3, 0), + "BTC": portfolio.Balance("BTC", { + "total": "0.65", + "exchange_total":"0.65", + "exchange_free": "0.35", + "exchange_used": "0.30"}), + "ETH": portfolio.Balance("ETH", { + "total": 3, + "exchange_total": 3, + "exchange_free": 3, + "exchange_used": 0}), } market = mock.Mock() get_ticker.return_value = { @@ -315,17 +453,26 @@ class BalanceTest(unittest.TestCase): self.assertEqual(D("0.65"), amounts["BTC"].value) self.assertEqual(D("0.27"), amounts["ETH"].value) - amounts = portfolio.Balance.in_currency("BTC", market, compute_value="bid", type="used") + amounts = portfolio.Balance.in_currency("BTC", market, compute_value="bid", type="exchange_used") self.assertEqual(D("0.30"), amounts["BTC"].value) self.assertEqual(0, amounts["ETH"].value) def test_currencies(self): portfolio.Balance.known_balances = { - "BTC": portfolio.Balance("BTC", "0.65", "0.35", "0.30"), - "ETH": portfolio.Balance("ETH", 3, 3, 0), + "BTC": portfolio.Balance("BTC", { + "total": "0.65", + "exchange_total":"0.65", + "exchange_free": "0.35", + "exchange_used": "0.30"}), + "ETH": portfolio.Balance("ETH", { + "total": 3, + "exchange_total": 3, + "exchange_free": 3, + "exchange_used": 0}), } self.assertListEqual(["BTC", "ETH"], list(portfolio.Balance.currencies())) + @unittest.expectedFailure @mock.patch.object(portfolio.market, "fetch_balance") def test_fetch_balances(self, fetch_balance): fetch_balance.return_value = self.fetch_balance @@ -339,7 +486,8 @@ class BalanceTest(unittest.TestCase): self.assertEqual(0, portfolio.Balance.known_balances["ETC"].total) self.assertListEqual(["USDT", "XVG", "ETC"], list(portfolio.Balance.currencies())) - @mock.patch.object(portfolio.Portfolio, "repartition_pertenthousand") + @unittest.expectedFailure + @mock.patch.object(portfolio.Portfolio, "repartition") @mock.patch.object(portfolio.market, "fetch_balance") def test_dispatch_assets(self, fetch_balance, repartition): fetch_balance.return_value = self.fetch_balance @@ -348,8 +496,8 @@ class BalanceTest(unittest.TestCase): self.assertNotIn("XEM", portfolio.Balance.currencies()) repartition.return_value = { - "XEM": 7500, - "BTC": 2600, + "XEM": (D("0.75"), "long"), + "BTC": (D("0.26"), "long"), } amounts = portfolio.Balance.dispatch_assets(portfolio.Amount("BTC", "10.1")) @@ -357,13 +505,14 @@ class BalanceTest(unittest.TestCase): self.assertEqual(D("2.6"), amounts["BTC"].value) self.assertEqual(D("7.5"), amounts["XEM"].value) - @mock.patch.object(portfolio.Portfolio, "repartition_pertenthousand") + @unittest.expectedFailure + @mock.patch.object(portfolio.Portfolio, "repartition") @mock.patch.object(portfolio.Trade, "get_ticker") @mock.patch.object(portfolio.Trade, "compute_trades") def test_prepare_trades(self, compute_trades, get_ticker, repartition): repartition.return_value = { - "XEM": 7500, - "BTC": 2500, + "XEM": (D("0.75"), "long"), + "BTC": (D("0.25"), "long"), } def _get_ticker(c1, c2, market): if c1 == "USDT" and c2 == "BTC": @@ -402,34 +551,21 @@ class BalanceTest(unittest.TestCase): def test_update_trades(self): pass + @unittest.expectedFailure def test__repr(self): balance = portfolio.Balance("BTX", 3, 1, 2) self.assertEqual("Balance(BTX [1.00000000 BTX/2.00000000 BTX/3.00000000 BTX])", repr(balance)) - def tearDown(self): - self.patcher.stop() - -class TradeTest(unittest.TestCase): - import time - - def setUp(self): - super(TradeTest, self).setUp() - - self.patcher = mock.patch.multiple(portfolio.Trade, - ticker_cache={}, - ticker_cache_timestamp=self.time.time(), - fees_cache={}, - trades={}) - self.patcher.start() +class TradeTest(WebMockTestCase): def test_get_ticker(self): market = mock.Mock() market.fetch_ticker.side_effect = [ { "bid": 1, "ask": 3 }, - portfolio.ccxt.ExchangeError("foo"), + portfolio.ExchangeError("foo"), { "bid": 10, "ask": 40 }, - portfolio.ccxt.ExchangeError("foo"), - portfolio.ccxt.ExchangeError("foo"), + portfolio.ExchangeError("foo"), + portfolio.ExchangeError("foo"), ] ticker = portfolio.Trade.get_ticker("ETH", "ETC", market) @@ -489,7 +625,21 @@ class TradeTest(unittest.TestCase): @unittest.skip("TODO") def test_values_assertion(self): - pass + value_from = Amount("BTC", "1.0") + value_from.linked_to = Amount("ETH", "10.0") + value_to = Amount("BTC", "1.0") + trade = portfolioTrade(value_from, value_to, "ETH") + self.assertEqual("BTC", trade.base_currency) + self.assertEqual("ETH", trade.currency) + + with self.assertRaises(AssertionError): + portfolio.Trade(value_from, value_to, "ETC") + with self.assertRaises(AssertionError): + value_from.linked_to = None + portfolio.Trade(value_from, value_to, "ETH") + with self.assertRaises(AssertionError): + value_from.currency = "ETH" + portfolio.Trade(value_from, value_to, "ETH") @unittest.skip("TODO") def test_fetch_fees(self): @@ -531,29 +681,8 @@ class TradeTest(unittest.TestCase): def test__repr(self): pass - def tearDown(self): - self.patcher.stop() - -class AcceptanceTest(unittest.TestCase): - import time - - def setUp(self): - super(AcceptanceTest, self).setUp() - - self.patchers = [ - mock.patch.multiple(portfolio.Balance, known_balances={}), - mock.patch.multiple(portfolio.Portfolio, data=None, liquidities={}), - mock.patch.multiple(portfolio.Trade, - ticker_cache={}, - ticker_cache_timestamp=self.time.time(), - fees_cache={}, - trades={}), - mock.patch.multiple(portfolio.Computation, - computations=portfolio.Computation.computations) - ] - for patcher in self.patchers: - patcher.start() - +class AcceptanceTest(WebMockTestCase): + @unittest.expectedFailure def test_success_sell_only_necessary(self): fetch_balance = { "ETH": { @@ -573,11 +702,12 @@ class AcceptanceTest(unittest.TestCase): }, } repartition = { - "ETH": 2500, - "ETC": 2500, - "BTC": 4000, - "BTD": 500, - "USDT": 500, + "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): @@ -605,8 +735,14 @@ class AcceptanceTest(unittest.TestCase): "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 portfolio.ccxt.ExchangeError + raise portfolio.ExchangeError if symbol == "BTC/USDT": return { "symbol": "BTC/USDT", @@ -618,7 +754,7 @@ class AcceptanceTest(unittest.TestCase): market = mock.Mock() market.fetch_balance.return_value = fetch_balance market.fetch_ticker.side_effect = fetch_ticker - with mock.patch.object(portfolio.Portfolio, "repartition_pertenthousand", return_value=repartition): + with mock.patch.object(portfolio.Portfolio, "repartition", return_value=repartition): # Action 1 portfolio.Balance.prepare_trades(market) @@ -640,9 +776,13 @@ class AcceptanceTest(unittest.TestCase): self.assertNotIn("BTC", trades) self.assertEqual(portfolio.Amount("BTC", D("0.00")), trades["BTD"].value_from) - self.assertEqual(portfolio.Amount("BTC", D("0.01")), trades["BTD"].value_to) + self.assertEqual(portfolio.Amount("BTC", D("0.002")), trades["BTD"].value_to) self.assertEqual("buy", trades["BTD"].action) + self.assertEqual(portfolio.Amount("BTC", D("0.00")), trades["B2X"].value_from) + self.assertEqual(portfolio.Amount("BTC", D("0.008")), trades["B2X"].value_to) + self.assertEqual("buy", trades["B2X"].action) + self.assertEqual(portfolio.Amount("BTC", D("0.00")), trades["USDT"].value_from) self.assertEqual(portfolio.Amount("BTC", D("0.01")), trades["USDT"].value_to) self.assertEqual("buy", trades["USDT"].action) @@ -662,11 +802,11 @@ class AcceptanceTest(unittest.TestCase): self.assertEqual(D("0.00003003"), all_orders[1].rate) - def create_order(symbol, type, action, amount, price=None): + 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(2, 3*amount) + self.assertEqual(D('0.66666666'), amount) self.assertEqual(D("0.14014"), price) elif symbol == "XVG/BTC": self.assertEqual("sell", action) @@ -679,6 +819,7 @@ class AcceptanceTest(unittest.TestCase): "id": symbol, } market.create_order.side_effect = create_order + market.order_precision.return_value = 8 # Action 3 portfolio.Trade.run_orders() @@ -720,7 +861,7 @@ class AcceptanceTest(unittest.TestCase): } market.fetch_balance.return_value = fetch_balance - with mock.patch.object(portfolio.Portfolio, "repartition_pertenthousand", return_value=repartition): + with mock.patch.object(portfolio.Portfolio, "repartition", return_value=repartition): # Action 5 portfolio.Balance.update_trades(market, only="buy", compute_value="average") @@ -743,9 +884,13 @@ class AcceptanceTest(unittest.TestCase): self.assertNotIn("BTC", trades) self.assertEqual(portfolio.Amount("BTC", D("0.00")), trades["BTD"].value_from) - self.assertEqual(portfolio.Amount("BTC", D("0.0097")), trades["BTD"].value_to) + self.assertEqual(portfolio.Amount("BTC", D("0.00194")), trades["BTD"].value_to) self.assertEqual("buy", trades["BTD"].action) + self.assertEqual(portfolio.Amount("BTC", D("0.00")), trades["B2X"].value_from) + self.assertEqual(portfolio.Amount("BTC", D("0.00776")), trades["B2X"].value_to) + self.assertEqual("buy", trades["B2X"].action) + self.assertEqual(portfolio.Amount("BTC", D("0.00")), trades["USDT"].value_from) self.assertEqual(portfolio.Amount("BTC", D("0.0097")), trades["USDT"].value_to) self.assertEqual("buy", trades["USDT"].action) @@ -757,30 +902,42 @@ class AcceptanceTest(unittest.TestCase): # Action 6 portfolio.Trade.prepare_orders(only="buy", compute_value=lambda x, y: x["ask"]) - all_orders = portfolio.Trade.all_orders(state="pending") - self.assertEqual(3, len(all_orders)) - self.assertEqual(portfolio.Amount("ETC", D("38.5")/3), all_orders[0].amount) + 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("24.25")/3), all_orders[1].amount) + self.assertEqual(portfolio.Amount("BTD", D("1.61666666")), round(all_orders[1].amount)) self.assertEqual(D("0.0012"), all_orders[1].rate) - self.assertEqual("buy", all_orders[1].action) + 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) - self.assertEqual(portfolio.Amount("BTC", D("0.0097")), all_orders[2].amount) - self.assertEqual(D("16000"), all_orders[2].rate) - self.assertEqual("sell", all_orders[2].action) + # Action 6b + # TODO: + # Move balances to margin + + # Action 7 + # TODO + # portfolio.Trade.run_orders() with mock.patch.object(portfolio.time, "sleep") as sleep: - # Action 7 + # Action 8 portfolio.Trade.follow_orders(verbose=False) sleep.assert_called_with(30) - def tearDown(self): - for patcher in self.patchers: - patcher.stop() - if __name__ == '__main__': unittest.main()