Skip to content

[R-Forge #4924] getDividends and getSplits to handle env argument properly #33

Description

@joshuaulrich

Submitted by: Stewart Wright
Assigned to: Nobody
R-Forge link

getDividends and getSplits should be able to update Symbol not in the parent environment, but they actually don't.

The 'svn diff' below fixes this by creating a temporary variable which gets updated and then reinserted into the appropriate environment.

I have to use the tmp.symbol variable as xtsAttributes only updates variables in the parent.frame.

Index: getDividends.R
===================================================================
--- getDividends.R  (revision 609)
+++ getDividends.R  (working copy)
@@ -2,8 +2,14 @@
 function(Symbol,from='1970-01-01',to=Sys.Date(),env=parent.frame(),src='yahoo',
          auto.assign=FALSE,auto.update=FALSE,verbose=FALSE,...) {

-  if(missing(env))
+  tmp.symbol <- Symbol
+  if(missing(env)) {
     env <- parent.frame(1)
+  } else {
+    if(exists(Symbol, envir = env)) {
+      tmp.symbol <- get(Symbol, envir = env)
+    }
+  }
   if(is.null(env))
     auto.assign <- FALSE
   Symbol.name <- ifelse(!is.character(Symbol),
@@ -27,10 +33,11 @@
   fr <- read.csv(tmp)
   unlink(tmp)
   fr <- xts(fr[,2],as.Date(fr[,1]))
-  if(is.xts(Symbol)) {
+
+  if(is.xts(tmp.symbol)) {
     if(auto.update) {
-      xtsAttributes(Symbol) <- list(dividends=fr)
-      assign(Symbol.name,Symbol,envir=env)
+      xtsAttributes(tmp.symbol) <- list(dividends=fr)
+      assign(Symbol.name,tmp.symbol,envir=env)
     }
   } else if(auto.assign) {
       assign(paste(Symbol.name,'div',sep='.'),fr,envir=env)
Index: getSplits.R
===================================================================
--- getSplits.R (revision 609)
+++ getSplits.R (working copy)
@@ -4,8 +4,13 @@

   # Function written by Joshua Ulrich, using
   # getSymbols.yahoo as a guide.
-  if(missing(env))
+  if(missing(env)) {
     env <- parent.frame(1)
+  } else {
+    if(exists(Symbol, envir = env)) {
+      tmp.symbol <- get(Symbol, envir = env)
+    }
+  }
   if(is.null(env))
     auto.assign <- FALSE
   Symbol.name <- ifelse(!is.character(Symbol),
@@ -39,10 +44,10 @@
     colnames(fr) <- paste(Symbol.name,'spl',sep='.')
   }

-  if(is.xts(Symbol)) {
+  if(is.xts(tmp.symbol)) {
     if(auto.update) {
-      xtsAttributes(Symbol) <- list(splits=fr)
-      assign(Symbol.name,Symbol,envir=env)
+      xtsAttributes(tmp.symbol) <- list(splits=fr)
+      assign(Symbol.name,tmp.symbol,envir=env)
     }
   } else if(auto.assign) {
       assign(paste(Symbol.name,'spl',sep='.'),fr,envir=env)

Followups:

Date: 2013-09-26 05:26
Sender: Stewart Wright
Updated the patch.

Previous version did not handle case of just calling functions with a Symbol (i.e. 'F'): getDividends('F'). So now behavior is consistent.


Note: The code above reflects Stewart's updated patch.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions