Coder Social home page Coder Social logo

python_assignment's Introduction

Project Description

This project provides simple APIs to query the stock price trends and statistics of a given company.

Table of contents

Tech Stack

Project is created with:

  • Git: Version control
  • Docker: Package and deploy application
  • Flask: API Service
  • SqlAlchemy + MySQL: Data persistence
python_assignment
│  docker-compose.yml
│  Dockerfile                  # dockerfile for api service
│  get_raw_data.py             # get raw financial data and store them in database
│  README.md
│  README_N.md
│  requirements.txt            # dependencies
│  schema.sql                  # sql statement for create financial_data table
│
└─financial
    │  app.py                  # flask app entrance
    │  __init__.py
    │
    ├─api                      # api entrance
    │      financial_data.py   # financial data query api
    │      statistics.py       # financial statistics query api
    │      util.py             # error handling decorator
    │      __init__.py
    │
    ├─mysql                    # database abstraction for python
    │      config.py           # mysql config file
    │      engine.py           # sqlalchemy engine
    │      __init__.py
    │
    └─test                     # tests for apis
            financial_data_test.py # tests for functions in financial_data module
            statistics_test.py     # tests for functions in statistics module
            __init__.py

Dependencies

requests     # requests is one of most popular python libraries to send http requests, so we choose to use it. 
flask        # Light weighted and popular web framework
PyMySQL      # Dependency of sqlalchemy
cryptography # Used for mysql connection
sqlalchemy   # Efficient and high-performing python database toolkit. We can use it to connect mysql and execute sql statements.

Usage

  1. Launch the api service and mysql in docker. The database and table used to store stock price information will be automatically created.
$ docker-compose up           
  1. After launch, the database table is empty, so we should retrieve some stock data from API provided by AlphaVantage and store them in local db.
$ python get_raw_data.py       
  1. Then, we can query data through APIs.
# HOST: http://localhost:5000

# Financial data API
# path: /api/financial_data
# method: GET
# request:
# {
#     "start_date": str,  # e.g., "2023-01-01", optional
#     "end_date": str,    # e.g., "2023-05-14", optional
#     "symbol": str,      # e.g., "IBM", optional
#     "limit": int,       # e.g., 3
#     "page": int         # e.g., 2
# }
# response:
# {
#     "data": [
#         {
#             "symbol": str,
#             "date": str,
#             "open_price": str,
#             "close_price": str,
#             "volume": str,
#         }
#     ],
#     "pagination": {
#         "count": int,
#         "page": int,
#         "limit": int,
#         "pages": int
#     },
#     "info": {'error': ''}
# }
# example:
curl -X GET 'http://localhost:5000/api/financial_data?start_date=2023-01-01&end_date=2023-05-14&symbol=IBM&limit=3&page=2'


# Financial statistics API
# path: /api/statistics
# method: GET
# request:
# {
#     "start_date": str,  # e.g., "2023-01-01"
#     "end_date": str,    # e.g., "2023-05-14"
#     "symbol": str,      # e.g., "IBM"
# }
# response:
# {
#     "data": [
#         {
#           "start_date": str,
#           "end_date": str,
#           "symbol": str,
#           "average_daily_open_price": float,
#           "average_daily_close_price": float,
#           "average_daily_volume": int
#         }
#     ],
#     "pagination": {
#         "count": int,
#         "page": int,
#         "limit": int,
#         "pages": int
#     },
#     "info": {'error': ''}
# }
# example:
curl -X GET 'http://localhost:5000/api/statistics?start_date=2023-01-01&end_date=2023-05-31&symbol=IBM'

Discussion

API key storage method:

Local development

Directly hard-coded in the python file.

Production environment

python_assignment's People

Contributors

wychootf4 avatar lsc1205 avatar

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.