HANG SENG INDEX (^HSI) is stock market Hong Kong

What does Hsi stand for in stock market in python?

HANG SENG INDEX (^HSI) is stock market Hong Kong.

What country is hsi?

Hong kong

Why is hsi droping?

We have described below through python

How to download hsi stock price in python?


!pip install yfinance
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

This is the stock price of hsi from 1985 to 2021

df = yf.download('^HSI',
start='1985-01-01',
end='2021-07-07',
progress=False)
df = df.loc[:, ['Adj Close']]
df.rename(columns={'Adj Close':'adj_close'}, inplace=True)
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)

Stock price of Hsi simple and log return

simple_rtnlog_rtn
Date
2021-06-07-0.004524-0.004534
2021-06-08-0.000205-0.000205
2021-06-09-0.001346-0.001347
2021-06-10-0.000130-0.000130
2021-06-110.0035930.003586
2021-06-15-0.007059-0.007084
2021-06-16-0.007043-0.007068
2021-06-170.0042810.004272
2021-06-180.0084980.008462
2021-06-21-0.010842-0.010901
2021-06-22-0.006292-0.006311
2021-06-230.0179200.017761
2021-06-240.0022690.002267
2021-06-250.0140490.013951
2021-06-28-0.000680-0.000680
2021-06-29-0.009369-0.009413
2021-06-30-0.005730-0.005747
2021-07-02-0.017952-0.018115
2021-07-05-0.005896-0.005914
2021-07-06-0.002510-0.002513

How to get realised volatily of hsi?

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(21, sharex=True)
ax[0].plot(df)
ax[1].plot(df_rv)
[<matplotlib.lines.Line2D at 0x7f0d3d28ba10>,
 <matplotlib.lines.Line2D at 0x7f0d3d2a35d0>,
 <matplotlib.lines.Line2D at 0x7f0d3d2a37d0>]

How to plot realised volatility?

fig, ax = plt.subplots(31, figsize=(2420), sharex=True)
df.adj_close.plot(ax=ax[0])
ax[0].set(title = 'HSI 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 (%)')
[Text(0, 0.5, 'Log returns (%)'), Text(0.5, 0, 'Date')]

How to plot hsi time series?

import cufflinks as cf
from plotly.offline import iplot, init_notebook_mode
init_notebook_mode()
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(rown_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
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("HSI returns")
ax.legend(loc='lower right')

HSI Outlier simple return

<matplotlib.legend.Legend at 0x7f0d36f254d0>
r_range = np.linspace(min(df.log_rtn), max(df.log_rtn), num=1000)
mu = df.log_rtn.mean()
sigma = df.log_rtn.std()
norm_pdf = scs.norm.pdf(r_range, loc=mu, scale=sigma)
fig, ax = plt.subplots(12, figsize=(168))
# histogram
sns.distplot(df.log_rtn, kde=False, norm_hist=True, ax=ax[0])
ax[0].set_title('Distribution of HSI returns', fontsize=16)
ax[0].plot(r_range, norm_pdf, 'g', lw=2,
label=f'N({mu:.2f}{sigma**2:.4f})')
ax[0].legend(loc='upper left');
/usr/local/lib/python3.7/dist-packages/seaborn/distributions.py:2557: FutureWarning:

`distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).



df.log_rtn.plot(title='Daily HSI returns')
<matplotlib.axes._subplots.AxesSubplot at 0x7f0d3811f850>


df = yf.download(['^HSI''^VIX'],
start='1985-01-01',
end='2021-07-07',
progress=False)
df.tail()
df = df[['Adj Close']]
df.tail()
df.columns = df.columns.droplevel(0)
df = df.rename(columns={'^HSI''hsi''^VIX''vix'})
df['log_rtn'] = np.log(df.hsi / df.hsi.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'BVSP vs. VIX ($\\rho$ = {corr_coeff:.2f})',
ylabel='VIX log returns',
xlabel='HSI log returns')
[Text(0, 0.5, 'VIX log returns'),
 Text(0.5, 0, 'HSI log returns'),
 Text(0.5, 1.0, 'HSI vs. VIX ($\\rho$ = -0.13)')]
For New updates of HSI analysis Click here

Post a Comment

0 Comments