From 0ece73578c67c64bf83147bc87f9e36e6f502aae Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 20 Sep 2022 19:51:42 +0200 Subject: [PATCH 01/10] Add typedDict for RPC messages Currently not fully functional. --- freqtrade/freqtradebot.py | 10 ++-- freqtrade/rpc/api_server/webserver.py | 5 +- freqtrade/rpc/rpc.py | 3 +- freqtrade/rpc/rpc_manager.py | 5 +- freqtrade/rpc/rpc_types.py | 82 +++++++++++++++++++++++++++ freqtrade/rpc/telegram.py | 3 +- freqtrade/rpc/webhook.py | 5 +- 7 files changed, 101 insertions(+), 12 deletions(-) create mode 100644 freqtrade/rpc/rpc_types.py diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 623d39c09..28dd1fae5 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -30,6 +30,7 @@ from freqtrade.plugins.protectionmanager import ProtectionManager from freqtrade.resolvers import ExchangeResolver, StrategyResolver from freqtrade.rpc import RPCManager from freqtrade.rpc.external_message_consumer import ExternalMessageConsumer +from freqtrade.rpc.rpc_types import RPCBuyMsg, RPCCancelMsg, RPCSellCancelMsg, RPCSellMsg from freqtrade.strategy.interface import IStrategy from freqtrade.strategy.strategy_wrapper import strategy_safe_wrapper from freqtrade.util import FtPrecise @@ -957,7 +958,7 @@ class FreqtradeBot(LoggingMixin): current_rate = self.exchange.get_rate( trade.pair, side='entry', is_short=trade.is_short, refresh=False) - msg = { + msg: RPCBuyMsg = { 'trade_id': trade.id, 'type': msg_type, 'buy_tag': trade.enter_tag, @@ -989,7 +990,7 @@ class FreqtradeBot(LoggingMixin): current_rate = self.exchange.get_rate( trade.pair, side='entry', is_short=trade.is_short, refresh=False) - msg = { + msg: RPCCancelMsg = { 'trade_id': trade.id, 'type': RPCMessageType.ENTRY_CANCEL, 'buy_tag': trade.enter_tag, @@ -1001,6 +1002,7 @@ class FreqtradeBot(LoggingMixin): 'limit': trade.open_rate, 'order_type': order_type, 'stake_amount': trade.stake_amount, + 'open_rate': trade.open_rate, 'stake_currency': self.config['stake_currency'], 'fiat_currency': self.config.get('fiat_display_currency', None), 'amount': trade.amount, @@ -1666,7 +1668,7 @@ class FreqtradeBot(LoggingMixin): amount = trade.amount gain = "profit" if profit_ratio > 0 else "loss" - msg = { + msg: RPCSellMsg = { 'type': (RPCMessageType.EXIT_FILL if fill else RPCMessageType.EXIT), 'trade_id': trade.id, @@ -1722,7 +1724,7 @@ class FreqtradeBot(LoggingMixin): profit_ratio = trade.calc_profit_ratio(profit_rate) gain = "profit" if profit_ratio > 0 else "loss" - msg = { + msg: RPCSellCancelMsg = { 'type': RPCMessageType.EXIT_CANCEL, 'trade_id': trade.id, 'exchange': trade.exchange.capitalize(), diff --git a/freqtrade/rpc/api_server/webserver.py b/freqtrade/rpc/api_server/webserver.py index b53662451..2413e5264 100644 --- a/freqtrade/rpc/api_server/webserver.py +++ b/freqtrade/rpc/api_server/webserver.py @@ -1,6 +1,6 @@ import logging from ipaddress import IPv4Address -from typing import Any, Dict, Optional +from typing import Any, Optional import orjson import uvicorn @@ -13,6 +13,7 @@ from freqtrade.exceptions import OperationalException from freqtrade.rpc.api_server.uvicorn_threaded import UvicornServer from freqtrade.rpc.api_server.ws.message_stream import MessageStream from freqtrade.rpc.rpc import RPC, RPCException, RPCHandler +from freqtrade.rpc.rpc_types import RPCSendMsg logger = logging.getLogger(__name__) @@ -108,7 +109,7 @@ class ApiServer(RPCHandler): cls._has_rpc = False cls._rpc = None - def send_msg(self, msg: Dict[str, Any]) -> None: + def send_msg(self, msg: RPCSendMsg) -> None: """ Publish the message to the message stream """ diff --git a/freqtrade/rpc/rpc.py b/freqtrade/rpc/rpc.py index c6a6f5cae..2b5eb107c 100644 --- a/freqtrade/rpc/rpc.py +++ b/freqtrade/rpc/rpc.py @@ -30,6 +30,7 @@ from freqtrade.persistence import Order, PairLocks, Trade from freqtrade.persistence.models import PairLock from freqtrade.plugins.pairlist.pairlist_helpers import expand_pairlist from freqtrade.rpc.fiat_convert import CryptoToFiatConverter +from freqtrade.rpc.rpc_types import RPCSendMsg from freqtrade.wallets import PositionWallet, Wallet @@ -79,7 +80,7 @@ class RPCHandler: """ Cleanup pending module resources """ @abstractmethod - def send_msg(self, msg: Dict[str, str]) -> None: + def send_msg(self, msg: RPCSendMsg) -> None: """ Sends a message to all registered rpc modules """ diff --git a/freqtrade/rpc/rpc_manager.py b/freqtrade/rpc/rpc_manager.py index c4d4fa2dd..e4c925995 100644 --- a/freqtrade/rpc/rpc_manager.py +++ b/freqtrade/rpc/rpc_manager.py @@ -3,11 +3,12 @@ This module contains class to manage RPC communications (Telegram, API, ...) """ import logging from collections import deque -from typing import Any, Dict, List +from typing import List from freqtrade.constants import Config from freqtrade.enums import NO_ECHO_MESSAGES, RPCMessageType from freqtrade.rpc import RPC, RPCHandler +from freqtrade.rpc.rpc_types import RPCSendMsg logger = logging.getLogger(__name__) @@ -58,7 +59,7 @@ class RPCManager: mod.cleanup() del mod - def send_msg(self, msg: Dict[str, Any]) -> None: + def send_msg(self, msg: RPCSendMsg) -> None: """ Send given message to all registered rpc modules. A message consists of one or more key value pairs of strings. diff --git a/freqtrade/rpc/rpc_types.py b/freqtrade/rpc/rpc_types.py new file mode 100644 index 000000000..0fb5a6bfa --- /dev/null +++ b/freqtrade/rpc/rpc_types.py @@ -0,0 +1,82 @@ +from datetime import datetime +from typing import Optional, TypedDict, Union + +from freqtrade.enums import RPCMessageType + + +class RPCSendMsgBase(TypedDict): + type: RPCMessageType + + +class RPCStatusMsg(RPCSendMsgBase): + """Used for Status, Startup and Warning messages""" + status: str + + +class RPCProtectionMsg(RPCSendMsgBase): + id: int + pair: str + base_currency: Optional[str] + lock_time: str + lock_timestamp: int + lock_end_time: str + lock_end_timestamp: int + reason: str + side: str + active: bool + + +class RPCBuyMsg(RPCSendMsgBase): + trade_id: str + buy_tag: str + enter_tag: str + exchange: str + pair: str + leverage: float + direction: str + limit: float + open_rate: float + order_type: Optional[str] # TODO: why optional?? + stake_amount: float + stake_currency: str + fiat_currency: Optional[str] + amount: float + open_date: datetime + current_rate: float + sub_trade: bool + + +class RPCCancelMsg(RPCBuyMsg): + reason: str + + +class RPCSellMsg(RPCBuyMsg): + cumulative_profit: float + gain: str # Literal["profit", "loss"] + close_rate: float + profit_amount: float + profit_ratio: float + sell_reason: str + exit_reason: str + close_date: datetime + current_rate: Optional[float] + + +class RPCSellCancelMsg(RPCBuyMsg): + reason: str + gain: str # Literal["profit", "loss"] + profit_amount: float + profit_ratio: float + sell_reason: str + exit_reason: str + close_date: datetime + + +RPCSendMsg = Union[ + RPCStatusMsg, + RPCProtectionMsg, + RPCBuyMsg, + RPCCancelMsg, + RPCSellMsg, + RPCSellCancelMsg + ] diff --git a/freqtrade/rpc/telegram.py b/freqtrade/rpc/telegram.py index 962c5e058..c1365702d 100644 --- a/freqtrade/rpc/telegram.py +++ b/freqtrade/rpc/telegram.py @@ -30,6 +30,7 @@ from freqtrade.exceptions import OperationalException from freqtrade.misc import chunks, plural, round_coin_value from freqtrade.persistence import Trade from freqtrade.rpc import RPC, RPCException, RPCHandler +from freqtrade.rpc.rpc_types import RPCSendMsg logger = logging.getLogger(__name__) @@ -429,7 +430,7 @@ class Telegram(RPCHandler): return None return message - def send_msg(self, msg: Dict[str, Any]) -> None: + def send_msg(self, msg: RPCSendMsg) -> None: """ Send a message to telegram channel """ default_noti = 'on' diff --git a/freqtrade/rpc/webhook.py b/freqtrade/rpc/webhook.py index 118ebed88..14b881126 100644 --- a/freqtrade/rpc/webhook.py +++ b/freqtrade/rpc/webhook.py @@ -10,6 +10,7 @@ from requests import RequestException, post from freqtrade.constants import Config from freqtrade.enums import RPCMessageType from freqtrade.rpc import RPC, RPCHandler +from freqtrade.rpc.rpc_types import RPCSendMsg logger = logging.getLogger(__name__) @@ -41,7 +42,7 @@ class Webhook(RPCHandler): """ pass - def _get_value_dict(self, msg: Dict[str, Any]) -> Optional[Dict[str, Any]]: + def _get_value_dict(self, msg: RPCSendMsg) -> Optional[Dict[str, Any]]: whconfig = self._config['webhook'] # Deprecated 2022.10 - only keep generic method. if msg['type'] in [RPCMessageType.ENTRY]: @@ -75,7 +76,7 @@ class Webhook(RPCHandler): return None return valuedict - def send_msg(self, msg: Dict[str, Any]) -> None: + def send_msg(self, msg: RPCSendMsg) -> None: """ Send a message to telegram channel """ try: From 70ad7b42b1b0492fe79f28ffc4ce855d318c8adc Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 6 Mar 2023 19:23:05 +0100 Subject: [PATCH 02/10] Improve msg typing --- freqtrade/rpc/api_server/webserver.py | 2 +- freqtrade/rpc/rpc_types.py | 21 +++++++++++---------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/freqtrade/rpc/api_server/webserver.py b/freqtrade/rpc/api_server/webserver.py index 2413e5264..8030e303b 100644 --- a/freqtrade/rpc/api_server/webserver.py +++ b/freqtrade/rpc/api_server/webserver.py @@ -1,6 +1,6 @@ import logging from ipaddress import IPv4Address -from typing import Any, Optional +from typing import Any, Dict, Optional import orjson import uvicorn diff --git a/freqtrade/rpc/rpc_types.py b/freqtrade/rpc/rpc_types.py index 0fb5a6bfa..bde985548 100644 --- a/freqtrade/rpc/rpc_types.py +++ b/freqtrade/rpc/rpc_types.py @@ -27,12 +27,12 @@ class RPCProtectionMsg(RPCSendMsgBase): class RPCBuyMsg(RPCSendMsgBase): - trade_id: str - buy_tag: str - enter_tag: str + trade_id: int + buy_tag: Optional[str] + enter_tag: Optional[str] exchange: str pair: str - leverage: float + leverage: Optional[float] direction: str limit: float open_rate: float @@ -42,7 +42,7 @@ class RPCBuyMsg(RPCSendMsgBase): fiat_currency: Optional[str] amount: float open_date: datetime - current_rate: float + current_rate: Optional[float] sub_trade: bool @@ -56,10 +56,11 @@ class RPCSellMsg(RPCBuyMsg): close_rate: float profit_amount: float profit_ratio: float - sell_reason: str - exit_reason: str + sell_reason: Optional[str] + exit_reason: Optional[str] close_date: datetime - current_rate: Optional[float] + # current_rate: Optional[float] + order_rate: Optional[float] class RPCSellCancelMsg(RPCBuyMsg): @@ -67,8 +68,8 @@ class RPCSellCancelMsg(RPCBuyMsg): gain: str # Literal["profit", "loss"] profit_amount: float profit_ratio: float - sell_reason: str - exit_reason: str + sell_reason: Optional[str] + exit_reason: Optional[str] close_date: datetime From 245ae99273dcc7ff591c37d830b1b8462b1efde6 Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 6 Mar 2023 19:44:23 +0100 Subject: [PATCH 03/10] Further typing ... --- freqtrade/freqtradebot.py | 15 ++++++++++----- freqtrade/rpc/rpc_types.py | 17 +++++++++++++++-- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 28dd1fae5..7db9c1e30 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -30,7 +30,8 @@ from freqtrade.plugins.protectionmanager import ProtectionManager from freqtrade.resolvers import ExchangeResolver, StrategyResolver from freqtrade.rpc import RPCManager from freqtrade.rpc.external_message_consumer import ExternalMessageConsumer -from freqtrade.rpc.rpc_types import RPCBuyMsg, RPCCancelMsg, RPCSellCancelMsg, RPCSellMsg +from freqtrade.rpc.rpc_types import (RPCBuyMsg, RPCCancelMsg, RPCProtectionMsg, RPCSellCancelMsg, + RPCSellMsg) from freqtrade.strategy.interface import IStrategy from freqtrade.strategy.strategy_wrapper import strategy_safe_wrapper from freqtrade.util import FtPrecise @@ -1853,14 +1854,18 @@ class FreqtradeBot(LoggingMixin): self.strategy.lock_pair(pair, datetime.now(timezone.utc), reason='Auto lock') prot_trig = self.protections.stop_per_pair(pair, side=side) if prot_trig: - msg = {'type': RPCMessageType.PROTECTION_TRIGGER, } - msg.update(prot_trig.to_json()) + msg: RPCProtectionMsg = { + 'type': RPCMessageType.PROTECTION_TRIGGER, + **prot_trig.to_json() + } self.rpc.send_msg(msg) prot_trig_glb = self.protections.global_stop(side=side) if prot_trig_glb: - msg = {'type': RPCMessageType.PROTECTION_TRIGGER_GLOBAL, } - msg.update(prot_trig_glb.to_json()) + msg = { + 'type': RPCMessageType.PROTECTION_TRIGGER_GLOBAL, + **prot_trig_glb.to_json() + } self.rpc.send_msg(msg) def apply_fee_conditional(self, trade: Trade, trade_base_currency: str, diff --git a/freqtrade/rpc/rpc_types.py b/freqtrade/rpc/rpc_types.py index bde985548..b81591954 100644 --- a/freqtrade/rpc/rpc_types.py +++ b/freqtrade/rpc/rpc_types.py @@ -1,19 +1,22 @@ from datetime import datetime -from typing import Optional, TypedDict, Union +from typing import List, Literal, Optional, TypedDict, Union from freqtrade.enums import RPCMessageType class RPCSendMsgBase(TypedDict): - type: RPCMessageType + pass + # ty1pe: Literal[RPCMessageType] class RPCStatusMsg(RPCSendMsgBase): """Used for Status, Startup and Warning messages""" + type: Literal[RPCMessageType.STATUS] status: str class RPCProtectionMsg(RPCSendMsgBase): + type: Literal[RPCMessageType.PROTECTION_TRIGGER, RPCMessageType.PROTECTION_TRIGGER_GLOBAL] id: int pair: str base_currency: Optional[str] @@ -26,7 +29,13 @@ class RPCProtectionMsg(RPCSendMsgBase): active: bool +class RPCWhitelistMsg(RPCSendMsgBase): + type: Literal[RPCMessageType.WHITELIST] + data: List[str] + + class RPCBuyMsg(RPCSendMsgBase): + type: Literal[RPCMessageType.ENTRY, RPCMessageType.ENTRY_FILL] trade_id: int buy_tag: Optional[str] enter_tag: Optional[str] @@ -47,10 +56,12 @@ class RPCBuyMsg(RPCSendMsgBase): class RPCCancelMsg(RPCBuyMsg): + type: Literal[RPCMessageType.ENTRY_CANCEL] reason: str class RPCSellMsg(RPCBuyMsg): + type: Literal[RPCMessageType.EXIT, RPCMessageType.EXIT_FILL] cumulative_profit: float gain: str # Literal["profit", "loss"] close_rate: float @@ -64,6 +75,7 @@ class RPCSellMsg(RPCBuyMsg): class RPCSellCancelMsg(RPCBuyMsg): + type: Literal[RPCMessageType.EXIT_CANCEL] reason: str gain: str # Literal["profit", "loss"] profit_amount: float @@ -76,6 +88,7 @@ class RPCSellCancelMsg(RPCBuyMsg): RPCSendMsg = Union[ RPCStatusMsg, RPCProtectionMsg, + RPCWhitelistMsg, RPCBuyMsg, RPCCancelMsg, RPCSellMsg, From 76d289f0cecb504c18e678cf06c93cbefab5ca0d Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 24 Mar 2023 20:35:01 +0100 Subject: [PATCH 04/10] Don't overwrite types --- freqtrade/rpc/rpc_types.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/freqtrade/rpc/rpc_types.py b/freqtrade/rpc/rpc_types.py index b81591954..a43cfd34e 100644 --- a/freqtrade/rpc/rpc_types.py +++ b/freqtrade/rpc/rpc_types.py @@ -34,8 +34,7 @@ class RPCWhitelistMsg(RPCSendMsgBase): data: List[str] -class RPCBuyMsg(RPCSendMsgBase): - type: Literal[RPCMessageType.ENTRY, RPCMessageType.ENTRY_FILL] +class __RPCBuyMsgBase(RPCSendMsgBase): trade_id: int buy_tag: Optional[str] enter_tag: Optional[str] @@ -55,12 +54,16 @@ class RPCBuyMsg(RPCSendMsgBase): sub_trade: bool -class RPCCancelMsg(RPCBuyMsg): +class RPCBuyMsg(__RPCBuyMsgBase): + type: Literal[RPCMessageType.ENTRY, RPCMessageType.ENTRY_FILL] + + +class RPCCancelMsg(__RPCBuyMsgBase): type: Literal[RPCMessageType.ENTRY_CANCEL] reason: str -class RPCSellMsg(RPCBuyMsg): +class RPCSellMsg(__RPCBuyMsgBase): type: Literal[RPCMessageType.EXIT, RPCMessageType.EXIT_FILL] cumulative_profit: float gain: str # Literal["profit", "loss"] @@ -74,7 +77,7 @@ class RPCSellMsg(RPCBuyMsg): order_rate: Optional[float] -class RPCSellCancelMsg(RPCBuyMsg): +class RPCSellCancelMsg(__RPCBuyMsgBase): type: Literal[RPCMessageType.EXIT_CANCEL] reason: str gain: str # Literal["profit", "loss"] From e8cffeeffd9972f97769406ffce6498875b089ba Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 24 Mar 2023 20:36:29 +0100 Subject: [PATCH 05/10] Update RPCStatusMessage type --- freqtrade/rpc/rpc_types.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/freqtrade/rpc/rpc_types.py b/freqtrade/rpc/rpc_types.py index a43cfd34e..5ddaa0c17 100644 --- a/freqtrade/rpc/rpc_types.py +++ b/freqtrade/rpc/rpc_types.py @@ -11,7 +11,7 @@ class RPCSendMsgBase(TypedDict): class RPCStatusMsg(RPCSendMsgBase): """Used for Status, Startup and Warning messages""" - type: Literal[RPCMessageType.STATUS] + type: Literal[RPCMessageType.STATUS, RPCMessageType.STARTUP, RPCMessageType.WARNING] status: str From 8928d3616a81a45d8c7084150f67beded55337a7 Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 24 Mar 2023 20:47:17 +0100 Subject: [PATCH 06/10] Improve msgtypes --- freqtrade/freqtradebot.py | 4 ++++ freqtrade/rpc/rpc_types.py | 8 ++++++++ freqtrade/rpc/telegram.py | 2 +- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 7db9c1e30..3aa1075d8 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -973,6 +973,7 @@ class FreqtradeBot(LoggingMixin): 'order_type': order_type, 'stake_amount': trade.stake_amount, 'stake_currency': self.config['stake_currency'], + 'base_currency': self.exchange.get_pair_base_currency(trade.pair), 'fiat_currency': self.config.get('fiat_display_currency', None), 'amount': order.safe_amount_after_fee if fill else (order.amount or trade.amount), 'open_date': trade.open_date or datetime.utcnow(), @@ -1005,6 +1006,7 @@ class FreqtradeBot(LoggingMixin): 'stake_amount': trade.stake_amount, 'open_rate': trade.open_rate, 'stake_currency': self.config['stake_currency'], + 'base_currency': self.exchange.get_pair_base_currency(trade.pair), 'fiat_currency': self.config.get('fiat_display_currency', None), 'amount': trade.amount, 'open_date': trade.open_date, @@ -1695,6 +1697,7 @@ class FreqtradeBot(LoggingMixin): 'close_date': trade.close_date or datetime.utcnow(), 'stake_amount': trade.stake_amount, 'stake_currency': self.config['stake_currency'], + 'base_currency': self.exchange.get_pair_base_currency(trade.pair), 'fiat_currency': self.config.get('fiat_display_currency'), 'sub_trade': sub_trade, 'cumulative_profit': trade.realized_profit, @@ -1747,6 +1750,7 @@ class FreqtradeBot(LoggingMixin): 'open_date': trade.open_date, 'close_date': trade.close_date or datetime.now(timezone.utc), 'stake_currency': self.config['stake_currency'], + 'base_currency': self.exchange.get_pair_base_currency(trade.pair), 'fiat_currency': self.config.get('fiat_display_currency', None), 'reason': reason, 'sub_trade': sub_trade, diff --git a/freqtrade/rpc/rpc_types.py b/freqtrade/rpc/rpc_types.py index 5ddaa0c17..5a3549aa7 100644 --- a/freqtrade/rpc/rpc_types.py +++ b/freqtrade/rpc/rpc_types.py @@ -15,6 +15,12 @@ class RPCStatusMsg(RPCSendMsgBase): status: str +class RPCStrategyMsg(RPCSendMsgBase): + """Used for Status, Startup and Warning messages""" + type: Literal[RPCMessageType.STRATEGY_MSG] + msg: str + + class RPCProtectionMsg(RPCSendMsgBase): type: Literal[RPCMessageType.PROTECTION_TRIGGER, RPCMessageType.PROTECTION_TRIGGER_GLOBAL] id: int @@ -40,6 +46,7 @@ class __RPCBuyMsgBase(RPCSendMsgBase): enter_tag: Optional[str] exchange: str pair: str + base_currency: str leverage: Optional[float] direction: str limit: float @@ -90,6 +97,7 @@ class RPCSellCancelMsg(__RPCBuyMsgBase): RPCSendMsg = Union[ RPCStatusMsg, + RPCStrategyMsg, RPCProtectionMsg, RPCWhitelistMsg, RPCBuyMsg, diff --git a/freqtrade/rpc/telegram.py b/freqtrade/rpc/telegram.py index c1365702d..6f34b7325 100644 --- a/freqtrade/rpc/telegram.py +++ b/freqtrade/rpc/telegram.py @@ -437,7 +437,7 @@ class Telegram(RPCHandler): msg_type = msg['type'] noti = '' - if msg_type == RPCMessageType.EXIT: + if msg['type'] == RPCMessageType.EXIT: sell_noti = self._config['telegram'] \ .get('notification_settings', {}).get(str(msg_type), {}) # For backward compatibility sell still can be string From ad58bac810bf5cfd2fbcd9556a192f0b1dc0d429 Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 24 Mar 2023 20:54:28 +0100 Subject: [PATCH 07/10] Type WS messagetypes --- freqtrade/data/dataprovider.py | 6 +++--- freqtrade/rpc/rpc_types.py | 25 +++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/freqtrade/data/dataprovider.py b/freqtrade/data/dataprovider.py index 3991432a4..d05ee5db7 100644 --- a/freqtrade/data/dataprovider.py +++ b/freqtrade/data/dataprovider.py @@ -21,6 +21,7 @@ from freqtrade.exchange import Exchange, timeframe_to_seconds from freqtrade.exchange.types import OrderBook from freqtrade.misc import append_candles_to_dataframe from freqtrade.rpc import RPCManager +from freqtrade.rpc.rpc_types import RPCAnalyzedDFMsg from freqtrade.util import PeriodicCache @@ -118,8 +119,7 @@ class DataProvider: :param new_candle: This is a new candle """ if self.__rpc: - self.__rpc.send_msg( - { + msg: RPCAnalyzedDFMsg = { 'type': RPCMessageType.ANALYZED_DF, 'data': { 'key': pair_key, @@ -127,7 +127,7 @@ class DataProvider: 'la': datetime.now(timezone.utc) } } - ) + self.__rpc.send_msg(msg) if new_candle: self.__rpc.send_msg({ 'type': RPCMessageType.NEW_CANDLE, diff --git a/freqtrade/rpc/rpc_types.py b/freqtrade/rpc/rpc_types.py index 5a3549aa7..a290ee78c 100644 --- a/freqtrade/rpc/rpc_types.py +++ b/freqtrade/rpc/rpc_types.py @@ -1,6 +1,7 @@ from datetime import datetime -from typing import List, Literal, Optional, TypedDict, Union +from typing import Any, List, Literal, Optional, TypedDict, Union +from freqtrade.constants import PairWithTimeframe from freqtrade.enums import RPCMessageType @@ -95,6 +96,24 @@ class RPCSellCancelMsg(__RPCBuyMsgBase): close_date: datetime +class __AnalyzedDFData(TypedDict): + key: PairWithTimeframe + df: Any + la: datetime + + +class RPCAnalyzedDFMsg(RPCSendMsgBase): + """New Analyzed dataframe message""" + type: Literal[RPCMessageType.ANALYZED_DF] + data: __AnalyzedDFData + + +class RPCNewCandleMsg(RPCSendMsgBase): + """New candle ping message, issued once per new candle/pair""" + type: Literal[RPCMessageType.NEW_CANDLE] + data: PairWithTimeframe + + RPCSendMsg = Union[ RPCStatusMsg, RPCStrategyMsg, @@ -103,5 +122,7 @@ RPCSendMsg = Union[ RPCBuyMsg, RPCCancelMsg, RPCSellMsg, - RPCSellCancelMsg + RPCSellCancelMsg, + RPCAnalyzedDFMsg, + RPCNewCandleMsg ] From 281dd7785ebf190460bdf483f0839799bcf55d58 Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 24 Mar 2023 20:56:18 +0100 Subject: [PATCH 08/10] Fix some remaining type errors --- freqtrade/freqtradebot.py | 7 +++---- freqtrade/rpc/telegram.py | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 3aa1075d8..1f7b4493c 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -948,7 +948,6 @@ class FreqtradeBot(LoggingMixin): """ Sends rpc notification when a entry order occurred. """ - msg_type = RPCMessageType.ENTRY_FILL if fill else RPCMessageType.ENTRY open_rate = order.safe_price if open_rate is None: @@ -961,7 +960,7 @@ class FreqtradeBot(LoggingMixin): msg: RPCBuyMsg = { 'trade_id': trade.id, - 'type': msg_type, + 'type': RPCMessageType.ENTRY_FILL if fill else RPCMessageType.ENTRY, 'buy_tag': trade.enter_tag, 'enter_tag': trade.enter_tag, 'exchange': trade.exchange.capitalize(), @@ -1860,7 +1859,7 @@ class FreqtradeBot(LoggingMixin): if prot_trig: msg: RPCProtectionMsg = { 'type': RPCMessageType.PROTECTION_TRIGGER, - **prot_trig.to_json() + **prot_trig.to_json() # type: ignore } self.rpc.send_msg(msg) @@ -1868,7 +1867,7 @@ class FreqtradeBot(LoggingMixin): if prot_trig_glb: msg = { 'type': RPCMessageType.PROTECTION_TRIGGER_GLOBAL, - **prot_trig_glb.to_json() + **prot_trig_glb.to_json() # type: ignore } self.rpc.send_msg(msg) diff --git a/freqtrade/rpc/telegram.py b/freqtrade/rpc/telegram.py index 6f34b7325..d79d8ea76 100644 --- a/freqtrade/rpc/telegram.py +++ b/freqtrade/rpc/telegram.py @@ -454,7 +454,7 @@ class Telegram(RPCHandler): # Notification disabled return - message = self.compose_message(deepcopy(msg), msg_type) + message = self.compose_message(deepcopy(msg), msg_type) # type: ignore if message: self._send_msg(message, disable_notification=(noti == 'silent')) From cbdd86d7775ac767b45c0ce4673cb192eeb30fad Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 24 Mar 2023 21:05:10 +0100 Subject: [PATCH 09/10] Fix test failures due to additional field --- freqtrade/rpc/rpc_types.py | 4 ++-- tests/test_freqtradebot.py | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/freqtrade/rpc/rpc_types.py b/freqtrade/rpc/rpc_types.py index a290ee78c..3277a2d6e 100644 --- a/freqtrade/rpc/rpc_types.py +++ b/freqtrade/rpc/rpc_types.py @@ -96,7 +96,7 @@ class RPCSellCancelMsg(__RPCBuyMsgBase): close_date: datetime -class __AnalyzedDFData(TypedDict): +class _AnalyzedDFData(TypedDict): key: PairWithTimeframe df: Any la: datetime @@ -105,7 +105,7 @@ class __AnalyzedDFData(TypedDict): class RPCAnalyzedDFMsg(RPCSendMsgBase): """New Analyzed dataframe message""" type: Literal[RPCMessageType.ANALYZED_DF] - data: __AnalyzedDFData + data: _AnalyzedDFData class RPCNewCandleMsg(RPCSendMsgBase): diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index cea70ec48..da98fed94 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -3326,6 +3326,7 @@ def test_execute_trade_exit_up(default_conf_usdt, ticker_usdt, fee, ticker_usdt_ 'profit_ratio': 0.00493809 if is_short else 0.09451372, 'stake_currency': 'USDT', 'fiat_currency': 'USD', + 'base_currency': 'ETH', 'sell_reason': ExitType.ROI.value, 'exit_reason': ExitType.ROI.value, 'open_date': ANY, @@ -3389,6 +3390,7 @@ def test_execute_trade_exit_down(default_conf_usdt, ticker_usdt, fee, ticker_usd 'profit_amount': -5.65990099 if is_short else -0.00075, 'profit_ratio': -0.0945681 if is_short else -1.247e-05, 'stake_currency': 'USDT', + 'base_currency': 'ETH', 'fiat_currency': 'USD', 'sell_reason': ExitType.STOP_LOSS.value, 'exit_reason': ExitType.STOP_LOSS.value, @@ -3474,6 +3476,7 @@ def test_execute_trade_exit_custom_exit_price( 'profit_amount': pytest.approx(profit_amount), 'profit_ratio': profit_ratio, 'stake_currency': 'USDT', + 'base_currency': 'ETH', 'fiat_currency': 'USD', 'sell_reason': 'foo', 'exit_reason': 'foo', @@ -3547,6 +3550,7 @@ def test_execute_trade_exit_down_stoploss_on_exchange_dry_run( 'profit_ratio': -0.00501253 if is_short else -0.01493766, 'stake_currency': 'USDT', 'fiat_currency': 'USD', + 'base_currency': 'ETH', 'sell_reason': ExitType.STOP_LOSS.value, 'exit_reason': ExitType.STOP_LOSS.value, 'open_date': ANY, @@ -3811,6 +3815,7 @@ def test_execute_trade_exit_market_order( 'profit_amount': pytest.approx(profit_amount), 'profit_ratio': profit_ratio, 'stake_currency': 'USDT', + 'base_currency': 'ETH', 'fiat_currency': 'USD', 'sell_reason': ExitType.ROI.value, 'exit_reason': ExitType.ROI.value, From c0a57d352f7f8058cb20c4b60b4b5779ca9b1a45 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 25 Mar 2023 08:16:07 +0100 Subject: [PATCH 10/10] send base_currency with messages that need it. --- freqtrade/freqtradebot.py | 2 ++ freqtrade/rpc/rpc_manager.py | 4 ---- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 1f7b4493c..1f072571d 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -1859,6 +1859,7 @@ class FreqtradeBot(LoggingMixin): if prot_trig: msg: RPCProtectionMsg = { 'type': RPCMessageType.PROTECTION_TRIGGER, + 'base_currency': self.exchange.get_pair_base_currency(prot_trig.pair), **prot_trig.to_json() # type: ignore } self.rpc.send_msg(msg) @@ -1867,6 +1868,7 @@ class FreqtradeBot(LoggingMixin): if prot_trig_glb: msg = { 'type': RPCMessageType.PROTECTION_TRIGGER_GLOBAL, + 'base_currency': self.exchange.get_pair_base_currency(prot_trig_glb.pair), **prot_trig_glb.to_json() # type: ignore } self.rpc.send_msg(msg) diff --git a/freqtrade/rpc/rpc_manager.py b/freqtrade/rpc/rpc_manager.py index e4c925995..1972ad6e5 100644 --- a/freqtrade/rpc/rpc_manager.py +++ b/freqtrade/rpc/rpc_manager.py @@ -70,10 +70,6 @@ class RPCManager: """ if msg.get('type') not in NO_ECHO_MESSAGES: logger.info('Sending rpc message: %s', msg) - if 'pair' in msg: - msg.update({ - 'base_currency': self._rpc._freqtrade.exchange.get_pair_base_currency(msg['pair']) - }) for mod in self.registered_modules: logger.debug('Forwarding message to rpc.%s', mod.name) try: