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, API calculates the profits and losses for the account over a specified period. If operations are in a currency different than GBP it converts the amounts to, as HMRC requires reporting in GBP.
- Apply stocks splits to buy and sells before calculating profits.
- Add authorization.
- Add logging
- Create deployment pipeline.
- Add parameter 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.
Command should execute tests on local environment:
pytest
or to target other environments:
ENV_PATH=env/.env.development pytest
ENV_PATH=env/.env.production pytest
If get test execution errors like 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
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
sudo apt update && sudo apt install unzip -y
unzip awscliv2.zip
sudo ./aws/install
Verify that installed correctly
aws --version
Follow the instructions in Terraform website
Verify that installed correctly
terraform -v
In the terraform
folder, execute command below to initialize the plugins for the provider (in this case AWS)
terraform init
Reviews the Terraform files and shows changes going to be applied to the infrastructure
terraform plan
Before applying the plan need to set AWS credentials with (specify --profile
option if have multiple AWS accounts. Ex. dev/stage/prod)
aws configure --profile <your-terraform-user>
Then you can create the resources in AWS
terraform apply
Remember to destroy the resources when not needed so don't incur unnecessary costs
terraform destroy
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.