RESTful API for Stock Portfolio Management and Tax Calculation
A few years ago, I created a script to help me fill in my HMRC tax returns for stock operation gains and losses (mainly losses). Seeking a project to showcase my Django skills, I decided to replicate and enhance the existing functionality using Django. Although the initial purpose did not require databases, APIs, or other advanced features, this project demonstrates my ability to build a robust and scalable solution.
Given a list of stock operations within a portfolio account, this API calculates the profits and losses for the account over a specified period.
- Currency conversion to GBP (as this is for HMRC) to operation amounts.
- Apply stocks splits to buy and sells when calculating profits.
- Create deployment pipeline.
- Add authorization.
- Add parameter to request to get account total details to specify in which currency the profits should be displayed.
Use Poetry, a package dependency manager, to install project dependencies:
poetry install
See pyproject.toml
for dependencies.
Application is setup to use a Postgres database. Change settings.py
to set the database of your liking or make sure Postgres database is installed, service is running and database created.
Package psycopg2-binary
is installed to interface with Postgres
poetry add psycopg2-binary
Installing Postgres in Ubuntu
sudo apt install postgresql postgresql-contrib libpq-dev
Start PostgreSQL service
sudo service postgresql start
Login to Postgres CLI
sudo -u postgres psql
and create the database
CREATE DATABASE portfoliodb;
With the application activated, run Django scripts to create database migrations. It will create tables based on models.py
file.
python manage.py makemigrations
python manage.py migrate
python manage.py runserver
Browser to Url https://127.0.0.1:8000/profits/
to see the list of endpoints.
To execute Django from VS Code follow this configuration example. Otherwise run from command line as in command below.
From bash command line execute command below pointing to local environment:
ENV_PATH=env/.env.local pytest
or for other environments
ENV_PATH=env/.env.development pytest
ENV_PATH=env/.env.production pytest
If get test errors connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refuse
make sure that the database is running.
sudo service postgresql status
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.