Merge branch 'freqtrade:feat/short' into feat/short

This commit is contained in:
Guillermo Rodríguez 2022-02-22 19:34:59 +01:00 committed by GitHub
commit 8e3839c74c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 15 additions and 6 deletions

View File

@ -14,6 +14,7 @@ from freqtrade.configuration.directory_operations import create_datadir, create_
from freqtrade.configuration.environment_vars import enironment_vars_to_dict
from freqtrade.configuration.load_config import load_config_file, load_file
from freqtrade.enums import NON_UTIL_MODES, TRADING_MODES, CandleType, RunMode
from freqtrade.enums.tradingmode import TradingMode
from freqtrade.exceptions import OperationalException
from freqtrade.loggers import setup_logging
from freqtrade.misc import deep_merge_dicts, parse_db_uri_for_logging
@ -436,7 +437,7 @@ class Configuration:
self._args_to_config(config, argname='trading_mode',
logstring='Detected --trading-mode: {}')
config['candle_type_def'] = CandleType.get_default(config.get('trading_mode', 'spot'))
config['trading_mode'] = TradingMode(config.get('trading_mode', 'spot'))
self._args_to_config(config, argname='candle_types',
logstring='Detected --candle-types: {}')

View File

@ -1,7 +1,7 @@
from enum import Enum
class TradingMode(Enum):
class TradingMode(str, Enum):
"""
Enum to distinguish between
spot, margin, futures or any other trading method

View File

@ -137,7 +137,7 @@ class Exchange:
self._trades_pagination_arg = self._ft_has['trades_pagination_arg']
# Leverage properties
self.trading_mode = TradingMode(config.get('trading_mode', 'spot'))
self.trading_mode: TradingMode = config.get('trading_mode', TradingMode.SPOT)
self.margin_mode: Optional[MarginMode] = (
MarginMode(config.get('margin_mode'))
if config.get('margin_mode')

View File

@ -104,7 +104,7 @@ class FreqtradeBot(LoggingMixin):
LoggingMixin.__init__(self, logger, timeframe_to_seconds(self.strategy.timeframe))
self.liquidation_buffer = float(self.config.get('liquidation_buffer', '0.05'))
self.trading_mode = TradingMode(self.config.get('trading_mode', 'spot'))
self.trading_mode: TradingMode = self.config.get('trading_mode', TradingMode.SPOT)
self.margin_mode_type: Optional[MarginMode] = None
if 'margin_mode' in self.config:
self.margin_mode = MarginMode(self.config['margin_mode'])

View File

@ -129,7 +129,7 @@ class Backtesting:
# TODO-lev: This should come from the configuration setting or better a
# TODO-lev: combination of config/strategy "use_shorts"(?) and "can_short" from the exchange
self.trading_mode = TradingMode(config.get('trading_mode', 'spot'))
self.trading_mode: TradingMode = config.get('trading_mode', TradingMode.SPOT)
self._can_short = self.trading_mode != TradingMode.SPOT
self.progress = BTProgress()

View File

@ -14,6 +14,7 @@ from pandas import DataFrame
from freqtrade.constants import ListPairsWithTimeframes
from freqtrade.data.dataprovider import DataProvider
from freqtrade.enums import CandleType, SellType, SignalDirection, SignalTagType, SignalType
from freqtrade.enums.tradingmode import TradingMode
from freqtrade.exceptions import OperationalException, StrategyError
from freqtrade.exchange import timeframe_to_minutes, timeframe_to_seconds
from freqtrade.exchange.exchange import timeframe_to_next_date
@ -765,7 +766,8 @@ class IStrategy(ABC, HyperStrategyMixin):
if enter_long == 1 and not any([exit_long, enter_short]):
enter_signal = SignalDirection.LONG
enter_tag_value = latest.get(SignalTagType.ENTER_TAG.value, None)
if enter_short == 1 and not any([exit_short, enter_long]):
if (self.config.get('trading_mode', TradingMode.SPOT) != TradingMode.SPOT
and enter_short == 1 and not any([exit_short, enter_long])):
enter_signal = SignalDirection.SHORT
enter_tag_value = latest.get(SignalTagType.ENTER_TAG.value, None)

View File

@ -74,6 +74,10 @@ def test_returns_latest_signal(ohlcv_history):
mocked_history.loc[1, 'exit_short'] = 0
mocked_history.loc[1, 'enter_tag'] = 'sell_signal_01'
# Don't provide short signal while in spot mode
assert _STRATEGY.get_entry_signal('ETH/BTC', '5m', mocked_history) == (None, None)
_STRATEGY.config['trading_mode'] = 'futures'
assert _STRATEGY.get_entry_signal(
'ETH/BTC', '5m', mocked_history) == (SignalDirection.SHORT, 'sell_signal_01')
assert _STRATEGY.get_exit_signal('ETH/BTC', '5m', mocked_history) == (False, False, None)
@ -89,6 +93,8 @@ def test_returns_latest_signal(ohlcv_history):
assert _STRATEGY.get_exit_signal(
'ETH/BTC', '5m', mocked_history, True) == (False, True, 'sell_signal_02')
_STRATEGY.config['trading_mode'] = 'spot'
def test_analyze_pair_empty(default_conf, mocker, caplog, ohlcv_history):
mocker.patch.object(_STRATEGY.dp, 'ohlcv', return_value=ohlcv_history)