feat: update dry-run calculation params to be more generic
This commit is contained in:
@@ -153,8 +153,7 @@ class Binance(Exchange):
|
|||||||
stake_amount: float,
|
stake_amount: float,
|
||||||
leverage: float,
|
leverage: float,
|
||||||
wallet_balance: float, # Or margin balance
|
wallet_balance: float, # Or margin balance
|
||||||
mm_ex_1: float = 0.0, # (Binance) Cross only
|
other_trades: list,
|
||||||
upnl_ex_1: float = 0.0, # (Binance) Cross only
|
|
||||||
) -> Optional[float]:
|
) -> Optional[float]:
|
||||||
"""
|
"""
|
||||||
Important: Must be fetching data from cached values as this is used by backtesting!
|
Important: Must be fetching data from cached values as this is used by backtesting!
|
||||||
@@ -172,6 +171,7 @@ class Binance(Exchange):
|
|||||||
:param wallet_balance: Amount of margin_mode in the wallet being used to trade
|
:param wallet_balance: Amount of margin_mode in the wallet being used to trade
|
||||||
Cross-Margin Mode: crossWalletBalance
|
Cross-Margin Mode: crossWalletBalance
|
||||||
Isolated-Margin Mode: isolatedWalletBalance
|
Isolated-Margin Mode: isolatedWalletBalance
|
||||||
|
:param other_trades: List of other open trades in the same wallet
|
||||||
|
|
||||||
# * Only required for Cross
|
# * Only required for Cross
|
||||||
:param mm_ex_1: (TMM)
|
:param mm_ex_1: (TMM)
|
||||||
@@ -180,15 +180,31 @@ class Binance(Exchange):
|
|||||||
:param upnl_ex_1: (UPNL)
|
:param upnl_ex_1: (UPNL)
|
||||||
Cross-Margin Mode: Unrealized PNL of all other contracts, excluding Contract 1.
|
Cross-Margin Mode: Unrealized PNL of all other contracts, excluding Contract 1.
|
||||||
Isolated-Margin Mode: 0
|
Isolated-Margin Mode: 0
|
||||||
|
:param other
|
||||||
"""
|
"""
|
||||||
|
cross_vars: float = 0.0
|
||||||
side_1 = -1 if is_short else 1
|
|
||||||
cross_vars = upnl_ex_1 - mm_ex_1 if self.margin_mode == MarginMode.CROSS else 0.0
|
|
||||||
|
|
||||||
# mm_ratio: Binance's formula specifies maintenance margin rate which is mm_ratio * 100%
|
# mm_ratio: Binance's formula specifies maintenance margin rate which is mm_ratio * 100%
|
||||||
# maintenance_amt: (CUM) Maintenance Amount of position
|
# maintenance_amt: (CUM) Maintenance Amount of position
|
||||||
mm_ratio, maintenance_amt = self.get_maintenance_ratio_and_amt(pair, stake_amount)
|
mm_ratio, maintenance_amt = self.get_maintenance_ratio_and_amt(pair, stake_amount)
|
||||||
|
|
||||||
|
if self.margin_mode == MarginMode.CROSS:
|
||||||
|
mm_ex_1: float = 0.0
|
||||||
|
upnl_ex_1: float = 0.0
|
||||||
|
for trade in other_trades:
|
||||||
|
mm_ratio1, maint_amnt1 = self.get_maintenance_ratio_and_amt(
|
||||||
|
trade["pair"], trade["stake_amount"]
|
||||||
|
)
|
||||||
|
maint_margin = trade["amount"] * trade["mark_price"] * mm_ratio1 - maint_amnt1
|
||||||
|
mm_ex_1 += maint_margin
|
||||||
|
|
||||||
|
upnl_ex_1 += (
|
||||||
|
trade["amount"] * trade["mark_price"] - trade["amount"] * trade["open_rate"]
|
||||||
|
)
|
||||||
|
cross_vars = upnl_ex_1 - mm_ex_1
|
||||||
|
|
||||||
|
side_1 = -1 if is_short else 1
|
||||||
|
|
||||||
if maintenance_amt is None:
|
if maintenance_amt is None:
|
||||||
raise OperationalException(
|
raise OperationalException(
|
||||||
"Parameter maintenance_amt is required by Binance.liquidation_price"
|
"Parameter maintenance_amt is required by Binance.liquidation_price"
|
||||||
|
|||||||
@@ -147,8 +147,7 @@ class Bybit(Exchange):
|
|||||||
stake_amount: float,
|
stake_amount: float,
|
||||||
leverage: float,
|
leverage: float,
|
||||||
wallet_balance: float, # Or margin balance
|
wallet_balance: float, # Or margin balance
|
||||||
mm_ex_1: float = 0.0, # (Binance) Cross only
|
other_trades: list,
|
||||||
upnl_ex_1: float = 0.0, # (Binance) Cross only
|
|
||||||
) -> Optional[float]:
|
) -> Optional[float]:
|
||||||
"""
|
"""
|
||||||
Important: Must be fetching data from cached values as this is used by backtesting!
|
Important: Must be fetching data from cached values as this is used by backtesting!
|
||||||
@@ -178,6 +177,7 @@ class Bybit(Exchange):
|
|||||||
:param wallet_balance: Amount of margin_mode in the wallet being used to trade
|
:param wallet_balance: Amount of margin_mode in the wallet being used to trade
|
||||||
Cross-Margin Mode: crossWalletBalance
|
Cross-Margin Mode: crossWalletBalance
|
||||||
Isolated-Margin Mode: isolatedWalletBalance
|
Isolated-Margin Mode: isolatedWalletBalance
|
||||||
|
:param other_trades: List of other open trades in the same wallet
|
||||||
"""
|
"""
|
||||||
|
|
||||||
market = self.markets[pair]
|
market = self.markets[pair]
|
||||||
|
|||||||
@@ -3532,8 +3532,7 @@ class Exchange:
|
|||||||
stake_amount: float,
|
stake_amount: float,
|
||||||
leverage: float,
|
leverage: float,
|
||||||
wallet_balance: float,
|
wallet_balance: float,
|
||||||
mm_ex_1: float = 0.0, # (Binance) Cross only
|
other_trades: list,
|
||||||
upnl_ex_1: float = 0.0, # (Binance) Cross only
|
|
||||||
) -> Optional[float]:
|
) -> Optional[float]:
|
||||||
"""
|
"""
|
||||||
Set's the margin mode on the exchange to cross or isolated for a specific pair
|
Set's the margin mode on the exchange to cross or isolated for a specific pair
|
||||||
@@ -3555,8 +3554,7 @@ class Exchange:
|
|||||||
leverage=leverage,
|
leverage=leverage,
|
||||||
stake_amount=stake_amount,
|
stake_amount=stake_amount,
|
||||||
wallet_balance=wallet_balance,
|
wallet_balance=wallet_balance,
|
||||||
mm_ex_1=mm_ex_1,
|
other_trades=other_trades,
|
||||||
upnl_ex_1=upnl_ex_1,
|
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
positions = self.fetch_positions(pair)
|
positions = self.fetch_positions(pair)
|
||||||
@@ -3582,8 +3580,7 @@ class Exchange:
|
|||||||
stake_amount: float,
|
stake_amount: float,
|
||||||
leverage: float,
|
leverage: float,
|
||||||
wallet_balance: float, # Or margin balance
|
wallet_balance: float, # Or margin balance
|
||||||
mm_ex_1: float = 0.0, # (Binance) Cross only
|
other_trades: list,
|
||||||
upnl_ex_1: float = 0.0, # (Binance) Cross only
|
|
||||||
) -> Optional[float]:
|
) -> Optional[float]:
|
||||||
"""
|
"""
|
||||||
Important: Must be fetching data from cached values as this is used by backtesting!
|
Important: Must be fetching data from cached values as this is used by backtesting!
|
||||||
@@ -3608,10 +3605,7 @@ class Exchange:
|
|||||||
:param wallet_balance: Amount of margin_mode in the wallet being used to trade
|
:param wallet_balance: Amount of margin_mode in the wallet being used to trade
|
||||||
Cross-Margin Mode: crossWalletBalance
|
Cross-Margin Mode: crossWalletBalance
|
||||||
Isolated-Margin Mode: isolatedWalletBalance
|
Isolated-Margin Mode: isolatedWalletBalance
|
||||||
|
:param other_trades: List of other open trades in the same wallet
|
||||||
# * Not required by Gate or OKX
|
|
||||||
:param mm_ex_1:
|
|
||||||
:param upnl_ex_1:
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
market = self.markets[pair]
|
market = self.markets[pair]
|
||||||
|
|||||||
Reference in New Issue
Block a user