implemented binance.get_maintenance_ratio_and_amt
This commit is contained in:
@@ -163,11 +163,10 @@ class Binance(Exchange):
|
||||
if pair not in self._leverage_brackets:
|
||||
return 1.0
|
||||
pair_brackets = self._leverage_brackets[pair]
|
||||
max_lev = 1.0
|
||||
for [notional_floor, maint_margin_ratio] in pair_brackets:
|
||||
for [notional_floor, mm_ratio, _] in reversed(pair_brackets):
|
||||
if nominal_value >= notional_floor:
|
||||
max_lev = 1/maint_margin_ratio
|
||||
return max_lev
|
||||
return 1/mm_ratio
|
||||
return 1.0
|
||||
|
||||
@retrier
|
||||
def _set_leverage(
|
||||
@@ -227,3 +226,114 @@ class Binance(Exchange):
|
||||
:return: The cutoff open time for when a funding fee is charged
|
||||
"""
|
||||
return open_date.minute > 0 or (open_date.minute == 0 and open_date.second > 15)
|
||||
|
||||
def get_maintenance_ratio_and_amt(
|
||||
self,
|
||||
pair: Optional[str],
|
||||
nominal_value: Optional[float]
|
||||
):
|
||||
'''
|
||||
Maintenance amt = Floor of Position Bracket on Level n *
|
||||
difference between
|
||||
Maintenance Margin Rate on Level n and
|
||||
Maintenance Margin Rate on Level n-1)
|
||||
+ Maintenance Amount on Level n-1
|
||||
https://www.binance.com/en/support/faq/b3c689c1f50a44cabb3a84e663b81d93
|
||||
'''
|
||||
if pair not in self._leverage_brackets:
|
||||
raise InvalidOrderException(f"Cannot calculate liquidation price for {pair}")
|
||||
pair_brackets = self._leverage_brackets[pair]
|
||||
for [notional_floor, mm_ratio, amt] in reversed(pair_brackets):
|
||||
if nominal_value >= notional_floor:
|
||||
return (mm_ratio, amt)
|
||||
raise OperationalException("nominal value can not be lower than 0")
|
||||
# The lowest notional_floor for any pair in loadLeverageBrackets is always 0 because it
|
||||
# describes the min amount for a bracket, and the lowest bracket will always go down to 0
|
||||
|
||||
def liquidation_price_helper(
|
||||
self,
|
||||
open_rate: float, # Entry price of position
|
||||
is_short: bool,
|
||||
leverage: float,
|
||||
trading_mode: TradingMode,
|
||||
mm_ratio: float,
|
||||
collateral: Collateral,
|
||||
maintenance_amt: Optional[float] = None, # (Binance)
|
||||
position: Optional[float] = None, # (Binance and Gateio) Absolute value of position size
|
||||
wallet_balance: Optional[float] = None, # (Binance and Gateio)
|
||||
taker_fee_rate: Optional[float] = None, # (Gateio & Okex)
|
||||
liability: Optional[float] = None, # (Okex)
|
||||
interest: Optional[float] = None, # (Okex)
|
||||
position_assets: Optional[float] = None, # * (Okex) Might be same as position
|
||||
mm_ex_1: Optional[float] = 0.0, # (Binance) Cross only
|
||||
upnl_ex_1: Optional[float] = 0.0, # (Binance) Cross only
|
||||
) -> Optional[float]:
|
||||
"""
|
||||
MARGIN: https://www.binance.com/en/support/faq/f6b010588e55413aa58b7d63ee0125ed
|
||||
PERPETUAL: https://www.binance.com/en/support/faq/b3c689c1f50a44cabb3a84e663b81d93
|
||||
|
||||
:param exchange_name:
|
||||
:param open_rate: (EP1) Entry price of position
|
||||
:param is_short: True if the trade is a short, false otherwise
|
||||
:param leverage: The amount of leverage on the trade
|
||||
:param trading_mode: SPOT, MARGIN, FUTURES, etc.
|
||||
:param position: Absolute value of position size (in base currency)
|
||||
:param mm_ratio: (MMR)
|
||||
# Binance's formula specifies maintenance margin rate which is mm_ratio * 100%
|
||||
:param collateral: Either ISOLATED or CROSS
|
||||
:param maintenance_amt: (CUM) Maintenance Amount of position
|
||||
:param wallet_balance: (WB)
|
||||
Cross-Margin Mode: crossWalletBalance
|
||||
Isolated-Margin Mode: isolatedWalletBalance
|
||||
:param position: Absolute value of position size (in base currency)
|
||||
|
||||
# * Not required by Binance
|
||||
:param taker_fee_rate:
|
||||
:param liability:
|
||||
:param interest:
|
||||
:param position_assets:
|
||||
|
||||
# * Only required for Cross
|
||||
:param mm_ex_1: (TMM)
|
||||
Cross-Margin Mode: Maintenance Margin of all other contracts, excluding Contract 1
|
||||
Isolated-Margin Mode: 0
|
||||
:param upnl_ex_1: (UPNL)
|
||||
Cross-Margin Mode: Unrealized PNL of all other contracts, excluding Contract 1.
|
||||
Isolated-Margin Mode: 0
|
||||
"""
|
||||
if trading_mode == TradingMode.SPOT:
|
||||
return None
|
||||
|
||||
if not collateral:
|
||||
raise OperationalException(
|
||||
"Parameter collateral is required by liquidation_price when trading_mode is "
|
||||
f"{trading_mode}"
|
||||
)
|
||||
if (
|
||||
(wallet_balance is None or maintenance_amt is None or position is None) or
|
||||
(collateral == Collateral.CROSS and (mm_ex_1 is None or upnl_ex_1 is None))
|
||||
):
|
||||
required_params = "wallet_balance, maintenance_amt, position"
|
||||
if collateral == Collateral.CROSS:
|
||||
required_params += ", mm_ex_1, upnl_ex_1"
|
||||
raise OperationalException(
|
||||
f"Parameters {required_params} are required by Binance.liquidation_price"
|
||||
f"for {collateral.name} {trading_mode.name}"
|
||||
)
|
||||
|
||||
side_1 = -1 if is_short else 1
|
||||
position = abs(position)
|
||||
cross_vars = upnl_ex_1 - mm_ex_1 if collateral == Collateral.CROSS else 0.0 # type: ignore
|
||||
|
||||
if trading_mode == TradingMode.FUTURES:
|
||||
return (
|
||||
(
|
||||
(wallet_balance + cross_vars + maintenance_amt) -
|
||||
(side_1 * position * open_rate)
|
||||
) / (
|
||||
(position * mm_ratio) - (side_1 * position)
|
||||
)
|
||||
)
|
||||
|
||||
raise OperationalException(
|
||||
f"Binance does not support {collateral.value} Mode {trading_mode.value} trading ")
|
||||
|
@@ -2003,8 +2003,11 @@ class Exchange:
|
||||
except ccxt.BaseError as e:
|
||||
raise OperationalException(e) from e
|
||||
|
||||
@retrier
|
||||
def get_mm_amt_rate(self, pair: str, amount: float):
|
||||
def get_maintenance_ratio_and_amt(
|
||||
self,
|
||||
pair: Optional[str],
|
||||
nominal_value: Optional[float]
|
||||
):
|
||||
'''
|
||||
:return: The maintenance amount, and maintenance margin rate
|
||||
'''
|
||||
|
@@ -616,7 +616,7 @@ class FreqtradeBot(LoggingMixin):
|
||||
# open_rate=open_rate,
|
||||
# is_short=is_short
|
||||
# )
|
||||
maintenance_amt, mm_rate = self.exchange.get_mm_amt_rate(pair, amount)
|
||||
mm_ratio, maintenance_amt = self.exchange.get_maintenance_ratio_and_amt(pair, amount)
|
||||
|
||||
if self.collateral_type == Collateral.ISOLATED:
|
||||
if self.config['dry_run']:
|
||||
@@ -630,9 +630,9 @@ class FreqtradeBot(LoggingMixin):
|
||||
mm_ex_1=0.0,
|
||||
upnl_ex_1=0.0,
|
||||
position=amount * open_rate,
|
||||
wallet_balance=amount/leverage, # TODO-lev: Is this correct?
|
||||
wallet_balance=amount/leverage, # TODO: Update for cross
|
||||
maintenance_amt=maintenance_amt,
|
||||
mm_rate=mm_rate,
|
||||
mm_ratio=mm_ratio,
|
||||
)
|
||||
else:
|
||||
isolated_liq = self.exchange.get_liquidation_price(pair)
|
||||
|
@@ -35,12 +35,12 @@ def liquidation_price(
|
||||
'''
|
||||
wallet_balance
|
||||
In Cross margin mode, WB is crossWalletBalance
|
||||
In Isolated margin mode, WB is isolatedWalletBalance of the isolated position,
|
||||
In Isolated margin mode, WB is isolatedWalletBalance of the isolated position,
|
||||
TMM=0, UPNL=0, substitute the position quantity, MMR, cum into the formula to calculate.
|
||||
Under the cross margin mode, the same ticker/symbol,
|
||||
both long and short position share the same liquidation price except in the isolated mode.
|
||||
Under the isolated mode, each isolated position will have different liquidation prices depending
|
||||
on the margin allocated to the positions.
|
||||
both long and short position share the same liquidation price except in the isolated mode.
|
||||
Under the isolated mode, each isolated position will have different liquidation prices
|
||||
depending on the margin allocated to the positions.
|
||||
position
|
||||
Absolute value of position size (in base currency)
|
||||
|
||||
|
@@ -368,7 +368,7 @@ class LocalTrade():
|
||||
wallet_balance: Optional[float] = None,
|
||||
current_price: Optional[float] = None,
|
||||
maintenance_amt: Optional[float] = None,
|
||||
mm_rate: Optional[float] = None,
|
||||
mm_ratio: Optional[float] = None,
|
||||
):
|
||||
"""
|
||||
Method you should use to set self.liquidation price.
|
||||
@@ -380,6 +380,16 @@ class LocalTrade():
|
||||
"wallet balance must be passed to LocalTrade.set_isolated_liq when param"
|
||||
"isolated_liq is None"
|
||||
)
|
||||
if (
|
||||
mm_ratio is None or
|
||||
wallet_balance is None or
|
||||
current_price is None or
|
||||
maintenance_amt is None
|
||||
):
|
||||
raise OperationalException(
|
||||
'mm_ratio, wallet_balance, current_price and maintenance_amt '
|
||||
'required in set_isolated_liq when isolated_liq is None'
|
||||
)
|
||||
isolated_liq = liquidation_price(
|
||||
exchange_name=self.exchange,
|
||||
open_rate=self.open_rate,
|
||||
@@ -390,9 +400,9 @@ class LocalTrade():
|
||||
mm_ex_1=0.0,
|
||||
upnl_ex_1=0.0,
|
||||
position=self.amount * current_price,
|
||||
wallet_balance=self.amount / self.leverage, # TODO-lev: Is this correct?
|
||||
wallet_balance=self.amount / self.leverage, # TODO: Update for cross
|
||||
maintenance_amt=maintenance_amt,
|
||||
mm_rate=mm_rate,
|
||||
mm_ratio=mm_ratio,
|
||||
|
||||
)
|
||||
if isolated_liq is None:
|
||||
|
Reference in New Issue
Block a user