forked from kodorvan/stcs
Compare commits
14 Commits
258ed970f1
...
stable
Author | SHA1 | Date | |
---|---|---|---|
9fcd92cc72 | |||
![]() |
e61b7334a4 | ||
97a199f31e | |||
![]() |
5ad69f3f6d | ||
![]() |
abad01352a | ||
![]() |
720b30d681 | ||
![]() |
3616e2cbd3 | ||
![]() |
7d108337fa | ||
![]() |
0f6e6a2168 | ||
951bc15957 | |||
5937058899 | |||
f0732607e2 | |||
56af1d8f3b | |||
9f069df68a |
@@ -10,6 +10,7 @@ from app.bybit.get_functions.get_tickers import get_tickers
|
||||
from app.bybit.logger_bybit.logger_bybit import LOGGING_CONFIG
|
||||
from app.bybit.set_functions.set_leverage import set_leverage
|
||||
from app.bybit.set_functions.set_margin_mode import set_margin_mode
|
||||
from app.bybit.set_functions.set_switch_position_mode import set_switch_position_mode
|
||||
from app.helper_functions import get_liquidation_price, safe_float
|
||||
|
||||
logging.config.dictConfig(LOGGING_CONFIG)
|
||||
@@ -24,11 +25,9 @@ async def start_trading_cycle(
|
||||
:param tg_id: Telegram user ID
|
||||
"""
|
||||
try:
|
||||
client = await get_bybit_client(tg_id=tg_id)
|
||||
symbol = await rq.get_user_symbol(tg_id=tg_id)
|
||||
additional_data = await rq.get_user_additional_settings(tg_id=tg_id)
|
||||
risk_management_data = await rq.get_user_risk_management(tg_id=tg_id)
|
||||
commission_fee = risk_management_data.commission_fee
|
||||
user_deals_data = await rq.get_user_deal_by_symbol(
|
||||
tg_id=tg_id, symbol=symbol
|
||||
)
|
||||
@@ -42,6 +41,7 @@ async def start_trading_cycle(
|
||||
max_bets_in_series = additional_data.max_bets_in_series
|
||||
take_profit_percent = risk_management_data.take_profit_percent
|
||||
stop_loss_percent = risk_management_data.stop_loss_percent
|
||||
total_commission = 0
|
||||
|
||||
get_side = "Buy"
|
||||
|
||||
@@ -62,34 +62,10 @@ async def start_trading_cycle(
|
||||
else:
|
||||
side = "Sell"
|
||||
|
||||
# Get fee rates
|
||||
fee_info = client.get_fee_rates(category="linear", symbol=symbol)
|
||||
|
||||
# Check if commission fee is enabled
|
||||
commission_fee_percent = 0.0
|
||||
if commission_fee == "Yes_commission_fee":
|
||||
commission_fee_percent = safe_float(
|
||||
fee_info["result"]["list"][0]["takerFeeRate"]
|
||||
)
|
||||
|
||||
get_ticker = await get_tickers(tg_id, symbol=symbol)
|
||||
price_symbol = safe_float(get_ticker.get("lastPrice")) or 0
|
||||
instruments_info = await get_instruments_info(tg_id=tg_id, symbol=symbol)
|
||||
qty_step_str = instruments_info.get("lotSizeFilter").get("qtyStep")
|
||||
qty_step = safe_float(qty_step_str)
|
||||
qty = safe_float(order_quantity) / safe_float(price_symbol)
|
||||
decimals = abs(int(round(math.log10(qty_step))))
|
||||
qty_formatted = math.floor(qty / qty_step) * qty_step
|
||||
qty_formatted = round(qty_formatted, decimals)
|
||||
|
||||
if trigger_price > 0:
|
||||
po_trigger_price = str(trigger_price)
|
||||
else:
|
||||
po_trigger_price = None
|
||||
|
||||
price_for_cals = trigger_price if po_trigger_price is not None else price_symbol
|
||||
total_commission = price_for_cals * qty_formatted * commission_fee_percent
|
||||
|
||||
await set_switch_position_mode(
|
||||
tg_id=tg_id,
|
||||
symbol=symbol,
|
||||
mode=0)
|
||||
await set_margin_mode(tg_id=tg_id, margin_mode=margin_type)
|
||||
await set_leverage(
|
||||
tg_id=tg_id,
|
||||
@@ -264,7 +240,7 @@ async def open_positions(
|
||||
instruments_info = await get_instruments_info(tg_id=tg_id, symbol=symbol)
|
||||
qty_step_str = instruments_info.get("lotSizeFilter").get("qtyStep")
|
||||
qty_step = safe_float(qty_step_str)
|
||||
qty = safe_float(order_quantity) / safe_float(price_symbol)
|
||||
qty = (safe_float(order_quantity) * safe_float(leverage)) / safe_float(price_symbol)
|
||||
decimals = abs(int(round(math.log10(qty_step))))
|
||||
qty_formatted = math.floor(qty / qty_step) * qty_step
|
||||
qty_formatted = round(qty_formatted, decimals)
|
||||
@@ -280,6 +256,9 @@ async def open_positions(
|
||||
|
||||
price_for_cals = trigger_price if po_trigger_price is not None else price_symbol
|
||||
|
||||
if qty_formatted <= 0:
|
||||
return "Order does not meet minimum order value"
|
||||
|
||||
if margin_type == "ISOLATED_MARGIN":
|
||||
liq_long, liq_short = await get_liquidation_price(
|
||||
tg_id=tg_id,
|
||||
@@ -291,10 +270,10 @@ async def open_positions(
|
||||
if (liq_long > 0 or liq_short > 0) and price_for_cals > 0:
|
||||
if side == "Buy":
|
||||
base_tp = price_for_cals + (price_for_cals - liq_long)
|
||||
take_profit_price = base_tp + commission_fee_percent
|
||||
take_profit_price = base_tp + commission_fee_percent / qty_formatted
|
||||
else:
|
||||
base_tp = price_for_cals - (liq_short - price_for_cals)
|
||||
take_profit_price = base_tp - commission_fee_percent
|
||||
take_profit_price = base_tp - commission_fee_percent / qty_formatted
|
||||
take_profit_price = max(take_profit_price, 0)
|
||||
else:
|
||||
take_profit_price = None
|
||||
@@ -302,11 +281,11 @@ async def open_positions(
|
||||
stop_loss_price = None
|
||||
else:
|
||||
if side == "Buy":
|
||||
take_profit_price = price_for_cals * (1 + take_profit_percent / 100) + commission_fee_percent
|
||||
stop_loss_price = price_for_cals * (1 - stop_loss_percent / 100) - commission_fee_percent
|
||||
take_profit_price = price_for_cals * (1 + take_profit_percent / 100) + commission_fee_percent / qty_formatted
|
||||
stop_loss_price = price_for_cals * (1 - stop_loss_percent / 100)
|
||||
else:
|
||||
take_profit_price = price_for_cals * (1 - take_profit_percent / 100) - commission_fee_percent
|
||||
stop_loss_price = price_for_cals * (1 + stop_loss_percent / 100) + commission_fee_percent
|
||||
take_profit_price = price_for_cals * (1 - take_profit_percent / 100) - commission_fee_percent / qty_formatted
|
||||
stop_loss_price = price_for_cals * (1 + stop_loss_percent / 100)
|
||||
|
||||
take_profit_price = max(take_profit_price, 0)
|
||||
stop_loss_price = max(stop_loss_price, 0)
|
||||
@@ -356,5 +335,5 @@ async def open_positions(
|
||||
return "InvalidRequestError"
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Error opening position for user %s: %s", tg_id, e)
|
||||
logger.error("Error opening position for user %s: %s", tg_id, e, exc_info=True)
|
||||
return None
|
||||
|
@@ -41,11 +41,26 @@ class TelegramMessageHandler:
|
||||
if order_status == "Filled" or order_status not in status_map:
|
||||
return None
|
||||
|
||||
user_auto_trading = await rq.get_user_auto_trading(
|
||||
tg_id=tg_id, symbol=symbol
|
||||
)
|
||||
auto_trading = (
|
||||
user_auto_trading.auto_trading if user_auto_trading else False
|
||||
)
|
||||
user_deals_data = await rq.get_user_deal_by_symbol(
|
||||
tg_id=tg_id, symbol=symbol
|
||||
)
|
||||
|
||||
text = (
|
||||
f"Торговая пара: {symbol}\n"
|
||||
f"Количество: {qty}\n"
|
||||
f"Движение: {side_rus}\n"
|
||||
)
|
||||
|
||||
if user_deals_data is not None and auto_trading:
|
||||
text += f"Текущая ставка: {user_deals_data.order_quantity}\n"
|
||||
else:
|
||||
text += f"Количество: {qty}\n"
|
||||
|
||||
if price and price != "0":
|
||||
text += f"Цена: {price}\n"
|
||||
if take_profit and take_profit != "Нет данных":
|
||||
@@ -67,13 +82,21 @@ class TelegramMessageHandler:
|
||||
closed_size = format_value(execution.get("closedSize"))
|
||||
symbol = format_value(execution.get("symbol"))
|
||||
exec_price = format_value(execution.get("execPrice"))
|
||||
exec_fee = format_value(execution.get("execFee"))
|
||||
exec_qty = format_value(execution.get("execQty"))
|
||||
exec_fees = format_value(execution.get("execFee"))
|
||||
fee_rate = format_value(execution.get("feeRate"))
|
||||
side = format_value(execution.get("side"))
|
||||
side_rus = (
|
||||
"Покупка"
|
||||
if side == "Buy"
|
||||
else "Продажа" if side == "Sell" else "Нет данных"
|
||||
)
|
||||
if safe_float(exec_fees) == 0:
|
||||
exec_fee = safe_float(exec_price) * safe_float(exec_qty) * safe_float(
|
||||
fee_rate
|
||||
)
|
||||
else:
|
||||
exec_fee = safe_float(exec_fees)
|
||||
|
||||
if safe_float(closed_size) == 0:
|
||||
await rq.set_fee_user_auto_trading(
|
||||
@@ -86,9 +109,7 @@ class TelegramMessageHandler:
|
||||
|
||||
get_total_fee = user_auto_trading.total_fee
|
||||
total_fee = safe_float(exec_fee) + safe_float(get_total_fee)
|
||||
await rq.set_total_fee_user_auto_trading(
|
||||
tg_id=tg_id, symbol=symbol, total_fee=total_fee
|
||||
)
|
||||
|
||||
|
||||
if user_auto_trading is not None and user_auto_trading.fee is not None:
|
||||
fee = user_auto_trading.fee
|
||||
@@ -109,17 +130,24 @@ class TelegramMessageHandler:
|
||||
)
|
||||
text = f"{header}\n" f"Торговая пара: {symbol}\n"
|
||||
|
||||
auto_trading = (
|
||||
user_auto_trading.auto_trading if user_auto_trading else False
|
||||
)
|
||||
user_deals_data = await rq.get_user_deal_by_symbol(
|
||||
tg_id=tg_id, symbol=symbol
|
||||
)
|
||||
exec_bet = user_deals_data.order_quantity
|
||||
base_quantity = user_deals_data.base_quantity
|
||||
if user_deals_data is not None and auto_trading:
|
||||
await rq.set_total_fee_user_auto_trading(
|
||||
tg_id=tg_id, symbol=symbol, total_fee=total_fee
|
||||
)
|
||||
text += f"Текущая ставка: {user_deals_data.order_quantity} USDT\n"
|
||||
else:
|
||||
text += f"Количество: {exec_qty}\n"
|
||||
|
||||
text += (
|
||||
f"Цена исполнения: {exec_price}\n"
|
||||
f"Текущая ставка: {exec_bet}\n"
|
||||
f"Движение: {side_rus}\n"
|
||||
f"Комиссия за сделку: {exec_fee}\n"
|
||||
f"Комиссия: {exec_fee:.8f}\n"
|
||||
)
|
||||
|
||||
if safe_float(closed_size) > 0:
|
||||
@@ -129,9 +157,6 @@ class TelegramMessageHandler:
|
||||
chat_id=tg_id, text=text, reply_markup=kbi.profile_bybit
|
||||
)
|
||||
|
||||
auto_trading = (
|
||||
user_auto_trading.auto_trading if user_auto_trading else False
|
||||
)
|
||||
user_symbols = user_auto_trading.symbol if user_auto_trading else None
|
||||
|
||||
if (
|
||||
@@ -154,6 +179,7 @@ class TelegramMessageHandler:
|
||||
await rq.set_fee_user_auto_trading(
|
||||
tg_id=tg_id, symbol=symbol, fee=0
|
||||
)
|
||||
base_quantity = user_deals_data.base_quantity
|
||||
await rq.set_order_quantity(
|
||||
tg_id=tg_id, order_quantity=base_quantity
|
||||
)
|
||||
@@ -174,7 +200,7 @@ class TelegramMessageHandler:
|
||||
"Risk is too high for this trade": "❗️ Риск сделки слишком высок для продолжения",
|
||||
"ab not enough for new order": "❗️ Недостаточно средств для продолжения торговли",
|
||||
"InvalidRequestError": "❗️ Недостаточно средств для размещения нового ордера с заданным количеством и плечом.",
|
||||
"The number of contracts exceeds maximum limit allowed": "❗️ Количество контрактов превышает допустимое максимальное количество контрактов",
|
||||
"The number of contracts exceeds maximum limit allowed": "❗️ Превышен максимальный лимит ставки",
|
||||
}
|
||||
error_text = errors.get(
|
||||
res, "❗️ Не удалось открыть новую сделку"
|
||||
|
@@ -7,6 +7,7 @@ from aiogram.types import CallbackQuery, Message
|
||||
import app.telegram.keyboards.inline as kbi
|
||||
import database.request as rq
|
||||
from app.bybit.get_functions.get_instruments_info import get_instruments_info
|
||||
from app.bybit.get_functions.get_positions import get_active_positions_by_symbol, get_active_orders_by_symbol
|
||||
from app.bybit.set_functions.set_leverage import set_leverage
|
||||
from app.bybit.set_functions.set_margin_mode import set_margin_mode
|
||||
from app.helper_functions import is_int, is_number, safe_float
|
||||
@@ -211,6 +212,31 @@ async def settings_for_margin_type(
|
||||
"""
|
||||
try:
|
||||
await state.clear()
|
||||
symbol = await rq.get_user_symbol(tg_id=callback_query.from_user.id)
|
||||
deals = await get_active_positions_by_symbol(
|
||||
tg_id=callback_query.from_user.id, symbol=symbol
|
||||
)
|
||||
position = next((d for d in deals if d.get("symbol") == symbol), None)
|
||||
|
||||
if position:
|
||||
size = position.get("size", 0)
|
||||
else:
|
||||
size = 0
|
||||
|
||||
if safe_float(size) > 0:
|
||||
await callback_query.answer(
|
||||
text="У вас есть активная позиция по текущей паре",
|
||||
)
|
||||
return
|
||||
|
||||
orders = await get_active_orders_by_symbol(
|
||||
tg_id=callback_query.from_user.id, symbol=symbol)
|
||||
|
||||
if orders is not None:
|
||||
await callback_query.answer(
|
||||
text="У вас есть активный ордер по текущей паре",
|
||||
)
|
||||
return
|
||||
await callback_query.message.edit_text(
|
||||
text="Выберите тип маржи:\n\n"
|
||||
"Примечание: Если у вас есть открытые позиции, то маржа примениться ко всем позициям",
|
||||
|
@@ -123,8 +123,8 @@ async def risk_management(callback_query: CallbackQuery, state: FSMContext) -> N
|
||||
|
||||
await callback_query.message.edit_text(
|
||||
text=f"Риск-менеджмент:\n\n"
|
||||
f"- Процент изменения цены для фиксации прибыли: {take_profit_percent}%\n"
|
||||
f"- Процент изменения цены для фиксации убытка: {stop_loss_percent}%\n\n"
|
||||
f"- Процент изменения цены для фиксации прибыли: {take_profit_percent:.2f}%\n"
|
||||
f"- Процент изменения цены для фиксации убытка: {stop_loss_percent:.2f}%\n\n"
|
||||
f"- Комиссия биржи для расчета прибыли: {commission_fee_rus}\n\n",
|
||||
reply_markup=kbi.risk_management,
|
||||
)
|
||||
|
@@ -7,7 +7,7 @@ from aiogram.types import CallbackQuery
|
||||
|
||||
import app.telegram.keyboards.inline as kbi
|
||||
import database.request as rq
|
||||
from app.bybit.get_functions.get_positions import get_active_positions_by_symbol
|
||||
from app.bybit.get_functions.get_positions import get_active_positions_by_symbol, get_active_orders_by_symbol
|
||||
from app.bybit.open_positions import start_trading_cycle
|
||||
from app.helper_functions import safe_float
|
||||
from app.telegram.tasks.tasks import (
|
||||
@@ -33,6 +33,7 @@ async def start_trading(callback_query: CallbackQuery, state: FSMContext) -> Non
|
||||
"""
|
||||
try:
|
||||
await state.clear()
|
||||
tg_id = callback_query.from_user.id
|
||||
symbol = await rq.get_user_symbol(tg_id=callback_query.from_user.id)
|
||||
deals = await get_active_positions_by_symbol(
|
||||
tg_id=callback_query.from_user.id, symbol=symbol
|
||||
@@ -46,7 +47,16 @@ async def start_trading(callback_query: CallbackQuery, state: FSMContext) -> Non
|
||||
|
||||
if safe_float(size) > 0:
|
||||
await callback_query.answer(
|
||||
text="У вас есть активная позиция",
|
||||
text="У вас есть активная позиция по текущей паре",
|
||||
)
|
||||
return
|
||||
|
||||
orders = await get_active_orders_by_symbol(
|
||||
tg_id=callback_query.from_user.id, symbol=symbol)
|
||||
|
||||
if orders is not None:
|
||||
await callback_query.answer(
|
||||
text="У вас есть активный ордер по текущей паре",
|
||||
)
|
||||
return
|
||||
|
||||
@@ -73,22 +83,29 @@ async def start_trading(callback_query: CallbackQuery, state: FSMContext) -> Non
|
||||
symbol=symbol,
|
||||
auto_trading=True,
|
||||
)
|
||||
await rq.set_total_fee_user_auto_trading(
|
||||
tg_id=tg_id, symbol=symbol, total_fee=0
|
||||
)
|
||||
await rq.set_fee_user_auto_trading(
|
||||
tg_id=tg_id, symbol=symbol, fee=0
|
||||
)
|
||||
res = await start_trading_cycle(
|
||||
tg_id=callback_query.from_user.id,
|
||||
)
|
||||
|
||||
error_messages = {
|
||||
"Limit price is out min price": "Цена лимитного ордера меньше минимального",
|
||||
"Limit price is out max price": "Цена лимитного ордера больше максимального",
|
||||
"Limit price is out min price": "Цена лимитного ордера меньше допустимого",
|
||||
"Limit price is out max price": "Цена лимитного ордера больше допустимого",
|
||||
"Risk is too high for this trade": "Риск сделки превышает допустимый убыток",
|
||||
"estimated will trigger liq": "Лимитный ордер может вызвать мгновенную ликвидацию. Проверьте параметры ордера.",
|
||||
"ab not enough for new order": "Недостаточно средств для создания нового ордера",
|
||||
"InvalidRequestError": "Произошла ошибка при запуске торговли.",
|
||||
"Order does not meet minimum order value": "Сумма ордера не достаточна для запуска торговли",
|
||||
"position idx not match position mode": "Ошибка режима позиции для данного инструмента",
|
||||
"Qty invalid": "Некорректное значение ордера для данного инструмента",
|
||||
"The number of contracts exceeds maximum limit allowed": "️️Количество контрактов превышает допустимое максимальное количество контрактов",
|
||||
"The number of contracts exceeds minimum limit allowed": "️️Количество контрактов превышает допустимое минимальное количество контрактов",
|
||||
"Order does not meet minimum order value": "Сумма ставки меньше допустимого для запуска торговли. "
|
||||
"Увеличьте ставку, чтобы запустить торговлю",
|
||||
"position idx not match position mode": "Измените режим позиции, чтобы запустить торговлю",
|
||||
"Qty invalid": "Некорректное значение ставки для данного инструмента",
|
||||
"The number of contracts exceeds maximum limit allowed": "️️Превышен максимальный лимит ставки",
|
||||
"The number of contracts exceeds minimum limit allowed": "️️Лимит ставки меньше минимально допустимого",
|
||||
}
|
||||
|
||||
if res == "OK":
|
||||
@@ -112,7 +129,7 @@ async def start_trading(callback_query: CallbackQuery, state: FSMContext) -> Non
|
||||
except Exception as e:
|
||||
await callback_query.answer(text="Произошла ошибка при запуске торговли")
|
||||
logger.error(
|
||||
"Error processing command long for user %s: %s",
|
||||
"Error processing command start_trading for user %s: %s",
|
||||
callback_query.from_user.id,
|
||||
e,
|
||||
)
|
||||
|
Reference in New Issue
Block a user