This commit is contained in:
algizn97
2025-09-17 20:51:30 +05:00
parent 17dba19078
commit 4c9901c14a
8 changed files with 104 additions and 112 deletions

View File

@@ -172,7 +172,7 @@ def parse_pnl_from_msg(msg) -> float:
return 0.0
async def calculate_total_budget(starting_quantity, martingale_factor, max_steps, commission_fee_percent, leverage, current_price):
async def calculate_total_budget(starting_quantity, martingale_factor, max_steps, commission_fee_percent):
"""
Вычисляет общий бюджет серии ставок с учётом цены пары, комиссии и кредитного плеча.
@@ -189,22 +189,16 @@ async def calculate_total_budget(starting_quantity, martingale_factor, max_steps
"""
total = 0
for step in range(max_steps):
quantity = starting_quantity * (martingale_factor ** step) # размер ставки на текущем шаге в USDT
base_quantity = starting_quantity * (martingale_factor ** step)
if commission_fee_percent == 0:
# Комиссия уже включена в сумму ставки, поэтому реальный размер позиции меньше
quantity = base_quantity / (1 + commission_fee_percent)
else:
# Комиссию добавляем сверху
quantity = base_quantity * (1 + commission_fee_percent)
# Переводим ставку из USDT в количество актива по текущей цене
quantity_in_asset = quantity / current_price
# Учитываем комиссию за вход и выход (умножаем на 2)
quantity_with_fee = quantity * (1 + 2 * commission_fee_percent / 100)
# Учитываем кредитное плечо - реальные собственные вложения меньше
effective_quantity = quantity_with_fee / leverage
total += effective_quantity
# Возвращаем бюджет в USDT
total_usdt = total * current_price
return total_usdt
total += quantity
return total
async def handle_execution_message(message, msg):
@@ -248,12 +242,12 @@ async def handle_execution_message(message, msg):
await rq.set_last_series_info(tg_id, last_side="Sell")
if trigger == "Автоматический" and closed_size > 0:
if pnl < 0:
if trading_mode == 'Switch':
side = data_main_stgs.get("last_side")
else:
side = "Buy" if trading_mode == "Long" else "Sell"
if trading_mode == 'Switch':
side = data_main_stgs.get("last_side")
else:
side = "Buy" if trading_mode == "Long" else "Sell"
if pnl < 0:
current_martingale = await rq.get_martingale_step(tg_id)
current_martingale_step = int(current_martingale)
@@ -262,6 +256,7 @@ async def handle_execution_message(message, msg):
float(martingale_factor) ** current_martingale_step
)
await rq.update_martingale_step(tg_id, current_martingale)
await rq.update_starting_quantity(tg_id=tg_id, num=next_quantity)
await message.answer(
f"❗️ Сделка закрылась в минус, открываю новую сделку с увеличенной ставкой.\n"
)
@@ -276,8 +271,11 @@ async def handle_execution_message(message, msg):
elif pnl > 0:
await rq.update_martingale_step(tg_id, 0)
num = data_main_stgs.get("base_quantity")
await rq.update_starting_quantity(tg_id=tg_id, num=num)
await message.answer(
"❗️ Прибыль достигнута, шаг мартингейла сброшен."
"❗️ Прибыль достигнута, шаг мартингейла сброшен. "
"Возврат к начальной ставке."
)
@@ -348,8 +346,6 @@ async def open_position(
max_risk_percent = safe_float(data_risk_stgs.get("max_risk_deal"))
loss_profit = safe_float(data_risk_stgs.get("price_loss"))
commission_fee = data_risk_stgs.get("commission_fee")
starting_quantity = safe_float(data_main_stgs.get('starting_quantity'))
martingale_factor = safe_float(data_main_stgs.get('martingale_factor'))
fee_info = client.get_fee_rates(category='linear', symbol=symbol)
instruments_resp = client.get_instruments_info(category="linear", symbol=symbol)
instrument = instruments_resp.get("result", {}).get("list", [])
@@ -359,33 +355,19 @@ async def open_position(
else:
commission_fee_percent = 0.0
total_budget = await calculate_total_budget(
starting_quantity=starting_quantity,
martingale_factor=martingale_factor,
max_steps=max_martingale_steps,
commission_fee_percent=commission_fee_percent,
leverage=leverage,
current_price=entry_price,
)
if commission_fee_percent > 0:
# Добавляем к тейк-профиту процент комиссии
tp_multiplier = 1 + (loss_profit / 100) + commission_fee_percent
else:
tp_multiplier = 1 + (loss_profit / 100)
balance = await balance_g.get_balance(tg_id, message)
if safe_float(balance) < total_budget:
logger.error(
f"Недостаточно средств для серии из {max_martingale_steps} шагов с текущими параметрами. "
f"Требуемый бюджет: {total_budget:.2f} USDT, доступно: {balance} USDT."
)
await message.answer(
f"Недостаточно средств для серии из {max_martingale_steps} шагов с текущими параметрами. "
f"Требуемый бюджет: {total_budget:.2f} USDT, доступно: {balance} USDT.",
reply_markup=inline_markup.back_to_main,
)
return
if order_type == "Limit" and limit_price:
price_for_calc = limit_price
else:
price_for_calc = entry_price
balance = await balance_g.get_balance(tg_id, message)
potential_loss = safe_float(quantity) * price_for_calc * (loss_profit / 100)
adjusted_loss = potential_loss / leverage
allowed_loss = safe_float(balance) * (max_risk_percent / 100)
@@ -465,6 +447,8 @@ async def open_position(
timeInForce="GTC",
orderLinkId=f"deal_{symbol}_{int(time.time())}",
)
if response.get("retCode", -1) == 0:
return True
if response.get("retCode", -1) != 0:
logger.error(f"Ошибка открытия ордера: {response}")
await message.answer(
@@ -480,9 +464,11 @@ async def open_position(
if liq_price > 0 and avg_price > 0:
if side.lower() == "buy":
take_profit_price = avg_price + (avg_price - liq_price)
base_tp = avg_price + (avg_price - liq_price)
take_profit_price = base_tp * (1 + commission_fee_percent)
else:
take_profit_price = avg_price - (liq_price - avg_price)
base_tp = avg_price - (liq_price - avg_price)
take_profit_price = base_tp * (1 - commission_fee_percent)
take_profit_price = max(take_profit_price, 0)
@@ -531,10 +517,10 @@ async def open_position(
base_price = limit_price
if side.lower() == "buy":
take_profit_price = base_price * (1 + loss_profit / 100)
take_profit_price = base_price * tp_multiplier
stop_loss_price = base_price * (1 - loss_profit / 100)
else:
take_profit_price = base_price * (1 - loss_profit / 100)
take_profit_price = base_price * (1 - (loss_profit / 100) - (commission_fee_percent))
stop_loss_price = base_price * (1 + loss_profit / 100)
take_profit_price = max(take_profit_price, 0)