forked from kodorvan/stcs
Compare commits
42 Commits
72ed35ccf8
...
stable
| Author | SHA1 | Date | |
|---|---|---|---|
| f0732607e2 | |||
|
|
458b34fcec | ||
| 56af1d8f3b | |||
|
|
4a7577b977 | ||
| 9f069df68a | |||
|
|
6e0a170f4b | ||
|
|
c7b4a08a6a | ||
|
|
d0971f59b4 | ||
| b92376d2da | |||
| 630f2002d3 | |||
| 0784cbb54a | |||
| eeb7f81440 | |||
| b03d05bb75 | |||
| e0e4ad5d4b | |||
| fab8ff5040 | |||
| 8071f8c896 | |||
| 3db001bd19 | |||
| 99c59be9ed | |||
| 37b7b6effd | |||
| ee285523f2 | |||
| b426eb2136 | |||
| 2df3b8b40d | |||
| 8c08451d82 | |||
| d81a47b669 | |||
| 2cdfba3537 | |||
| c89c2ad803 | |||
| 3986989dbd | |||
| c0e40dc205 | |||
| 6c6f0dbb7b | |||
| 44c4fde036 | |||
| 21a93d47d4 | |||
| 3f43d42651 | |||
| aab05994ce | |||
| a58ebe6a46 | |||
| 1ec1f1784d | |||
| 7901af86af | |||
| fedfa00c10 | |||
|
|
fc8ab19ae9 | ||
| 898ff91392 | |||
| 2047dd5ac6 | |||
| fec367cc1d | |||
| aebcc9dff2 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -146,6 +146,8 @@ myenv
|
||||
ENV/
|
||||
env.bak/
|
||||
venv.bak/
|
||||
/logger_helper/loggers
|
||||
/app/bybit/logger_bybit/loggers
|
||||
# Spyder project settings
|
||||
.spyderproject
|
||||
.spyproject
|
||||
|
||||
@@ -55,7 +55,12 @@ cp .env.sample .env
|
||||
nvim .env
|
||||
```
|
||||
|
||||
5. Запустите бота:
|
||||
5. Для применения миграций выполните команду:
|
||||
```bash
|
||||
alembic upgrade head
|
||||
```
|
||||
|
||||
6. Запустите бота:
|
||||
|
||||
```bash
|
||||
python run.py
|
||||
|
||||
40
alembic/versions/07020b2808d3_fixed_switch_side_type.py
Normal file
40
alembic/versions/07020b2808d3_fixed_switch_side_type.py
Normal file
@@ -0,0 +1,40 @@
|
||||
"""fixed switch_side type
|
||||
|
||||
Revision ID: 07020b2808d3
|
||||
Revises: c710f4e2259c
|
||||
Create Date: 2025-10-09 14:36:07.393387
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '07020b2808d3'
|
||||
down_revision: Union[str, Sequence[str], None] = 'c710f4e2259c'
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Upgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.alter_column('user_additional_settings', 'switch_side',
|
||||
existing_type=sa.BOOLEAN(),
|
||||
type_=sa.String(),
|
||||
existing_nullable=False,
|
||||
existing_server_default=sa.text('false'))
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.alter_column('user_additional_settings', 'switch_side',
|
||||
existing_type=sa.String(),
|
||||
type_=sa.BOOLEAN(),
|
||||
existing_nullable=False,
|
||||
existing_server_default=sa.text('false'))
|
||||
# ### end Alembic commands ###
|
||||
34
alembic/versions/baf03ce269e0_added_base_quantity.py
Normal file
34
alembic/versions/baf03ce269e0_added_base_quantity.py
Normal file
@@ -0,0 +1,34 @@
|
||||
"""added base_quantity
|
||||
|
||||
Revision ID: baf03ce269e0
|
||||
Revises: 07020b2808d3
|
||||
Create Date: 2025-10-09 16:49:52.979556
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = 'baf03ce269e0'
|
||||
down_revision: Union[str, Sequence[str], None] = '07020b2808d3'
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Upgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.add_column('user_deals', sa.Column('base_quantity', sa.Float(), nullable=True))
|
||||
op.drop_column('user_deals', 'trading_type')
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.add_column('user_deals', sa.Column('trading_type', sa.VARCHAR(), autoincrement=False, nullable=True))
|
||||
op.drop_column('user_deals', 'base_quantity')
|
||||
# ### end Alembic commands ###
|
||||
@@ -16,7 +16,9 @@ logging.config.dictConfig(LOGGING_CONFIG)
|
||||
logger = logging.getLogger("open_positions")
|
||||
|
||||
|
||||
async def start_trading_cycle(tg_id: int) -> str | None:
|
||||
async def start_trading_cycle(
|
||||
tg_id: int
|
||||
) -> str | None:
|
||||
"""
|
||||
Start trading cycle
|
||||
:param tg_id: Telegram user ID
|
||||
@@ -27,7 +29,9 @@ async def start_trading_cycle(tg_id: int) -> str | None:
|
||||
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)
|
||||
user_deals_data = await rq.get_user_deal_by_symbol(
|
||||
tg_id=tg_id, symbol=symbol
|
||||
)
|
||||
trade_mode = additional_data.trade_mode
|
||||
switch_side = additional_data.switch_side
|
||||
margin_type = additional_data.margin_type
|
||||
@@ -103,7 +107,7 @@ async def start_trading_cycle(tg_id: int) -> str | None:
|
||||
leverage=leverage,
|
||||
take_profit_percent=take_profit_percent,
|
||||
stop_loss_percent=stop_loss_percent,
|
||||
commission_fee_percent=total_commission,
|
||||
commission_fee_percent=total_commission
|
||||
)
|
||||
|
||||
if res == "OK":
|
||||
@@ -121,7 +125,7 @@ async def start_trading_cycle(tg_id: int) -> str | None:
|
||||
max_bets_in_series=max_bets_in_series,
|
||||
take_profit_percent=take_profit_percent,
|
||||
stop_loss_percent=stop_loss_percent,
|
||||
base_quantity=order_quantity,
|
||||
base_quantity=order_quantity
|
||||
)
|
||||
return "OK"
|
||||
return (
|
||||
@@ -138,7 +142,7 @@ async def start_trading_cycle(tg_id: int) -> str | None:
|
||||
"position idx not match position mode",
|
||||
"Qty invalid",
|
||||
"The number of contracts exceeds maximum limit allowed",
|
||||
"The number of contracts exceeds minimum limit allowed",
|
||||
"The number of contracts exceeds minimum limit allowed"
|
||||
}
|
||||
else None
|
||||
)
|
||||
@@ -148,12 +152,12 @@ async def start_trading_cycle(tg_id: int) -> str | None:
|
||||
return None
|
||||
|
||||
|
||||
async def trading_cycle(tg_id: int, symbol: str, reverse_side: str) -> str | None:
|
||||
async def trading_cycle(
|
||||
tg_id: int, symbol: str, reverse_side: str
|
||||
) -> str | None:
|
||||
try:
|
||||
user_deals_data = await rq.get_user_deal_by_symbol(tg_id=tg_id, symbol=symbol)
|
||||
user_auto_trading_data = await rq.get_user_auto_trading(
|
||||
tg_id=tg_id, symbol=symbol
|
||||
)
|
||||
user_auto_trading_data = await rq.get_user_auto_trading(tg_id=tg_id, symbol=symbol)
|
||||
total_fee = user_auto_trading_data.total_fee
|
||||
trade_mode = user_deals_data.trade_mode
|
||||
margin_type = user_deals_data.margin_type
|
||||
@@ -184,7 +188,9 @@ async def trading_cycle(tg_id: int, symbol: str, reverse_side: str) -> str | Non
|
||||
if trade_mode == "Switch":
|
||||
side = "Sell" if real_side == "Buy" else "Buy"
|
||||
|
||||
next_quantity = safe_float(order_quantity) * (safe_float(martingale_factor))
|
||||
next_quantity = safe_float(order_quantity) * (
|
||||
safe_float(martingale_factor)
|
||||
)
|
||||
current_step += 1
|
||||
|
||||
if max_bets_in_series < current_step:
|
||||
@@ -200,7 +206,7 @@ async def trading_cycle(tg_id: int, symbol: str, reverse_side: str) -> str | Non
|
||||
leverage=leverage,
|
||||
take_profit_percent=take_profit_percent,
|
||||
stop_loss_percent=stop_loss_percent,
|
||||
commission_fee_percent=total_fee,
|
||||
commission_fee_percent=total_fee
|
||||
)
|
||||
|
||||
if res == "OK":
|
||||
@@ -218,7 +224,7 @@ async def trading_cycle(tg_id: int, symbol: str, reverse_side: str) -> str | Non
|
||||
max_bets_in_series=max_bets_in_series,
|
||||
take_profit_percent=take_profit_percent,
|
||||
stop_loss_percent=stop_loss_percent,
|
||||
base_quantity=base_quantity,
|
||||
base_quantity=base_quantity
|
||||
)
|
||||
return "OK"
|
||||
|
||||
@@ -249,7 +255,7 @@ async def open_positions(
|
||||
leverage: str,
|
||||
take_profit_percent: float,
|
||||
stop_loss_percent: float,
|
||||
commission_fee_percent: float,
|
||||
commission_fee_percent: float
|
||||
) -> str | None:
|
||||
try:
|
||||
client = await get_bybit_client(tg_id=tg_id)
|
||||
|
||||
@@ -46,7 +46,9 @@ class WebSocketBot:
|
||||
self.user_sockets.clear()
|
||||
self.user_messages.clear()
|
||||
self.user_keys.clear()
|
||||
logger.info("Closed old websocket for user %s due to key change", tg_id)
|
||||
logger.info(
|
||||
"Closed old websocket for user %s due to key change", tg_id
|
||||
)
|
||||
|
||||
success = await self.try_connect_user(api_key, api_secret, tg_id)
|
||||
if success:
|
||||
|
||||
@@ -158,7 +158,7 @@ async def get_liquidation_price(
|
||||
|
||||
|
||||
async def calculate_total_budget(
|
||||
quantity, martingale_factor, max_steps, commission_fee_percent
|
||||
quantity, martingale_factor, max_steps
|
||||
) -> float:
|
||||
"""
|
||||
Calculate the total budget for a series of trading steps.
|
||||
@@ -167,20 +167,15 @@ async def calculate_total_budget(
|
||||
quantity (float): The initial quantity of the asset.
|
||||
martingale_factor (float): The factor by which the quantity is multiplied for each step.
|
||||
max_steps (int): The maximum number of trading steps.
|
||||
commission_fee_percent (float): The commission fee percentage.
|
||||
|
||||
Returns:
|
||||
float: The total budget for the series of trading steps.
|
||||
"""
|
||||
total = 0
|
||||
for step in range(max_steps):
|
||||
set_quantity = quantity * (martingale_factor ** step)
|
||||
if commission_fee_percent == 0:
|
||||
# Commission fee is not added to the position size
|
||||
set_quantity = quantity * (martingale_factor**step)
|
||||
|
||||
r_quantity = set_quantity
|
||||
else:
|
||||
# Commission fee is added to the position size
|
||||
r_quantity = set_quantity * (1 + 2 * commission_fee_percent)
|
||||
|
||||
total += r_quantity
|
||||
return total
|
||||
|
||||
@@ -6,10 +6,11 @@ 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_tickers import get_tickers
|
||||
from app.bybit.get_functions.get_instruments_info import get_instruments_info
|
||||
from app.bybit.profile_bybit import user_profile_bybit
|
||||
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 safe_float
|
||||
from app.telegram.states.states import ChangingTheSymbolState
|
||||
@@ -98,9 +99,7 @@ async def set_symbol(message: Message, state: FSMContext) -> None:
|
||||
)
|
||||
return
|
||||
|
||||
instruments_info = await get_instruments_info(
|
||||
tg_id=message.from_user.id, symbol=symbol
|
||||
)
|
||||
instruments_info = await get_instruments_info(tg_id=message.from_user.id, symbol=symbol)
|
||||
max_leverage = instruments_info.get("leverageFilter").get("maxLeverage")
|
||||
req = await rq.set_user_symbol(tg_id=message.from_user.id, symbol=symbol)
|
||||
|
||||
@@ -124,8 +123,7 @@ async def set_symbol(message: Message, state: FSMContext) -> None:
|
||||
await rq.set_leverage(tg_id=message.from_user.id, leverage=str(max_leverage))
|
||||
risk_percent = 100 / safe_float(max_leverage)
|
||||
await rq.set_stop_loss_percent(
|
||||
tg_id=message.from_user.id, stop_loss_percent=risk_percent
|
||||
)
|
||||
tg_id=message.from_user.id, stop_loss_percent=risk_percent)
|
||||
await rq.set_trigger_price(tg_id=message.from_user.id, trigger_price=0)
|
||||
await rq.set_order_quantity(tg_id=message.from_user.id, order_quantity=1.0)
|
||||
|
||||
|
||||
@@ -142,9 +142,7 @@ async def switch_side_start(callback_query: CallbackQuery, state: FSMContext) ->
|
||||
)
|
||||
|
||||
|
||||
@router_additional_settings.callback_query(
|
||||
lambda c: c.data == "switch_direction" or c.data == "switch_opposite"
|
||||
)
|
||||
@router_additional_settings.callback_query(lambda c: c.data == "switch_direction" or c.data == "switch_opposite")
|
||||
async def switch_side_handler(callback_query: CallbackQuery, state: FSMContext) -> None:
|
||||
"""
|
||||
Handles callback queries related to switch side selection.
|
||||
@@ -216,7 +214,7 @@ async def settings_for_margin_type(
|
||||
await callback_query.message.edit_text(
|
||||
text="Выберите тип маржи:\n\n"
|
||||
"Примечание: Если у вас есть открытые позиции, то маржа примениться ко всем позициям",
|
||||
reply_markup=kbi.margin_type,
|
||||
reply_markup=kbi.margin_type
|
||||
)
|
||||
logger.debug(
|
||||
"Command margin_type processed successfully for user: %s",
|
||||
@@ -555,8 +553,7 @@ async def set_leverage_handler(message: Message, state: FSMContext) -> None:
|
||||
)
|
||||
risk_percent = 100 / safe_float(leverage_float)
|
||||
await rq.set_stop_loss_percent(
|
||||
tg_id=message.from_user.id, stop_loss_percent=risk_percent
|
||||
)
|
||||
tg_id=message.from_user.id, stop_loss_percent=risk_percent)
|
||||
logger.info(
|
||||
"User %s set leverage: %s", message.from_user.id, leverage_float
|
||||
)
|
||||
@@ -770,14 +767,9 @@ async def set_martingale_factor(message: Message, state: FSMContext) -> None:
|
||||
martingale_factor_value_float = safe_float(martingale_factor_value)
|
||||
|
||||
if martingale_factor_value_float < 0.1 or martingale_factor_value_float > 10:
|
||||
await message.answer(
|
||||
text="Ошибка: коэффициент мартингейла должен быть в диапазоне от 0.1 до 10"
|
||||
)
|
||||
logger.debug(
|
||||
"User %s input invalid (not in range 0.1 to 10): %s",
|
||||
message.from_user.id,
|
||||
martingale_factor_value_float,
|
||||
)
|
||||
await message.answer(text="Ошибка: коэффициент мартингейла должен быть в диапазоне от 0.1 до 10")
|
||||
logger.debug("User %s input invalid (not in range 0.1 to 10): %s", message.from_user.id,
|
||||
martingale_factor_value_float)
|
||||
return
|
||||
|
||||
req = await rq.set_martingale_factor(
|
||||
@@ -886,10 +878,7 @@ async def set_max_bets_in_series(message: Message, state: FSMContext) -> None:
|
||||
)
|
||||
return
|
||||
|
||||
if (
|
||||
safe_float(max_bets_in_series_value) < 1
|
||||
or safe_float(max_bets_in_series_value) > 100
|
||||
):
|
||||
if safe_float(max_bets_in_series_value) < 1 or safe_float(max_bets_in_series_value) > 100:
|
||||
await message.answer(
|
||||
"Ошибка: число должно быть в диапазоне от 1 до 100.",
|
||||
reply_markup=kbi.back_to_additional_settings,
|
||||
|
||||
@@ -98,10 +98,7 @@ async def set_take_profit_percent(message: Message, state: FSMContext) -> None:
|
||||
)
|
||||
return
|
||||
|
||||
if (
|
||||
safe_float(take_profit_percent_value) < 1
|
||||
or safe_float(take_profit_percent_value) > 100
|
||||
):
|
||||
if safe_float(take_profit_percent_value) < 1 or safe_float(take_profit_percent_value) > 100:
|
||||
await message.answer(
|
||||
text="Ошибка: введите число от 1 до 100.",
|
||||
reply_markup=kbi.back_to_risk_management,
|
||||
@@ -222,10 +219,7 @@ async def set_stop_loss_percent(message: Message, state: FSMContext) -> None:
|
||||
)
|
||||
return
|
||||
|
||||
if (
|
||||
safe_float(stop_loss_percent_value) < 1
|
||||
or safe_float(stop_loss_percent_value) > 100
|
||||
):
|
||||
if safe_float(stop_loss_percent_value) < 1 or safe_float(stop_loss_percent_value) > 100:
|
||||
await message.answer(
|
||||
text="Ошибка: введите число от 1 до 100.",
|
||||
reply_markup=kbi.back_to_risk_management,
|
||||
@@ -238,8 +232,7 @@ async def set_stop_loss_percent(message: Message, state: FSMContext) -> None:
|
||||
return
|
||||
|
||||
req = await rq.set_stop_loss_percent(
|
||||
tg_id=message.from_user.id,
|
||||
stop_loss_percent=safe_float(stop_loss_percent_value),
|
||||
tg_id=message.from_user.id, stop_loss_percent=safe_float(stop_loss_percent_value)
|
||||
)
|
||||
|
||||
if req:
|
||||
|
||||
@@ -6,7 +6,7 @@ from aiogram.types import CallbackQuery
|
||||
|
||||
import app.telegram.keyboards.inline as kbi
|
||||
import database.request as rq
|
||||
from app.bybit import get_bybit_client
|
||||
|
||||
from app.helper_functions import calculate_total_budget, safe_float
|
||||
from logger_helper.logger_helper import LOGGING_CONFIG
|
||||
|
||||
@@ -25,7 +25,6 @@ async def additional_settings(callback_query: CallbackQuery, state: FSMContext)
|
||||
try:
|
||||
await state.clear()
|
||||
tg_id = callback_query.from_user.id
|
||||
symbol = await rq.get_user_symbol(tg_id=tg_id)
|
||||
additional_data = await rq.get_user_additional_settings(tg_id=tg_id)
|
||||
|
||||
if not additional_data:
|
||||
@@ -63,30 +62,15 @@ async def additional_settings(callback_query: CallbackQuery, state: FSMContext)
|
||||
max_bets = additional_data.max_bets_in_series
|
||||
quantity = f(additional_data.order_quantity)
|
||||
trigger_price = f(additional_data.trigger_price) or 0
|
||||
risk_management_data = await rq.get_user_risk_management(tg_id=tg_id)
|
||||
commission_fee = risk_management_data.commission_fee
|
||||
client = await get_bybit_client(tg_id=tg_id)
|
||||
fee_info = client.get_fee_rates(category="linear", symbol=symbol)
|
||||
|
||||
if commission_fee == "Yes_commission_fee":
|
||||
commission_fee_percent = safe_float(
|
||||
fee_info["result"]["list"][0]["takerFeeRate"]
|
||||
)
|
||||
|
||||
else:
|
||||
commission_fee_percent = 0.0
|
||||
|
||||
switch_side_mode = ""
|
||||
if trade_mode == "Switch":
|
||||
switch_side_mode = f"- Направление первой сделки: {switch_side}\n"
|
||||
|
||||
quantity_price = quantity * trigger_price
|
||||
total_commission = quantity_price * commission_fee_percent
|
||||
total_budget = await calculate_total_budget(
|
||||
quantity=quantity,
|
||||
martingale_factor=martingale,
|
||||
max_steps=max_bets,
|
||||
commission_fee_percent=total_commission,
|
||||
)
|
||||
text = (
|
||||
f"Основные настройки:\n\n"
|
||||
@@ -98,7 +82,7 @@ async def additional_settings(callback_query: CallbackQuery, state: FSMContext)
|
||||
f"- Коэффициент мартингейла: {martingale:.2f}\n"
|
||||
f"- Триггер цена: {trigger_price:.4f} USDT\n"
|
||||
f"- Максимальное кол-во ставок в серии: {max_bets}\n\n"
|
||||
f"- Бюджет серии: {total_budget:.4f} USDT\n"
|
||||
f"- Бюджет серии: {total_budget:.2f} USDT\n"
|
||||
)
|
||||
|
||||
keyboard = kbi.get_additional_settings_keyboard(mode=trade_mode)
|
||||
|
||||
@@ -10,7 +10,10 @@ import database.request as rq
|
||||
from app.bybit.get_functions.get_positions import get_active_positions_by_symbol
|
||||
from app.bybit.open_positions import start_trading_cycle
|
||||
from app.helper_functions import safe_float
|
||||
from app.telegram.tasks.tasks import add_start_task_merged, cancel_start_task_merged
|
||||
from app.telegram.tasks.tasks import (
|
||||
add_start_task_merged,
|
||||
cancel_start_task_merged
|
||||
)
|
||||
from logger_helper.logger_helper import LOGGING_CONFIG
|
||||
|
||||
logging.config.dictConfig(LOGGING_CONFIG)
|
||||
@@ -117,7 +120,9 @@ async def start_trading(callback_query: CallbackQuery, state: FSMContext) -> Non
|
||||
logger.error("Cancelled timer for user %s", callback_query.from_user.id)
|
||||
|
||||
|
||||
@router_start_trading.callback_query(lambda c: c.data == "cancel_timer_merged")
|
||||
@router_start_trading.callback_query(
|
||||
lambda c: c.data == "cancel_timer_merged"
|
||||
)
|
||||
async def cancel_start_trading(
|
||||
callback_query: CallbackQuery, state: FSMContext
|
||||
) -> None:
|
||||
|
||||
@@ -61,7 +61,8 @@ main_settings = InlineKeyboardMarkup(
|
||||
|
||||
|
||||
# additional_settings
|
||||
def get_additional_settings_keyboard(mode: str) -> InlineKeyboardMarkup:
|
||||
def get_additional_settings_keyboard(mode: str
|
||||
) -> InlineKeyboardMarkup:
|
||||
"""
|
||||
Create keyboard for additional settings
|
||||
:param mode: Trade mode
|
||||
@@ -76,23 +77,23 @@ def get_additional_settings_keyboard(mode: str) -> InlineKeyboardMarkup:
|
||||
InlineKeyboardButton(
|
||||
text="Размер кредитного плеча", callback_data="leverage"
|
||||
),
|
||||
InlineKeyboardButton(text="Базовая ставка", callback_data="order_quantity"),
|
||||
InlineKeyboardButton(
|
||||
text="Базовая ставка", callback_data="order_quantity"),
|
||||
],
|
||||
|
||||
[
|
||||
InlineKeyboardButton(
|
||||
text="Коэффициент мартингейла", callback_data="martingale_factor"
|
||||
),
|
||||
InlineKeyboardButton(text="Триггер цена", callback_data="trigger_price"),
|
||||
InlineKeyboardButton(text="Триггер цена", callback_data="trigger_price"
|
||||
|
||||
),
|
||||
],
|
||||
]
|
||||
|
||||
if mode == "Switch":
|
||||
buttons.append(
|
||||
[
|
||||
InlineKeyboardButton(
|
||||
text="Направление первой сделки", callback_data="switch_side_start"
|
||||
)
|
||||
]
|
||||
[InlineKeyboardButton(text="Направление первой сделки", callback_data="switch_side_start")]
|
||||
)
|
||||
|
||||
buttons.append(
|
||||
@@ -116,7 +117,9 @@ def get_additional_settings_keyboard(mode: str) -> InlineKeyboardMarkup:
|
||||
trade_mode = InlineKeyboardMarkup(
|
||||
inline_keyboard=[
|
||||
[
|
||||
InlineKeyboardButton(text="Лонг", callback_data="Long"),
|
||||
InlineKeyboardButton(
|
||||
text="Лонг", callback_data="Long"
|
||||
),
|
||||
InlineKeyboardButton(text="Шорт", callback_data="Short"),
|
||||
InlineKeyboardButton(text="Свитч", callback_data="Switch"),
|
||||
],
|
||||
@@ -185,7 +188,9 @@ risk_management = InlineKeyboardMarkup(
|
||||
InlineKeyboardButton(
|
||||
text="Тейк-профит", callback_data="take_profit_percent"
|
||||
),
|
||||
InlineKeyboardButton(text="Стоп-лосс", callback_data="stop_loss_percent"),
|
||||
InlineKeyboardButton(
|
||||
text="Стоп-лосс", callback_data="stop_loss_percent"
|
||||
),
|
||||
],
|
||||
[InlineKeyboardButton(text="Комиссия биржи", callback_data="commission_fee")],
|
||||
[
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
from aiogram.types import KeyboardButton, ReplyKeyboardMarkup
|
||||
|
||||
profile = ReplyKeyboardMarkup(
|
||||
keyboard=[[KeyboardButton(text="Панель Bybit"), KeyboardButton(text="Профиль")],
|
||||
[KeyboardButton(text="Подключить платформу Bybit")]],
|
||||
keyboard=[
|
||||
[KeyboardButton(text="Панель Bybit"), KeyboardButton(text="Профиль")],
|
||||
[KeyboardButton(text="Подключить платформу Bybit")],
|
||||
],
|
||||
resize_keyboard=True,
|
||||
one_time_keyboard=True,
|
||||
input_field_placeholder="Выберите пункт меню...",
|
||||
|
||||
@@ -12,9 +12,7 @@ logger = logging.getLogger("database")
|
||||
|
||||
async_engine = create_async_engine(DATABASE_URL, echo=False)
|
||||
|
||||
async_session = async_sessionmaker(
|
||||
async_engine, class_=AsyncSession, expire_on_commit=False
|
||||
)
|
||||
async_session = async_sessionmaker(async_engine, class_=AsyncSession, expire_on_commit=False)
|
||||
|
||||
|
||||
async def init_db():
|
||||
|
||||
@@ -1,15 +1,6 @@
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.ext.asyncio import AsyncAttrs
|
||||
from sqlalchemy import (
|
||||
Column,
|
||||
ForeignKey,
|
||||
Integer,
|
||||
String,
|
||||
BigInteger,
|
||||
Float,
|
||||
Boolean,
|
||||
UniqueConstraint,
|
||||
)
|
||||
from sqlalchemy import Column, ForeignKey, Integer, String, BigInteger, Float, Boolean, UniqueConstraint
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
Base = declarative_base(cls=AsyncAttrs)
|
||||
@@ -17,77 +8,61 @@ Base = declarative_base(cls=AsyncAttrs)
|
||||
|
||||
class User(Base):
|
||||
"""User model."""
|
||||
|
||||
__tablename__ = "users"
|
||||
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
tg_id = Column(BigInteger, nullable=False, unique=True)
|
||||
username = Column(String, nullable=False)
|
||||
|
||||
user_api = relationship(
|
||||
"UserApi",
|
||||
user_api = relationship("UserApi",
|
||||
back_populates="user",
|
||||
cascade="all, delete-orphan",
|
||||
passive_deletes=True,
|
||||
uselist=False,
|
||||
)
|
||||
uselist=False)
|
||||
|
||||
user_symbol = relationship(
|
||||
"UserSymbol",
|
||||
user_symbol = relationship("UserSymbol",
|
||||
back_populates="user",
|
||||
cascade="all, delete-orphan",
|
||||
passive_deletes=True,
|
||||
uselist=False,
|
||||
)
|
||||
uselist=False)
|
||||
|
||||
user_additional_settings = relationship(
|
||||
"UserAdditionalSettings",
|
||||
user_additional_settings = relationship("UserAdditionalSettings",
|
||||
back_populates="user",
|
||||
cascade="all, delete-orphan",
|
||||
passive_deletes=True,
|
||||
uselist=False,
|
||||
)
|
||||
uselist=False)
|
||||
|
||||
user_risk_management = relationship(
|
||||
"UserRiskManagement",
|
||||
user_risk_management = relationship("UserRiskManagement",
|
||||
back_populates="user",
|
||||
cascade="all, delete-orphan",
|
||||
passive_deletes=True,
|
||||
uselist=False,
|
||||
)
|
||||
uselist=False)
|
||||
|
||||
user_conditional_settings = relationship(
|
||||
"UserConditionalSettings",
|
||||
user_conditional_settings = relationship("UserConditionalSettings",
|
||||
back_populates="user",
|
||||
cascade="all, delete-orphan",
|
||||
passive_deletes=True,
|
||||
uselist=False,
|
||||
)
|
||||
uselist=False)
|
||||
|
||||
user_deals = relationship(
|
||||
"UserDeals",
|
||||
user_deals = relationship("UserDeals",
|
||||
back_populates="user",
|
||||
cascade="all, delete-orphan",
|
||||
passive_deletes=True,
|
||||
)
|
||||
passive_deletes=True)
|
||||
|
||||
user_auto_trading = relationship(
|
||||
"UserAutoTrading",
|
||||
user_auto_trading = relationship("UserAutoTrading",
|
||||
back_populates="user",
|
||||
cascade="all, delete-orphan",
|
||||
passive_deletes=True,
|
||||
)
|
||||
passive_deletes=True)
|
||||
|
||||
|
||||
class UserApi(Base):
|
||||
"""User API model."""
|
||||
|
||||
__tablename__ = "user_api"
|
||||
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
user_id = Column(
|
||||
Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False, unique=True
|
||||
)
|
||||
user_id = Column(Integer,
|
||||
ForeignKey("users.id", ondelete="CASCADE"),
|
||||
nullable=False, unique=True)
|
||||
api_key = Column(String, nullable=False)
|
||||
api_secret = Column(String, nullable=False)
|
||||
|
||||
@@ -96,13 +71,12 @@ class UserApi(Base):
|
||||
|
||||
class UserSymbol(Base):
|
||||
"""User symbol model."""
|
||||
|
||||
__tablename__ = "user_symbol"
|
||||
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
user_id = Column(
|
||||
Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False, unique=True
|
||||
)
|
||||
user_id = Column(Integer,
|
||||
ForeignKey("users.id", ondelete="CASCADE"),
|
||||
nullable=False, unique=True)
|
||||
symbol = Column(String, nullable=False, default="BTCUSDT")
|
||||
|
||||
user = relationship("User", back_populates="user_symbol")
|
||||
@@ -110,13 +84,12 @@ class UserSymbol(Base):
|
||||
|
||||
class UserAdditionalSettings(Base):
|
||||
"""User additional settings model."""
|
||||
|
||||
__tablename__ = "user_additional_settings"
|
||||
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
user_id = Column(
|
||||
Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False, unique=True
|
||||
)
|
||||
user_id = Column(Integer,
|
||||
ForeignKey("users.id", ondelete="CASCADE"),
|
||||
nullable=False, unique=True)
|
||||
trade_mode = Column(String, nullable=False, default="Merged_Single")
|
||||
switch_side = Column(String, nullable=False, default="По направлению")
|
||||
trigger_price = Column(Float, nullable=False, default=0.0)
|
||||
@@ -131,13 +104,12 @@ class UserAdditionalSettings(Base):
|
||||
|
||||
class UserRiskManagement(Base):
|
||||
"""User risk management model."""
|
||||
|
||||
__tablename__ = "user_risk_management"
|
||||
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
user_id = Column(
|
||||
Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False, unique=True
|
||||
)
|
||||
user_id = Column(Integer,
|
||||
ForeignKey("users.id", ondelete="CASCADE"),
|
||||
nullable=False, unique=True)
|
||||
take_profit_percent = Column(Float, nullable=False, default=1)
|
||||
stop_loss_percent = Column(Float, nullable=False, default=1)
|
||||
commission_fee = Column(String, nullable=False, default="Yes_commission_fee")
|
||||
@@ -147,13 +119,12 @@ class UserRiskManagement(Base):
|
||||
|
||||
class UserConditionalSettings(Base):
|
||||
"""User conditional settings model."""
|
||||
|
||||
__tablename__ = "user_conditional_settings"
|
||||
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
user_id = Column(
|
||||
Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False, unique=True
|
||||
)
|
||||
user_id = Column(Integer,
|
||||
ForeignKey("users.id", ondelete="CASCADE"),
|
||||
nullable=False, unique=True)
|
||||
|
||||
timer_start = Column(Integer, nullable=False, default=0)
|
||||
timer_end = Column(Integer, nullable=False, default=0)
|
||||
@@ -163,13 +134,12 @@ class UserConditionalSettings(Base):
|
||||
|
||||
class UserDeals(Base):
|
||||
"""User deals model."""
|
||||
|
||||
__tablename__ = "user_deals"
|
||||
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
user_id = Column(
|
||||
Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False
|
||||
)
|
||||
user_id = Column(Integer,
|
||||
ForeignKey("users.id", ondelete="CASCADE"),
|
||||
nullable=False)
|
||||
current_step = Column(Integer, nullable=True)
|
||||
symbol = Column(String, nullable=True)
|
||||
trade_mode = Column(String, nullable=True)
|
||||
@@ -187,18 +157,19 @@ class UserDeals(Base):
|
||||
|
||||
user = relationship("User", back_populates="user_deals")
|
||||
|
||||
__table_args__ = (UniqueConstraint("user_id", "symbol", name="uq_user_symbol"),)
|
||||
__table_args__ = (
|
||||
UniqueConstraint('user_id', 'symbol', name='uq_user_symbol'),
|
||||
)
|
||||
|
||||
|
||||
class UserAutoTrading(Base):
|
||||
"""User auto trading model."""
|
||||
|
||||
__tablename__ = "user_auto_trading"
|
||||
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
user_id = Column(
|
||||
Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False
|
||||
)
|
||||
user_id = Column(Integer,
|
||||
ForeignKey("users.id", ondelete="CASCADE"),
|
||||
nullable=False)
|
||||
symbol = Column(String, nullable=True)
|
||||
auto_trading = Column(Boolean, nullable=True)
|
||||
fee = Column(Float, nullable=True)
|
||||
|
||||
@@ -906,7 +906,7 @@ async def set_user_deal(
|
||||
max_bets_in_series: int,
|
||||
take_profit_percent: int,
|
||||
stop_loss_percent: int,
|
||||
base_quantity: float,
|
||||
base_quantity: float
|
||||
):
|
||||
"""
|
||||
Set the user deal in the database.
|
||||
@@ -969,7 +969,7 @@ async def set_user_deal(
|
||||
max_bets_in_series=max_bets_in_series,
|
||||
take_profit_percent=take_profit_percent,
|
||||
stop_loss_percent=stop_loss_percent,
|
||||
base_quantity=base_quantity,
|
||||
base_quantity=base_quantity
|
||||
)
|
||||
session.add(new_deal)
|
||||
|
||||
@@ -978,9 +978,7 @@ async def set_user_deal(
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
"Error setting user deal for user %s and symbol %s: %s", tg_id, symbol, e
|
||||
)
|
||||
logger.error("Error setting user deal for user %s and symbol %s: %s", tg_id, symbol, e)
|
||||
return False
|
||||
|
||||
|
||||
@@ -1000,9 +998,7 @@ async def get_user_deal_by_symbol(tg_id: int, symbol: str):
|
||||
deal = result_deal.scalars().first()
|
||||
return deal
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
"Error getting deal for user %s and symbol %s: %s", tg_id, symbol, e
|
||||
)
|
||||
logger.error("Error getting deal for user %s and symbol %s: %s", tg_id, symbol, e)
|
||||
return None
|
||||
|
||||
|
||||
@@ -1044,26 +1040,18 @@ async def set_fee_user_deal_by_symbol(tg_id: int, symbol: str, fee: float):
|
||||
if record:
|
||||
record.fee = fee
|
||||
else:
|
||||
logger.error(
|
||||
f"User deal with user_id={user.id} and symbol={symbol} not found"
|
||||
)
|
||||
logger.error(f"User deal with user_id={user.id} and symbol={symbol} not found")
|
||||
return False
|
||||
await session.commit()
|
||||
logger.info("Set fee for user %s and symbol %s", tg_id, symbol)
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
"Error setting user deal fee for user %s and symbol %s: %s",
|
||||
tg_id,
|
||||
symbol,
|
||||
e,
|
||||
)
|
||||
logger.error("Error setting user deal fee for user %s and symbol %s: %s", tg_id, symbol, e)
|
||||
return False
|
||||
|
||||
|
||||
# USER AUTO TRADING
|
||||
|
||||
|
||||
async def get_all_user_auto_trading(tg_id: int):
|
||||
"""Get all user auto trading from the database asynchronously."""
|
||||
try:
|
||||
@@ -1098,9 +1086,7 @@ async def get_user_auto_trading(tg_id: int, symbol: str):
|
||||
auto_trading = result_auto_trading.scalars().first()
|
||||
return auto_trading
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
"Error getting auto trading for user %s and symbol %s: %s", tg_id, symbol, e
|
||||
)
|
||||
logger.error("Error getting auto trading for user %s and symbol %s: %s", tg_id, symbol, e)
|
||||
return None
|
||||
|
||||
|
||||
@@ -1134,17 +1120,10 @@ async def set_auto_trading(tg_id: int, symbol: str, auto_trading: bool) -> bool:
|
||||
)
|
||||
session.add(new_record)
|
||||
await session.commit()
|
||||
logger.info(
|
||||
"Set auto_trading=%s for user %s and symbol %s",
|
||||
auto_trading,
|
||||
tg_id,
|
||||
symbol,
|
||||
)
|
||||
logger.info("Set auto_trading=%s for user %s and symbol %s", auto_trading, tg_id, symbol)
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
"Error setting auto_trading for user %s and symbol %s: %s", tg_id, symbol, e
|
||||
)
|
||||
logger.error("Error setting auto_trading for user %s and symbol %s: %s", tg_id, symbol, e)
|
||||
return False
|
||||
|
||||
|
||||
@@ -1182,18 +1161,11 @@ async def set_fee_user_auto_trading(tg_id: int, symbol: str, fee: float) -> bool
|
||||
logger.info("Set fee for user %s and symbol %s", tg_id, symbol)
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
"Error setting user auto trading fee for user %s and symbol %s: %s",
|
||||
tg_id,
|
||||
symbol,
|
||||
e,
|
||||
)
|
||||
logger.error("Error setting user auto trading fee for user %s and symbol %s: %s", tg_id, symbol, e)
|
||||
return False
|
||||
|
||||
|
||||
async def set_total_fee_user_auto_trading(
|
||||
tg_id: int, symbol: str, total_fee: float
|
||||
) -> bool:
|
||||
async def set_total_fee_user_auto_trading(tg_id: int, symbol: str, total_fee: float) -> bool:
|
||||
"""
|
||||
Set the total fee for a user auto trading in the database.
|
||||
:param tg_id: Telegram user ID
|
||||
@@ -1227,10 +1199,5 @@ async def set_total_fee_user_auto_trading(
|
||||
logger.info("Set total fee for user %s and symbol %s", tg_id, symbol)
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
"Error setting user auto trading total fee for user %s and symbol %s: %s",
|
||||
tg_id,
|
||||
symbol,
|
||||
e,
|
||||
)
|
||||
logger.error("Error setting user auto trading total fee for user %s and symbol %s: %s", tg_id, symbol, e)
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user