Install Packages and Libraries
#install.packages("quantmod")
#install.packages("PerformanceAnalytics")
#others installed at R x64
library("quantmod")
library("PerformanceAnalytics")
library("rugarch")
library("xts")
Get Symbol
## [1] "XRP-USD"
chartSeries(`XRP-USD`)
chartSeries(`XRP-USD`["2022"])
store XRP-USD in object xrp
xrp <- `XRP-USD`
str(xrp)
## An 'xts' object on 2020-01-01/2022-09-24 containing:
## Data: num [1:998, 1:6] 0.193 0.193 0.188 0.194 0.194 ...
## - attr(*, "dimnames")=List of 2
## ..$ : NULL
## ..$ : chr [1:6] "XRP-USD.Open" "XRP-USD.High" "XRP-USD.Low" "XRP-USD.Close" ...
## Indexed by objects of class: [Date] TZ: UTC
## xts Attributes:
## List of 2
## $ src : chr "yahoo"
## $ updated: POSIXct[1:1], format: "2022-09-24 17:23:04"
returns
returns <- CalculateReturns(xrp$"XRP-USD.Close")
returns1 <- returns[-1]
head(returns1,10)
## XRP-USD.Close
## 2020-01-02 -0.023999958
## 2020-01-03 0.029131635
## 2020-01-04 0.004309610
## 2020-01-05 0.006081655
## 2020-01-06 0.132829081
## 2020-01-07 -0.034278362
## 2020-01-08 -0.022864008
## 2020-01-09 -0.019743955
## 2020-01-10 0.036149518
## 2020-01-11 -0.002143133
tail(returns1,10)
## XRP-USD.Close
## 2022-09-15 -0.04603282
## 2022-09-16 0.09301178
## 2022-09-17 0.05695734
## 2022-09-18 -0.04955120
## 2022-09-19 0.08023487
## 2022-09-20 0.07262196
## 2022-09-21 -0.04420942
## 2022-09-22 0.22244650
## 2022-09-23 0.04549568
## 2022-09-24 -0.02633234
hist(returns1)
Avg daily return from Jan 2020-Mar2022
#In percentage
mean(returns1)
## [1] 0.003004297
#estimate returns from Jan 2020 to present (in percent)
mean(returns1)*nrow(returns1)
## [1] 2.995284
chart.Histogram(returns1,
methods = c("add.density", "add.normal", "add.risk"),
colorset = c("blue","green","red"))
Number of days “of Gains”
#number of days data points retrieved
num <- nrow(returns1)
num
## [1] 997
#10% returns distribution
upten <- (1-pnorm(0.1,mean(returns1),sd(returns1)))*num
upten
## [1] 67.88928
#5% returns distribution
upfive<- (1-pnorm(0.05,mean(returns1),sd(returns1)))*num
upfive
## [1] 234.4439
#0 returns distribution
(1-pnorm(0,mean(returns1),sd(returns1)))*num
## [1] 516.8514
Number of days “of Losses”
#number of days data points retrieved
nrow(returns1)
## [1] 997
#10% loss distribution
downten<-pnorm(-0.1,mean(returns1),sd(returns1))*nrow(returns1)
downten
## [1] 56.60301
#5% loss distribution
downfive <- (pnorm(-0.05,mean(returns1),sd(returns1)))*nrow(returns1)
downfive
## [1] 207.1125
Returns Diff
#difference in days
tendiff <- upten-downten
tendiff
## [1] 11.28627
fivediff <- upfive - downfive
fivediff
## [1] 27.33139
Volatility
sd(returns1)
## [1] 0.06509162
Return volatility
return_volatility <- returns1
chartSeries(return_volatility, theme = "white",)
Volatility check
volcheck <- tail(returns1,200)
head(volcheck)
## XRP-USD.Close
## 2022-03-09 0.06416155
## 2022-03-10 -0.03880292
## 2022-03-11 0.08771942
## 2022-03-12 -0.01940772
## 2022-03-13 -0.03208291
## 2022-03-14 0.01747551
sd(volcheck)
## [1] 0.04336984
mean(volcheck)
## [1] -0.0009519188
check_volatility <- volcheck
chartSeries(check_volatility, theme = "white",)
Monthly Volatility
chart.RollingPerformance(R=returns1["2020::2022"],
width = 22,
FUN = "sd.annualized",
scale = 252,
main = "Volatiliy by month")
s <- ugarchspec(mean.model = list(armaOrder = c(0,0)),
variance.model = list(model = "sGARCH"),
distribution.model = "norm")
m <- ugarchfit(data = returns1, spec = s)
#plot(m)
f <- ugarchforecast(fitORspec = m,
n.ahead = 30)
f
##
## *------------------------------------*
## * GARCH Model Forecast *
## *------------------------------------*
## Model: sGARCH
## Horizon: 30
## Roll Steps: 0
## Out of Sample: 0
##
## 0-roll forecast [T0=2022-09-24]:
## Series Sigma
## T+1 -0.0003893 0.08388
## T+2 -0.0003893 0.08422
## T+3 -0.0003893 0.08455
## T+4 -0.0003893 0.08488
## T+5 -0.0003893 0.08521
## T+6 -0.0003893 0.08553
## T+7 -0.0003893 0.08586
## T+8 -0.0003893 0.08618
## T+9 -0.0003893 0.08650
## T+10 -0.0003893 0.08682
## T+11 -0.0003893 0.08714
## T+12 -0.0003893 0.08746
## T+13 -0.0003893 0.08777
## T+14 -0.0003893 0.08809
## T+15 -0.0003893 0.08840
## T+16 -0.0003893 0.08871
## T+17 -0.0003893 0.08902
## T+18 -0.0003893 0.08933
## T+19 -0.0003893 0.08964
## T+20 -0.0003893 0.08995
## T+21 -0.0003893 0.09025
## T+22 -0.0003893 0.09055
## T+23 -0.0003893 0.09085
## T+24 -0.0003893 0.09116
## T+25 -0.0003893 0.09146
## T+26 -0.0003893 0.09175
## T+27 -0.0003893 0.09205
## T+28 -0.0003893 0.09235
## T+29 -0.0003893 0.09264
## T+30 -0.0003893 0.09293
#plot(f)