Skip to content

Latest commit

 

History

History
228 lines (181 loc) · 5.25 KB

README.md

File metadata and controls

228 lines (181 loc) · 5.25 KB

stocklerts: your personal stock price tracker

A Python-based stock alerting service that monitors prices (One symbol/sec) using Finnhub API and sends push notifications via Alertzy based on user-defined thresholds.

Features

  • Monitor multiple stock tickers with percentage-based alert thresholds.
  • Send push notifications to configured devices using Alertzy.
  • Automatically pull the code and restart the server when the repository updates.
  • Free service for gail residents, add symbols and your encrypted alertzy account id (check below on how to encrypt) in config.yaml file to get started

Setup

Prerequisites

  • Python 3.7+
  • Poetry
  • Docker/Docker Compose

Installation

  1. Clone the Repository

    git clone https://github.com/vinaykudari/stocklerts.git
    cd stocklerts
  2. Install Dependencies

    poetry install
  3. Configure Environment Variables

    Register at finnhub and set the key:

    export FINNHUB_API_KEY="<your-finnhub-api-key>"
  • Gail residents use our wifi passsword twice <password><password> to encrypt your account id

  • Encrypt your Alertz account id at devglan.com using the exact same settings and set the passcode as env var

    img.png

        export ENCRYPT_KEY="<encrypt-key>"
  1. Configure Application

    Install Alerty app and copy the account id and update config/config.yaml with your desired settings:

    defaults:
     cooldown_period_minutes: 60
     max_notifications_per_day: 100
     max_quote_calls_per_min: 60
    
    alertzy:
     accounts:
       - user_id: 1
         account_id: <encrypted_account_id>
    
       - user_id: 2
         account_id: 18tu6LkU4y9uNArpNlAyog==
    
    tickers:
     - symbol: AAPL
       threshold:
           - value: 5
             users:
               - 1
           - value: 0
             users:
               - 2
    
     - symbol: MSFT
       threshold:
         - value: 5
           users:
             - 1
    
     - symbol: GOOGL
       threshold:
         - value: 0
           users:
             - 1
    
  2. Run the application

  • Install the dependencies from webhook_handler_reqs.txt
  • Start the webhook handler (Check at the end to set this as system service)
gunicorn -b 0.0.0.0:5005 webhook_handler:app
  • Start the app server
poetry run python -m app.main
  • Alternatively, use docker
docker compose build 
# make sure you sent the env vars before you run this
docker compose up

CI/CD setup to restart the server when the codebase updates

Setup webhook_handler as system service

  1. Setup webhook in github repository

  2. Install venv for your python version

sudo apt install python3.10-venv
  1. Create a venv in the directory
python3 -m venv venv
  1. Update the permissions
sudo chown -R <username>:<username> /path/to/project/venv
  1. Create a system service to run the server on boot as daemon
sudo vim /etc/systemd/system/stocklerts_webhook_handler.service
[Unit]
Description=Stocklerts Webhook Handler
After=network.target

[Service]
Type=simple
User=dexter
Group=dexter
WorkingDirectory=/path/to/project
Environment="GH_WEBHOOK_SECRET=<webhhok-secret-you-setup-in-github>"
Environment="ENCRYPT_KEY=<encrypt-key>"
Environment="FINNHUB_API_KEY=<your-finnhub-api-key>"
Environment=PATH=/path/to/project/venv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
ExecStartPre=/usr/bin/python3 -m venv venv
ExecStartPre=/path/to/project/venv/bin/pip install -r /path/to/project/webhook_handler_reqs.txt
ExecStart=/path/to/project/venv/bin/gunicorn --bind 0.0.0.0:5005 webhook_handler:app
Restart=on-failure

[Install]
WantedBy=multi-user.target
  • Update the service
sudo systemctl daemon-reload
  • Start the service
sudo systemctl start stocklerts_webhook_handler.service
  • Enable the service to start at boot
sudo systemctl enable stocklerts_webhook_handler
  • Monitor logs
journalctl -u stocklerts_webhook_handler.service -f

Tunnel webhook_handler using ngrok

  1. Setup ngrok account and add your config to the file
  • Create ngrok config file in home directory
vim ~/ngrok.yml
authtoken: <auth-token>
version: 1
tunnels:
  stocklerts:
    proto: http
    addr: 5005
    hostname: <your-static-domain>
  1. Create a system service to run the server on boot as daemon
sudo vim /etc/systemd/system/ngrok_tunnel.service
[Unit]
Description=Ngrok Tunnel for Stocklerts
After=network.target

[Service]
ExecStart=/snap/bin/ngrok start --config /home/<username>/ngrok.yml stocklerts
Restart=on-failure
User=<username>

[Install]
WantedBy=multi-user.target
  • Update the service
sudo systemctl daemon-reload
  • Start the service
sudo systemctl start ngrok_tunnel.service
  • Enable the service to start at boot
sudo systemctl enable ngrok_tunnel