Update open trades too

This commit is contained in:
Matthias 2020-08-22 15:48:42 +02:00
parent fd33282eb1
commit 11e69bdd65
2 changed files with 25 additions and 8 deletions

View File

@ -267,10 +267,20 @@ class FreqtradeBot:
if order: if order:
logger.info(f"Updating sell-fee on trade {trade} for order {order.order_id}.") logger.info(f"Updating sell-fee on trade {trade} for order {order.order_id}.")
self.update_trade_state(trade, order.order_id, self.update_trade_state(trade, order.order_id,
order.ft_order_side == 'stoploss') stoploss_order=order.ft_order_side == 'stoploss')
trades: List[Trade] = Trade.get_open_trades_without_assigned_fees()
for trade in trades:
if trade.is_open and not trade.fee_updated('buy'):
order = trade.select_order('buy', 'closed')
if order:
logger.info(f"Updating buy-fee on trade {trade} for order {order.order_id}.")
self.update_trade_state(trade, order.order_id)
def handle_insufficient_funds(self, trade: Trade): def handle_insufficient_funds(self, trade: Trade):
""" """
Determine if we ever opened a sell order for this trade.
If not, try update buy fees - otherwise "refind" the open order we obviously lost.
""" """
sell_order = trade.select_order('sell', None) sell_order = trade.select_order('sell', None)
if sell_order: if sell_order:
@ -278,11 +288,10 @@ class FreqtradeBot:
else: else:
self.reupdate_buy_order_fees(trade) self.reupdate_buy_order_fees(trade)
# See if we ever opened a sell order for this
# If not, try update buy fees
def reupdate_buy_order_fees(self, trade: Trade): def reupdate_buy_order_fees(self, trade: Trade):
""" """
Get buy order from database, and try to reupdate.
Handles trades where the initial fee-update did not work.
""" """
logger.info(f"Trying to reupdate buy fees for {trade}") logger.info(f"Trying to reupdate buy fees for {trade}")
order = trade.select_order('buy', 'closed') order = trade.select_order('buy', 'closed')
@ -295,8 +304,6 @@ class FreqtradeBot:
Try refinding a lost trade. Try refinding a lost trade.
Only used when InsufficientFunds appears on sell orders (stoploss or sell). Only used when InsufficientFunds appears on sell orders (stoploss or sell).
Tries to walk the stored orders and sell them off eventually. Tries to walk the stored orders and sell them off eventually.
TODO: maybe remove this method again.
""" """
logger.info(f"Trying to refind lost order for {trade}") logger.info(f"Trying to refind lost order for {trade}")
for order in trade.orders: for order in trade.orders:
@ -318,7 +325,7 @@ class FreqtradeBot:
continue continue
if fo: if fo:
self.update_trade_state(trade, order.order_id, fo, self.update_trade_state(trade, order.order_id, fo,
order.ft_order_side == 'stoploss') stoploss_order=order.ft_order_side == 'stoploss')
except ExchangeError: except ExchangeError:
logger.warning(f"Error updating {order.order_id}") logger.warning(f"Error updating {order.order_id}")

View File

@ -8,7 +8,7 @@ from typing import Any, Dict, List, Optional
import arrow import arrow
from sqlalchemy import (Boolean, Column, DateTime, Float, ForeignKey, Integer, from sqlalchemy import (Boolean, Column, DateTime, Float, ForeignKey, Integer,
String, create_engine, desc, func, inspect, or_) String, create_engine, desc, func, inspect)
from sqlalchemy.exc import NoSuchModuleError from sqlalchemy.exc import NoSuchModuleError
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Query, relationship from sqlalchemy.orm import Query, relationship
@ -554,6 +554,16 @@ class Trade(_DECL_BASE):
""" """
return Trade.get_trades(Trade.open_order_id.isnot(None)).all() return Trade.get_trades(Trade.open_order_id.isnot(None)).all()
@staticmethod
def get_open_trades_without_assigned_fees():
"""
Returns all open trades which don't have open fees set correctly
"""
return Trade.get_trades([Trade.fee_open_currency.is_(None),
Trade.orders.any(),
Trade.is_open.is_(True),
]).all()
@staticmethod @staticmethod
def get_sold_trades_without_assigned_fees(): def get_sold_trades_without_assigned_fees():
""" """