[Python] Why is my Python trading bot not executing buy orders even with correct model...

Discussão em 'Python' iniciado por Stack, Maio 17, 2025.

  1. Stack

    Stack Membro Participativo

    I'm developing a real-time simulated trading bot in Python that uses a RandomForestClassifier to predict buy/sell signals for NASDAQ-100 stocks. The bot uses fake money and no APIs—it operates in a Tkinter GUI with a portfolio balance and candlestick chart visualization.

    The logic is:

    model.predict() returns 1 for buy, -1 for sell

    If prediction == 1 and there's enough fake cash, the bot should buy

    If prediction == -1 and the stock is owned, the bot should sell

    However, the bot only seems to sell and never executes buy orders, even when the model outputs 1.

    I’ve confirmed that:

    prediction == 1 does occur (I logged it)

    There is enough cash

    The current price is a float like 174.12, cash is initialized as 10000.0

    Possible issues I’ve considered:

    Model output isn't correctly mapped

    Logic bug in the if-statement conditions

    GUI state (Tkinter) is affecting updates

    Has anyone run into a similar issue with simulated trading logic in a GUI or ML setting? Any ideas on why this buy block isn't firing?

    if prediction == 1 and cash_balance >= current_price:
    portfolio[symbol] = portfolio.get(symbol, 0) + 1
    cash_balance -= current_price
    print(f"Bought {symbol}")
    elif prediction == -1 and portfolio.get(symbol, 0) > 0:
    portfolio[symbol] -= 1
    cash_balance += current_price
    print(f"Sold {symbol}")

    Continue reading...

Compartilhe esta Página