Add test for new /status telegram message

This commit is contained in:
Matthias 2022-02-03 19:41:45 +01:00
parent 29879bb415
commit 1e6362debf
5 changed files with 53 additions and 10 deletions

View File

@ -213,8 +213,6 @@ class RPC:
order['type'], order['side'], order['remaining'] order['type'], order['side'], order['remaining']
) if order else None, ) if order else None,
)) ))
cp_cfg = self._config
trade_dict['position_adjustment_enable'] = cp_cfg['position_adjustment_enable']
results.append(trade_dict) results.append(trade_dict)
return results return results

View File

@ -391,8 +391,8 @@ class Telegram(RPCHandler):
sumA += (filled_trades[y]["amount"] * filled_trades[y]["average"]) sumA += (filled_trades[y]["amount"] * filled_trades[y]["average"])
sumB += filled_trades[y]["amount"] sumB += filled_trades[y]["amount"]
prev_avg_price = sumA/sumB prev_avg_price = sumA/sumB
price_to_1st_buy = (cur_buy_average - filled_trades[0]["average"]) \ price_to_1st_buy = ((cur_buy_average - filled_trades[0]["average"])
/ filled_trades[0]["average"] / filled_trades[0]["average"])
minus_on_buy = (cur_buy_average - prev_avg_price)/prev_avg_price minus_on_buy = (cur_buy_average - prev_avg_price)/prev_avg_price
dur_buys = current_buy_datetime - arrow.get(filled_trades[x-1]["order_filled_date"]) dur_buys = current_buy_datetime - arrow.get(filled_trades[x-1]["order_filled_date"])
days = dur_buys.days days = dur_buys.days
@ -433,13 +433,12 @@ class Telegram(RPCHandler):
trade_ids = [int(i) for i in context.args if i.isnumeric()] trade_ids = [int(i) for i in context.args if i.isnumeric()]
results = self._rpc._rpc_trade_status(trade_ids=trade_ids) results = self._rpc._rpc_trade_status(trade_ids=trade_ids)
position_adjust = self._config.get('position_adjustment_enable', False)
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(r['filled_entry_orders'])
r['sell_reason'] = r.get('sell_reason', "") r['sell_reason'] = r.get('sell_reason', "")
r['position_adjustment_enable'] = r.get('position_adjustment_enable', False)
lines = [ lines = [
"*Trade ID:* `{trade_id}` `(since {open_date_hum})`", "*Trade ID:* `{trade_id}` `(since {open_date_hum})`",
"*Current Pair:* {pair}", "*Current Pair:* {pair}",
@ -448,8 +447,8 @@ class Telegram(RPCHandler):
"*Sell Reason:* `{sell_reason}`" if r['sell_reason'] else "", "*Sell Reason:* `{sell_reason}`" if r['sell_reason'] else "",
] ]
if r['position_adjustment_enable']: if position_adjust:
lines.append("*Number of Buy(s):* `{num_buys}`") lines.append("*Number of Buy(s):* `{num_entries}`")
lines.extend([ lines.extend([
"*Open Rate:* `{open_rate:.8f}`", "*Open Rate:* `{open_rate:.8f}`",

View File

@ -14,6 +14,7 @@ def mock_order_1():
'side': 'buy', 'side': 'buy',
'type': 'limit', 'type': 'limit',
'price': 0.123, 'price': 0.123,
'average': 0.123,
'amount': 123.0, 'amount': 123.0,
'filled': 123.0, 'filled': 123.0,
'remaining': 0.0, 'remaining': 0.0,

View File

@ -108,7 +108,6 @@ 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',
'position_adjustment_enable': False,
'filled_entry_orders': [{ 'filled_entry_orders': [{
'amount': 91.07468123, 'average': 1.098e-05, 'amount': 91.07468123, 'average': 1.098e-05,
'cost': 0.0009999999999054, 'filled': 91.07468123, 'ft_order_side': 'buy', 'cost': 0.0009999999999054, 'filled': 91.07468123, 'ft_order_side': 'buy',
@ -184,7 +183,6 @@ 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',
'position_adjustment_enable': False,
'filled_entry_orders': [{ 'filled_entry_orders': [{
'amount': 91.07468123, 'average': 1.098e-05, 'amount': 91.07468123, 'average': 1.098e-05,
'cost': 0.0009999999999054, 'filled': 91.07468123, 'ft_order_side': 'buy', 'cost': 0.0009999999999054, 'filled': 91.07468123, 'ft_order_side': 'buy',

View File

@ -23,6 +23,7 @@ from freqtrade.exceptions import OperationalException
from freqtrade.freqtradebot import FreqtradeBot from freqtrade.freqtradebot import FreqtradeBot
from freqtrade.loggers import setup_logging from freqtrade.loggers import setup_logging
from freqtrade.persistence import PairLocks, Trade from freqtrade.persistence import PairLocks, Trade
from freqtrade.persistence.models import Order
from freqtrade.rpc import RPC from freqtrade.rpc import RPC
from freqtrade.rpc.rpc import RPCException from freqtrade.rpc.rpc import RPCException
from freqtrade.rpc.telegram import Telegram, authorized_only from freqtrade.rpc.telegram import Telegram, authorized_only
@ -218,6 +219,52 @@ def test_telegram_status(default_conf, update, mocker) -> None:
assert status_table.call_count == 1 assert status_table.call_count == 1
@pytest.mark.usefixtures("init_persistence")
def test_telegram_status_multi_entry(default_conf, update, mocker, fee) -> None:
update.message.chat.id = "123"
default_conf['telegram']['enabled'] = False
default_conf['telegram']['chat_id'] = "123"
default_conf['position_adjustment_enable'] = True
mocker.patch.multiple(
'freqtrade.exchange.Exchange',
fetch_order=MagicMock(return_value=None),
get_rate=MagicMock(return_value=0.22),
)
telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf)
create_mock_trades(fee)
trades = Trade.get_open_trades()
trade = trades[0]
trade.orders.append(Order(
order_id='5412vbb',
ft_order_side='buy',
ft_pair=trade.pair,
ft_is_open=False,
status="closed",
symbol=trade.pair,
order_type="market",
side="buy",
price=trade.open_rate * 0.95,
average=trade.open_rate * 0.95,
filled=trade.amount,
remaining=0,
cost=trade.amount,
order_date=trade.open_date,
order_filled_date=trade.open_date,
)
)
trade.recalc_trade_from_orders()
Trade.commit()
telegram._status(update=update, context=MagicMock())
assert msg_mock.call_count == 4
msg = msg_mock.call_args_list[0][0][0]
assert re.search(r'Number of Buy.*2', msg)
assert re.search(r'Average Buy Price', msg)
assert re.search(r'Order filled at', msg)
def test_status_handle(default_conf, update, ticker, fee, mocker) -> None: def test_status_handle(default_conf, update, ticker, fee, mocker) -> None:
default_conf['max_open_trades'] = 3 default_conf['max_open_trades'] = 3
mocker.patch.multiple( mocker.patch.multiple(