Skip to content

Jackson-Wozniak/Stock-Market-Simulation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Stock-Market-Simulator

Explore The CodeΒ»

license license license license license

Github Β· Report Bug Β· Request Feature

πŸ“š Table of Contents

  1. Features
  2. Design Choices
  3. Local Deployment
  4. Contributing
  5. Api Documentation
  6. Results & Data From Simulations

πŸ““ Features & Overview

A server side implementation for a stock market and stock trading simulator. Users can create accounts and trade stocks using paper money with the goal of increasing their profits and outperforming other accounts, while keeping track of market activity to best plan for price trajectories

All stock prices, news, earnings etc. are completely simulated and are not reflective of real world market activity.

Default stock data, such as the name, ticker symbol, market cap and sector are based on real world companies


πŸ“: Design Choices

Microservices

I am currently in the process of migrating the current backend server to multiple microservices. The three microservices will be:

  1. Stock Market API
  2. Trading API
  3. Trading Bots

Here are the main justifications for doing this:

  • Learn microservices & gain experience building applications that talk to other services

  • Implement Spring Security, but isolate its features only to the trading API. The market is a publically available API, and so its endpoints do not require any authorization/authentication. Likewise, the trading bot service has no endpoints, and so requires minimal security measures, at least in the context of APIs. This means Spring Security is only relevant for 1/3 functions

  • Although this is a simulation, and there are no plans for deployment, the idea here is that each of the 3 services would be seperate "companies." The trading API acts a brokerage firm, the market API simulated a countries stock market, and the trading bot service acts a quant firm that interacts with its domestic market through publically-unavailable bots. Seperating the server into these 3 services means that it more closely follows the goal of having 3 seperate "companies" that all work together to build a stock market simulation


πŸ“: Contributing

For all info on contributing, please head to this document


✏️ Local Deployment

To run locally, first ensure that Docker Desktop & Maven is downloaded to your system. Then run the following commands:

  - git clone https://github.com/Jackson-Wozniak/Stock-Market-Simulation.git
  - cd Backend/
  - mvn clean package -DskipTests
  - cd ../
  - docker-compose up
  
  To update docker-compose after changes to code, run:
  - cd Backend/
  - mvn clean package -DskipTests
  - cd ../
  - docker-compose up --build
  
  To remove created containers after you are done, run:
  - docker-compose down

πŸ”Œ API Endpoints

(click to see details)

πŸ“† Market

  • The market date is tracked on intervals that can be altered. The default "day" is 24 intervals of 10 seconds, and 30 "days" is a month
  • The date is formatted as month/day/year
  • Stock prices change after each 10 second interval, and certain stock events can happen at the end of each day
  • There are 3 market types; Bear, Bull and Normal. Bear markets occur if the average stock price falls 10% in a month, while bull markets happen if prices rise 10% monthly. Normal market conditions cover all scenarios in between

➑️ Market Endpoints

Market Conditions: GET | http://localhost:8080/api/v1/market

{
  "id" : 1,
  "date" : "1/4/1",
  "lastMonthAveragePrice" : 100.0,
  "marketTrajectory" : "NORMAL"
}


πŸ’΅ Stocks

  • Stock prices change on an interval (10 seconds)
  • Stock prices change based on three factors: market cap, momentum, and volatility
  • Market Cap: Large and small cap stocks experience higher movement, in an upward or downward trajectory
  • Momentum: When stock prices rise for 3 days, they experience positive momentum, while they experience negative momentum if they fall for 3 days
  • Volatility: Each stock is judged on whether it is volatile or not. This is an unchangeable boolean value, and is based on the nature of the real world company. Volatile stocks receive a slight increase in movement each time their prices change

➑️ Stock Endpoints

Note: {___} in url represents path variable

Specific Stock By Name: GET | http://localhost:8080/api/v1/stocks/{ticker}

{
  "ticker" : "AAPL",
  "companyName" : "Apple",
  "sector" : "Technology",
  "marketCap" : "Large",
  "price" : 99.742,
  "lastDayPrice" : 99.457,
  "optimism" : -1,
  "volatileStock" : false
}

Specific Stock Price: GET | http://localhost:8080/api/v1/stocks/price/{ticker}

{
  104.36
}

All Stocks: GET | http://localhost:8080/api/v1/stocks The data below doesn't show all stocks, but shows the general format

[ {
  "ticker" : "AAPL",
  "companyName" : "Apple",
  "sector" : "Technology",
  "marketCap" : "Large",
  "price" : 104.2,
  "lastDayPrice" : 103.01,
  "momentum" : 1,
  "momentumStreakInDays" : 5,
  "volatileStock" : false,
  "newsHistory" : [ ]
}, {
  "ticker" : "AMZN",
  "companyName" : "Amazon",
  "sector" : "Technology",
  "marketCap" : "Large",
  "price" : 104.66,
  "lastDayPrice" : 105.63,
  "momentum" : 1,
  "momentumStreakInDays" : 3,
  "volatileStock" : true,
  "newsHistory" : [ ]
}, {
  "ticker" : "BRK.B",
  "companyName" : "Berkshire Hathaway",
  "sector" : "Insurance",
  "marketCap" : "Large",
  "price" : 100.62,
  "lastDayPrice" : 100.87,
  "momentum" : 1,
  "momentumStreakInDays" : 3,
  "volatileStock" : false,
  "newsHistory" : [ ]
}]

Detailed Stock Data: GET | http://localhost:8080/api/v1/stocks/detailed The data below doesn't show all stocks, but shows the general format

[ {
  "ticker" : "AAPL",
  "companyName" : "Apple",
  "sector" : "Technology",
  "marketCap" : "Large",
  "price" : 104.2,
  "lastDayPrice" : 103.01,
  "momentum" : 1,
  "momentumStreakInDays" : 5,
  "volatileStock" : false,
  "newsHistory" : [ ]
}, {
  "ticker" : "AMZN",
  "companyName" : "Amazon",
  "sector" : "Technology",
  "marketCap" : "Large",
  "price" : 104.66,
  "lastDayPrice" : 105.63,
  "momentum" : 1,
  "momentumStreakInDays" : 3,
  "volatileStock" : true,
  "newsHistory" : [ ]
}, {
  "ticker" : "BRK.B",
  "companyName" : "Berkshire Hathaway",
  "sector" : "Insurance",
  "marketCap" : "Large",
  "price" : 100.62,
  "lastDayPrice" : 100.87,
  "momentum" : 1,
  "momentumStreakInDays" : 3,
  "volatileStock" : false,
  "newsHistory" : [ ]
}]

Stocks By Sector: GET | http://localhost:8080/api/v1/stocks/sector/{sector}

[ {
  "ticker" : "AAPL",
  "companyName" : "Apple",
  "sector" : "Technology",
  "marketCap" : "Large",
  "price" : 103.94,
  "lastDayPrice" : 104.09,
  "momentum" : 1,
  "momentumStreakInDays" : 6,
  "volatileStock" : false,
  "newsHistory" : [ ]
}, {
  "ticker" : "AMZN",
  "companyName" : "Amazon",
  "sector" : "Technology",
  "marketCap" : "Large",
  "price" : 105.66,
  "lastDayPrice" : 105.29,
  "momentum" : 0,
  "momentumStreakInDays" : 2,
  "volatileStock" : true,
  "newsHistory" : [ ]
}, {
  "ticker" : "GME",
  "companyName" : "GameStop Corp",
  "sector" : "Technology",
  "marketCap" : "Mid",
  "price" : 20.32,
  "lastDayPrice" : 20.38,
  "momentum" : 1,
  "momentumStreakInDays" : 4,
  "volatileStock" : true,
  "newsHistory" : [ ]
}, {
  "ticker" : "GOOG",
  "companyName" : "Google",
  "sector" : "Technology",
  "marketCap" : "Large",
  "price" : 100.58,
  "lastDayPrice" : 100.58,
  "momentum" : 0,
  "momentumStreakInDays" : -2,
  "volatileStock" : false,
  "newsHistory" : [ ]
}, {
  "ticker" : "SLAB",
  "companyName" : "Silicon Laboratories",
  "sector" : "Technology",
  "marketCap" : "Mid",
  "price" : 19.69,
  "lastDayPrice" : 19.67,
  "momentum" : -2,
  "momentumStreakInDays" : -3,
  "volatileStock" : false,
  "newsHistory" : [ ]
}, {
  "ticker" : "TSLA",
  "companyName" : "Tesla",
  "sector" : "Technology",
  "marketCap" : "Large",
  "price" : 98.08,
  "lastDayPrice" : 98.24,
  "momentum" : -2,
  "momentumStreakInDays" : -4,
  "volatileStock" : true,
  "newsHistory" : [ ]
} ]

Stocks By Market Cap: GET | http://localhost:8080/api/v1/stocks/marketCap/{marketCap}

[ {
  "ticker" : "GME",
  "companyName" : "GameStop Corp",
  "sector" : "Technology",
  "marketCap" : "Mid",
  "price" : 20.34,
  "lastDayPrice" : 20.38,
  "momentum" : 1,
  "momentumStreakInDays" : 4,
  "volatileStock" : true,
  "newsHistory" : [ ]
}, {
  "ticker" : "OWL",
  "companyName" : "Big Owl Capital",
  "sector" : "Finance",
  "marketCap" : "Mid",
  "price" : 19.67,
  "lastDayPrice" : 19.65,
  "momentum" : -2,
  "momentumStreakInDays" : -4,
  "volatileStock" : false,
  "newsHistory" : [ ]
}, {
  "ticker" : "SLAB",
  "companyName" : "Silicon Laboratories",
  "sector" : "Technology",
  "marketCap" : "Mid",
  "price" : 19.69,
  "lastDayPrice" : 19.67,
  "momentum" : -2,
  "momentumStreakInDays" : -3,
  "volatileStock" : false,
  "newsHistory" : [ ]
} ]


πŸ“ˆ Stock History

  • Stock prices are saved each day, and the history of a stocks price can be viewed
  • Price history is reset at the end of each year

➑️ Stock History Endpoints

Stock History By Ticker Symbol: GET | http://localhost:8080/api/v1/stocks/history/{ticker}

[ {
"marketDate" : "7/7/1",
"ticker" : "AMZN",
"stockPrice" : 97.8
}, {
"marketDate" : "7/8/1",
"ticker" : "AMZN",
"stockPrice" : 98.66
}, {
"marketDate" : "7/9/1",
"ticker" : "AMZN",
"stockPrice" : 101.1
}, {
"marketDate" : "7/10/1",
"ticker" : "AMZN",
"stockPrice" : 101.79
}, {
"marketDate" : "7/11/1",
"ticker" : "AMZN",
"stockPrice" : 102.92
}, {
"marketDate" : "7/12/1",
"ticker" : "AMZN",
"stockPrice" : 101.48
}, {
"marketDate" : "7/13/1",
"ticker" : "AMZN",
"stockPrice" : 102.53
}, {
"marketDate" : "7/14/1",
"ticker" : "AMZN",
"stockPrice" : 102.04
}, {
"marketDate" : "7/15/1",
"ticker" : "AMZN",
"stockPrice" : 102.59
}, {
"marketDate" : "7/16/1",
"ticker" : "AMZN",
"stockPrice" : 100.57
}, {
"marketDate" : "7/17/1",
"ticker" : "AMZN",
"stockPrice" : 100.79
} ]


πŸ“° Stock News

  • At the end of each day, there is a chance that a specific stock will release a news story, which will have a large effect on their price
  • Positive news, such as buyouts, will increase the stocks by around price 10%
  • Negative news, such as lawsuits or management shakeups will decrease stock price by around 10%
  • Bankruptcies will occur if a stock price dips below $1, where a buyout will occur and the stocks price will reset back to the default

➑️ News Endpoints

Note: {___} in url represents path variable

All News On The Market: GET | http://localhost:8080/api/v1/news

[ {
  "event" : "Charles Schwab Corporation announces buyout of small Finance company. There price soared as a result",
  "dateReleased" : "1/2/1"
}, {
  "event" : "Lawsuit announced against GameStop today. Investigations are ongoing.",
  "dateReleased" : "2/15/1"
} ]

All News On A Specific Stock: GET | http://localhost:8080/api/v1/news/{ticker}

[ {
  "event" : "Charles Schwab Corporation announces buyout of small Finance company. There price soared as a result",
  "dateReleased" : "1/2/1"
} ]


πŸ’² Stock Earnings Reports

  • Stocks release earnings reports on the first day of every 3rd month (3rd, 6th, 9th, 12th)
  • Earnings reports effect stock prices and optimism, and are also affected by previous optimism

➑️ Earnings Reports Endpoints

Note: {___} in url represents path variable

All Earnings Reports History: GET | http://localhost:8080/api/v1/earnings

[ {
  "estimatedEPS" : 2.03,
  "actualEPS" : 3.3,
  "reportMessage" : "Date: 1/1/1. Apple announces increased profits in new earnings report today, causing a spike in their stock price. Their EPS of 3.3 exceeded expectations of 2.03 EPS.",
  "dateOfRelease" : "3/1/1"
}, {
  "estimatedEPS" : 2.84,
  "actualEPS" : 4.16,
  "reportMessage" : "Date: 1/1/1. Amazon announces increased profits in new earnings report today, causing a spike in their stock price. Their EPS of 4.16 exceeded expectations of 2.84 EPS.",
  "dateOfRelease" : "6/1/1"
}, {
  "estimatedEPS" : 2.68,
  "actualEPS" : 4.06,
  "reportMessage" : "Date: 1/1/1. Berkshire Hathaway announces increased profits in new earnings report today, causing a spike in their stock price. Their EPS of 4.06 exceeded expectations of 2.68 EPS.",
  "dateOfRelease" : "3/1/1"
}, {
  "estimatedEPS" : 2.72,
  "actualEPS" : 3.09,
  "reportMessage" : "Date: 1/1/1. Costco Wholesale announces stable profits in new earnings report today. Their EPS of 3.09 fell in line with expectations of 2.72 EPS.",
  "dateOfRelease" : "12/1/1"
}]

All Earnings Report History Of A Specific Stock: GET | http://http://localhost:8080/api/v1/earnings/stock/{ticker}

[ {
  "estimatedEPS" : 2.84,
  "actualEPS" : 4.16,
  "reportMessage" : "Date: 1/1/1. Amazon announces increased profits in new earnings report today, causing a spike in their stock price. Their EPS of 4.16 exceeded expectations of 2.84 EPS.",
  "dateOfRelease" : "3/1/1"
}, {
  "estimatedEPS" : 2.72,
  "actualEPS" : 3.09,
  "reportMessage" : "Date: 1/1/1. Amazon announces stable profits in new earnings report today. Their EPS of 3.09 fell in line with expectations of 2.72 EPS.",
  "dateOfRelease" : "6/1/1"
} ]

All Earnings Report History On A Date: GET | http://http://localhost:8080/api/v1/earnings/date/{date}

Date is formatted as month_day_year and will return an error if incorrectly formatted

[ {
  "estimatedEPS" : 2.03,
  "actualEPS" : 3.3,
  "reportMessage" : "Date: 1/1/1. Apple announces increased profits in new earnings report today, causing a spike in their stock price. Their EPS of 3.3 exceeded expectations of 2.03 EPS.",
  "dateOfRelease" : "3/1/1"
}, {
  "estimatedEPS" : 2.84,
  "actualEPS" : 4.16,
  "reportMessage" : "Date: 1/1/1. Amazon announces increased profits in new earnings report today, causing a spike in their stock price. Their EPS of 4.16 exceeded expectations of 2.84 EPS.",
  "dateOfRelease" : "3/1/1"
}, {
  "estimatedEPS" : 2.68,
  "actualEPS" : 4.06,
  "reportMessage" : "Date: 1/1/1. Berkshire Hathaway announces increased profits in new earnings report today, causing a spike in their stock price. Their EPS of 4.06 exceeded expectations of 2.68 EPS.",
  "dateOfRelease" : "3/1/1"
} ]


πŸ“Š Index Funds

  • Index funds track the average price of a specific category of stocks
  • These cannot be traded, but only serve to estimate the total market trajectory

➑️ Index Fund Endpoints

Note: {___} in url represents path variable

Total Market Fund: GET | http://localhost:8080/api/v1/funds/total-market

{
  "name" : "Total Market ETF",
  "price" : 78.44,
  "fundTracking" : "TOTAL_MARKET"
}

{
  "name" : "Large Cap Index Fund",
  "price" : 101.0,
  "fundTracking" : "MARKET_CAP",
  "marketCap" : "large"
}

Index Fund By Sector: GET | http://localhost:8080/api/v1/funds/sector/{sector}

{
  "name" : "Technology Fund",
  "price" : 74.86,
  "fundTracking" : "SECTOR",
  "sector" : "technology"
}

Stable Fund (Non Volatile): GET | http://localhost:8080/api/v1/funds/stable

{
  "name" : "Stable Index Fund",
  "price" : 86.02,
  "fundTracking" : "VOLATILITY",
  "volatility" : false
}

Volatile Fund: GET | http://localhost:8080/api/v1/fund/volatile

{
  "name" : "Volatile Index Fund",
  "price" : 57.3,
  "fundTracking" : "VOLATILITY",
  "volatility" : true
}


πŸ’³ Trading & Accounts

➑️ Trading & Account Endpoints

Get Account By Name : GET | http://localhost:8080/api/v1/account/{username}

{
  "username" : "username",
  "accountBalance" : "1000",
  "stocksOwned" : [ {
    "ticker" : "AMZN" ,
    "amountOwned" : 2
  }, {
    "ticker" : "GOOG",
    "amountOwned" : 5
  } ]
}

Get Account History By Name : GET | http://localhost:8080/api/v1/account/history/{username}

[ {
  "date" : "3/20/1",
  "balance" : -8.06
}, {
  "date" : "3/21/1",
  "balance" : -8.06
}, {
  "date" : "3/22/1",
  "balance" : -8.06
}, {
  "date" : "3/23/1",
  "balance" : 3.4
}, {
  "date" : "3/24/1",
  "balance" : 4.3
}, {
  "date" : "3/25/1",
  "balance" : 4.3
}, {
  "date" : "3/26/1",
  "balance" : 4.3
}, {
  "date" : "3/27/1",
  "balance" : 4.3
}, {
  "date" : "3/28/1",
  "balance" : 4.3
} ]
Get Active Limit Orders By Name : GET | http://localhost:8080/api/v1/inventory/orders/get/{username}

Create Account : POST | http://localhost:8080/api/v1/account/{username}
Deposit Funds To Account: POST | http://localhost:8080/api/v1/account/deposit

πŸ† Leaderboard

  • A leaderboard tracks total user profits for each account, sorting them by how much each account earns
  • Trading stocks for a profit will push you up the leaderboard, with the goal of beating a trading bot

➑️ Leaderboard Endpoints

Get Top 10 User Accounts On Leaderboard | http://localhost:8080/api/v1/leaderboard

[ {
"ranking" : 1,
"username" : "user2",
"totalProfits" : 4.5
}, {
"ranking" : 2,
"username" : "user3",
"totalProfits" : 4.3
}, {
"ranking" : 3,
"username" : "user1",
"totalProfits" : 1.23
}, {
"ranking" : 4,
"username" : "user4",
"totalProfits" : -2.00
}, {
"ranking" : 5,
"username" : "user6",
"totalProfits" : -4.3
}, {
"ranking" : 6,
"username" : "user6",
"totalProfits" : -12.64
}, {
"ranking" : 7,
"username" : "user8",
"totalProfits" : -16.23
}, {
"ranking" : 8,
"username" : "user7",
"totalProfits" : -20.00
}, {
"ranking" : 9,
"username" : "user10",
"totalProfits" : -40.3
}, {
"ranking" : 10,
"username" : "user9",
"totalProfits" : -122.64
} ]

Get Top 3 User Accounts On Leaderboard | http://localhost:8080/api/v1/leaderboard

[ {
"ranking" : 1,
"username" : "user2",
"totalProfits" : 4.5
}, {
"ranking" : 2,
"username" : "user3",
"totalProfits" : 4.3
}, {
"ranking" : 3,
"username" : "user1",
"totalProfits" : 1.23
} ]

Get Ranking By Account: GET | http://localhost:8080/api/v1/leaderboard/ranking/{username}

{
  "ranking" : 2,
  "username" : "user3",
  "totalProfits" : 4.3
}


πŸ““ Results & Data From Simulation

Current Stock Price Simulation

Below is a chart where prices are simulated four times using the current method. Each simulation is one month in duration and their movements are overlayed

Result_Overlay_4_Large_Cap

The current calculation method is as followed:

NP = P + (P * R) + (P * (R * V)) + (M * PR)

NP = New Price
P -> Original Price
R -> Random Number. For these simulations the number weres -.0015 to .0015 and -.002 to .002
V -> Volatility of the stock. Values of 0 to 4
M -> Momentum of the stock. Values of -2 to 2
PR -> Positive random number up to .002

It's worth noting that the large flucuations betweens single days are new announcements that affect stock prices

The Benchmark

A common way to randomly simulate stock data is the model of Geometric Brownian Motion. An example of this can be seen below, with the same price and duration as my simulations:

Screenshot (51)