Stock Market Simulator Application
For my final project, I wanted to do something that showcased the skills that I’ve acquired throughout this program and also showcase my capability of tackling big projects and adapting.
The interest in stock trading has become a hot topic within the past few years. With applications like Robinhood and Coinbase, it’s easier than ever for people to jump right in and start trading. But platforms like these can come with some big risks. Users are using their own funds, so if they’re not balancing their portfolio well, it can lead to some big losses. So I created a basic simulator that could give someone the tools needed to learn about how the stock market operates, without the risk. My application was created using Rails on the backend and React on the frontend.
I needed to find an external API that I could use to get real-time stock data, pull that data to be stored on my database and keep it up-to-date so that users could get real-time data on how their stocks are doing. On top of that, I also needed to figure out how to integrate calculations, so that users could buy, sell, keep track of their gains, losses, and the average cost of their stock.
I started out using a stock_quote gem that used IEXCLOUD.IO API. Everything was going fine until I started building the chart aspect of my application. I, unfortunately, didn’t know that making a historical-price request would use up to 10% of my credits for each request. Oops. Because of this, I decided to split it up. I used IEXCLOUD for getting stock quotes and Alpha Vantage for creating the charts for each stock.
I integrated these APIs into my backend for two reasons. First, I know that is best practice to keep your API keys secret. And the best way to do that is to make requests to your backend, and not on the client-side. Second, I wanted to make a single call to my backend, to get the data I need on the frontend, so that all the “behind the scenes” action stays there. Freeing up the frontend to just display the data.
As I was building my app, I noticed my code was getting messy. I was using the APIs in a few different routes and having to repeat a lot of the same code. So to DRY up my code. I created a module that held my API request that I could call on when I needed them.
I created my own API to store users and their saved stocks. In my previous project, I used JWT token Auth already. So I wanted to practice using a different type of auth. This time, when a user logs in and the credentials are verified, the tokens are stored in the sessions, and when they log out, the session is destroyed. I also implemented authorization so that a user can trade stocks and save stocks to their watchlist if they are logged in, but if someone is not a site user, they can still use the search feature of the application.
I also created class methods that can be called upon, when a user trades a stock. When a user purchases a stock the buy stock method is called, sums up the new shares with the user’s existing shares, calculates the average cost per share and the total cost of the purchase.
When a user sells a stock, the sell_stock method is called subtracts the sold shares from the user, sets the cost to 0 if the user is selling off all their shares, and recalculates the average cost and total cost.
I really put my skills to the test when building this application. I also gained some new ones. You can see my full code here if you are interested in seeing my final product!