/trading-app

Java-based web application which lets the user to trade in stocks

Primary LanguageJava

Introduction

This trading application is an online stock trading simulation REST API which can be used to create an account which would allow account holder to buy and sell stocks from Investor Exchange i.e IEX. Traders can withdraw money and/or deposit money into their account. They can also view latest quotes of any stock directly from this application.

This REST API can be used by front-end developers, mobile-app developers, and traders.

The architecture used here is based on microservices concept which is implemented using SpringBoot, IEX API and PSQL database.

Quick Start

CentOS 7, Docker and Java are to be installed prior to use this REST API.

Initial Setup

Create an account on https://iexcloud.io and get your iex_public_token

#put the following env var in ~/.bash_profile
export IEX_PUB_TOKEN='your_iex_pub_key'
export PSQL_PASSWORD="your password" 
export PSQL_USER=" psql user" 
export PSQL_HOST="psql host"
$ source ~/.bash_profile

Usage

  1. Start docker
    sudo systemctl start docker
  2. Download the source code.
    git clone https://github.com/saud-aslam/trading-app.git
  3. Build trading app
    sudo docker build -t trading-app .
  4. Build psql image
    cd psql/
    sudo docker build -t jrvs-psql .
  5. Initialize PostgreSQL and start SpringBoot application via bash start_up.sh PSQL_HOST PSQL_USER PSQL_PASSWORD
  6. Consume the API through your browser by: http://localhost:8080/swagger-ui.html
  7. Swagger

  8. Postman can also be used to execute requests. Open_API has to be imported using http://localhost:8080/v2/api-docs post-host

REST API usage

Swagger

Swagger is an open-source software framework backed by a large ecosystem of tools that helps developers design, build, document, and consume RESTful web services. By visualizing the endpoints of the program, it allows user to access function and execute them with the help of interactive GUI.

Qoute Controller

Through this endpoint, IEX quotes are accessed through sending HTTP requests to the IEXCloud.

  • GET '/quote/dailyList' - List all quotes securities already in the database.
  • GET '/quote/iex/ticker/{ticker} - Input IEX symbol and shows IEX market data’s quotes information.
  • POST '/quote/ticker/{ticker} - Adds a new ticker to the Quote database
  • PUT '/quote/ - Allows user to manually insert quote data.
  • PUT '/quote/iexMarketData' - Updates all quotes which are in database.

Trader Controller

Provides trader to add or withdraw amount from his account. New account can be created or old one can be deleted.

  • DELETE '/trader/traderId/{traderId}' - Delete a trader with trader id provided that the trader has no account balance and no security in his account.
  • POST '/trader/ - Creates a trader with specified given details
  • PUT '/trader/deposit/traderId/{traderId}/amount/{amount} - Adds amount to trader’s account balance
  • PUT '/trader/withdraw/traderId/{traderId}/amount/{amount} - Deduct amount from trader’s account balance

Order Controller

A trader can buy or sell a stock by using this endpoint.

  • POST '/order/marketOrder' - Based on the input size, buying or selling of stock takes place. It the size is positive and account have enough money, then buying will get executed and if the size is negative and account has enough securities, then selling will occur.

Dashboard Controller

Shows trader his account and securties details.

  • GET '/dashboard/portfolio/traderId/{traderId}' -Gives the quotes and position of all the securities which the trader have.
  • GET '/dashboard/profile/traderId/{traderId}' - Shows the Trader and Account data from the database of the given trader_id.

App Controller

  • GET '/health' to see whether the SprinBoot app is running.

Architecture

    Arc

  • Controller - This layer is what the user interact with. Together with Swagger UI, the function of this layer is to invoke the service layer (in most cases) based on the input from the user. The request is translated and the response is retrieved back to this layer where it is shown to user in JSON format.

  • Service - This layer performs business logic and interacts with DAO layer. For example, validity of incoming Trader account details would be performed here before it being sent to TraderDao to be saved in the database.

  • DAO - This layer performs the create, read, update, and delete actions on the database.

  • SpringBoot: This framework is used to manage the application components i.e different layers together with Apache TomCat. Tomcat provides web servlet which maps HTTP request to appropriate methods inside the application.

  • PSQL Database - This is the database used to store our data in the tables and views which would be persisted here. The data can always be retrieved back whenever we want to.

  • IEX - IEX provides us REST API which we used to obtain stock information. By sending HTTP request to it, we get a response, which is parsed according to the needs.

Improvements

  • We have assumed that trader and account has one-one to relation. This can be improved by allowing trader to have multiple accounts.
  • Auto-increament ids once deleted can not be re-used. In some cases, it can be re-used in future implementation.
  • Auto-updation of dailylist is possible and can be implemented in future.
  • Email/sms alert can be set e.g when the price of specific security is changed.
  • Once the market is closed, a trader can not trade in stocks. This can be improved by saving the quote just before the market closes and used that to allow trader to trade and immediately execute their request when the market re-opens.