allowing custom rpc messages to be sent

This commit is contained in:
Emilio Basualdo 2022-08-11 13:46:41 -03:00
parent dd4e44931e
commit 803c94f735
5 changed files with 20 additions and 1 deletions

View File

@ -19,6 +19,8 @@ class RPCMessageType(Enum):
STRATEGY_MSG = 'strategy_msg' STRATEGY_MSG = 'strategy_msg'
CUSTOM_MSG = "custom_msg"
def __repr__(self): def __repr__(self):
return self.value return self.value

View File

@ -81,6 +81,7 @@ class FreqtradeBot(LoggingMixin):
# the initial state of the bot. # the initial state of the bot.
# Keep this at the end of this initialization method. # Keep this at the end of this initialization method.
self.rpc: RPCManager = RPCManager(self) self.rpc: RPCManager = RPCManager(self)
self.strategy.set_rpc_send_msg_function(self.rpc.send_msg)
self.pairlists = PairListManager(self.exchange, self.config) self.pairlists = PairListManager(self.exchange, self.config)

View File

@ -407,6 +407,8 @@ class Telegram(RPCHandler):
message = f"{msg['status']}" message = f"{msg['status']}"
elif msg_type == RPCMessageType.STRATEGY_MSG: elif msg_type == RPCMessageType.STRATEGY_MSG:
message = f"{msg['msg']}" message = f"{msg['msg']}"
elif msg_type == RPCMessageType.CUSTOM_MSG:
message = f"{msg['msg']}"
else: else:
raise NotImplementedError(f"Unknown message type: {msg_type}") raise NotImplementedError(f"Unknown message type: {msg_type}")
return message return message

View File

@ -5,7 +5,7 @@ This module defines the interface to apply for strategies
import logging import logging
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from datetime import datetime, timedelta, timezone from datetime import datetime, timedelta, timezone
from typing import Dict, List, Optional, Tuple, Union from typing import Dict, List, Optional, Tuple, Union, Callable
import arrow import arrow
from pandas import DataFrame from pandas import DataFrame
@ -18,6 +18,7 @@ from freqtrade.enums.runmode import RunMode
from freqtrade.exceptions import OperationalException, StrategyError from freqtrade.exceptions import OperationalException, StrategyError
from freqtrade.exchange import timeframe_to_minutes, timeframe_to_next_date, timeframe_to_seconds from freqtrade.exchange import timeframe_to_minutes, timeframe_to_next_date, timeframe_to_seconds
from freqtrade.persistence import Order, PairLocks, Trade from freqtrade.persistence import Order, PairLocks, Trade
from freqtrade.rpc import RPC
from freqtrade.strategy.hyper import HyperStrategyMixin from freqtrade.strategy.hyper import HyperStrategyMixin
from freqtrade.strategy.informative_decorator import (InformativeData, PopulateIndicators, from freqtrade.strategy.informative_decorator import (InformativeData, PopulateIndicators,
_create_and_merge_informative_pair, _create_and_merge_informative_pair,
@ -119,6 +120,9 @@ class IStrategy(ABC, HyperStrategyMixin):
# Definition of plot_config. See plotting documentation for more details. # Definition of plot_config. See plotting documentation for more details.
plot_config: Dict = {} plot_config: Dict = {}
# Add rpc handler to send custom messages
send_msg = lambda x: x
def __init__(self, config: dict) -> None: def __init__(self, config: dict) -> None:
self.config = config self.config = config
# Dict to determine if analysis is necessary # Dict to determine if analysis is necessary
@ -1191,3 +1195,6 @@ class IStrategy(ABC, HyperStrategyMixin):
if 'exit_long' not in df.columns: if 'exit_long' not in df.columns:
df = df.rename({'sell': 'exit_long'}, axis='columns') df = df.rename({'sell': 'exit_long'}, axis='columns')
return df return df
def set_rpc_send_msg_function(self, rpc_send_msg_function: Callable):
self.send_msg = rpc_send_msg_function

View File

@ -6,6 +6,7 @@ import numpy as np # noqa
import pandas as pd # noqa import pandas as pd # noqa
from pandas import DataFrame from pandas import DataFrame
from freqtrade.enums import RPCMessageType
from freqtrade.strategy import (BooleanParameter, CategoricalParameter, DecimalParameter, from freqtrade.strategy import (BooleanParameter, CategoricalParameter, DecimalParameter,
IStrategy, IntParameter) IStrategy, IntParameter)
@ -108,6 +109,12 @@ class SampleStrategy(IStrategy):
} }
} }
def bot_start(self, **kwargs) -> None:
self.send_msg({
'type': RPCMessageType.CUSTOM_MSG,
'msg': "This is a custom message sent to all RPCs"
})
def informative_pairs(self): def informative_pairs(self):
""" """
Define additional, informative pair/interval combinations to be cached from the exchange. Define additional, informative pair/interval combinations to be cached from the exchange.