aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIsmaël Bouya <ismael.bouya@normalesup.org>2018-03-12 01:55:17 +0100
committerIsmaël Bouya <ismael.bouya@normalesup.org>2018-03-12 02:03:17 +0100
commit186f7d816a6bfc4fbf64027c3d7ecea10b97db38 (patch)
tree0c04c4ace99227a054c6e9015b6f4fdc07c4b4e2
parentf9226903cb53a9b303a26de562e321159349f8df (diff)
downloadTrader-186f7d816a6bfc4fbf64027c3d7ecea10b97db38.tar.gz
Trader-186f7d816a6bfc4fbf64027c3d7ecea10b97db38.tar.zst
Trader-186f7d816a6bfc4fbf64027c3d7ecea10b97db38.zip
Fix fullfiled not having correct currencies
-rw-r--r--portfolio.py17
-rw-r--r--test.py75
2 files changed, 65 insertions, 27 deletions
diff --git a/portfolio.py b/portfolio.py
index 0f2c011..65fdc6c 100644
--- a/portfolio.py
+++ b/portfolio.py
@@ -291,6 +291,7 @@ class Trade:
291 self.orders = [] 291 self.orders = []
292 self.market = market 292 self.market = market
293 self.closed = False 293 self.closed = False
294 self.inverted = None
294 assert self.value_from.value * self.value_to.value >= 0 295 assert self.value_from.value * self.value_to.value >= 0
295 assert self.value_from.currency == self.value_to.currency 296 assert self.value_from.currency == self.value_to.currency
296 if self.value_from != 0: 297 if self.value_from != 0:
@@ -315,8 +316,8 @@ class Trade:
315 else: 316 else:
316 return "dispose" 317 return "dispose"
317 318
318 def order_action(self, inverted): 319 def order_action(self):
319 if (self.value_from < self.value_to) != inverted: 320 if (self.value_from < self.value_to) != self.inverted:
320 return "buy" 321 return "buy"
321 else: 322 else:
322 return "sell" 323 return "sell"
@@ -339,7 +340,7 @@ class Trade:
339 340
340 @property 341 @property
341 def is_fullfiled(self): 342 def is_fullfiled(self):
342 return abs(self.filled_amount(in_base_currency=True)) >= abs(self.delta) 343 return abs(self.filled_amount(in_base_currency=(not self.inverted))) >= abs(self.delta)
343 344
344 def filled_amount(self, in_base_currency=False): 345 def filled_amount(self, in_base_currency=False):
345 filled_amount = 0 346 filled_amount = 0
@@ -385,17 +386,17 @@ class Trade:
385 if self.action is None: 386 if self.action is None:
386 return None 387 return None
387 ticker = self.market.get_ticker(self.currency, self.base_currency) 388 ticker = self.market.get_ticker(self.currency, self.base_currency)
388 inverted = ticker["inverted"] 389 self.inverted = ticker["inverted"]
389 if inverted: 390 if self.inverted:
390 ticker = ticker["original"] 391 ticker = ticker["original"]
391 rate = Computation.compute_value(ticker, self.order_action(inverted), compute_value=compute_value) 392 rate = Computation.compute_value(ticker, self.order_action(), compute_value=compute_value)
392 393
393 # FIXME: Dust amount should be removed from there if they werent 394 # FIXME: Dust amount should be removed from there if they werent
394 # honored in other sales 395 # honored in other sales
395 delta_in_base = abs(self.delta) 396 delta_in_base = abs(self.delta)
396 # 9 BTC's worth of move (10 - 1 or 1 - 10 depending on case) 397 # 9 BTC's worth of move (10 - 1 or 1 - 10 depending on case)
397 398
398 if not inverted: 399 if not self.inverted:
399 base_currency = self.base_currency 400 base_currency = self.base_currency
400 # BTC 401 # BTC
401 if self.action == "dispose": 402 if self.action == "dispose":
@@ -453,7 +454,7 @@ class Trade:
453 self.market.report.log_error("prepare_order", message="Less to do than already filled: {}".format(delta)) 454 self.market.report.log_error("prepare_order", message="Less to do than already filled: {}".format(delta))
454 return None 455 return None
455 456
456 order = Order(self.order_action(inverted), 457 order = Order(self.order_action(),
457 delta, rate, base_currency, self.trade_type, 458 delta, rate, base_currency, self.trade_type,
458 self.market, self, close_if_possible=close_if_possible) 459 self.market, self, close_if_possible=close_if_possible)
459 self.orders.append(order) 460 self.orders.append(order)
diff --git a/test.py b/test.py
index a45010b..33a817d 100644
--- a/test.py
+++ b/test.py
@@ -1375,16 +1375,20 @@ class TradeTest(WebMockTestCase):
1375 value_to = portfolio.Amount("BTC", "1.0") 1375 value_to = portfolio.Amount("BTC", "1.0")
1376 trade = portfolio.Trade(value_from, value_to, "ETH", self.m) 1376 trade = portfolio.Trade(value_from, value_to, "ETH", self.m)
1377 1377
1378 self.assertEqual("buy", trade.order_action(False)) 1378 trade.inverted = False
1379 self.assertEqual("sell", trade.order_action(True)) 1379 self.assertEqual("buy", trade.order_action())
1380 trade.inverted = True
1381 self.assertEqual("sell", trade.order_action())
1380 1382
1381 value_from = portfolio.Amount("BTC", "0") 1383 value_from = portfolio.Amount("BTC", "0")
1382 value_from.linked_to = portfolio.Amount("ETH", "0") 1384 value_from.linked_to = portfolio.Amount("ETH", "0")
1383 value_to = portfolio.Amount("BTC", "-1.0") 1385 value_to = portfolio.Amount("BTC", "-1.0")
1384 trade = portfolio.Trade(value_from, value_to, "ETH", self.m) 1386 trade = portfolio.Trade(value_from, value_to, "ETH", self.m)
1385 1387
1386 self.assertEqual("sell", trade.order_action(False)) 1388 trade.inverted = False
1387 self.assertEqual("buy", trade.order_action(True)) 1389 self.assertEqual("sell", trade.order_action())
1390 trade.inverted = True
1391 self.assertEqual("buy", trade.order_action())
1388 1392
1389 def test_trade_type(self): 1393 def test_trade_type(self):
1390 value_from = portfolio.Amount("BTC", "0.5") 1394 value_from = portfolio.Amount("BTC", "0.5")
@@ -1402,26 +1406,59 @@ class TradeTest(WebMockTestCase):
1402 self.assertEqual("short", trade.trade_type) 1406 self.assertEqual("short", trade.trade_type)
1403 1407
1404 def test_is_fullfiled(self): 1408 def test_is_fullfiled(self):
1405 value_from = portfolio.Amount("BTC", "0.5") 1409 with self.subTest(inverted=False):
1406 value_from.linked_to = portfolio.Amount("ETH", "10.0") 1410 value_from = portfolio.Amount("BTC", "0.5")
1407 value_to = portfolio.Amount("BTC", "1.0") 1411 value_from.linked_to = portfolio.Amount("ETH", "10.0")
1408 trade = portfolio.Trade(value_from, value_to, "ETH", self.m) 1412 value_to = portfolio.Amount("BTC", "1.0")
1413 trade = portfolio.Trade(value_from, value_to, "ETH", self.m)
1409 1414
1410 order1 = mock.Mock() 1415 order1 = mock.Mock()
1411 order1.filled_amount.return_value = portfolio.Amount("BTC", "0.3") 1416 order1.filled_amount.return_value = portfolio.Amount("BTC", "0.3")
1412 1417
1413 order2 = mock.Mock() 1418 order2 = mock.Mock()
1414 order2.filled_amount.return_value = portfolio.Amount("BTC", "0.01") 1419 order2.filled_amount.return_value = portfolio.Amount("BTC", "0.01")
1415 trade.orders.append(order1) 1420 trade.orders.append(order1)
1416 trade.orders.append(order2) 1421 trade.orders.append(order2)
1422
1423 self.assertFalse(trade.is_fullfiled)
1424
1425 order3 = mock.Mock()
1426 order3.filled_amount.return_value = portfolio.Amount("BTC", "0.19")
1427 trade.orders.append(order3)
1428
1429 self.assertTrue(trade.is_fullfiled)
1430
1431 order1.filled_amount.assert_called_with(in_base_currency=True)
1432 order2.filled_amount.assert_called_with(in_base_currency=True)
1433 order3.filled_amount.assert_called_with(in_base_currency=True)
1434
1435 with self.subTest(inverted=True):
1436 value_from = portfolio.Amount("BTC", "0.5")
1437 value_from.linked_to = portfolio.Amount("USDT", "1000.0")
1438 value_to = portfolio.Amount("BTC", "1.0")
1439 trade = portfolio.Trade(value_from, value_to, "USDT", self.m)
1440 trade.inverted = True
1441
1442 order1 = mock.Mock()
1443 order1.filled_amount.return_value = portfolio.Amount("BTC", "0.3")
1444
1445 order2 = mock.Mock()
1446 order2.filled_amount.return_value = portfolio.Amount("BTC", "0.01")
1447 trade.orders.append(order1)
1448 trade.orders.append(order2)
1449
1450 self.assertFalse(trade.is_fullfiled)
1451
1452 order3 = mock.Mock()
1453 order3.filled_amount.return_value = portfolio.Amount("BTC", "0.19")
1454 trade.orders.append(order3)
1417 1455
1418 self.assertFalse(trade.is_fullfiled) 1456 self.assertTrue(trade.is_fullfiled)
1419 1457
1420 order3 = mock.Mock() 1458 order1.filled_amount.assert_called_with(in_base_currency=False)
1421 order3.filled_amount.return_value = portfolio.Amount("BTC", "0.19") 1459 order2.filled_amount.assert_called_with(in_base_currency=False)
1422 trade.orders.append(order3) 1460 order3.filled_amount.assert_called_with(in_base_currency=False)
1423 1461
1424 self.assertTrue(trade.is_fullfiled)
1425 1462
1426 def test_filled_amount(self): 1463 def test_filled_amount(self):
1427 value_from = portfolio.Amount("BTC", "0.5") 1464 value_from = portfolio.Amount("BTC", "0.5")