forked from kodorvan/stcs
Compare commits
2 Commits
stable
...
0945be242a
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0945be242a | ||
|
|
4663888190 |
@@ -12,7 +12,9 @@ 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,6 +1,15 @@
|
||||
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)
|
||||
@@ -8,61 +17,77 @@ 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",
|
||||
back_populates="user",
|
||||
cascade="all, delete-orphan",
|
||||
passive_deletes=True,
|
||||
uselist=False)
|
||||
user_api = relationship(
|
||||
"UserApi",
|
||||
back_populates="user",
|
||||
cascade="all, delete-orphan",
|
||||
passive_deletes=True,
|
||||
uselist=False,
|
||||
)
|
||||
|
||||
user_symbol = relationship("UserSymbol",
|
||||
back_populates="user",
|
||||
cascade="all, delete-orphan",
|
||||
passive_deletes=True,
|
||||
uselist=False)
|
||||
user_symbol = relationship(
|
||||
"UserSymbol",
|
||||
back_populates="user",
|
||||
cascade="all, delete-orphan",
|
||||
passive_deletes=True,
|
||||
uselist=False,
|
||||
)
|
||||
|
||||
user_additional_settings = relationship("UserAdditionalSettings",
|
||||
back_populates="user",
|
||||
cascade="all, delete-orphan",
|
||||
passive_deletes=True,
|
||||
uselist=False)
|
||||
user_additional_settings = relationship(
|
||||
"UserAdditionalSettings",
|
||||
back_populates="user",
|
||||
cascade="all, delete-orphan",
|
||||
passive_deletes=True,
|
||||
uselist=False,
|
||||
)
|
||||
|
||||
user_risk_management = relationship("UserRiskManagement",
|
||||
back_populates="user",
|
||||
cascade="all, delete-orphan",
|
||||
passive_deletes=True,
|
||||
uselist=False)
|
||||
user_risk_management = relationship(
|
||||
"UserRiskManagement",
|
||||
back_populates="user",
|
||||
cascade="all, delete-orphan",
|
||||
passive_deletes=True,
|
||||
uselist=False,
|
||||
)
|
||||
|
||||
user_conditional_settings = relationship("UserConditionalSettings",
|
||||
back_populates="user",
|
||||
cascade="all, delete-orphan",
|
||||
passive_deletes=True,
|
||||
uselist=False)
|
||||
user_conditional_settings = relationship(
|
||||
"UserConditionalSettings",
|
||||
back_populates="user",
|
||||
cascade="all, delete-orphan",
|
||||
passive_deletes=True,
|
||||
uselist=False,
|
||||
)
|
||||
|
||||
user_deals = relationship("UserDeals",
|
||||
back_populates="user",
|
||||
cascade="all, delete-orphan",
|
||||
passive_deletes=True)
|
||||
user_deals = relationship(
|
||||
"UserDeals",
|
||||
back_populates="user",
|
||||
cascade="all, delete-orphan",
|
||||
passive_deletes=True,
|
||||
)
|
||||
|
||||
user_auto_trading = relationship("UserAutoTrading",
|
||||
back_populates="user",
|
||||
cascade="all, delete-orphan",
|
||||
passive_deletes=True)
|
||||
user_auto_trading = relationship(
|
||||
"UserAutoTrading",
|
||||
back_populates="user",
|
||||
cascade="all, delete-orphan",
|
||||
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)
|
||||
|
||||
@@ -71,12 +96,13 @@ 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")
|
||||
@@ -84,12 +110,13 @@ 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)
|
||||
@@ -104,12 +131,13 @@ 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")
|
||||
@@ -119,12 +147,13 @@ 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)
|
||||
@@ -134,12 +163,13 @@ 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)
|
||||
@@ -157,22 +187,21 @@ 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)
|
||||
total_fee = Column(Float, nullable=True)
|
||||
|
||||
user = relationship("User", back_populates="user_auto_trading")
|
||||
user = relationship("User", back_populates="user_auto_trading")
|
||||
|
||||
@@ -893,20 +893,20 @@ async def set_stop_timer(tg_id: int, timer_end: int) -> bool:
|
||||
|
||||
# USER DEALS
|
||||
async def set_user_deal(
|
||||
tg_id: int,
|
||||
symbol: str,
|
||||
last_side: str,
|
||||
current_step: int,
|
||||
trade_mode: str,
|
||||
margin_type: str,
|
||||
leverage: str,
|
||||
order_quantity: float,
|
||||
trigger_price: float,
|
||||
martingale_factor: float,
|
||||
max_bets_in_series: int,
|
||||
take_profit_percent: int,
|
||||
stop_loss_percent: int,
|
||||
base_quantity: float
|
||||
tg_id: int,
|
||||
symbol: str,
|
||||
last_side: str,
|
||||
current_step: int,
|
||||
trade_mode: str,
|
||||
margin_type: str,
|
||||
leverage: str,
|
||||
order_quantity: float,
|
||||
trigger_price: float,
|
||||
martingale_factor: float,
|
||||
max_bets_in_series: int,
|
||||
take_profit_percent: int,
|
||||
stop_loss_percent: int,
|
||||
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,7 +978,9 @@ 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
|
||||
|
||||
|
||||
@@ -998,7 +1000,9 @@ 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
|
||||
|
||||
|
||||
@@ -1040,18 +1044,26 @@ 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:
|
||||
@@ -1086,7 +1098,9 @@ 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
|
||||
|
||||
|
||||
@@ -1120,10 +1134,17 @@ 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
|
||||
|
||||
|
||||
@@ -1161,11 +1182,18 @@ 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
|
||||
@@ -1199,5 +1227,10 @@ async def set_total_fee_user_auto_trading(tg_id: int, symbol: str, total_fee: fl
|
||||
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