Fixed
This commit is contained in:
@@ -22,7 +22,7 @@ async def get_active_positions(tg_id: int) -> list | None:
|
||||
]
|
||||
if active_symbols:
|
||||
logger.info("Active positions for user: %s", tg_id)
|
||||
return active_symbols
|
||||
return positions
|
||||
else:
|
||||
logger.warning("No active positions found for user: %s", tg_id)
|
||||
return ["No active positions found"]
|
||||
@@ -50,7 +50,7 @@ async def get_active_positions_by_symbol(tg_id: int, symbol: str) -> dict | None
|
||||
positions = response.get("result", {}).get("list", [])
|
||||
if positions:
|
||||
logger.info("Active positions for user: %s", tg_id)
|
||||
return positions[0]
|
||||
return positions
|
||||
else:
|
||||
logger.warning("No active positions found for user: %s", tg_id)
|
||||
return None
|
||||
@@ -85,7 +85,7 @@ async def get_active_orders(tg_id: int) -> list | None:
|
||||
]
|
||||
if active_orders:
|
||||
logger.info("Active orders for user: %s", tg_id)
|
||||
return active_orders
|
||||
return orders
|
||||
else:
|
||||
logger.warning("No active orders found for user: %s", tg_id)
|
||||
return ["No active orders found"]
|
||||
@@ -115,7 +115,7 @@ async def get_active_orders_by_symbol(tg_id: int, symbol: str) -> dict | None:
|
||||
orders = response.get("result", {}).get("list", [])
|
||||
if orders:
|
||||
logger.info("Active orders for user: %s", tg_id)
|
||||
return orders[0]
|
||||
return orders
|
||||
else:
|
||||
logger.warning("No active orders found for user: %s", tg_id)
|
||||
return None
|
||||
|
@@ -169,6 +169,7 @@ async def trading_cycle(tg_id: int, symbol: str, reverse_side: str, size: str) -
|
||||
leverage=leverage,
|
||||
)
|
||||
|
||||
|
||||
if reverse_side == "Buy":
|
||||
real_side = "Sell"
|
||||
else:
|
||||
@@ -219,7 +220,7 @@ async def trading_cycle(tg_id: int, symbol: str, reverse_side: str, size: str) -
|
||||
leverage_to_sell=leverage_to_sell,
|
||||
order_type=order_type,
|
||||
conditional_order_type=conditional_order_type,
|
||||
order_quantity=current_step,
|
||||
order_quantity=next_quantity,
|
||||
limit_price=limit_price,
|
||||
trigger_price=trigger_price,
|
||||
martingale_factor=martingale_factor,
|
||||
@@ -258,9 +259,9 @@ async def open_positions(
|
||||
trigger_price: float,
|
||||
trade_mode: str,
|
||||
margin_type: str,
|
||||
leverage: float,
|
||||
leverage_to_buy: float,
|
||||
leverage_to_sell: float,
|
||||
leverage: str,
|
||||
leverage_to_buy: str,
|
||||
leverage_to_sell: str,
|
||||
take_profit_percent: float,
|
||||
stop_loss_percent: float,
|
||||
max_risk_percent: float,
|
||||
@@ -317,19 +318,22 @@ async def open_positions(
|
||||
|
||||
if trade_mode == "Both_Sides":
|
||||
po_position_idx = 1 if side == "Buy" else 2
|
||||
leverage = safe_float(
|
||||
if margin_type == "ISOLATED_MARGIN":
|
||||
get_leverage = safe_float(
|
||||
leverage_to_buy if side == "Buy" else leverage_to_sell
|
||||
)
|
||||
else:
|
||||
get_leverage = safe_float(leverage)
|
||||
else:
|
||||
po_position_idx = 0
|
||||
leverage = safe_float(leverage)
|
||||
get_leverage = safe_float(leverage)
|
||||
|
||||
potential_loss = (
|
||||
safe_float(order_quantity)
|
||||
* safe_float(price_for_calc)
|
||||
* (stop_loss_percent / 100)
|
||||
)
|
||||
adjusted_loss = potential_loss / leverage
|
||||
adjusted_loss = potential_loss / get_leverage
|
||||
allowed_loss = safe_float(user_balance) * (max_risk_percent / 100)
|
||||
|
||||
if adjusted_loss > allowed_loss:
|
||||
@@ -356,10 +360,11 @@ async def open_positions(
|
||||
entry_price=price_for_calc,
|
||||
symbol=symbol,
|
||||
order_quantity=order_quantity,
|
||||
leverage=get_leverage,
|
||||
)
|
||||
|
||||
if liq_long > 0 or liq_short > 0 and price_for_calc > 0:
|
||||
if side.lower() == "buy":
|
||||
if (liq_long > 0 or liq_short > 0) and price_for_calc > 0:
|
||||
if side == "Buy":
|
||||
base_tp = price_for_calc + (price_for_calc - liq_long)
|
||||
take_profit_price = base_tp + total_commission
|
||||
else:
|
||||
@@ -371,7 +376,7 @@ async def open_positions(
|
||||
|
||||
stop_loss_price = None
|
||||
else:
|
||||
if side.lower() == "buy":
|
||||
if side == "Buy":
|
||||
take_profit_price = price_for_calc * tp_multiplier
|
||||
stop_loss_price = price_for_calc * (1 - stop_loss_percent / 100)
|
||||
else:
|
||||
@@ -383,6 +388,7 @@ async def open_positions(
|
||||
take_profit_price = max(take_profit_price, 0)
|
||||
stop_loss_price = max(stop_loss_price, 0)
|
||||
|
||||
|
||||
# Place order
|
||||
order_params = {
|
||||
"category": "linear",
|
||||
|
@@ -43,6 +43,12 @@ async def set_switch_position_mode(tg_id: int, symbol: str, mode: int) -> str |
|
||||
tg_id,
|
||||
)
|
||||
return "You have an existing position, so position mode cannot be switched"
|
||||
if str(e).startswith("Open orders exist, so you cannot change position mode"):
|
||||
logger.debug(
|
||||
"Open orders exist, so you cannot change position mode for user: %s",
|
||||
tg_id,
|
||||
)
|
||||
return "Open orders exist, so you cannot change position mode"
|
||||
else:
|
||||
logger.error("Error connecting to Bybit for user %s: %s", tg_id, e)
|
||||
return False
|
||||
|
@@ -1,7 +1,6 @@
|
||||
import logging.config
|
||||
|
||||
from app.bybit import get_bybit_client
|
||||
from app.bybit.get_functions.get_positions import get_active_positions_by_symbol
|
||||
from app.bybit.logger_bybit.logger_bybit import LOGGING_CONFIG
|
||||
|
||||
logging.config.dictConfig(LOGGING_CONFIG)
|
||||
@@ -13,6 +12,7 @@ async def set_tp_sl_for_position(
|
||||
symbol: str,
|
||||
take_profit_price: float,
|
||||
stop_loss_price: float,
|
||||
position_idx: int,
|
||||
) -> bool:
|
||||
"""
|
||||
Set take profit and stop loss for a symbol.
|
||||
@@ -20,14 +20,11 @@ async def set_tp_sl_for_position(
|
||||
:param symbol: Symbol to set take profit and stop loss for
|
||||
:param take_profit_price: Take profit price
|
||||
:param stop_loss_price: Stop loss price
|
||||
:param position_idx: Position index
|
||||
:return: bool
|
||||
"""
|
||||
try:
|
||||
client = await get_bybit_client(tg_id)
|
||||
user_positions = await get_active_positions_by_symbol(
|
||||
tg_id=tg_id, symbol=symbol
|
||||
)
|
||||
position_idx = user_positions.get("positionIdx")
|
||||
resp = client.set_trading_stop(
|
||||
category="linear",
|
||||
symbol=symbol,
|
||||
|
@@ -106,12 +106,27 @@ class TelegramMessageHandler:
|
||||
if side == "Buy"
|
||||
else "Продажа" if side == "Sell" else "Нет данных"
|
||||
)
|
||||
|
||||
if safe_float(closed_size) == 0:
|
||||
await rq.set_fee_user_auto_trading(
|
||||
tg_id=tg_id, symbol=symbol, side=side, fee=safe_float(exec_fee)
|
||||
)
|
||||
|
||||
user_auto_trading = await rq.get_user_auto_trading(
|
||||
tg_id=tg_id, symbol=symbol
|
||||
)
|
||||
|
||||
if user_auto_trading is not None and user_auto_trading.fee is not None:
|
||||
fee = user_auto_trading.fee
|
||||
else:
|
||||
fee = 0
|
||||
|
||||
exec_pnl = format_value(execution.get("execPnl"))
|
||||
risk_management_data = await rq.get_user_risk_management(tg_id=tg_id)
|
||||
commission_fee = risk_management_data.commission_fee
|
||||
|
||||
if commission_fee == "Yes_commission_fee":
|
||||
total_pnl = safe_float(exec_pnl) - safe_float(exec_fee)
|
||||
total_pnl = safe_float(exec_pnl) - safe_float(exec_fee) - fee
|
||||
else:
|
||||
total_pnl = safe_float(exec_pnl)
|
||||
|
||||
@@ -138,13 +153,12 @@ class TelegramMessageHandler:
|
||||
chat_id=tg_id, text=text, reply_markup=kbi.profile_bybit
|
||||
)
|
||||
|
||||
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_symbols = await rq.get_user_deal_by_symbol(tg_id=tg_id, symbol=symbol)
|
||||
user_symbols = (
|
||||
user_auto_trading.symbol if user_auto_trading else None
|
||||
)
|
||||
|
||||
if (
|
||||
auto_trading
|
||||
@@ -152,20 +166,22 @@ class TelegramMessageHandler:
|
||||
and user_symbols is not None
|
||||
):
|
||||
if safe_float(total_pnl) > 0:
|
||||
await rq.set_auto_trading(
|
||||
tg_id=tg_id, symbol=symbol, auto_trading=False
|
||||
)
|
||||
profit_text = "📈 Прибыль достигнута\n"
|
||||
await self.telegram_bot.send_message(
|
||||
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(tg_id=tg_id, symbol=symbol, auto_trading=False, side=r_side)
|
||||
else:
|
||||
open_order_text = "\n❗️ Сделка закрылась в минус, открываю новую сделку с увеличенной ставкой.\n"
|
||||
await self.telegram_bot.send_message(
|
||||
chat_id=tg_id, text=open_order_text
|
||||
)
|
||||
res = await trading_cycle(
|
||||
tg_id=tg_id, symbol=symbol, reverse_side=side
|
||||
tg_id=tg_id, symbol=symbol, reverse_side=side, size=closed_size
|
||||
)
|
||||
|
||||
if res == "OK":
|
||||
@@ -180,8 +196,12 @@ class TelegramMessageHandler:
|
||||
error_text = errors.get(
|
||||
res, "❗️ Не удалось открыть новую сделку"
|
||||
)
|
||||
if side == "Buy":
|
||||
r_side = "Sell"
|
||||
else:
|
||||
r_side = "Buy"
|
||||
await rq.set_auto_trading(
|
||||
tg_id=tg_id, symbol=symbol, auto_trading=False
|
||||
tg_id=tg_id, symbol=symbol, auto_trading=False, side=r_side
|
||||
)
|
||||
await self.telegram_bot.send_message(
|
||||
chat_id=tg_id,
|
||||
|
@@ -64,7 +64,6 @@ class WebSocketBot:
|
||||
channel_type="private",
|
||||
api_key=api_key,
|
||||
api_secret=api_secret,
|
||||
restart_on_error=True,
|
||||
)
|
||||
|
||||
self.user_sockets[tg_id] = self.ws_private
|
||||
|
Reference in New Issue
Block a user