Skip to content

Latest commit

 

History

History
231 lines (162 loc) · 5.59 KB

README.md

File metadata and controls

231 lines (162 loc) · 5.59 KB

Stock Monitor

This project demonstrates some aspects of working with real-time time series (stock tickers) data.

There are two ways to run server: as a Docker container (simplest) or locally in current environment.

Features

  • Fetch ticker prices from third-party service in realtime
  • Cache requested data
  • Execute queries on historical data
  • Supported data providers: IEX

Running as Docker container (on Ubuntu Linux)

Requirements
  • Docker >= 18.03
    sudo su
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
    add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
    apt update
    apt install -y docker-ce
    curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    chmod +x /usr/local/bin/docker-compose
    usermod -aG docker $USER
Clone repository
git clone https://github.com/kirgene/stock-monitor.git
cd stock-monitor
Build and run Docker image
docker-compose up -d

Note: if you receive error like Couldn't connect to Docker daemon try to reboot your system.

Access server:
http://localhost:8000

Running Locally (on Ubuntu Linux)

Requirements
Setup PostgreSQL database:
CREATE DATABASE stock_db;
CREATE USER stock_admin WITH PASSWORD 'stock_pass';
GRANT ALL PRIVILEGES ON DATABASE stock_db TO stock_admin;
Setup server:
git clone https://github.com/kirgene/stock-monitor.git
cd stock-monitor
npm install
Configurations

Copy .env.example template to .env and modify according to your environment

cp .env.example .env
Prepare database
npm run migrate
Run server
  • Development mode:
    npm run start-dev
  • Production mode:
    npm run build
    npm start
Access server:
http://localhost:8000

API Description

  • Success Response:

    • Code: 200
      Content: { data: [{...}, {...}, ...] }
  • Error Response:

    • Code: 404 NOT FOUND
      Content: { errors : ["...", "....", ...] }

List Stocks

Return available stocks.

  • URL

    /stocks

  • Method:

    GET

  • URL Params

    Field Required Data Type Description Examples
    name JSON string array Case insensitive filter on stock symbols and company names with wildcard * matching support ["ab*","f*p"], ["abc"]
  • Response:

    Object key Value Type Description Examples
    id integer Item ID 23, 35
    name string Company name Intel Corporation, Alphabet Inc.
    symbol string Stock symbol INTC, GOOG

List Stocks Prices

Return stock prices using different filters.

  • URL

    /prices

  • Method:

    GET

  • URL Params

    Field Data Type Description Examples
    name JSON string array Case insensitive filter on stock symbols and company names with wildcard * matching support ["ab*","f*p"], ["abc"]
    start ISO 8601 date Start datetime 2019-06-27T14:00:00, 2019-06-27T16:00:00
    end ISO 8601 date End datetime 2019-06-27T14:00:00, 2019-06-27T16:00:00
    high float List stocks with prices below this value (high water mark) 150.40, 600
    low float List stocks with prices above this value (low water mark) 10.25, 50

NOTICE: specifying start and end params on a fresh database will lead to downloading historical data for a given time range and the amount of that data can be HUGE ( ~ 700 - 900 MB for a single day).
The progress and estimated completion time of this processing are displayed in real-time in logs.

  • Response:

    Object key Value Type Description Examples
    id integer Item ID 23, 35
    name string Company name Intel Corporation, Alphabet Inc.
    symbol string Stock symbol INTC, GOOG
    price float Stock price 150.40, 600
    time ISO 8601 Timestamp 2019-06-26T15:33:46.812Z

Get Latest Stocks Prices

Subscribe for latest stocks prices

  • URL

    /latest-prices

  • Method:

    WebSocket (ws://)

  • Request Message

    Object Key Data Type Description Examples
    type string Use subscribe/unsubscribe to subscribe/unsubscribe for/from a given stock ["ab*","f*p"], ["abc"]
    name JSON string array Case insensitive filter on stock symbols and company names with wildcard * matching support ["ab*","f*p"], ["abc"]
  • Response:

    Refer to Response section of List Stocks Prices API.


Demo

  • Show latest prices for single company:

    http://localhost:8000/demo/?name=["baba"]
    
  • Show latest prices for multiple companies:

    http://localhost:8000/demo/?name=["baba", "v"]
    
  • Show prices for a given time range (from 12:00 to 18:10 of March 20, 2019):

    http://localhost:8000/demo/?name=["BABA"]&start=2019-03-20T12:00:00&end=2019-03-20T18:10:00
    
  • Show prices for a given time range with high ($180.5) and low ($180) price filter:

    http://localhost:8000/demo/?name=["BABA"]&start=2019-03-20T12:00:00&end=2019-03-20T18:10:00&low=180&high=180.5