Resources 2
Resources 2
ticker symbols is scraped. For the further analysis I'll be choosing Infosys Limited stock
(mainly because it is one of the most actively traded stocks for the past few days).
It's ticker symbol is INFY.NS
You are free to choose any other stock and experiment with it.
The core component of the project is Random Walk Theory, which is a belief that market
price movements are independent of past values except in the way that they follow the same
distribution.
You can read more about it here:
Random walk
We'll be using Monte Carlo method for generating various simulations for predicting the
future price movement. Simply put, the Monte Carlo method runs an enormous number
of trials with different random numbers generated from an underlying distribution
(here Normal Distribution) for the uncertain variables (here price of the stock).
Create a new jupyter notebook on Google Colab and start by importing the required libraries.
Choose a stock of your choice and scrape its price data for some fixed days (say 30 days).
(Have a look at the data and maybe try predicting the future price movements before actually
implementing the code.)
Important note: We'll be using Adjusted close price for our analysis because of being
adjusted with any important decisions and events like stock splits and dividends it is a good
measure of market price. Make sure you select the 'Adj Close' column of your scraped
data.
Now to implement Monte Carlo Method, you need to know the independent and dependent
variables. Clearly price is the dependent variable and we need to predict it based on the
randomized returns (independent variable).
Make sure you remove NaN(Not a Number) values, generated in the first row because of not
having data from the previous day to calculate percent change.
For the next step of the Monte Carlo method you are required to know the probability
distribution of the independent variable.
For the infosys stock, here's what I got:
It follows a somewhat left skewed normal distribution.
Using np.random.normal we'll choose values from standard normal distribution and then to
account for skewness we'll take about 20% values above mean and 25% values below
mean.
(I am choosing 10 simulations to calculate price for next 30 days)
Set upper and lower limits (using nom.ppf() which basically finds the value of a random
variable for which the CDF takes the given value), and if any value is beyond this limit make
them equal to the limit values.
Make sure you set the np.random.seed() to some value so that you get the same results
every time you run the code.
Now from standard normal distribution we'll calculate returns based on the formula:
Store predicted price values in a new list by initializing with last price you had and calculating
new price by:
new price= previous price*(1+returns)
Plot the figure to see your predicted values
With this you are done with predicting future prices using the Monte Carlo method.
For example, instead of using data from the last 30 days, I scraped data for the last 45 days
and calculated results on the first 30 days only and compared my results on the next 15
days. Here's what I got:
Here black color represents the actual price, following values similar to predicted price
range.