Merge pull request #8184 from LangLazy/feature

Feature market direction
This commit is contained in:
Matthias
2023-02-28 17:22:31 +01:00
committed by GitHub
7 changed files with 109 additions and 9 deletions

View File

@@ -19,8 +19,8 @@ from freqtrade.configuration.timerange import TimeRange
from freqtrade.constants import CANCEL_REASON, DATETIME_PRINT_FORMAT, Config
from freqtrade.data.history import load_data
from freqtrade.data.metrics import calculate_max_drawdown
from freqtrade.enums import (CandleType, ExitCheckTuple, ExitType, SignalDirection, State,
TradingMode)
from freqtrade.enums import (CandleType, ExitCheckTuple, ExitType, MarketDirection, SignalDirection,
State, TradingMode)
from freqtrade.exceptions import ExchangeError, PricingError
from freqtrade.exchange import timeframe_to_minutes, timeframe_to_msecs
from freqtrade.loggers import bufferHandler
@@ -1205,3 +1205,9 @@ class RPC:
'last_process_loc': last_p.astimezone(tzlocal()).strftime(DATETIME_PRINT_FORMAT),
'last_process_ts': int(last_p.timestamp()),
}
def _update_market_direction(self, direction: MarketDirection) -> None:
self._freqtrade.strategy.market_direction = direction
def _get_market_direction(self) -> MarketDirection:
return self._freqtrade.strategy.market_direction

View File

@@ -25,7 +25,7 @@ from telegram.utils.helpers import escape_markdown
from freqtrade.__init__ import __version__
from freqtrade.constants import DUST_PER_COIN, Config
from freqtrade.enums import RPCMessageType, SignalDirection, TradingMode
from freqtrade.enums import MarketDirection, RPCMessageType, SignalDirection, TradingMode
from freqtrade.exceptions import OperationalException
from freqtrade.misc import chunks, plural, round_coin_value
from freqtrade.persistence import Trade
@@ -129,7 +129,8 @@ class Telegram(RPCHandler):
r'/weekly$', r'/weekly \d+$', r'/monthly$', r'/monthly \d+$',
r'/forcebuy$', r'/forcelong$', r'/forceshort$',
r'/forcesell$', r'/forceexit$',
r'/edge$', r'/health$', r'/help$', r'/version$'
r'/edge$', r'/health$', r'/help$', r'/version$', r'/marketdir (long|short|even|none)$',
r'/marketdir$'
]
# Create keys for generation
valid_keys_print = [k.replace('$', '') for k in valid_keys]
@@ -197,6 +198,7 @@ class Telegram(RPCHandler):
CommandHandler('health', self._health),
CommandHandler('help', self._help),
CommandHandler('version', self._version),
CommandHandler('marketdir', self._changemarketdir)
]
callbacks = [
CallbackQueryHandler(self._status_table, pattern='update_status_table'),
@@ -1502,6 +1504,9 @@ class Telegram(RPCHandler):
"*/count:* `Show number of active trades compared to allowed number of trades`\n"
"*/edge:* `Shows validated pairs by Edge if it is enabled` \n"
"*/health* `Show latest process timestamp - defaults to 1970-01-01 00:00:00` \n"
"*/marketdir [long | short | even | none]:* `Updates the user managed variable "
"that represents the current market direction. If no direction is provided `"
"`the currently set market direction will be output.` \n"
"_Statistics_\n"
"------------\n"
@@ -1685,3 +1690,39 @@ class Telegram(RPCHandler):
'TelegramError: %s! Giving up on that message.',
telegram_err.message
)
@authorized_only
def _changemarketdir(self, update: Update, context: CallbackContext) -> None:
"""
Handler for /marketdir.
Updates the bot's market_direction
:param bot: telegram bot
:param update: message update
:return: None
"""
if context.args and len(context.args) == 1:
new_market_dir_arg = context.args[0]
old_market_dir = self._rpc._get_market_direction()
new_market_dir = None
if new_market_dir_arg == "long":
new_market_dir = MarketDirection.LONG
elif new_market_dir_arg == "short":
new_market_dir = MarketDirection.SHORT
elif new_market_dir_arg == "even":
new_market_dir = MarketDirection.EVEN
elif new_market_dir_arg == "none":
new_market_dir = MarketDirection.NONE
if new_market_dir is not None:
self._rpc._update_market_direction(new_market_dir)
self._send_msg("Successfully updated market direction"
f" from *{old_market_dir}* to *{new_market_dir}*.")
else:
raise RPCException("Invalid market direction provided. \n"
"Valid market directions: *long, short, even, none*")
elif context.args is not None and len(context.args) == 0:
old_market_dir = self._rpc._get_market_direction()
self._send_msg(f"Currently set market direction: *{old_market_dir}*")
else:
raise RPCException("Invalid usage of command /marketdir. \n"
"Usage: */marketdir [short | long | even | none]*")