What is Nikkei 225?
Nikkei 225 is a stock market index for the Tokyo stock exchange.
How to get Nikkei 225 stock price?
We can get data from yahoo finance,quandai,wiki etc.
You can get data through python code.
we need to downloaded some python package.
import yfinance as yf
import numpy as np
import pandas as pd
import seaborn as sns
import scipy.stats as scs
import statsmodels.api as sm
import statsmodels.tsa.api as smt
import matplotlib.pyplot as plt
Code in python
df = yf.download('^N225',
start='1985-01-01',
end='2021-08-04',
progress=False)
How to convert Adj Close price?
df = df.loc[:, ['Adj Close']]
df.rename(columns={'Adj Close':'adj_close'}, inplace=True)
Getting simple and log return
df['simple_rtn'] = df.adj_close.pct_change()
df['log_rtn'] = np.log(df.adj_close/df.adj_close.shift(1))
df[['simple_rtn','log_rtn']].tail(20)
simple_rtn | log_rtn | |
---|---|---|
Date | ||
2021-07-06 | 0.001574 | 0.001573 |
2021-07-07 | -0.009645 | -0.009692 |
2021-07-08 | -0.008775 | -0.008814 |
2021-07-09 | -0.006317 | -0.006337 |
2021-07-12 | 0.022498 | 0.022249 |
2021-07-13 | 0.005223 | 0.005210 |
2021-07-14 | -0.003822 | -0.003829 |
2021-07-15 | -0.011514 | -0.011581 |
2021-07-16 | -0.009760 | -0.009808 |
2021-07-19 | -0.012511 | -0.012590 |
2021-07-20 | -0.009568 | -0.009614 |
2021-07-21 | 0.005836 | 0.005819 |
2021-07-26 | 0.010356 | 0.010303 |
2021-07-27 | 0.004920 | 0.004908 |
2021-07-28 | -0.013892 | -0.013989 |
2021-07-29 | 0.007279 | 0.007252 |
2021-07-30 | -0.017955 | -0.018118 |
2021-08-02 | 0.018232 | 0.018068 |
2021-08-03 | -0.005010 | -0.005023 |
2021-08-04 | -0.002089 | -0.002091 |
How to get realised volatility in python?
def realized_volatility(x):
return np.sqrt(np.sum(x**2))
df_rv = df.groupby(pd.Grouper(freq='M')).apply(realized_volatility)
df_rv.rename(columns={'log_rtn': 'rv'}, inplace=True)
df_rv.rv = df_rv.rv * np.sqrt(12)
fig, ax = plt.subplots(2, 1, sharex=True)
ax[0].plot(df)
ax[1].plot(df_rv)
fig, ax = plt.subplots(3, 1, figsize=(24, 20), sharex=True)
df.adj_close.plot(ax=ax[0])
ax[0].set(title = 'Nikkei225 time series',
ylabel = 'Stock price ($)')
df.simple_rtn.plot(ax=ax[1])
ax[1].set(ylabel = 'Simple returns (%)')
df.log_rtn.plot(ax=ax[2])
ax[2].set(xlabel = 'Date',
ylabel = 'Log returns (%)')
df_rolling = df[['simple_rtn']].rolling(window=21) \
.agg(['mean', 'std'])
df_rolling.columns = df_rolling.columns.droplevel()
df_outliers = df.join(df_rolling)
def indentify_outliers(row, n_sigmas=3):
x = row['simple_rtn']
mu = row['mean']
sigma = row['std']
if (x > mu + 3 * sigma) | (x < mu - 3 * sigma):
return 1
else:
return 0
How to plot outlier in python?
df_outliers['outlier'] = df_outliers.apply(indentify_outliers,
axis=1)
outliers = df_outliers.loc[df_outliers['outlier'] == 1,
['simple_rtn']]
fig, ax = plt.subplots()
ax.plot(df_outliers.index, df_outliers.simple_rtn,
color='blue', label='Normal')
ax.scatter(outliers.index, outliers.simple_rtn,
color='red', label='Anomaly')
ax.set_title("Nikkei225 returns")
ax.legend(loc='lower right')
How to plot Nikkei225 and vix in python?
df = yf.download(['^N225', '^VIX'],
start='1985-01-01',
end='2021-07-28',
progress=False)
df = df[['Adj Close']]
df.columns = df.columns.droplevel(0)
df = df.rename(columns={'^N225': 'Nikkei225', '^VIX': 'vix'})
df['log_rtn'] = np.log(df.Nikkei225 / df.Nikkei225.shift(1))
df['vol_rtn'] = np.log(df.vix / df.vix.shift(1))
df.dropna(how='any', axis=0, inplace=True)
corr_coeff = df.log_rtn.corr(df.vol_rtn)
ax = sns.regplot(x='log_rtn', y='vol_rtn', data=df,
line_kws={'color': 'red'})
ax.set(title=f'N225 vs. VIX ($\\rho$ = {corr_coeff:.2f})',
ylabel='VIX log returns',
xlabel='Nikkei225 log returns')
0 Comments