Skip to content

BotRepository API Reference

tradingbot.utils.bot_repository.BotRepository

Handles database operations for Bot entities.

create_or_get_bot(name: str) -> BotModel staticmethod

Create or retrieve bot from database.

Parameters:

Name Type Description Default
name str

Bot name

required

Returns:

Type Description
Bot

BotModel instance (detached from session but with attributes loaded)

Source code in tradingbot/utils/bot_repository.py
@staticmethod
def create_or_get_bot(name: str) -> BotModel:
    """
    Create or retrieve bot from database.

    Args:
        name: Bot name

    Returns:
        BotModel instance (detached from session but with attributes loaded)
    """
    with get_db_session() as session:
        bot = session.query(BotModel).filter_by(name=name).first()
        if not bot:
            bot = BotModel(name=name)
            session.add(bot)
            session.flush()  # Flush to get the ID, but let context manager commit
            session.refresh(bot)
        # Access portfolio to ensure it's loaded before expunging
        _ = bot.portfolio
        # Expunge the instance so it can be used outside the session
        # This detaches it but keeps loaded attributes accessible
        session.expunge(bot)
        return bot

log_trade(bot_name: str, symbol: str, quantity: float, price: float, is_buy: bool, profit: Optional[float] = None) -> Trade staticmethod

Log a trade to the database.

Parameters:

Name Type Description Default
bot_name str

Name of the bot executing the trade

required
symbol str

Trading symbol

required
quantity float

Number of shares/units

required
price float

Price per unit

required
is_buy bool

True for buy, False for sell

required
profit Optional[float]

Profit from the trade (for sells)

None

Returns:

Type Description
Trade

Created Trade object

Source code in tradingbot/utils/bot_repository.py
@staticmethod
def log_trade(
    bot_name: str,
    symbol: str,
    quantity: float,
    price: float,
    is_buy: bool,
    profit: Optional[float] = None,
) -> Trade:
    """
    Log a trade to the database.

    Args:
        bot_name: Name of the bot executing the trade
        symbol: Trading symbol
        quantity: Number of shares/units
        price: Price per unit
        is_buy: True for buy, False for sell
        profit: Profit from the trade (for sells)

    Returns:
        Created Trade object
    """
    with get_db_session() as session:
        trade = Trade(
            bot_name=bot_name,
            symbol=symbol,
            isBuy=is_buy,
            quantity=float(quantity),
            price=float(price),
            timestamp=datetime.utcnow(),
            profit=float(profit) if profit is not None else None,
        )
        session.add(trade)
        session.flush()  # Flush to get the ID, but let context manager commit
        session.refresh(trade)
        return trade

update_bot(bot: BotModel) -> BotModel staticmethod

Update bot state in database.

Parameters:

Name Type Description Default
bot Bot

BotModel instance to update

required

Returns:

Type Description
Bot

Updated BotModel instance

Source code in tradingbot/utils/bot_repository.py
@staticmethod
def update_bot(bot: BotModel) -> BotModel:
    """
    Update bot state in database.

    Args:
        bot: BotModel instance to update

    Returns:
        Updated BotModel instance
    """
    with get_db_session() as session:
        session.merge(bot)
        # Context manager will commit automatically
        return bot