Skip to content

acoalex/stock-manager

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Stock Price Monitor and LLM Agent

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.

Features

  • 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 .env file.
  • Extensible: The project is structured to be easily extensible with new features.

Project Structure

/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

Getting Started

Prerequisites

  • 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)

Installation

  1. Clone the repository:

    git clone <repository-url>
    cd stock-manager
  2. Create and activate the virtual environment:

    python3 -m venv venv
    source venv/bin/activate
  3. Install the dependencies:

    pip install -r requirements.txt

Configuration

  1. Create a .env file by copying the example:

    cp .env.example .env
  2. Edit the .env file 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"]
    }
    

Usage

To run the application, execute the following command:

python main.py

The application will start the scheduler. The scheduler is configured to fetch stock prices, generate recommendations, and send an email at specific times.

Scheduler

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.

Running in Production

For a production environment, it's recommended to run the application as a background service.

1. Manage Environment Variables

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.).

2. Run as a Background Service

To ensure the application runs continuously, you can set it up as a systemd service.

  1. Create a service file:

    sudo nano /etc/systemd/system/stock-monitor.service
  2. 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
  3. 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.

About

Stock manager script for getting info of the specified stock options and send an email with the SELL/BUY recomendations

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors