Unify functions and make it easy to get a list of filled buy orders

This commit is contained in:
Reigo Reinmets 2022-01-14 20:02:35 +02:00
parent 08cae6f067
commit 320c9ccf90
2 changed files with 60 additions and 6 deletions

View File

@ -614,15 +614,25 @@ class LocalTrade():
else:
return None
def select_filled_orders(self, order_side: str) -> List['Order']:
"""
Finds filled orders for this orderside.
:param order_side: Side of the order (either 'buy' or 'sell')
:return: array of Order objects
"""
return [o for o in self.orders if o.ft_order_side == order_side and
o.ft_is_open is False and
(o.filled or 0) > 0 and
o.status in NON_OPEN_EXCHANGE_STATES]
@property
def nr_of_successful_buys(self) -> int:
"""
Helper function to count the number of buy orders that have been filled.
:return: int count of buy orders that have been filled for this trade.
"""
return len([o for o in self.orders if o.ft_order_side == 'buy' and
o.status in NON_OPEN_EXCHANGE_STATES and
(o.filled or 0) > 0])
return len(self.select_filled_orders('buy'))
@property
def nr_of_successful_sells(self) -> int:
@ -630,9 +640,7 @@ class LocalTrade():
Helper function to count the number of sell orders that have been filled.
:return: int count of sell orders that have been filled for this trade.
"""
return len([o for o in self.orders if o.ft_order_side == 'sell' and
o.status in NON_OPEN_EXCHANGE_STATES and
(o.filled or 0) > 0])
return len(self.select_filled_orders('sell'))
@staticmethod
def get_trades_proxy(*, pair: str = None, is_open: bool = None,

View File

@ -1663,3 +1663,49 @@ def test_recalc_trade_from_orders_ignores_bad_orders(fee):
assert trade.fee_open_cost == 2 * o1_fee_cost
assert trade.open_trade_value == 2 * o1_trade_val
assert trade.nr_of_successful_buys == 2
@pytest.mark.usefixtures("init_persistence")
def test_select_filled_orders(fee):
create_mock_trades(fee)
trades = Trade.get_trades().all()
# Closed buy order, no sell order
orders = trades[0].select_filled_orders('buy')
assert orders is not None
assert len(orders) == 1
order = orders[0]
assert order.amount > 0
assert order.filled > 0
assert order.side == 'buy'
assert order.ft_order_side == 'buy'
assert order.status == 'closed'
orders = trades[0].select_filled_orders('sell')
assert orders is not None
assert len(orders) == 0
# closed buy order, and closed sell order
orders = trades[1].select_filled_orders('buy')
assert orders is not None
assert len(orders) == 1
orders = trades[1].select_filled_orders('sell')
assert orders is not None
assert len(orders) == 1
# Has open buy order
orders = trades[3].select_filled_orders('buy')
assert orders is not None
assert len(orders) == 0
orders = trades[3].select_filled_orders('sell')
assert orders is not None
assert len(orders) == 0
# Open sell order
orders = trades[4].select_filled_orders('buy')
assert orders is not None
assert len(orders) == 1
orders = trades[4].select_filled_orders('sell')
assert orders is not None
assert len(orders) == 0