From 9ec23a0292ac4a0795031245d56f285b88c71a33 Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 21 Dec 2023 18:14:43 +0100 Subject: [PATCH] Improve rpc typing --- freqtrade/freqtradebot.py | 8 ++++---- freqtrade/rpc/rpc_types.py | 7 +++++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 772e36a3f..9d3605d38 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -33,8 +33,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 (RPCCancelMsg, RPCEntryMsg, RPCExitCancelMsg, RPCExitMsg, - RPCProtectionMsg) +from freqtrade.rpc.rpc_types import (ProfitLossStr, RPCCancelMsg, RPCEntryMsg, RPCExitCancelMsg, + RPCExitMsg, RPCProtectionMsg) from freqtrade.strategy.interface import IStrategy from freqtrade.strategy.strategy_wrapper import strategy_safe_wrapper from freqtrade.util import FtPrecise @@ -1792,7 +1792,7 @@ class FreqtradeBot(LoggingMixin): order_rate = trade.safe_close_rate profit = trade.calculate_profit(rate=order_rate) amount = trade.amount - gain = "profit" if profit.profit_ratio > 0 else "loss" + gain: ProfitLossStr = "profit" if profit.profit_ratio > 0 else "loss" msg: RPCExitMsg = { 'type': (RPCMessageType.EXIT_FILL if fill @@ -1846,7 +1846,7 @@ class FreqtradeBot(LoggingMixin): profit = trade.calculate_profit(rate=profit_rate) current_rate = self.exchange.get_rate( trade.pair, side='exit', is_short=trade.is_short, refresh=False) - gain = "profit" if profit.profit_ratio > 0 else "loss" + gain: ProfitLossStr = "profit" if profit.profit_ratio > 0 else "loss" msg: RPCExitCancelMsg = { 'type': RPCMessageType.EXIT_CANCEL, diff --git a/freqtrade/rpc/rpc_types.py b/freqtrade/rpc/rpc_types.py index c2f05bba5..c7403dc0f 100644 --- a/freqtrade/rpc/rpc_types.py +++ b/freqtrade/rpc/rpc_types.py @@ -5,6 +5,9 @@ from freqtrade.constants import PairWithTimeframe from freqtrade.enums import RPCMessageType +ProfitLossStr = Literal["profit", "loss"] + + class RPCSendMsgBase(TypedDict): pass # ty1pe: Literal[RPCMessageType] @@ -74,7 +77,7 @@ class RPCCancelMsg(__RPCEntryExitMsgBase): class RPCExitMsg(__RPCEntryExitMsgBase): type: Literal[RPCMessageType.EXIT, RPCMessageType.EXIT_FILL] cumulative_profit: float - gain: str # Literal["profit", "loss"] + gain: ProfitLossStr close_rate: float profit_amount: float profit_ratio: float @@ -88,7 +91,7 @@ class RPCExitMsg(__RPCEntryExitMsgBase): class RPCExitCancelMsg(__RPCEntryExitMsgBase): type: Literal[RPCMessageType.EXIT_CANCEL] reason: str - gain: str # Literal["profit", "loss"] + gain: ProfitLossStr profit_amount: float profit_ratio: float sell_reason: Optional[str]