allow adjust_trade_position to return tuples in backtesting

This commit is contained in:
Matthias
2024-01-28 20:05:40 +01:00
parent e8288a34c9
commit 95e51bf816
2 changed files with 14 additions and 4 deletions
+11 -3
View File
@@ -537,14 +537,22 @@ class Backtesting:
min_stake = self.exchange.get_min_pair_stake_amount(trade.pair, current_rate, -0.1) min_stake = self.exchange.get_min_pair_stake_amount(trade.pair, current_rate, -0.1)
max_stake = self.exchange.get_max_pair_stake_amount(trade.pair, current_rate) max_stake = self.exchange.get_max_pair_stake_amount(trade.pair, current_rate)
stake_available = self.wallets.get_available_stake_amount() stake_available = self.wallets.get_available_stake_amount()
stake_amount = strategy_safe_wrapper(self.strategy.adjust_trade_position, resp = strategy_safe_wrapper(self.strategy.adjust_trade_position,
default_retval=None, supress_error=True)( default_retval=None, supress_error=True)(
trade=trade, # type: ignore[arg-type] trade=trade, # type: ignore[arg-type]
current_time=current_time, current_rate=current_rate, current_time=current_time, current_rate=current_rate,
current_profit=current_profit, min_stake=min_stake, current_profit=current_profit, min_stake=min_stake,
max_stake=min(max_stake, stake_available), max_stake=min(max_stake, stake_available),
current_entry_rate=current_rate, current_exit_rate=current_rate, current_entry_rate=current_rate, current_exit_rate=current_rate,
current_entry_profit=current_profit, current_exit_profit=current_profit) current_entry_profit=current_profit, current_exit_profit=current_profit)
order_tag = ''
if isinstance(resp, tuple):
if len(resp) >= 1:
stake_amount = resp[0]
if len(resp) > 1:
order_tag = resp[1] or ''
else:
stake_amount = resp
# Check if we should increase our position # Check if we should increase our position
if stake_amount is not None and stake_amount > 0.0: if stake_amount is not None and stake_amount > 0.0:
@@ -569,7 +577,7 @@ class Backtesting:
if min_stake and remaining != 0 and remaining < min_stake: if min_stake and remaining != 0 and remaining < min_stake:
# Remaining stake is too low to be sold. # Remaining stake is too low to be sold.
return trade return trade
exit_ = ExitCheckTuple(ExitType.PARTIAL_EXIT) exit_ = ExitCheckTuple(ExitType.PARTIAL_EXIT, order_tag)
pos_trade = self._get_exit_for_signal(trade, row, exit_, current_time, amount) pos_trade = self._get_exit_for_signal(trade, row, exit_, current_time, amount)
if pos_trade is not None: if pos_trade is not None:
order = pos_trade.orders[-1] order = pos_trade.orders[-1]
+3 -1
View File
@@ -511,7 +511,8 @@ class IStrategy(ABC, HyperStrategyMixin):
min_stake: Optional[float], max_stake: float, min_stake: Optional[float], max_stake: float,
current_entry_rate: float, current_exit_rate: float, current_entry_rate: float, current_exit_rate: float,
current_entry_profit: float, current_exit_profit: float, current_entry_profit: float, current_exit_profit: float,
**kwargs) -> Optional[float]: **kwargs
) -> Union[Optional[float], Tuple[Optional[float], Optional[str]]]:
""" """
Custom trade adjustment logic, returning the stake amount that a trade should be Custom trade adjustment logic, returning the stake amount that a trade should be
increased or decreased. increased or decreased.
@@ -537,6 +538,7 @@ class IStrategy(ABC, HyperStrategyMixin):
:return float: Stake amount to adjust your trade, :return float: Stake amount to adjust your trade,
Positive values to increase position, Negative values to decrease position. Positive values to increase position, Negative values to decrease position.
Return None for no action. Return None for no action.
Optionally, return a tuple with a 2nd element with an order reason
""" """
return None return None