forked from kodorvan/stcs
Fixed liq price calculation
This commit is contained in:
@@ -134,7 +134,11 @@ def check_limit_price(limit_price, min_price, max_price) -> str | None:
|
||||
|
||||
|
||||
async def get_liquidation_price(
|
||||
tg_id: int, symbol: str, entry_price: float, order_quantity: float
|
||||
tg_id: int,
|
||||
symbol: str,
|
||||
entry_price: float,
|
||||
order_quantity: float,
|
||||
leverage: float
|
||||
) -> tuple[float, float]:
|
||||
"""
|
||||
Function to get liquidation price
|
||||
@@ -144,15 +148,15 @@ async def get_liquidation_price(
|
||||
get_risk_info = client.get_risk_limit(category="linear", symbol=symbol)
|
||||
risk_list = get_risk_info.get("result", {}).get("list", [])
|
||||
risk_level = risk_list[0] if risk_list else {}
|
||||
maint_margin_rate = safe_float(risk_level.get("maintenanceMargin"))
|
||||
maintenance_margin_rate = safe_float(risk_level.get("maintenanceMargin"))
|
||||
maint_margin_deduction = safe_float(risk_level.get("initialMargin"))
|
||||
max_leverage = safe_float(risk_level.get("maxLeverage"))
|
||||
position_value = order_quantity * entry_price
|
||||
initial_margin = position_value / max_leverage
|
||||
maint_margin = (position_value * maint_margin_rate) - maint_margin_deduction
|
||||
maint_margin = (position_value * maintenance_margin_rate) - maint_margin_deduction
|
||||
|
||||
liq_price_long = entry_price - (initial_margin - maint_margin) / order_quantity
|
||||
liq_price_short = entry_price + (initial_margin - maint_margin) / order_quantity
|
||||
liq_price_long = entry_price * (1 - 1 / leverage + maintenance_margin_rate)
|
||||
liq_price_short = entry_price * (1 + 1 / leverage - maintenance_margin_rate)
|
||||
|
||||
liq_price = liq_price_long, liq_price_short
|
||||
|
||||
@@ -163,7 +167,7 @@ async def get_liquidation_price(
|
||||
|
||||
|
||||
async def calculate_total_budget(
|
||||
quantity, martingale_factor, max_steps, commission_fee_percent
|
||||
quantity, martingale_factor, max_steps, commission_fee_percent
|
||||
) -> float:
|
||||
"""
|
||||
Calculate the total budget for a series of trading steps.
|
||||
@@ -179,7 +183,7 @@ async def calculate_total_budget(
|
||||
"""
|
||||
total = 0
|
||||
for step in range(max_steps):
|
||||
set_quantity = quantity * (martingale_factor**step)
|
||||
set_quantity = quantity * (martingale_factor ** step)
|
||||
if commission_fee_percent == 0:
|
||||
# Commission fee is not added to the position size
|
||||
r_quantity = set_quantity
|
||||
|
Reference in New Issue
Block a user