Order should have a "is_open" flag

This commit is contained in:
Matthias 2020-08-13 17:17:52 +02:00
parent da2a515d0b
commit c4d7aff5c3
2 changed files with 13 additions and 8 deletions

View File

@ -108,14 +108,15 @@ def migrate_trades_table(decl_base, inspector, engine, table_back_name: str, col
def migrate_open_orders_to_trades(engine):
engine.execute("""
insert into orders (trade_id, ft_pair, order_id, ft_order_side)
select id trade_id, pair ft_pair, open_order_id,
insert into orders (ft_trade_id, ft_pair, order_id, ft_order_side, ft_is_open)
select id ft_trade_id, pair ft_pair, open_order_id,
case when close_rate_requested is null then 'buy'
else 'sell' end ft_order_side
else 'sell' end ft_order_side, true ft_is_open
from trades
where open_order_id is not null
union all
select id trade_id, pair ft_pair, stoploss_order_id order_id, 'stoploss' ft_order_side
select id ft_trade_id, pair ft_pair, stoploss_order_id order_id,
'stoploss' ft_order_side, true ft_is_open
from trades
where stoploss_order_id is not null
""")

View File

@ -104,13 +104,14 @@ class Order(_DECL_BASE):
__tablename__ = 'orders'
# Uniqueness should be ensured over pair, order_id
# its likely that order_id is unique per Pair on some exchanges.
__table_args__ = (UniqueConstraint('ft_pair', 'order_id'),)
__table_args__ = (UniqueConstraint('ft_pair', 'order_id', name="_order_pair_order_id"),)
id = Column(Integer, primary_key=True)
trade_id = Column(Integer, ForeignKey('trades.id'), index=True)
ft_trade_id = Column(Integer, ForeignKey('trades.id'), index=True)
ft_order_side = Column(String, nullable=False)
ft_pair = Column(String, nullable=False)
ft_is_open = Column(Boolean, nullable=False, default=True, index=True)
order_id = Column(String, nullable=False, index=True)
status = Column(String, nullable=True)
@ -128,7 +129,7 @@ class Order(_DECL_BASE):
def __repr__(self):
return (f'Order(id={self.id}, order_id={self.order_id}, trade_id={self.trade_id}, '
return (f'Order(id={self.id}, order_id={self.order_id}, trade_id={self.ft_trade_id}, '
f'side={self.side}, status={self.status})')
def update_from_ccxt_object(self, order):
@ -151,6 +152,10 @@ class Order(_DECL_BASE):
if 'timestamp' in order and order['timestamp'] is not None:
self.order_date = datetime.fromtimestamp(order['timestamp'] / 1000)
if self.status in ('closed', 'canceled', 'cancelled'):
self.ft_is_open = False
self.order_update_date = datetime.now()
@staticmethod
def update_orders(orders: List['Order'], order: Dict[str, Any]):
"""
@ -159,7 +164,6 @@ class Order(_DECL_BASE):
if filtered_orders:
oobj = filtered_orders[0]
oobj.update_from_ccxt_object(order)
oobj.order_update_date = datetime.now()
else:
logger.warning(f"Did not find order for {order['id']}.")