2
0
forked from kodorvan/stcs
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)

View File

@@ -10,6 +10,7 @@ from app.services.Bybit.functions.Futures import (close_user_trade, set_take_pro
get_active_positions_by_symbol, get_active_orders_by_symbol,
get_active_positions, get_active_orders, cancel_all_tp_sl_orders,
open_position, close_trade_after_delay, safe_float,
calculate_total_budget, get_bybit_client,
)
from app.services.Bybit.functions.balance import get_balance
import app.telegram.Keyboards.inline_keyboards as inline_markup
@@ -17,6 +18,7 @@ import app.telegram.Keyboards.inline_keyboards as inline_markup
import app.telegram.database.requests as rq
from aiogram.types import Message, CallbackQuery
from app.services.Bybit.functions.price_symbol import get_price
import app.services.Bybit.functions.balance as balance_g
from app.states.States import (state_update_entry_type, state_update_symbol, state_limit_price,
SetTP_SL_State, CloseTradeTimerState)
@@ -196,11 +198,18 @@ async def start_trading_process(callback: CallbackQuery) -> None:
tg_id = callback.from_user.id
message = callback.message
data_main_stgs = await rq.get_user_main_settings(tg_id)
data_risk_stgs = await rq.get_user_risk_management_settings(tg_id)
client = await get_bybit_client(tg_id)
symbol = await rq.get_symbol(tg_id)
margin_mode = data_main_stgs.get('margin_type', 'Isolated')
trading_mode = data_main_stgs.get('trading_mode')
starting_quantity = safe_float(data_main_stgs.get('starting_quantity'))
switch_state = data_main_stgs.get("switch_state", "По направлению")
martingale_factor = safe_float(data_main_stgs.get('martingale_factor'))
max_martingale_steps = int(data_main_stgs.get("maximal_quantity", 0))
commission_fee = data_risk_stgs.get("commission_fee")
fee_info = client.get_fee_rates(category='linear', symbol=symbol)
if trading_mode == 'Switch':
if switch_state == "По направлению":
@@ -221,7 +230,33 @@ async def start_trading_process(callback: CallbackQuery) -> None:
reply_markup=inline_markup.back_to_main)
return
if commission_fee == "Да":
commission_fee_percent = safe_float(fee_info['result']['list'][0]['takerFeeRate'])
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,
)
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
await message.answer("Начинаю торговлю с использованием текущих настроек...")
await rq.update_trigger(tg_id=tg_id, trigger="Автоматический")
timer_data = await rq.get_user_timer(tg_id)
if isinstance(timer_data, dict):
@@ -259,6 +294,7 @@ async def cancel_start_trading(callback: CallbackQuery):
pass
user_trade_tasks.pop(tg_id, None)
await rq.update_user_timer(tg_id, minutes=0)
await rq.update_trigger(tg_id, "Ручной")
await callback.message.answer("Запуск торговли отменён.", reply_markup=inline_markup.back_to_main)
await callback.message.edit_reply_markup(reply_markup=None)
else:
@@ -505,9 +541,13 @@ async def stop_immediately(callback: CallbackQuery):
Останавливает торговлю немедленно.
"""
tg_id = callback.from_user.id
symbol = await rq.get_symbol(tg_id)
await close_user_trade(tg_id, symbol)
await rq.update_trigger(tg_id, "Ручной")
await callback.message.answer("Автоматическая торговля остановлена.", reply_markup=inline_markup.back_to_main)
await rq.update_martingale_step(tg_id, 1)
await callback.message.answer("Торговля остановлена.", reply_markup=inline_markup.back_to_main)
await callback.answer()
@@ -543,8 +583,13 @@ async def process_stop_delay(message: Message, state: FSMContext):
await message.answer(f"Торговля будет остановлена через {delay_minutes} минут.",
reply_markup=inline_markup.back_to_main)
await asyncio.sleep(delay_seconds)
symbol = await rq.get_symbol(tg_id)
await close_user_trade(tg_id, symbol)
await rq.update_trigger(tg_id, "Ручной")
await message.answer("Автоматическая торговля остановлена.", reply_markup=inline_markup.back_to_main)
await rq.update_martingale_step(tg_id, 1)
await message.answer("Торговля остановлена.", reply_markup=inline_markup.back_to_main)
await state.clear()
@@ -559,4 +604,4 @@ async def cancel(callback: CallbackQuery, state: FSMContext) -> None:
await callback.message.answer("Отменено!", reply_markup=inline_markup.back_to_main)
await callback.answer()
except Exception as e:
logger.error("Ошибка при обработке отмены: %s", e)
logger.error("Ошибка при обработке отмены: %s", e)