Test and document leverage strategy callback

This commit is contained in:
Matthias
2021-09-22 20:14:52 +02:00
parent c791b95405
commit 5928ba9c88
5 changed files with 83 additions and 20 deletions

View File

@@ -36,6 +36,7 @@ logging.getLogger('').setLevel(logging.INFO)
np.seterr(all='raise')
CURRENT_TEST_STRATEGY = 'StrategyTestV3'
TRADE_SIDES = ('long', 'short')
def pytest_addoption(parser):

View File

@@ -1,5 +1,6 @@
# pragma pylint: disable=missing-docstring, invalid-name, pointless-string-statement
from datetime import datetime
import talib.abstract as ta
from pandas import DataFrame
@@ -159,3 +160,11 @@ class StrategyTestV3(IStrategy):
# TODO-lev: Add short logic
return dataframe
def leverage(self, pair: str, current_time: datetime, current_rate: float,
proposed_leverage: float, max_leverage: float, side: str,
**kwargs) -> float:
# Return 3.0 in all cases.
# Bot-logic must make sure it's an allowed leverage and eventually adjust accordingly.
return 3.0

View File

@@ -21,7 +21,7 @@ from freqtrade.strategy.hyper import (BaseParameter, BooleanParameter, Categoric
DecimalParameter, IntParameter, RealParameter)
from freqtrade.strategy.interface import SellCheckTuple
from freqtrade.strategy.strategy_wrapper import strategy_safe_wrapper
from tests.conftest import log_has, log_has_re
from tests.conftest import CURRENT_TEST_STRATEGY, TRADE_SIDES, log_has, log_has_re
from .strats.strategy_test_v3 import StrategyTestV3
@@ -506,6 +506,32 @@ def test_custom_sell(default_conf, fee, caplog) -> None:
assert log_has_re('Custom sell reason returned from custom_sell is too long.*', caplog)
@pytest.mark.parametrize('side', TRADE_SIDES)
def test_leverage_callback(default_conf, side) -> None:
default_conf['strategy'] = 'StrategyTestV2'
strategy = StrategyResolver.load_strategy(default_conf)
assert strategy.leverage(
pair='XRP/USDT',
current_time=datetime.now(timezone.utc),
current_rate=2.2,
proposed_leverage=1.0,
max_leverage=5.0,
side=side,
) == 1
default_conf['strategy'] = CURRENT_TEST_STRATEGY
strategy = StrategyResolver.load_strategy(default_conf)
assert strategy.leverage(
pair='XRP/USDT',
current_time=datetime.now(timezone.utc),
current_rate=2.2,
proposed_leverage=1.0,
max_leverage=5.0,
side=side,
) == 3
def test_analyze_ticker_default(ohlcv_history, mocker, caplog) -> None:
caplog.set_level(logging.DEBUG)
ind_mock = MagicMock(side_effect=lambda x, meta: x)