modify trade life cycle (should fix #112)

This commit is contained in:
gcarq
2017-11-17 20:17:29 +01:00
parent 59d04d1d0c
commit 63c95a3546
3 changed files with 14 additions and 34 deletions

View File

@@ -85,20 +85,27 @@ class Trade(_DECL_BASE):
if not order['closed']:
return
logger.debug('Updating trade (id=%d) ...', self.id)
logger.info('Updating trade (id=%d) ...', self.id)
if order['type'] == 'LIMIT_BUY':
# Update open rate and actual amount
self.open_rate = order['rate']
self.amount = order['amount']
logger.info('LIMIT_BUY has been fulfilled for %s.', self)
elif order['type'] == 'LIMIT_SELL':
# Set close rate and set actual profit
self.close_rate = order['rate']
self.close_profit = self.calc_profit()
self.close_date = datetime.utcnow()
self.is_open = False
logger.info(
'Marking %s as closed as the trade is fulfilled and found no open orders for it.',
self
)
else:
raise ValueError('Unknown order type: {}'.format(order['type']))
self.open_order_id = None
Trade.session.flush()
def calc_profit(self, rate: Optional[float] = None) -> float:
"""