import unittest
from decimal import Decimal as D
from unittest import mock
+import requests
+import requests_mock
+from io import StringIO
-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={},
+ debug=False,
+ 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)
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)
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)
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)
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))
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, parse_int=portfolio.D, parse_float=portfolio.D)
-
- 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': (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()
- mock_get.assert_called_once_with()
-
- @mock.patch.object(portfolio.Portfolio, "get_cryptoportfolio")
- def test_repartition(self, mock_get):
- mock_get.side_effect = self.fill_data
-
- 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"))
-
- def tearDown(self):
- self.patcher.stop()
-
-class BalanceTest(unittest.TestCase):
+class BalanceTest(WebMockTestCase):
def setUp(self):
super(BalanceTest, self).setUp()
self.fetch_balance = {
- "free": "foo",
- "info": "bar",
- "used": "baz",
- "total": "bazz",
"ETC": {
- "free": 0.0,
- "used": 0.0,
- "total": 0.0
+ "exchange_free": 0,
+ "exchange_used": 0,
+ "exchange_total": 0,
+ "margin_total": 0,
},
"USDT": {
- "free": 6.0,
- "used": 1.2,
- "total": 7.2
+ "exchange_free": D("6.0"),
+ "exchange_used": D("1.2"),
+ "exchange_total": D("7.2"),
+ "margin_total": 0,
},
"XVG": {
- "free": 16,
- "used": 0.0,
- "total": 16
+ "exchange_free": 16,
+ "exchange_used": 0,
+ "exchange_total": 16,
+ "margin_total": 0,
},
"XMR": {
- "free": 0.0,
- "used": 0.0,
- "total": 0.0
+ "exchange_free": 0,
+ "exchange_used": 0,
+ "exchange_total": 0,
+ "margin_total": D("-1.0"),
+ "margin_free": 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 = {
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()))
- @mock.patch.object(portfolio.market, "fetch_balance")
- def test_fetch_balances(self, fetch_balance):
- fetch_balance.return_value = self.fetch_balance
+ @mock.patch.object(portfolio.market, "fetch_all_balances")
+ def test_fetch_balances(self, fetch_all_balances):
+ fetch_all_balances.return_value = self.fetch_balance
portfolio.Balance.fetch_balances(portfolio.market)
- self.assertNotIn("XMR", portfolio.Balance.currencies())
- self.assertListEqual(["USDT", "XVG"], list(portfolio.Balance.currencies()))
+ self.assertNotIn("ETC", portfolio.Balance.currencies())
+ self.assertListEqual(["USDT", "XVG", "XMR"], list(portfolio.Balance.currencies()))
- portfolio.Balance.known_balances["ETC"] = portfolio.Balance("ETC", "1", "0", "1")
+ portfolio.Balance.known_balances["ETC"] = portfolio.Balance("ETC", {
+ "exchange_total": "1", "exchange_free": "0",
+ "exchange_used": "1" })
portfolio.Balance.fetch_balances(portfolio.market)
self.assertEqual(0, portfolio.Balance.known_balances["ETC"].total)
- self.assertListEqual(["USDT", "XVG", "ETC"], list(portfolio.Balance.currencies()))
+ self.assertListEqual(["USDT", "XVG", "XMR", "ETC"], list(portfolio.Balance.currencies()))
@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
+ @mock.patch.object(portfolio.market, "fetch_all_balances")
+ def test_dispatch_assets(self, fetch_all_balances, repartition):
+ fetch_all_balances.return_value = self.fetch_balance
portfolio.Balance.fetch_balances(portfolio.market)
self.assertNotIn("XEM", portfolio.Balance.currencies())
get_ticker.side_effect = _get_ticker
market = mock.Mock()
- market.fetch_balance.return_value = {
+ market.fetch_all_balances.return_value = {
"USDT": {
- "free": D("10000.0"),
- "used": D("0.0"),
+ "exchange_free": D("10000.0"),
+ "exchange_used": D("0.0"),
+ "exchange_total": D("10000.0"),
"total": D("10000.0")
},
"XVG": {
- "free": D("10000.0"),
- "used": D("0.0"),
+ "exchange_free": D("10000.0"),
+ "exchange_used": D("0.0"),
+ "exchange_total": D("10000.0"),
"total": D("10000.0")
},
}
self.assertEqual(D("0.2525"), call[0][1]["BTC"].value)
self.assertEqual(D("0.7575"), call[0][1]["XEM"].value)
- @unittest.skip("TODO")
- def test_update_trades(self):
- pass
+ @mock.patch.object(portfolio.Portfolio, "repartition")
+ @mock.patch.object(portfolio.Trade, "get_ticker")
+ @mock.patch.object(portfolio.Trade, "compute_trades")
+ def test_update_trades(self, compute_trades, get_ticker, repartition):
+ repartition.return_value = {
+ "XEM": (D("0.75"), "long"),
+ "BTC": (D("0.25"), "long"),
+ }
+ def _get_ticker(c1, c2, market):
+ if c1 == "USDT" and c2 == "BTC":
+ return { "average": D("0.0001") }
+ if c1 == "XVG" and c2 == "BTC":
+ return { "average": D("0.000001") }
+ if c1 == "XEM" and c2 == "BTC":
+ return { "average": D("0.001") }
+ self.fail("Should be called with {}, {}".format(c1, c2))
+ get_ticker.side_effect = _get_ticker
- 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))
+ market = mock.Mock()
+ market.fetch_all_balances.return_value = {
+ "USDT": {
+ "exchange_free": D("10000.0"),
+ "exchange_used": D("0.0"),
+ "exchange_total": D("10000.0"),
+ "total": D("10000.0")
+ },
+ "XVG": {
+ "exchange_free": D("10000.0"),
+ "exchange_used": D("0.0"),
+ "exchange_total": D("10000.0"),
+ "total": D("10000.0")
+ },
+ }
+ portfolio.Balance.update_trades(market)
+ compute_trades.assert_called()
- def tearDown(self):
- self.patcher.stop()
+ call = compute_trades.call_args
+ self.assertEqual(market, call[1]["market"])
+ self.assertEqual(1, call[0][0]["USDT"].value)
+ self.assertEqual(D("0.01"), call[0][0]["XVG"].value)
+ self.assertEqual(D("0.2525"), call[0][1]["BTC"].value)
+ self.assertEqual(D("0.7575"), call[0][1]["XEM"].value)
-class TradeTest(unittest.TestCase):
- import time
+ @mock.patch.object(portfolio.Portfolio, "repartition")
+ @mock.patch.object(portfolio.Trade, "get_ticker")
+ @mock.patch.object(portfolio.Trade, "compute_trades")
+ def test_prepare_trades_to_sell_all(self, compute_trades, get_ticker, repartition):
+ def _get_ticker(c1, c2, market):
+ if c1 == "USDT" and c2 == "BTC":
+ return { "average": D("0.0001") }
+ if c1 == "XVG" and c2 == "BTC":
+ return { "average": D("0.000001") }
+ self.fail("Should be called with {}, {}".format(c1, c2))
+ get_ticker.side_effect = _get_ticker
- def setUp(self):
- super(TradeTest, self).setUp()
+ market = mock.Mock()
+ market.fetch_all_balances.return_value = {
+ "USDT": {
+ "exchange_free": D("10000.0"),
+ "exchange_used": D("0.0"),
+ "exchange_total": D("10000.0"),
+ "total": D("10000.0")
+ },
+ "XVG": {
+ "exchange_free": D("10000.0"),
+ "exchange_used": D("0.0"),
+ "exchange_total": D("10000.0"),
+ "total": D("10000.0")
+ },
+ }
+ portfolio.Balance.prepare_trades_to_sell_all(market)
+ repartition.assert_not_called()
+ compute_trades.assert_called()
+
+ call = compute_trades.call_args
+ self.assertEqual(market, call[1]["market"])
+ self.assertEqual(1, call[0][0]["USDT"].value)
+ self.assertEqual(D("0.01"), call[0][0]["XVG"].value)
+ self.assertEqual(D("1.01"), call[0][1]["BTC"].value)
+
+ def test__repr(self):
+ self.assertEqual("Balance(BTX Exch: [✔2.00000000 BTX])",
+ repr(portfolio.Balance("BTX", { "exchange_free": 2, "exchange_total": 2 })))
+ balance = portfolio.Balance("BTX", { "exchange_total": 3,
+ "exchange_used": 1, "exchange_free": 2 })
+ self.assertEqual("Balance(BTX Exch: [✔2.00000000 BTX + ❌1.00000000 BTX = 3.00000000 BTX])", repr(balance))
- self.patcher = mock.patch.multiple(portfolio.Trade,
- ticker_cache={},
- ticker_cache_timestamp=self.time.time(),
- fees_cache={},
- trades={})
- self.patcher.start()
+ balance = portfolio.Balance("BTX", { "margin_total": 3,
+ "margin_borrowed": 1, "margin_free": 2 })
+ self.assertEqual("Balance(BTX Margin: [✔2.00000000 BTX + borrowed 1.00000000 BTX = 3.00000000 BTX])", repr(balance))
+
+ balance = portfolio.Balance("BTX", { "margin_total": -3,
+ "margin_borrowed_base_price": D("0.1"),
+ "margin_borrowed_base_currency": "BTC",
+ "margin_lending_fees": D("0.002") })
+ self.assertEqual("Balance(BTX Margin: [-3.00000000 BTX @@ 0.10000000 BTC/0.00200000 BTC])", repr(balance))
+
+class TradeTest(WebMockTestCase):
def test_get_ticker(self):
market = mock.Mock()
self.assertDictEqual(ticker6, ticker7)
self.assertEqual(1.2, ticker8["bid"])
- @unittest.skip("TODO")
def test_values_assertion(self):
- 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")
+ value_from = portfolio.Amount("BTC", "1.0")
+ value_from.linked_to = portfolio.Amount("ETH", "10.0")
+ value_to = portfolio.Amount("BTC", "1.0")
+ trade = portfolio.Trade(value_from, value_to, "ETH")
self.assertEqual("BTC", trade.base_currency)
self.assertEqual("ETH", trade.currency)
value_from.currency = "ETH"
portfolio.Trade(value_from, value_to, "ETH")
- @unittest.skip("TODO")
+ value_from = portfolio.Amount("BTC", 0)
+ trade = portfolio.Trade(value_from, value_to, "ETH")
+ self.assertEqual(0, trade.value_from.linked_to)
+
def test_fetch_fees(self):
- pass
+ market = mock.Mock()
+ market.fetch_fees.return_value = "Foo"
+ self.assertEqual("Foo", portfolio.Trade.fetch_fees(market))
+ market.fetch_fees.assert_called_once()
+ self.assertEqual("Foo", portfolio.Trade.fetch_fees(market))
+ market.fetch_fees.assert_called_once()
+
+ def test_action(self):
+ value_from = portfolio.Amount("BTC", "1.0")
+ value_from.linked_to = portfolio.Amount("ETH", "10.0")
+ value_to = portfolio.Amount("BTC", "1.0")
+ trade = portfolio.Trade(value_from, value_to, "ETH")
+
+ self.assertIsNone(trade.action)
+
+ value_from = portfolio.Amount("BTC", "1.0")
+ value_from.linked_to = portfolio.Amount("BTC", "1.0")
+ value_to = portfolio.Amount("BTC", "1.0")
+ trade = portfolio.Trade(value_from, value_to, "BTC")
+
+ self.assertIsNone(trade.action)
+
+ value_from = portfolio.Amount("BTC", "0.5")
+ value_from.linked_to = portfolio.Amount("ETH", "10.0")
+ value_to = portfolio.Amount("BTC", "1.0")
+ trade = portfolio.Trade(value_from, value_to, "ETH")
+
+ self.assertEqual("acquire", trade.action)
+
+ value_from = portfolio.Amount("BTC", "0")
+ value_from.linked_to = portfolio.Amount("ETH", "0")
+ value_to = portfolio.Amount("BTC", "-1.0")
+ trade = portfolio.Trade(value_from, value_to, "ETH")
+
+ self.assertEqual("dispose", trade.action)
+
+ def test_order_action(self):
+ value_from = portfolio.Amount("BTC", "0.5")
+ value_from.linked_to = portfolio.Amount("ETH", "10.0")
+ value_to = portfolio.Amount("BTC", "1.0")
+ trade = portfolio.Trade(value_from, value_to, "ETH")
+
+ self.assertEqual("buy", trade.order_action(False))
+ self.assertEqual("sell", trade.order_action(True))
+
+ value_from = portfolio.Amount("BTC", "0")
+ value_from.linked_to = portfolio.Amount("ETH", "0")
+ value_to = portfolio.Amount("BTC", "-1.0")
+ trade = portfolio.Trade(value_from, value_to, "ETH")
+
+ self.assertEqual("sell", trade.order_action(False))
+ self.assertEqual("buy", trade.order_action(True))
+
+ def test_trade_type(self):
+ value_from = portfolio.Amount("BTC", "0.5")
+ value_from.linked_to = portfolio.Amount("ETH", "10.0")
+ value_to = portfolio.Amount("BTC", "1.0")
+ trade = portfolio.Trade(value_from, value_to, "ETH")
+
+ self.assertEqual("long", trade.trade_type)
+
+ value_from = portfolio.Amount("BTC", "0")
+ value_from.linked_to = portfolio.Amount("ETH", "0")
+ value_to = portfolio.Amount("BTC", "-1.0")
+ trade = portfolio.Trade(value_from, value_to, "ETH")
+
+ self.assertEqual("short", trade.trade_type)
+
+ def test_filled_amount(self):
+ value_from = portfolio.Amount("BTC", "0.5")
+ value_from.linked_to = portfolio.Amount("ETH", "10.0")
+ value_to = portfolio.Amount("BTC", "1.0")
+ trade = portfolio.Trade(value_from, value_to, "ETH")
+
+ order1 = mock.Mock()
+ order1.filled_amount = portfolio.Amount("ETH", "0.3")
+
+ order2 = mock.Mock()
+ order2.filled_amount = portfolio.Amount("ETH", "0.01")
+ trade.orders.append(order1)
+ trade.orders.append(order2)
+
+ self.assertEqual(portfolio.Amount("ETH", "0.31"), trade.filled_amount)
+
+ def test_prepare_orders(self):
+ trade_mock1 = mock.Mock()
+ trade_mock2 = mock.Mock()
+
+ portfolio.Trade.trades.append(trade_mock1)
+ portfolio.Trade.trades.append(trade_mock2)
+
+ portfolio.Trade.prepare_orders()
+ trade_mock1.prepare_order.assert_called_with(compute_value="default")
+ trade_mock2.prepare_order.assert_called_with(compute_value="default")
+
+ portfolio.Trade.prepare_orders(compute_value="bla")
+ trade_mock1.prepare_order.assert_called_with(compute_value="bla")
+ trade_mock2.prepare_order.assert_called_with(compute_value="bla")
+
+ trade_mock1.prepare_order.reset_mock()
+ trade_mock2.prepare_order.reset_mock()
+
+ trade_mock1.action = "foo"
+ trade_mock2.action = "bar"
+ portfolio.Trade.prepare_orders(only="bar")
+ trade_mock1.prepare_order.assert_not_called()
+ trade_mock2.prepare_order.assert_called_with(compute_value="default")
@unittest.skip("TODO")
def test_compute_trades(self):
pass
@unittest.skip("TODO")
- def test_action(self):
+ def test_prepare_order(self):
pass
@unittest.skip("TODO")
- def test_action(self):
+ def test_update_order(self):
pass
@unittest.skip("TODO")
- def test_order_action(self):
+ def test_follow_orders(self):
pass
@unittest.skip("TODO")
- def test_prepare_order(self):
+ def test_move_balances(self):
pass
- @unittest.skip("TODO")
def test_all_orders(self):
- pass
+ trade_mock1 = mock.Mock()
+ trade_mock2 = mock.Mock()
- @unittest.skip("TODO")
- def test_follow_orders(self):
- pass
+ order_mock1 = mock.Mock()
+ order_mock2 = mock.Mock()
+ order_mock3 = mock.Mock()
+
+ trade_mock1.orders = [order_mock1, order_mock2]
+ trade_mock2.orders = [order_mock3]
+
+ order_mock1.status = "pending"
+ order_mock2.status = "open"
+ order_mock3.status = "open"
+
+ portfolio.Trade.trades.append(trade_mock1)
+ portfolio.Trade.trades.append(trade_mock2)
+
+ orders = portfolio.Trade.all_orders()
+ self.assertEqual(3, len(orders))
+
+ open_orders = portfolio.Trade.all_orders(state="open")
+ self.assertEqual(2, len(open_orders))
+ self.assertEqual([order_mock2, order_mock3], open_orders)
+
+ @mock.patch.object(portfolio.Trade, "all_orders")
+ def test_run_orders(self, all_orders):
+ order_mock1 = mock.Mock()
+ order_mock2 = mock.Mock()
+ order_mock3 = mock.Mock()
+ all_orders.return_value = [order_mock1, order_mock2, order_mock3]
+ portfolio.Trade.run_orders()
+ all_orders.assert_called_with(state="pending")
+
+ order_mock1.run.assert_called()
+ order_mock2.run.assert_called()
+ order_mock3.run.assert_called()
+
+ @mock.patch.object(portfolio.Trade, "all_orders")
+ def test_update_all_orders_status(self, all_orders):
+ order_mock1 = mock.Mock()
+ order_mock2 = mock.Mock()
+ order_mock3 = mock.Mock()
+ all_orders.return_value = [order_mock1, order_mock2, order_mock3]
+ portfolio.Trade.update_all_orders_status()
+ all_orders.assert_called_with(state="open")
+
+ order_mock1.get_status.assert_called()
+ order_mock2.get_status.assert_called()
+ order_mock3.get_status.assert_called()
+
+ def test_print_all_with_order(self):
+ trade_mock1 = mock.Mock()
+ trade_mock2 = mock.Mock()
+ trade_mock3 = mock.Mock()
+ portfolio.Trade.trades = [trade_mock1, trade_mock2, trade_mock3]
+
+ portfolio.Trade.print_all_with_order()
+
+ trade_mock1.print_with_order.assert_called()
+ trade_mock2.print_with_order.assert_called()
+ trade_mock3.print_with_order.assert_called()
+
+ @mock.patch('sys.stdout', new_callable=StringIO)
+ def test_print_with_order(self, mock_stdout):
+ value_from = portfolio.Amount("BTC", "0.5")
+ value_from.linked_to = portfolio.Amount("ETH", "10.0")
+ value_to = portfolio.Amount("BTC", "1.0")
+ trade = portfolio.Trade(value_from, value_to, "ETH")
+
+ order_mock1 = mock.Mock()
+ order_mock1.__repr__ = mock.Mock()
+ order_mock1.__repr__.return_value = "Mock 1"
+ order_mock2 = mock.Mock()
+ order_mock2.__repr__ = mock.Mock()
+ order_mock2.__repr__.return_value = "Mock 2"
+ trade.orders.append(order_mock1)
+ trade.orders.append(order_mock2)
+
+ trade.print_with_order()
+
+ out = mock_stdout.getvalue().split("\n")
+ self.assertEqual("Trade(0.50000000 BTC [10.00000000 ETH] -> 1.00000000 BTC in ETH, acquire)", out[0])
+ self.assertEqual("\tMock 1", out[1])
+ self.assertEqual("\tMock 2", out[2])
- @unittest.skip("TODO")
def test_compute_value(self):
- pass
+ compute = mock.Mock()
+ portfolio.Trade.compute_value("foo", "buy", compute_value=compute)
+ compute.assert_called_with("foo", "ask")
- @unittest.skip("TODO")
- def test__repr(self):
- pass
+ compute.reset_mock()
+ portfolio.Trade.compute_value("foo", "sell", compute_value=compute)
+ compute.assert_called_with("foo", "bid")
- def tearDown(self):
- self.patcher.stop()
+ compute.reset_mock()
+ portfolio.Trade.compute_value("foo", "ask", compute_value=compute)
+ compute.assert_called_with("foo", "ask")
-class AcceptanceTest(unittest.TestCase):
- import time
+ compute.reset_mock()
+ portfolio.Trade.compute_value("foo", "bid", compute_value=compute)
+ compute.assert_called_with("foo", "bid")
- def setUp(self):
- super(AcceptanceTest, self).setUp()
+ compute.reset_mock()
+ portfolio.Computation.computations["test"] = compute
+ portfolio.Trade.compute_value("foo", "bid", compute_value="test")
+ compute.assert_called_with("foo", "bid")
- 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 test__repr(self):
+ value_from = portfolio.Amount("BTC", "0.5")
+ value_from.linked_to = portfolio.Amount("ETH", "10.0")
+ value_to = portfolio.Amount("BTC", "1.0")
+ trade = portfolio.Trade(value_from, value_to, "ETH")
+ self.assertEqual("Trade(0.50000000 BTC [10.00000000 ETH] -> 1.00000000 BTC in ETH, acquire)", str(trade))
+
+class AcceptanceTest(WebMockTestCase):
+ @unittest.expectedFailure
def test_success_sell_only_necessary(self):
fetch_balance = {
"ETH": {
- "free": D("1.0"),
- "used": D("0.0"),
+ "exchange_free": D("1.0"),
+ "exchange_used": D("0.0"),
+ "exchange_total": D("1.0"),
"total": D("1.0"),
},
"ETC": {
- "free": D("4.0"),
- "used": D("0.0"),
+ "exchange_free": D("4.0"),
+ "exchange_used": D("0.0"),
+ "exchange_total": D("4.0"),
"total": D("4.0"),
},
"XVG": {
- "free": D("1000.0"),
- "used": D("0.0"),
+ "exchange_free": D("1000.0"),
+ "exchange_used": D("0.0"),
+ "exchange_total": D("1000.0"),
"total": D("1000.0"),
},
}
self.fail("Shouldn't have been called with {}".format(symbol))
market = mock.Mock()
- market.fetch_balance.return_value = fetch_balance
+ market.fetch_all_balances.return_value = fetch_balance
market.fetch_ticker.side_effect = fetch_ticker
with mock.patch.object(portfolio.Portfolio, "repartition", return_value=repartition):
# Action 1
trades = portfolio.Trade.trades
- self.assertEqual(portfolio.Amount("BTC", D("0.15")), trades["ETH"].value_from)
- self.assertEqual(portfolio.Amount("BTC", D("0.05")), trades["ETH"].value_to)
- self.assertEqual("sell", trades["ETH"].action)
+ 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["ETC"].value_from)
- self.assertEqual(portfolio.Amount("BTC", D("0.05")), trades["ETC"].value_to)
- self.assertEqual("buy", trades["ETC"].action)
-
- self.assertNotIn("BTC", trades)
+ 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.00")), trades["BTD"].value_from)
- 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.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["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[3].value_from)
+ self.assertEqual(portfolio.Amount("BTC", D("-0.002")), trades[3].value_to)
+ self.assertEqual("dispose", trades[3].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)
+ 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.04")), trades["XVG"].value_from)
- self.assertEqual(portfolio.Amount("BTC", D("0.00")), trades["XVG"].value_to)
- self.assertEqual("sell", trades["XVG"].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.Trade.prepare_orders(only="sell", compute_value=lambda x, y: x["bid"] * D("1.001"))
+ portfolio.Trade.prepare_orders(only="dispose", compute_value=lambda x, y: x["bid"] * D("1.001"))
all_orders = portfolio.Trade.all_orders()
self.assertEqual(2, len(all_orders))
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.Trade.run_orders()
sleep.assert_called_with(30)
- def tearDown(self):
- for patcher in self.patchers:
- patcher.stop()
-
if __name__ == '__main__':
unittest.main()