Showing posts with label Analyze-daily-return-DJI-Volatility. Show all posts
Showing posts with label Analyze-daily-return-DJI-Volatility. Show all posts

Tuesday, September 3, 2019

Analyze daily return DJI Volatility with-R

 How to analyze Dow Jones Industrial Average (^DJI) Volatility

getSymbols("DJI", from="2004-01-01", to=Sys.Date())
[1] "DJI"
> chartSeries(Cl(DJI))
dow jones industrial average,dow jones,dow jones industrial average (index),dow jones index,dow jones industrials technical analyis,dow jones trading,dow jones & company (business operation),dow jones technical analysis,dow jones today,dow jones top 5 stocks,dow,technical analysis,elliots wave theory dow jones,how to analyze the stock market,swing trading,charles dow (organization founder),stock market

                                     Dow Jones Industrial Average


> ret <- dailyReturn(Cl(DJI), type='log')
Warning message:
In to_period(xx, period = on.opts[[period]], ...) :
  missing values removed from data
> par(mfrow=c(2,2))
> acf(ret, main="Return ACF");
> pacf(ret, main="Return PACF");
> acf(ret^2, main="Squared return ACF");
> pacf(ret^2, main="Squared return PACF")
> par(mfrow=c(1,1))
> m=mean(ret);s=sd(ret);
> par(mfrow=c(1,2))
dow jones industrial average,dow jones,dow jones industrial average (index),dow jones index,dow jones industrials technical analyis,dow jones trading,dow jones & company (business operation),dow jones technical analysis,dow jones today,dow jones top 5 stocks,dow,technical analysis,elliots wave theory dow jones,how to analyze the stock market,swing trading,charles dow (organization founder),stock market

                                Dow Jones Industrial Average ACF


> hist(ret, nclass=40, freq=FALSE, main='Return histogram');curve(dnorm(x,
+                                                                       mean=m,sd=s), from = -0.3, to = 0.2, add=TRUE, col="red")
> plot(density(ret), main='Return empirical distribution');curve(dnorm(x,
+                                                                      mean=m,sd=s), from = -0.3, to = 0.2, add=TRUE, col="red")
dow jones industrial average,dow jones,dow jones industrial average (index),dow jones index,dow jones industrials technical analyis,dow jones trading,dow jones & company (business operation),dow jones technical analysis,dow jones today,dow jones top 5 stocks,dow,technical analysis,elliots wave theory dow jones,how to analyze the stock market,swing trading,charles dow (organization founder),stock market

                           Dow Jones Industrial Average Histogram



> par(mfrow=c(1,1))
> library("moments", lib.loc="~/R/win-library/3.6")

> kurtosis(ret)

daily.returns
     14.22914 
> plot(density(ret), main='Return EDF - upper tail', xlim = c(0.1, 0.2),
+      ylim=c(0,2));

Volatility

dow jones industrial average,dow jones,dow jones industrial average (index),dow jones index,dow jones industrials technical analyis,dow jones trading,dow jones & company (business operation),dow jones technical analysis,dow jones today,dow jones top 5 stocks,dow,technical analysis,elliots wave theory dow jones,how to analyze the stock market,swing trading,charles dow (organization founder),stock market

                             Dow Jones Industrial Average Density


> curve(dnorm(x, mean=m,sd=s), from = -0.3, to = 0.2, add=TRUE, col="red")
> plot(density(ret), xlim=c(-5*s,5*s),log='y', main='Density on log-scale')
dow jones industrial average,dow jones,dow jones industrial average (index),dow jones index,dow jones industrials technical analyis,dow jones trading,dow jones & company (business operation),dow jones technical analysis,dow jones today,dow jones top 5 stocks,dow,technical analysis,elliots wave theory dow jones,how to analyze the stock market,swing trading,charles dow (organization founder),stock market

                                Dow Jones Industrial Average


Warning message:
In xy.coords(x, y, xlabel, ylabel, log) :
  21 y values <= 0 omitted from logarithmic plot
> curve(dnorm(x, mean=m,sd=s), from=-5*s, to=5*s, log="y", add=TRUE,
+       col="red")
> qqnorm(ret);qqline(ret);
> library("rugarch", lib.loc="~/R/win-library/3.6")
Loading required package: parallel

Attaching package: ‘rugarch’


The following object is masked from ‘package:stats’:

    sigma

> chartSeries(ret)
dow jones industrial average,dow jones,dow jones industrial average (index),dow jones index,dow jones industrials technical analyis,dow jones trading,dow jones & company (business operation),dow jones technical analysis,dow jones today,dow jones top 5 stocks,dow,technical analysis,elliots wave theory dow jones,how to analyze the stock market,swing trading,charles dow (organization founder),stock market

> garch11.spec = ugarchspec(variance.model = list(model="sGARCH",
+             garchOrder=c(1,1)), mean.model = list(armaOrder=c(0,0)))
> dji.garch11.fit = ugarchfit(spec=garch11.spec, data=ret)
> coef(dji.garch11.fit)
          mu        omega       alpha1
6.032757e-04 2.170827e-06 1.186304e-01
       beta1
8.581520e-01
> coef(dji.garch11.fit)
          mu        omega       alpha1 
6.032757e-04 2.170827e-06 1.186304e-01 
       beta1 
8.581520e-01 
> vcov(dji.garch11.fit)
              [,1]          [,2]          [,3]
[1,]  1.319089e-08  4.896674e-12  1.295144e-07
[2,]  4.896674e-12  6.755927e-13  4.623311e-09
[3,]  1.295144e-07  4.623311e-09  1.270512e-04
[4,] -1.354373e-07 -7.986498e-09 -1.253682e-04
              [,4]
[1,] -1.354373e-07
[2,] -7.986498e-09
[3,] -1.253682e-04
[4,]  1.552785e-04
> infocriteria(dji.garch11.fit)
                   

Akaike       -6.734543Bayes        -6.728084Shibata      -6.734545Hannan-Quinn -6.732250

> newsimpact(dji.garch11.fit)
$zy
  [1] 1.075914e-02 1.033212e-02 9.913807e-03
  [4] 9.504211e-03 9.103330e-03 8.711163e-03
  [7] 8.327711e-03 7.952974e-03 7.586952e-03
 [10] 7.229645e-03 6.881052e-03 6.541174e-03
 [13] 6.210011e-03 5.887563e-03 5.573830e-03
 [16] 5.268811e-03 4.972508e-03 4.684919e-03
 [19] 4.406045e-03 4.135886e-03 3.874441e-03
 [22] 3.621711e-03 3.377697e-03 3.142397e-03
 [25] 2.915812e-03 2.697941e-03 2.488786e-03
 [28] 2.288345e-03 2.096619e-03 1.913608e-03
 [31] 1.739312e-03 1.573730e-03 1.416863e-03
 [34] 1.268712e-03 1.129275e-03 9.985524e-04
 [37] 8.765450e-04 7.632524e-04 6.586746e-04
 [40] 5.628117e-04 4.756635e-04 3.972302e-04
 [43] 3.275117e-04 2.665080e-04 2.142191e-04
 [46] 1.706450e-04 1.357858e-04 1.096413e-04
 [49] 9.221171e-05 8.349689e-05 8.349689e-05
 [52] 9.221171e-05 1.096413e-04 1.357858e-04
 [55] 1.706450e-04 2.142191e-04 2.665080e-04
 [58] 3.275117e-04 3.972302e-04 4.756635e-04
 [61] 5.628117e-04 6.586746e-04 7.632524e-04
 [64] 8.765450e-04 9.985524e-04 1.129275e-03
 [67] 1.268712e-03 1.416863e-03 1.573730e-03
 [70] 1.739312e-03 1.913608e-03 2.096619e-03
 [73] 2.288345e-03 2.488786e-03 2.697941e-03
 [76] 2.915812e-03 3.142397e-03 3.377697e-03
 [79] 3.621711e-03 3.874441e-03 4.135886e-03
 [82] 4.406045e-03 4.684919e-03 4.972508e-03
 [85] 5.268811e-03 5.573830e-03 5.887563e-03
 [88] 6.210011e-03 6.541174e-03 6.881052e-03
 [91] 7.229645e-03 7.586952e-03 7.952974e-03
 [94] 8.327711e-03 8.711163e-03 9.103330e-03
 [97] 9.504211e-03 9.913807e-03 1.033212e-02
[100] 1.075914e-02

$zx
  [1] -0.300000000 -0.293939394 -0.287878788
  [4] -0.281818182 -0.275757576 -0.269696970
  [7] -0.263636364 -0.257575758 -0.251515152
 [10] -0.245454545 -0.239393939 -0.233333333
 [13] -0.227272727 -0.221212121 -0.215151515
 [16] -0.209090909 -0.203030303 -0.196969697
 [19] -0.190909091 -0.184848485 -0.178787879
 [22] -0.172727273 -0.166666667 -0.160606061
 [25] -0.154545455 -0.148484848 -0.142424242
 [28] -0.136363636 -0.130303030 -0.124242424
 [31] -0.118181818 -0.112121212 -0.106060606
 [34] -0.100000000 -0.093939394 -0.087878788
 [37] -0.081818182 -0.075757576 -0.069696970
 [40] -0.063636364 -0.057575758 -0.051515152
 [43] -0.045454545 -0.039393939 -0.033333333
 [46] -0.027272727 -0.021212121 -0.015151515
 [49] -0.009090909 -0.003030303  0.003030303
 [52]  0.009090909  0.015151515  0.021212121
 [55]  0.027272727  0.033333333  0.039393939
 [58]  0.045454545  0.051515152  0.057575758
 [61]  0.063636364  0.069696970  0.075757576
 [64]  0.081818182  0.087878788  0.093939394
 [67]  0.100000000  0.106060606  0.112121212
 [70]  0.118181818  0.124242424  0.130303030
 [73]  0.136363636  0.142424242  0.148484848
 [76]  0.154545455  0.160606061  0.166666667
 [79]  0.172727273  0.178787879  0.184848485
 [82]  0.190909091  0.196969697  0.203030303
 [85]  0.209090909  0.215151515  0.221212121
 [88]  0.227272727  0.233333333  0.239393939
 [91]  0.245454545  0.251515152  0.257575758
 [94]  0.263636364  0.269696970  0.275757576
 [97]  0.281818182  0.287878788  0.293939394
[100]  0.300000000

$yexpr
expression(sigma[t]^2)

$xexpr
expression(epsilon[t - 1])
 signbias(dji.garch11.fit) 
                      t-value         prob sig
Sign Bias           2.6955475 7.057665e-03 ***
Negative Sign Bias  0.7519089 4.521515e-01    
Positive Sign Bias  1.7476753 8.059950e-02   *
Joint Effect       21.2179261 9.485556e-05 ***
 uncmean(dji.garch11.fit) 

[1] 0.0006032757
uncvariance(dji.garch11.fit) 

[1] 9.349942e-05

getSymbols("^NSEI", from="2004-01-01", to=Sys.Date())

[1] "^NSEI"
chartSeries(ClCl(NSEI))
NSEI

> NSEI.ret <- diff(log(Ad(NSEI)))
> dataDaily <- na.omit(merge(ret,NSEI.ret), join='inner')
> DJI.M <- to.monthly(ret)$ret.Close

> NSEI.M <- to.monthly(NSEI.ret)$NSEI.ret),join='inner')

var1 <- VAR(dataDaily, lag.max=4, ic="AIC")
> VARselect(dataDaily,lag.max=4)
$selection
AIC(n)  HQ(n)  SC(n) FPE(n) 
     3      3      2      3 

$criteria
                   1             2
AIC(n) -1.765057e+01 -1.765985e+01
HQ(n)  -1.764591e+01 -1.765209e+01
SC(n)  -1.763768e+01 -1.763837e+01
FPE(n)  2.160015e-08  2.140055e-08
                   3             4
AIC(n) -1.766351e+01 -1.766312e+01
HQ(n)  -1.765265e+01 -1.764916e+01
SC(n)  -1.763344e+01 -1.762446e+01
FPE(n)  2.132239e-08  2.133058e-08

> summary(var1)

VAR Estimation Results:
========================= 
Endogenous variables: daily.returns, NSEI.Adjusted 
Deterministic variables: const 
Sample size: 2758 
Log Likelihood: 16545.072 
Roots of the characteristic polynomial:
0.3803 0.3803 0.3752 0.3752 0.3435 0.3435
Call:
VAR(y = dataDaily, lag.max = 4, ic = "AIC")


Estimation results for equation daily.returns: 
============================================== 
daily.returns = daily.returns.l1 + NSEI.Adjusted.l1 + daily.returns.l2 + NSEI.Adjusted.l2 + daily.returns.l3 + NSEI.Adjusted.l3 + const 

                   Estimate Std. Error t value
daily.returns.l1 -0.0944606  0.0201679  -4.684
NSEI.Adjusted.l1  0.0107388  0.0168926   0.636
daily.returns.l2 -0.0622671  0.0208366  -2.988
NSEI.Adjusted.l2  0.0244324  0.0168066   1.454
daily.returns.l3 -0.0261142  0.0206199  -1.266
NSEI.Adjusted.l3 -0.0130916  0.0163010  -0.803
const             0.0001135  0.0002168   0.524
                 Pr(>|t|)    
daily.returns.l1 2.95e-06 ***
NSEI.Adjusted.l1  0.52502    
daily.returns.l2  0.00283 ** 
NSEI.Adjusted.l2  0.14613    
daily.returns.l3  0.20546    
NSEI.Adjusted.l3  0.42198    
const             0.60056    
---
Signif. codes:  
0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1


Residual standard error: 0.01138 on 2751 degrees of freedom
Multiple R-Squared: 0.01114, Adjusted R-squared: 0.008979 
F-statistic: 5.163 on 6 and 2751 DF,  p-value: 2.733e-05 


Estimation results for equation NSEI.Adjusted: 
============================================== 
NSEI.Adjusted = daily.returns.l1 + NSEI.Adjusted.l1 + daily.returns.l2 + NSEI.Adjusted.l2 + daily.returns.l3 + NSEI.Adjusted.l3 + const 

                   Estimate Std. Error t value
daily.returns.l1  0.2725282  0.0240243  11.344
NSEI.Adjusted.l1 -0.0276338  0.0201228  -1.373
daily.returns.l2  0.1044792  0.0248209   4.209
NSEI.Adjusted.l2 -0.0358318  0.0200203  -1.790
daily.returns.l3  0.0667242  0.0245628   2.716
NSEI.Adjusted.l3 -0.0585520  0.0194180  -3.015
const             0.0003083  0.0002582   1.194
                 Pr(>|t|)    
daily.returns.l1  < 2e-16 ***
NSEI.Adjusted.l1  0.16978    
daily.returns.l2 2.64e-05 ***
NSEI.Adjusted.l2  0.07360 .  
daily.returns.l3  0.00664 ** 
NSEI.Adjusted.l3  0.00259 ** 
const             0.23261    
---
Signif. codes:  
0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1


Residual standard error: 0.01355 on 2751 degrees of freedom
Multiple R-Squared: 0.05239, Adjusted R-squared: 0.05032 
F-statistic: 25.35 on 6 and 2751 DF,  p-value: < 2.2e-16 



Covariance matrix of residuals:
              daily.returns NSEI.Adjusted
daily.returns     1.294e-04     5.042e-05
NSEI.Adjusted     5.042e-05     1.836e-04

Correlation matrix of residuals:
              daily.returns NSEI.Adjusted
daily.returns        1.0000        0.3271

NSEI.Adjusted        0.3271        1.0000

Black-Scholes formula-R

 Black-Scholes formula-R > BlackScholes <- function(TypeFlag = c("c", "p"), S, X, Time, r, b, sigma) { TypeFla...