From 4434a54d598b1a1ec3ea1fa2122d90834dfa91cf Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 13 Aug 2020 16:14:28 +0200 Subject: [PATCH] Add unique key to order-Model --- freqtrade/freqtradebot.py | 6 +++--- freqtrade/persistence/models.py | 9 +++++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 18d39e471..038bf1f4f 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -527,7 +527,7 @@ class FreqtradeBot: order = self.exchange.buy(pair=pair, ordertype=order_type, amount=amount, rate=buy_limit_requested, time_in_force=time_in_force) - order_obj = Order.parse_from_ccxt_object(order, 'buy') + order_obj = Order.parse_from_ccxt_object(order, pair, 'buy') order_id = order['id'] order_status = order.get('status', None) @@ -784,7 +784,7 @@ class FreqtradeBot: stop_price=stop_price, order_types=self.strategy.order_types) - order_obj = Order.parse_from_ccxt_object(stoploss_order, 'stoploss') + order_obj = Order.parse_from_ccxt_object(stoploss_order, trade.pair, 'stoploss') trade.orders.append(order_obj) trade.stoploss_order_id = str(stoploss_order['id']) return True @@ -1137,7 +1137,7 @@ class FreqtradeBot: time_in_force=time_in_force ) - order_obj = Order.parse_from_ccxt_object(order, 'sell') + order_obj = Order.parse_from_ccxt_object(order, trade.pair, 'sell') trade.orders.append(order_obj) trade.open_order_id = order['id'] diff --git a/freqtrade/persistence/models.py b/freqtrade/persistence/models.py index d930fbeb7..14e2b0da8 100644 --- a/freqtrade/persistence/models.py +++ b/freqtrade/persistence/models.py @@ -15,6 +15,7 @@ from sqlalchemy.orm import Query, relationship from sqlalchemy.orm.scoping import scoped_session from sqlalchemy.orm.session import sessionmaker from sqlalchemy.pool import StaticPool +from sqlalchemy.sql.schema import UniqueConstraint from freqtrade.exceptions import OperationalException from freqtrade.misc import safe_value_fallback @@ -101,11 +102,15 @@ class Order(_DECL_BASE): Mirrors CCXT Order structure """ __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'),) id = Column(Integer, primary_key=True) trade_id = Column(Integer, ForeignKey('trades.id'), index=True) ft_order_side = Column(String, nullable=False) + ft_pair = Column(String, nullable=False) order_id = Column(String, nullable=False, index=True) status = Column(String, nullable=True) @@ -159,11 +164,11 @@ class Order(_DECL_BASE): logger.warning(f"Did not find order for {order['id']}.") @staticmethod - def parse_from_ccxt_object(order: Dict[str, Any], side: str) -> 'Order': + def parse_from_ccxt_object(order: Dict[str, Any], pair: str, side: str) -> 'Order': """ Parse an order from a ccxt object and return a new order Object. """ - o = Order(order_id=str(order['id']), ft_order_side=side) + o = Order(order_id=str(order['id']), ft_order_side=side, ft_pair=pair) o.update_from_ccxt_object(order) return o