This is the part 3 of our Python for Stock Market Analysis series and here, we will explore some of popular growth rates that can be used to see how well is our value is changing over the period of time. Lets take some of scenarios:
The scenarios can be many more but lets focus on some.
Again we will be using the data reading part's code from the previous blogs.
import pandas as pd
import numpy as np
import plotly.express as px
import cufflinks
import plotly.io as pio
import yfinance as yf
import warnings
warnings.filterwarnings("ignore")
cufflinks.go_offline()
cufflinks.set_config_file(world_readable=True, theme='pearl')
pio.renderers.default = "notebook"
pd.options.display.max_columns = None
symbols = ["AAPL"]
df = yf.download(tickers=symbols)
# convert column names into lowercase
df.columns = [c.lower() for c in df.columns]
df.rename(columns={"adj close":"adj_close"},inplace=True)
df.head()
NumExpr defaulting to 8 threads.
[*********************100%***********************] 1 of 1 completed
open | high | low | close | adj_close | volume | |
---|---|---|---|---|---|---|
Date | ||||||
1980-12-12 | 0.128348 | 0.128906 | 0.128348 | 0.128348 | 0.100323 | 469033600 |
1980-12-15 | 0.122210 | 0.122210 | 0.121652 | 0.121652 | 0.095089 | 175884800 |
1980-12-16 | 0.113281 | 0.113281 | 0.112723 | 0.112723 | 0.088110 | 105728000 |
1980-12-17 | 0.115513 | 0.116071 | 0.115513 | 0.115513 | 0.090291 | 86441600 |
1980-12-18 | 0.118862 | 0.119420 | 0.118862 | 0.118862 | 0.092908 | 73449600 |
Lets suppose that we bought a stock 2 months ago and we want to find out how much profit we currently have then we might subtract the price at the time we bought from the current price. And it can be simply called return. The rate of return is simple measurement that tells us how much has been the price increase from the base period. It is calculated as:
$$ ror = \frac{V_{current}-V_{initial}}{V_{initial}} * 100 $$RoR is the simplest growth rate and it does not take external factors like inflation into consideration.
This is the simple measurement of the growth rate where we simply calculate the rate of change from the previous month.
$$ rate = \frac{v_t - v_{t-1}}{v_{t-1}} * 100 $$Where,
Lets calculate this in our python. But first, lets make a dataframe to store the closing price of the month only.
mdf = df.resample("1M").close.last().rename("Close").reset_index()
mdf["momgr"] = mdf.Close.pct_change()*100
mdf
Date | Close | momgr | |
---|---|---|---|
0 | 1980-12-31 | 0.152344 | NaN |
1 | 1981-01-31 | 0.126116 | -17.216307 |
2 | 1981-02-28 | 0.118304 | -6.194292 |
3 | 1981-03-31 | 0.109375 | -7.547504 |
4 | 1981-04-30 | 0.126674 | 15.816225 |
... | ... | ... | ... |
491 | 2021-11-30 | 165.300003 | 10.347129 |
492 | 2021-12-31 | 177.570007 | 7.422870 |
493 | 2022-01-31 | 174.779999 | -1.571216 |
494 | 2022-02-28 | 165.119995 | -5.526950 |
495 | 2022-03-31 | 173.041000 | 4.797121 |
import plotly.graph_objects as go
from plotly.subplots import make_subplots
fig=make_subplots(specs=[[{"secondary_y": True}]])
lastn = 100
ldf = mdf[-lastn:]
fig.add_trace(go.Line(x=ldf.Date, y=ldf.momgr, line=dict(
color='rgb(104, 14, 24)',
),
name="MOM Growth Rate Closing Price"),secondary_y=True)
fig.add_trace(go.Line(x=ldf.Date, y=ldf.Close, line=dict(
color='rgb(10, 104, 204)',),
name="MOM Closing Price"),secondary_y=False)
fig.update_layout(
title= "AAPL Stock Data",
yaxis_title="MOM Growth Rate Closing Price",
xaxis_title="Date")
fig.update_yaxes(title_text="MOM Closing Price", secondary_y=False)
fig.update_yaxes(title_text="MOM Growth Rate Closing Price", secondary_y=True)
fig.show()