Switzerland(SMI) stock market

What is the name of Switzerland(SMI) stock market?

The SMI is symbol of the Switzerland(SMI) stock market.

How to get Switzerland(SMI) stock market data ?

you get data from yahoo finance,Quanda,google etc.

How to install data in python?

Here install python lib.
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

Some python code data collected from yahoo finance

df = yf.download('^SSMI',
start='1985-01-01',
end='2021-07-07',
progress=False)
How to get indivisual column SMI adj price?
df = df.loc[:, ['Adj Close']]
df.rename(columns={'Adj Close':'adj_close'}, inplace=True)

Simple return and Log return SSMI

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)

How to get realised volatility SMI 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(21, sharex=True)
ax[0].plot(df)
ax[1].plot(df_rv)
Plot Realised volatility SMI
[<matplotlib.lines.Line2D at 0x7f8af4356f50>,
 <matplotlib.lines.Line2D at 0x7f8af4366090>,
 <matplotlib.lines.Line2D at 0x7f8af4366250>]
realised volatility smi
Plot time series
fig, ax = plt.subplots(31, figsize=(2420), sharex=True)
df.adj_close.plot(ax=ax[0])
ax[0].set(title = 'SSMI 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 (%)')

Get mean and standard deviation

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

How to plot Outliers?

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("SSMI returns")
ax.legend(loc='lower right')
<matplotlib.legend.Legend at 0x7f8aef0d5690>
SSMI return

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 SSMI 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');
df.log_rtn.plot(title='Daily SSMI returns')
<matplotlib.axes._subplots.AxesSubplot at 0x7f8aeeea6810>
daily return smi
How to compare SSMI and VIX?
df = yf.download(['^SSMI''^VIX'],
start='1985-01-01',
end='2021-07-07',
progress=False)
ax = sns.regplot(x='log_rtn', y='vol_rtn', data=df,
line_kws={'color''red'})
ax.set(title=f'SSMI vs. VIX ($\\rho$ = {corr_coeff:.2f})',
ylabel='VIX log returns',
xlabel='SSMI log returns')
[Text(0, 0.5, 'VIX log returns'),
 Text(0.5, 0, 'SSMI log returns'),
 Text(0.5, 1.0, 'SSMI vs. VIX ($\\rho$ = -0.38)')]


Post a Comment

0 Comments