Platform Stonks: Building the Foundations

GITHUB: Platform_Stonks

The Background

I have wanted to create a stock analysis site for a long time but kept putting it off for various reasons. However, I finally decided to dive in and attempt to build my first deployable stock analysis platform. Platform Stonks had been on paper for a long time, and it was finally time to bring it onto the screen.

I have always had an interest in the stock market, going back to school, and continued learning about it in my spare time at university. The volume of data passing through stock exchanges every day is incredible, and I realised it would give me a large amount of information to work with while creating interesting challenges around how I store, process and use that data across the platform.

The premise of this project is to learn different concepts and broaden my understanding of how systems are delivered end to end. Having spent six years working on the business side of technology, I have performed requirements analysis and produced project documentation countless times. With this project, however, I wanted to dive into the technical side and understand how a development team might approach delivering a system like this.

Setting Goals and Requirements

I set multiple goals for V1 and trimmed several of my larger ideas into later releases. I sometimes allow my imagination to run away with me and start trying to solve the larger problem before defining reasonable steps to get there.

The key goals I wanted to achieve with this project were:

  • Run all services using Docker

  • Create my own APIs to connect the frontend and backend

  • Build a database that stores the platform's data locally

  • Create a daily job that automatically retrieves data from an external source and stores it

  • Build a basic frontend capable of displaying the data

  • Implement basic user login and administrative functionality

These six goals became my main objectives for V1. To achieve them, I added further requirements, including API authentication and authorisation to control which functionality is available to different users.

I also wanted to make sure my database followed relational database design and normalisation principles. This would give me a solid foundation for later development involving more advanced SQL concepts such as window functions, Common Table Expressions (CTEs) and aggregate functions, with the possibility of exploring vector databases much further down the line.

The Bigger Vision

As I mentioned earlier, I want this project to evolve over time and push the limits of what I can deliver as software. It also gives me somewhere to experiment with technologies and concepts as I learn them.

I want to containerise each component so that the platform can eventually run on my own homelab, with Terraform and Ansible used to explore Infrastructure as Code (IaC) and automated configuration management.

Beyond simply knowing how to call and test APIs, I want to improve my understanding of how APIs are designed and used within larger applications. I want to explore concepts such as API wrappers, service layers, repositories and API clients. Building these foundations will also prepare the platform for the eventual introduction of an AI service, where I will need to think carefully about what functionality the model can access and how that access can be provided safely and securely.

Another major goal is to sharpen my SQL skills. Working with the large amount of data generated by financial markets should give me plenty of opportunities to develop more advanced queries and database concepts. I also want to explore database migrations, cloning production data into development environments, sharding and primary-standby configurations, all of which will give me a better understanding of how databases are managed as systems grow.

Eventually, I could even separate the database infrastructure within my homelab and run dedicated database services that other applications communicate with. That is something for a much later stage of the project.

Later in this series, I will discuss why I chose to containerise the different components separately and how this supports the future direction of Platform Stonks.

The Beginning: Database and CSV Upload

Once I began working on the project, I thought the most logical place to start was with the company and price tables, as I first needed somewhere to store the data. I was using CSV files from a provider I already used, so I initially based the table structure on the data available within those files.

I selected PostgreSQL because it is the relational database I am most familiar with from university and previous experimentation. It is also widely used in real-world applications, which aligns with my goal of using technologies across my projects that I am likely to encounter professionally.

I created two initial tables:

Company: Code*, Company Name, Exchange, Active

Price: Code*, Date*, Open, High, Low, Close, Dollar Change, Percentage Change, Volume

* Indicates a field used in the primary key.

I separated the company information from the price information to avoid repeating information such as the company name for every price record. This also gives me the flexibility to expand either table independently as the platform develops.

The company table currently uses the company code as its primary key because each company within the exchange I am currently working with has a unique code.

For the price table, I used a composite primary key consisting of the company code and date. A company should only have one price record for a given trading day, so the combination of these fields gives each price record a unique identifier without requiring an additional ID column.

However, there is a limitation with this design.

Using the company code as a unique identifier works while I am dealing with a single exchange. Once I introduce additional exchanges, two companies could potentially use the same ticker code. At that point, the current key design could create conflicts.

As I am currently working exclusively with the Australian stock market, I decided to keep the simpler design for V1. This is something I already know I will need to revisit when multi-exchange support is introduced.

After creating the SQL schema, I successfully had PostgreSQL running inside a Docker container. I attached a persistent Docker volume so that the database would retain its data when containers were restarted or my computer was shut down.

The next logical step was getting the data from my CSV files into the database.

Loading Data

My first decision was how the initial data should be loaded.

Do I download all the CSV files I want and use PostgreSQL's initialisation functionality to populate the tables when the database starts? Or do I build a function capable of loading CSV data while the database is already running?

I decided the second option made more sense. I could download CSV files from my source, store them locally and run an ingestion function whenever I needed to load them. This meant I wouldn't need to purge the database volume and recreate the database whenever I wanted to introduce additional data.

I got to work building the function, which gave me a useful refresher on handling file paths and performing small data transformations to align the incoming column names with my database schema.

I also introduced some basic data validation. Before loading anything into PostgreSQL, the function checks that the expected columns are present, that unexpected columns haven't been introduced and that required values aren't missing. If the validation fails, the data isn't inserted.

I also made sure the insertion logic would not create duplicate records. If a company already exists, it isn't inserted again. The same applies to price records where the combination of company code and date already exists. I handled these conflicts using PostgreSQL's ON CONFLICT DO NOTHING.

After debugging the function and adding some simple development logging, I had a working command-line process capable of validating a CSV file and loading its contents into the database.

While building this, I ran into one of my first architectural problems.

PostgreSQL was running inside Docker while my Python ingestion code was running directly on the host machine. I therefore had to configure the database connection correctly so that the local application could communicate with the containerised database. This was handled through the database URL stored in my environment configuration.

Although this worked, it also made me reconsider how I wanted the application to run. I created another Dockerfile for the ingestion service, added it to my Docker Compose configuration and ran it alongside PostgreSQL. This moved the project closer to the containerised architecture I wanted for V1.

This type of development environment issue appeared several times throughout the project because I don't yet have a dedicated development environment. It is one of the reasons I have started planning my homelab, which will eventually provide isolated development and production-like environments for projects such as Platform Stonks. I'll cover that separately in a future post.

At this stage, I was working towards the following architecture for V1:

With the database running, data validation in place and an ingestion process capable of loading market data, I had the foundations of Platform Stonks working. The next challenge was figuring out how the rest of the application should interact with that data.

In Part 2, I'll cover the introduction of the API layer, retrieving market data from external sources and building the frontend that finally put the data onto the screen.

Previous
Previous

Why I Didn't Build My Own Website

Next
Next

Building a Network Scanner with Python