Update orders

This commit is contained in:
Matthias 2020-08-13 14:13:58 +02:00
parent 4924d8487e
commit 396e781bf4
3 changed files with 23 additions and 10 deletions

View File

@ -8,7 +8,6 @@ import logging
from copy import deepcopy from copy import deepcopy
from datetime import datetime, timezone from datetime import datetime, timezone
from math import ceil from math import ceil
from random import randint
from typing import Any, Dict, List, Optional, Tuple from typing import Any, Dict, List, Optional, Tuple
import arrow import arrow
@ -474,11 +473,11 @@ class Exchange:
def dry_run_order(self, pair: str, ordertype: str, side: str, amount: float, def dry_run_order(self, pair: str, ordertype: str, side: str, amount: float,
rate: float, params: Dict = {}) -> Dict[str, Any]: rate: float, params: Dict = {}) -> Dict[str, Any]:
order_id = f'dry_run_{side}_{randint(0, 10**6)}' order_id = f'dry_run_{side}_{datetime.now().timestamp()}'
_amount = self.amount_to_precision(pair, amount) _amount = self.amount_to_precision(pair, amount)
dry_order = { dry_order = {
"id": order_id, 'id': order_id,
'pair': pair, 'symbol': pair,
'price': rate, 'price': rate,
'average': rate, 'average': rate,
'amount': _amount, 'amount': _amount,

View File

@ -1260,6 +1260,7 @@ class FreqtradeBot:
except InvalidOrderException as exception: except InvalidOrderException as exception:
logger.warning('Unable to fetch order %s: %s', order_id, exception) logger.warning('Unable to fetch order %s: %s', order_id, exception)
return False return False
Order.update_order(order)
# Try update amount (binance-fix) # Try update amount (binance-fix)
try: try:
new_amount = self.get_real_amount(trade, order, order_amount) new_amount = self.get_real_amount(trade, order, order_amount)

View File

@ -58,6 +58,10 @@ def init(db_url: str, clean_open_orders: bool = False) -> None:
# We should use the scoped_session object - not a seperately initialized version # We should use the scoped_session object - not a seperately initialized version
Trade.session = scoped_session(sessionmaker(bind=engine, autoflush=True, autocommit=True)) Trade.session = scoped_session(sessionmaker(bind=engine, autoflush=True, autocommit=True))
Trade.query = Trade.session.query_property() Trade.query = Trade.session.query_property()
# Copy session attributes to order object too
Order.session = Trade.session
Order.query = Order.session.query_property()
_DECL_BASE.metadata.create_all(engine) _DECL_BASE.metadata.create_all(engine)
check_migrate(engine, decl_base=_DECL_BASE) check_migrate(engine, decl_base=_DECL_BASE)
@ -103,7 +107,7 @@ class Order(_DECL_BASE):
ft_order_side = Column(String, nullable=False) ft_order_side = Column(String, nullable=False)
order_id = Column(String, nullable=False, index=True) order_id = Column(String, nullable=False, unique=True, index=True)
status = Column(String, nullable=True) status = Column(String, nullable=True)
symbol = Column(String, nullable=True) symbol = Column(String, nullable=True)
order_type = Column(String, nullable=True) order_type = Column(String, nullable=True)
@ -115,6 +119,12 @@ class Order(_DECL_BASE):
cost = Column(Float, nullable=True) cost = Column(Float, nullable=True)
order_date = Column(DateTime, nullable=False, default=datetime.utcnow) order_date = Column(DateTime, nullable=False, default=datetime.utcnow)
order_filled_date = Column(DateTime, nullable=True) order_filled_date = Column(DateTime, nullable=True)
order_update_date = Column(DateTime, nullable=True)
def __repr__(self):
return (f'Order(id={self.id}, order_id={self.order_id}, trade_id={self.trade_id}, '
f'side={self.side}, status={self.status})')
def update_from_ccxt_object(self, order): def update_from_ccxt_object(self, order):
""" """
@ -136,6 +146,14 @@ class Order(_DECL_BASE):
if 'timestamp' in order and order['timestamp'] is not None: if 'timestamp' in order and order['timestamp'] is not None:
self.order_date = datetime.fromtimestamp(order['timestamp']) self.order_date = datetime.fromtimestamp(order['timestamp'])
@staticmethod
def update_order(order: Dict[str, Any]):
"""
"""
oobj = Order.query.filter(Order.order_id == order['id']).first()
oobj.update_from_ccxt_object(order)
oobj.order_update_date = datetime.now()
@staticmethod @staticmethod
def parse_from_ccxt_object(order: Dict[str, Any], side: str) -> 'Order': def parse_from_ccxt_object(order: Dict[str, Any], side: str) -> 'Order':
""" """
@ -146,11 +164,6 @@ class Order(_DECL_BASE):
o.update_from_ccxt_object(order) o.update_from_ccxt_object(order)
return o return o
def __repr__(self):
return (f'Order(id={self.id}, trade_id={self.trade_id}, side={self.side}, '
f'status={self.status})')
class Trade(_DECL_BASE): class Trade(_DECL_BASE):
""" """