Update trade response to use a single Order object

This commit is contained in:
Matthias 2022-02-27 16:54:14 +01:00
parent 17c9c3caf3
commit e9456cdf15
7 changed files with 15 additions and 26 deletions

View File

@ -340,14 +340,7 @@ class LocalTrade():
def to_json(self) -> Dict[str, Any]: def to_json(self) -> Dict[str, Any]:
filled_orders = self.select_filled_orders() filled_orders = self.select_filled_orders()
filled_entries = [] orders = [order.to_json() for order in filled_orders]
filled_exits = []
if len(filled_orders) > 0:
for order in filled_orders:
if order.ft_order_side == 'buy':
filled_entries.append(order.to_json())
if order.ft_order_side == 'sell':
filled_exits.append(order.to_json())
return { return {
'trade_id': self.id, 'trade_id': self.id,
@ -412,8 +405,7 @@ class LocalTrade():
'max_rate': self.max_rate, 'max_rate': self.max_rate,
'open_order_id': self.open_order_id, 'open_order_id': self.open_order_id,
'filled_entry_orders': filled_entries, 'orders': orders,
'filled_exit_orders': filled_exits,
} }
@staticmethod @staticmethod

View File

@ -240,8 +240,7 @@ class TradeSchema(BaseModel):
min_rate: Optional[float] min_rate: Optional[float]
max_rate: Optional[float] max_rate: Optional[float]
open_order_id: Optional[str] open_order_id: Optional[str]
filled_entry_orders: List[OrderSchema] orders: List[OrderSchema]
filled_exit_orders: List[OrderSchema]
class OpenTradeSchema(TradeSchema): class OpenTradeSchema(TradeSchema):

View File

@ -379,6 +379,8 @@ class Telegram(RPCHandler):
first_avg = filled_orders[0]["safe_price"] first_avg = filled_orders[0]["safe_price"]
for x, order in enumerate(filled_orders): for x, order in enumerate(filled_orders):
if order['ft_order_side'] != 'buy':
continue
cur_entry_datetime = arrow.get(order["order_filled_date"]) cur_entry_datetime = arrow.get(order["order_filled_date"])
cur_entry_amount = order["amount"] cur_entry_amount = order["amount"]
cur_entry_average = order["safe_price"] cur_entry_average = order["safe_price"]
@ -444,7 +446,7 @@ class Telegram(RPCHandler):
messages = [] messages = []
for r in results: for r in results:
r['open_date_hum'] = arrow.get(r['open_date']).humanize() r['open_date_hum'] = arrow.get(r['open_date']).humanize()
r['num_entries'] = len(r['filled_entry_orders']) r['num_entries'] = len([o for o in r['orders'] if o['ft_order_side'] == 'buy'])
r['sell_reason'] = r.get('sell_reason', "") r['sell_reason'] = r.get('sell_reason', "")
lines = [ lines = [
"*Trade ID:* `{trade_id}`" + "*Trade ID:* `{trade_id}`" +
@ -488,8 +490,8 @@ class Telegram(RPCHandler):
lines.append("*Open Order:* `{open_order}`") lines.append("*Open Order:* `{open_order}`")
lines_detail = self._prepare_entry_details( lines_detail = self._prepare_entry_details(
r['filled_entry_orders'], r['base_currency'], r['is_open']) r['orders'], r['base_currency'], r['is_open'])
lines.extend((lines_detail if (len(r['filled_entry_orders']) > 1) else "")) lines.extend(lines_detail if lines_detail else "")
# Filter empty lines using list-comprehension # Filter empty lines using list-comprehension
messages.append("\n".join([line for line in lines if line]).format(**r)) messages.append("\n".join([line for line in lines if line]).format(**r))

View File

@ -109,14 +109,13 @@ def test_rpc_trade_status(default_conf, ticker, fee, mocker) -> None:
'stoploss_entry_dist_ratio': -0.10448878, 'stoploss_entry_dist_ratio': -0.10448878,
'open_order': None, 'open_order': None,
'exchange': 'binance', 'exchange': 'binance',
'filled_entry_orders': [{ 'orders': [{
'amount': 91.07468123, 'average': 1.098e-05, 'safe_price': 1.098e-05, 'amount': 91.07468123, 'average': 1.098e-05, 'safe_price': 1.098e-05,
'cost': 0.0009999999999054, 'filled': 91.07468123, 'ft_order_side': 'buy', 'cost': 0.0009999999999054, 'filled': 91.07468123, 'ft_order_side': 'buy',
'order_date': ANY, 'order_timestamp': ANY, 'order_filled_date': ANY, 'order_date': ANY, 'order_timestamp': ANY, 'order_filled_date': ANY,
'order_filled_timestamp': ANY, 'order_type': 'limit', 'price': 1.098e-05, 'order_filled_timestamp': ANY, 'order_type': 'limit', 'price': 1.098e-05,
'is_open': False, 'pair': 'ETH/BTC', 'order_id': ANY, 'is_open': False, 'pair': 'ETH/BTC', 'order_id': ANY,
'remaining': ANY, 'status': ANY}], 'remaining': ANY, 'status': ANY}],
'filled_exit_orders': []
} }
mocker.patch('freqtrade.exchange.Exchange.get_rate', mocker.patch('freqtrade.exchange.Exchange.get_rate',
@ -184,14 +183,13 @@ def test_rpc_trade_status(default_conf, ticker, fee, mocker) -> None:
'stoploss_entry_dist_ratio': -0.10448878, 'stoploss_entry_dist_ratio': -0.10448878,
'open_order': None, 'open_order': None,
'exchange': 'binance', 'exchange': 'binance',
'filled_entry_orders': [{ 'orders': [{
'amount': 91.07468123, 'average': 1.098e-05, 'safe_price': 1.098e-05, 'amount': 91.07468123, 'average': 1.098e-05, 'safe_price': 1.098e-05,
'cost': 0.0009999999999054, 'filled': 91.07468123, 'ft_order_side': 'buy', 'cost': 0.0009999999999054, 'filled': 91.07468123, 'ft_order_side': 'buy',
'order_date': ANY, 'order_timestamp': ANY, 'order_filled_date': ANY, 'order_date': ANY, 'order_timestamp': ANY, 'order_filled_date': ANY,
'order_filled_timestamp': ANY, 'order_type': 'limit', 'price': 1.098e-05, 'order_filled_timestamp': ANY, 'order_type': 'limit', 'price': 1.098e-05,
'is_open': False, 'pair': 'ETH/BTC', 'order_id': ANY, 'is_open': False, 'pair': 'ETH/BTC', 'order_id': ANY,
'remaining': ANY, 'status': ANY}], 'remaining': ANY, 'status': ANY}],
'filled_exit_orders': []
} }

View File

@ -902,8 +902,7 @@ def test_api_status(botclient, mocker, ticker, fee, markets):
'buy_tag': None, 'buy_tag': None,
'timeframe': 5, 'timeframe': 5,
'exchange': 'binance', 'exchange': 'binance',
'filled_entry_orders': [], 'orders': [ANY],
'filled_exit_orders': [],
} }
@ -1092,6 +1091,7 @@ def test_api_forcebuy(botclient, mocker, fee):
'buy_tag': None, 'buy_tag': None,
'timeframe': 5, 'timeframe': 5,
'exchange': 'binance', 'exchange': 'binance',
'orders': [],
} }

View File

@ -203,7 +203,7 @@ def test_telegram_status(default_conf, update, mocker) -> None:
'stop_loss_ratio': -0.0001, 'stop_loss_ratio': -0.0001,
'open_order': '(limit buy rem=0.00000000)', 'open_order': '(limit buy rem=0.00000000)',
'is_open': True, 'is_open': True,
'filled_entry_orders': [] 'orders': []
}]), }]),
) )

View File

@ -901,8 +901,7 @@ def test_to_json(default_conf, fee):
'buy_tag': None, 'buy_tag': None,
'timeframe': None, 'timeframe': None,
'exchange': 'binance', 'exchange': 'binance',
'filled_entry_orders': [], 'orders': [],
'filled_exit_orders': []
} }
# Simulate dry_run entries # Simulate dry_run entries
@ -970,8 +969,7 @@ def test_to_json(default_conf, fee):
'buy_tag': 'buys_signal_001', 'buy_tag': 'buys_signal_001',
'timeframe': None, 'timeframe': None,
'exchange': 'binance', 'exchange': 'binance',
'filled_entry_orders': [], 'orders': [],
'filled_exit_orders': []
} }