Convert select_order to use ft_is_open flag

This commit is contained in:
Matthias 2020-09-11 07:12:10 +02:00
parent 41942e3af1
commit aa8832f70e
4 changed files with 53 additions and 8 deletions

View File

@ -260,7 +260,7 @@ class FreqtradeBot:
if not trade.is_open and not trade.fee_updated('sell'):
# Get sell fee
order = trade.select_order('sell', 'closed')
order = trade.select_order('sell', False)
if order:
logger.info(f"Updating sell-fee on trade {trade} for order {order.order_id}.")
self.update_trade_state(trade, order.order_id,
@ -269,7 +269,7 @@ class FreqtradeBot:
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')
order = trade.select_order('buy', False)
if order:
logger.info(f"Updating buy-fee on trade {trade} for order {order.order_id}.")
self.update_trade_state(trade, order.order_id)
@ -291,7 +291,7 @@ class FreqtradeBot:
Handles trades where the initial fee-update did not work.
"""
logger.info(f"Trying to reupdate buy fees for {trade}")
order = trade.select_order('buy', 'closed')
order = trade.select_order('buy', False)
if order:
logger.info(f"Updating buy-fee on trade {trade} for order {order.order_id}.")
self.update_trade_state(trade, order.order_id)

View File

@ -520,16 +520,16 @@ class Trade(_DECL_BASE):
profit_ratio = (close_trade_price / self.open_trade_price) - 1
return float(f"{profit_ratio:.8f}")
def select_order(self, order_side: str, status: Optional[str]) -> Optional[Order]:
def select_order(self, order_side: str, is_open: Optional[bool]) -> Optional[Order]:
"""
Finds latest order for this orderside and status
:param order_side: Side of the order (either 'buy' or 'sell')
:param status: Optionally filter on open / closed orders
:param is_open: Only search for open orders?
:return: latest Order object if it exists, else None
"""
orders = [o for o in self.orders if o.side == order_side]
if status:
orders = [o for o in orders if o.status == status]
if is_open is not None:
orders = [o for o in orders if o.ft_is_open == is_open]
if len(orders) > 0:
return orders[-1]
else:

View File

@ -4295,7 +4295,7 @@ def test_update_closed_trades_without_assigned_fees(mocker, default_conf, fee):
for trade in trades:
if trade.is_open:
# Exclude Trade 4 - as the order is still open.
if trade.select_order('buy', 'closed'):
if trade.select_order('buy', False):
assert trade.fee_open_cost is not None
assert trade.fee_open_currency is not None
else:

View File

@ -1078,3 +1078,48 @@ def test_update_order_from_ccxt():
ccxt_order.update({'id': 'somethingelse'})
with pytest.raises(DependencyException, match=r"Order-id's don't match"):
o.update_from_ccxt_object(ccxt_order)
@pytest.mark.usefixtures("init_persistence")
def test_select_order(fee):
create_mock_trades(fee)
trades = Trade.get_trades().all()
# Open buy order, no sell order
order = trades[0].select_order('buy', True)
assert order is None
order = trades[0].select_order('buy', False)
assert order is not None
order = trades[0].select_order('sell', None)
assert order is None
# closed buy order, and open sell order
order = trades[1].select_order('buy', True)
assert order is None
order = trades[1].select_order('buy', False)
assert order is not None
order = trades[1].select_order('buy', None)
assert order is not None
order = trades[1].select_order('sell', True)
assert order is None
order = trades[1].select_order('sell', False)
assert order is not None
# Has open buy order
order = trades[3].select_order('buy', True)
assert order is not None
order = trades[3].select_order('buy', False)
assert order is None
# Open sell order
order = trades[4].select_order('buy', True)
assert order is None
order = trades[4].select_order('buy', False)
assert order is not None
order = trades[4].select_order('sell', True)
assert order is not None
assert order.ft_order_side == 'stoploss'
order = trades[4].select_order('sell', False)
assert order is None