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_stage.assert_called_once_with("prepare_trades",
+ base_currency='BTC', compute_value='average',
+ liquidity='medium', only=None, repartition=None)
m.report.log_balances.assert_called_once_with(tag="tag")
- @mock.patch.object(portfolio.Portfolio, "repartition")
- @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):
- if c1 == "USDT" and c2 == "BTC":
- return { "average": D("0.0001") }
- if c1 == "XVG" and c2 == "BTC":
- return { "average": D("0.000001") }
- if c1 == "XEM" and c2 == "BTC":
- return { "average": D("0.001") }
- self.fail("Should be called with {}, {}".format(c1, c2))
- get_ticker.side_effect = _get_ticker
-
- 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")
-
- 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(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):
- if c1 == "USDT" and c2 == "BTC":
- return { "average": D("0.0001") }
- if c1 == "XVG" and c2 == "BTC":
- return { "average": D("0.000001") }
- self.fail("Should be called with {}, {}".format(c1, c2))
- get_ticker.side_effect = _get_ticker
-
- 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")
-
- 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(market.TradeStore, "all_orders")
trade_mock2.prepare_order.return_value = 2
trade_mock3.prepare_order.return_value = 3
- trade_mock1.is_fullfiled = False
- trade_mock2.is_fullfiled = False
- trade_mock3.is_fullfiled = True
+ trade_mock1.pending = True
+ trade_mock2.pending = True
+ trade_mock3.pending = False
trade_store.all.append(trade_mock1)
trade_store.all.append(trade_mock2)
order_mock2.get_status.assert_called()
order_mock3.get_status.assert_called()
+ def test_close_trades(self):
+ trade_mock1 = mock.Mock()
+ trade_mock2 = mock.Mock()
+ trade_mock3 = mock.Mock()
+
+ trade_store = market.TradeStore(self.m)
+
+ trade_store.all.append(trade_mock1)
+ trade_store.all.append(trade_mock2)
+ trade_store.all.append(trade_mock3)
+
+ trade_store.close_trades()
+
+ trade_mock1.close.assert_called_once_with()
+ trade_mock2.close.assert_called_once_with()
+ trade_mock3.close.assert_called_once_with()
+
def test_pending(self):
trade_mock1 = mock.Mock()
- trade_mock1.is_fullfiled = False
+ trade_mock1.pending = True
trade_mock2 = mock.Mock()
- trade_mock2.is_fullfiled = False
+ trade_mock2.pending = True
trade_mock3 = mock.Mock()
- trade_mock3.is_fullfiled = True
+ trade_mock3.pending = False
trade_store = market.TradeStore(self.m)
self.assertEqual("\t\tMouvement 1", str(calls[3][1][0]))
self.assertEqual("\t\tMouvement 2", str(calls[4][1][0]))
+ def test_close(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)
+
+ trade.close()
+
+ self.assertEqual(True, trade.closed)
+
+ def test_pending(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)
+
+ trade.closed = True
+ self.assertEqual(False, trade.pending)
+
+ trade.closed = False
+ self.assertEqual(True, trade.pending)
+
+ order1 = mock.Mock()
+ order1.filled_amount.return_value = portfolio.Amount("BTC", "0.5")
+ trade.orders.append(order1)
+ self.assertEqual(False, trade.pending)
+
def test__repr(self):
value_from = portfolio.Amount("BTC", "0.5")
value_from.linked_to = portfolio.Amount("ETH", "10.0")
@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")
+ c = lambda x: x
+ report_store.log_stage("foo", bar="baz", c=c, d=portfolio.Amount("BTC", 1))
print_log.assert_has_calls([
mock.call("-----------"),
- mock.call("[Stage] foo"),
+ mock.call("[Stage] foo bar=baz, c=c = lambda x: x, d={'currency': 'BTC', 'value': Decimal('1')}"),
])
- add_log.assert_called_once_with({'type': 'stage', 'stage': 'foo'})
+ add_log.assert_called_once_with({
+ 'type': 'stage',
+ 'stage': 'foo',
+ 'args': {
+ 'bar': 'baz',
+ 'c': 'c = lambda x: x',
+ 'd': {
+ 'currency': 'BTC',
+ 'value': D('1')
+ }
+ }
+ })
@mock.patch.object(market.ReportStore, "print_log")
@mock.patch.object(market.ReportStore, "add_log")
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):
+ @mock.patch("helper.Processor.process")
+ def test_main_process_market(self, process):
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()
+ m = mock.Mock()
+ helper.main_process_market(m, None)
+
+ process.assert_not_called()
- buy.reset_mock()
- wait.reset_mock()
- sell.reset_mock()
+ process.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")
+ helper.main_process_market(m, None, before=True)
+
+ process.assert_called_once_with("sell_all", steps="before")
- buy.reset_mock()
- wait.reset_mock()
- sell.reset_mock()
+ process.reset_mock()
with self.subTest(before=False, after=True):
- helper.main_process_market("user", None, after=True)
+ helper.main_process_market(m, None, after=True)
- wait.assert_called_once_with("user")
- buy.assert_called_once_with("user")
- sell.assert_not_called()
+ process.assert_called_once_with("sell_all", steps="after")
- buy.reset_mock()
- wait.reset_mock()
- sell.reset_mock()
+ process.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")
+ helper.main_process_market(m, None, before=True, after=True)
- buy.reset_mock()
- wait.reset_mock()
- sell.reset_mock()
+ process.assert_has_calls([
+ mock.call("sell_all", steps="before"),
+ mock.call("sell_all", steps="after"),
+ ])
+
+ process.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()
+ process.assert_not_called()
print_balances.assert_called_once_with("user")
with self.subTest(action="print_orders"),\
mock.patch("helper.print_balances") as print_balances:
helper.main_process_market("user", ["print_orders", "print_balances"])
- buy.assert_not_called()
- wait.assert_not_called()
- sell.assert_not_called()
+ process.assert_not_called()
print_orders.assert_called_once_with("user")
print_balances.assert_called_once_with("user")
mock.call(portfolio.Amount("BTC", "0.95")),
])
- def test_process_sell_needed__1_sell(self):
- helper.process_sell_needed__1_sell(self.m)
+@unittest.skipUnless("unit" in limits, "Unit skipped")
+class ProcessorTest(WebMockTestCase):
+ def test_values(self):
+ processor = helper.Processor(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")
- ])
+ self.assertEqual(self.m, processor.market)
- def test_process_sell_needed__2_buy(self):
- helper.process_sell_needed__2_buy(self.m)
+ def test_run_action(self):
+ processor = helper.Processor(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")
- ])
+ with mock.patch.object(processor, "parse_args") as parse_args:
+ method_mock = mock.Mock()
+ parse_args.return_value = [method_mock, { "foo": "bar" }]
- def test_process_sell_all__1_sell(self):
- helper.process_sell_all__1_all_sell(self.m)
+ processor.run_action("foo", "bar", "baz")
- 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")
- ])
+ parse_args.assert_called_with("foo", "bar", "baz")
- def test_process_sell_all__2_all_buy(self):
- helper.process_sell_all__2_all_buy(self.m)
+ method_mock.assert_called_with(foo="bar")
+
+ processor.run_action("wait_for_recent", "bar", "baz")
+
+ method_mock.assert_called_with(self.m, foo="bar")
+
+ def test_select_step(self):
+ processor = helper.Processor(self.m)
+
+ scenario = processor.scenarios["sell_all"]
+
+ self.assertEqual(scenario, processor.select_steps(scenario, "all"))
+ self.assertEqual(["all_sell"], list(map(lambda x: x["name"], processor.select_steps(scenario, "before"))))
+ self.assertEqual(["wait", "all_buy"], list(map(lambda x: x["name"], processor.select_steps(scenario, "after"))))
+ self.assertEqual(["wait"], list(map(lambda x: x["name"], processor.select_steps(scenario, 2))))
+ self.assertEqual(["wait"], list(map(lambda x: x["name"], processor.select_steps(scenario, "wait"))))
+
+ with self.assertRaises(TypeError):
+ processor.select_steps(scenario, ["wait"])
+
+ @mock.patch("helper.Processor.process_step")
+ def test_process(self, process_step):
+ processor = helper.Processor(self.m)
+
+ processor.process("sell_all", foo="bar")
+ self.assertEqual(3, process_step.call_count)
+
+ steps = list(map(lambda x: x[1][1]["name"], process_step.mock_calls))
+ scenario_names = list(map(lambda x: x[1][0], process_step.mock_calls))
+ kwargs = list(map(lambda x: x[1][2], process_step.mock_calls))
+ self.assertEqual(["all_sell", "wait", "all_buy"], steps)
+ self.assertEqual(["sell_all", "sell_all", "sell_all"], scenario_names)
+ self.assertEqual([{"foo":"bar"}, {"foo":"bar"}, {"foo":"bar"}], kwargs)
+
+ process_step.reset_mock()
+
+ processor.process("sell_needed", steps=["before", "after"])
+ self.assertEqual(3, process_step.call_count)
+
+ def test_method_arguments(self):
+ ccxt = mock.Mock(spec=market.ccxt.poloniexE)
+ m = market.Market(ccxt)
+
+ processor = helper.Processor(m)
+
+ method, arguments = processor.method_arguments("wait_for_recent")
+ self.assertEqual(portfolio.Portfolio.wait_for_recent, method)
+ self.assertEqual(["delta"], arguments)
+
+ method, arguments = processor.method_arguments("prepare_trades")
+ self.assertEqual(m.prepare_trades, method)
+ self.assertEqual(['base_currency', 'liquidity', 'compute_value', 'repartition', 'only'], arguments)
+
+ method, arguments = processor.method_arguments("prepare_orders")
+ self.assertEqual(m.trades.prepare_orders, method)
+
+ method, arguments = processor.method_arguments("move_balances")
+ self.assertEqual(m.move_balances, method)
+
+ method, arguments = processor.method_arguments("run_orders")
+ self.assertEqual(m.trades.run_orders, method)
+
+ method, arguments = processor.method_arguments("follow_orders")
+ self.assertEqual(m.follow_orders, method)
+
+ method, arguments = processor.method_arguments("close_trades")
+ self.assertEqual(m.trades.close_trades, method)
+
+ def test_process_step(self):
+ processor = helper.Processor(self.m)
+
+ with mock.patch.object(processor, "run_action") as run_action:
+ step = processor.scenarios["sell_needed"][1]
+
+ processor.process_step("foo", step, {"foo":"bar"})
+
+ self.m.report.log_stage.assert_has_calls([
+ mock.call("process_foo__1_sell_begin"),
+ mock.call("process_foo__1_sell_end"),
+ ])
+ self.m.balances.fetch_balances.assert_has_calls([
+ mock.call(tag="process_foo__1_sell_begin"),
+ mock.call(tag="process_foo__1_sell_end"),
+ ])
+
+ self.assertEqual(5, run_action.call_count)
+
+ run_action.assert_has_calls([
+ mock.call('prepare_trades', {}, {'foo': 'bar'}),
+ mock.call('prepare_orders', {'only': 'dispose', 'compute_value': 'average'}, {'foo': 'bar'}),
+ mock.call('run_orders', {}, {'foo': 'bar'}),
+ mock.call('follow_orders', {}, {'foo': 'bar'}),
+ mock.call('close_trades', {}, {'foo': 'bar'}),
+ ])
+
+ self.m.reset_mock()
+ with mock.patch.object(processor, "run_action") as run_action:
+ step = processor.scenarios["sell_needed"][0]
+
+ processor.process_step("foo", step, {"foo":"bar"})
+ self.m.balances.fetch_balances.assert_not_called()
+
+ def test_parse_args(self):
+ processor = helper.Processor(self.m)
+
+ with mock.patch.object(processor, "method_arguments") as method_arguments:
+ method_mock = mock.Mock()
+ method_arguments.return_value = [
+ method_mock,
+ ["foo2", "foo"]
+ ]
+ method, args = processor.parse_args("action", {"foo": "bar", "foo2": "bar"}, {"foo": "bar2", "bla": "bla"})
+
+ self.assertEqual(method_mock, method)
+ self.assertEqual({"foo": "bar2", "foo2": "bar"}, args)
+
+ with mock.patch.object(processor, "method_arguments") as method_arguments:
+ method_mock = mock.Mock()
+ method_arguments.return_value = [
+ method_mock,
+ ["repartition"]
+ ]
+ method, args = processor.parse_args("action", {"repartition": { "base_currency": 1 }}, {})
+
+ self.assertEqual(1, len(args["repartition"]))
+ self.assertIn("BTC", args["repartition"])
+
+ with mock.patch.object(processor, "method_arguments") as method_arguments:
+ method_mock = mock.Mock()
+ method_arguments.return_value = [
+ method_mock,
+ ["repartition", "base_currency"]
+ ]
+ method, args = processor.parse_args("action", {"repartition": { "base_currency": 1 }}, {"base_currency": "USDT"})
+
+ self.assertEqual(1, len(args["repartition"]))
+ self.assertIn("USDT", args["repartition"])
+
+ with mock.patch.object(processor, "method_arguments") as method_arguments:
+ method_mock = mock.Mock()
+ method_arguments.return_value = [
+ method_mock,
+ ["repartition", "base_currency"]
+ ]
+ method, args = processor.parse_args("action", {"repartition": { "ETH": 1 }}, {"base_currency": "USDT"})
+
+ self.assertEqual(1, len(args["repartition"]))
+ self.assertIn("ETH", args["repartition"])
- 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):
with mock.patch.object(portfolio.Portfolio, "repartition", return_value=repartition):
# Action 5
- helper.update_trades(market, only="acquire", compute_value="average")
+ helper.prepare_trades(market, only="acquire", compute_value="average")
balances = portfolio.BalanceStore.all
self.assertEqual(portfolio.Amount("ETH", 1 / D("3")), balances["ETH"].total)