[Blotter-commits] r824 - in pkg/FinancialInstrument: . R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Fri Oct 21 02:04:46 CEST 2011
Author: gsee
Date: 2011-10-21 02:04:46 +0200 (Fri, 21 Oct 2011)
New Revision: 824
Added:
pkg/FinancialInstrument/R/saveSymbols.R
pkg/FinancialInstrument/man/saveSymbols.days.Rd
Modified:
pkg/FinancialInstrument/DESCRIPTION
pkg/FinancialInstrument/NAMESPACE
Log:
functions to saveSymbols the way getSymbols.FI expects
Modified: pkg/FinancialInstrument/DESCRIPTION
===================================================================
--- pkg/FinancialInstrument/DESCRIPTION 2011-10-20 23:58:56 UTC (rev 823)
+++ pkg/FinancialInstrument/DESCRIPTION 2011-10-21 00:04:46 UTC (rev 824)
@@ -42,3 +42,4 @@
'ls_by_currency.R'
'saveInstruments.R'
'ls_expiries.R'
+ 'saveSymbols.R'
Modified: pkg/FinancialInstrument/NAMESPACE
===================================================================
--- pkg/FinancialInstrument/NAMESPACE 2011-10-20 23:58:56 UTC (rev 823)
+++ pkg/FinancialInstrument/NAMESPACE 2011-10-21 00:04:46 UTC (rev 824)
@@ -91,6 +91,8 @@
export(rm_stocks)
export(rm_synthetics)
export(saveInstruments)
+export(saveSymbols.common)
+export(saveSymbols.days)
export(setSymbolLookup.FI)
export(sort_ids)
export(spread)
Added: pkg/FinancialInstrument/R/saveSymbols.R
===================================================================
--- pkg/FinancialInstrument/R/saveSymbols.R (rev 0)
+++ pkg/FinancialInstrument/R/saveSymbols.R 2011-10-21 00:04:46 UTC (rev 824)
@@ -0,0 +1,64 @@
+#' save data to disk
+#'
+#' save data to disk the way that \code{getSymbols.FI}
+#' expects it to be saved.
+#'
+#' @param Symbols vector of character names of objects to be saved
+#' @param base_dir character. directory in which to store data.
+#' @param env environment that holds the data to be saved (.GlobalEnv by default)
+#' @return called for side-effect.
+#' @seealso \code{\link{getSymbols.FI}}
+#' @examples
+#' \dontrun{
+#' getSymbols("SPY", src='yahoo')
+#' saveSymbols.common("SPY", base_dir="~/")
+#' rm("SPY")
+#' getSymbols("SPY", src='FI', dir="~/", split_method='common')
+#' unlink("~/SPY", recursive=TRUE)
+#' }
+#' @export
+#' @rdname saveSymbols.days
+saveSymbols.days <-
+function(Symbols, base_dir="", env=.GlobalEnv) {
+ if (base_dir != "" &&
+ !is.null(base_dir) &&
+ substr(base_dir,nchar(base_dir),nchar(base_dir)) != "/")
+ base_dir <- paste(base_dir,"/",sep="")
+ tmpenv <- new.env()
+ for (symbol in Symbols) {
+ tmp <- try(get(symbol,pos=env))
+ if (!is.null(tmp) && !inherits(tmp,'try-error')) {
+ D <- split(tmp,'days')
+ if (!file.exists(paste(base_dir,symbol,sep=""))) dir.create(paste(base_dir,symbol,sep=""))
+ fnames <- paste(base_dir,symbol,"/",
+ unlist(lapply(D, FUN=function(x) format(index(first(x)),"%Y.%m.%d"))),
+ ".", symbol, ".rda", sep="")
+ for (i in 1:length(fnames)) {
+ assign(symbol,D[[i]],envir=tmpenv)
+ save(list=symbol,file=fnames[i],envir=tmpenv)
+ }
+ }
+ }
+}
+
+#' @export
+#' @rdname saveSymbols.days
+saveSymbols.common <- function (Symbols, base_dir = "", env = .GlobalEnv)
+{
+ if (base_dir != "" && !is.null(base_dir) && substr(base_dir,
+ nchar(base_dir), nchar(base_dir)) != "/")
+ base_dir <- paste(base_dir, "/", sep = "")
+ tmpenv <- new.env()
+ for (symbol in Symbols) {
+ tmp <- try(get(symbol, pos = env))
+ if (!is.null(tmp) && !inherits(tmp, "try-error")) {
+ if (!file.exists(paste(base_dir, symbol, sep = "")))
+ dir.create(paste(base_dir, symbol, sep = ""))
+ fnames <- paste(base_dir, symbol, "/", symbol, ".rda", sep = "")
+ assign(symbol, tmp, envir = tmpenv)
+ save(list = symbol, file = fnames, envir = tmpenv)
+ }
+ }
+}
+
+
Added: pkg/FinancialInstrument/man/saveSymbols.days.Rd
===================================================================
--- pkg/FinancialInstrument/man/saveSymbols.days.Rd (rev 0)
+++ pkg/FinancialInstrument/man/saveSymbols.days.Rd 2011-10-21 00:04:46 UTC (rev 824)
@@ -0,0 +1,41 @@
+\name{saveSymbols.days}
+\alias{saveSymbols.common}
+\alias{saveSymbols.days}
+\title{save data to disk}
+\usage{
+ saveSymbols.days(Symbols, base_dir = "", env =
+ .GlobalEnv)
+
+ saveSymbols.common(Symbols, base_dir = "", env =
+ .GlobalEnv)
+}
+\arguments{
+ \item{Symbols}{vector of character names of objects to be
+ saved}
+
+ \item{base_dir}{character. directory in which to store
+ data.}
+
+ \item{env}{environment that holds the data to be saved
+ (.GlobalEnv by default)}
+}
+\value{
+ called for side-effect.
+}
+\description{
+ save data to disk the way that \code{getSymbols.FI}
+ expects it to be saved.
+}
+\examples{
+\dontrun{
+getSymbols("SPY", src='yahoo')
+saveSymbols.common("SPY", base_dir="~/")
+rm("SPY")
+getSymbols("SPY", src='FI', dir="~/", split_method='common')
+unlink("~/SPY", recursive=TRUE)
+}
+}
+\seealso{
+ \code{\link{getSymbols.FI}}
+}
+
More information about the Blotter-commits
mailing list