aboutsummaryrefslogtreecommitdiff
path: root/portfolio.py
diff options
context:
space:
mode:
authorIsmaël Bouya <ismael.bouya@normalesup.org>2018-03-04 23:35:16 +0100
committerIsmaël Bouya <ismael.bouya@normalesup.org>2018-03-04 23:35:16 +0100
commitf9226903cb53a9b303a26de562e321159349f8df (patch)
tree671e139c9cadaf4070287f50d1f71c0d26d6ba80 /portfolio.py
parentc5a7f2863f5c041ff906b2407a74971e2178a729 (diff)
downloadTrader-f9226903cb53a9b303a26de562e321159349f8df.tar.gz
Trader-f9226903cb53a9b303a26de562e321159349f8df.tar.zst
Trader-f9226903cb53a9b303a26de562e321159349f8df.zip
Fixes after night runv0.5
- Currency pair doesn’t work when fetching orders - Store error in main to report. - Cancel orders when closing trade - Print closed status of trade to repr - Don’t try to cancel not cancellable orders
Diffstat (limited to 'portfolio.py')
-rw-r--r--portfolio.py26
1 files changed, 20 insertions, 6 deletions
diff --git a/portfolio.py b/portfolio.py
index f27e84f..0f2c011 100644
--- a/portfolio.py
+++ b/portfolio.py
@@ -3,7 +3,7 @@ from datetime import datetime, timedelta
3from decimal import Decimal as D, ROUND_DOWN 3from decimal import Decimal as D, ROUND_DOWN
4from json import JSONDecodeError 4from json import JSONDecodeError
5from simplejson.errors import JSONDecodeError as SimpleJSONDecodeError 5from simplejson.errors import JSONDecodeError as SimpleJSONDecodeError
6from ccxt import ExchangeError, InsufficientFunds, ExchangeNotAvailable, InvalidOrder, OrderNotCached 6from ccxt import ExchangeError, InsufficientFunds, ExchangeNotAvailable, InvalidOrder, OrderNotCached, OrderNotFound
7from retry import retry 7from retry import retry
8import requests 8import requests
9 9
@@ -333,6 +333,8 @@ class Trade:
333 return not (self.is_fullfiled or self.closed) 333 return not (self.is_fullfiled or self.closed)
334 334
335 def close(self): 335 def close(self):
336 for order in self.orders:
337 order.cancel()
336 self.closed = True 338 self.closed = True
337 339
338 @property 340 @property
@@ -467,11 +469,19 @@ class Trade:
467 } 469 }
468 470
469 def __repr__(self): 471 def __repr__(self):
470 return "Trade({} -> {} in {}, {})".format( 472 if self.closed and not self.is_fullfiled:
473 closed = " ❌"
474 elif self.is_fullfiled:
475 closed = " ✔"
476 else:
477 closed = ""
478
479 return "Trade({} -> {} in {}, {}{})".format(
471 self.value_from, 480 self.value_from,
472 self.value_to, 481 self.value_to,
473 self.currency, 482 self.currency,
474 self.action) 483 self.action,
484 closed)
475 485
476 def print_with_order(self, ind=""): 486 def print_with_order(self, ind=""):
477 self.market.report.print_log("{}{}".format(ind, self)) 487 self.market.report.print_log("{}{}".format(ind, self))
@@ -600,7 +610,7 @@ class Order:
600 self.market.report.log_debug_action("Fetching {}".format(self)) 610 self.market.report.log_debug_action("Fetching {}".format(self))
601 return 611 return
602 try: 612 try:
603 result = self.market.ccxt.fetch_order(self.id, symbol=self.amount.currency) 613 result = self.market.ccxt.fetch_order(self.id)
604 self.results.append(result) 614 self.results.append(result)
605 self.status = result["status"] 615 self.status = result["status"]
606 # Time at which the order started 616 # Time at which the order started
@@ -645,8 +655,12 @@ class Order:
645 self.market.report.log_debug_action("Mark {} as cancelled".format(self)) 655 self.market.report.log_debug_action("Mark {} as cancelled".format(self))
646 self.status = "canceled" 656 self.status = "canceled"
647 return 657 return
648 self.market.ccxt.cancel_order(self.id) 658 if self.open and self.id is not None:
649 self.fetch() 659 try:
660 self.market.ccxt.cancel_order(self.id)
661 except OrderNotFound as e: # Closed inbetween
662 self.market.report.log_error("cancel_order", message="Already cancelled order", exception=e)
663 self.fetch()
650 664
651class Mouvement: 665class Mouvement:
652 def __init__(self, currency, base_currency, hash_): 666 def __init__(self, currency, base_currency, hash_):