Re: [R-SIG-Finance] from a list of array to tibble
On Mon, 16 Oct 2023, arnaud gaboury writes: > I work with a list of crypto assets daily closing prices in xts class. Here > is a limited example: > > asset.xts.lst <- list(BTCUSDT = structure(c(26759.63, 26862, 26852.48, > 27154.15, > 27973.45), dim = c(5L, 1L), index = structure(c(1697068800, 1697155200, > 1697241600, 1697328000, 1697414400), tzone = "UTC", tclass = "Date"), class > = c("xts", > "zoo")), ETHUSDT = structure(c(1539.61, 1552.16, 1554.94, 1557.77, > 1579.73), dim = c(5L, 1L), index = structure(c(1697068800, 1697155200, > 1697241600, 1697328000, 1697414400), tzone = "UTC", tclass = "Date"), class > = c("xts", > "zoo")), TRXUSDT = structure(c(0.08481, 0.08549, 0.08501, 0.08667, > 0.08821), dim = c(5L, 1L), index = structure(c(1697068800, 1697155200, > 1697241600, 1697328000, 1697414400), tzone = "UTC", tclass = "Date"), class > = c("xts", > "zoo"))) > > I will compute some function from PerformanceAnalytics package and write > all results in a tibble. Let's apply a first function, Return.annualized() > (at first I computed returns from daily prices). I have now a list of > arrays named my.ret.lst: > > my.ret.lst <- list(BTCUSDT = structure(15.36, dim = c(1L, 1L), dimnames = > list( > "Annualized Return", NULL)), ETHUSDT = structure(4.06, dim = c(1L, > 1L), dimnames = list("Annualized Return", NULL)), TRXUSDT = structure(10.9, > dim = c(1L, > 1L), dimnames = list("Annualized Return", NULL))) > > Now I can't find a way to start my tibble. The idea is to have variables > BTCUSDT, ETHUSDT and TRXUSDT (token names), with observations (like > annualized return) and the respective values. I will add of course many > other observations.My row names can be either my token names, or the > observation (annualized return). > In my example, I can't find a way to pass each list element name to the > wanted tibble. > > Thank you for help > Do you mean something like this? data.frame(my.ret.lst) ## BTCUSDT ETHUSDT TRXUSDT ## Annualized Return 15.364.0610.9 (I don't use tibbles, but there is probably an 'as.tibble' method or similar.) -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] Data
On Sat, 20 Mar 2021, Fianu, Emmanuel Senyo writes: > Hi Enrico, > > thanks for the feedback. > I am having something different here: > I would be grateful for your assistance. > > res <- strsplit(as.character(my.data$Strike), ":") > #[[1]] > #[1] "character(0)" > > #[[2]] > #[1] "c(\"CAP ATM Fwd\", \" -0.49%\")" > > #[[3]] > #[1] "c(\"SWT 0.50% fwd\", \" 0.05%\")" > > #[[4]] > #[1] "c(\"CAP ATM Fwd\", \" -0.46%\")" > > #[[5]] > #[1] "c(\"SWT 0.50% fwd\", \" 0.08%\")" > Please show what 'my.data$Strike' is, by using ?str and ?dput. (It looks as if it's a data frame; but 'strsplit' expects a character vector.) Enrico > > > col1 <- sapply(res, `[[`, 1L) > > #> col1 > #[1] "character(0)" "c(\"CAP ATM Fwd\", \" -0.49%\")" > "c(\"SWT 0.50% fwd\", \" 0.05%\")" > #[4] "c(\"CAP ATM Fwd\", \" -0.46%\")" "c(\"SWT 0.50% fwd\", \" 0.08%\")" > "c(\"CAP ATM Fwd\", \" -0.40%\")" > #[7] "c(\"SWT 0.50% fwd\", \" 0.11%\")" "c(\"CAP ATM Fwd\", \" -0.32%\")" > "c(\"SWT 0.50% fwd\", \" 0.14%\")" > #[10] "c(\"CAP ATM Fwd\", \" -0.23%\")" "c(\"SWT 0.50% fwd\", \" 0.17%\")" > "c(\"CAP ATM Fwd\", \" -0.14%\")" > > > col2 <- sapply(res, `[[`, 2L) > > > #Error in FUN(X[[i]], ...) : subscript out of bounds > > col2 <- as.numeric(sub("%", "", col2, fixed = TRUE))/100 > res <- data.frame(col1, col2) > > > #col1 col2 > #1 character(0) NA > #2 c("CAP ATM Fwd", " -0.49%") NA > #3 c("SWT 0.50% fwd", " 0.05%") NA > #4 c("CAP ATM Fwd", " -0.46%") NA > #5 c("SWT 0.50% fwd", " 0.08%") NA > #6 c("CAP ATM Fwd", " -0.40%") NA > #7 c("SWT 0.50% fwd", " 0.11%") NA > #8 c("CAP ATM Fwd", " -0.32%") NA > > i <- seq(from = 1, to = nrow(res), by = 2) > res[i, ] > #col1 col2 > #1 character(0) NA > #3 c("SWT 0.50% fwd", " 0.05%") NA > #5 c("SWT 0.50% fwd", " 0.08%") NA > #7 c("SWT 0.50% fwd", " 0.11%") NA > #9 c("SWT 0.50% fwd", " 0.14%") NA > #11 c("SWT 0.50% fwd", " 0.17%") N > res[i + 1, ] > > #2 c("CAP ATM Fwd", " -0.49%") NA > #4 c("CAP ATM Fwd", " -0.46%") NA > #6 c("CAP ATM Fwd", " -0.40%") NA > #8 c("CAP ATM Fwd", " -0.32%") NA > #10 c("CAP ATM Fwd", " -0.23%") NA > > Many thanks, > Emmanuel [...] > On Sat, Mar 20, 2021 at 9:18 AM Enrico Schumann > wrote: > >> On Sat, 20 Mar 2021, Fianu, Emmanuel Senyo writes: >> >> > Hello Enrico, >> > >> > here is a reproducible result: >> > As you could see, I have separated CAP ATM Fwd and SWT 0.5% fwd >> > Now, I move on to have two separate tables). I would be grateful for any >> > support. >> > Currently, I want to remove all: "c(\"" and if necessary have decimals >> > instead of % symbols. >> > #my.data$Strike >> > #1 CAP ATM Fwd: -0.49% >> > #2 SWT 0.50% fwd: 0.05% >> > # 3 CAP ATM Fwd: -0.46% >> > #4 SWT 0.50% fwd: 0.08% >> > # 5 CAP ATM Fwd: -0.40% >> > #6 SWT 0.50% fwd: 0.11% >> > >> > >> > txt0<-strsplit(as.character(df),'CAP ATM Fwd:', "") >> > #[[1]] >> > #[1] "c(\"" " -0.49%\", \"SWT 0.50% >> > fwd: 0.05%\", \"" >> > #[3] " -0.46%\", \"SWT 0.50% fwd: 0.08%\", \"" " -0.40%\", \"SWT 0.50% >> > fwd: 0.11%\", \"" >> > #[5] " -0.32%\", \"SWT 0.50% fwd: 0.14%\", \"" " -0.23%\", \"SWT 0.50% >> > fwd: 0.17%\", \"" >> > #[7] " -0.14%\", \"SWT 0.50% fwd: 0.20%\", \"" " -0.05%\", \"SWT 0.50% >> > fwd: 0.23%\", \"" >> > #[9] " 0.07
Re: [R-SIG-Finance] Data
" -0.05%\", \"SWT 0.50% > fwd: 0.23%\", \"" > #[9] " 0.07%\", \"SWT 0.50% fwd: 0.25%\", \"" " 0.15%\", \"SWT 0.50% fwd: > 0.27%\", \"" > #[11] " 0.22%\", \"SWT 0.50% fwd: 0.28%\", \"" " 0.29%\", \"SWT 0.50% > fwd: 0.28%\", \n\"" > #[13] " 0.32%\", \"SWT 0.50% fwd: 0.28%\", \"" " 0.34%\", \"SWT 0.50% > fwd: 0.28%\", \"" > #[15] " 0.35%\", \"SWT 0.50% fwd: 0.26%\", \"" " 0.33%\", \"SWT 0.50% > fwd: 0.25%\", \"" > #[17] " 0.28%\", \"SWT 0.50% fwd: 0.22%\", \"" " 0.26%\", \"SWT 0.50% > fwd: 0.20%\", \"" > #[19] " 0.21%\", \"SWT 0.50% fwd: 0.18%\", \"" " 0.13%\", \"" > > #[21] " 0.11%\", \"" " 0.08%\")" > > txt1<-strsplit(as.character(df),'SWT 0.50% fwd:',"x") > #[[1]] > #[1] "c(\"CAP ATM Fwd: -0.49%\", \"" > > #[2] " 0.05%\", \"CAP ATM Fwd: -0.46%\", \"" > > #[3] " 0.08%\", \"CAP ATM Fwd: -0.40%\", \"" > > #[4] " 0.11%\", \"CAP ATM Fwd: -0.32%\", \"" > > #[5] " 0.14%\", \"CAP ATM Fwd: -0.23%\", \"" > > #[6] " 0.17%\", \"CAP ATM Fwd: -0.14%\", \"" > > #[7] " 0.20%\", \"CAP ATM Fwd: -0.05%\", \"" > > #[8] " 0.23%\", \"CAP ATM Fwd: 0.07%\", \"" > > #[9] " 0.25%\", \"CAP ATM Fwd: 0.15%\", \"" > > #[10] " 0.27%\", \"CAP ATM Fwd: 0.22%\", \"" > > #[11] " 0.28%\", \"CAP ATM Fwd: 0.29%\", \"" > > #[12] " 0.28%\", \n\"CAP ATM Fwd: 0.32%\", \"" > > #[13] " 0.28%\", \"CAP ATM Fwd: 0.34%\", \"" > > #[14] " 0.28%\", \"CAP ATM Fwd: 0.35%\", \"" > > #[15] " 0.26%\", \"CAP ATM Fwd: 0.33%\", \"" > > #[16] " 0.25%\", \"CAP ATM Fwd: 0.28%\", \"" > > #[17] " 0.22%\", \"CAP ATM Fwd: 0.26%\", \"" > > #[18] " 0.20%\", \"CAP ATM Fwd: 0.21%\", \"" > > #[19] " 0.18%\", \"CAP ATM Fwd: 0.13%\", \"CAP ATM Fwd: 0.11%\", \"CAP ATM > Fwd: 0.08%\")" > > Many thanks, > Emm > Hi Emm try this: my.data <- list() my.data$Strike <- c("CAP ATM Fwd: -0.49%", "SWT 0.50% fwd: 0.05%", "CAP ATM Fwd: -0.46%", "SWT 0.50% fwd: 0.08%", "CAP ATM Fwd: -0.40%", "SWT 0.50% fwd: 0.11%") res <- strsplit(my.data$Strike, ":") ## [[1]] ## [1] "CAP ATM Fwd" " -0.49%" ## ## [[2]] ## [1] "SWT 0.50% fwd" " 0.05%" ## ## [[3]] ## [1] "CAP ATM Fwd" " -0.46%" ## ## [[4]] ## [1] "SWT 0.50% fwd" " 0.08%" ## ## [[5]] ## [1] "CAP ATM Fwd" " -0.40%" ## ## [[6]] ## [1] "SWT 0.50% fwd" " 0.11%" col1 <- sapply(res, `[[`, 1L) col2 <- sapply(res, `[[`, 2L) col2 <- as.numeric(sub("%", "", col2, fixed = TRUE))/100 res <- data.frame(col1, col2) ##col1col2 ## 1 CAP ATM Fwd -0.0049 ## 2 SWT 0.50% fwd 0.0005 ## 3 CAP ATM Fwd -0.0046 ## 4 SWT 0.50% fwd 0.0008 ## 5 CAP ATM Fwd -0.0040 ## 6 SWT 0.50% fwd 0.0011 If CAP and SWT rows alternate, you can easily extract every second row: i <- seq(from = 1, to = nrow(res), by = 2) res[i, ] ## col1 col2 ## 1 CAP ATM Fwd -0.0049 ## 3 CAP ATM Fwd -0.0046 ## 5 CAP ATM Fwd -0.0040 res[i + 1, ] ##col1 col2 ## 2 SWT 0.50% fwd 0.0005 ## 4 SWT 0.50% fwd 0.0008 ## 6 SWT 0.50% fwd 0.0011 kind regards Enrico > On Fri, Mar 19, 2021 at 9:07 PM Enrico Schumann > wrote: > >> On Fri, 19 Mar 2021, Fianu, Emmanuel Senyo writes: >> >> > Hello Enrico, >> > >> > thanks for the feedback: this is what I have: >> > As you can see we have to things here: >> > >> > "CAP ATM Fwd" a
Re: [R-SIG-Finance] Data
On Fri, 19 Mar 2021, Fianu, Emmanuel Senyo writes: > Hello Enrico, > > thanks for the feedback: this is what I have: > As you can see we have to things here: > > "CAP ATM Fwd" and SWT 0.50% fwd > > strsplit(as.character(df),':', '')# separate the column entry into > multiples specified but not organized into tables. > [[1]] > [1] "c(\"CAP ATM Fwd"" -0.49%\", \"SWT 0.50% fwd" " 0.05%\", > \"CAP ATM Fwd"" -0.46%\", \"SWT 0.50% fwd" > [5] " 0.08%\", \"CAP ATM Fwd"" -0.40%\", \"SWT 0.50% fwd" " 0.11%\", > \"CAP ATM Fwd"" -0.32%\", \"SWT 0.50% fwd" > [9] " 0.14%\", \"CAP ATM Fwd"" -0.23%\", \"SWT 0.50% fwd" " 0.17%\", > \"CAP ATM Fwd"" -0.14%\", \"SWT 0.50% fwd" > [13] " 0.20%\", \"CAP ATM Fwd"" -0.05%\", \"SWT 0.50% fwd" " 0.23%\", > \"CAP ATM Fwd"" 0.07%\", \"SWT 0.50% fwd" > [17] " 0.25%\", \"CAP ATM Fwd"" 0.15%\", \"SWT 0.50% fwd" " 0.27%\", > \"CAP ATM Fwd"" 0.22%\", \"SWT 0.50% fwd" > > Many thanks > Emmanuel > (I put R-SIG-Finance into Cc. again.) As I said, please provide a reproducible example: use ?dput to provide example data, and also describe what you want to achieve. Perhaps this helps: txt <- c("CAP ATM Fwd: -0.49%", "CAP ATM Fwd: -0.49%") spl <- strsplit(txt, ":", fixed = TRUE) ## [[1]] ## [1] "CAP ATM Fwd" " -0.49%" ## ## [[2]] ## [1] "CAP ATM Fwd" " -0.49%" sapply(spl, `[[`, 1) ## column 1 ## [1] "CAP ATM Fwd" "CAP ATM Fwd" sapply(spl, `[[`, 2) ## column 2 ## [1] " -0.49%" " -0.49%" Now you can remove the '%' and call 'as.numeric' and so on [...] > On Fri, Mar 19, 2021 at 9:59 AM Enrico Schumann > wrote: > >> On Fri, 19 Mar 2021, Fianu, Emmanuel Senyo writes: >> >> > Dear All, >> > >> > Please, I am working on raw financial data and would like to have the >> data >> > cleaned. I am working on it a bit further and will be very grateful for >> any >> > idea or support. Below are my codes and results. >> > I would like to have the numbers after the semi-columns separate. >> > >> > >> > Strike<-unlist(c(my.data$Strike)) >> > # >> > # >> > Strike <- sapply(Strike[], trimws) # löscht überflüssige Leerzeichen >> > Strike <- Strike[-1] # löscht die erste Zeile >> > >> > CAP ATM Fwd: -0.49%SWT 0.50% fwd: 0.05% >> > "CAP ATM Fwd: -0.49%" "SWT 0.50% fwd: 0.05%" >> > CAP ATM Fwd: -0.46%SWT 0.50% fwd: 0.08% >> > "CAP ATM Fwd: -0.46%" "SWT 0.50% fwd: 0.08%" >> > CAP ATM Fwd: -0.40%SWT 0.50% fwd: 0.11% >> > "CAP ATM Fwd: -0.40%" "SWT 0.50% fwd: 0.11%" >> > CAP ATM Fwd: -0.32%SWT 0.50% fwd: 0.14% >> > "CAP ATM Fwd: -0.32%" "SWT 0.50% fwd: 0.14%" >> > CAP ATM Fwd: -0.23%SWT 0.50% fwd: 0.17% >> > "CAP ATM Fwd: -0.23%" "SWT 0.50% fwd: 0.17%" >> > CAP ATM Fwd: -0.14%SWT 0.50% fwd: 0.20% >> > "CAP ATM Fwd: -0.14%" "SWT 0.50% fwd: 0.20%" >> > CAP ATM Fwd: -0.05%SWT 0.50% fwd: 0.23% >> > "CAP ATM Fwd: -0.05%" "SWT 0.50% fwd: 0.23%" >> > CAP ATM Fwd: 0.07%SWT 0.50% fwd: 0.25% >> >"CAP ATM Fwd: 0.07%" "SWT 0.50% fwd: 0.25%" >> > >> > Many thanks >> > Emmanuel >> > >> >> Please provide a reproducible example, so that people can help you. >> >> (It's not what you have written, but did you mean "the >> numbers after the colons"? Then perhaps look at >> ?strsplit : >> >>strsplit("CAP ATM Fwd: -0.49%", split = ":", fixed = TRUE) >>## [[1]] >>## [1] "CAP ATM Fwd" " -0.49%" >> >>sapply(strsplit("CAP ATM Fwd: -0.49%", ":", fixed = TRUE), `[[`, 2L) >>## [1] " -0.49%" >> >> ) -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] Data
On Fri, 19 Mar 2021, Fianu, Emmanuel Senyo writes: > Dear All, > > Please, I am working on raw financial data and would like to have the data > cleaned. I am working on it a bit further and will be very grateful for any > idea or support. Below are my codes and results. > I would like to have the numbers after the semi-columns separate. > > > Strike<-unlist(c(my.data$Strike)) > # > # > Strike <- sapply(Strike[], trimws) # löscht überflüssige Leerzeichen > Strike <- Strike[-1] # löscht die erste Zeile > > CAP ATM Fwd: -0.49%SWT 0.50% fwd: 0.05% > "CAP ATM Fwd: -0.49%" "SWT 0.50% fwd: 0.05%" > CAP ATM Fwd: -0.46%SWT 0.50% fwd: 0.08% > "CAP ATM Fwd: -0.46%" "SWT 0.50% fwd: 0.08%" > CAP ATM Fwd: -0.40%SWT 0.50% fwd: 0.11% > "CAP ATM Fwd: -0.40%" "SWT 0.50% fwd: 0.11%" > CAP ATM Fwd: -0.32%SWT 0.50% fwd: 0.14% > "CAP ATM Fwd: -0.32%" "SWT 0.50% fwd: 0.14%" > CAP ATM Fwd: -0.23%SWT 0.50% fwd: 0.17% > "CAP ATM Fwd: -0.23%" "SWT 0.50% fwd: 0.17%" > CAP ATM Fwd: -0.14%SWT 0.50% fwd: 0.20% > "CAP ATM Fwd: -0.14%" "SWT 0.50% fwd: 0.20%" > CAP ATM Fwd: -0.05%SWT 0.50% fwd: 0.23% > "CAP ATM Fwd: -0.05%" "SWT 0.50% fwd: 0.23%" > CAP ATM Fwd: 0.07%SWT 0.50% fwd: 0.25% >"CAP ATM Fwd: 0.07%" "SWT 0.50% fwd: 0.25%" > > Many thanks > Emmanuel > Please provide a reproducible example, so that people can help you. (It's not what you have written, but did you mean "the numbers after the colons"? Then perhaps look at ?strsplit : strsplit("CAP ATM Fwd: -0.49%", split = ":", fixed = TRUE) ## [[1]] ## [1] "CAP ATM Fwd" " -0.49%" sapply(strsplit("CAP ATM Fwd: -0.49%", ":", fixed = TRUE), `[[`, 2L) ## [1] " -0.49%" ) -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] PairTrading package
On Mon, 21 Sep 2020, Alec Schmidt writes: > Daniel and Jasen, thanks much! > > Daniel, sorry, I'm on Windows PC, so I used dir instead of ls and I do see > PairTrading but your command > R CMD INSTALL PairTrading/ doesn't work: > > 'R' is not recognized as an internal or external command,operable program or > batch file. > > Thanks again, Alec It's more of a question for R-help, but: If you follow the URL https://cran.r-project.org/package=PairTrading , you'll find that the package has been archived. You can still follow the archive link, download the package and then install (on Windows) from the R-GUI by selecting "install from local files" in the packages menu. (Keep in mind that packages are archived for a reason, usually.) If you want to try Daniel's suggestion: That 'R' is not found tells you it's not in the search path. You can set it on Windows in the system/environment variable settings; just add 'C:/Program Files/R <>/bin ' (or whereever R is installed on your system) to the path. kind regards Enrico > > From: Daniel Cegiełka > Sent: Monday, September 21, 2020 5:01 PM > To: Alec Schmidt > Cc: r-sig-finance@r-project.org > Subject: Re: [R-SIG-Finance] PairTrading package > > Hi Alec, > > $ R --version > R version 4.0.0 (2020-04-24) -- "Arbor Day" > Copyright (C) 2020 The R Foundation for Statistical Computing > Platform: x86_64-apple-darwin17.0 (64-bit) > > R is free software and comes with ABSOLUTELY NO WARRANTY. > You are welcome to redistribute it under the terms of the > GNU General Public License versions 2 or 3. > For more information about these matters see > https://nam02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.gnu.org%2Flicenses%2Fdata=02%7C01%7Caschmid1%40stevens.edu%7Ce85102d9ef0f4cea022e08d85e718b7e%7C8d1a69ec03b54345ae21dad112f5fb4f%7C0%7C0%7C637363189046435726sdata=OfQqu78x4j3D6D1oHM4KrPcqTha0Y%2FMLVPUESTCQH4k%3Dreserved=0. > > $ git clone > https://nam02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fcran%2FPairTradingdata=02%7C01%7Caschmid1%40stevens.edu%7Ce85102d9ef0f4cea022e08d85e718b7e%7C8d1a69ec03b54345ae21dad112f5fb4f%7C0%7C0%7C637363189046440727sdata=RqrpSGXxOiJgM5I05DV0fPmRFUmkBnYHD5kge1Id3AE%3Dreserved=0 > Cloning into 'PairTrading'... > remote: Enumerating objects: 39, done. > remote: Counting objects: 100% (39/39), done. > remote: Compressing objects: 100% (24/24), done. > remote: Total 39 (delta 11), reused 39 (delta 11), pack-reused 0 > Unpacking objects: 100% (39/39), done. > > $ ls > PairTrading > > $ R CMD INSTALL PairTrading/ > * installing to library > ‘/Library/Frameworks/R.framework/Versions/4.0/Resources/library’ > * installing *source* package ‘PairTrading’ ... > > (…) > > ** building package indices > ** testing if installed package can be loaded from temporary location > ** testing if installed package can be loaded from final location > ** testing if installed package keeps a record of temporary installation path > * DONE (PairTrading) > > > Best regards, > Daniel > > >> On 21 Sep 2020, at 22:38, Alec Schmidt wrote: >> >> I used to have R version 3.6.0 and tried to install PairTrading but got a >> message that the package is not available for that version. >> Now I've updated R to 4.0.2. but still have the message: >> >> package �PairTrading� is not available (for R version 4.0.2) >> >> I wonder if anything can be done about it or there may be other packages >> with similar functionality. >> >> Thanks! Alec >> [...] -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] Valuation of FID
;> > > >> > > bogaso.christo...@gmail.com >> > > >> > > >> > > > wrote: >> > > >> > > >> > > Hi, >> > > >> > > >> > > I had placed this question in some other forums, however failed to >> > > >> > > garner sufficient information till date. Presenting the same here >> > > >> > > hoping to get some insightful ideas from experts here. >> > > >> > > >> > > Typically in a Bond the Principal is constant over it's life. >> > > However >> > > >> > > I have come across a Bond whose principal is variable, say, average >> > > of >> > > >> > > S quote for the last one month and coupon is paid based on that, >> > > >> > > coupon rate being constant. I was looking for some idea how such >> > > bond >> > > >> > > can be priced? >> > > >> > > >> > > Any idea will be highly appreciated. >> > > >> > > >> > > Thanks and regards, >> > > >> > > >> > > ___ >> > > >> > > R-SIG-Finance@r-project.org >> > > >> > > >> > > mailing list >> > > >> > > https://stat.ethz.ch/mailman/listinfo/r-sig-finance >> > > >> > > >> > > >> > > -- Subscriber-posting only. If you want to post, subscribe first. >> > > >> > > -- Also note that this is not the r-help list where general R >> > > questions should go. >> > > >> > > >> > > ___ >> > > >> > > R-SIG-Finance@r-project.org >> > > >> > > >> > > mailing list >> > > >> > > https://stat.ethz.ch/mailman/listinfo/r-sig-finance >> > > >> > > >> > > >> > > -- Subscriber-posting only. If you want to post, subscribe first. >> > > >> > > -- Also note that this is not the r-help list where general R >> > > questions should go. >> > >> > ___ >> > R-SIG-Finance@r-project.org >> > mailing list >> > https://stat.ethz.ch/mailman/listinfo/r-sig-finance >> > >> > -- Subscriber-posting only. If you want to post, subscribe first. >> > -- Also note that this is not the r-help list where general R >> > questions should go. >> >> ___ >> R-SIG-Finance@r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-sig-finance >> -- Subscriber-posting only. If you want to post, subscribe first. >> -- Also note that this is not the r-help list where general R questions >> should go. > > ___ > R-SIG-Finance@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-sig-finance > -- Subscriber-posting only. If you want to post, subscribe first. > -- Also note that this is not the r-help list where general R questions > should go. -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] query on adjRatios() function from TTR package
On Mon, 08 Jun 2020, Pitabas Mohanty writes: > Hi > > Can anyone tell me how to find the adjusted closing price when there are > multiple split events on the same day? In India, a few companies have > announced stock split and stock dividend on the same day. When I use the > adjRatios() function, it only adjusts for the first event and not the > second event. > > Any help will be greatly appreciated. > Function 'split_adjust' in package 'PMwR' supports several splits on a single timestamp. Suppose a stock split two times 2-for-1 on timestamp 2. Then: split_adjust(x = c(100, 25, 25), ## prices t = c(2, 2), ## index when split occurs ratio = c(2, 2)) ## split ratio ## [1] 25 25 25 split_adjust(x = c(100, 25, 49), t = c(2, 2), ratio = c(2,2), backward = FALSE) ## [1] 100 100 100 -- Enrico Schumann (maintainer of PMwR) Lucerne, Switzerland http://enricoschumann.net ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] Using optimize.portfolio
On Fri, 05 Jun 2020, Roger Bos writes: > All, > > I am comparing optimize.portfolio from the PortfolioAnalytics package to > portfolio.optim from the tseries package. portfolio.optim seems a bit > easier to use, but I like the set up of optimize.portfolio. I have created > a minimal reprex below that compares the output of both in case that helps > answer my questions. > Here are my two primary questions: > > 1) What if I wanted to pass a custom covariance matrix to > optimize.portfolio, like from a risk model. Is that possible? I can pass > it to portfolio.optim because covmat is one of the parameters. > 2) What if I wanted to pass forecasted returns to optimize.portfolio? How > would that be done. > > If there is anything that can be improved in this example, that would be > helpful as well. Thank you in advance for any assistance, Roger. > > ### > > library(PortfolioAnalytics) > library(tidyquant) > > symbols <- > c("MSFT","AAPL","AMZN","NVDA","CSCO","ADBE","AMGN","ORCL","QCOM","GILD") > > getYahooReturns <- function(symbols, return_column = "Ad") { > returns <- list() > for (symbol in symbols) { > getSymbols(symbol, from = '2000-01-01', adjustOHLC = TRUE, env = > .GlobalEnv, auto.assign = TRUE) > return <- Return.calculate(Ad(get(symbol))) > colnames(return) <- gsub("\\.Adjusted", "", colnames(return)) > returns[[symbol]] <- return > } > returns <- do.call(cbind, returns) > return(returns) > } > > returns <- getYahooReturns(symbols) > returns <- returns[-1, ] > returns[is.na(returns)] <- 0 > > # portfolio.optim from tseries package > library(tseries) > sigma <- cov(returns) > reslow <- rep(0, ncol(returns)) > reshigh <- rep(1, ncol(returns)) > popt <- portfolio.optim(x = returns, covmat = sigma, reslow = reslow, > reshigh = reshigh) > popt$pw > > pspec <- portfolio.spec(assets = symbols) > pspec <- add.constraint(portfolio=pspec, type="box", > min = 0, max = 1, min_sum = 0.99, max_sum = 1.01) > pspec <- add.objective(portfolio=pspec, >type="return", >name="mean") > pspec <- add.objective(portfolio=pspec, >type="risk", >name="var") > > opt <- optimize.portfolio(R = returns, >portfolio = pspec, >optimize_method = "DEoptim", ) > data.frame(optimize.portfolio = opt$weights, portfolio.optim = > round(popt$pw, 3)) > If all else fails, and supposing that 'PortfolioAnalytics' per default computes means and covariances in the standard way, you could create input data (time series) that have exactly the desired covariances and means: https://stackoverflow.com/questions/58293991/how-to-use-fportfolio-package-in-r-for-non-time-series-input/58302451#58302451 -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] Partitioning approach towards portfolio construction - ensemble models/weights based on parameter sets
>>>>> "Sam" == Sam H writes: Sam> Hi, Sam> Is there some (example) code available somewhere (can be highly Sam> experimental) that would enable conducting this kind of analysis (portfolio Sam> construction) (possibly wrapping PortfolioAnalytics): Sam> - https://blog.thinknewfound.com/2019/07/ensemble-multi-asset-momentum/ Sam> - Sam> https://docs.wixstatic.com/ugd/7c4c63_b3f66bbea0f648e19e535b1da004aeba.pdf Sam> - Sam> https://docs.wixstatic.com/ugd/7c4c63_735bc38a987340cc8db85691a41dbfe4.pdf Sam> So to be able to create average/ensemble weights based on a set of Sam> parameters (like rebalance date, look back periods for momentum and Sam> whatever the parameters are). Something like quantstrat has Sam> with apply.paramset, add.distribution, add.distribution.constraint, ... Sam> Original message was not delivered due to attachments, I guess. Sam> -- Sam> Best regards, Sam> Sam Perhaps the examples in https://ssrn.com/abstract=3374195 are of interest (though they do not use PortfolioAnalytics). kind regards Enrico -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] Lo catches slow
>>>>> "Joshua" == Joshua Ulrich writes: Joshua> On Tue, Mar 12, 2019 at 11:53 PM Ilya Kipnis wrote: >> >> Minimum reproducible example: >> >> require(quantmod) >> require(TTR) >> >> getSymbols('SPY') >> data <- cbind(OHLC(SPY), stoch(HLC(SPY))) >> head(Lo(data)) >> head(HLC(data)) >> >> Lo catches both the low from the OHLC object >> (which it's supposed to), and the slowD column >> (because it has low in the name), which it isn't >> supposed to. >> Joshua> This is a known issue that is hard to fix. See Joshua> https://github.com/joshuaulrich/quantmod/issues/24 Perhaps one could add a warning for this case, either in `Lo` or `has.Lo`? i <- grep("Low", colnames(x), ignore.case = TRUE) if (length(i) > 1L) warning("more than one column match 'low': ", paste(colnames(x)[i], collapse = " ")) >> This can cause issues down the line with other functions that call >> functions that search for the Low column which might cause a dimnames error >> down the line. >> Joshua> -- Joshua> Joshua Ulrich | about.me/joshuaulrich Joshua> FOSS Trading | www.fosstrading.com Joshua> R/Finance 2019 | www.rinfinance.com -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] Mixed integer programming
>>>>> "Hannu" == Hannu Kahra writes: Hannu> I have tried to replicate Example 5.1 in Luenberger: Investment Science. Hannu> Here is the model Hannu> model <- MIPModel() %>% Hannu> add_variable(x[i], i = 1:7, type = "binary") %>% Hannu> set_objective(200*x[1]+30*x[2]+200*x[3]+60*x[4]+50*x[5]+100*x[6]+50*x[7]) Hannu> %>% Hannu> add_constraint(100*x[1]+20*x[2]+150*x[3]+50*x[4]+50*x[5]+150*x[6]+150*x[7] Hannu> <= 500) Hannu> solve_model(with_ROI("glpk",verbose = TRUE)) Hannu> I get the following error Hannu> Error in UseMethod("solve_model") : Hannu> no applicable method for 'solve_model' applied to an object of class Hannu> "function" Hannu> modelMixed integer linear optimization problem Hannu> Variables: Hannu> Continuous: 0 Hannu> Integer: 0 Hannu> Binary: 7 Hannu> Model sense: maximize Hannu> Constraints: 1 Hannu> What is wrong? Thank you in advance. Hannu> -Hannu If you want people to help you, you need to provide a reproducible example. Also, please do not post in HTML, as code examples get scrambled and become unreadable. (If I had to venture a guess, I'd think you've forgotten one of those `%>%` before 'solve_model'.) -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
[R-SIG-Finance] Alternative solvers in rugarch (was: GARCH parameter estimation with rugarch: estimates seem inaccurate)
>>>>> "alexios" == alexios ghalanos writes: alexios> Hi Curtis, alexios> There is a function in rugarch called ugarchdistribution for performing these types of experiments: alexios> spec1 <- ugarchspec(mean.model = list(armaOrder = c(0,0), include.mean = FALSE), alexios> fixed.pars = list("omega" = 0.2, "alpha1" = 0.2, "beta1" = 0.2)) alexios> d=ugarchdistribution(spec1, n.sim=2000, m.sim=100, recursive = TRUE, recursive.length = 6000, solver.control=list(trace=1)) alexios> Try this and perhaps also read this blog post: alexios> http://www.unstarched.net/2012/12/26/garch-parameter-uncertainty-and-data-size/ alexios> Could we benefit from a better nonlinear solver? Perhaps. alexios> Could we benefit from code contributions to make it better? Definitely. alexios> Feel free to contribute. alexios> Best, alexios> Alexios I am not using 'rugarch' and only had a brief look at the code. But is it possible to "plug in" alternative solvers, i.e. without changing the package code? If not, that could be a useful feature, as it would allow to quickly test solvers. An "external" solver would have to comply with some interface convention, i.e. the solver would have to be provided as a function that takes certain defined input arguments and evaluates to defined outputs. alexios> On Mon, 28 Jan 2019 16:23:09 +, Curtis Miller wrote: >> Hello all, >> >> Over a year ago I wrote a blog post about the problems I was having >> estimating the parameters of GARCH models via fGarch. I got a lot of >> feedback and I've now followed up with another article taking that >> feedback into account: >> https://ntguardian.wordpress.com/2019/01/28/problems-estimating-garch-parameters-r-part-2-rugarch/ >> >> First, I switched from fGarch to rugarch, which is supposedly still >> maintained. I also looked at other parameter combinations in simulation >> experiments that others requested. >> >> It seems that rugarch isn't necessarily better when it comes to >> parameter accuracy and one needs a lot of data (in the order of >> thousands) to get good estimates of the parameter values. That said, CIs >> computed are highly unreliable even at large sample sizes and there is >> certainly no "silver bullet" optimization algorithm. >> >> I'd like feedback if I'm not doing things right. I heard once that >> others could not replicate my results; that is, they have reliable >> estimates for GARCH parameters. But I never found out who those people >> were and they did not give me their code to see what I was doing wrong. >> >> If the community is aware of better approaches, I would like to hear >> them as well. >> >> Thank you all, >> >> Curtis Miller >> -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] the package nmof
>>>>> "m" == mmm ammm writes: m> Dear Enrico, m> Thank you so much. It works now. m> But, the function DEopt worked without changing this setting and gave m> approximately the same result; could you please explain this for me? m> Many thanks The reason is that 'DEopt' does not drop the dimension when a solution is selected and passed to the objective function; a single solution remains a matrix (of one column). Such a conversion is simple to do in the objective function: use 'as.matrix()' to create a column vector; or 'c()' or 'drop()' to drop the dimension and create a vector. kind regards Enrico m> On 26/01/2019, Enrico Schumann wrote: >>>>>>> "m" == mmm ammm writes: >> m> Dear all, m> i'm hoping that one of you can help me in the following code that is m> used for asset selection based on nmof package and please guide me m> where is the mistake: >> m> the error message is: "Error in colSums(x) : 'x' must be an array of m> at least two dimensions". >> m> The entire code is below (it works with DEopt) but does not with >> PSO; m> it is for asset selection exaclty from the package nmof. >> m> require("NMOF") m> na<-31 >> m> nn<- read.table("n.txt") # n is the a 31*31 matrix. m> Sigma <- data.matrix(nn) >> m> OF2 <- function(x, data) { m> # res <- colSums (data$Sigma %*% x * x) m> res <- colSums (Sigma %*% x * x) m> #z<-c(x,x) m> n <- colSums (x); res <- res / n^2 m> } m> ### pso # m> data <- list( m> na = na, m> max = rep( 0.05, na), m> min = rep(-0.05, na) m> ) m> algo <- list(nP = 31L, m> nG = 1000L, m> c1 = 0.5, m> c2 = 1.5, m> #min = data$min, max = data$max, m> max = rep( 0.05, na), min = rep(-0.05, na), m> #repair = repair, pen = penalty, m> iner = 0.7, initV = 1, maxV = 0.2 m> #printBar = FALSE, printDetail = TRUE m> ) m> #x<-array(x, c(2,2)) >> m> system.time(sol <- PSopt(OF = OF2,algo = algo, data)) >> >> You could get rid of the error by setting 'loopOF' to >> FALSE (as part of the settings passed with list >> 'algo'). I will explain below what this setting does. >> >> But in any case, are you sure your objective function >> does what it should? If I read it correctly, it >> assumes that 'x' is logical. But both DEopt and PSopt >> work with numeric (i.e. real-valued) vectors. >> >> What 'loopOF' does: Differential Evolution and Particle >> Swarm Optimisation are multiple-solution methods, aka >> population-based methods. The NMOF implementations >> 'DEopt' and 'PSopt' arrange the populations as >> matrices; every column in such a matrix represents one >> solution. To compute the objective function of the >> solutions, with the default settings both 'DEopt' and >> 'PSopt' use a loop. The objective function should thus >> receive a single solution as input, and should evaluate >> to a single number. >> >> Sometimes an objective function may be computed for the >> whole population (i.e. all solutions) in one step. In >> such a case, the objective function should expect the >> population matrix (i.e. all solutions) as input, and >> should evaluate to a vector: the objective-function >> values corresponding to the columns of the population >> matrix. However, since the user specifies the >> objective function, 'DEopt'/'PSopt' cannot know >> automatically in what way the objective function is >> written; so you need to tell the functions by setting >> 'loopOF' to TRUE (the default) or to FALSE. >> >> kind regards >> Enrico >> >> >> -- >> Enrico Schumann (maintainer of package NMOF) >> Lucerne, Switzerland >> http://enricoschumann.net >> -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] the package nmof
>>>>> "m" == mmm ammm writes: m> Dear all, m> i'm hoping that one of you can help me in the following code that is m> used for asset selection based on nmof package and please guide me m> where is the mistake: m> the error message is: "Error in colSums(x) : 'x' must be an array of m> at least two dimensions". m> The entire code is below (it works with DEopt) but does not with PSO; m> it is for asset selection exaclty from the package nmof. m> require("NMOF") m> na<-31 m> nn<- read.table("n.txt") # n is the a 31*31 matrix. m> Sigma <- data.matrix(nn) m> OF2 <- function(x, data) { m> # res <- colSums (data$Sigma %*% x * x) m> res <- colSums (Sigma %*% x * x) m> #z<-c(x,x) m> n <- colSums (x); res <- res / n^2 m> } m> ### pso # m> data <- list( m> na = na, m> max = rep( 0.05, na), m> min = rep(-0.05, na) m> ) m> algo <- list(nP = 31L, m> nG = 1000L, m> c1 = 0.5, m> c2 = 1.5, m> #min = data$min, max = data$max, m> max = rep( 0.05, na), min = rep(-0.05, na), m> #repair = repair, pen = penalty, m> iner = 0.7, initV = 1, maxV = 0.2 m> #printBar = FALSE, printDetail = TRUE m> ) m> #x<-array(x, c(2,2)) m> system.time(sol <- PSopt(OF = OF2,algo = algo, data)) You could get rid of the error by setting 'loopOF' to FALSE (as part of the settings passed with list 'algo'). I will explain below what this setting does. But in any case, are you sure your objective function does what it should? If I read it correctly, it assumes that 'x' is logical. But both DEopt and PSopt work with numeric (i.e. real-valued) vectors. What 'loopOF' does: Differential Evolution and Particle Swarm Optimisation are multiple-solution methods, aka population-based methods. The NMOF implementations 'DEopt' and 'PSopt' arrange the populations as matrices; every column in such a matrix represents one solution. To compute the objective function of the solutions, with the default settings both 'DEopt' and 'PSopt' use a loop. The objective function should thus receive a single solution as input, and should evaluate to a single number. Sometimes an objective function may be computed for the whole population (i.e. all solutions) in one step. In such a case, the objective function should expect the population matrix (i.e. all solutions) as input, and should evaluate to a vector: the objective-function values corresponding to the columns of the population matrix. However, since the user specifies the objective function, 'DEopt'/'PSopt' cannot know automatically in what way the objective function is written; so you need to tell the functions by setting 'loopOF' to TRUE (the default) or to FALSE. kind regards Enrico -- Enrico Schumann (maintainer of package NMOF) Lucerne, Switzerland http://enricoschumann.net ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] corrections vs drawdowns
On Tue, 08 Jan 2019, Alec Schmidt writes: > I tried to use the function findDrawdowns() to compile NASDAQ (^IXIC) > corrections. For the sample starting on > > 2007-01-01, I get the following start -to-trough periods with > drawdowns higher than 10% > > 08/30/2018 - 12/24/2018 (-23.64%) [80 Days] > 07/21/2015 - 02/11/2016 (-18.24%) [143 Days] > 09/17/2012 - 11/15/2012 (-10.90%) [42 Days] > 03/27/2012 - 06/01/2012 (-12.01%) [47 Days] > 05/02/2011 - 10/03/2011 (-18.71%) [108 Days] > 11/01/2007 - 03/09/2009 (-55.63%) [339 Days] > > > However, if the sample starts on 2000-06-01, I get > 08/30/2018 - 12/24/2018 (-23.64%) [80 Days] > 07/21/2015 - 02/11/2016 (-18.24%) [143 Days] > 07/18/2000 - 10/09/2002 (-73.94%) [559 Days] > > i.e. no bear market of 2008... > > This is because ^IXIC didn't recover in 2007 from its > fall from top in 2000. This implies that various > reports on market corrections do not use the max > drawdown. Is there consensus (and possibly R scripts) > that address this problem? > > Thanks! Alec > Perhaps the function 'streaks' in package 'PMwR' does what you want. library("tseries") library("PMwR") z <- get.hist.quote("^IXIC", quote = "Close", retclass = "zoo", start = as.Date("2007-1-1")) streaks(z) ##startend state return ## 1 2007-01-03 2007-03-05 -0.03403819 ## 2 2007-03-05 2007-10-31up 0.22149128 ## 3 2007-10-31 2008-11-20 down -0.53967656 ## 4 2008-11-20 2009-01-06up 0.25549343 ## 5 2009-01-06 2009-03-09 down -0.23223471 ## 6 2009-03-09 2018-08-29up 5.39242799 ## 7 2018-08-29 2019-01-04 down -0.16903607 See also https://stats.stackexchange.com/questions/354157/determining-up-down-market-trends-in-timeseries-data/373622#373622 https://cran.r-project.org/web/packages/PMwR/vignettes/Drawdowns_streaks.pdf -- Enrico Schumann (maintainer of PMwR) Lucerne, Switzerland http://enricoschumann.net ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] problem with reqMktData
On Do, 18 Okt 2018, Stephen Choularton writes: > Hi > > I can use this code to get historic data: > > library("IBrokers") > > sym1 <- 'ZS' > mkt1 <- 'ECBOT' > sym2 <- 'ZC' > mkt2 <- 'ECBOT' > > contract1 <- twsContract(0,sym1, "CONTFUT", > mkt1,"","","0.0","USD","","","",NULL,NULL,"0") > > reqHistoricalData(tws, contract1, endDateTime = > format(Sys.Date(), "%Y%m%d 24:00:00"), duration = "3 > M") > > but I can't get the code below to work > > reqMktData(tws, contract1) > > nor if I put a maturity in and use the same call like this > > contract1 <- twsContract(0,sym1, "CONTFUT", > mkt1,"","20181114","0.0","USD","","","",NULL,NULL,"0") > > or > > contract1 <- twsContract(0,sym1, "CONTFUT", > mkt1,"","201811","0.0","USD","","","",NULL,NULL,"0") > > although TWS specified this contract matures on 14 Nov. > > All I get is: > > TWS Message: 2 1 300 Can't find EId with tickerId:1 > TWS Message: 2 1 321 Error validating request:-'bN' : > cause - Please enter a valid security type > > I have tried all sorts of alternates on the date but > can't make it work and I tried this which I found on > the web which was claimed to work: > > contract = > twsFuture(symbol="ES",exch="GLOBEX",primary="GLOBEX",currency="USD",expiry="20171215") > ESHistorical = reqHistoricalData(tws, contract) > ESRealTime = reqMktData(tws, contract) > > but got a different error message: > > TWS Message: 2 1 300 Can't find EId with tickerId:1 > TWS Message: 2 1 200 No security definition has been found for the request > > Can anyone tell me where I have gone wrong with my own definition? > > > --- > > > Stephen Choularton PhD, FIoD > I don't think you can request market data for continuous contracts. As the IB docs say: , | Continuous futures are available from the API with TWS | v971 and higher. Continuous futures cannot be used with | real time data or to place orders, but only for | historical data. ` If I understand you correctly, you want data for the soybeans Nov-18 future? , | library("IBrokers") | tws <- twsConnect() | contract1 <- twsContract( | local = "ZS NOV 18", | sectype = "FUT", | exch = "ECBOT", | currency = "USD", | include_expired = "1", | conId = "", symbol = "", | primary = "", expiry = "", | strike = "", right = "", | multiplier = "", combo_legs_desc = "", | comboleg = "", secIdType = "", secId = "") | reqMktData(tws, contract1) ` See the description for the contract specification here: https://github.com/enricoschumann/IButils#finding-the-contract-specification -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] xts 'order.by' cannot contain 'NA', 'NaN', or 'Inf' in optimize.portfolio.rebalancing
On Di, 25 Sep 2018, Simon Hovmark writes: > I am trying to run the following optimize.portfolio.rebalancing: > > opt <- optimize.portfolio.rebalancing(R=returns, portfolio=tranch1, > optimize_method="ROI", > #momentFUN = tranch1_boudt, > rebalance_on = rebal.freq, > training_period = training.period, > rolling_window = rolling.window) > But when I use summary(opt) I get the following error: > > xts(x, order.by = order.by, frequency = frequency, ...) :'order.by' cannot > contain 'NA', 'NaN', or 'Inf' > I can see that other has had a similar problem, but I've not been able to > solve it using their answers. When I sum NA, NaN and InF on returns$dato I > get 0. > > A subset of my data is here: > > dato stock_1stock_2stock_3 > 1999-10-14 -0.002006019 0.016164145 -100 > 1999-10-15 0.0 0.0 -100 > 1999-10-18 -0.036813973 -0.049017341 -100 > 1999-10-19 0.016529302 0.0 -100 > 1999-10-20 0.016260521 0.011996238 -100 > 1999-10-21 0.008032172 0.005806736 -100 > 1999-10-22 0.0 0.0 -100 > 1999-10-25 0.039220713 0.023164955 -100 > 1999-10-26 0.028437935 0.002152853 -100 > 1999-10-27 -0.032291505 0.014941580 -100 > 1999-10-28 0.030420597 0.011061477 -100 > 1999-10-29 0.0 0.0 -100 > 1999-11-02 0.027702603 -0.003410734 -100 > 1999-11-03 0.007259560 -0.007650743 -100 > 1999-11-04 0.003610112 0.0 -100 > 1999-11-05 0.0 0.0 -100 > 1999-11-08 0.014311514 0.005546033 -100 > 1999-11-09 0.007079676 -0.002373106 -100 > 1999-11-10 0.039763233 0.024512309 -100 > 1999-11-11 -0.001696353 -0.018721296 -100 > 1999-11-12 0.0 0.0 -100 > And here is my full code. > > rebal.freq <- "years" > training.period <- 0 > rolling.window <- 120 > > returns <- read_excel("HEX.xlsx", sheet = 1, col_names = TRUE) > returns <- xts(returns[,-1], order.by= returns[,1]) > returns <- Return.calculate(returns, method = "log") > returns <- returns[-1,] > > returns[!is.finite(returns)] <- NA > returns[!is.finite(returns)] <- NA > returns <- na.fill(returns, fill = -100) > sum(is.nan(returns$dato)) #returns 0 > sum(is.infinite(returns$dato)) #returns 0 > sum(is.na(returns$dato)) #returns 0 > > fund.names <- colnames(returns) > tranch1 <- portfolio.spec(assets = fund.names) > tranch1 <- add.constraint(portfolio = tranch1, type = "leverage") > tranch1 <- add.constraint(portfolio = tranch1, type = "long_only") > tranch1 <- add.objective(portfolio=tranch1, type="return", name="mean") > tranch1 <- add.objective(portfolio=tranch1, type="risk", name="StdDev") > > opt <- optimize.portfolio.rebalancing(R=returns, portfolio=tranch1, > optimize_method="ROI", > #momentFUN = tranch1_boudt, > rebalance_on = rebal.freq, > training_period = training.period, > rolling_window = rolling.window) > > > summary(opt) > And my sessioninfo: > > R version 3.3.1 (2016-06-21) > Platform: x86_64-apple-darwin13.4.0 (64-bit) > Running under: OS X 10.13.3 (unknown) > > locale: > [1] C > > attached base packages: > [1] stats graphics grDevices utils datasets methods base > > other attached packages: > [1] RColorBrewer_1.1-2readxl_0.1.1 > DEoptimR_1.0-8 > [4] PortfolioAnalytics_1.0.3636 PerformanceAnalytics_1.4.3541 > foreach_1.4.4 > [7] xts_0.10-1zoo_1.7-13 > > loaded via a namespace (and not attached): > [1] compiler_3.3.1 parallel_3.3.1 tools_3.3.1 Rcpp_0.12.9 > codetools_0.2-14 > [6] grid_3.3.1 iterators_1.0.8 DEoptim_2.2-4lattice_0.20-34 > EDIT When I read in the Excel file, then class(returns$Dato) returns > > class(returns$Dato) [1] "POSIXct" "POSIXt" > Then instead of the below > > returns <- xts(returns[,-1], order.by= returns[,1]) > I tried using > > returns <- xts(returns[, -1], order.by=as.Date(paste(returns$Dato, > "%m/%d/%Y"))) > and run the optimization but summary(opt) again returned > > xts(x, order.by = orde
Re: [R-SIG-Finance] R and Bloomberg Data License
Quoting "Rassenti, Luca" <luca.rasse...@kochind.com>: Hello, I am starting to work on setting up our own locally hosted Bloomberg database using a Bloomberg data license. Any tips for setting this up? Any R code which can do most of setup work? Any suggestions on database design, table structure, etc.? Any help or reference to resources/references would be much appreciated. At minimum I will be setting up the database to store all futures data across all asset classes (equities, bonds, commodities), forwards for bonds and fx, spot prices series and yield series for those futures and bond series. Thank you, Luca This is an extremely-broad question, and so it is hard to give a concise, useful answer. Some random pointers (only on the R/database part; I have no idea what a Bloomberg data licence allows you to do or not): * First, there is of course the Rblpapi package (which you probably know, but did not mention). * There is ample support for databases in R: packages that allow you to access external databases, such as RODBC, and also standalone solutions, such as MonetDBLite. Choosing a database is not easy, and it may pay to experiment with different ones. (It will be much more work to later migrate your data to another database.) Your considerations should include the type of data (see next point), and also how many users may want to access the data. * Do you intent to store only time-series data? If yes, you may want to search the web (aka googling) for time-series databases; there are many discussions. * If you plan to store time-series, at what frequency? As long as you have only daily data (i.e. no intraday data), many specialised time-series databases may be overkill. For instance, for daily data I found an ad-hoc storing/updating in CSV-files sufficient (https://github.com/enricoschumann/tsdb) HTH -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] R packages/resources for Financial Risk Management
Quoting Pankaj K Agarwal via R-SIG-Finance <r-sig-finance@r-project.org>: Dear all Can someone suggest some good resources/package on Financial Risk Management based on R? I would like to use them for a graduate class i teach. Thanks in advance. Regards,Pankaj K Agarwal +91-98397-11444http://in.linkedin.com/in/pankajkagarwal/ As a starting point, you could look at the packages listed here: https://cran.r-project.org/web/views/Finance.html -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] Fteching options data using package IBrokers and interactivebrokers
On Sun, 25 Jun 2017, amol gupta writes: > Hello, > > I have been trying to fetch contract details and finally fetch contract > historical data. I have been able to do it with stock futures but I have > been failing with options. Here is a very small snippet. > > > c<-twsOPT(local = 'HDFC17JUN1780CE',strike = 1780, right = 'C', > currency = 'INR',primary = 'NSE',multiplier = 1, > conId = 271618757) > d<-reqContractDetails(tws,c) > > Warning message: > In reqContractDetails(tws, c) : error in contract details > > > here > <https://misc.interactivebrokers.com/cstools/contract_info/v3.9/index.php?action=Conid%20Info=IB=271618719=en> > are the contract details. Please help me figure out what am I doing wrong. Use 'twsContract' and specify only the local symbol, the exchange, the currency and the security type: require("IBrokers") c <- twsContract(local = 'HDFC17JUN1780CE', sectype = "OPT", currency = 'INR', exch = 'NSE', include_expired = 1, conId = "", symbol = "", primary = "", expiry = "", strike = "", right = "", multiplier = "", combo_legs_desc = "", comboleg = "", secIdType = "", secId = "") tws <- twsConnect() reqContractDetails(tws, c) ## List of 18 ## $ version : chr "8" ## $ contract :List of 16 ## ..$ conId : chr "271618757" ## ..$ symbol : chr "HDFC" ## ..$ sectype: chr "OPT" ## ..$ exch : chr "NSE" ## [] See also https://github.com/enricoschumann/IButils#finding-the-contract-specification -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] getting a subset corresponding to a list element
Zitat von Michael Ashton <m.ash...@enduringinvestments.com>: I'm not sure how to ask this with the proper terminology, but here goes: The BDH() function in RBLPAPI returns, for a list of symbols (e.g., 'SPX Index','RIY Index','IBM Equity') a list of closing prices. The problem is that the result is not a matrix or a dataframe, but a list. So, if I run the query with 3 symbols, I get a list with 3 elements. For example, in this case, if symbolist <-c("SPX Index","MXWO Index","MXEA Index") resultlist <- bdh(symbollist, "PX_LAST", options=opt,start.date=as.Date(begdate)) then resultlist is a list with 3 elements, and as many rows as there are dates between "begdate" and today (or as many month-ends, if "opt" declares monthly periodicity). Suppose in this case I've set this up to retrieve 60 dates. But I don't WANT a list. I want a zoo object containing each of these as an element. I thought about starting by trying to put each element in a matrix by data<-matrix(nrow=60,ncol=length(symbollist)) and then looping through from 1 to length(symbolist), letting data[,i] <- resultlist$symbollist[i][,2] but this clearly doesn't work since what I really want is data[,1] <-resultlist$'SPX Index'[,2] data[,2] <-resultlist$'MXWO Index'[,2] etc But there's probably a much easier way to do this. I am sending this to both the general help list and the r-sig-finance list since there is probably both a general way to stuff a list into a zoo object and a way to do it cleanly with the BDH() command. Thanks in advance for help. Mike Try this: require("Rblpapi") require("zoo") blpConnect() res <- bdh(c("SPX Index","MXWO Index","MXEA Index"), fields = "PX_LAST", start.date = as.Date("2017-01-01"), end.date = as.Date("2017-01-05")) do.call("merge", lapply(res, function(x) zoo(x[[2]], x[[1]]))) -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] Performanceanalytics -- table.calendarreturns question
On Fri, 07 Apr 2017, Jason Hart writes: > Hello - I'm hoping someone has solved this already but I am trying to run the > table.calendarreturns function in > performanceanalytics for many funds and then export the data to a csv file > (I'm interested in comparing monthly > returns between funds). The code itself runs fine, it's the presentation in > the CSV file that's a problem. I'd > like to have the output for each fund show up vertically on top of each other > in the csv file the exact same way > the output shows up in the console after the code is run.Right now the > way the data is exported all the > monthly returns for the funds show up in one long horizontal row for each > year. Evidently I need to add another > line or two of code to manipulate the data for export > > Thanks for any help! > > Here's an example along w/ a sample data template > library(PerformanceAnalytics) > library(timeSeries) > library(fPortfolio) > library(quantmod) > library(caTools) > > returns <- read.csv("example.csv") > lct <- Sys.getlocale("LC_TIME"); Sys.setlocale("LC_TIME", "C") > returns[,1] = as.Date(as.character(returns[,1]),format="%d-%b-%y") > Sys.setlocale("LC_TIME", lct) > > returns.saved <- returns ## Saves the data if it needs to be reset later. > start.date <- returns[,1][1] ##Default start date; use this when you want > entire data set > TickerListexcel <- colnames(returns[,-1]) > > #Creating time series object > returns = timeSeries(returns[,-1], charvec = returns[,1]) > #colnames(returns) <- abbreviate(colnames(returns),minlength=6) > str(returns) > returns > > ###CALENDAR RETURNS LOOP FOR MANY FUNDS > # Calendar Tables > cal.list <- list() > for (i in 1:dim(returns)[2]) { > cal.list[[i]] <- table.CalendarReturns(returns[,i], digits=2, as.perc = TRUE) > } > names(cal.list) <- TickerListexcel > write.csv(cal.list,file="Calendar Year Returns.csv") > Do you mean the output of print(returns) or of print(cal.list)? In either case, if you want to store the results as they show up in the console, you could use ?capture.output, as in capture.output(cal.list, file = "returns.txt") -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] Syntax - symbol problem
On Wed, 01 Mar 2017, Christian Lear writes: > Hi, > > I’m using Interactive Brokers (IB) data feed running through R code (R > studio) with moving average filters on futures contracts. > > I have listed the contracts in an excel sheet. The sheet has 4 > columns, contract (or ticker) i.e. “CLJ7" for April 2007 Crude Oil, > Security Type “Future”, Exchange “NYMEX” and Use 1 or 0 to activate > the excel line (contract) or not. > > My program works perfectly for Nymex energy contracts (CL, NG, RB, > HO), Nymex metals (GC, SI, etc.), Globex mini S (ES). However, the > program stops working and has an error when running Globex Euro FX > (6E), Japanese Yen (6J) and the other currencies. Note that the > currency contracts have the same syntax as the Nymex Energy, and Nymex > metals, namely “TickerMonthYear" (6EM7, CLJ7, GCJ7 etc.) > > 1. Does anyone know why the program fails to > recognize the currency futures contracts? Any > suggestions? > > Grains are also a problem. IB uses a different symbol structure, > namely Ticker MONth YEar “ZS MONabbrev YEar” (ZS JUL 17). > > 2. How would you solve for one code to read both the > CL contracts and the Grains ticker, knowing that the > symbol structure is different? Any ideas? > > I can send a short code and excel sheet test list to better > demonstrate the issue. > > Thanks for your help. > > Christian > 1) In my experience, you need four attributes to reliably specify a contract: the local symbol, the exchange, the currency and the security type. For 6E futures, that would mean symbol = 6EH7 exchange = GLOBEX currency = USD type = FUT 2) An easy and _safe_ way is to keep a list/database of contracts with the four mentioned attributes, and then refer to this list when contract information is needed. -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] Jegadeesh & Titman Strategy Implementation
signal = mom_weights, do.signal = seq(1, length(timestamp), by = 2), b = 13, convert.weights = TRUE, initial.cash = 100, timestamp = timestamp, include.data = TRUE) unique(journal(result)$timestamp) ## [1] "1998-02-28" "1998-04-30" "1998-06-30" "1998-08-31" "1998-10-31" ## [6] "1998-12-31" "1999-02-28" "1999-04-30" "1999-06-30" "1999-08-31" Kind regards Enrico ## Appendix: Random data na <- 100 ## number of assets timestamp <- seq(as.Date("1996-12-01"), as.Date("2016-12-31"), by = "1 day") timestamp <- aggregate(timestamp, by = list(format(timestamp, "%Y-%m")), FUN = tail, 1)[[2]] np <- length(timestamp) P <- array(rnorm(np*na, mean = 0.0025, sd = 0.04), dim = c(np, na)) P[1, ] <- 0 P <- apply(P, 2, function(x) cumprod(1 + x)) plot(P[ ,1]) -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] comparing solve.pq and nloptr for min variance portfolio
On Fri, 18 Mar 2016, Alec Schmidt <aschm...@stevens.edu> writes: > Enrico, > Here we're. I attach two scripts: one for solve.pq, another for > nloptr. Both run with the same input file and print output on the > screen. > > Thanks again, Alec Hi Alec, but these are still long programmes, which makes it hard to figure out what exactly is wrong. For a start, I would not compute the whole frontier, but concentrate on one point. I computed the minimum-variance portfolio (without short sales) with an alternative method, and it matches your output for 'solve.QP'. So, one thing to check is your implementation of the objective function for 'nloptr'. Kind regards (and good luck) Enrico > ________ > From: Enrico Schumann <e...@enricoschumann.net> > Sent: Friday, March 18, 2016 11:08 AM > To: Alec Schmidt > Cc: R-SIG-Finance@r-project.org > Subject: Re: [R-SIG-Finance] comparing solve.pq and nloptr for min variance > portfolio > > On Fri, 18 Mar 2016, Alec Schmidt <aschm...@stevens.edu> writes: > >> Hi Enrico, >> Many thanks for your interest. I attach my script and input file with >> asset tickers. Sorry for lots of unrelated stuff - it's a working >> draft. >> >> Alec > > Thanks for sending the script, Alec. But you will need to > simplify it if people are to help you. [My bad: I should have > said _minimal_ reproducible example: > https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example > ] > > > >> >> From: Enrico Schumann <e...@enricoschumann.net> >> Sent: Friday, March 18, 2016 10:25 AM >> To: Alec Schmidt >> Cc: R-SIG-Finance@r-project.org >> Subject: Re: [R-SIG-Finance] comparing solve.pq and nloptr for min variance >> portfolio >> >> On Fri, 18 Mar 2016, Alec Schmidt <aschm...@stevens.edu> writes: >> >>> I'm puzzled that I cannot reproduce results for asset weights using >>> solve.pq and nloptr even in the case of just three assets. E.g. if I >>> use NLOPT_LD_SLSQP and start with initial weights of 1/3, I may obtain >>> (0.47, 0, 0.53) vs (0.52, 0, 0.47). If I start with (0.52, 0, 0.47), >>> I do get (0.52, 0, 0.47)... >>> >>> When I use NLOPT_GN_ISRES or other nloptr solvers that permit equality >>> constraint sum(weights)=1 with initial weights of 1/3, I obtain >>> (almost) the same initial weights after 2 iterations with >>> xtol_rel=1.0e-8... >>> >>> I remember from my MC simulations of protein structures (20 years ago) >>> that sampling is key due to multiple local minimums but is it so bad >>> for a simple portfolio? >>> >>> >>> I'll greatly appreciate relevant comments. >>> >>> Alec >> >> [...] >> >> Unless your covariance matrix is 'broken' in some way, a >> minimum-variance portfolio with only a budget constraint should be >> fairly easy to compute (no multiple local minima, smooth objective >> function, ...). Please provide a reproducible example. >> >> Kind regards, >> Enrico -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] comparing solve.pq and nloptr for min variance portfolio
On Fri, 18 Mar 2016, Alec Schmidt <aschm...@stevens.edu> writes: > I'm puzzled that I cannot reproduce results for asset weights using > solve.pq and nloptr even in the case of just three assets. E.g. if I > use NLOPT_LD_SLSQP and start with initial weights of 1/3, I may obtain > (0.47, 0, 0.53) vs (0.52, 0, 0.47). If I start with (0.52, 0, 0.47), > I do get (0.52, 0, 0.47)... > > When I use NLOPT_GN_ISRES or other nloptr solvers that permit equality > constraint sum(weights)=1 with initial weights of 1/3, I obtain > (almost) the same initial weights after 2 iterations with > xtol_rel=1.0e-8... > > I remember from my MC simulations of protein structures (20 years ago) > that sampling is key due to multiple local minimums but is it so bad > for a simple portfolio? > > > I'll greatly appreciate relevant comments. > > Alec [...] Unless your covariance matrix is 'broken' in some way, a minimum-variance portfolio with only a budget constraint should be fairly easy to compute (no multiple local minima, smooth objective function, ...). Please provide a reproducible example. Kind regards, Enrico -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] comparing solve.pq and nloptr for min variance portfolio
On Fri, 18 Mar 2016, Alec Schmidt <aschm...@stevens.edu> writes: > Hi Enrico, > Many thanks for your interest. I attach my script and input file with > asset tickers. Sorry for lots of unrelated stuff - it's a working > draft. > > Alec Thanks for sending the script, Alec. But you will need to simplify it if people are to help you. [My bad: I should have said _minimal_ reproducible example: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example ] > ________ > From: Enrico Schumann <e...@enricoschumann.net> > Sent: Friday, March 18, 2016 10:25 AM > To: Alec Schmidt > Cc: R-SIG-Finance@r-project.org > Subject: Re: [R-SIG-Finance] comparing solve.pq and nloptr for min variance > portfolio > > On Fri, 18 Mar 2016, Alec Schmidt <aschm...@stevens.edu> writes: > >> I'm puzzled that I cannot reproduce results for asset weights using >> solve.pq and nloptr even in the case of just three assets. E.g. if I >> use NLOPT_LD_SLSQP and start with initial weights of 1/3, I may obtain >> (0.47, 0, 0.53) vs (0.52, 0, 0.47). If I start with (0.52, 0, 0.47), >> I do get (0.52, 0, 0.47)... >> >> When I use NLOPT_GN_ISRES or other nloptr solvers that permit equality >> constraint sum(weights)=1 with initial weights of 1/3, I obtain >> (almost) the same initial weights after 2 iterations with >> xtol_rel=1.0e-8... >> >> I remember from my MC simulations of protein structures (20 years ago) >> that sampling is key due to multiple local minimums but is it so bad >> for a simple portfolio? >> >> >> I'll greatly appreciate relevant comments. >> >> Alec > > [...] > > Unless your covariance matrix is 'broken' in some way, a > minimum-variance portfolio with only a budget constraint should be > fairly easy to compute (no multiple local minima, smooth objective > function, ...). Please provide a reproducible example. > > Kind regards, > Enrico -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] Failure of solve.QP in portfolio modeling
On Tue, 22 Sep 2015, aschmid1 <aschm...@stevens.edu> writes: > Hi everyone, > I'm trying to estimate optimal Markowitz portfolio weights for a list > of stocks chosen upon some criterion using solve.QP from quadprog > library. When the number of stocks N reaches some limit, I get a > message "matrix D in quadratic function is not positive definite." For > example, if I rebalance every 6 weeks (which implies that variance is > calculated for 6-week interval prior to the period for which I > calculate portfolio weights), I can get solution for 25>=N<50. For > 12-week interval, solution exists for 50>=N<100, and for 24-week > interval, I can get solution for N=100. My attempt to remedy this > problem with Higham's method doesn't help. I'll greatly appreciate you > input: first, why this may happen (can there be lack of local > minimum?), and second, whether there are R solvers that may need only > semi positive definite matrix. > > Thanks! Alec > The thing you may want to look up is the "rank" of a matrix. For instance, I create a small data set R -- suppose these were daily-returns data of 10 equities. na <- 10 ## number of assets nobs <- 10 ## number of observations R <- array(rnorm(nobs * na, sd = 0.01), dim = c(nobs, na)) qr(cov(R))$rank The rank of the covariance matrix is only 9; you need na+1 observations to get full rank. You can still compute the standard deviation of a portfolio: ew <- rep(1/na, na) ## equal-weight portfolio sqrt(ew %*% cov(R) %*% ew) But with a non-full rank matrix and no constraints, it is guaranteed that you have portfolios like this one: zerovol <- svd(cov(R))$v[,10] sqrt(abs(zerovol %*% cov(R) %*% zerovol)) You get a zero-volatility portfolio. Whether that matters depends on your application. With constraints, perhaps not. An example in which it does not matter is in Section "1.3 Redundant assets" in https://cran.r-project.org/web/packages/NMOF/vignettes/TAportfolio.pdf There you also have an example of a solver. A lengthier discussion is in Section "13.2.5 Repairing Matrices" in this book @BOOK{Gilli2011b, title= {Numerical Methods and Optimization in Finance}, publisher= {Elsevier/Academic Press}, year = 2011, author = {Gilli, Manfred and Maringer, Dietmar and Schumann, Enrico} } of which [disclosure], I am a co-author. -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] assetsLPM from fAssets and 0.moment
On Tue, 12 May 2015, Nils Tobias Kramer ntobiaskra...@gmail.com writes: Hi there, I was wondering how to calculate LPMs in R. My interpretation of the 0.moment is somehow different from what I can see in fAssets: #using quantmod to get just any data getSymbols(IBM) x - weeklyReturn(IBM) #using fAssets to calculate lpm, 0.order tau - 0 #to make things easier assetsLPM(x,tau,0) #returns 1 $mu weekly.returns 1 $Sigma [,1] [1,]0 attr(,control) a tau 0 0 When I think of lpm 0 I think of the probability that my returns are below tau. This is what I expect let's say from Matlab. Code of assetsLPM: TauX - Tau - x #0-x in my case X.max - ((TauX) + abs(TauX))/2 #equals max(0,-returns), so either 0 for positive returns and for negative returns the absolute value of that return LPM - colMeans(X.max^a) Checking what R does by ^0: -1^0 [1] -1 0^0 [1] 1 1^0 [1] 1 I can see why the sum is expected to be 1 as there are only elements either 0 or positive which are taken to the power of 0 which equals 1 for each entry. Somehow I can't get my interpretation (probability) and the output of 1 together. Maybe someone can help me out? Aside, this here confuses my a little: getSymbols(IBM) x - weeklyReturn(IBM) y - -x head(y^0) weekly.returns 2007-01-05 1 2007-01-12 1 2007-01-19 1 2007-01-26 1 2007-02-02 1 2007-02-09 1 head(y) weekly.returns 2007-01-05 -0.002469644 2007-01-12 -0.019708479 2007-01-190.031910610 2007-01-26 -0.013309764 2007-02-02 -0.017650077 2007-02-090.006251891 Now all entries are 1 when taken to the power of 0 whereas I expected only the positive numbers to turn to 1 and the negative ones to -1!? Anyways, the first and second moments of assetsLPM are just what I expected, so I was wondering why I can't figure out the reason for the results of the 0. moment. Any insights? Thanks Toby I am not sure I understand your question, or your questions. Since no one else answered, let me venture two remarks. First, on partial moments. The standard way to compute a partial moment from a sample is this: (i) sum over all returns below tau and raise them to zeroth power (in your case). Any number below tau will map to one, and any number above tau will map to zero. (ii) divide this sum by the total number of observations. So, for instance, with 10 observations and 5 of them below tau, you will get 5/10 = 0.5, which is the empirical frequency of returns below tau in your sample. Hence the interpretation of probability. Second, on raising negative numbers to the zeroth power: you need to consider operator precendence. The ^ binds more tightly than -, hence -1^0 is -1. But (-1)^0 will give 1. -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] Finding the strike price of an option from all other data
On Sun, 17 May 2015, Kunal Shah kunalshah...@gmail.com writes: Hello, If I have all the other data and if I need to find the strike price of an option, some numerical technique needs to be applied Can someone guide me how to do this? One possible solution is 1: Initialise with some random strike 2: Use Black Scholes and find the price 3:Increase/Decrease the strike accordingly and do steps 1 and 2 until you reach at the answer I think some numerical techniques must be there to dot his Regards What you look for is called root finding or zero finding. See ?uniroot . -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] DEoptim and guarantees
On Sun, 24 Aug 2014, alexios ghalanos alex...@4dscape.com writes: On 24/08/2014 19:24, Enrico Schumann wrote: On Fri, 22 Aug 2014, alexios ghalanos alex...@4dscape.com writes: 1. DEoptim is a nonlinear global optimization solver. Global optimization is usually reserved for hard to solve non-convex problems with many local minima. There is no guarantee of optimality not even for convex problems, nor any idea of whether the answer you are getting is anything other than a local optimum. There is no *mathematical* guarantee. But that does not imply that you cannot use Differential Evolution (the method that DEoptim implements) with confidence. Just because you cannot prove something does not mean that it is not the case. Accepted, but I'm sure you are not saying that a convex problem which can be confidently and quickly solved by convex solvers should instead be solved by differential evolution or other global optimization solvers? I can think of (and remember) situations in which using a heuristic is fine, even if one could have used a more-specialised/more-efficient algorithm. The key advantage of a heuristic is flexibility. Even if a model is convex initially, it may quickly become non-convex because of a constraint that is added. At least in my world, this happens all the time. You do not need mathematical proofs to make meaningful statements about whether or how well an optimisation method works.[*] For a given model class (such as particular portfolio-selection models), you can run experiments. Experimental results are no general proof, of course; but they are evidence of how a method performs for that particular type of model, and typically that is all that we care about when we apply a method. In other words, you may not be able to mathematically prove that a method works, but you can have empirical evidence that is does. Yes, but if the problem is convex, then there is one solution, and this can usually be attained quite quickly with specialized convex solvers. In practical optimisation, it is not useful to think of the [optimal] solution to a model, and all the rest. An appropriate way to think of it is no solution, some solution, a better solution, an even better solution, ... and so on. That is, think of iterative improvement, not of optimisation. If by practical optimization you mean problems which are non-convex or particularly difficult to solve (e.g. mixed integer), then perhaps. But for convex problems, there is only one solution (by definition). Whether that solution turns out to be sub-optimal in the out of sample, that is down to uncertainty, quality of inputs etc. There is one solution to the model. But with a sensible model, a solution close to the optimum should be better (in terms of its objective-function value) than a randomly-chosen solution. So the quality of a solution should not be considered either optimal or non-optimal, but there are shades of grey, even for convex problems. These shades of grey exist just as well in the out-of-sample period. This may be hard to square with optimisation theory, but it makes sense for the application. However, I have seen a tendency to equate practical optimization, with a simply lazy consideration of an optimization problem without making the effort to see whether that problem can be put in a convex form. Plug-and-play, without making some effort to understand the problem is never, IMHO, a good way to do things. By practical optimisation I mean optimisation in which the results are actually used for financial decision-making, and hence the results and their interpretation are the main priority. Practical optimisation is conditional, of course, on having trustworthy algorithms. As I said, empirical evidence goes a long way when it comes to creating trust and confidence. But here is my point: an optimisation algorithm is the tool, and once we have established that the tool works sufficiently well (ie, is trustworthy), we can stop. Finding the optimal algorithm is not the goal in practical optimisation; good enough is enough. The actual goal is to find good models. In my experience, the thing that one often lacks in practical optimisation is time. I do not mean the time that is saved when running a faster algorithm, but the time to rewrite code that works (just to have a more efficient algorithm). Understanding a model, then, has less to do with how one solves the model, but with analysing how results change when inputs change, how results look for different datasets, or how specific statistical properties of the data affect the results. Or (to come back back what I said before) how different in-sample shades of grey relate to out-of-sample results. [For a sensible model, I would want to see a positive correlation between the in-sample quality of solutions and their out-of-sample quality.] [*] If you need an example other than Differential Evolution
Re: [R-SIG-Finance] Calibration of Heston Model in R
On Sun, 13 Jul 2014, Shivam shivamsi...@gmail.com writes: Hi All, It is a very basic question, in the sense that I need to start from scratch. I need to know what are the resources available in R to calibrate the Heston model. A lot of help is available for MATLAB, but I have been working with R for a while now and dont want to migrate. I know the basic idea of the calibration and the theory around the model, but am at a loss as to how do I really go about the calibration in R. Any inputs would be of great help. Thanks in advance, Shivam It is some time that I have worked with the Heston model (and I have never used it since, and it was with MATLAB), but a few pointers. In terms of computational tools you will need two things: 1) A pricing mechanism (there are several) There is an implementation of a pricing method in the NMOF package [1], but for calibration you may need something faster. The pricing method we used is described in detail in chapter 15 of Gilli et al [2]. I had started a translation into R in the NMOF manual [3], but it is not complete. 2) An optimisation algorithm (there are several) Have a look at the Optimization Task View [4]. Since the problem is only mildly constrained, I would suggest Differential Evolution. (Classical -- derivatives-based -- methods did not work very well for me.) See package DEoptim or function DEopt in package NMOF. As I said, just a few pointers. Regards, Enrico [1] Disclosure: I am the package author. [2] http://enricoschumann.net/NMOF.htm#Book [3] http://enricoschumann.net/NMOF.htm#NMOFmanual [4] http://cran.r-project.org/web/views/Optimization.html -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] Errors in assetsFit function of fPortfolio package
On Fri, 31 Jan 2014, Mohammad Nikzad mnik...@willamette.edu writes: Dear Sir/Madam I get an error when I run a assetsFit function of fPortfolio package. You just need to load the fPortfolio package and run this command to get the same error that I get. The only fitting method that works is norm function.The snorm and st methods give error. library(fPortfolio) fit - assetsFit(LPP2005.RET[, 1:3], method = st) Error in .mvstFit(x = x, fixed.df = fixed.df, trace = trace, ...) : could not find function mst.mle Thank you in advance for your help. Best, Arsa Nikzad I cannot reproduce this error. If you suspect a bug in the package, then you should contact the package maintainer. But please provide more information -- at the minimum, send the output of ?sessionInfo. Here is mine: sessionInfo() R version 3.0.2 (2013-09-25) Platform: x86_64-pc-linux-gnu (64-bit) locale: [1] LC_CTYPE=en_GB.UTF-8 LC_NUMERIC=C [3] LC_TIME=en_US.UTF-8LC_COLLATE=en_GB.UTF-8 [5] LC_MONETARY=en_US.UTF-8LC_MESSAGES=en_GB.UTF-8 [7] LC_PAPER=en_US.UTF-8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] fPortfolio_2130.80 fAssets_2110.79fCopulae_3000.79 sn_0.4-18 [5] mnormt_1.4-5 fBasics_3010.86timeSeries_3010.97 timeDate_3010.98 [9] robustbase_0.9-10 MASS_7.3-29 loaded via a namespace (and not attached): [1] quadprog_1.5-5 Rglpk_0.4-1 slam_0.1-28 stabledist_0.6-6 -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] Range intersections
On Sat, 27 Jul 2013, Mark Knecht markkne...@gmail.com writes: For the sake of asking here assume I have 5 models that attempt to predict a pair of future values like tomorrow's high low. The predictions are placed in a matrix with the low prediction in column 1 and the high prediction in column 2. _IF_ there is an intersection of all 5 predictions then I can test for that using the simple equation in Range1. However if there is a prediction that's a complete outlier as is Pred2[4,] then the simple equation fails and says the high is less than the low. Are there any R packages that automatically handle simple intersection problems like this? My real goals include things like probability distributions across the ranges, but for now I'm just looking for what's out there? Thanks, Mark Pred1 = matrix(data=NA, nrow=5, ncol=2) Pred1[1,] = c(100,110) Pred1[2,] = c(88,125) Pred1[3,] = c(92,120) Pred1[4,] = c(25,105) Pred1[5,] = c(91,121) Range1 = c(max(Pred1[,1]), min(Pred1[,2])) Range1 Pred2 = matrix(data=NA, nrow=5, ncol=2) Pred2[1,] = c(100,110) Pred2[2,] = c(88,125) Pred2[3,] = c(92,120) Pred2[4,] = c(25,70) #outlier - the high is too low Pred2[5,] = c(91,121) Range2 = c(max(Pred2[,1]), min(Pred2[,2])) Range2 Yes, taking the intersection fails because in the second case the intersection is empty. So /you/ will have to decide how you want to handle such cases. Perhaps remove the outlier, or use some majority rule to find a most likely outcome (according to your preditions). For instance, you could count how often a given price falls in the range of any of the models. ind - function(x, min, max) { s - numeric(length(x)) for (i in seq_along(min)) s - s + as.integer(x = min[i] x = max[i]) s } x - seq(0, 130, by = 0.5) s - ind(x, Pred2[ ,1], Pred2[ ,2]) plot(x, s, type = S) range(ca - x[s == max(s)])## consensus area all.equal(max(diff(ca)), diff(x)[1L]) ## single area? Regards, Enrico -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] Warning: timeLastNdayInMonth gets Fridays one week off
On Wed, 12 Jun 2013, Ilya Kipnis ilya.kip...@gmail.com writes: For those of you looking to find the last Fridays in a month (quarter, etc.), I just wanted to issue a warning about using the timeLastNdayInMonth function. For instance, run these lines: #dates-seq(as.Date(2010-01-01),as.Date(2013-04-02),by=day) test case friDates-dates[which(weekdays(dates)==Friday)] buggedLastMonthlyFridays-unique(timeLastNdayInMonth(friDates,5)) #gives one week after the last friday of the month and one will notice that the dates are often at the beginning of the next month. This is my fix: buggedLastMonthlyFridays[which(as.numeric(substr(buggedLastMonthlyFridays,9,10))20)]- as.Date(buggedLastMonthlyFridays[which(as.numeric(substr(buggedLastMonthlyFridays,9,10))20)])-7 It's not exactly pretty, but I hope this helps someone out there. -Ilya Kipnis I suppose the 'timeLastNdayInMonth' function is from a package? Then you should also contact directly the package's maintainer. -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] Passing variables...
On Mon, 20 May 2013, Dominykas Grigonis dominykasgrigo...@gmail.com writes: Hello, I am writing a scenario analysis for option strategies. I am trying to build a framework, such that new pricing functions can be added as well as volatility surface, interest rate term structure, etc… But for now I am sticking to ear vanilla case. Anyways, the problem is not actually related to finance, but I could not find any answer so far. fun1 - function(a,b){fun2(a)} fun2 - function(c){fun3(c)} fun3 - function(d){d+b} fun1(2,2) #Error in fun3(c) : object 'b' not found I have always thought that if functions can not find a variable it gradually goes back up through each single environment looking for that variable. However, now it is apparent, that it only looks for it in .GlobalEnv. If a variable is not found during function evaluation, R will indeed move through the environments that enclose the function's environment. But the function's environment is the one in which the function was *created*, not where it was *called*. Example: fun1 - function() a + 1 fun2 - function(f){ a - 5 f() } fun2(fun1) ## Error in f() (from #1) : object 'a' not found This is an error because 'a' was not defined in environment of 'fun1'. But the following would work, even though 'a' does not exist in the global environment. makefun - function() { a - 5 function() a + 1 } fun3 - makefun() fun2(fun3) [...] anyways, you got the idea of my issue. I do not want to pass all of these via arguments through 2 functions. The main reason is that it becomes messy and complicates the code significantly. I was thinking My suggestion would still be: pass everything. If you don't like to have too many arguments, collect all information in one list, 'Data', say. Then you only have to pass one object. In my experience, the increased 'complication' of adding more arguments to your functions is nothing compared with the trouble you will have when you need to debug your code. Do you have any ideas how to overcome this issue? I could share the code, but there is quite a lot of it. I hope you understand my problem and I will appreciate any help. Thank you in advance. You could of course assign all variables in the global environment. But if you really want to use environments, I would suggest to make this dependence explicit: Data - new.env() Data$a - 5 environment(fun1) - Data ls() ## [1] Data fun1 fun2 ls(environment(fun1)) ## [1] a fun2(fun1) ## [1] 6 It is easier then to keep track of what you add to or remove from 'Data'. Regards, Enrico Kind regards,-- Dominykas Grigonis [[alternative HTML version deleted]] Please send plain-text messages to this mailing list. -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] euro call by integration
On Sat, 13 Apr 2013, Dominykas Grigonis dominykasgrigo...@gmail.com writes: Valuing european call option by taking expectation and integrating: gmb - function(x,s0=100,r=0.05,vol=0.1){ s0*exp( (r - vol^2/2) + vol * x)} fun - function(x, K=100){ (gbm(x) - 100) * dnorm(x)} min = -(log(1) + (0.05-0.1^2/2)) / 0.1 integrate(fun,min,Inf) could someone tell me what am I doing wrong? I do not even know if this is R related question or not... returns 7.153855, while actual risk neutral price is 6.804958 Thank you in advance. Kind regards,-- Dominykas Grigonis I did not run your code, but shouldn't you discount the result? -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] ibrokers - problems retrieving Indices/ETFs
Hi Matti, On Tue, 19 Mar 2013, Matti Zemack matti.zem...@gmail.com writes: Hi all, First of all; Thanks Jeff for the ibrokers package. It's amazing. My simple first code looks like Jeffs demo; library(IBrokers) tws - twsConnect() spy.csv - file(SPY.csv, open=w) reqMktData(tws, twsEquity(SPY, exch=ARCA), eventWrapper=eWrapper.MktData.CSV(1), file=spy.csv) Working perfectly fine. Now I try to read an Index/ETF from ibrokers to file. I get 2 1 300 Can't find EId with tickerId:1 reqMktData(tws, ttwsEquity(SPX, exch=CBOE), eventWrapper=eWrapper.MktData.CSV(1), file=spy.csv) I had this problem also previously for the first request, and then simply subscribing to correct data feed at IB helped me out. Newbie fault. This time I suspect I don't receive data is because twsEquity/twsSTK isn't made for Indices/ETFs. If this is so; could someone please nudge me some pointers to where in iBrokers package I could start trying to fix this. did you really write ttwsEquity? Perhaps try ?twsContract (untested): myCon - twsContract(local = SPX, sectype = IND, exch = CBOE, currency = USD, include_expired = 1, conId = , symbol = , primary = , expiry = , strike = , right = , multiplier = , combo_legs_desc = , comboleg = , secIdType = , secId = ) regards, Enrico Thanks and Regards, Matti Zemack - Matti Zemack, Broadcast Technology, Stockholm, Sweden matti.zem...@gmail.com -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] Portfolio construction with integer constraints
On Thu, 14 Feb 2013, Sigurdsson, Magnus msigurds...@tiff.org writes: Dear R Finance enthusiasts I was wondering if anyone had experience with using R for portfolio construction with integer constraints, such as mean variance optimization, or equal risk contribution optimization, where you can only have integer number of shares/contracts in each underlying asset? The case I am referring to is, for example, a futures trading strategy, where you trade a few different futures markets. Due to the generally large nominal size of futures contract, your budget allows you only to trade 1 to 5 contracts of each different asset. The problem being creating an optimal portfolio with those constraints. One way to solve such models is via heuristics like Genetic Algorithms. There are a number of packages in R that implement such methods, but typically you have to do some programming yourself. There has been some discussion of such methods on this list in the past; Patrick Burns has also had some blog posts about R implementations (there you could get some pointers to R packages). Best regards, Enrico -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] Automatically sending .Rmd - html files
Hi, With all the buzz around R Markdown, has anyone yet experimented with getting R Markdown files to automatically run, convert to html as usual and then attach / run in-line on an email and send it? I wasn't aware of any buzz :-) But seriously, as you mentioned yourself, this is not really a finance question. So you may get better answers on R-help. I don't use Markdown; but still, some thoughts/pointers: (1) To borrow Perl's slogan, there is more than one way to do it. And this is definitely the rule here: just find some setup that works for you. In the end, you will anyway collect all pieces in a script, ie, a batch file on Windows. The Task Scheduler accepts such batch files. (If you want complete automation, you will also want to think about error-handling: Hm, you want me to send this file, but it is more than 24h old.) I wouldn't expect to do everything in R; there are many incredibly-useful tools that can be run from the command line, even on Windows (as an example, I use Sweave to automatically create tex-files; tex-files could be transformed into HTML via tools like Pandoc). You may also want to have a look at the CRAN Task View for Reproducible Research. (2) You will run R non-interactively, so have a look at the documentation of RScript and R CMD BATCH. (3) You can send e-mails from the command line on Windows with programmes like Blat ( http://www.blat.net/ ). This may also allow you to send HTML mails, but I wouldn't know: I only send and read plain-text mails, ie, the format that this mailing list expects. Regards, Enrico I think I can put this together with a cron job, but i'd be interested in thoughts on running this via task Scheduler on Win 7; so far I've not found a way of getting Task Scheduler to associate .Rmd files with R. Ideally, what I'm trying to achieve is, at a given time of day (say 6am): * Launch R * Run `xyz.Rmd` * Take the standard output (`xyz.html') and use in the body of an email (or, at least, have an attachment of the html) * Send the email Any thoughts very much appreciated. I know this isn't strictly finance, but I can imagine the finance community probably has the most amount of use for something like this on a daily basis (emailing automated analysis to yourself / desk etc) n. -- Enrico Schumann Lucerne, Switzerland http://nmof.net ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] Are there genetic algorithm for trading strategy evolution in R?
Am 08.03.2012 19:16, schrieb Michael: Thanks folks! After digging further on the Internet, I have the following questions: Q1: I read the following article: http://cran.r-project.org/web/packages/DEoptim/vignettes/DEoptimPortfolioOptimization.pdf It seems that there are a bunch of parameters in this optimizer and the results are sensitive to these parameters. So there is another layer of optimization with respect to these optimizer parameters. Is the tweaking of these optimizer parameters data-mining, which will lead to data-snooping bias? No. You need to distinguish between your optimisation model on the one hand, and the numerical technique you use to solve the model on the other. And Differential Evolution (DE) is a numerical technique. If your model overfits, it is because of the model, not the optimisation technique. Suppose you wanted to estimate a linear regression, with the mean squared residual as the criterion of fit. Now you can compute a solution via QR; or if you use DE *properly*. it will give you exactly the same fit (up to numerical precision). Whether the model overfits, depends on how you set up the model, how you select the data in the model -- it has nothing to do with the numerical technique. Q2: Due to the random nature of the optimizer, each time you run the backtest, you will have different performance. What do you do in that case? There is only one way to find out: run experiments. Put an outer loop around your backtest in which repeat your analysis; then you can see how the stochastic nature of the optimisation affects your results. In my view, it's not a problem: see for instance this paper http://ssrn.com/abstract=1420058 And experiments are also the only reliable way to find out if your optimisation technique works properly. All described in detail in this book (of which I happen to be a co-author) @BOOK{Gilli2011b, title = {Numerical Methods and Optimization in Finance}, publisher = {Academic Press}, year = {2011}, author = {Gilli, Manfred and Maringer, Dietmar and Schumann, Enrico} } So for out-of-sample real-trading, we are trading a random strategy? No, again: model vs optimisation. Eventually, you have to accept some set of parameters for your optimisation and use these to trade. Q3: It's pretty easy to understand using Genetic Algorithms to serve as a replacement for regular optimizers; but using Genetic Algorithms to evolve trading strategies seem to be different. Anywhere we could find such an example in R? What you probably mean is Genetic Programming, not Genetic Algorithms. Regards, Enrico -- Enrico Schumann Lucerne, Switzerland http://nmof.net On Thu, Mar 8, 2012 at 8:25 AM, Zachary Mayerzach.ma...@gmail.com wrote: There is the DEoptimhttp://cran.r-project.org/web/packages/DEoptim/index.htmllibrary in r, which is an excellent library for differential evolution. If you can define your trading strategy in terms of a bunch of parameters to adjust and an objective function (i.e. turn it into an optimization problem), DEoptim will help you find the minimum (or maximum). DEoptim works well on non-differentiable problems with many local minima. Here is an example of using it to solve a portfolio optimization problem: http://cran.r-project.org/web/packages/DEoptim/vignettes/DEoptimPortfolioOptimization.pdf On Thu, Mar 8, 2012 at 12:43 AM, Sofian Hadiwijayareztinpe...@gmail.comwrote: how about quantmod library.. On Wed, Mar 7, 2012 at 10:30 PM, Michaelcomtech@gmail.com wrote: Hi all, Good morning, good afternoon and good evening! Could anybody please kindly point me to resources in R which shows about how to use Genetic algorithm to evolve trading strategies? I did a lot search on Google these days and certainly it's a well-covered and popular topic, but I don't see anywhere in R... Thanks a lot! ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] American option sensitivities
a experiment with different levels of 'h', where you can clearly see when the gamma diverges. |h | gamma | |1 | 0.01905385 | | 0.01 | 0.01905612 | | 1e-4 | 0.01905605 | | 1e-5 | 0.01915801 | | 1e-6 | 0.03463896 | | 1e-8 | 8.881784 | In the literatur, you find a number of tricks to smooth the function, but in my experience, you are fine if you make 'h' small with respect to 'x' -- small, not tiny. So if the stock price is 100, a change of 1 or 0.1 is small. (And think of it: even if we found that a change of one-thousandth of a cent led to a meaningful numerical difference; if the stock price never moves by such an amount, such a computation would not be empirically meaningful.) Regards, Enrico James | | Thanks for any clarification. | | Best, | | | James | | ___ | R-SIG-Finance@r-project.org mailing list | https://stat.ethz.ch/mailman/listinfo/r-sig-finance | -- Subscriber-posting only. If you want to post, subscribe first. | -- Also note that this is not the r-help list where general R questions should go. -- Outside of a dog, a book is a man's best friend. Inside of a dog, it is too dark to read. -- Groucho Marx ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go. -- Enrico Schumann Lucerne, Switzerland http://nmof.net/ ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] American option sensitivities
Am 10.02.2012 15:24, schrieb J Toll: On Fri, Feb 10, 2012 at 3:18 AM, Enrico Schumann enricoschum...@yahoo.de wrote: Hi all, (comments below) Am 10.02.2012 01:02, schrieb J Toll: On Thu, Feb 9, 2012 at 5:17 PM, Dirk Eddelbuettele...@debian.orgwrote: On 9 February 2012 at 17:06, J Toll wrote: | Hi, | | I'd like to calculate sensitivities on American options. I was hoping | somebody might be able to summarize of the current state of that | functionality within the various R packages. It's my understanding | that the fOptions package can calculate greeks for European options | but not American. RQuantLib appears to have had the ability to | calculate greeks for American options at one point, but it appears | that functionality was removed in Release 0.1.8 sometime around | 2003-11-28. ... because that functionality was removed upstream by QuantLib. | | http://lists.r-forge.r-project.org/pipermail/rquantlib-commits/2010-August/000117.html | | Additionally, from RQuantLib ?AmericanOptions says, | | Note that under the new pricing framework used in QuantLib, binary | pricers do not provide analytics for 'Greeks'. This is expected to be | addressed in future releases of QuantLib. | | I haven't found any other packages for calculating option | sensitivities. Are there any other packages? | | Regarding RQuantLib, is the issue that that functionality hasn't been | implemented in R yet, or is it QuantLib that's broken? There is a third door behind which you find the price: numerical shocks. Evaluate your american option, then shift the various parameters (spot, vol, int.rate, time to mat, ...) each by a small amount and calculate the change in option price -- voila for the approximate change in option value for change input. You can also compute twice at 'x - eps' and 'x + eps' etc. Dirk Dirk, Thank you for your response. I was hoping you might reply. I understand the concept of your suggestion, although I don't have any practical experience implementing it. I'm guessing this is what's generally referred to as finite difference methods. In theory, the first order greeks should be simple enough, although my impression is the second or third order greeks may be a bit more challenging. I hate to trouble you for more information, but I'm curious why? Is this the standard method of calculating greeks for American options? Has QuantLib decided not to implement this calculation? Just curious. Thanks again, A simple forward difference is [f(x + h) - f(x)] / h 'f' is the option pricing formula; 'x' are the arguments to the formula, and 'h' is a small offset. Numerically, 'h' should not be made too small: (1) Even for smooth functions, we trade off truncation error (which is large when 'h' is large) against roundoff-error (in the extreme, 'x + h' may still be 'x' for a very small 'h'). (2) American options are typically valued via finite-difference or tree methods, and hence 'f' is not smooth and any 'bumps' in the function will be magnified by dividing by a very small 'h'. So when 'h' is too small, the results will become nonsensical. Here is an example. As a first test, I use a European option. require(RQuantLib) h- 1e-4 S- 100 K- 100 tau- 0.5 vol- 0.3 C0- EuropeanOption(type = call, underlying = S, strike = K, dividendYield = 0.0, riskFreeRate = 0.03, maturity = tau, volatility = 0.3) Cplus- EuropeanOption(type=call, underlying = S + h, strike = K, dividendYield = 0.0, riskFreeRate=0.03, maturity=tau, volatility=0.3) Cminus- EuropeanOption(type=call, underlying = S - h, strike=K, dividendYield=0.0, riskFreeRate=0.03, maturity=tau, volatility=0.3) ## a first-order difference: delta (Cplus$value-C0$value)/h ## [1] 0.570159 C0$delta ## [1] 0.5701581 ## a second-order difference (Cplus$delta-C0$delta)/h ## [1] 0.01851474 C0$gamma ## [1] 0.01851475 Now for an American option. Here we don't have the delta, so we first need to compute it as well. C0- AmericanOption(type=put, underlying = S, strike=K, dividendYield=0.0, riskFreeRate=0.03, maturity=tau, volatility=vol) Cplus- AmericanOption(type=put, underlying = S + h, strike=K, dividendYield=0.0, riskFreeRate=0.03, maturity=tau, volatility=vol) Cminus- AmericanOption(type=put, underlying = S - h, strike=K, dividendYield=0.0, riskFreeRate=0.03, maturity=tau, volatility=vol) ## a first-order difference: delta (dplus- (Cplus$value - C0$value)/h) (dminus-(C0$value - Cminus
Re: [R-SIG-Finance] Monte Carlo Option Pricing formula R code vs Matlab
subsidiaries have effected or may effect transactions for their own account in any investments or related investments referred to herein. In the case of certain securities Commonwealth Bank is or may be the only market maker. -Original Message- From: Enrico Schumann [mailto:enricoschum...@yahoo.de] Sent: Thursday, 2 February 2012 8:45 PM To: Roupell, Darko Cc: r-sig-finance@r-project.org Subject: Re: [R-SIG-Finance] Monte Carlo Option Pricing formula Hi, Darko, Am 02.02.2012 07:44, schrieb Roupell, Darko: Hi All, I am trying to cross check option implied employee option price that was derived using Monte Carlo simulation. Below is code and parameter used and after accounting for dividend yield ( 1.46%) the derived option price is 206.8843 using the code snippet provided. Approx 1 cent below 207.95 that is listed in company prospect using their Monte Carlo simulation and below parameters. As we all know number of iteration can also slightly impact the average price, but am I rightly concerned that my methodology may not be correct? Hm, I have not really looked at your programme so I cannot comment whether it is correct. But we are talking about a difference of about half a percentage point here. Which is not much. I just ran you script 20 times. summary(results) Min. 1st Qu. MedianMean 3rd Qu.Max. 205.6 206.0 206.3 206.3 206.8 206.9 Admittedly, all results are all below the company's price, but nevertheless: they vary. There are details that they might have done differently. For instance, you do not compound (if I see correctly). What if you replaced riskFreeRate*timeToExpiry with (1+riskFreeRate)*timeToExpiry-1 But even if that gives you the price: from a practical point of view, the difference is small, really. (Much better would be to check what would happen if the div did not turn out as expected, if the vol were different, etc) Regards, Enrico Any feedback will be appreciated. exercisePrice = 0; timeToExpiry= 3;#% in years underlyingPrice = 490; #% underlying in cents expectedVol = 0.5; #% expected volatility expectedDiv = 0.0146;#% expected dividend in cents riskFreeRate= 0.0425; #% interest rate itr = 50 #% number of iterations delS = 0*array(0,itr) for( i in (1:itr)) { eps = rnorm(1) #% random number generator dS = expectedDiv*underlyingPrice+underlyingPrice*(riskFreeRate*timeToExpiry) + (underlyingPrice*expectedVol*eps*sqrt(timeToExpiry)) mv = dS - exercisePrice; delS[i] = max(mv,0); } mean(delS) __ Commonwealth Bank Darko Roupell Associate Quantitative Analyst Institutional Banking Markets Equities Research Darling Park Tower 1 Level 23, 201 Sussex Street Sydney, NSW 2000 P: +61 2 9117 1254 F: +61 2 9118 1000 M: +61 400 170 515 E: darko.roup...@cba.com.au [...] ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go. -- Enrico Schumann Lucerne, Switzerland http://nmof.net/ ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] Monte Carlo Option Pricing formula
Hi, Darko, Am 02.02.2012 07:44, schrieb Roupell, Darko: Hi All, I am trying to cross check option implied employee option price that was derived using Monte Carlo simulation. Below is code and parameter used and after accounting for dividend yield ( 1.46%) the derived option price is 206.8843 using the code snippet provided. Approx 1 cent below 207.95 that is listed in company prospect using their Monte Carlo simulation and below parameters. As we all know number of iteration can also slightly impact the average price, but am I rightly concerned that my methodology may not be correct? Hm, I have not really looked at your programme so I cannot comment whether it is correct. But we are talking about a difference of about half a percentage point here. Which is not much. I just ran you script 20 times. summary(results) Min. 1st Qu. MedianMean 3rd Qu.Max. 205.6 206.0 206.3 206.3 206.8 206.9 Admittedly, all results are all below the company's price, but nevertheless: they vary. There are details that they might have done differently. For instance, you do not compound (if I see correctly). What if you replaced riskFreeRate*timeToExpiry with (1+riskFreeRate)*timeToExpiry-1 But even if that gives you the price: from a practical point of view, the difference is small, really. (Much better would be to check what would happen if the div did not turn out as expected, if the vol were different, etc) Regards, Enrico Any feedback will be appreciated. exercisePrice = 0; timeToExpiry= 3;#% in years underlyingPrice = 490; #% underlying in cents expectedVol = 0.5; #% expected volatility expectedDiv = 0.0146;#% expected dividend in cents riskFreeRate= 0.0425; #% interest rate itr = 50 #% number of iterations delS = 0*array(0,itr) for( i in (1:itr)) { eps = rnorm(1) #% random number generator dS = expectedDiv*underlyingPrice+underlyingPrice*(riskFreeRate*timeToExpiry) + (underlyingPrice*expectedVol*eps*sqrt(timeToExpiry)) mv = dS - exercisePrice; delS[i] = max(mv,0); } mean(delS) __ Commonwealth Bank Darko Roupell Associate Quantitative Analyst Institutional Banking Markets Equities Research Darling Park Tower 1 Level 23, 201 Sussex Street Sydney, NSW 2000 P: +61 2 9117 1254 F: +61 2 9118 1000 M: +61 400 170 515 E: darko.roup...@cba.com.au [...] ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go. -- Enrico Schumann Lucerne, Switzerland http://nmof.net/ ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] The challenge to fit the greek gov curve with termstrc R Package!
: num [1:13] 4.375 104.375 0.426 100.404 0.45 ... .. ..$ DATE:Class 'Date' num [1:13] 15380 15746 15335 15426 15375 ... ..$ TODAY :Class 'Date' num 15316 - attr(*, class)= chr couponbonds Christos Arvanitis Assistant Manager Treasury Support Operations PIRAEUS BANK S.A. 5 Korai str., Athens, GR 10564 Tel: • +30 210 3288678 Mobile: Ë+30 6936114432 e-mail:arvaniti...@piraeusbank.grmailto:arvaniti...@piraeusbank.gr MESSAGE CONFIDENTIALITY SECURITY NOTICE = This message and/or its attachments may contain confidential and privileged information and is intended for the named person or entity to which it is addressed. Any use, copying or distribution of this information by anyone other than the intended recipient(s) is prohibited by law. If you receive this in error, please immediately delete it from your system and notify the sender. The contents of this message contain personal opinions of the sender, which are not the official views of Piraeus Bank nor do they consist a provision of financial or advisory services unless expressly stated otherwise. This message is not a solicitation and/or an offer or acceptance of any proposal in relation to any contract or transaction unless expressly otherwise indicated in the message itself. The Internet is not a secure or error-free environment, and Piraeus Bank does not accept liability for any loss or damage arising from the use of this message or from delayed, intercepted, corrupted or virus-infected e-mail transmission. [[alternative HTML version deleted]] ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go. -- Enrico Schumann Lucerne, Switzerland http://nmof.net/ ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] The challenge to fit the greek gov curve with termstrc R Package!
[1:6] 0.625 2.5 2.125 2.75 2.625 ... ..$ PRICE : num [1:6] 100 132 106 109 106 ... ..$ ACCRUED : num [1:6] 0.275 0.999 0.935 1.211 0.827 ... ..$ CASHFLOWS :List of 3 .. ..$ ISIN: chr [1:136] US912828PW43 US912828PW43 US912828PW43 US912810PZ57 ... .. ..$ CF : num [1:136] 0.312 0.312 100.312 1.32 1.345 ... .. ..$ DATE:Class 'Date' num [1:136] 15338 15520 15704 15353 15535 ... ..$ TODAY :Class 'Date' num 15316 $ ote:List of 8 ..$ ISIN: chr [1:3] XS0173549659 XS0275776283 XS0346402463 ..$ MATURITYDATE:Class 'Date' num [1:3] 15921 16940 16477 ..$ ISSUEDATE :Class 'Date' num [1:3] 12268 13472 13920 ..$ COUPONRATE : num [1:3] 5 4.62 6 ..$ PRICE : num [1:3] 72.2 57.7 62 ..$ ACCRUED : num [1:3] 1.78 2.62 5 ..$ CASHFLOWS :List of 3 .. ..$ ISIN: chr [1:11] XS0173549659 XS0173549659 XS0275776283 XS0275776283 ... .. ..$ CF : num [1:11] 5 105 4.62 4.62 4.62 ... .. ..$ DATE:Class 'Date' num [1:11] 15556 15921 15479 15844 16209 ... ..$ TODAY :Class 'Date' num 15316 $ eurob :List of 8 ..$ ISIN: chr [1:3] XS0443680052 XS0291422979 XS0284635702 ..$ MATURITYDATE:Class 'Date' num [1:3] 15746 15426 16105 ..$ ISSUEDATE :Class 'Date' num [1:3] 14466 13599 13548 ..$ COUPONRATE : num [1:3] 4.38 1.68 1.78 ..$ PRICE : num [1:3] 56.4 80.7 55.3 ..$ ACCRUED : num [1:3] 3.656 0.356 0.178 ..$ CASHFLOWS :List of 3 .. ..$ ISIN: chr [1:13] XS0443680052 XS0443680052 XS0291422979 XS0291422979 ... .. ..$ CF : num [1:13] 4.375 104.375 0.426 100.404 0.45 ... .. ..$ DATE:Class 'Date' num [1:13] 15380 15746 15335 15426 15375 ... ..$ TODAY :Class 'Date' num 15316 - attr(*, class)= chr couponbonds Christos Arvanitis Assistant Manager Treasury Support Operations PIRAEUS BANK S.A. 5 Korai str., Athens, GR 10564 Tel: • +30 210 3288678 Mobile: Ë+30 6936114432 e-mail:arvaniti...@piraeusbank.grmailto:arvaniti...@piraeusbank.gr MESSAGE CONFIDENTIALITY SECURITY NOTICE = This message and/or its attachments may contain confidential and privileged information and is intended for the named person or entity to which it is addressed. Any use, copying or distribution of this information by anyone other than the intended recipient(s) is prohibited by law. If you receive this in error, please immediately delete it from your system and notify the sender. The contents of this message contain personal opinions of the sender, which are not the official views of Piraeus Bank nor do they consist a provision of financial or advisory services unless expressly stated otherwise. This message is not a solicitation and/or an offer or acceptance of any proposal in relation to any contract or transaction unless expressly otherwise indicated in the message itself. The Internet is not a secure or error-free environment, and Piraeus Bank does not accept liability for any loss or damage arising from the use of this message or from delayed, intercepted, corrupted or virus-infected e-mail transmission. [[alternative HTML version deleted]] ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go. -- Enrico Schumann Lucerne, Switzerland http://nmof.net/ ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] Constrained Regression with Intercept in pcls
Hi Philipp, you should be able to solve this with package quadprog/solve.QP. require(quadprog) p - 3## number of regressors T - 100 ## number of obs X - array(rnorm(T*p), dim = c(T,p)) X - cbind(1,X) ## add a constant y - rnorm(T) ## variant 1 -- linear regression coef(lm(y ~ -1 + X)) ## variant 2 -- quadprog (should be the same as variant 1) Dmat - crossprod(X) dvec - as.vector(t(as.matrix(y)) %*% X) Amat - as.matrix(rep(0, p+1)) solve.QP(Dmat = Dmat, dvec = dvec, Amat = Amat)$solution ## variant 3 -- quadprog, now with restrictions bvec - c(1, rep(0, p)) Amat - rbind(1,diag(p)) Amat - cbind(0, Amat) w - solve.QP(Dmat = Dmat, dvec = dvec, Amat = t(Amat), bvec=bvec, meq=1)$solution w ## result sum(w[-1]) ## check constraint see also here http://comisef.wikidot.com/tutorial:minimisevariance regards, Enrico Am 01.12.2011 17:18, schrieb Philipp: Dear all, I already asked about to run a constrained regression like y=b0 + b1X1+b2x2+b3x3, with constraints: b1+b2+b3=1 and b1,b2,b3=0. I thought it will run with style.QPfit with the performance Analytic packages. However the style.QPfit function does not estimate an intercept. As i really can not extend this function for my problem, I noticed pcls. Pcls is maybe an alternative for my problem. Can somebody help me to implement the constraints in pcls. Maybe it can be done by designing a designed matrix X with extra column for the inercept (b0) and to force C in a way. But how I do not know. Your help is greatly appreciated Philipp -- View this message in context: http://r.789695.n4.nabble.com/Constrained-Regression-with-Intercept-in-pcls-tp4129187p4129187.html Sent from the Rmetrics mailing list archive at Nabble.com. ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go. -- Enrico Schumann Lucerne, Switzerland http://nmof.net/ ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] options profit/loss graph (beginner question)
Hi Uwe Am 25.11.2011 09:05, schrieb Uwe Voelker: I'm completely new to R, but I hope it is good suited for this task. I It is (but you will have to learn a bit about using it). found fOptions from rmetrics package, but it only evaluates options. I'm missing the link to the graph/plot. I googled a lot and could not The link is the graphics and plotting capabilities that come with R. Start with ?plot. Can you please point me to an example? Here is a very simple example: ## a call under Black--Scholes--Merton callBSM - function(S,X,tau,r,q,vol) { if (tau 0) { d1 - (log(S/X) + (r - q + vol^2 / 2)*tau) / (vol*sqrt(tau)) d2 - d1 - vol*sqrt(tau) S * exp(-q * tau) * pnorm(d1) - X * exp(-r * tau) * pnorm(d2) } else { pmax(S-X,0) } } ## now S - 70:130 ## evaluate for these spot prices X - 100; tau - 1; r - 0.02; q - 0.01; vol - 0.2 plot(S, callBSM(S,X,tau,r,q,vol), col = grey(.7), type=l, ylab = payoff) ## some time later tau - 0.5 lines(S, callBSM(S,X,tau,r,q,vol), col = grey(.5), type=l) ## expiry tau - 0 lines(S, callBSM(S,X,tau,r,q,vol), col = grey(.3), type=l) ## ... or you may want to have a look at http://dirk.eddelbuettel.com/code/rquantlib.html Regards, Enrico Thanks a lot, with kind regards, Uwe ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go. -- Enrico Schumann Lucerne, Switzerland http://nmof.net/ ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] Currency data from GOOGLE/Yahoo finance.
Try this: require(quantmod) INRUSD - getSymbols(INR=X,src=yahoo, auto.assign = FALSE) Regards, Enrico Am 22.09.2011 10:42, schrieb Wolfgang Wu: Ashim, Unfortunately I couldn't get any data from Yahoo or Google either. I thought you had to do something like getSymbols(CURRENCY:INRUSD,src=google), but that doesn't work anymore either. Best solution I can give you is to use the Oanda source for currency data. They do limit you to 500 days of data but you can easily write a loop changing the from and to times and rbind the xts object together afterwards. INRUSD- getSymbols('INR/USD', src='oanda', auto.assign = FALSE) Regards, Wolfgang Wu - Ursprüngliche Message - Von: Ashim Kapoorashimkap...@gmail.com An: r-sig-finance@r-project.org Cc: Gesendet: 8:32 Donnerstag, 22.September 2011 Betreff: [R-SIG-Finance] Currency data from GOOGLE/Yahoo finance. Dear R-helpers, I am trying to use getSymbols to download INR/USD data from yahoo or google finance. In each case I 1st list the website and then the R error which I get on trying to download the symbol. Could someone please tell me where I am wrong? http://www.google.com/finance?q=inrusd getSymbols(inrusd,src=google) Error in download.file(paste(google.URL, q=, Symbols.name, startdate=, : cannot open URL ' http://finance.google.com/finance/historical?q=inrusdstartdate=Jan+01,+2007enddate=Sep+22,+2011output=csv ' In addition: Warning message: In download.file(paste(google.URL, q=, Symbols.name, startdate=, : cannot open: HTTP status was '400 Bad Request' http://in.finance.yahoo.com/q?s=USDINR%3DXql=1 getSymbols(USDINR=X,src=yahoo) Error in download.file(paste(yahoo.URL, s=, Symbols.name, a=, from.m, : cannot open URL ' http://chart.yahoo.com/table.csv?s=USDINR=Xa=0b=01c=2007d=8e=22f=2011g=dq=qy=0z=USDINR=Xx=.csv ' In addition: Warning message: In download.file(paste(yahoo.URL, s=, Symbols.name, a=, from.m, : unable to resolve 'chart.yahoo.com' Best Regards, Ashim [[alternative HTML version deleted]] ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go. ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go. -- Enrico Schumann Lucerne, Switzerland http://nmof.net/ ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] Currency data from GOOGLE/Yahoo finance.
hm, the last words were actually check on finance.yahoo.com ;) but they have been cut away. Am 22.09.2011 11:50, schrieb Enrico Schumann: It seems a convention that if the rate is USDCCY, you get it from Yahoo with CCY=X. At least I have had this with JPY=X, GBP=X. But I am not sure how consistent that is. The simple trick is to directly check on Am 22.09.2011 11:40, schrieb Ashim Kapoor: Dear Enrico, How / where did you discover that it should be INR=X instead of the symbol USDINR=X Many thanks, Ashim On Thu, Sep 22, 2011 at 2:46 PM, Enrico Schumannenricoschum...@yahoo.dewrote: Try this: require(quantmod) INRUSD- getSymbols(INR=X,src=yahoo**, auto.assign = FALSE) Regards, Enrico Am 22.09.2011 10:42, schrieb Wolfgang Wu: Ashim, Unfortunately I couldn't get any data from Yahoo or Google either. I thought you had to do something like getSymbols(CURRENCY:INRUSD,**src=google), but that doesn't work anymore either. Best solution I can give you is to use the Oanda source for currency data. They do limit you to 500 days of data but you can easily write a loop changing the from and to times and rbind the xts object together afterwards. INRUSD- getSymbols('INR/USD', src='oanda', auto.assign = FALSE) Regards, Wolfgang Wu - Ursprüngliche Message - Von: Ashim Kapoorashimkap...@gmail.com An: r-sig-finance@r-project.org Cc: Gesendet: 8:32 Donnerstag, 22.September 2011 Betreff: [R-SIG-Finance] Currency data from GOOGLE/Yahoo finance. Dear R-helpers, I am trying to use getSymbols to download INR/USD data from yahoo or google finance. In each case I 1st list the website and then the R error which I get on trying to download the symbol. Could someone please tell me where I am wrong? http://www.google.com/finance?**q=inrusdhttp://www.google.com/finance?q=inrusd getSymbols(inrusd,src=**google) Error in download.file(paste(google.**URL, q=, Symbols.name, startdate=, : cannot open URL ' http://finance.google.com/**finance/historical?q=inrusd** startdate=Jan+01,+2007**enddate=Sep+22,+2011output=**csvhttp://finance.google.com/finance/historical?q=inrusdstartdate=Jan+01,+2007enddate=Sep+22,+2011output=csv ' In addition: Warning message: In download.file(paste(google.**URL, q=, Symbols.name, startdate=, : cannot open: HTTP status was '400 Bad Request' http://in.finance.yahoo.com/q?**s=USDINR%3DXql=1http://in.finance.yahoo.com/q?s=USDINR%3DXql=1 getSymbols(USDINR=X,src=**yahoo) Error in download.file(paste(yahoo.URL, s=, Symbols.name, a=, from.m, : cannot open URL ' http://chart.yahoo.com/table.**csv?s=USDINR=Xa=0b=01c=** 2007d=8e=22f=2011g=dq=q**y=0z=USDINR=Xx=.csvhttp://chart.yahoo.com/table.csv?s=USDINR=Xa=0b=01c=2007d=8e=22f=2011g=dq=qy=0z=USDINR=Xx=.csv ' In addition: Warning message: In download.file(paste(yahoo.URL, s=, Symbols.name, a=, from.m, : unable to resolve 'chart.yahoo.com' Best Regards, Ashim [[alternative HTML version deleted]] __**_ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r-sig-financehttps://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go. __**_ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r-sig-financehttps://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go. -- Enrico Schumann Lucerne, Switzerland http://nmof.net/ -- Enrico Schumann Lucerne, Switzerland http://nmof.net/ ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Re: [R-SIG-Finance] Sharpe's algorithm for portfolio improvement
[comments inline] -Ursprüngliche Nachricht- Von: Brian G. Peterson [mailto:br...@braverock.com] Gesendet: Dienstag, 2. August 2011 14:37 An: Enrico Schumann Cc: 'John P. Burkett'; R-SIG-Finance@r-project.org Betreff: Re: [R-SIG-Finance] Sharpe's algorithm for portfolio improvement n Tue, 2011-08-02 at 07:31 +0200, Enrico Schumann wrote: One potential problem with DE is how the constraints are implemented, more specifically the lower/upper limits. When DE creates new solutions, it does not observe any constraints except for such that one implicitly adds (eg, like you did with the budget constraint). This isn't *quite* correct. DE will observe your lower and upper bounds on each objective parameter (asset weight), but will not observe some additional overall constraint such as a full investment constraint. Sorry if there was a misunderstanding: when I referred to 'DE', I meant Differential Evolution in general, not 'DEoptim'. I don't know how the constraints are incorporated in 'DEoptim'. Thus, one typically repairs the constraints after new solutions are created, This mirrors Pat Burns' point about letting the optimizer do its thing, and then 'adjusting' the parameter set inside your objective function to apply the constraint. For many Just for the record: actually we have even two possibilities: really repair, or just map the solutions into feasible ones. In the latter case, we go on evolving the solutions that violate the constraints. portfolios, this works quite well, but for those where it doesn't work, a penalty function needs to be applied. or penalises solutions that violate constraints. If you penalise too strongly, the algorithm will in effect throw away many candidate solutions, and so the algorithm may indeed have a hard time to find a solution, since in portfolio optimisation problems the solution is often on the boundary. I would put this difficulty a little differently. If the constraint we are concerned with is the full investment constraint, then the issue is one of how large the feasible adjusted constrained space (portfolios with weight bounds min_w and max_w where sum(w)==1) in comparison to the total space the optimizer will search (all portfolios comprised of assets in any combination of weights min_w to max_w). If the feasible space is large in comparison to the total space, a global optimizer will locate the solution. If the if it works properly, yes :) feasible space is not large in comparison to the total space, and a large penalty function is used to tell the optimizer that it is not close, then it could take a long time to find a solution. There are several approaches to addressing this. One is to graduate your penalty response, for example penalizing with some multiplier (which should be scaled somewhat larger than a 'good' value of the objective function, so for global minima at -1, perhaps a multiplier of 10 or 100 will serve) the absolute value of the amount the sum of the weights exceeds 1. You can make this work even better by applying a fraction of the penalty for any sum of weights that is less than one, because this solution is likely closer to the feasible space. Yes, the important point, I think, is that in such cases the feedback from the penalty need be constructive, ie, how much do I violate a constraint? (if I want to push some object from the table to the floor, then feedback like 'the object is still on the table' is less useful than 'the object is now 3cm off the edge of the table') But you can also directly use the constraint in the creation of the new solutions, which is what Sharpe suggested (and what works quite well, and not just for smooth functions/constraints): if you only add zero-sum changes to a feasible portfolio, it will remain feasible with respect to the budget constraint. If you want min/max-holding sizes, choose asset weights such that the min/max-constraints remain unviolated. This is straightforward in methods like Simulated Annealing/Threshold Accepting, but somewhat more difficult in 'DE'. (Which does not mean that 'DE' is not as good as the other methods, just that the efficient approaches possibly differ, depending on the method.) I find that it is best to pre-seed the optimizer with a population of candidate portfolios that are all inside the feasible space, so that even random perturbations are still closer to the feasible space. In PortfolioAnalytics, we pre-seed be default, and use a full-investment constraint adjustment by default (with a graduated penalty function as another supported alternative), and of course all these options are changeable by the user. I haven't been able to locate the original Sharpe paper, but I suspect he utilized a smooth objective function given the date of the paper alone. In that case, as I said previously, you don't need
Re: [R-SIG-Finance] problem with getting Historical data for futures using I Brokers package
try adding include_expired = '1' to the contract definition; see ?twsContract regards, enrico -Ursprüngliche Nachricht- Von: r-sig-finance-boun...@r-project.org [mailto:r-sig-finance-boun...@r-project.org] Im Auftrag von Gautam Garg Gesendet: Montag, 13. Juni 2011 11:53 An: r-sig-finance@r-project.org Betreff: [R-SIG-Finance] problem with getting Historical data for futures using I Brokers package Hi , I am trying to get historical data for futures using I brokers package. I am able to do so with the expiry dates which are yet to come but not for the dates that have passed. Below is my code : t-twsFUT(symbol=III,exch=NSE,expiry=201106,currency=INR) abb - reqHistoricalData(tws,t ,file=III.csv ,bar=1 day, dur=5 D) waiting for TWS reply on III done. but if the write the code below: t-twsFUT(symbol=III,exch=NSE,expiry=201105,currency=INR) abb - reqHistoricalData(tws,t ,file=LT.csv ,bar=1 day, dur=5 D) TWS Message: 2 -1 2107 HMDS data farm connection is inactive but should be available upon demand.hkhmds2 waiting for TWS reply on III failed. Warning message: In errorHandler(con, verbose, OK = c(165, 300, 366, 2104, 2106, : No security definition has been found for the request What is that I am missing? Thanks in advance Gautam Garg -- View this message in context: http://r.789695.n4.nabble.com/problem-with-getting-Historical- data-for-futures-using-I-Brokers-package-tp3593480p3593480.html Sent from the Rmetrics mailing list archive at Nabble.com. ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go. ___ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.