aboutsummaryrefslogtreecommitdiff
path: root/portfolio.py
diff options
context:
space:
mode:
authorIsmaël Bouya <ismael.bouya@normalesup.org>2018-04-03 20:43:40 +0200
committerIsmaël Bouya <ismael.bouya@normalesup.org>2018-04-03 20:43:40 +0200
commit341a4b07e8c205711fff0e93dd3679708828a961 (patch)
tree9c63fc677c00ec89fd4c2ca0b9434606f4685b56 /portfolio.py
parent7a676e2b0db5a168f08b419701cb71c28d7c76ee (diff)
parent7ba831c52bc08032a37b576e3fa1098fed0b7635 (diff)
downloadTrader-341a4b07e8c205711fff0e93dd3679708828a961.tar.gz
Trader-341a4b07e8c205711fff0e93dd3679708828a961.tar.zst
Trader-341a4b07e8c205711fff0e93dd3679708828a961.zip
Merge branch 'retry_vanished' into dev
Diffstat (limited to 'portfolio.py')
-rw-r--r--portfolio.py50
1 files changed, 22 insertions, 28 deletions
diff --git a/portfolio.py b/portfolio.py
index 9e98c43..535aaa8 100644
--- a/portfolio.py
+++ b/portfolio.py
@@ -269,20 +269,24 @@ class Trade:
269 filled_amount += order.filled_amount(in_base_currency=in_base_currency) 269 filled_amount += order.filled_amount(in_base_currency=in_base_currency)
270 return filled_amount 270 return filled_amount
271 271
272 def update_order(self, order, tick): 272 tick_actions = {
273 actions = { 273 0: ["waiting", None],
274 0: ["waiting", None], 274 1: ["waiting", None],
275 1: ["waiting", None], 275 2: ["adjusting", lambda x, y: (x[y] + x["average"]) / 2],
276 2: ["adjusting", lambda x, y: (x[y] + x["average"]) / 2], 276 3: ["waiting", None],
277 3: ["waiting", None], 277 4: ["waiting", None],
278 4: ["waiting", None], 278 5: ["adjusting", lambda x, y: (x[y]*2 + x["average"]) / 3],
279 5: ["adjusting", lambda x, y: (x[y]*2 + x["average"]) / 3], 279 6: ["waiting", None],
280 6: ["waiting", None], 280 7: ["market_fallback", "default"],
281 7: ["market_fallback", "default"], 281 }
282 }
283 282
284 if tick in actions: 283 def tick_actions_recreate(self, tick, default="average"):
285 update, compute_value = actions[tick] 284 return ([default] + \
285 [ y[1] for x, y in self.tick_actions.items() if x <= tick and y[1] is not None ])[-1]
286
287 def update_order(self, order, tick):
288 if tick in self.tick_actions:
289 update, compute_value = self.tick_actions[tick]
286 elif tick % 3 == 1: 290 elif tick % 3 == 1:
287 update = "market_adjust" 291 update = "market_adjust"
288 compute_value = "default" 292 compute_value = "default"
@@ -381,14 +385,6 @@ class Trade:
381 self.orders.append(order) 385 self.orders.append(order)
382 return order 386 return order
383 387
384 def reopen_same_order(self, order):
385 new_order = Order(order.action, order.amount, order.rate,
386 order.base_currency, order.trade_type, self.market,
387 self, close_if_possible=order.close_if_possible)
388 self.orders.append(new_order)
389 new_order.run()
390 return new_order
391
392 def as_json(self): 388 def as_json(self):
393 return { 389 return {
394 "action": self.action, 390 "action": self.action,
@@ -550,14 +546,11 @@ class Order:
550 self.fetch() 546 self.fetch()
551 return self.status 547 return self.status
552 548
553 def fix_disappeared_order(self): 549 def mark_disappeared_order(self):
554 if self.status.startswith("closed") and \ 550 if self.status.startswith("closed") and \
555 len(self.mouvements) == 1 and \ 551 len(self.mouvements) > 0 and \
556 self.mouvements[0].total_in_base == 0: 552 self.mouvements[-1].total_in_base == 0:
557 self.status = "error_disappeared" 553 self.status = "error_disappeared"
558 new_order = self.trade.reopen_same_order(self)
559 self.market.report.log_error("fetch",
560 message="Order {} disappeared, recreating it as {}".format(self, new_order))
561 554
562 def mark_finished_order(self): 555 def mark_finished_order(self):
563 if self.status.startswith("closed") and self.market.debug: 556 if self.status.startswith("closed") and self.market.debug:
@@ -582,7 +575,7 @@ class Order:
582 575
583 self.fetch_mouvements() 576 self.fetch_mouvements()
584 577
585 self.fix_disappeared_order() 578 self.mark_disappeared_order()
586 579
587 self.mark_finished_order() 580 self.mark_finished_order()
588 # FIXME: consider open order with dust remaining as closed 581 # FIXME: consider open order with dust remaining as closed
@@ -614,6 +607,7 @@ class Order:
614 for mouvement_hash in mouvements: 607 for mouvement_hash in mouvements:
615 self.mouvements.append(Mouvement(self.amount.currency, 608 self.mouvements.append(Mouvement(self.amount.currency,
616 self.base_currency, mouvement_hash)) 609 self.base_currency, mouvement_hash))
610 self.mouvements.sort(key= lambda x: x.date)
617 611
618 def cancel(self): 612 def cancel(self):
619 if self.market.debug: 613 if self.market.debug: