library(tidyverse)
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:dplyr':
#>
#> intersect, setdiff, union
#> The following objects are masked from 'package:base':
#>
#> date, intersect, setdiff, union
#library(readxl)
#library(highcharter)
library(tidyquant)
#> Loading required package: PerformanceAnalytics
#> Loading required package: xts
#> Loading required package: zoo
#>
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#>
#> as.Date, as.Date.numeric
#>
#> Attaching package: 'xts'
#> The following objects are masked from 'package:dplyr':
#>
#> first, last
#>
#> Attaching package: 'PerformanceAnalytics'
#> The following object is masked from 'package:graphics':
#>
#> legend
#> Loading required package: quantmod
#> Loading required package: TTR
#> Registered S3 method overwritten by 'quantmod':
#> method from
#> as.zoo.data.frame zoo
#> Version 0.4-0 included new data defaults. See ?getSymbols.
#> == Need to Learn tidyquant? =========================================
#> Business Science offers a 1-hour course - Learning Lab #9: Performance Analysis & Portfolio Optimization with tidyquant!
#> </> Learn more at: https://university.business-science.io/p/learning-labs-pro </>
#library(timetk)
library(tibbletime)
#>
#> Attaching package: 'tibbletime'
#> The following object is masked from 'package:stats':
#>
#> filter
library(quantmod)
library(PerformanceAnalytics)
#library(scales)
library(fBasics)
#> Loading required package: timeDate
#>
#> Attaching package: 'timeDate'
#> The following objects are masked from 'package:PerformanceAnalytics':
#>
#> kurtosis, skewness
#> Loading required package: timeSeries
#>
#> Attaching package: 'timeSeries'
#> The following object is masked from 'package:zoo':
#>
#> time<-
#>
#> Attaching package: 'fBasics'
#> The following object is masked from 'package:TTR':
#>
#> volatility
symbols <- c("SPY","IBM","MSFT","INTC")
prices <-
getSymbols(symbols, src = 'yahoo',
from = "2012-12-31",
to = "2020-05-02",
auto.assign = TRUE,
warnings = FALSE) %>%
map(~Ad(get(.))) %>%
reduce(merge) %>%
`colnames<-`(symbols)
#> 'getSymbols' currently uses auto.assign=TRUE by default, but will
#> use auto.assign=FALSE in 0.5-0. You will still be able to use
#> 'loadSymbols' to automatically load data. getOption("getSymbols.env")
#> and getOption("getSymbols.auto.assign") will still be checked for
#> alternate defaults.
#>
#> This message is shown once per session and may be disabled by setting
#> options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
daily_returns <-
prices %>%
tk_tbl(preserve_index = TRUE,
rename_index = "date") %>%
gather(asset, prices, -date) %>%
group_by(asset) %>%
tq_transmute(mutate_fun = periodReturn,
period = "daily",
type = "log") %>%
spread(asset, daily.returns) %>%
select(date, symbols) %>%
slice(-1)
#> Error in tk_tbl(., preserve_index = TRUE, rename_index = "date"): could not find function "tk_tbl"
#na.omit()
str(SPY)
#> An 'xts' object on 2012-12-31/2020-05-01 containing:
#> Data: num [1:1847, 1:6] 140 145 146 146 146 ...
#> - attr(*, "dimnames")=List of 2
#> ..$ : NULL
#> ..$ : chr [1:6] "SPY.Open" "SPY.High" "SPY.Low" "SPY.Close" ...
#> Indexed by objects of class: [Date] TZ: UTC
#> xts Attributes:
#> List of 2
#> $ src : chr "yahoo"
#> $ updated: POSIXct[1:1], format: "2020-05-04 08:25:09"
spdr <- dailyReturn(SPY$SPY.Adjusted)
spr <- timeSeries(spdr)
histPlot(spr, labels = TRUE, col = "steelblue", fit = TRUE,
title = TRUE, grid = TRUE, rug = TRUE)
densityPlot(spr, labels = TRUE, col = "steelblue", fit = TRUE, hist = TRUE,
title = TRUE, grid = TRUE, rug = TRUE)
library(fPortfolio)
#> Loading required package: fAssets
cv1 <- cvarRisk(spr,weight = 1, alpha = .05)
cv2 <-CVaR(spr, p = 0.95, method = "historical")
out_cvar <- c(cv1,cv2)
summary(out_cvar)
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> -0.02591 -0.02589 -0.02587 -0.02587 -0.02585 -0.02583
Yesterdays_price <-last(SPY$SPY.Adjusted)
Yesterdays_price * (out_cvar)
#> Warning in `*.default`(Yesterdays_price, (out_cvar)): Recycling array of length 1 in array-vector arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Error in .xts(e, .index(e1)): index length must match number of observations
HurstIndex(spr)
#> daily.returns
#> Hurst Index 0.3921051
library(pracma)
#>
#> Attaching package: 'pracma'
#> The following objects are masked from 'package:fBasics':
#>
#> akimaInterp, inv, kron, pascal
#> The following object is masked from 'package:purrr':
#>
#> cross
hrst<- hurstexp(spr)
#> Simple R/S Hurst estimation: 0.4914289
#> Corrected R over S Hurst exponent: 0.5065912
#> Empirical Hurst exponent: 0.4966126
#> Corrected empirical Hurst exponent: 0.460818
#> Theoretical Hurst exponent: 0.5345702
library(fractal)
#> Loading required package: splus2R
#>
#> Attaching package: 'splus2R'
#> The following object is masked from 'package:pracma':
#>
#> peaks
#> The following objects are masked from 'package:fBasics':
#>
#> colIds, rowIds, rowMaxs, rowMins, stdev
#> The following objects are masked from 'package:timeSeries':
#>
#> colMaxs, colMins, colVars, seriesData
#> Loading required package: ifultools
phrst<- hurstSpec(spr)
factorx <- factor(cut(spr, breaks=nclass.Sturges(spr)))
#> Error in timeDate(from): argument "from" is missing, with no default
#TGDXulate and turn into data.frame
xoutspr <- as.data.frame(table(factorx))
#> Error in table(factorx): object 'factorx' not found
#Add cumFreq and proportions
xoutspr <- transform(xoutspr, cumFreq = cumsum(Freq), relative = prop.table(Freq))
#> Error in transform(xoutspr, cumFreq = cumsum(Freq), relative = prop.table(Freq)): object 'xoutspr' not found
# regression library which outputs nice simple rounded row summary data alernative to summarry command
xoutspr
#> Error in eval(expr, envir, enclos): object 'xoutspr' not found
#write.csv(xoutspr, file = "spyfreq2.csv")
prices_daily <- to.daily(prices,OHLC = FALSE)
head(prices_daily, 3)
#> SPY IBM MSFT INTC
#> 2012-12-31 123.3469 146.2995 22.59281 16.63493
#> 2013-01-02 126.5084 149.9656 23.36254 17.24805
#> 2013-01-03 126.2225 149.1407 23.04957 17.19966
asset_returns_xts <-
Return.calculate(prices_daily,
method = "log") %>%
na.omit()
charts.PerformanceSummary(asset_returns_xts)
Created on 2020-05-04 by the reprex package (v0.3.0)