This project monitors the stock prices of specified companies, stores the data in a local SQLite database, and uses an LLM agent to provide buy/sell/hold recommendations based on historical data.
- Stock Price Monitoring: Fetches real-time stock prices using the Finnhub API.
- Data Storage: Stores historical price data in a local SQLite database using SQLAlchemy.
- LLM Integration: Interacts with an LLM (e.g., ChatGPT) to get stock recommendations.
- Email Notifications: Sends email notifications with the LLM recommendations.
- Scheduler: Runs automatically at configured times.
- Configurable: Easily configure API keys, stock symbols, and other settings via a
.envfile. - Extensible: The project is structured to be easily extensible with new features.
/Users/alex/workspace/stock-manager
├── .env
├── .gitignore
├── main.py
├── README.md
├── requirements.txt
├── src
│ ├── config
│ │ └── settings.py
│ ├── db
│ │ └── database.py
│ ├── main.py
│ ├── models
│ │ └── stock_price.py
│ └── services
│ ├── email_service.py
│ ├── finnhub_service.py
│ └── llm_service.py
├── tests
└── venv
- Python 3.11
- A Finnhub API key
- An OpenAI API key (or another LLM provider)
- Email credentials for sending notifications (e.g., Gmail App Password)
-
Clone the repository:
git clone <repository-url> cd stock-manager
-
Create and activate the virtual environment:
python3 -m venv venv source venv/bin/activate -
Install the dependencies:
pip install -r requirements.txt
-
Create a
.envfile by copying the example:cp .env.example .env
-
Edit the
.envfile with your API keys, desired stock symbols, and email settings:# Finnhub API Key FINNHUB_API_KEY=your_finnhub_api_key # Stock symbols to monitor, comma-separated STOCK_SYMBOLS=AAPL,TSLA,MSFT # LLM Provider Settings LLM_PROVIDER=openai # (e.g., openai, gemini, deepseek) OPENAI_API_KEY=your_openai_api_key GEMINI_API_KEY=your_gemini_api_key DEEPSEEK_API_KEY=your_deepseek_api_key LLM_MODEL=gpt-3.5-turbo LLM_API_BASE_URL=https://api.openai.com/v1 # Optional, for custom API endpoints # Email Settings SMTP_SERVER=smtp.example.com SMTP_PORT=587 SMTP_USERNAME=your_email@example.com SMTP_PASSWORD=your_email_password SMTP_FROM=your_email@example.com RECIPIENT_EMAIL=recipient_email@example.com # Scheduler Settings (JSON format) SCHEDULER_CONFIG={ "monday": ["16:00", "21:00"], "tuesday": ["16:00", "21:00"], "wednesday": ["16:00", "21:00"], "thursday": ["16:00", "21:00"], "friday": ["16:00", "21:00"], "saturday": ["13:07"] }
To run the application, execute the following command:
python main.pyThe application will start the scheduler. The scheduler is configured to fetch stock prices, generate recommendations, and send an email at specific times.
The application uses the schedule library to run the fetch_and_store_prices function at specific times.
The schedule is configured in the .env file using the SCHEDULER_CONFIG variable. The configuration is a JSON object where the keys are the days of the week (in lowercase) and the values are a list of times in "HH:MM" format.
If SCHEDULER_CONFIG is not set in the .env file, a default schedule will be used.
The fetch_and_store_prices function fetches the latest stock prices, gets a recommendation from the LLM service, and sends an email with the recommendations.
For a production environment, it's recommended to run the application as a background service.
In a production environment, it's not recommended to use a .env file. Instead, you should use your hosting provider's method for setting environment variables (e.g., Docker environment variables, systemd service files, etc.).
To ensure the application runs continuously, you can set it up as a systemd service.
-
Create a service file:
sudo nano /etc/systemd/system/stock-monitor.service
-
Add the following content to the file, adjusting the paths as necessary:
[Unit] Description=Stock Monitor Service After=network.target [Service] User=your_user Group=your_group WorkingDirectory=/path/to/your/project ExecStart=/path/to/your/project/venv/bin/python /path/to/your/project/main.py Restart=always [Install] WantedBy=multi-user.target
-
Enable and start the service:
sudo systemctl enable stock-monitor sudo systemctl start stock-monitor
This setup provides a more stable and scalable environment for running the application in production.