18

Create a Stock Price Simulator With Python

 2 years ago
source link: https://towardsdatascience.com/create-a-stock-price-simulator-with-python-b08a184f197d
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Create a Stock Price Simulator With Python

Easy application of a Geometric Brownian Motion process to simulate stock prices

Photo by Markus Winkler on Unsplash

Note from Towards Data Science’s editors: While we allow independent authors to publish articles in accordance with our rules and guidelines, we do not endorse each author’s contribution. You should not rely on an author’s works without seeking professional advice. See our Reader Terms for details.

In this article I will try to briefly explain a method for simulating stock prices, which is the result of studies related to financial modelling processes in the search to reduce exposure and risk in financial investments.

In this case, I’m utilizing theGeometric Brownian Motion (GBM) process to emulate the random path of an asset’s returns, particularly a stock.

Understanding the model’s principles:

  • According to this model, volatility remains constant through the successive trading periods in which the stock trades.
  • Expected return has an independent behavior to the performance of the stock.
  • In addition, returns are normally distributed, which means that calculated in great scale (meaning hundreds of thousands or millions), returns tend to average 0 and have a standard deviation of 1.

In essence, the stock price will have a series of returns each trading period and we are trying to replicate such behavior with a constant interest rate, which is usually also called “drift”, to which we add a random positive or negative deviation, also know as “shock”, in the price of the asset.

As it turns out, all this concepts can be easily reflected in a Python code, to take advantage of the benefits of computational power that allow us to perform thousands of calculations in a matter of milliseconds.

In this code, I am only using modules from Python’s Standard Library, which means that efficiency in calculations could be improved simply by implementing other libraries such as Numpy.

  • In the first place, proceed with imports of mathematical and random number generator libraries.
  • Secondly, write the the function that automatizes the task of calculating the price of the stock, of course with the application of the GBM process.
  • Finally, perform a loop of thousands of simulations in which the price of the stock is emulated, with the objective of reaching a certain target. In my case, $130 is the desired price from an initial $100, which can certainly not be reached if the random behavior of the asset doesn’t allow it.

Truth to be told, regardless of the countless applications in finance related to this model, it tends to show shortcomings when taken to real-trading scenarios as its main assumptions are constant expected returns and constant volatility, which in fact isn’t actually the reality.

In order to assess these inefficiencies of the model, it would be preferable to model these parameters as stochastic processes, which means they’re independently distributed and not correlated with the asset’s performance.

No doubt there’s plenty of financial applications of programming skills such as this one and that are easily realized thanks to use of technology. It’s part of our challenge to contribute to the growth of this ecosystem.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK