How to get Australia Stock Market Index analysis
forecast in python
df = yf.download('^AXJO',
start='1985-01-01',
end='2021-08-12',
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)
AXJO realized_volatility
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)
Australia Stock Market Index analysis charts
[<matplotlib.lines.Line2D at 0x7f6dfb74c450>, <matplotlib.lines.Line2D at 0x7f6dfb758810>, <matplotlib.lines.Line2D at 0x7f6dfb7589d0>]
fig, ax = plt.subplots(3, 1, figsize=(24, 20), sharex=True)
df.adj_close.plot(ax=ax[0])
ax[0].set(title = 'AXJO 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 (%)')
Australia Stock Market Index log return graph analysis python
[Text(0, 0.5, 'Log returns (%)'), Text(0.5, 0, 'Date')]
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_rolling = df[['simple_rtn']].rolling(window=21) \
.agg(['mean', 'std'])
df_rolling.columns = df_rolling.columns.droplevel()
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
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("AXJO returns")
ax.legend(loc='lower right')
Current australia Stock Market Index outlier analysis python
<matplotlib.legend.Legend at 0x7f6df66b0790>
df.log_rtn.plot(title='Daily AXJO returns')
Current australia Stock Market Index analysis python
<matplotlib.axes._subplots.AxesSubplot at 0x7f447d0d1b90>
Current australia Stock Market Index and VIX analysis python
df = yf.download(['^AXJO', '^VIX'],
start='1985-01-01',
end='2021-07-10',
progress=False)
df.tail()
df = df[['Adj Close']]
df.columns = df.columns.droplevel(0)
df = df.rename(columns={'^AXJO': 'axjo', '^VIX': 'vix'})
df['log_rtn'] = np.log(df.axjo / df.axjo.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)
import pandas as pd
import numpy as np
import yfinance as yf
aus = yf.download('^AXJO',
start='1985-01-01',
end='2021-08-12',
progress=False)
import matplotlib.pyplot as plt
aus['Close'].plot(figsize=(12,8))
Current australia Stock Market Index Close and volume analysis
<matplotlib.axes._subplots.AxesSubplot at 0x7f6df6073750>
aus['Volume'].plot(figsize=(12,8))
<matplotlib.axes._subplots.AxesSubplot at 0x7f6df57b5950>
aus[['Close', 'Volume']].plot(subplots=True, style='b',
figsize=(12, 8))
array([<matplotlib.axes._subplots.AxesSubplot object at 0x7f6df56c0110>, <matplotlib.axes._subplots.AxesSubplot object at 0x7f6df56603d0>], dtype=object)
aus.describe()
aus['simple_rtn'] = aus.Close.pct_change()
aus['log_rtn'] = np.log(aus.Close/aus.Close.shift(1))
aus['log_rtn'].plot(subplots=True, style='b',
figsize=(12, 8))
array([<matplotlib.axes._subplots.AxesSubplot object at 0x7f6df5591f90>], dtype=object)
aus['log_rtn'].tail(12)
Date
2021-07-28 -0.007035
2021-07-29 0.005150
2021-07-30 -0.003349
2021-08-02 0.013276
2021-08-03 -0.002258
2021-08-04 0.003832
2021-08-05 0.001052
2021-08-06 0.003628
2021-08-09 0.000000
2021-08-10 0.003205
2021-08-11 0.002865
2021-08-12 0.000514
Name: log_rtn, dtype: float64
import cufflinks as cf
from plotly.offline import iplot, init_notebook_mode
df_rolling = aus[['simple_rtn']].rolling(window=21) \
.agg(['mean', 'std'])
df_rolling.columns = df_rolling.columns.droplevel()
df_outliers = aus.join(df_rolling)
df = yf.download(['^AXJO', '^VIX'],
start='1985-01-01',
end='2021-08-12',
progress=False)
df = df[['Adj Close']]
df.columns = df.columns.droplevel(0)
df = df.rename(columns={'^AXJO': 'axjo', '^VIX': 'vix'})
df['log_rtn'] = np.log(df.axjo / df.axjo.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)
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'AXJO vs. VIX ($\\rho$ = {corr_coeff:.2f})',
ylabel='VIX log returns',
xlabel='AXJO log returns')
[Text(0, 0.5, 'VIX log returns'), Text(0.5, 0, 'AXJO log returns'), Text(0.5, 1.0, 'AXJO vs. VIX ($\\rho$ = -0.12)')]
Current australia Stock Market Index and VIX correlation analysis python
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(1, 2, figsize=(16, 8))
# histogram
sns.distplot(df.log_rtn, kde=False, norm_hist=True, ax=ax[0])
ax[0].set_title('Distribution of AJOX 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');
# Q-Q plot
qq = sm.qqplot(df.log_rtn.values, line='s', ax=ax[1])
ax[1].set_title('Q-Q plot', fontsize = 16)
/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).
Text(0.5, 1.0, 'Q-Q plot')
Distributation and Q-Q plot of AJOX
acf = smt.graphics.plot_acf(df.log_rtn,
lags=N_LAGS,
alpha=SIGNIFICANCE_LEVEL
)
fig, ax = plt.subplots(2, 1, figsize=(12, 10))
smt.graphics.plot_acf(df.log_rtn ** 2, lags=N_LAGS,
alpha=SIGNIFICANCE_LEVEL, ax = ax[0])
ax[0].set(title='Autocorrelation Plots',
ylabel='Squared Returns')
smt.graphics.plot_acf(np.abs(df.log_rtn), lags=N_LAGS,
alpha=SIGNIFICANCE_LEVEL, ax = ax[1])
ax[1].set(ylabel='Absolute Returns',
xlabel='Lag')
[Text(0, 0.5, 'Absolute Returns'), Text(0.5, 0, 'Lag')]
Current australia Stock Market Index autocorrelation analysis python
df['moving_std_252'] = df[['log_rtn']].rolling(window=252).std()
df['moving_std_21'] = df[['log_rtn']].rolling(window=21).std()
fig, ax = plt.subplots(3, 1, figsize=(18, 15),
sharex=True)
df.plot(ax=ax[0])
ax[0].set(title='axjo time series',
ylabel='Stock price ($)')
df.log_rtn.plot(ax=ax[1])
ax[1].set(ylabel='Log returns (%)')
df.moving_std_252.plot(ax=ax[2], color='r',
label='Moving Volatility 252d')
df.moving_std_21.plot(ax=ax[2], color='g',
label='Moving Volatility 21d')
ax[2].set(ylabel='Moving Volatility',
xlabel='Date')
ax[2].legend()
<matplotlib.legend.Legend at 0x7f6dec802c50>
Current australia Stock Market Index Moving volatility analysis python
NEW AXJO ANALYSIS
0 Comments