Added the ability to summarize all commissions within a series.Minor bugs have been fixed
This commit is contained in:
@@ -4,7 +4,7 @@ import app.telegram.keyboards.inline as kbi
|
|||||||
import database.request as rq
|
import database.request as rq
|
||||||
from app.bybit.logger_bybit.logger_bybit import LOGGING_CONFIG
|
from app.bybit.logger_bybit.logger_bybit import LOGGING_CONFIG
|
||||||
from app.bybit.open_positions import trading_cycle
|
from app.bybit.open_positions import trading_cycle
|
||||||
from app.helper_functions import format_value, safe_float, safe_int
|
from app.helper_functions import format_value, safe_float
|
||||||
|
|
||||||
logging.config.dictConfig(LOGGING_CONFIG)
|
logging.config.dictConfig(LOGGING_CONFIG)
|
||||||
logger = logging.getLogger("telegram_message_handler")
|
logger = logging.getLogger("telegram_message_handler")
|
||||||
@@ -22,12 +22,6 @@ class TelegramMessageHandler:
|
|||||||
order_data = message.get("data", [{}])[0]
|
order_data = message.get("data", [{}])[0]
|
||||||
symbol = format_value(order_data.get("symbol"))
|
symbol = format_value(order_data.get("symbol"))
|
||||||
qty = format_value(order_data.get("qty"))
|
qty = format_value(order_data.get("qty"))
|
||||||
order_type = format_value(order_data.get("orderType"))
|
|
||||||
order_type_rus = (
|
|
||||||
"Рыночный"
|
|
||||||
if order_type == "Market"
|
|
||||||
else "Лимитный" if order_type == "Limit" else "Нет данных"
|
|
||||||
)
|
|
||||||
side = format_value(order_data.get("side"))
|
side = format_value(order_data.get("side"))
|
||||||
side_rus = (
|
side_rus = (
|
||||||
"Покупка"
|
"Покупка"
|
||||||
@@ -40,39 +34,16 @@ class TelegramMessageHandler:
|
|||||||
take_profit = format_value(order_data.get("takeProfit"))
|
take_profit = format_value(order_data.get("takeProfit"))
|
||||||
stop_loss = format_value(order_data.get("stopLoss"))
|
stop_loss = format_value(order_data.get("stopLoss"))
|
||||||
|
|
||||||
position_idx = safe_int(order_data.get("positionIdx"))
|
|
||||||
position_idx_rus = (
|
|
||||||
"Односторонний"
|
|
||||||
if position_idx == 0
|
|
||||||
else (
|
|
||||||
"Покупка в режиме хеджирования"
|
|
||||||
if position_idx == 1
|
|
||||||
else (
|
|
||||||
"Продажа в режиме хеджирования"
|
|
||||||
if position_idx == 2
|
|
||||||
else "Нет данных"
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
status_map = {
|
status_map = {
|
||||||
"New": "Ордер создан",
|
|
||||||
"Cancelled": "Ордер отменен",
|
|
||||||
"Deactivated": "Ордер деактивирован",
|
|
||||||
"Untriggered": "Условный ордер выставлен",
|
"Untriggered": "Условный ордер выставлен",
|
||||||
}
|
}
|
||||||
|
|
||||||
if order_status == "Filled" or order_status not in status_map:
|
if order_status == "Filled" or order_status not in status_map:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
status_text = status_map[order_status]
|
|
||||||
|
|
||||||
text = (
|
text = (
|
||||||
f"{status_text}:\n"
|
|
||||||
f"Торговая пара: {symbol}\n"
|
f"Торговая пара: {symbol}\n"
|
||||||
f"Режим позиции: {position_idx_rus}\n"
|
|
||||||
f"Количество: {qty}\n"
|
f"Количество: {qty}\n"
|
||||||
f"Тип ордера: {order_type_rus}\n"
|
|
||||||
f"Движение: {side_rus}\n"
|
f"Движение: {side_rus}\n"
|
||||||
)
|
)
|
||||||
if price and price != "0":
|
if price and price != "0":
|
||||||
@@ -97,13 +68,6 @@ class TelegramMessageHandler:
|
|||||||
symbol = format_value(execution.get("symbol"))
|
symbol = format_value(execution.get("symbol"))
|
||||||
exec_price = format_value(execution.get("execPrice"))
|
exec_price = format_value(execution.get("execPrice"))
|
||||||
exec_fee = format_value(execution.get("execFee"))
|
exec_fee = format_value(execution.get("execFee"))
|
||||||
exec_qty = format_value(execution.get("execQty"))
|
|
||||||
order_type = format_value(execution.get("orderType"))
|
|
||||||
order_type_rus = (
|
|
||||||
"Рыночный"
|
|
||||||
if order_type == "Market"
|
|
||||||
else "Лимитный" if order_type == "Limit" else "Нет данных"
|
|
||||||
)
|
|
||||||
side = format_value(execution.get("side"))
|
side = format_value(execution.get("side"))
|
||||||
side_rus = (
|
side_rus = (
|
||||||
"Покупка"
|
"Покупка"
|
||||||
@@ -113,14 +77,17 @@ class TelegramMessageHandler:
|
|||||||
|
|
||||||
if safe_float(closed_size) == 0:
|
if safe_float(closed_size) == 0:
|
||||||
await rq.set_fee_user_auto_trading(
|
await rq.set_fee_user_auto_trading(
|
||||||
tg_id=tg_id, symbol=symbol, side=side, fee=safe_float(exec_fee)
|
tg_id=tg_id, symbol=symbol, fee=safe_float(exec_fee)
|
||||||
)
|
)
|
||||||
if side == "Buy":
|
|
||||||
res_side = "Sell"
|
|
||||||
else:
|
|
||||||
res_side = "Buy"
|
|
||||||
user_auto_trading = await rq.get_user_auto_trading(
|
user_auto_trading = await rq.get_user_auto_trading(
|
||||||
tg_id=tg_id, symbol=symbol, side=res_side
|
tg_id=tg_id, symbol=symbol
|
||||||
|
)
|
||||||
|
|
||||||
|
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:
|
if user_auto_trading is not None and user_auto_trading.fee is not None:
|
||||||
@@ -142,13 +109,15 @@ class TelegramMessageHandler:
|
|||||||
)
|
)
|
||||||
text = f"{header}\n" f"Торговая пара: {symbol}\n"
|
text = f"{header}\n" f"Торговая пара: {symbol}\n"
|
||||||
|
|
||||||
if safe_float(closed_size) > 0:
|
user_deals_data = await rq.get_user_deal_by_symbol(
|
||||||
text += f"Количество закрытых сделок: {closed_size}\n"
|
tg_id=tg_id, symbol=symbol
|
||||||
|
)
|
||||||
|
exec_bet = user_deals_data.order_quantity
|
||||||
|
base_quantity = user_deals_data.base_quantity
|
||||||
|
|
||||||
text += (
|
text += (
|
||||||
f"Цена исполнения: {exec_price}\n"
|
f"Цена исполнения: {exec_price}\n"
|
||||||
f"Количество исполненных сделок: {exec_qty}\n"
|
f"Текущая ставка: {exec_bet}\n"
|
||||||
f"Тип ордера: {order_type_rus}\n"
|
|
||||||
f"Движение: {side_rus}\n"
|
f"Движение: {side_rus}\n"
|
||||||
f"Комиссия за сделку: {exec_fee}\n"
|
f"Комиссия за сделку: {exec_fee}\n"
|
||||||
)
|
)
|
||||||
@@ -175,27 +144,26 @@ class TelegramMessageHandler:
|
|||||||
await self.telegram_bot.send_message(
|
await self.telegram_bot.send_message(
|
||||||
chat_id=tg_id, text=profit_text, reply_markup=kbi.profile_bybit
|
chat_id=tg_id, text=profit_text, reply_markup=kbi.profile_bybit
|
||||||
)
|
)
|
||||||
if side == "Buy":
|
|
||||||
r_side = "Sell"
|
|
||||||
else:
|
|
||||||
r_side = "Buy"
|
|
||||||
await rq.set_auto_trading(
|
await rq.set_auto_trading(
|
||||||
tg_id=tg_id, symbol=symbol, auto_trading=False, side=r_side
|
tg_id=tg_id, symbol=symbol, auto_trading=False
|
||||||
)
|
)
|
||||||
user_deals_data = await rq.get_user_deal_by_symbol(
|
|
||||||
tg_id=tg_id, symbol=symbol
|
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
|
||||||
|
)
|
||||||
|
await rq.set_order_quantity(
|
||||||
|
tg_id=message.from_user.id, order_quantity=base_quantity
|
||||||
)
|
)
|
||||||
if user_deals_data and user_deals_data.switch_side_mode:
|
|
||||||
await rq.set_auto_trading(
|
|
||||||
tg_id=tg_id, symbol=symbol, auto_trading=False, side=side
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
open_order_text = "\n❗️ Сделка закрылась в минус, открываю новую сделку с увеличенной ставкой.\n"
|
open_order_text = "\n❗️ Сделка закрылась в минус, открываю новую сделку с увеличенной ставкой.\n"
|
||||||
await self.telegram_bot.send_message(
|
await self.telegram_bot.send_message(
|
||||||
chat_id=tg_id, text=open_order_text
|
chat_id=tg_id, text=open_order_text
|
||||||
)
|
)
|
||||||
res = await trading_cycle(
|
res = await trading_cycle(
|
||||||
tg_id=tg_id, symbol=symbol, reverse_side=side, size=closed_size
|
tg_id=tg_id, symbol=symbol, reverse_side=side
|
||||||
)
|
)
|
||||||
|
|
||||||
if res == "OK":
|
if res == "OK":
|
||||||
@@ -211,27 +179,21 @@ class TelegramMessageHandler:
|
|||||||
error_text = errors.get(
|
error_text = errors.get(
|
||||||
res, "❗️ Не удалось открыть новую сделку"
|
res, "❗️ Не удалось открыть новую сделку"
|
||||||
)
|
)
|
||||||
if side == "Buy":
|
|
||||||
r_side = "Sell"
|
|
||||||
else:
|
|
||||||
r_side = "Buy"
|
|
||||||
await rq.set_auto_trading(
|
await rq.set_auto_trading(
|
||||||
tg_id=tg_id, symbol=symbol, auto_trading=False, side=r_side
|
tg_id=tg_id, symbol=symbol, auto_trading=False
|
||||||
)
|
)
|
||||||
user_deals_data = await rq.get_user_deal_by_symbol(
|
|
||||||
tg_id=tg_id, symbol=symbol
|
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
|
||||||
)
|
)
|
||||||
if user_deals_data and user_deals_data.switch_side_mode:
|
|
||||||
await rq.set_auto_trading(
|
|
||||||
tg_id=tg_id,
|
|
||||||
symbol=symbol,
|
|
||||||
auto_trading=False,
|
|
||||||
side=side,
|
|
||||||
)
|
|
||||||
await self.telegram_bot.send_message(
|
await self.telegram_bot.send_message(
|
||||||
chat_id=tg_id,
|
chat_id=tg_id,
|
||||||
text=error_text,
|
text=error_text,
|
||||||
reply_markup=kbi.profile_bybit,
|
reply_markup=kbi.profile_bybit,
|
||||||
)
|
)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error("Error in telegram_message_handler: %s", e)
|
logger.error("Error in telegram_message_handler: %s", e)
|
||||||
|
Reference in New Issue
Block a user