2
0
forked from kodorvan/stcs

Added the addition of a common commission

This commit is contained in:
algizn97
2025-10-10 13:16:00 +05:00
parent a0a2fd30f0
commit 09606a057b

View File

@@ -198,15 +198,12 @@ async def create_user_additional_settings(tg_id: int) -> None:
# Create the user additional settings
user_additional_settings = UserAdditionalSettings(
user=user,
trade_mode="Merged_Single", # Default value
order_type="Market",
conditional_order_type="Market",
trade_mode="Long", # Default value
switch_side="По направлению",
margin_type="ISOLATED_MARGIN",
leverage="10",
leverage_to_buy="10",
leverage_to_sell="10",
order_quantity=5.0,
martingale_factor=1.0,
order_quantity=1.0,
martingale_factor=2.0,
max_bets_in_series=10,
)
session.add(user_additional_settings)
@@ -283,90 +280,6 @@ async def set_trade_mode(tg_id: int, trade_mode: str) -> bool:
return False
async def set_order_type(tg_id: int, order_type: str) -> bool:
"""
Set order type for a user in the database.
:param tg_id: Telegram user ID
:param order_type: "Market" or "Limit"
:return: True if successful, False otherwise
"""
try:
async with async_session() as session:
result = await session.execute(
select(User)
.options(joinedload(User.user_additional_settings))
.filter_by(tg_id=tg_id)
)
user = result.scalars().first()
if user:
if user.user_additional_settings:
# Updating existing record
user.user_additional_settings.order_type = order_type
else:
# Creating new record
user_additional_settings = UserAdditionalSettings(
order_type=order_type,
user=user,
)
session.add(user_additional_settings)
await session.commit()
logger.info("User order type updated for user: %s", tg_id)
return True
else:
logger.error("User not found with tg_id: %s", tg_id)
return False
except Exception as e:
logger.error("Error adding/updating user order type for user %s: %s", tg_id, e)
return False
async def set_conditional_order_type(tg_id: int, conditional_order_type: str) -> bool:
"""
Set conditional order type for a user in the database.
:param tg_id: Telegram user ID
:param conditional_order_type: "Market" or "Limit"
:return: True if successful, False otherwise
"""
try:
async with async_session() as session:
result = await session.execute(
select(User)
.options(joinedload(User.user_additional_settings))
.filter_by(tg_id=tg_id)
)
user = result.scalars().first()
if user:
if user.user_additional_settings:
# Updating existing record
user.user_additional_settings.conditional_order_type = (
conditional_order_type
)
else:
# Creating new record
user_additional_settings = UserAdditionalSettings(
conditional_order_type=conditional_order_type,
user=user,
)
session.add(user_additional_settings)
await session.commit()
logger.info("User conditional order type updated for user: %s", tg_id)
return True
else:
logger.error("User not found with tg_id: %s", tg_id)
return False
except Exception as e:
logger.error(
"Error adding/updating user conditional order type for user %s: %s",
tg_id,
e,
)
return False
async def set_margin_type(tg_id: int, margin_type: str) -> bool:
"""
Set margin type for a user in the database.
@@ -406,6 +319,45 @@ async def set_margin_type(tg_id: int, margin_type: str) -> bool:
return False
async def set_switch_side(tg_id: int, switch_side: str) -> bool:
"""
Set switch side for a user in the database.
:param tg_id: Telegram user ID
:param switch_side: "По направлению" or "По цене"
:return: True if successful, False otherwise
"""
try:
async with async_session() as session:
result = await session.execute(
select(User)
.options(joinedload(User.user_additional_settings))
.filter_by(tg_id=tg_id)
)
user = result.scalars().first()
if user:
if user.user_additional_settings:
# Updating existing record
user.user_additional_settings.switch_side = switch_side
else:
# Creating new record
user_additional_settings = UserAdditionalSettings(
switch_side=switch_side,
user=user,
)
session.add(user_additional_settings)
await session.commit()
logger.info("User switch side updated for user: %s", tg_id)
return True
else:
logger.error("User not found with tg_id: %s", tg_id)
return False
except Exception as e:
logger.error("Error adding/updating user switch side for user %s: %s", tg_id, e)
return False
async def set_leverage(tg_id: int, leverage: str) -> bool:
"""
Set leverage for a user in the database.
@@ -445,50 +397,6 @@ async def set_leverage(tg_id: int, leverage: str) -> bool:
return False
async def set_leverage_to_buy_and_sell(
tg_id: int, leverage_to_buy: str, leverage_to_sell: str
) -> bool:
"""
Set leverage for a user in the database.
:param tg_id: Telegram user ID
:param leverage_to_buy: Leverage to buy
:param leverage_to_sell: Leverage to sell
:return: True if successful, False otherwise
"""
try:
async with async_session() as session:
result = await session.execute(
select(User)
.options(joinedload(User.user_additional_settings))
.filter_by(tg_id=tg_id)
)
user = result.scalars().first()
if user:
if user.user_additional_settings:
# Updating existing record
user.user_additional_settings.leverage_to_buy = leverage_to_buy
user.user_additional_settings.leverage_to_sell = leverage_to_sell
else:
# Creating new record
user_additional_settings = UserAdditionalSettings(
leverage_to_buy=leverage_to_buy,
leverage_to_sell=leverage_to_sell,
user=user,
)
session.add(user_additional_settings)
await session.commit()
logger.info("User leverage updated for user: %s", tg_id)
return True
else:
logger.error("User not found with tg_id: %s", tg_id)
return False
except Exception as e:
logger.error("Error adding/updating user leverage for user %s: %s", tg_id, e)
return False
async def set_order_quantity(tg_id: int, order_quantity: float) -> bool:
"""
Set order quantity for a user in the database.
@@ -614,45 +522,6 @@ async def set_max_bets_in_series(tg_id: int, max_bets_in_series: int) -> bool:
return False
async def set_limit_price(tg_id: int, limit_price: float) -> bool:
"""
Set limit price for a user in the database.
:param tg_id:
:param limit_price:
:return: bool
"""
try:
async with async_session() as session:
result = await session.execute(
select(User)
.options(joinedload(User.user_additional_settings))
.filter_by(tg_id=tg_id)
)
user = result.scalars().first()
if user:
if user.user_additional_settings:
# Updating existing record
user.user_additional_settings.limit_price = limit_price
else:
# Creating new record
user_additional_settings = UserAdditionalSettings(
limit_price=limit_price,
user=user,
)
session.add(user_additional_settings)
await session.commit()
logger.info("User limit price updated for user: %s", tg_id)
return True
else:
logger.error("User not found with tg_id: %s", tg_id)
return False
except Exception as e:
logger.error("Error adding/updating user limit price for user %s: %s", tg_id, e)
return False
async def set_trigger_price(tg_id: int, trigger_price: float) -> bool:
"""
Set trigger price for a user in the database.
@@ -718,9 +587,8 @@ async def create_user_risk_management(tg_id: int) -> None:
# Create the user risk management
user_risk_management = UserRiskManagement(
user=user,
take_profit_percent=1,
stop_loss_percent=1,
max_risk_percent=100,
take_profit_percent=1.0,
stop_loss_percent=1.0,
commission_fee="Yes_commission_fee",
)
session.add(user_risk_management)
@@ -758,7 +626,7 @@ async def get_user_risk_management(tg_id: int):
return None
async def set_take_profit_percent(tg_id: int, take_profit_percent: int) -> bool:
async def set_take_profit_percent(tg_id: int, take_profit_percent: float) -> bool:
"""
Set take profit percent for a user in the database.
:param tg_id: Telegram user ID
@@ -799,7 +667,7 @@ async def set_take_profit_percent(tg_id: int, take_profit_percent: int) -> bool:
return False
async def set_stop_loss_percent(tg_id: int, stop_loss_percent: int) -> bool:
async def set_stop_loss_percent(tg_id: int, stop_loss_percent: float) -> bool:
"""
Set stop loss percent for a user in the database.
:param tg_id: Telegram user ID
@@ -840,47 +708,6 @@ async def set_stop_loss_percent(tg_id: int, stop_loss_percent: int) -> bool:
return False
async def set_max_risk_percent(tg_id: int, max_risk_percent: int) -> bool:
"""
Set max risk percent for a user in the database.
:param tg_id: Telegram user ID
:param max_risk_percent: Max risk percent
:return: True if successful, False otherwise
"""
try:
async with async_session() as session:
result = await session.execute(
select(User)
.options(joinedload(User.user_risk_management))
.filter_by(tg_id=tg_id)
)
user = result.scalars().first()
if user:
if user.user_risk_management:
# Updating existing record
user.user_risk_management.max_risk_percent = max_risk_percent
else:
# Creating new record
user_risk_management = UserRiskManagement(
max_risk_percent=max_risk_percent,
user=user,
)
session.add(user_risk_management)
await session.commit()
logger.info("User max risk percent updated for user: %s", tg_id)
return True
else:
logger.error("User not found with tg_id: %s", tg_id)
return False
except Exception as e:
logger.error(
"Error adding/updating user max risk percent for user %s: %s", tg_id, e
)
return False
async def set_commission_fee(tg_id: int, commission_fee: str) -> bool:
"""
Set commission fee for a user in the database.
@@ -1073,19 +900,13 @@ async def set_user_deal(
trade_mode: str,
margin_type: str,
leverage: str,
leverage_to_buy: str,
leverage_to_sell: str,
order_type: str,
conditional_order_type: str,
order_quantity: float,
limit_price: float,
trigger_price: float,
martingale_factor: float,
max_bets_in_series: int,
take_profit_percent: int,
stop_loss_percent: int,
max_risk_percent: int,
switch_side_mode: bool,
base_quantity: float
):
"""
Set the user deal in the database.
@@ -1096,19 +917,13 @@ async def set_user_deal(
:param trade_mode: Trade mode
:param margin_type: Margin type
:param leverage: Leverage
:param leverage_to_buy: Leverage to buy
:param leverage_to_sell: Leverage to sell
:param order_type: Order type
:param conditional_order_type: Conditional order type
:param order_quantity: Order quantity
:param limit_price: Limit price
:param trigger_price: Trigger price
:param martingale_factor: Martingale factor
:param max_bets_in_series: Max bets in series
:param take_profit_percent: Take profit percent
:param stop_loss_percent: Stop loss percent
:param max_risk_percent: Max risk percent
:param switch_side_mode: Switch side mode
:param base_quantity: Base quantity
:return: bool
"""
try:
@@ -1131,19 +946,13 @@ async def set_user_deal(
deal.trade_mode = trade_mode
deal.margin_type = margin_type
deal.leverage = leverage
deal.leverage_to_buy = leverage_to_buy
deal.leverage_to_sell = leverage_to_sell
deal.order_type = order_type
deal.conditional_order_type = conditional_order_type
deal.order_quantity = order_quantity
deal.limit_price = limit_price
deal.trigger_price = trigger_price
deal.martingale_factor = martingale_factor
deal.max_bets_in_series = max_bets_in_series
deal.take_profit_percent = take_profit_percent
deal.stop_loss_percent = stop_loss_percent
deal.max_risk_percent = max_risk_percent
deal.switch_side_mode = switch_side_mode
deal.base_quantity = base_quantity
else:
# Creating new record
new_deal = UserDeals(
@@ -1154,19 +963,13 @@ async def set_user_deal(
trade_mode=trade_mode,
margin_type=margin_type,
leverage=leverage,
leverage_to_buy=leverage_to_buy,
leverage_to_sell=leverage_to_sell,
order_type=order_type,
conditional_order_type=conditional_order_type,
order_quantity=order_quantity,
limit_price=limit_price,
trigger_price=trigger_price,
martingale_factor=martingale_factor,
max_bets_in_series=max_bets_in_series,
take_profit_percent=take_profit_percent,
stop_loss_percent=stop_loss_percent,
max_risk_percent=max_risk_percent,
switch_side_mode=switch_side_mode,
base_quantity=base_quantity
)
session.add(new_deal)
@@ -1268,7 +1071,7 @@ async def get_all_user_auto_trading(tg_id: int):
return []
async def get_user_auto_trading(tg_id: int, symbol: str, side: str):
async def get_user_auto_trading(tg_id: int, symbol: str):
"""Get user auto trading from the database asynchronously."""
try:
async with async_session() as session:
@@ -1278,7 +1081,7 @@ async def get_user_auto_trading(tg_id: int, symbol: str, side: str):
return None
result_auto_trading = await session.execute(
select(UserAutoTrading).filter_by(user_id=user.id, symbol=symbol, side=side)
select(UserAutoTrading).filter_by(user_id=user.id, symbol=symbol)
)
auto_trading = result_auto_trading.scalars().first()
return auto_trading
@@ -1287,13 +1090,12 @@ async def get_user_auto_trading(tg_id: int, symbol: str, side: str):
return None
async def set_auto_trading(tg_id: int, symbol: str, auto_trading: bool, side: str) -> bool:
async def set_auto_trading(tg_id: int, symbol: str, auto_trading: bool) -> bool:
"""
Set the auto trading for a user in the database.
:param tg_id: Telegram user ID
:param symbol: Symbol
:param auto_trading: Auto trading
:param side: Side
:return: bool
"""
try:
@@ -1305,7 +1107,7 @@ async def set_auto_trading(tg_id: int, symbol: str, auto_trading: bool, side: st
return False
result = await session.execute(
select(UserAutoTrading).filter_by(user_id=user.id, symbol=symbol, side=side)
select(UserAutoTrading).filter_by(user_id=user.id, symbol=symbol)
)
record = result.scalars().first()
if record:
@@ -1315,7 +1117,6 @@ async def set_auto_trading(tg_id: int, symbol: str, auto_trading: bool, side: st
user_id=user.id,
symbol=symbol,
auto_trading=auto_trading,
side=side
)
session.add(new_record)
await session.commit()
@@ -1326,12 +1127,11 @@ async def set_auto_trading(tg_id: int, symbol: str, auto_trading: bool, side: st
return False
async def set_fee_user_auto_trading(tg_id: int, symbol: str, side: str, fee: float) -> bool:
async def set_fee_user_auto_trading(tg_id: int, symbol: str, fee: float) -> bool:
"""
Set the fee for a user auto trading in the database.
:param tg_id:
:param symbol:
:param side:
:param fee:
:return:
"""
@@ -1344,7 +1144,7 @@ async def set_fee_user_auto_trading(tg_id: int, symbol: str, side: str, fee: flo
return False
result = await session.execute(
select(UserAutoTrading).filter_by(user_id=user.id, symbol=symbol, side=side)
select(UserAutoTrading).filter_by(user_id=user.id, symbol=symbol)
)
record = result.scalars().first()
@@ -1355,7 +1155,6 @@ async def set_fee_user_auto_trading(tg_id: int, symbol: str, side: str, fee: flo
user_id=user.id,
symbol=symbol,
fee=fee,
side=side
)
session.add(user_fee)
await session.commit()
@@ -1364,3 +1163,41 @@ async def set_fee_user_auto_trading(tg_id: int, symbol: str, side: str, fee: flo
except Exception as 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:
"""
Set the total fee for a user auto trading in the database.
:param tg_id: Telegram user ID
:param symbol: Symbol
:param total_fee: Total fee
:return: bool
"""
try:
async with async_session() as session:
result = await session.execute(select(User).filter_by(tg_id=tg_id))
user = result.scalars().first()
if user is None:
logger.error(f"User with tg_id={tg_id} not found")
return False
result = await session.execute(
select(UserAutoTrading).filter_by(user_id=user.id, symbol=symbol)
)
record = result.scalars().first()
if record:
record.total_fee = total_fee
else:
user_total_fee = UserAutoTrading(
user_id=user.id,
symbol=symbol,
total_fee=total_fee,
)
session.add(user_total_fee)
await session.commit()
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)
return False