Add unique key to order-Model

This commit is contained in:
Matthias 2020-08-13 16:14:28 +02:00
parent ebd755e36a
commit 4434a54d59
2 changed files with 10 additions and 5 deletions

View File

@ -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']

View File

@ -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