joshuaulrich / joshuaulrich/quantmod
modelReturn ignores parameter ret.type
- Dominant language
- R
- Stars
- 906
- Forks
- 233
- PR merge metrics
- No merged PRs in 30d
Description
### Description
modelReturn has the paramteter ret.type.
tradeModel calls quantmod:::modelReturn.
```r
args(tradeModel)
# function (x, signal.threshold = c(0, 0), leverage = 1, return.model = TRUE,
# plot.model = FALSE, trade.dates = NULL, exclude.training = TRUE,
# ret.type = c("weeks", "months", "quarters", "years"), ...)
args(quantmod:::modelReturn)
# function (tR.results, trade.dates = NULL, ret.type = "months",
# leverage = 1, exclude.training = TRUE)
```
### Expected behavior
modelReturn should use the parameter ret.type
Call and output should look like the following (currently, it does not).
```r
tm <- tradeModel(m.built,leverage=2, ret.type = "months")
tm
Model: rpart1540510651.23703
C.A.G.R.: -28.34% H.P.R.: -52.51%
Returns by period summary:
monthly
Max. 6.77%
3rd Qu. -0.81%
Mean -3.73%
Median -2.52%
2rd Qu. -5.89%
Min. -16.07%
Period to date returns:
monthly
-16.07%
```
Call and output currently looks like the following with ret.type ignored.
```r
> tm <- tradeModel(m.built,leverage=2, ret.type = "months")
> tm
Model: rpart1540510651.23703
C.A.G.R.: -28.34% H.P.R.: -52.51%
Returns by period summary:
weekly monthly quarterly yearly
Max. 12.25% 6.77% -1.57% -26.19%
3rd Qu. 0.85% -0.81% -5.34% -28.74%
Mean -0.86% -3.73% -10.06% -31.29%
Median -0.49% -2.52% -11.78% -31.29%
2rd Qu. -3.17% -5.89% -14.40% -33.84%
Min. -11.92% -16.07% -17.60% -36.39%
Period to date returns:
weekly monthly quarterly yearly
-10.00% -16.07% -16.07% -36.39%
```
```r
debug(quantmod:::modelReturn)
```
These lines in quantmod:::modelReturn are involved in determining the output.
The output is never restricted
```r
returnsBy <- allReturns(model.cumret)
```
Here is the flow.
```r
model.cumret <- cumprod(1 + model.results)
Browse[2]> tail(model.cumret)
Next.OpCl.IBM
2018-10-12 0.5276723
2018-10-15 0.5333106
2018-10-16 0.5169676
2018-10-17 0.5029678
2018-10-18 0.4864460
2018-10-19 0.4749040
periods <- match.arg(ret.type, c("weeks", "months", "quarters", "years"), several.ok = TRUE)
Browse[2]> periods
[1] "months"
# so, in no way is ret.type restricting the output of the columns
returnsBy <- allReturns(model.cumret)
Browse[2]> head(returnsBy)
daily weekly monthly quarterly yearly
2017-04-04 NA NA NA NA NA
2017-04-05 -0.0208356271 NA NA NA NA
2017-04-06 -0.0117600045 NA NA NA NA
2017-04-07 0.0006973152 -0.03167585 NA NA NA
2017-04-10 -0.0154176318 NA NA NA NA
2017-04-11 -0.0008202989 NA NA NA NA
# a possible sufficient solution may be the following
# match the columns chosen by ret.type
Browse[2]> c("weeks", "months", "quarters", "years") %in% ret.type
[1] FALSE TRUE FALSE FALSE
Browse[2]> periods <- c("weeks", "months", "quarters", "years") %in% ret.type
Browse[2]> head( returnsBy[, c(TRUE, periods)])
daily monthly
2017-04-04 NA NA
2017-04-05 -0.0208356271 NA
2017-04-06 -0.0117600045 NA
2017-04-07 0.0006973152 NA
2017-04-10 -0.0154176318 NA
2017-04-11 -0.0008202989 NA
# so replace the line
# returnsBy <- allReturns(model.cumret)
# by
returnsBy <- allReturns(model.cumret)[, c(TRUE, periods)]
Browse[2]> head(returnsBy)
daily monthly
2017-04-04 NA NA
2017-04-05 -0.0208356271 NA
2017-04-06 -0.0117600045 NA
2017-04-07 0.0006973152 NA
2017-04-10 -0.0154176318 NA
2017-04-11 -0.0008202989 NA
# continue with the rest of the program
# end of program
tm
Model: rpart1540510651.23703
C.A.G.R.: -28.34% H.P.R.: -52.51%
Returns by period summary:
monthly
Max. 6.77%
3rd Qu. -0.81%
Mean -3.73%
Median -2.52%
2rd Qu. -5.89%
Min. -16.07%
Period to date returns:
monthly
-16.07%
```
### Minimal, reproducible example
Full example (what I used above)
```r
library(quantmod)
getSymbols("IBM", from = "2017-01-01", to = "2018-10-20")
# [1] "IBM"
m <- specifyModel(Next(OpCl(IBM)) ~ Lag(OpHi(IBM)))
m.built <- buildModel(m,method='rpart',training.per=c("2017-01-01","2017-04-01"))
# loading required package: rpart
# tradeMdel calls quantmod:::modelReturn and I want to debug that
debug(quantmod:::modelReturn)
tm <- tradeModel(m.built,leverage=2, ret.type = "months")
tm
Model: rpart1540510651.23703
C.A.G.R.: -28.34% H.P.R.: -52.51%
Returns by period summary:
weekly monthly quarterly yearly
Max. 12.25% 6.77% -1.57% -26.19%
3rd Qu. 0.85% -0.81% -5.34% -28.74%
Mean -0.86% -3.73% -10.06% -31.29%
Median -0.49% -2.52% -11.78% -31.29%
2rd Qu. -3.17% -5.89% -14.40% -33.84%
Min. -11.92% -16.07% -17.60% -36.39%
Period to date returns:
weekly monthly quarterly yearly
-10.00% -16.07% -16.07% -36.39%
```
### Session Info
```r
Browse[2]> devtools::session_info()
- Session info ---------------------------------------------------------------
setting value
version R version 3.5.1 Patched (2018-10-09 r75424)
os Windows 10 x64
system x86_64, mingw32
ui RTerm
language (EN)
collate English_United States.1252
ctype English_United States.1252
tz America/Chicago
date 2018-10-25
- Packages -------------------------------------------------------------------
package * version date lib source
curl 3.2 2018-03-28 [1] CRAN (R 3.5.1)
devtools 2.0.0 2018-10-19 [1] CRAN (R 3.5.1)
quantmod * 0.4-13 2018-04-13 [1] CRAN (R 3.5.1)
TTR * 0.23-4 2018-09-20 [1] CRAN (R 3.5.1)
xts * 0.11-1 2018-09-12 [1] CRAN (R 3.5.1)
zoo * 1.8-4 2018-09-19 [1] CRAN (R 3.5.1)
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Start at quantmod:::modelReturn, called by tradeModel, and inspect how ret.type becomes periods. Compare the allReturns(model.cumret) result with the issue's proposed column selection, then run the supplied IBM/tradeModel reproducible example. Done means ret.type = "months" produces only monthly period summaries and period-to-date returns instead of all periods.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- r
- Domain
- fintech-quant
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100