backtesting._enter_trade get liquidation_price and backtesting._leverage_prep

This commit is contained in:
Sam Germain 2022-02-26 10:07:00 -06:00
parent 92ad353169
commit 6dbd249570
1 changed files with 48 additions and 2 deletions

View File

@ -591,6 +591,42 @@ class Backtesting:
else:
return self._get_sell_trade_entry_for_candle(trade, sell_row)
def _leverage_prep(
self,
pair: str,
open_rate: float,
amount: float, # quote currency, includes leverage
leverage: float,
is_short: bool
) -> Tuple[float, Optional[float]]:
# if TradingMode == TradingMode.MARGIN:
# interest_rate = self.exchange.get_interest_rate(
# pair=pair,
# open_rate=open_rate,
# is_short=is_short
# )
if self.trading_mode == TradingMode.SPOT:
return (0.0, None)
elif (
self.margin_mode == MarginMode.ISOLATED and
self.trading_mode == TradingMode.FUTURES
):
wallet_balance = (amount * open_rate)/leverage
isolated_liq = self.exchange.get_liquidation_price(
pair=pair,
open_rate=open_rate,
is_short=is_short,
position=amount,
wallet_balance=wallet_balance,
mm_ex_1=0.0,
upnl_ex_1=0.0,
)
return (0.0, isolated_liq)
else:
raise OperationalException(
"Freqtrade only supports isolated futures for leverage trading")
def _enter_trade(self, pair: str, row: Tuple, direction: str,
stake_amount: Optional[float] = None,
trade: Optional[LocalTrade] = None) -> Optional[LocalTrade]:
@ -666,6 +702,14 @@ class Backtesting:
amount = round((stake_amount / propose_rate) * leverage, 8)
if trade is None:
# Enter trade
is_short = (direction == 'short')
(interest_rate, isolated_liq) = self._leverage_prep(
pair=pair,
open_rate=propose_rate,
amount=amount,
leverage=leverage,
is_short=is_short,
)
self.trade_id_counter += 1
trade = LocalTrade(
id=self.trade_id_counter,
@ -682,10 +726,12 @@ class Backtesting:
is_open=True,
enter_tag=entry_tag,
exchange=self._exchange_name,
is_short=(direction == 'short'),
is_short=is_short,
trading_mode=self.trading_mode,
leverage=leverage,
orders=[]
interest_rate=interest_rate,
isolated_liq=isolated_liq,
orders=[],
)
trade.adjust_stop_loss(trade.open_rate, self.strategy.stoploss, initial=True)