import sys
import portfolio
import unittest
+import datetime
from decimal import Decimal as D
from unittest import mock
import requests
import requests_mock
from io import StringIO
-import helper
+import portfolio, helper, market
limits = ["acceptance", "unit"]
for test_type in limits:
self.wm = requests_mock.Mocker()
self.wm.start()
+ # market
+ self.m = mock.Mock(name="Market", spec=market.Market)
+ self.m.debug = False
+
self.patchers = [
- mock.patch.multiple(portfolio.BalanceStore,
- all={},),
- mock.patch.multiple(portfolio.TradeStore,
- all=[],
- debug=False),
- mock.patch.multiple(portfolio.Portfolio, data=None, liquidities={}),
+ mock.patch.multiple(portfolio.Portfolio, last_date=None, data=None, liquidities={}),
mock.patch.multiple(portfolio.Computation,
computations=portfolio.Computation.computations),
- mock.patch.multiple(helper,
+ mock.patch.multiple(market.Market,
fees_cache={},
ticker_cache={},
ticker_cache_timestamp=self.time.time()),
for patcher in self.patchers:
patcher.start()
-
def tearDown(self):
for patcher in self.patchers:
patcher.stop()
{"text": "System Error", "status_code": 500},
{"exc": requests.exceptions.ConnectTimeout},
])
- portfolio.Portfolio.get_cryptoportfolio()
+ portfolio.Portfolio.get_cryptoportfolio(self.m)
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)
+ self.m.report.log_error.assert_not_called()
+ self.m.report.log_http_request.assert_called_once()
+ self.m.report.log_http_request.reset_mock()
- portfolio.Portfolio.get_cryptoportfolio()
+ portfolio.Portfolio.get_cryptoportfolio(self.m)
self.assertIsNone(portfolio.Portfolio.data)
self.assertEqual(2, self.wm.call_count)
+ self.m.report.log_error.assert_not_called()
+ self.m.report.log_http_request.assert_called_once()
+ self.m.report.log_http_request.reset_mock()
+
portfolio.Portfolio.data = "Foo"
- portfolio.Portfolio.get_cryptoportfolio()
+ portfolio.Portfolio.get_cryptoportfolio(self.m)
self.assertEqual("Foo", portfolio.Portfolio.data)
self.assertEqual(3, self.wm.call_count)
+ self.m.report.log_error.assert_called_once_with("get_cryptoportfolio",
+ exception=mock.ANY)
+ self.m.report.log_http_request.assert_not_called()
def test_parse_cryptoportfolio(self):
- portfolio.Portfolio.parse_cryptoportfolio()
+ portfolio.Portfolio.parse_cryptoportfolio(self.m)
self.assertListEqual(
["medium", "high"],
'SC': (D("0.0623"), "long"),
'ZEC': (D("0.3701"), "long"),
}
- self.assertDictEqual(expected, liquidities["high"]['2018-01-08'])
+ date = portfolio.datetime(2018, 1, 8)
+ self.assertDictEqual(expected, liquidities["high"][date])
expected = {
'BTC': (D("1.1102e-16"), "long"),
'VIA': (D("0.1"), "long"),
'XCP': (D("0.1"), "long"),
}
- self.assertDictEqual(expected, liquidities["medium"]['2018-01-08'])
+ self.assertDictEqual(expected, liquidities["medium"][date])
+ self.assertEqual(portfolio.datetime(2018, 1, 15), portfolio.Portfolio.last_date)
+
+ self.m.report.log_http_request.assert_called_once_with("GET",
+ portfolio.Portfolio.URL, None, mock.ANY, mock.ANY)
+ self.m.report.log_http_request.reset_mock()
# It doesn't refetch the data when available
- portfolio.Portfolio.parse_cryptoportfolio()
+ portfolio.Portfolio.parse_cryptoportfolio(self.m)
+ self.m.report.log_http_request.assert_not_called()
self.assertEqual(1, self.wm.call_count)
+ portfolio.Portfolio.parse_cryptoportfolio(self.m, refetch=True)
+ self.assertEqual(2, self.wm.call_count)
+ self.m.report.log_http_request.assert_called_once()
+
def test_repartition(self):
expected_medium = {
'BTC': (D("1.1102e-16"), "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"))
+ self.assertEqual(expected_medium, portfolio.Portfolio.repartition(self.m))
+ self.assertEqual(expected_medium, portfolio.Portfolio.repartition(self.m, liquidity="medium"))
+ self.assertEqual(expected_high, portfolio.Portfolio.repartition(self.m, liquidity="high"))
+
+ self.assertEqual(1, self.wm.call_count)
+
+ portfolio.Portfolio.repartition(self.m)
+ self.assertEqual(1, self.wm.call_count)
+
+ portfolio.Portfolio.repartition(self.m, refetch=True)
+ self.assertEqual(2, self.wm.call_count)
+ self.m.report.log_http_request.assert_called()
+ self.assertEqual(2, self.m.report.log_http_request.call_count)
+
+ @mock.patch.object(portfolio.time, "sleep")
+ @mock.patch.object(portfolio.Portfolio, "repartition")
+ def test_wait_for_recent(self, repartition, sleep):
+ self.call_count = 0
+ def _repartition(market, refetch):
+ self.assertEqual(self.m, market)
+ self.assertTrue(refetch)
+ self.call_count += 1
+ portfolio.Portfolio.last_date = portfolio.datetime.now()\
+ - portfolio.timedelta(10)\
+ + portfolio.timedelta(self.call_count)
+ repartition.side_effect = _repartition
+
+ portfolio.Portfolio.wait_for_recent(self.m)
+ sleep.assert_called_with(30)
+ self.assertEqual(6, sleep.call_count)
+ self.assertEqual(7, repartition.call_count)
+ self.m.report.print_log.assert_called_with("Attempt to fetch up-to-date cryptoportfolio")
+
+ sleep.reset_mock()
+ repartition.reset_mock()
+ portfolio.Portfolio.last_date = None
+ self.call_count = 0
+ portfolio.Portfolio.wait_for_recent(self.m, delta=15)
+ sleep.assert_not_called()
+ self.assertEqual(1, repartition.call_count)
+
+ sleep.reset_mock()
+ repartition.reset_mock()
+ portfolio.Portfolio.last_date = None
+ self.call_count = 0
+ portfolio.Portfolio.wait_for_recent(self.m, delta=1)
+ sleep.assert_called_with(30)
+ self.assertEqual(9, sleep.call_count)
+ self.assertEqual(10, repartition.call_count)
@unittest.skipUnless("unit" in limits, "Unit skipped")
class AmountTest(WebMockTestCase):
def test_in_currency(self):
amount = portfolio.Amount("ETC", 10)
- self.assertEqual(amount, amount.in_currency("ETC", None))
+ self.assertEqual(amount, amount.in_currency("ETC", self.m))
- ticker_mock = unittest.mock.Mock()
- with mock.patch.object(helper, 'get_ticker', new=ticker_mock):
- ticker_mock.return_value = None
+ with self.subTest(desc="no ticker for currency"):
+ self.m.get_ticker.return_value = None
- self.assertRaises(Exception, amount.in_currency, "ETH", None)
+ self.assertRaises(Exception, amount.in_currency, "ETH", self.m)
- with mock.patch.object(helper, 'get_ticker', new=ticker_mock):
- ticker_mock.return_value = {
+ with self.subTest(desc="nominal case"):
+ self.m.get_ticker.return_value = {
"bid": D("0.2"),
"ask": D("0.4"),
"average": D("0.3"),
"foo": "bar",
}
- converted_amount = amount.in_currency("ETH", None)
+ converted_amount = amount.in_currency("ETH", self.m)
self.assertEqual(D("3.0"), converted_amount.value)
self.assertEqual("ETH", converted_amount.currency)
self.assertEqual(amount, converted_amount.linked_to)
self.assertEqual("bar", converted_amount.ticker["foo"])
- converted_amount = amount.in_currency("ETH", None, action="bid", compute_value="default")
+ converted_amount = amount.in_currency("ETH", self.m, action="bid", compute_value="default")
self.assertEqual(D("2"), converted_amount.value)
- converted_amount = amount.in_currency("ETH", None, compute_value="ask")
+ converted_amount = amount.in_currency("ETH", self.m, compute_value="ask")
self.assertEqual(D("4"), converted_amount.value)
- converted_amount = amount.in_currency("ETH", None, rate=D("0.02"))
+ converted_amount = amount.in_currency("ETH", self.m, rate=D("0.02"))
self.assertEqual(D("0.2"), converted_amount.value)
def test__round(self):
amount4 = portfolio.Amount("ETH", 0.0)
self.assertEqual(amount1, amount1 + amount4)
+ self.assertEqual(amount1, amount1 + 0)
+
def test__radd(self):
amount = portfolio.Amount("XVG", "12.9")
amount4 = portfolio.Amount("ETH", 0.0)
self.assertEqual(amount1, amount1 - amount4)
+ def test__rsub(self):
+ amount = portfolio.Amount("ETH", "1.6")
+ with self.assertRaises(Exception):
+ 3 - amount
+
+ self.assertEqual(portfolio.Amount("ETH", "-1.6"), 0-amount)
+
def test__mul(self):
amount = portfolio.Amount("XEM", 11)
amount2.linked_to = amount3
self.assertEqual("Amount(32.00000000 BTX -> Amount(12000.00000000 USDT -> Amount(0.10000000 BTC)))", repr(amount1))
+ def test_as_json(self):
+ amount = portfolio.Amount("BTX", 32)
+ self.assertEqual({"currency": "BTX", "value": D("32")}, amount.as_json())
+
+ amount = portfolio.Amount("BTX", "1E-10")
+ self.assertEqual({"currency": "BTX", "value": D("0")}, amount.as_json())
+
+ amount = portfolio.Amount("BTX", "1E-5")
+ self.assertEqual({"currency": "BTX", "value": D("0.00001")}, amount.as_json())
+ self.assertEqual("0.00001", str(amount.as_json()["value"]))
+
@unittest.skipUnless("unit" in limits, "Unit skipped")
class BalanceTest(WebMockTestCase):
def test_values(self):
"margin_borrowed": 1, "exchange_free": 2, "exchange_total": 2})
self.assertEqual("Balance(BTX Exch: [✔2.00000000 BTX] Margin: [borrowed 1.00000000 BTX] Total: [0.00000000 BTX])", repr(balance))
+ def test_as_json(self):
+ balance = portfolio.Balance("BTX", { "exchange_free": 2, "exchange_total": 2 })
+ as_json = balance.as_json()
+ self.assertEqual(set(portfolio.Balance.base_keys), set(as_json.keys()))
+ self.assertEqual(D(0), as_json["total"])
+ self.assertEqual(D(2), as_json["exchange_total"])
+ self.assertEqual(D(2), as_json["exchange_free"])
+ self.assertEqual(D(0), as_json["exchange_used"])
+ self.assertEqual(D(0), as_json["margin_total"])
+ self.assertEqual(D(0), as_json["margin_free"])
+ self.assertEqual(D(0), as_json["margin_borrowed"])
+
@unittest.skipUnless("unit" in limits, "Unit skipped")
-class HelperTest(WebMockTestCase):
+class MarketTest(WebMockTestCase):
+ def setUp(self):
+ super(MarketTest, self).setUp()
+
+ self.ccxt = mock.Mock(spec=market.ccxt.poloniexE)
+
+ def test_values(self):
+ m = market.Market(self.ccxt)
+
+ self.assertEqual(self.ccxt, m.ccxt)
+ self.assertFalse(m.debug)
+ self.assertIsInstance(m.report, market.ReportStore)
+ self.assertIsInstance(m.trades, market.TradeStore)
+ self.assertIsInstance(m.balances, market.BalanceStore)
+ self.assertEqual(m, m.report.market)
+ self.assertEqual(m, m.trades.market)
+ self.assertEqual(m, m.balances.market)
+ self.assertEqual(m, m.ccxt._market)
+
+ m = market.Market(self.ccxt, debug=True)
+ self.assertTrue(m.debug)
+
+ m = market.Market(self.ccxt, debug=False)
+ self.assertFalse(m.debug)
+
+ @mock.patch("market.ccxt")
+ def test_from_config(self, ccxt):
+ with mock.patch("market.ReportStore"):
+ ccxt.poloniexE.return_value = self.ccxt
+ self.ccxt.session.request.return_value = "response"
+
+ m = market.Market.from_config({"key": "key", "secred": "secret"})
+
+ self.assertEqual(self.ccxt, m.ccxt)
+
+ self.ccxt.session.request("GET", "URL", data="data",
+ headers="headers")
+ m.report.log_http_request.assert_called_with('GET', 'URL', 'data',
+ 'headers', 'response')
+
+ m = market.Market.from_config({"key": "key", "secred": "secret"}, debug=True)
+ self.assertEqual(True, m.debug)
+
def test_get_ticker(self):
- market = mock.Mock()
- market.fetch_ticker.side_effect = [
+ m = market.Market(self.ccxt)
+ self.ccxt.fetch_ticker.side_effect = [
{ "bid": 1, "ask": 3 },
- helper.ExchangeError("foo"),
+ market.ExchangeError("foo"),
{ "bid": 10, "ask": 40 },
- helper.ExchangeError("foo"),
- helper.ExchangeError("foo"),
+ market.ExchangeError("foo"),
+ market.ExchangeError("foo"),
]
- ticker = helper.get_ticker("ETH", "ETC", market)
- market.fetch_ticker.assert_called_with("ETH/ETC")
+ ticker = m.get_ticker("ETH", "ETC")
+ self.ccxt.fetch_ticker.assert_called_with("ETH/ETC")
self.assertEqual(1, ticker["bid"])
self.assertEqual(3, ticker["ask"])
self.assertEqual(2, ticker["average"])
self.assertFalse(ticker["inverted"])
- ticker = helper.get_ticker("ETH", "XVG", market)
+ ticker = m.get_ticker("ETH", "XVG")
self.assertEqual(0.0625, ticker["average"])
self.assertTrue(ticker["inverted"])
self.assertIn("original", ticker)
self.assertEqual(10, ticker["original"]["bid"])
- ticker = helper.get_ticker("XVG", "XMR", market)
+ ticker = m.get_ticker("XVG", "XMR")
self.assertIsNone(ticker)
- market.fetch_ticker.assert_has_calls([
+ self.ccxt.fetch_ticker.assert_has_calls([
mock.call("ETH/ETC"),
mock.call("ETH/XVG"),
mock.call("XVG/ETH"),
mock.call("XMR/XVG"),
])
- market2 = mock.Mock()
- market2.fetch_ticker.side_effect = [
+ self.ccxt = mock.Mock(spec=market.ccxt.poloniexE)
+ m1b = market.Market(self.ccxt)
+ m1b.get_ticker("ETH", "ETC")
+ self.ccxt.fetch_ticker.assert_not_called()
+
+ self.ccxt = mock.Mock(spec=market.ccxt.poloniex)
+ m2 = market.Market(self.ccxt)
+ self.ccxt.fetch_ticker.side_effect = [
{ "bid": 1, "ask": 3 },
{ "bid": 1.2, "ask": 3.5 },
]
- ticker1 = helper.get_ticker("ETH", "ETC", market2)
- ticker2 = helper.get_ticker("ETH", "ETC", market2)
- ticker3 = helper.get_ticker("ETC", "ETH", market2)
- market2.fetch_ticker.assert_called_once_with("ETH/ETC")
+ ticker1 = m2.get_ticker("ETH", "ETC")
+ ticker2 = m2.get_ticker("ETH", "ETC")
+ ticker3 = m2.get_ticker("ETC", "ETH")
+ self.ccxt.fetch_ticker.assert_called_once_with("ETH/ETC")
self.assertEqual(1, ticker1["bid"])
self.assertDictEqual(ticker1, ticker2)
self.assertDictEqual(ticker1, ticker3["original"])
- ticker4 = helper.get_ticker("ETH", "ETC", market2, refresh=True)
- ticker5 = helper.get_ticker("ETH", "ETC", market2)
+ ticker4 = m2.get_ticker("ETH", "ETC", refresh=True)
+ ticker5 = m2.get_ticker("ETH", "ETC")
self.assertEqual(1.2, ticker4["bid"])
self.assertDictEqual(ticker4, ticker5)
- market3 = mock.Mock()
- market3.fetch_ticker.side_effect = [
+ self.ccxt = mock.Mock(spec=market.ccxt.binance)
+ m3 = market.Market(self.ccxt)
+ self.ccxt.fetch_ticker.side_effect = [
{ "bid": 1, "ask": 3 },
{ "bid": 1.2, "ask": 3.5 },
]
- ticker6 = helper.get_ticker("ETH", "ETC", market3)
- helper.ticker_cache_timestamp -= 4
- ticker7 = helper.get_ticker("ETH", "ETC", market3)
- helper.ticker_cache_timestamp -= 2
- ticker8 = helper.get_ticker("ETH", "ETC", market3)
+ ticker6 = m3.get_ticker("ETH", "ETC")
+ m3.ticker_cache_timestamp -= 4
+ ticker7 = m3.get_ticker("ETH", "ETC")
+ m3.ticker_cache_timestamp -= 2
+ ticker8 = m3.get_ticker("ETH", "ETC")
self.assertDictEqual(ticker6, ticker7)
self.assertEqual(1.2, ticker8["bid"])
def test_fetch_fees(self):
- market = mock.Mock()
- market.fetch_fees.return_value = "Foo"
- self.assertEqual("Foo", helper.fetch_fees(market))
- market.fetch_fees.assert_called_once()
- self.assertEqual("Foo", helper.fetch_fees(market))
- market.fetch_fees.assert_called_once()
+ m = market.Market(self.ccxt)
+ self.ccxt.fetch_fees.return_value = "Foo"
+ self.assertEqual("Foo", m.fetch_fees())
+ self.ccxt.fetch_fees.assert_called_once()
+ self.ccxt.reset_mock()
+ self.assertEqual("Foo", m.fetch_fees())
+ self.ccxt.fetch_fees.assert_not_called()
@mock.patch.object(portfolio.Portfolio, "repartition")
- @mock.patch.object(helper, "get_ticker")
- @mock.patch.object(portfolio.TradeStore, "compute_trades")
+ @mock.patch.object(market.Market, "get_ticker")
+ @mock.patch.object(market.TradeStore, "compute_trades")
def test_prepare_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):
+ def _get_ticker(c1, c2):
if c1 == "USDT" and c2 == "BTC":
return { "average": D("0.0001") }
if c1 == "XVG" and c2 == "BTC":
self.fail("Should be called with {}, {}".format(c1, c2))
get_ticker.side_effect = _get_ticker
- 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")
- },
- }
- helper.prepare_trades(market)
- compute_trades.assert_called()
+ with mock.patch("market.ReportStore"):
+ m = market.Market(self.ccxt)
+ self.ccxt.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")
+ },
+ }
+
+ m.balances.fetch_balances(tag="tag")
- 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)
+ m.prepare_trades()
+ compute_trades.assert_called()
+
+ call = compute_trades.call_args
+ 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)
+ m.report.log_stage.assert_called_once_with("prepare_trades")
+ m.report.log_balances.assert_called_once_with(tag="tag")
@mock.patch.object(portfolio.Portfolio, "repartition")
- @mock.patch.object(helper, "get_ticker")
- @mock.patch.object(portfolio.TradeStore, "compute_trades")
+ @mock.patch.object(market.Market, "get_ticker")
+ @mock.patch.object(market.TradeStore, "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):
+ def _get_ticker(c1, c2):
if c1 == "USDT" and c2 == "BTC":
return { "average": D("0.0001") }
if c1 == "XVG" and c2 == "BTC":
self.fail("Should be called with {}, {}".format(c1, c2))
get_ticker.side_effect = _get_ticker
- 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")
- },
- }
- helper.update_trades(market)
- compute_trades.assert_called()
+ with mock.patch("market.ReportStore"):
+ m = market.Market(self.ccxt)
+ self.ccxt.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")
+ },
+ }
- 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)
+ m.balances.fetch_balances(tag="tag")
+
+ m.update_trades()
+ compute_trades.assert_called()
+
+ call = compute_trades.call_args
+ 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)
+ m.report.log_stage.assert_called_once_with("update_trades")
+ m.report.log_balances.assert_called_once_with(tag="tag")
@mock.patch.object(portfolio.Portfolio, "repartition")
- @mock.patch.object(helper, "get_ticker")
- @mock.patch.object(portfolio.TradeStore, "compute_trades")
+ @mock.patch.object(market.Market, "get_ticker")
+ @mock.patch.object(market.TradeStore, "compute_trades")
def test_prepare_trades_to_sell_all(self, compute_trades, get_ticker, repartition):
- def _get_ticker(c1, c2, market):
+ def _get_ticker(c1, c2):
if c1 == "USDT" and c2 == "BTC":
return { "average": D("0.0001") }
if c1 == "XVG" and c2 == "BTC":
self.fail("Should be called with {}, {}".format(c1, c2))
get_ticker.side_effect = _get_ticker
- 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")
- },
- }
- helper.prepare_trades_to_sell_all(market)
- repartition.assert_not_called()
- compute_trades.assert_called()
+ with mock.patch("market.ReportStore"):
+ m = market.Market(self.ccxt)
+ self.ccxt.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")
+ },
+ }
- 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)
+ m.balances.fetch_balances(tag="tag")
+
+ m.prepare_trades_to_sell_all()
+
+ repartition.assert_not_called()
+ compute_trades.assert_called()
+
+ call = compute_trades.call_args
+ 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)
+ m.report.log_stage.assert_called_once_with("prepare_trades_to_sell_all")
+ m.report.log_balances.assert_called_once_with(tag="tag")
@mock.patch.object(portfolio.time, "sleep")
- @mock.patch.object(portfolio.TradeStore, "all_orders")
+ @mock.patch.object(market.TradeStore, "all_orders")
def test_follow_orders(self, all_orders, time_mock):
- for verbose, debug, sleep in [
- (True, False, None), (False, False, None),
- (True, True, None), (True, False, 12),
- (True, True, 12)]:
- with self.subTest(sleep=sleep, debug=debug, verbose=verbose), \
- mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock:
- portfolio.TradeStore.debug = debug
+ for debug, sleep in [
+ (False, None), (True, None),
+ (False, 12), (True, 12)]:
+ with self.subTest(sleep=sleep, debug=debug), \
+ mock.patch("market.ReportStore"):
+ m = market.Market(self.ccxt, debug=debug)
+
order_mock1 = mock.Mock()
order_mock2 = mock.Mock()
order_mock3 = mock.Mock()
order_mock2.trade = mock.Mock()
order_mock3.trade = mock.Mock()
- helper.follow_orders(verbose=verbose, sleep=sleep)
+ m.follow_orders(sleep=sleep)
order_mock1.trade.update_order.assert_any_call(order_mock1, 1)
order_mock1.trade.update_order.assert_any_call(order_mock1, 2)
order_mock3.trade.update_order.assert_any_call(order_mock3, 2)
self.assertEqual(1, order_mock3.trade.update_order.call_count)
self.assertEqual(2, order_mock3.get_status.call_count)
+ m.report.log_stage.assert_called()
+ calls = [
+ mock.call("follow_orders_begin"),
+ mock.call("follow_orders_tick_1"),
+ mock.call("follow_orders_tick_2"),
+ mock.call("follow_orders_tick_3"),
+ mock.call("follow_orders_end"),
+ ]
+ m.report.log_stage.assert_has_calls(calls)
+ m.report.log_orders.assert_called()
+ self.assertEqual(3, m.report.log_orders.call_count)
+ calls = [
+ mock.call([order_mock1, order_mock2], tick=1),
+ mock.call([order_mock1, order_mock3], tick=2),
+ mock.call([order_mock1, order_mock3], tick=3),
+ ]
+ m.report.log_orders.assert_has_calls(calls)
+ calls = [
+ mock.call(order_mock1, 3, finished=True),
+ mock.call(order_mock3, 3, finished=True),
+ ]
+ m.report.log_order.assert_has_calls(calls)
if sleep is None:
if debug:
+ m.report.log_debug_action.assert_called_with("Set follow_orders tick to 7s")
time_mock.assert_called_with(7)
else:
time_mock.assert_called_with(30)
else:
time_mock.assert_called_with(sleep)
- if verbose:
- self.assertNotEqual("", stdout_mock.getvalue())
+ @mock.patch.object(market.BalanceStore, "fetch_balances")
+ def test_move_balance(self, fetch_balances):
+ for debug in [True, False]:
+ with self.subTest(debug=debug),\
+ mock.patch("market.ReportStore"):
+ m = market.Market(self.ccxt, debug=debug)
+
+ value_from = portfolio.Amount("BTC", "1.0")
+ value_from.linked_to = portfolio.Amount("ETH", "10.0")
+ value_to = portfolio.Amount("BTC", "10.0")
+ trade1 = portfolio.Trade(value_from, value_to, "ETH", m)
+
+ value_from = portfolio.Amount("BTC", "0.0")
+ value_from.linked_to = portfolio.Amount("ETH", "0.0")
+ value_to = portfolio.Amount("BTC", "-3.0")
+ trade2 = portfolio.Trade(value_from, value_to, "ETH", m)
+
+ value_from = portfolio.Amount("USDT", "0.0")
+ value_from.linked_to = portfolio.Amount("XVG", "0.0")
+ value_to = portfolio.Amount("USDT", "-50.0")
+ trade3 = portfolio.Trade(value_from, value_to, "XVG", m)
+
+ m.trades.all = [trade1, trade2, trade3]
+ balance1 = portfolio.Balance("BTC", { "margin_free": "0" })
+ balance2 = portfolio.Balance("USDT", { "margin_free": "100" })
+ balance3 = portfolio.Balance("ETC", { "margin_free": "10" })
+ m.balances.all = {"BTC": balance1, "USDT": balance2, "ETC": balance3}
+
+ m.move_balances()
+
+ fetch_balances.assert_called_with()
+ m.report.log_move_balances.assert_called_once()
+
+ if debug:
+ m.report.log_debug_action.assert_called()
+ self.assertEqual(3, m.report.log_debug_action.call_count)
else:
- self.assertEqual("", stdout_mock.getvalue())
-
+ self.ccxt.transfer_balance.assert_any_call("BTC", 3, "exchange", "margin")
+ self.ccxt.transfer_balance.assert_any_call("USDT", 50, "margin", "exchange")
+ self.ccxt.transfer_balance.assert_any_call("ETC", 10, "margin", "exchange")
+
@unittest.skipUnless("unit" in limits, "Unit skipped")
class TradeStoreTest(WebMockTestCase):
- @mock.patch.object(portfolio.BalanceStore, "currencies")
- @mock.patch.object(portfolio.TradeStore, "add_trade_if_matching")
- def test_compute_trades(self, add_trade_if_matching, currencies):
- currencies.return_value = ["XMR", "DASH", "XVG", "BTC", "ETH"]
+ def test_compute_trades(self):
+ self.m.balances.currencies.return_value = ["XMR", "DASH", "XVG", "BTC", "ETH"]
values_in_base = {
"XMR": portfolio.Amount("BTC", D("0.9")),
"BTC": portfolio.Amount("BTC", D("0.4")),
"ETH": portfolio.Amount("BTC", D("0.3")),
}
+ side_effect = [
+ (True, 1),
+ (False, 2),
+ (False, 3),
+ (True, 4),
+ (True, 5)
+ ]
- portfolio.TradeStore.compute_trades(values_in_base,
- new_repartition, only="only", market="market")
-
- self.assertEqual(5, add_trade_if_matching.call_count)
- add_trade_if_matching.assert_any_call(
- portfolio.Amount("BTC", D("0.9")),
- portfolio.Amount("BTC", 0),
- "XMR", only="only", market="market"
- )
- add_trade_if_matching.assert_any_call(
- portfolio.Amount("BTC", D("0.4")),
- portfolio.Amount("BTC", D("0.5")),
- "DASH", only="only", market="market"
- )
- add_trade_if_matching.assert_any_call(
- portfolio.Amount("BTC", D("-0.5")),
- portfolio.Amount("BTC", D("0")),
- "XVG", only="only", market="market"
- )
- add_trade_if_matching.assert_any_call(
- portfolio.Amount("BTC", D("0")),
- portfolio.Amount("BTC", D("0.1")),
- "XVG", only="only", market="market"
- )
- add_trade_if_matching.assert_any_call(
- portfolio.Amount("BTC", D("0")),
- portfolio.Amount("BTC", D("0.3")),
- "ETH", only="only", market="market"
- )
-
- def test_add_trade_if_matching(self):
- result = portfolio.TradeStore.add_trade_if_matching(
- portfolio.Amount("BTC", D("0")),
- portfolio.Amount("BTC", D("0.3")),
- "ETH", only="nope", market="market"
- )
- self.assertEqual(0, len(portfolio.TradeStore.all))
- self.assertEqual(False, result)
-
- portfolio.TradeStore.all = []
- result = portfolio.TradeStore.add_trade_if_matching(
- portfolio.Amount("BTC", D("0")),
- portfolio.Amount("BTC", D("0.3")),
- "ETH", only=None, market="market"
- )
- self.assertEqual(1, len(portfolio.TradeStore.all))
- self.assertEqual(True, result)
-
- portfolio.TradeStore.all = []
- result = portfolio.TradeStore.add_trade_if_matching(
- portfolio.Amount("BTC", D("0")),
- portfolio.Amount("BTC", D("0.3")),
- "ETH", only="acquire", market="market"
- )
- self.assertEqual(1, len(portfolio.TradeStore.all))
- self.assertEqual(True, result)
-
- portfolio.TradeStore.all = []
- result = portfolio.TradeStore.add_trade_if_matching(
- portfolio.Amount("BTC", D("0")),
- portfolio.Amount("BTC", D("0.3")),
- "ETH", only="dispose", market="market"
- )
- self.assertEqual(0, len(portfolio.TradeStore.all))
- self.assertEqual(False, result)
+ with mock.patch.object(market.TradeStore, "trade_if_matching") as trade_if_matching:
+ trade_store = market.TradeStore(self.m)
+ trade_if_matching.side_effect = side_effect
+
+ trade_store.compute_trades(values_in_base,
+ new_repartition, only="only")
+
+ self.assertEqual(5, trade_if_matching.call_count)
+ self.assertEqual(3, len(trade_store.all))
+ self.assertEqual([1, 4, 5], trade_store.all)
+ self.m.report.log_trades.assert_called_with(side_effect, "only")
+
+ def test_trade_if_matching(self):
+
+ with self.subTest(only="nope"):
+ trade_store = market.TradeStore(self.m)
+ result = trade_store.trade_if_matching(
+ portfolio.Amount("BTC", D("0")),
+ portfolio.Amount("BTC", D("0.3")),
+ "ETH", only="nope")
+ self.assertEqual(False, result[0])
+ self.assertIsInstance(result[1], portfolio.Trade)
+
+ with self.subTest(only=None):
+ trade_store = market.TradeStore(self.m)
+ result = trade_store.trade_if_matching(
+ portfolio.Amount("BTC", D("0")),
+ portfolio.Amount("BTC", D("0.3")),
+ "ETH", only=None)
+ self.assertEqual(True, result[0])
+
+ with self.subTest(only="acquire"):
+ trade_store = market.TradeStore(self.m)
+ result = trade_store.trade_if_matching(
+ portfolio.Amount("BTC", D("0")),
+ portfolio.Amount("BTC", D("0.3")),
+ "ETH", only="acquire")
+ self.assertEqual(True, result[0])
+
+ with self.subTest(only="dispose"):
+ trade_store = market.TradeStore(self.m)
+ result = trade_store.trade_if_matching(
+ portfolio.Amount("BTC", D("0")),
+ portfolio.Amount("BTC", D("0.3")),
+ "ETH", only="dispose")
+ self.assertEqual(False, result[0])
def test_prepare_orders(self):
+ trade_store = market.TradeStore(self.m)
+
trade_mock1 = mock.Mock()
trade_mock2 = mock.Mock()
- portfolio.TradeStore.all.append(trade_mock1)
- portfolio.TradeStore.all.append(trade_mock2)
+ trade_mock1.prepare_order.return_value = 1
+ trade_mock2.prepare_order.return_value = 2
- portfolio.TradeStore.prepare_orders()
+ trade_store.all.append(trade_mock1)
+ trade_store.all.append(trade_mock2)
+
+ trade_store.prepare_orders()
trade_mock1.prepare_order.assert_called_with(compute_value="default")
trade_mock2.prepare_order.assert_called_with(compute_value="default")
+ self.m.report.log_orders.assert_called_once_with([1, 2], None, "default")
+
+ self.m.report.log_orders.reset_mock()
- portfolio.TradeStore.prepare_orders(compute_value="bla")
+ trade_store.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")
+ self.m.report.log_orders.assert_called_once_with([1, 2], None, "bla")
trade_mock1.prepare_order.reset_mock()
trade_mock2.prepare_order.reset_mock()
+ self.m.report.log_orders.reset_mock()
trade_mock1.action = "foo"
trade_mock2.action = "bar"
- portfolio.TradeStore.prepare_orders(only="bar")
+ trade_store.prepare_orders(only="bar")
trade_mock1.prepare_order.assert_not_called()
trade_mock2.prepare_order.assert_called_with(compute_value="default")
+ self.m.report.log_orders.assert_called_once_with([2], "bar", "default")
def test_print_all_with_order(self):
trade_mock1 = mock.Mock()
trade_mock2 = mock.Mock()
trade_mock3 = mock.Mock()
- portfolio.TradeStore.all = [trade_mock1, trade_mock2, trade_mock3]
+ trade_store = market.TradeStore(self.m)
+ trade_store.all = [trade_mock1, trade_mock2, trade_mock3]
- portfolio.TradeStore.print_all_with_order()
+ trade_store.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.object(portfolio.TradeStore, "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.TradeStore.run_orders()
- all_orders.assert_called_with(state="pending")
+ def test_run_orders(self):
+ with mock.patch.object(market.TradeStore, "all_orders") as all_orders:
+ order_mock1 = mock.Mock()
+ order_mock2 = mock.Mock()
+ order_mock3 = mock.Mock()
+ trade_store = market.TradeStore(self.m)
+
+ all_orders.return_value = [order_mock1, order_mock2, order_mock3]
+
+ trade_store.run_orders()
+
+ all_orders.assert_called_with(state="pending")
order_mock1.run.assert_called()
order_mock2.run.assert_called()
order_mock3.run.assert_called()
+ self.m.report.log_stage.assert_called_with("run_orders")
+ self.m.report.log_orders.assert_called_with([order_mock1, order_mock2,
+ order_mock3])
+
def test_all_orders(self):
trade_mock1 = mock.Mock()
trade_mock2 = mock.Mock()
order_mock2.status = "open"
order_mock3.status = "open"
- portfolio.TradeStore.all.append(trade_mock1)
- portfolio.TradeStore.all.append(trade_mock2)
+ trade_store = market.TradeStore(self.m)
+ trade_store.all.append(trade_mock1)
+ trade_store.all.append(trade_mock2)
- orders = portfolio.TradeStore.all_orders()
+ orders = trade_store.all_orders()
self.assertEqual(3, len(orders))
- open_orders = portfolio.TradeStore.all_orders(state="open")
+ open_orders = trade_store.all_orders(state="open")
self.assertEqual(2, len(open_orders))
self.assertEqual([order_mock2, order_mock3], open_orders)
- @mock.patch.object(portfolio.TradeStore, "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.TradeStore.update_all_orders_status()
- all_orders.assert_called_with(state="open")
+ def test_update_all_orders_status(self):
+ with mock.patch.object(market.TradeStore, "all_orders") as all_orders:
+ order_mock1 = mock.Mock()
+ order_mock2 = mock.Mock()
+ order_mock3 = mock.Mock()
+
+ all_orders.return_value = [order_mock1, order_mock2, order_mock3]
+
+ trade_store = market.TradeStore(self.m)
+
+ trade_store.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()
- order_mock1.get_status.assert_called()
- order_mock2.get_status.assert_called()
- order_mock3.get_status.assert_called()
@unittest.skipUnless("unit" in limits, "Unit skipped")
class BalanceStoreTest(WebMockTestCase):
},
}
- @mock.patch.object(helper, "get_ticker")
- def test_in_currency(self, get_ticker):
- portfolio.BalanceStore.all = {
+ def test_in_currency(self):
+ self.m.get_ticker.return_value = {
+ "bid": D("0.09"),
+ "ask": D("0.11"),
+ "average": D("0.1"),
+ }
+
+ balance_store = market.BalanceStore(self.m)
+ balance_store.all = {
"BTC": portfolio.Balance("BTC", {
"total": "0.65",
"exchange_total":"0.65",
"exchange_free": 3,
"exchange_used": 0}),
}
- market = mock.Mock()
- get_ticker.return_value = {
- "bid": D("0.09"),
- "ask": D("0.11"),
- "average": D("0.1"),
- }
- amounts = portfolio.BalanceStore.in_currency("BTC", market)
+ amounts = balance_store.in_currency("BTC")
self.assertEqual("BTC", amounts["ETH"].currency)
self.assertEqual(D("0.65"), amounts["BTC"].value)
self.assertEqual(D("0.30"), amounts["ETH"].value)
+ self.m.report.log_tickers.assert_called_once_with(amounts, "BTC",
+ "average", "total")
+ self.m.report.log_tickers.reset_mock()
- amounts = portfolio.BalanceStore.in_currency("BTC", market, compute_value="bid")
+ amounts = balance_store.in_currency("BTC", compute_value="bid")
self.assertEqual(D("0.65"), amounts["BTC"].value)
self.assertEqual(D("0.27"), amounts["ETH"].value)
+ self.m.report.log_tickers.assert_called_once_with(amounts, "BTC",
+ "bid", "total")
+ self.m.report.log_tickers.reset_mock()
- amounts = portfolio.BalanceStore.in_currency("BTC", market, compute_value="bid", type="exchange_used")
+ amounts = balance_store.in_currency("BTC", compute_value="bid", type="exchange_used")
self.assertEqual(D("0.30"), amounts["BTC"].value)
self.assertEqual(0, amounts["ETH"].value)
+ self.m.report.log_tickers.assert_called_once_with(amounts, "BTC",
+ "bid", "exchange_used")
+ self.m.report.log_tickers.reset_mock()
def test_fetch_balances(self):
- market = mock.Mock()
- market.fetch_all_balances.return_value = self.fetch_balance
+ self.m.ccxt.fetch_all_balances.return_value = self.fetch_balance
+
+ balance_store = market.BalanceStore(self.m)
- portfolio.BalanceStore.fetch_balances(market)
- self.assertNotIn("ETC", portfolio.BalanceStore.currencies())
- self.assertListEqual(["USDT", "XVG", "XMR"], list(portfolio.BalanceStore.currencies()))
+ balance_store.fetch_balances()
+ self.assertNotIn("ETC", balance_store.currencies())
+ self.assertListEqual(["USDT", "XVG", "XMR"], list(balance_store.currencies()))
- portfolio.BalanceStore.all["ETC"] = portfolio.Balance("ETC", {
+ balance_store.all["ETC"] = portfolio.Balance("ETC", {
"exchange_total": "1", "exchange_free": "0",
"exchange_used": "1" })
- portfolio.BalanceStore.fetch_balances(market)
- self.assertEqual(0, portfolio.BalanceStore.all["ETC"].total)
- self.assertListEqual(["USDT", "XVG", "XMR", "ETC"], list(portfolio.BalanceStore.currencies()))
+ balance_store.fetch_balances(tag="foo")
+ self.assertEqual(0, balance_store.all["ETC"].total)
+ self.assertListEqual(["USDT", "XVG", "XMR", "ETC"], list(balance_store.currencies()))
+ self.m.report.log_balances.assert_called_with(tag="foo")
@mock.patch.object(portfolio.Portfolio, "repartition")
def test_dispatch_assets(self, repartition):
- market = mock.Mock()
- market.fetch_all_balances.return_value = self.fetch_balance
- portfolio.BalanceStore.fetch_balances(market)
+ self.m.ccxt.fetch_all_balances.return_value = self.fetch_balance
- self.assertNotIn("XEM", portfolio.BalanceStore.currencies())
+ balance_store = market.BalanceStore(self.m)
+ balance_store.fetch_balances()
- repartition.return_value = {
+ self.assertNotIn("XEM", balance_store.currencies())
+
+ repartition_hash = {
"XEM": (D("0.75"), "long"),
"BTC": (D("0.26"), "long"),
"DASH": (D("0.10"), "short"),
}
+ repartition.return_value = repartition_hash
- amounts = portfolio.BalanceStore.dispatch_assets(portfolio.Amount("BTC", "11.1"))
- self.assertIn("XEM", portfolio.BalanceStore.currencies())
+ amounts = balance_store.dispatch_assets(portfolio.Amount("BTC", "11.1"))
+ repartition.assert_called_with(self.m, liquidity="medium")
+ self.assertIn("XEM", balance_store.currencies())
self.assertEqual(D("2.6"), amounts["BTC"].value)
self.assertEqual(D("7.5"), amounts["XEM"].value)
self.assertEqual(D("-1.0"), amounts["DASH"].value)
+ self.m.report.log_balances.assert_called_with(tag=None)
+ self.m.report.log_dispatch.assert_called_once_with(portfolio.Amount("BTC",
+ "11.1"), amounts, "medium", repartition_hash)
def test_currencies(self):
- portfolio.BalanceStore.all = {
+ balance_store = market.BalanceStore(self.m)
+
+ balance_store.all = {
"BTC": portfolio.Balance("BTC", {
"total": "0.65",
"exchange_total":"0.65",
"exchange_free": 3,
"exchange_used": 0}),
}
- self.assertListEqual(["BTC", "ETH"], list(portfolio.BalanceStore.currencies()))
+ self.assertListEqual(["BTC", "ETH"], list(balance_store.currencies()))
+
+ def test_as_json(self):
+ balance_mock1 = mock.Mock()
+ balance_mock1.as_json.return_value = 1
+
+ balance_mock2 = mock.Mock()
+ balance_mock2.as_json.return_value = 2
+
+ balance_store = market.BalanceStore(self.m)
+ balance_store.all = {
+ "BTC": balance_mock1,
+ "ETH": balance_mock2,
+ }
+
+ as_json = balance_store.as_json()
+ self.assertEqual(1, as_json["BTC"])
+ self.assertEqual(2, as_json["ETH"])
+
@unittest.skipUnless("unit" in limits, "Unit skipped")
class ComputationTest(WebMockTestCase):
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")
+ trade = portfolio.Trade(value_from, value_to, "ETH", self.m)
self.assertEqual("BTC", trade.base_currency)
self.assertEqual("ETH", trade.currency)
+ self.assertEqual(self.m, trade.market)
with self.assertRaises(AssertionError):
- portfolio.Trade(value_from, value_to, "ETC")
+ portfolio.Trade(value_from, value_to, "ETC", self.m)
with self.assertRaises(AssertionError):
value_from.linked_to = None
- portfolio.Trade(value_from, value_to, "ETH")
+ portfolio.Trade(value_from, value_to, "ETH", self.m)
with self.assertRaises(AssertionError):
value_from.currency = "ETH"
- portfolio.Trade(value_from, value_to, "ETH")
+ portfolio.Trade(value_from, value_to, "ETH", self.m)
value_from = portfolio.Amount("BTC", 0)
- trade = portfolio.Trade(value_from, value_to, "ETH")
+ trade = portfolio.Trade(value_from, value_to, "ETH", self.m)
self.assertEqual(0, trade.value_from.linked_to)
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")
+ trade = portfolio.Trade(value_from, value_to, "ETH", self.m)
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", "2.0")
- trade = portfolio.Trade(value_from, value_to, "BTC")
+ trade = portfolio.Trade(value_from, value_to, "BTC", self.m)
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")
+ trade = portfolio.Trade(value_from, value_to, "ETH", self.m)
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")
+ trade = portfolio.Trade(value_from, value_to, "ETH", self.m)
- self.assertEqual("dispose", trade.action)
+ self.assertEqual("acquire", 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")
+ trade = portfolio.Trade(value_from, value_to, "ETH", self.m)
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")
+ trade = portfolio.Trade(value_from, value_to, "ETH", self.m)
self.assertEqual("sell", trade.order_action(False))
self.assertEqual("buy", trade.order_action(True))
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")
+ trade = portfolio.Trade(value_from, value_to, "ETH", self.m)
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")
+ trade = portfolio.Trade(value_from, value_to, "ETH", self.m)
self.assertEqual("short", trade.trade_type)
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")
+ trade = portfolio.Trade(value_from, value_to, "ETH", self.m)
order1 = mock.Mock()
order1.filled_amount.return_value = portfolio.Amount("ETH", "0.3")
order1.filled_amount.assert_called_with(in_base_currency=True)
order2.filled_amount.assert_called_with(in_base_currency=True)
- @mock.patch.object(helper, "get_ticker")
@mock.patch.object(portfolio.Computation, "compute_value")
@mock.patch.object(portfolio.Trade, "filled_amount")
@mock.patch.object(portfolio, "Order")
- def test_prepare_order(self, Order, filled_amount, compute_value, get_ticker):
+ def test_prepare_order(self, Order, filled_amount, compute_value):
Order.return_value = "Order"
with self.subTest(desc="Nothing to do"):
value_from.rate = D("0.1")
value_from.linked_to = portfolio.Amount("FOO", "100")
value_to = portfolio.Amount("BTC", "10")
- trade = portfolio.Trade(value_from, value_to, "FOO", market="market")
+ trade = portfolio.Trade(value_from, value_to, "FOO", self.m)
trade.prepare_order()
self.assertEqual(0, len(trade.orders))
Order.assert_not_called()
- get_ticker.return_value = { "inverted": False }
- with self.subTest(desc="Already filled"), mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock:
+ self.m.get_ticker.return_value = { "inverted": False }
+ with self.subTest(desc="Already filled"):
filled_amount.return_value = portfolio.Amount("FOO", "100")
compute_value.return_value = D("0.125")
value_from.rate = D("0.1")
value_from.linked_to = portfolio.Amount("FOO", "100")
value_to = portfolio.Amount("BTC", "0")
- trade = portfolio.Trade(value_from, value_to, "FOO", market="market")
+ trade = portfolio.Trade(value_from, value_to, "FOO", self.m)
trade.prepare_order()
filled_amount.assert_called_with(in_base_currency=False)
- compute_value.assert_called_with(get_ticker.return_value, "sell", compute_value="default")
+ compute_value.assert_called_with(self.m.get_ticker.return_value, "sell", compute_value="default")
self.assertEqual(0, len(trade.orders))
- self.assertRegex(stdout_mock.getvalue(), "Less to do than already filled: ")
+ self.m.report.log_error.assert_called_with("prepare_order", message=mock.ANY)
Order.assert_not_called()
with self.subTest(action="dispose", inverted=False):
value_from.rate = D("0.1")
value_from.linked_to = portfolio.Amount("FOO", "100")
value_to = portfolio.Amount("BTC", "1")
- trade = portfolio.Trade(value_from, value_to, "FOO", market="market")
+ trade = portfolio.Trade(value_from, value_to, "FOO", self.m)
trade.prepare_order()
filled_amount.assert_called_with(in_base_currency=False)
- compute_value.assert_called_with(get_ticker.return_value, "sell", compute_value="default")
+ compute_value.assert_called_with(self.m.get_ticker.return_value, "sell", compute_value="default")
self.assertEqual(1, len(trade.orders))
Order.assert_called_with("sell", portfolio.Amount("FOO", 30),
- D("0.125"), "BTC", "long", "market",
+ D("0.125"), "BTC", "long", self.m,
trade, close_if_possible=False)
with self.subTest(action="acquire", inverted=False):
value_from.rate = D("0.1")
value_from.linked_to = portfolio.Amount("FOO", "10")
value_to = portfolio.Amount("BTC", "10")
- trade = portfolio.Trade(value_from, value_to, "FOO", market="market")
+ trade = portfolio.Trade(value_from, value_to, "FOO", self.m)
trade.prepare_order()
filled_amount.assert_called_with(in_base_currency=True)
- compute_value.assert_called_with(get_ticker.return_value, "buy", compute_value="default")
+ compute_value.assert_called_with(self.m.get_ticker.return_value, "buy", compute_value="default")
self.assertEqual(1, len(trade.orders))
Order.assert_called_with("buy", portfolio.Amount("FOO", 48),
- D("0.125"), "BTC", "long", "market",
+ D("0.125"), "BTC", "long", self.m,
trade, close_if_possible=False)
with self.subTest(close_if_possible=True):
value_from.rate = D("0.1")
value_from.linked_to = portfolio.Amount("FOO", "100")
value_to = portfolio.Amount("BTC", "0")
- trade = portfolio.Trade(value_from, value_to, "FOO", market="market")
+ trade = portfolio.Trade(value_from, value_to, "FOO", self.m)
trade.prepare_order()
filled_amount.assert_called_with(in_base_currency=False)
- compute_value.assert_called_with(get_ticker.return_value, "sell", compute_value="default")
+ compute_value.assert_called_with(self.m.get_ticker.return_value, "sell", compute_value="default")
self.assertEqual(1, len(trade.orders))
Order.assert_called_with("sell", portfolio.Amount("FOO", 100),
- D("0.125"), "BTC", "long", "market",
+ D("0.125"), "BTC", "long", self.m,
trade, close_if_possible=True)
- get_ticker.return_value = { "inverted": True, "original": {} }
+ self.m.get_ticker.return_value = { "inverted": True, "original": {} }
with self.subTest(action="dispose", inverted=True):
filled_amount.return_value = portfolio.Amount("FOO", "300")
compute_value.return_value = D("125")
value_from.rate = D("0.01")
value_from.linked_to = portfolio.Amount("FOO", "1000")
value_to = portfolio.Amount("BTC", "1")
- trade = portfolio.Trade(value_from, value_to, "FOO", market="market")
+ trade = portfolio.Trade(value_from, value_to, "FOO", self.m)
trade.prepare_order(compute_value="foo")
filled_amount.assert_called_with(in_base_currency=True)
- compute_value.assert_called_with(get_ticker.return_value["original"], "buy", compute_value="foo")
+ compute_value.assert_called_with(self.m.get_ticker.return_value["original"], "buy", compute_value="foo")
self.assertEqual(1, len(trade.orders))
Order.assert_called_with("buy", portfolio.Amount("BTC", D("4.8")),
- D("125"), "FOO", "long", "market",
+ D("125"), "FOO", "long", self.m,
trade, close_if_possible=False)
with self.subTest(action="acquire", inverted=True):
value_from.rate = D("0.01")
value_from.linked_to = portfolio.Amount("FOO", "100")
value_to = portfolio.Amount("BTC", "10")
- trade = portfolio.Trade(value_from, value_to, "FOO", market="market")
+ trade = portfolio.Trade(value_from, value_to, "FOO", self.m)
trade.prepare_order(compute_value="foo")
filled_amount.assert_called_with(in_base_currency=False)
- compute_value.assert_called_with(get_ticker.return_value["original"], "sell", compute_value="foo")
+ compute_value.assert_called_with(self.m.get_ticker.return_value["original"], "sell", compute_value="foo")
self.assertEqual(1, len(trade.orders))
Order.assert_called_with("sell", portfolio.Amount("BTC", D("5")),
- D("125"), "FOO", "long", "market",
+ D("125"), "FOO", "long", self.m,
trade, close_if_possible=False)
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")
- def _prepare_order(compute_value=None):
- trade.orders.append(new_order_mock)
- prepare_order.side_effect = _prepare_order
+ trade = portfolio.Trade(value_from, value_to, "ETH", self.m)
+ prepare_order.return_value = new_order_mock
for i in [0, 1, 3, 4, 6]:
- with self.subTest(tick=i), mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock:
+ with self.subTest(tick=i):
trade.update_order(order_mock, i)
order_mock.cancel.assert_not_called()
new_order_mock.run.assert_not_called()
- self.assertRegex(stdout_mock.getvalue(), "tick {}, waiting".format(i))
- self.assertEqual(0, len(trade.orders))
+ self.m.report.log_order.assert_called_once_with(order_mock, i,
+ update="waiting", compute_value=None, new_order=None)
order_mock.reset_mock()
new_order_mock.reset_mock()
trade.orders = []
-
- with mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock:
- trade.update_order(order_mock, 2)
- order_mock.cancel.assert_called()
- new_order_mock.run.assert_called()
- prepare_order.assert_called()
- self.assertRegex(stdout_mock.getvalue(), "tick 2, cancelling and adjusting")
- self.assertEqual(1, len(trade.orders))
+ self.m.report.log_order.reset_mock()
+
+ trade.update_order(order_mock, 2)
+ order_mock.cancel.assert_called()
+ new_order_mock.run.assert_called()
+ prepare_order.assert_called()
+ self.m.report.log_order.assert_called()
+ self.assertEqual(2, self.m.report.log_order.call_count)
+ calls = [
+ mock.call(order_mock, 2, update="adjusting",
+ compute_value='lambda x, y: (x[y] + x["average"]) / 2',
+ new_order=new_order_mock),
+ mock.call(order_mock, 2, new_order=new_order_mock),
+ ]
+ self.m.report.log_order.assert_has_calls(calls)
order_mock.reset_mock()
new_order_mock.reset_mock()
trade.orders = []
-
- with mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock:
- trade.update_order(order_mock, 5)
- order_mock.cancel.assert_called()
- new_order_mock.run.assert_called()
- prepare_order.assert_called()
- self.assertRegex(stdout_mock.getvalue(), "tick 5, cancelling and adjusting")
- self.assertEqual(1, len(trade.orders))
+ self.m.report.log_order.reset_mock()
+
+ trade.update_order(order_mock, 5)
+ order_mock.cancel.assert_called()
+ new_order_mock.run.assert_called()
+ prepare_order.assert_called()
+ self.assertEqual(2, self.m.report.log_order.call_count)
+ self.m.report.log_order.assert_called()
+ calls = [
+ mock.call(order_mock, 5, update="adjusting",
+ compute_value='lambda x, y: (x[y]*2 + x["average"]) / 3',
+ new_order=new_order_mock),
+ mock.call(order_mock, 5, new_order=new_order_mock),
+ ]
+ self.m.report.log_order.assert_has_calls(calls)
order_mock.reset_mock()
new_order_mock.reset_mock()
trade.orders = []
-
- with mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock:
- trade.update_order(order_mock, 7)
- order_mock.cancel.assert_called()
- new_order_mock.run.assert_called()
- prepare_order.assert_called_with(compute_value="default")
- self.assertRegex(stdout_mock.getvalue(), "tick 7, fallbacking to market value")
- self.assertRegex(stdout_mock.getvalue(), "tick 7, market value, cancelling and adjusting to")
- self.assertEqual(1, len(trade.orders))
+ self.m.report.log_order.reset_mock()
+
+ trade.update_order(order_mock, 7)
+ order_mock.cancel.assert_called()
+ new_order_mock.run.assert_called()
+ prepare_order.assert_called_with(compute_value="default")
+ self.m.report.log_order.assert_called()
+ self.assertEqual(2, self.m.report.log_order.call_count)
+ calls = [
+ mock.call(order_mock, 7, update="market_fallback",
+ compute_value='default',
+ new_order=new_order_mock),
+ mock.call(order_mock, 7, new_order=new_order_mock),
+ ]
+ self.m.report.log_order.assert_has_calls(calls)
order_mock.reset_mock()
new_order_mock.reset_mock()
trade.orders = []
+ self.m.report.log_order.reset_mock()
for i in [10, 13, 16]:
- with self.subTest(tick=i), mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock:
+ with self.subTest(tick=i):
trade.update_order(order_mock, i)
order_mock.cancel.assert_called()
new_order_mock.run.assert_called()
prepare_order.assert_called_with(compute_value="default")
- self.assertNotRegex(stdout_mock.getvalue(), "tick {}, fallbacking to market value".format(i))
- self.assertRegex(stdout_mock.getvalue(), "tick {}, market value, cancelling and adjusting to".format(i))
- self.assertEqual(1, len(trade.orders))
+ self.m.report.log_order.assert_called()
+ self.assertEqual(2, self.m.report.log_order.call_count)
+ calls = [
+ mock.call(order_mock, i, update="market_adjust",
+ compute_value='default',
+ new_order=new_order_mock),
+ mock.call(order_mock, i, new_order=new_order_mock),
+ ]
+ self.m.report.log_order.assert_has_calls(calls)
order_mock.reset_mock()
new_order_mock.reset_mock()
trade.orders = []
+ self.m.report.log_order.reset_mock()
for i in [8, 9, 11, 12]:
- with self.subTest(tick=i), mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock:
+ with self.subTest(tick=i):
trade.update_order(order_mock, i)
order_mock.cancel.assert_not_called()
new_order_mock.run.assert_not_called()
- self.assertEqual("", stdout_mock.getvalue())
- self.assertEqual(0, len(trade.orders))
+ self.m.report.log_order.assert_called_once_with(order_mock, i, update="waiting",
+ compute_value=None, new_order=None)
order_mock.reset_mock()
new_order_mock.reset_mock()
trade.orders = []
+ self.m.report.log_order.reset_mock()
- @mock.patch('sys.stdout', new_callable=StringIO)
- def test_print_with_order(self, mock_stdout):
+ def test_print_with_order(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")
+ trade = portfolio.Trade(value_from, value_to, "ETH", self.m)
order_mock1 = mock.Mock()
order_mock1.__repr__ = mock.Mock()
order_mock2 = mock.Mock()
order_mock2.__repr__ = mock.Mock()
order_mock2.__repr__.return_value = "Mock 2"
+ order_mock1.mouvements = []
+ mouvement_mock1 = mock.Mock()
+ mouvement_mock1.__repr__ = mock.Mock()
+ mouvement_mock1.__repr__.return_value = "Mouvement 1"
+ mouvement_mock2 = mock.Mock()
+ mouvement_mock2.__repr__ = mock.Mock()
+ mouvement_mock2.__repr__.return_value = "Mouvement 2"
+ order_mock2.mouvements = [
+ mouvement_mock1, mouvement_mock2
+ ]
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])
+ self.m.report.print_log.assert_called()
+ calls = self.m.report.print_log.mock_calls
+ self.assertEqual("Trade(0.50000000 BTC [10.00000000 ETH] -> 1.00000000 BTC in ETH, acquire)", str(calls[0][1][0]))
+ self.assertEqual("\tMock 1", str(calls[1][1][0]))
+ self.assertEqual("\tMock 2", str(calls[2][1][0]))
+ self.assertEqual("\t\tMouvement 1", str(calls[3][1][0]))
+ self.assertEqual("\t\tMouvement 2", str(calls[4][1][0]))
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")
+ trade = portfolio.Trade(value_from, value_to, "ETH", self.m)
self.assertEqual("Trade(0.50000000 BTC [10.00000000 ETH] -> 1.00000000 BTC in ETH, acquire)", str(trade))
+ def test_as_json(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.m)
+
+ as_json = trade.as_json()
+ self.assertEqual("acquire", as_json["action"])
+ self.assertEqual(D("0.5"), as_json["from"])
+ self.assertEqual(D("1.0"), as_json["to"])
+ self.assertEqual("ETH", as_json["currency"])
+ self.assertEqual("BTC", as_json["base_currency"])
+
+@unittest.skipUnless("unit" in limits, "Unit skipped")
+class OrderTest(WebMockTestCase):
+ def test_values(self):
+ order = portfolio.Order("buy", portfolio.Amount("ETH", 10),
+ D("0.1"), "BTC", "long", "market", "trade")
+ self.assertEqual("buy", order.action)
+ self.assertEqual(10, order.amount.value)
+ self.assertEqual("ETH", order.amount.currency)
+ self.assertEqual(D("0.1"), order.rate)
+ self.assertEqual("BTC", order.base_currency)
+ self.assertEqual("market", order.market)
+ self.assertEqual("long", order.trade_type)
+ self.assertEqual("pending", order.status)
+ self.assertEqual("trade", order.trade)
+ self.assertIsNone(order.id)
+ self.assertFalse(order.close_if_possible)
+
+ def test__repr(self):
+ order = portfolio.Order("buy", portfolio.Amount("ETH", 10),
+ D("0.1"), "BTC", "long", "market", "trade")
+ self.assertEqual("Order(buy long 10.00000000 ETH at 0.1 BTC [pending])", repr(order))
+
+ order = portfolio.Order("buy", portfolio.Amount("ETH", 10),
+ D("0.1"), "BTC", "long", "market", "trade",
+ close_if_possible=True)
+ self.assertEqual("Order(buy long 10.00000000 ETH at 0.1 BTC [pending] ✂)", repr(order))
+
+ def test_as_json(self):
+ order = portfolio.Order("buy", portfolio.Amount("ETH", 10),
+ D("0.1"), "BTC", "long", "market", "trade")
+ mouvement_mock1 = mock.Mock()
+ mouvement_mock1.as_json.return_value = 1
+ mouvement_mock2 = mock.Mock()
+ mouvement_mock2.as_json.return_value = 2
+
+ order.mouvements = [mouvement_mock1, mouvement_mock2]
+ as_json = order.as_json()
+ self.assertEqual("buy", as_json["action"])
+ self.assertEqual("long", as_json["trade_type"])
+ self.assertEqual(10, as_json["amount"])
+ self.assertEqual("ETH", as_json["currency"])
+ self.assertEqual("BTC", as_json["base_currency"])
+ self.assertEqual(D("0.1"), as_json["rate"])
+ self.assertEqual("pending", as_json["status"])
+ self.assertEqual(False, as_json["close_if_possible"])
+ self.assertIsNone(as_json["id"])
+ self.assertEqual([1, 2], as_json["mouvements"])
+
+ def test_account(self):
+ order = portfolio.Order("buy", portfolio.Amount("ETH", 10),
+ D("0.1"), "BTC", "long", "market", "trade")
+ self.assertEqual("exchange", order.account)
+
+ order = portfolio.Order("sell", portfolio.Amount("ETH", 10),
+ D("0.1"), "BTC", "short", "market", "trade")
+ self.assertEqual("margin", order.account)
+
+ def test_pending(self):
+ order = portfolio.Order("buy", portfolio.Amount("ETH", 10),
+ D("0.1"), "BTC", "long", "market", "trade")
+ self.assertTrue(order.pending)
+ order.status = "open"
+ self.assertFalse(order.pending)
+
+ def test_open(self):
+ order = portfolio.Order("buy", portfolio.Amount("ETH", 10),
+ D("0.1"), "BTC", "long", "market", "trade")
+ self.assertFalse(order.open)
+ order.status = "open"
+ self.assertTrue(order.open)
+
+ def test_finished(self):
+ order = portfolio.Order("buy", portfolio.Amount("ETH", 10),
+ D("0.1"), "BTC", "long", "market", "trade")
+ self.assertFalse(order.finished)
+ order.status = "closed"
+ self.assertTrue(order.finished)
+ order.status = "canceled"
+ self.assertTrue(order.finished)
+ order.status = "error"
+ self.assertTrue(order.finished)
+
+ @mock.patch.object(portfolio.Order, "fetch")
+ def test_cancel(self, fetch):
+ self.m.debug = True
+ order = portfolio.Order("buy", portfolio.Amount("ETH", 10),
+ D("0.1"), "BTC", "long", self.m, "trade")
+ order.status = "open"
+
+ order.cancel()
+ self.m.ccxt.cancel_order.assert_not_called()
+ self.m.report.log_debug_action.assert_called_once()
+ self.m.report.log_debug_action.reset_mock()
+ self.assertEqual("canceled", order.status)
+
+ self.m.debug = False
+ order = portfolio.Order("buy", portfolio.Amount("ETH", 10),
+ D("0.1"), "BTC", "long", self.m, "trade")
+ order.status = "open"
+ order.id = 42
+
+ order.cancel()
+ self.m.ccxt.cancel_order.assert_called_with(42)
+ fetch.assert_called_once()
+ self.m.report.log_debug_action.assert_not_called()
+
+ def test_dust_amount_remaining(self):
+ order = portfolio.Order("buy", portfolio.Amount("ETH", 10),
+ D("0.1"), "BTC", "long", self.m, "trade")
+ order.remaining_amount = mock.Mock(return_value=portfolio.Amount("ETH", 1))
+ self.assertFalse(order.dust_amount_remaining())
+
+ order.remaining_amount = mock.Mock(return_value=portfolio.Amount("ETH", D("0.0001")))
+ self.assertTrue(order.dust_amount_remaining())
+
+ @mock.patch.object(portfolio.Order, "fetch")
+ @mock.patch.object(portfolio.Order, "filled_amount", return_value=portfolio.Amount("ETH", 1))
+ def test_remaining_amount(self, filled_amount, fetch):
+ order = portfolio.Order("buy", portfolio.Amount("ETH", 10),
+ D("0.1"), "BTC", "long", self.m, "trade")
+
+ self.assertEqual(9, order.remaining_amount().value)
+ order.fetch.assert_not_called()
+
+ order.status = "open"
+ self.assertEqual(9, order.remaining_amount().value)
+ fetch.assert_called_once()
+
+ @mock.patch.object(portfolio.Order, "fetch")
+ def test_filled_amount(self, fetch):
+ order = portfolio.Order("buy", portfolio.Amount("ETH", 10),
+ D("0.1"), "BTC", "long", self.m, "trade")
+ order.mouvements.append(portfolio.Mouvement("ETH", "BTC", {
+ "tradeID": 42, "type": "buy", "fee": "0.0015",
+ "date": "2017-12-30 12:00:12", "rate": "0.1",
+ "amount": "3", "total": "0.3"
+ }))
+ order.mouvements.append(portfolio.Mouvement("ETH", "BTC", {
+ "tradeID": 43, "type": "buy", "fee": "0.0015",
+ "date": "2017-12-30 13:00:12", "rate": "0.2",
+ "amount": "2", "total": "0.4"
+ }))
+ self.assertEqual(portfolio.Amount("ETH", 5), order.filled_amount())
+ fetch.assert_not_called()
+ order.status = "open"
+ self.assertEqual(portfolio.Amount("ETH", 5), order.filled_amount(in_base_currency=False))
+ fetch.assert_called_once()
+ self.assertEqual(portfolio.Amount("BTC", "0.7"), order.filled_amount(in_base_currency=True))
+
+ def test_fetch_mouvements(self):
+ self.m.ccxt.privatePostReturnOrderTrades.return_value = [
+ {
+ "tradeID": 42, "type": "buy", "fee": "0.0015",
+ "date": "2017-12-30 12:00:12", "rate": "0.1",
+ "amount": "3", "total": "0.3"
+ },
+ {
+ "tradeID": 43, "type": "buy", "fee": "0.0015",
+ "date": "2017-12-30 13:00:12", "rate": "0.2",
+ "amount": "2", "total": "0.4"
+ }
+ ]
+ order = portfolio.Order("buy", portfolio.Amount("ETH", 10),
+ D("0.1"), "BTC", "long", self.m, "trade")
+ order.id = 12
+ order.mouvements = ["Foo", "Bar", "Baz"]
+
+ order.fetch_mouvements()
+
+ self.m.ccxt.privatePostReturnOrderTrades.assert_called_with({"orderNumber": 12})
+ self.assertEqual(2, len(order.mouvements))
+ self.assertEqual(42, order.mouvements[0].id)
+ self.assertEqual(43, order.mouvements[1].id)
+
+ self.m.ccxt.privatePostReturnOrderTrades.side_effect = portfolio.ExchangeError
+ order = portfolio.Order("buy", portfolio.Amount("ETH", 10),
+ D("0.1"), "BTC", "long", self.m, "trade")
+ order.fetch_mouvements()
+ self.assertEqual(0, len(order.mouvements))
+
+ def test_mark_finished_order(self):
+ order = portfolio.Order("buy", portfolio.Amount("ETH", 10),
+ D("0.1"), "BTC", "short", self.m, "trade",
+ close_if_possible=True)
+ order.status = "closed"
+ self.m.debug = False
+
+ order.mark_finished_order()
+ self.m.ccxt.close_margin_position.assert_called_with("ETH", "BTC")
+ self.m.ccxt.close_margin_position.reset_mock()
+
+ order.status = "open"
+ order.mark_finished_order()
+ self.m.ccxt.close_margin_position.assert_not_called()
+
+ order = portfolio.Order("buy", portfolio.Amount("ETH", 10),
+ D("0.1"), "BTC", "short", self.m, "trade",
+ close_if_possible=False)
+ order.status = "closed"
+ order.mark_finished_order()
+ self.m.ccxt.close_margin_position.assert_not_called()
+
+ order = portfolio.Order("sell", portfolio.Amount("ETH", 10),
+ D("0.1"), "BTC", "short", self.m, "trade",
+ close_if_possible=True)
+ order.status = "closed"
+ order.mark_finished_order()
+ self.m.ccxt.close_margin_position.assert_not_called()
+
+ order = portfolio.Order("buy", portfolio.Amount("ETH", 10),
+ D("0.1"), "BTC", "long", self.m, "trade",
+ close_if_possible=True)
+ order.status = "closed"
+ order.mark_finished_order()
+ self.m.ccxt.close_margin_position.assert_not_called()
+
+ self.m.debug = True
+
+ order = portfolio.Order("buy", portfolio.Amount("ETH", 10),
+ D("0.1"), "BTC", "short", self.m, "trade",
+ close_if_possible=True)
+ order.status = "closed"
+
+ order.mark_finished_order()
+ self.m.ccxt.close_margin_position.assert_not_called()
+ self.m.report.log_debug_action.assert_called_once()
+
+ @mock.patch.object(portfolio.Order, "fetch_mouvements")
+ def test_fetch(self, fetch_mouvements):
+ time = self.time.time()
+ with mock.patch.object(portfolio.time, "time") as time_mock:
+ order = portfolio.Order("buy", portfolio.Amount("ETH", 10),
+ D("0.1"), "BTC", "long", self.m, "trade")
+ order.id = 45
+ with self.subTest(debug=True):
+ self.m.debug = True
+ order.fetch()
+ time_mock.assert_not_called()
+ self.m.report.log_debug_action.assert_called_once()
+ self.m.report.log_debug_action.reset_mock()
+ order.fetch(force=True)
+ time_mock.assert_not_called()
+ self.m.ccxt.fetch_order.assert_not_called()
+ fetch_mouvements.assert_not_called()
+ self.m.report.log_debug_action.assert_called_once()
+ self.m.report.log_debug_action.reset_mock()
+ self.assertIsNone(order.fetch_cache_timestamp)
+
+ with self.subTest(debug=False):
+ self.m.debug = False
+ time_mock.return_value = time
+ self.m.ccxt.fetch_order.return_value = {
+ "status": "foo",
+ "datetime": "timestamp"
+ }
+ order.fetch()
+
+ self.m.ccxt.fetch_order.assert_called_once()
+ fetch_mouvements.assert_called_once()
+ self.assertEqual("foo", order.status)
+ self.assertEqual("timestamp", order.timestamp)
+ self.assertEqual(time, order.fetch_cache_timestamp)
+ self.assertEqual(1, len(order.results))
+
+ self.m.ccxt.fetch_order.reset_mock()
+ fetch_mouvements.reset_mock()
+
+ time_mock.return_value = time + 8
+ order.fetch()
+ self.m.ccxt.fetch_order.assert_not_called()
+ fetch_mouvements.assert_not_called()
+
+ order.fetch(force=True)
+ self.m.ccxt.fetch_order.assert_called_once()
+ fetch_mouvements.assert_called_once()
+
+ self.m.ccxt.fetch_order.reset_mock()
+ fetch_mouvements.reset_mock()
+
+ time_mock.return_value = time + 19
+ order.fetch()
+ self.m.ccxt.fetch_order.assert_called_once()
+ fetch_mouvements.assert_called_once()
+ self.m.report.log_debug_action.assert_not_called()
+
+ @mock.patch.object(portfolio.Order, "fetch")
+ @mock.patch.object(portfolio.Order, "mark_finished_order")
+ def test_get_status(self, mark_finished_order, fetch):
+ with self.subTest(debug=True):
+ self.m.debug = True
+ order = portfolio.Order("buy", portfolio.Amount("ETH", 10),
+ D("0.1"), "BTC", "long", self.m, "trade")
+ self.assertEqual("pending", order.get_status())
+ fetch.assert_not_called()
+ self.m.report.log_debug_action.assert_called_once()
+
+ with self.subTest(debug=False, finished=False):
+ self.m.debug = False
+ order = portfolio.Order("buy", portfolio.Amount("ETH", 10),
+ D("0.1"), "BTC", "long", self.m, "trade")
+ def _fetch(order):
+ def update_status():
+ order.status = "open"
+ return update_status
+ fetch.side_effect = _fetch(order)
+ self.assertEqual("open", order.get_status())
+ mark_finished_order.assert_not_called()
+ fetch.assert_called_once()
+
+ mark_finished_order.reset_mock()
+ fetch.reset_mock()
+ with self.subTest(debug=False, finished=True):
+ self.m.debug = False
+ order = portfolio.Order("buy", portfolio.Amount("ETH", 10),
+ D("0.1"), "BTC", "long", self.m, "trade")
+ def _fetch(order):
+ def update_status():
+ order.status = "closed"
+ return update_status
+ fetch.side_effect = _fetch(order)
+ self.assertEqual("closed", order.get_status())
+ mark_finished_order.assert_called_once()
+ fetch.assert_called_once()
+
+ def test_run(self):
+ self.m.ccxt.order_precision.return_value = 4
+ with self.subTest(debug=True):
+ self.m.debug = True
+ order = portfolio.Order("buy", portfolio.Amount("ETH", 10),
+ D("0.1"), "BTC", "long", self.m, "trade")
+ order.run()
+ self.m.ccxt.create_order.assert_not_called()
+ self.m.report.log_debug_action.assert_called_with("market.ccxt.create_order('ETH/BTC', 'limit', 'buy', 10.0000, price=0.1, account=exchange)")
+ self.assertEqual("open", order.status)
+ self.assertEqual(1, len(order.results))
+ self.assertEqual(-1, order.id)
+
+ self.m.ccxt.create_order.reset_mock()
+ with self.subTest(debug=False):
+ self.m.debug = False
+ order = portfolio.Order("buy", portfolio.Amount("ETH", 10),
+ D("0.1"), "BTC", "long", self.m, "trade")
+ self.m.ccxt.create_order.return_value = { "id": 123 }
+ order.run()
+ self.m.ccxt.create_order.assert_called_once()
+ self.assertEqual(1, len(order.results))
+ self.assertEqual("open", order.status)
+
+ self.m.ccxt.create_order.reset_mock()
+ with self.subTest(exception=True):
+ order = portfolio.Order("buy", portfolio.Amount("ETH", 10),
+ D("0.1"), "BTC", "long", self.m, "trade")
+ self.m.ccxt.create_order.side_effect = Exception("bouh")
+ order.run()
+ self.m.ccxt.create_order.assert_called_once()
+ self.assertEqual(0, len(order.results))
+ self.assertEqual("error", order.status)
+ self.m.report.log_error.assert_called_once()
+
+ self.m.ccxt.create_order.reset_mock()
+ with self.subTest(dust_amount_exception=True),\
+ mock.patch.object(portfolio.Order, "mark_finished_order") as mark_finished_order:
+ order = portfolio.Order("buy", portfolio.Amount("ETH", 0.001),
+ D("0.1"), "BTC", "long", self.m, "trade")
+ self.m.ccxt.create_order.side_effect = portfolio.InvalidOrder
+ order.run()
+ self.m.ccxt.create_order.assert_called_once()
+ self.assertEqual(0, len(order.results))
+ self.assertEqual("closed", order.status)
+ mark_finished_order.assert_called_once()
+
+ self.m.ccxt.order_precision.return_value = 8
+ self.m.ccxt.create_order.reset_mock()
+ with self.subTest(insufficient_funds=True),\
+ mock.patch.object(portfolio.Order, "mark_finished_order") as mark_finished_order:
+ order = portfolio.Order("buy", portfolio.Amount("ETH", "0.001"),
+ D("0.1"), "BTC", "long", self.m, "trade")
+ self.m.ccxt.create_order.side_effect = [
+ portfolio.InsufficientFunds,
+ portfolio.InsufficientFunds,
+ portfolio.InsufficientFunds,
+ { "id": 123 },
+ ]
+ order.run()
+ self.m.ccxt.create_order.assert_has_calls([
+ mock.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account='exchange', price=D('0.1')),
+ mock.call('ETH/BTC', 'limit', 'buy', D('0.00099'), account='exchange', price=D('0.1')),
+ mock.call('ETH/BTC', 'limit', 'buy', D('0.0009801'), account='exchange', price=D('0.1')),
+ mock.call('ETH/BTC', 'limit', 'buy', D('0.00097029'), account='exchange', price=D('0.1')),
+ ])
+ self.assertEqual(4, self.m.ccxt.create_order.call_count)
+ self.assertEqual(1, len(order.results))
+ self.assertEqual("open", order.status)
+ self.assertEqual(4, order.tries)
+ self.m.report.log_error.assert_called()
+ self.assertEqual(4, self.m.report.log_error.call_count)
+
+ self.m.ccxt.order_precision.return_value = 8
+ self.m.ccxt.create_order.reset_mock()
+ self.m.report.log_error.reset_mock()
+ with self.subTest(insufficient_funds=True),\
+ mock.patch.object(portfolio.Order, "mark_finished_order") as mark_finished_order:
+ order = portfolio.Order("buy", portfolio.Amount("ETH", "0.001"),
+ D("0.1"), "BTC", "long", self.m, "trade")
+ self.m.ccxt.create_order.side_effect = [
+ portfolio.InsufficientFunds,
+ portfolio.InsufficientFunds,
+ portfolio.InsufficientFunds,
+ portfolio.InsufficientFunds,
+ portfolio.InsufficientFunds,
+ ]
+ order.run()
+ self.m.ccxt.create_order.assert_has_calls([
+ mock.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account='exchange', price=D('0.1')),
+ mock.call('ETH/BTC', 'limit', 'buy', D('0.00099'), account='exchange', price=D('0.1')),
+ mock.call('ETH/BTC', 'limit', 'buy', D('0.0009801'), account='exchange', price=D('0.1')),
+ mock.call('ETH/BTC', 'limit', 'buy', D('0.00097029'), account='exchange', price=D('0.1')),
+ mock.call('ETH/BTC', 'limit', 'buy', D('0.00096059'), account='exchange', price=D('0.1')),
+ ])
+ self.assertEqual(5, self.m.ccxt.create_order.call_count)
+ self.assertEqual(0, len(order.results))
+ self.assertEqual("error", order.status)
+ self.assertEqual(5, order.tries)
+ self.m.report.log_error.assert_called()
+ self.assertEqual(5, self.m.report.log_error.call_count)
+ self.m.report.log_error.assert_called_with(mock.ANY, message="Giving up Order(buy long 0.00096060 ETH at 0.1 BTC [pending])", exception=mock.ANY)
+
+
+@unittest.skipUnless("unit" in limits, "Unit skipped")
+class MouvementTest(WebMockTestCase):
+ def test_values(self):
+ mouvement = portfolio.Mouvement("ETH", "BTC", {
+ "tradeID": 42, "type": "buy", "fee": "0.0015",
+ "date": "2017-12-30 12:00:12", "rate": "0.1",
+ "amount": "10", "total": "1"
+ })
+ self.assertEqual("ETH", mouvement.currency)
+ self.assertEqual("BTC", mouvement.base_currency)
+ self.assertEqual(42, mouvement.id)
+ self.assertEqual("buy", mouvement.action)
+ self.assertEqual(D("0.0015"), mouvement.fee_rate)
+ self.assertEqual(portfolio.datetime(2017, 12, 30, 12, 0, 12), mouvement.date)
+ self.assertEqual(D("0.1"), mouvement.rate)
+ self.assertEqual(portfolio.Amount("ETH", "10"), mouvement.total)
+ self.assertEqual(portfolio.Amount("BTC", "1"), mouvement.total_in_base)
+
+ mouvement = portfolio.Mouvement("ETH", "BTC", { "foo": "bar" })
+ self.assertIsNone(mouvement.date)
+ self.assertIsNone(mouvement.id)
+ self.assertIsNone(mouvement.action)
+ self.assertEqual(-1, mouvement.fee_rate)
+ self.assertEqual(0, mouvement.rate)
+ self.assertEqual(portfolio.Amount("ETH", 0), mouvement.total)
+ self.assertEqual(portfolio.Amount("BTC", 0), mouvement.total_in_base)
+
+ def test__repr(self):
+ mouvement = portfolio.Mouvement("ETH", "BTC", {
+ "tradeID": 42, "type": "buy", "fee": "0.0015",
+ "date": "2017-12-30 12:00:12", "rate": "0.1",
+ "amount": "10", "total": "1"
+ })
+ self.assertEqual("Mouvement(2017-12-30 12:00:12 ; buy 10.00000000 ETH (1.00000000 BTC) fee: 0.1500%)", repr(mouvement))
+
+ mouvement = portfolio.Mouvement("ETH", "BTC", {
+ "tradeID": 42, "type": "buy",
+ "date": "garbage", "rate": "0.1",
+ "amount": "10", "total": "1"
+ })
+ self.assertEqual("Mouvement(No date ; buy 10.00000000 ETH (1.00000000 BTC))", repr(mouvement))
+
+ def test_as_json(self):
+ mouvement = portfolio.Mouvement("ETH", "BTC", {
+ "tradeID": 42, "type": "buy", "fee": "0.0015",
+ "date": "2017-12-30 12:00:12", "rate": "0.1",
+ "amount": "10", "total": "1"
+ })
+ as_json = mouvement.as_json()
+
+ self.assertEqual(D("0.0015"), as_json["fee_rate"])
+ self.assertEqual(portfolio.datetime(2017, 12, 30, 12, 0, 12), as_json["date"])
+ self.assertEqual("buy", as_json["action"])
+ self.assertEqual(D("10"), as_json["total"])
+ self.assertEqual(D("1"), as_json["total_in_base"])
+ self.assertEqual("BTC", as_json["base_currency"])
+ self.assertEqual("ETH", as_json["currency"])
+
+@unittest.skipUnless("unit" in limits, "Unit skipped")
+class ReportStoreTest(WebMockTestCase):
+ def test_add_log(self):
+ report_store = market.ReportStore(self.m)
+ report_store.add_log({"foo": "bar"})
+
+ self.assertEqual({"foo": "bar", "date": mock.ANY}, report_store.logs[0])
+
+ def test_set_verbose(self):
+ report_store = market.ReportStore(self.m)
+ with self.subTest(verbose=True):
+ report_store.set_verbose(True)
+ self.assertTrue(report_store.verbose_print)
+
+ with self.subTest(verbose=False):
+ report_store.set_verbose(False)
+ self.assertFalse(report_store.verbose_print)
+
+ def test_print_log(self):
+ report_store = market.ReportStore(self.m)
+ with self.subTest(verbose=True),\
+ mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock:
+ report_store.set_verbose(True)
+ report_store.print_log("Coucou")
+ report_store.print_log(portfolio.Amount("BTC", 1))
+ self.assertEqual(stdout_mock.getvalue(), "Coucou\n1.00000000 BTC\n")
+
+ with self.subTest(verbose=False),\
+ mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock:
+ report_store.set_verbose(False)
+ report_store.print_log("Coucou")
+ report_store.print_log(portfolio.Amount("BTC", 1))
+ self.assertEqual(stdout_mock.getvalue(), "")
+
+ def test_to_json(self):
+ report_store = market.ReportStore(self.m)
+ report_store.logs.append({"foo": "bar"})
+ self.assertEqual('[{"foo": "bar"}]', report_store.to_json())
+ report_store.logs.append({"date": portfolio.datetime(2018, 2, 24)})
+ self.assertEqual('[{"foo": "bar"}, {"date": "2018-02-24T00:00:00"}]', report_store.to_json())
+ report_store.logs.append({"amount": portfolio.Amount("BTC", 1)})
+ self.assertEqual('[{"foo": "bar"}, {"date": "2018-02-24T00:00:00"}, {"amount": "1.00000000 BTC"}]', report_store.to_json())
+
+ @mock.patch.object(market.ReportStore, "print_log")
+ @mock.patch.object(market.ReportStore, "add_log")
+ def test_log_stage(self, add_log, print_log):
+ report_store = market.ReportStore(self.m)
+ report_store.log_stage("foo")
+ print_log.assert_has_calls([
+ mock.call("-----------"),
+ mock.call("[Stage] foo"),
+ ])
+ add_log.assert_called_once_with({'type': 'stage', 'stage': 'foo'})
+
+ @mock.patch.object(market.ReportStore, "print_log")
+ @mock.patch.object(market.ReportStore, "add_log")
+ def test_log_balances(self, add_log, print_log):
+ report_store = market.ReportStore(self.m)
+ self.m.balances.as_json.return_value = "json"
+ self.m.balances.all = { "FOO": "bar", "BAR": "baz" }
+
+ report_store.log_balances(tag="tag")
+ print_log.assert_has_calls([
+ mock.call("[Balance]"),
+ mock.call("\tbar"),
+ mock.call("\tbaz"),
+ ])
+ add_log.assert_called_once_with({
+ 'type': 'balance',
+ 'balances': 'json',
+ 'tag': 'tag'
+ })
+
+ @mock.patch.object(market.ReportStore, "print_log")
+ @mock.patch.object(market.ReportStore, "add_log")
+ def test_log_tickers(self, add_log, print_log):
+ report_store = market.ReportStore(self.m)
+ amounts = {
+ "BTC": portfolio.Amount("BTC", 10),
+ "ETH": portfolio.Amount("BTC", D("0.3"))
+ }
+ amounts["ETH"].rate = D("0.1")
+
+ report_store.log_tickers(amounts, "BTC", "default", "total")
+ print_log.assert_not_called()
+ add_log.assert_called_once_with({
+ 'type': 'tickers',
+ 'compute_value': 'default',
+ 'balance_type': 'total',
+ 'currency': 'BTC',
+ 'balances': {
+ 'BTC': D('10'),
+ 'ETH': D('0.3')
+ },
+ 'rates': {
+ 'BTC': None,
+ 'ETH': D('0.1')
+ },
+ 'total': D('10.3')
+ })
+
+ @mock.patch.object(market.ReportStore, "print_log")
+ @mock.patch.object(market.ReportStore, "add_log")
+ def test_log_dispatch(self, add_log, print_log):
+ report_store = market.ReportStore(self.m)
+ amount = portfolio.Amount("BTC", "10.3")
+ amounts = {
+ "BTC": portfolio.Amount("BTC", 10),
+ "ETH": portfolio.Amount("BTC", D("0.3"))
+ }
+ report_store.log_dispatch(amount, amounts, "medium", "repartition")
+ print_log.assert_not_called()
+ add_log.assert_called_once_with({
+ 'type': 'dispatch',
+ 'liquidity': 'medium',
+ 'repartition_ratio': 'repartition',
+ 'total_amount': {
+ 'currency': 'BTC',
+ 'value': D('10.3')
+ },
+ 'repartition': {
+ 'BTC': D('10'),
+ 'ETH': D('0.3')
+ }
+ })
+
+ @mock.patch.object(market.ReportStore, "print_log")
+ @mock.patch.object(market.ReportStore, "add_log")
+ def test_log_trades(self, add_log, print_log):
+ report_store = market.ReportStore(self.m)
+ trade_mock1 = mock.Mock()
+ trade_mock2 = mock.Mock()
+ trade_mock1.as_json.return_value = { "trade": "1" }
+ trade_mock2.as_json.return_value = { "trade": "2" }
+
+ matching_and_trades = [
+ (True, trade_mock1),
+ (False, trade_mock2),
+ ]
+ report_store.log_trades(matching_and_trades, "only")
+
+ print_log.assert_not_called()
+ add_log.assert_called_with({
+ 'type': 'trades',
+ 'only': 'only',
+ 'debug': False,
+ 'trades': [
+ {'trade': '1', 'skipped': False},
+ {'trade': '2', 'skipped': True}
+ ]
+ })
+
+ @mock.patch.object(market.ReportStore, "print_log")
+ @mock.patch.object(market.ReportStore, "add_log")
+ def test_log_orders(self, add_log, print_log):
+ report_store = market.ReportStore(self.m)
+
+ order_mock1 = mock.Mock()
+ order_mock2 = mock.Mock()
+
+ order_mock1.as_json.return_value = "order1"
+ order_mock2.as_json.return_value = "order2"
+
+ orders = [order_mock1, order_mock2]
+
+ report_store.log_orders(orders, tick="tick",
+ only="only", compute_value="compute_value")
+
+ print_log.assert_called_once_with("[Orders]")
+ self.m.trades.print_all_with_order.assert_called_once_with(ind="\t")
+
+ add_log.assert_called_with({
+ 'type': 'orders',
+ 'only': 'only',
+ 'compute_value': 'compute_value',
+ 'tick': 'tick',
+ 'orders': ['order1', 'order2']
+ })
+
+ @mock.patch.object(market.ReportStore, "print_log")
+ @mock.patch.object(market.ReportStore, "add_log")
+ def test_log_order(self, add_log, print_log):
+ report_store = market.ReportStore(self.m)
+ order_mock = mock.Mock()
+ order_mock.as_json.return_value = "order"
+ new_order_mock = mock.Mock()
+ new_order_mock.as_json.return_value = "new_order"
+ order_mock.__repr__ = mock.Mock()
+ order_mock.__repr__.return_value = "Order Mock"
+ new_order_mock.__repr__ = mock.Mock()
+ new_order_mock.__repr__.return_value = "New order Mock"
+
+ with self.subTest(finished=True):
+ report_store.log_order(order_mock, 1, finished=True)
+ print_log.assert_called_once_with("[Order] Finished Order Mock")
+ add_log.assert_called_once_with({
+ 'type': 'order',
+ 'tick': 1,
+ 'update': None,
+ 'order': 'order',
+ 'compute_value': None,
+ 'new_order': None
+ })
+
+ add_log.reset_mock()
+ print_log.reset_mock()
+
+ with self.subTest(update="waiting"):
+ report_store.log_order(order_mock, 1, update="waiting")
+ print_log.assert_called_once_with("[Order] Order Mock, tick 1, waiting")
+ add_log.assert_called_once_with({
+ 'type': 'order',
+ 'tick': 1,
+ 'update': 'waiting',
+ 'order': 'order',
+ 'compute_value': None,
+ 'new_order': None
+ })
+
+ add_log.reset_mock()
+ print_log.reset_mock()
+ with self.subTest(update="adjusting"):
+ report_store.log_order(order_mock, 3,
+ update="adjusting", new_order=new_order_mock,
+ compute_value="default")
+ print_log.assert_called_once_with("[Order] Order Mock, tick 3, cancelling and adjusting to New order Mock")
+ add_log.assert_called_once_with({
+ 'type': 'order',
+ 'tick': 3,
+ 'update': 'adjusting',
+ 'order': 'order',
+ 'compute_value': "default",
+ 'new_order': 'new_order'
+ })
+
+ add_log.reset_mock()
+ print_log.reset_mock()
+ with self.subTest(update="market_fallback"):
+ report_store.log_order(order_mock, 7,
+ update="market_fallback", new_order=new_order_mock)
+ print_log.assert_called_once_with("[Order] Order Mock, tick 7, fallbacking to market value")
+ add_log.assert_called_once_with({
+ 'type': 'order',
+ 'tick': 7,
+ 'update': 'market_fallback',
+ 'order': 'order',
+ 'compute_value': None,
+ 'new_order': 'new_order'
+ })
+
+ add_log.reset_mock()
+ print_log.reset_mock()
+ with self.subTest(update="market_adjusting"):
+ report_store.log_order(order_mock, 17,
+ update="market_adjust", new_order=new_order_mock)
+ print_log.assert_called_once_with("[Order] Order Mock, tick 17, market value, cancelling and adjusting to New order Mock")
+ add_log.assert_called_once_with({
+ 'type': 'order',
+ 'tick': 17,
+ 'update': 'market_adjust',
+ 'order': 'order',
+ 'compute_value': None,
+ 'new_order': 'new_order'
+ })
+
+ @mock.patch.object(market.ReportStore, "print_log")
+ @mock.patch.object(market.ReportStore, "add_log")
+ def test_log_move_balances(self, add_log, print_log):
+ report_store = market.ReportStore(self.m)
+ needed = {
+ "BTC": portfolio.Amount("BTC", 10),
+ "USDT": 1
+ }
+ moving = {
+ "BTC": portfolio.Amount("BTC", 3),
+ "USDT": -2
+ }
+ report_store.log_move_balances(needed, moving)
+ print_log.assert_not_called()
+ add_log.assert_called_once_with({
+ 'type': 'move_balances',
+ 'debug': False,
+ 'needed': {
+ 'BTC': D('10'),
+ 'USDT': 1
+ },
+ 'moving': {
+ 'BTC': D('3'),
+ 'USDT': -2
+ }
+ })
+
+ @mock.patch.object(market.ReportStore, "print_log")
+ @mock.patch.object(market.ReportStore, "add_log")
+ def test_log_http_request(self, add_log, print_log):
+ report_store = market.ReportStore(self.m)
+ response = mock.Mock()
+ response.status_code = 200
+ response.text = "Hey"
+
+ report_store.log_http_request("method", "url", "body",
+ "headers", response)
+ print_log.assert_not_called()
+ add_log.assert_called_once_with({
+ 'type': 'http_request',
+ 'method': 'method',
+ 'url': 'url',
+ 'body': 'body',
+ 'headers': 'headers',
+ 'status': 200,
+ 'response': 'Hey'
+ })
+
+ @mock.patch.object(market.ReportStore, "print_log")
+ @mock.patch.object(market.ReportStore, "add_log")
+ def test_log_error(self, add_log, print_log):
+ report_store = market.ReportStore(self.m)
+ with self.subTest(message=None, exception=None):
+ report_store.log_error("action")
+ print_log.assert_called_once_with("[Error] action")
+ add_log.assert_called_once_with({
+ 'type': 'error',
+ 'action': 'action',
+ 'exception_class': None,
+ 'exception_message': None,
+ 'message': None
+ })
+
+ print_log.reset_mock()
+ add_log.reset_mock()
+ with self.subTest(message="Hey", exception=None):
+ report_store.log_error("action", message="Hey")
+ print_log.assert_has_calls([
+ mock.call("[Error] action"),
+ mock.call("\tHey")
+ ])
+ add_log.assert_called_once_with({
+ 'type': 'error',
+ 'action': 'action',
+ 'exception_class': None,
+ 'exception_message': None,
+ 'message': "Hey"
+ })
+
+ print_log.reset_mock()
+ add_log.reset_mock()
+ with self.subTest(message=None, exception=Exception("bouh")):
+ report_store.log_error("action", exception=Exception("bouh"))
+ print_log.assert_has_calls([
+ mock.call("[Error] action"),
+ mock.call("\tException: bouh")
+ ])
+ add_log.assert_called_once_with({
+ 'type': 'error',
+ 'action': 'action',
+ 'exception_class': "Exception",
+ 'exception_message': "bouh",
+ 'message': None
+ })
+
+ print_log.reset_mock()
+ add_log.reset_mock()
+ with self.subTest(message="Hey", exception=Exception("bouh")):
+ report_store.log_error("action", message="Hey", exception=Exception("bouh"))
+ print_log.assert_has_calls([
+ mock.call("[Error] action"),
+ mock.call("\tException: bouh"),
+ mock.call("\tHey")
+ ])
+ add_log.assert_called_once_with({
+ 'type': 'error',
+ 'action': 'action',
+ 'exception_class': "Exception",
+ 'exception_message': "bouh",
+ 'message': "Hey"
+ })
+
+ @mock.patch.object(market.ReportStore, "print_log")
+ @mock.patch.object(market.ReportStore, "add_log")
+ def test_log_debug_action(self, add_log, print_log):
+ report_store = market.ReportStore(self.m)
+ report_store.log_debug_action("Hey")
+
+ print_log.assert_called_once_with("[Debug] Hey")
+ add_log.assert_called_once_with({
+ 'type': 'debug_action',
+ 'action': 'Hey'
+ })
+
+@unittest.skipUnless("unit" in limits, "Unit skipped")
+class HelperTest(WebMockTestCase):
+ def test_main_store_report(self):
+ file_open = mock.mock_open()
+ with self.subTest(file=None), mock.patch("__main__.open", file_open):
+ helper.main_store_report(None, 1, self.m)
+ file_open.assert_not_called()
+
+ file_open = mock.mock_open()
+ with self.subTest(file="present"), mock.patch("helper.open", file_open),\
+ mock.patch.object(helper, "datetime") as time_mock:
+ time_mock.now.return_value = datetime.datetime(2018, 2, 25)
+ self.m.report.to_json.return_value = "json_content"
+
+ helper.main_store_report("present", 1, self.m)
+
+ file_open.assert_any_call("present/2018-02-25T00:00:00_1.json", "w")
+ file_open().write.assert_called_once_with("json_content")
+ self.m.report.to_json.assert_called_once_with()
+
+ with self.subTest(file="error"),\
+ mock.patch("helper.open") as file_open,\
+ mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock:
+ file_open.side_effect = FileNotFoundError
+
+ helper.main_store_report("error", 1, self.m)
+
+ self.assertRegex(stdout_mock.getvalue(), "impossible to store report file: FileNotFoundError;")
+
+ @mock.patch("helper.process_sell_all__1_all_sell")
+ @mock.patch("helper.process_sell_all__2_all_buy")
+ @mock.patch("portfolio.Portfolio.wait_for_recent")
+ def test_main_process_market(self, wait, buy, sell):
+ with self.subTest(before=False, after=False):
+ helper.main_process_market("user", None)
+
+ wait.assert_not_called()
+ buy.assert_not_called()
+ sell.assert_not_called()
+
+ buy.reset_mock()
+ wait.reset_mock()
+ sell.reset_mock()
+ with self.subTest(before=True, after=False):
+ helper.main_process_market("user", None, before=True)
+
+ wait.assert_not_called()
+ buy.assert_not_called()
+ sell.assert_called_once_with("user")
+
+ buy.reset_mock()
+ wait.reset_mock()
+ sell.reset_mock()
+ with self.subTest(before=False, after=True):
+ helper.main_process_market("user", None, after=True)
+
+ wait.assert_called_once_with("user")
+ buy.assert_called_once_with("user")
+ sell.assert_not_called()
+
+ buy.reset_mock()
+ wait.reset_mock()
+ sell.reset_mock()
+ with self.subTest(before=True, after=True):
+ helper.main_process_market("user", None, before=True, after=True)
+
+ wait.assert_called_once_with("user")
+ buy.assert_called_once_with("user")
+ sell.assert_called_once_with("user")
+
+ buy.reset_mock()
+ wait.reset_mock()
+ sell.reset_mock()
+ with self.subTest(action="print_balances"),\
+ mock.patch("helper.print_balances") as print_balances:
+ helper.main_process_market("user", "print_balances")
+
+ buy.assert_not_called()
+ wait.assert_not_called()
+ sell.assert_not_called()
+ print_balances.assert_called_once_with("user")
+
+ with self.subTest(action="print_orders"),\
+ mock.patch("helper.print_orders") as print_orders:
+ helper.main_process_market("user", "print_orders")
+
+ buy.assert_not_called()
+ wait.assert_not_called()
+ sell.assert_not_called()
+ print_orders.assert_called_once_with("user")
+
+ with self.subTest(action="unknown"),\
+ self.assertRaises(NotImplementedError):
+ helper.main_process_market("user", "unknown")
+
+ @mock.patch.object(helper, "psycopg2")
+ def test_fetch_markets(self, psycopg2):
+ connect_mock = mock.Mock()
+ cursor_mock = mock.MagicMock()
+ cursor_mock.__iter__.return_value = ["row_1", "row_2"]
+
+ connect_mock.cursor.return_value = cursor_mock
+ psycopg2.connect.return_value = connect_mock
+
+ with self.subTest(user=None):
+ rows = list(helper.main_fetch_markets({"foo": "bar"}, None))
+
+ psycopg2.connect.assert_called_once_with(foo="bar")
+ cursor_mock.execute.assert_called_once_with("SELECT config,user_id FROM market_configs")
+
+ self.assertEqual(["row_1", "row_2"], rows)
+
+ psycopg2.connect.reset_mock()
+ cursor_mock.execute.reset_mock()
+ with self.subTest(user=1):
+ rows = list(helper.main_fetch_markets({"foo": "bar"}, 1))
+
+ psycopg2.connect.assert_called_once_with(foo="bar")
+ cursor_mock.execute.assert_called_once_with("SELECT config,user_id FROM market_configs WHERE user_id = %s", 1)
+
+ self.assertEqual(["row_1", "row_2"], rows)
+
+ @mock.patch.object(helper.sys, "exit")
+ def test_main_parse_args(self, exit):
+ with self.subTest(config="config.ini"):
+ args = helper.main_parse_args([])
+ self.assertEqual("config.ini", args.config)
+ self.assertFalse(args.before)
+ self.assertFalse(args.after)
+ self.assertFalse(args.debug)
+
+ args = helper.main_parse_args(["--before", "--after", "--debug"])
+ self.assertTrue(args.before)
+ self.assertTrue(args.after)
+ self.assertTrue(args.debug)
+
+ exit.assert_not_called()
+
+ with self.subTest(config="inexistant"),\
+ mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock:
+ args = helper.main_parse_args(["--config", "foo.bar"])
+ exit.assert_called_once_with(1)
+ self.assertEqual("no config file found, exiting\n", stdout_mock.getvalue())
+
+ @mock.patch.object(helper.sys, "exit")
+ @mock.patch("helper.configparser")
+ @mock.patch("helper.os")
+ def test_main_parse_config(self, os, configparser, exit):
+ with self.subTest(pg_config=True, report_path=None):
+ config_mock = mock.MagicMock()
+ configparser.ConfigParser.return_value = config_mock
+ def config(element):
+ return element == "postgresql"
+
+ config_mock.__contains__.side_effect = config
+ config_mock.__getitem__.return_value = "pg_config"
+
+ result = helper.main_parse_config("configfile")
+
+ config_mock.read.assert_called_with("configfile")
+
+ self.assertEqual(["pg_config", None], result)
+
+ with self.subTest(pg_config=True, report_path="present"):
+ config_mock = mock.MagicMock()
+ configparser.ConfigParser.return_value = config_mock
+
+ config_mock.__contains__.return_value = True
+ config_mock.__getitem__.side_effect = [
+ {"report_path": "report_path"},
+ {"report_path": "report_path"},
+ "pg_config",
+ ]
+
+ os.path.exists.return_value = False
+ result = helper.main_parse_config("configfile")
+
+ config_mock.read.assert_called_with("configfile")
+ self.assertEqual(["pg_config", "report_path"], result)
+ os.path.exists.assert_called_once_with("report_path")
+ os.makedirs.assert_called_once_with("report_path")
+
+ with self.subTest(pg_config=False),\
+ mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock:
+ config_mock = mock.MagicMock()
+ configparser.ConfigParser.return_value = config_mock
+ result = helper.main_parse_config("configfile")
+
+ config_mock.read.assert_called_with("configfile")
+ exit.assert_called_once_with(1)
+ self.assertEqual("no configuration for postgresql in config file\n", stdout_mock.getvalue())
+
+
+ def test_print_orders(self):
+ helper.print_orders(self.m)
+
+ self.m.report.log_stage.assert_called_with("print_orders")
+ self.m.balances.fetch_balances.assert_called_with(tag="print_orders")
+ self.m.prepare_trades.assert_called_with(base_currency="BTC",
+ compute_value="average")
+ self.m.trades.prepare_orders.assert_called_with(compute_value="average")
+
+ def test_print_balances(self):
+ self.m.balances.in_currency.return_value = {
+ "BTC": portfolio.Amount("BTC", "0.65"),
+ "ETH": portfolio.Amount("BTC", "0.3"),
+ }
+
+ helper.print_balances(self.m)
+
+ self.m.report.log_stage.assert_called_once_with("print_balances")
+ self.m.balances.fetch_balances.assert_called_with()
+ self.m.report.print_log.assert_has_calls([
+ mock.call("total:"),
+ mock.call(portfolio.Amount("BTC", "0.95")),
+ ])
+
+ def test_process_sell_needed__1_sell(self):
+ helper.process_sell_needed__1_sell(self.m)
+
+ self.m.balances.fetch_balances.assert_has_calls([
+ mock.call(tag="process_sell_needed__1_sell_begin"),
+ mock.call(tag="process_sell_needed__1_sell_end"),
+ ])
+ self.m.prepare_trades.assert_called_with(base_currency="BTC",
+ liquidity="medium")
+ self.m.trades.prepare_orders.assert_called_with(compute_value="average",
+ only="dispose")
+ self.m.trades.run_orders.assert_called()
+ self.m.follow_orders.assert_called()
+ self.m.report.log_stage.assert_has_calls([
+ mock.call("process_sell_needed__1_sell_begin"),
+ mock.call("process_sell_needed__1_sell_end")
+ ])
+
+ def test_process_sell_needed__2_buy(self):
+ helper.process_sell_needed__2_buy(self.m)
+
+ self.m.balances.fetch_balances.assert_has_calls([
+ mock.call(tag="process_sell_needed__2_buy_begin"),
+ mock.call(tag="process_sell_needed__2_buy_end"),
+ ])
+ self.m.update_trades.assert_called_with(base_currency="BTC",
+ liquidity="medium", only="acquire")
+ self.m.trades.prepare_orders.assert_called_with(compute_value="average",
+ only="acquire")
+ self.m.move_balances.assert_called_with()
+ self.m.trades.run_orders.assert_called()
+ self.m.follow_orders.assert_called()
+ self.m.report.log_stage.assert_has_calls([
+ mock.call("process_sell_needed__2_buy_begin"),
+ mock.call("process_sell_needed__2_buy_end")
+ ])
+
+ def test_process_sell_all__1_sell(self):
+ helper.process_sell_all__1_all_sell(self.m)
+
+ self.m.balances.fetch_balances.assert_has_calls([
+ mock.call(tag="process_sell_all__1_all_sell_begin"),
+ mock.call(tag="process_sell_all__1_all_sell_end"),
+ ])
+ self.m.prepare_trades_to_sell_all.assert_called_with(base_currency="BTC")
+ self.m.trades.prepare_orders.assert_called_with(compute_value="average")
+ self.m.trades.run_orders.assert_called()
+ self.m.follow_orders.assert_called()
+ self.m.report.log_stage.assert_has_calls([
+ mock.call("process_sell_all__1_all_sell_begin"),
+ mock.call("process_sell_all__1_all_sell_end")
+ ])
+
+ def test_process_sell_all__2_all_buy(self):
+ helper.process_sell_all__2_all_buy(self.m)
+
+ self.m.balances.fetch_balances.assert_has_calls([
+ mock.call(tag="process_sell_all__2_all_buy_begin"),
+ mock.call(tag="process_sell_all__2_all_buy_end"),
+ ])
+ self.m.prepare_trades.assert_called_with(base_currency="BTC",
+ liquidity="medium")
+ self.m.trades.prepare_orders.assert_called_with(compute_value="average")
+ self.m.move_balances.assert_called_with()
+ self.m.trades.run_orders.assert_called()
+ self.m.follow_orders.assert_called()
+ self.m.report.log_stage.assert_has_calls([
+ mock.call("process_sell_all__2_all_buy_begin"),
+ mock.call("process_sell_all__2_all_buy_end")
+ ])
+
@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"),
self.assertEqual(portfolio.Amount("XVG", 1000), balances["XVG"].total)
- trades = TradeStore.all
+ 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.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("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[5].action)
# Action 2
- portfolio.Trade.prepare_orders(only="dispose", compute_value=lambda x, y: x["bid"] * D("1.001"))
+ portfolio.TradeStore.prepare_orders(only="dispose", compute_value=lambda x, y: x["bid"] * D("1.001"))
- all_orders = portfolio.Trade.all_orders()
+ 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("open", all_orders[0].status)
self.assertEqual("open", all_orders[1].status)
- market.fetch_order.return_value = { "status": "closed" }
+ 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(portfolio.time, "sleep") as sleep:
# Action 4
helper.follow_orders(verbose=False)
fetch_balance = {
"ETH": {
- "free": D("1.0") / 3,
- "used": D("0.0"),
+ "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": {
- "free": D("0.134"),
- "used": D("0.0"),
+ "exchange_free": D("0.134"),
+ "exchange_used": D("0.0"),
+ "exchange_total": D("0.134"),
+ "margin_total": 0,
"total": D("0.134"),
},
"ETC": {
- "free": D("4.0"),
- "used": D("0.0"),
+ "exchange_free": D("4.0"),
+ "exchange_used": D("0.0"),
+ "exchange_total": D("4.0"),
+ "margin_total": 0,
"total": D("4.0"),
},
"XVG": {
- "free": D("0.0"),
- "used": D("0.0"),
+ "exchange_free": D("0.0"),
+ "exchange_used": D("0.0"),
+ "exchange_total": D("0.0"),
+ "margin_total": 0,
"total": D("0.0"),
},
}
- market.fetch_balance.return_value = fetch_balance
+ market.fetch_all_balances.return_value = fetch_balance
with mock.patch.object(portfolio.Portfolio, "repartition", return_value=repartition):
# Action 5
- helper.update_trades(market, only="buy", compute_value="average")
+ helper.update_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("XVG", 0), balances["XVG"].total)
- trades = TradeStore.all
- 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)
+ 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["ETC"].value_from)
- self.assertEqual(portfolio.Amount("BTC", D("0.0485")), trades["ETC"].value_to)
- self.assertEqual("buy", trades["ETC"].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.00")), trades["BTD"].value_from)
- 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.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.00776")), 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("acquire", trades[3].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)
+ 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 6
- portfolio.Trade.prepare_orders(only="buy", compute_value=lambda x, y: x["ask"])
+ portfolio.TradeStore.prepare_orders(only="acquire", compute_value=lambda x, y: x["ask"])
- all_orders = portfolio.Trade.all_orders(state="pending")
+ 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)