If the Sys.timezone() is different from xts::tzone() and the index is POSIXctPOSIXt class, then quantmod::specifyModel() fails, and this affects quantmod::buildModel() and quantmod::tradeModel(). I believe this is unexpected because in both cases xts::tzone() is UTC.
I have created some synthetic OHLC-data to reproduce the behavior, display the expected behavior and showing a manual fix.
Expected behavior
set.seed(1903)
# 0) create generic
# OHLC zoo object
open <- runif(n = 50,min = 10, max = 100)
ticker <- zoo::as.zoo(
cbind(
Open = open,
High = open + 1,
Low = open - 1,
Close = open + 0.5
)
)
# 1) daily index
zoo::index(ticker) <- seq(
from = Sys.Date(),
by = "+1 day",
length.out = nrow(ticker)
)
# 2) convert to xts
ticker <- xts::as.xts(
ticker
)
# 3) Specify and run model
# accordingly
model <- quantmod::buildModel(
x = quantmod::specifyModel(
quantmod::Next(quantmod::OpCl(ticker)) ~ quantmod::Lag(quantmod::OpHi(ticker))
),
method = "lm",
training.per = c(zoo::index(ticker)[1],zoo::index(ticker)[10])
)
#> Registered S3 method overwritten by 'quantmod':
#> method from
#> as.zoo.data.frame zoo
# 3.1) Trade modell
quantmod::tradeModel(
x = model
)
#> Warning in modelReturn(quantmodResults, trade.dates = trade.dates, leverage =
#> leverage, : Model results are all one direction.
#>
#> Model: lm1708018884.17448
#>
#> C.A.G.R.: 1272.66% H.P.R.: 48.44%
#>
#> Returns by period summary:
#>
#> weekly monthly quarterly yearly
#> Max. 9.61% 37.81% 40.81% 46.68%
#> 3rd Qu. 7.65% 20.99% 31.65% 46.68%
#> Mean 6.61% 14.72% 22.49% 46.68%
#> Median 6.30% 4.17% 22.49% 46.68%
#> 2rd Qu. 5.45% 3.17% 13.33% 46.68%
#> Min. 4.17% 2.18% 4.17% 46.68%
#>
#> Period to date returns:
#>
#> weekly monthly quarterly yearly
#> 4.17% 4.17% 4.17% 46.68%
I can fix this by either setting Sys.setenv(TZ = "UTC") or changing the index to a Date-class. (But the latter fix is not desirable for if one is to apply the quantmod-functions to, say, hourly data)
set.seed(1903)
# 0) create generic
# OHLC zoo object
open <- runif(n = 50,min = 10, max = 100)
ticker <- zoo::as.zoo(
cbind(
Open = open,
High = open + 1,
Low = open - 1,
Close = open + 0.5
)
)
# 1) daily index
zoo::index(ticker) <- seq(
from = as.POSIXct(Sys.Date(), tz = "UTC", origin = '1970-01-01'),
by = "+1 day",
length.out = nrow(ticker)
)
# 2) convert to xts
ticker <- xts::as.xts(
ticker
)
# 2.1) set TZ
Sys.setenv(TZ = "UTC")
# 3) Specify and run model
# accordingly
model <- quantmod::buildModel(
x = quantmod::specifyModel(
quantmod::Next(quantmod::OpCl(ticker)) ~ quantmod::Lag(quantmod::OpHi(ticker))
),
method = "lm",
training.per = c(zoo::index(ticker)[1],zoo::index(ticker)[10])
)
#> Registered S3 method overwritten by 'quantmod':
#> method from
#> as.zoo.data.frame zoo
# 3.1) Trade modell
quantmod::tradeModel(
x = model
)
#> Warning in modelReturn(quantmodResults, trade.dates = trade.dates, leverage =
#> leverage, : Model results are all one direction.
#>
#> Model: lm1708019051.06961
#>
#> C.A.G.R.: 1272.66% H.P.R.: 48.44%
#>
#> Returns by period summary:
#>
#> weekly monthly quarterly yearly
#> Max. 9.61% 37.81% 40.81% 46.68%
#> 3rd Qu. 7.65% 20.99% 31.65% 46.68%
#> Mean 6.61% 14.72% 22.49% 46.68%
#> Median 6.30% 4.17% 22.49% 46.68%
#> 2rd Qu. 5.45% 3.17% 13.33% 46.68%
#> Min. 4.17% 2.18% 4.17% 46.68%
#>
#> Period to date returns:
#>
#> weekly monthly quarterly yearly
#> 4.17% 4.17% 4.17% 46.68%
Description
If the
Sys.timezone()
is different fromxts::tzone()
and the index isPOSIXct
POSIXt
class, thenquantmod::specifyModel()
fails, and this affectsquantmod::buildModel()
andquantmod::tradeModel()
. I believe this is unexpected because in both casesxts::tzone()
isUTC
.I have created some synthetic
OHLC
-data to reproduce the behavior, display the expected behavior and showing a manual fix.Expected behavior
Created on 2024-02-15 with reprex v2.1.0
Minimal, reproducible example
Where it fails
Here I change the
index
toPOSIXct
as this supports smaller granularity.Created on 2024-02-15 with reprex v2.1.0
Manual fix
I can fix this by either setting
Sys.setenv(TZ = "UTC")
or changing theindex
to aDate
-class. (But the latter fix is not desirable for if one is to apply thequantmod
-functions to, say, hourly data)Created on 2024-02-15 with reprex v2.1.0
Session Info