GithubHelp home page GithubHelp logo

costajob / stock_prices Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 1.0 136 KB

A simple collector, visualiser and forecaster program for closing stock prices written in Python.

License: MIT License

Dockerfile 0.04% Python 2.63% HTML 97.33%

stock_prices's Introduction

Table of Contents

Scope

This is the implementation of the python code kata Data Analysis and Visualisation for financial stock data.

Requirements

Versions

The library is compatible and it has been tested with the following python versions:

  • 3.4.8

  • 3.6.4

  • 3.7.1

Python 2

Support for old python 2 is not guaranteed, since some components makes use of the concurrent.futures introduced on standard library since version 3.2.

Chart

The chart.js library is used in order to plot data into a non-static chart.
The minimized script is loaded by an external CDN, thus requiring a network connection to properly works.

Footprint

To grant resiliency (and courtesy of the Python's broad standard library) the external dependencies footprint is kept to a minimum:

  • Flask: a WSGI-compliant HTTP micro-framework, required by objectives

  • Gunicorn: a pre-fork WSGI-compliant HTTP server, used to distribute the load among available cores

  • Meinheld: a WSGI-compliant Web server, based on green threads and asynchronous network I/O to reduce latency

  • BeautifulSoup: a HTML parser library aimed at screen-scraping

Design

SRP

The code design follows the single responsibility principle by using a dedicated class for any specific task. Each class is confined within meaningful modules:

  • data: data related objects, such as Fetcher, Parser and Downloader

  • entity: plain value objects, such as Stock

  • computer: computation logic objects, such as Forecaster and Global

Data

The stock closing data are fetched by remote HTML documents. The fetching is quite inefficient, since the whole document need to be downloaded and scanned in order to collect just last month prices, but the objectives do not advise of any available APIs.
HTML documents are fetched once at server start and cached at ./cache/corn.html (etc.) to avoid further network latency. Just delete the cached HTML documents and restart gunicorn to fetch fresh copies.

Tests

The library is covered, by fast, isolated unit and doc testing (the latter to grant reliable documentation):

python -m unittest discover -s stockp -p '*'
..........
----------------------------------------------------------------------
Ran 10 tests in 0.166s

OK

Usage

Depending on your python version use the appropriate binaries available on your workstation:

Installation

Install the external dependencies via pip:

pip install -r requirements.txt

Start Server

Development

To start the WSGI server on development just execute the main.py file with the python binary:

python main.py

Production

To start the server on production use the gunicorn executable by spawning as many workers as you need and by specifying the HTTP port:

gunicorn -w 4 -k meinheld.gmeinheld.MeinheldWorker -b :8888 main:app

Docker

This application can be built and deployed as a Docker container by relying on the python:3.7.1 official image:

docker build -t stockp .

Once the container has been built, just run it by:

docker run -d -p 8888:8888 stockp

API

The library exposes a single HTTP endpoint at port 8888 (or at the port you bound at server start), which renders a HTML chart representation of last month stock prices and their forecast for Nasdaq, Corn and Gasoline stocks:

Performance

Courtesy of the gunicorn and meinheld it is possible to squeeze decent throughput by stressing the server via the wrk tool:

wrk -t 4 -c 100 -d30s --timeout 2000 http://127.0.0.1:8888
Running 30s test @ http://127.0.0.1:8888
  4 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    11.10ms    3.62ms  51.38ms   75.78%
    Req/Sec     2.27k   268.19     2.85k    68.08%
  271111 requests in 30.01s, 584.07MB read
Requests/sec:   9034.28
Transfer/sec:     19.46MB

stock_prices's People

Contributors

costajob avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

mkcosta

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.