Skip to content

MitulMistry/paper-trader

Repository files navigation

Paper Trader

A paper trading web application to practice trading stocks, built with Flask.

screenshot

Demo App

You can see a demo version of the application deployed to Heroku here: https://paper-trader-py.fly.dev/

Functionality

Users can create an account and search for stock quotes for companies as well as get recent news articles regarding the stock. Users can practice buying and selling stocks based on a set amount of money they define at account creation and track their hypothetical gains and losses.

Users can see their holdings on a stock's quote page if they own shares in it. There is a portfolio page where users can see their collection of holdings and compare their average stock purchase prices with current stock values as well as easily buy and sell them. Users can add (pretend) money to their account or reset their account to a blank state. There is also a history page where users can see all their past transactions.

Install Instructions

The application depends on Python, which can be installed and managed a variety of ways. For this project, I used pyenv and pyenv-virtualenv, following this guide.

Once Python is installed, install Flask and dependencies using the command: pip install -r requirements.txt or python -m pip install -r requirements.txt.

An API key for News API is required to run the application. It can be acquired from the previous link. Stock data is queried from the Yahoo Finance API which does not require a key. You also need to set up a PostgreSQL database.

To run the application in development mode, configure Flask by entering the following commands in the application directory:

export FLASK_APP=app.py
export FLASK_ENV=development
export SECRET_KEY=""
export DATABASE_URL=""
export NEWS_API_KEY=""

Insert the url for the Postgresql database in the quotes after DATABASE_URL. Generate a secret key using a tool of your choice and insert it. Insert the News API key in its respective spot as well (after registering for an account).

Run migrations with the command flask db upgrade or python -m flask db upgrade. Then run either flask run or python -m flask run to start the development server.

For production, the application uses Gunicorn for the server as defined in the Procfile (for Fly.io deployment).

Project Structure

app.py - Application (controller) logic is defined here. All routes are processed and requests are responded to.

config.py - Configuration settings for different environments are stored here (development, production, etc.).

models.py - Models are defined here using SQLAlchemy as the ORM. This forms the basis for the database migrations.

helpers.py - Helper functions (like implementation of API calls) are defined here. These functions are called in app.py or in templates.

/templates - This directory contains all the templates (views) used by app.py to produce HTML responses to requests (with the help of Jinja templating).

/migrations - This directory contains the necessary files to migrate schema from the models file to the PostgreSQL database using Flask-Migrate (which in turn uses Alembic).

requirements.txt - This file is used by Python to keep track of the application's dependencies.

Procfile - Configuration for Heroku production deployment is stored here (which is set up to use Gunicorn).

.env - You can store environment variables like FLASK_APP, FLASK_ENV, DATABASE_URL, NEWS_API_KEY here (not commited to Git). If doing so, it needs to be configured using Python-dotenv or direnv.

More Info

This application began as the final project for Harvard's CS50x Computer Science course: https://cs50.harvard.edu/x/2021/project/, inspired by CS50 Finance.

License

This project is open source under the terms of the MIT License.

About

A paper trading web application to practice trading stocks, built with Flask

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published