A fully featured trading bot that uses reinforcement learning–inspired parameter tuning, a Flask web dashboard, market-hour awareness, and end-of-day watchlist discovery. This bot uses yfinance to retrieve near real-time historical data and executes trades via Alpaca's API. It calculates a composite signal based on technical indicators (RSI, MACD, SMA, etc.) to determine buy/sell decisions during US market hours and automatically adjusts its strategy parameters over time.
- Near Real-Time Data: Uses yfinance for up-to-date historical data.
- Automated Trading: Executes trades via Alpaca’s API (paper trading for testing).
- Composite Signal Calculation: Combines RSI, MACD, and SMA signals using configurable weights.
- Reinforcement Learning–Style Parameter Tuning: Automatically adjusts trading parameters based on recent trade performance.
- End-of-Day Watchlist Discovery: Updates the watchlist at market close, retaining only the top-performing stocks.
- Web Dashboard: Hosts a Flask-based dashboard to view current strategy settings, trade logs, and asset signals.
- Python 3.11+
- alpaca-trade-api
- pandas
- numpy
- Flask
- python-dotenv
- yfinance
-
Clone the repository:
git clone https://github.com/ProgramingPro2/Reinforcement-Stocks.git cd Reinforcement-Stocks -
Create a virtual environment and install packages:
Linux:
python3 -m venv venv source venv/bin/activate pip install alpaca_trade_api pandas numpy flask python-dotenv yfinanceWindows:
python3 -m venv venv venv\Scripts\activate pip install alpaca_trade_api pandas numpy flask python-dotenv yfinance
-
Start script:
python main.py
or in debug mode:
python main.py --debug
Environment Variables: Create a .env file in the project root and add your Alpaca API credentials:
ALPACA_KEY=your_api_key_here
ALPACA_SECRET=your_api_secret_here
ENDPOINT=https://paper-api.alpaca.marketsAdjust the ORDER_AMOUNT (dollar amount per trade) and other strategy parameters in the data.json file or main.py as needed.
Create a stocks.json file in the working directory containing a list of stock tickers. The following characters are not valid, (){}[].,^$@! For example:
["AAPL", "MSFT", "GOOGL", "AMZN", "TSLA"]to automatically verify your file run:
python stocks_file_verifier.pyRun the trading bot with the debug flag (optional):
python main.py --debugWhen you run the script, it will:
Retrieve near real-time historical data via yfinance.
Compute composite signals based on RSI, MACD, and SMA.
Execute trades on Alpaca’s paper trading endpoint (ensure your API credentials are set).
Automatically adjust strategy parameters over time.
Launch a flask dashboard you can access http://0.0.0.0:5000.
This project is for educational purposes only. Trading involves risk, and you should always perform thorough testing using paper trading before using any strategy with real capital. The author is not responsible for any losses incurred.
Contributions, issues, and feature requests are welcome! Please check the issues page for open problems or to suggest enhancements.