Day trading Simulator built with Raylib
(On mobile play the game in fullscreen)
A simple game built with Raylib and WASM that simulates the intraday price of a stock. The player can buy then sell or sell then buy the asset. In the first case the profit is price of the buy minus the price of the sell. In the second case the profit is the negative of price of the buy minus the price of the sell. The total profit is the sum of all the profits of the trades.
The math of day trading
The prices of a stock in short periods of time are assumed to follow a model called Geometric Brownian Motion and here will be describing the discrete version of the model.
Given $S$ as the price of the stock at time $t$, the change of the price ($\Delta S$) is proportional to the expected return rate of that stock ($\mu$) and to the volatility of the price the stock ($\sigma$). The volatility affects the change of the price by a random shock $\epsilon$ which follows a standard normal distribution ($\epsilon \sim N(0, 1)$). For a small time step ($\Delta t$), $\mu \Delta t$ is the expected change in $ \frac{\Delta S}{S} $ and it's variance is $\sigma \epsilon \sqrt{(\Delta t)}$.
So, the price change ($\Delta S$) for a small time interval $ \Delta t$ can be written mathematically as
$$ \Delta S = \mu S \Delta t + \sigma S \epsilon \sqrt{(\Delta t)}. $$
For more details please consult Hull (2015) page 330 and 331.
How this game was built
This game was built in C using a Raylib, a wonderful library for building simple games. The code was compiled to WebAssembly using Emscripten, a compiler focused in the Web. The full code along with the scripts to building the game are available here.
References
- EMSCRIPTEN. Emscripten: Compiling C/C++ to WebAssembly. Available at: https://emscripten.org/
- HULL, John C. Options, futures and other derivatives. 9. ed. Harlow, England: Pearson, 2015. 869 p.
- RAYLIB. raylib - a simple and easy-to-use library to enjoy videogames programming. Available at: www.raylib.com.