Properly preserve trade's low during backtesting
This commit is contained in:
parent
5bfb9edf02
commit
cf27968b97
@ -354,12 +354,12 @@ class LocalTrade():
|
||||
LocalTrade.trades_open = []
|
||||
LocalTrade.total_profit = 0
|
||||
|
||||
def adjust_min_max_rates(self, current_price: float) -> None:
|
||||
def adjust_min_max_rates(self, current_price: float, current_price_low: float) -> None:
|
||||
"""
|
||||
Adjust the max_rate and min_rate.
|
||||
"""
|
||||
self.max_rate = max(current_price, self.max_rate or self.open_rate)
|
||||
self.min_rate = min(current_price, self.min_rate or self.open_rate)
|
||||
self.min_rate = min(current_price_low, self.min_rate or self.open_rate)
|
||||
|
||||
def _set_new_stoploss(self, new_loss: float, stoploss: float):
|
||||
"""Assign new stop value"""
|
||||
|
@ -568,7 +568,7 @@ class IStrategy(ABC, HyperStrategyMixin):
|
||||
current_rate = rate
|
||||
current_profit = trade.calc_profit_ratio(current_rate)
|
||||
|
||||
trade.adjust_min_max_rates(high or current_rate)
|
||||
trade.adjust_min_max_rates(high or current_rate, low or current_rate)
|
||||
|
||||
stoplossflag = self.stop_loss_reached(current_rate=current_rate, trade=trade,
|
||||
current_time=date, current_profit=current_profit,
|
||||
|
@ -584,7 +584,7 @@ def test_backtest_one(default_conf, fee, mocker, testdatadir) -> None:
|
||||
'initial_stop_loss_ratio': [-0.1, -0.1],
|
||||
'stop_loss_abs': [0.0940005, 0.09272236],
|
||||
'stop_loss_ratio': [-0.1, -0.1],
|
||||
'min_rate': [0.1038, 0.10302485],
|
||||
'min_rate': [0.10370188, 0.10300000000000001],
|
||||
'max_rate': [0.10501, 0.1038888],
|
||||
'is_open': [False, False],
|
||||
'buy_tag': [None, None],
|
||||
|
@ -398,7 +398,7 @@ def test_stop_loss_reached(default_conf, fee, profit, adjusted, expected, traili
|
||||
exchange='binance',
|
||||
open_rate=1,
|
||||
)
|
||||
trade.adjust_min_max_rates(trade.open_rate)
|
||||
trade.adjust_min_max_rates(trade.open_rate, trade.open_rate)
|
||||
strategy.trailing_stop = trailing
|
||||
strategy.trailing_stop_positive = -0.05
|
||||
strategy.use_custom_stoploss = custom
|
||||
|
@ -799,25 +799,30 @@ def test_adjust_min_max_rates(fee):
|
||||
open_rate=1,
|
||||
)
|
||||
|
||||
trade.adjust_min_max_rates(trade.open_rate)
|
||||
trade.adjust_min_max_rates(trade.open_rate, trade.open_rate)
|
||||
assert trade.max_rate == 1
|
||||
assert trade.min_rate == 1
|
||||
|
||||
# check min adjusted, max remained
|
||||
trade.adjust_min_max_rates(0.96)
|
||||
trade.adjust_min_max_rates(0.96, 0.96)
|
||||
assert trade.max_rate == 1
|
||||
assert trade.min_rate == 0.96
|
||||
|
||||
# check max adjusted, min remains
|
||||
trade.adjust_min_max_rates(1.05)
|
||||
trade.adjust_min_max_rates(1.05, 1.05)
|
||||
assert trade.max_rate == 1.05
|
||||
assert trade.min_rate == 0.96
|
||||
|
||||
# current rate "in the middle" - no adjustment
|
||||
trade.adjust_min_max_rates(1.03)
|
||||
trade.adjust_min_max_rates(1.03, 1.03)
|
||||
assert trade.max_rate == 1.05
|
||||
assert trade.min_rate == 0.96
|
||||
|
||||
# current rate "in the middle" - no adjustment
|
||||
trade.adjust_min_max_rates(1.10, 0.91)
|
||||
assert trade.max_rate == 1.10
|
||||
assert trade.min_rate == 0.91
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("init_persistence")
|
||||
@pytest.mark.parametrize('use_db', [True, False])
|
||||
|
Loading…
Reference in New Issue
Block a user