0

I am trying to apply the user defined function augenSpike over a set of tickers stored in an environment but somehow it is not working. If someone can help this entry level R user, I would much appreciated...

library(quantmod)

slideapply <- function(x, n, FUN=sd) {
  v <- c(rep(NA, length(x)))
  for (i in n:length(x) ) {
    v[i] <- FUN(x[(i-n+1):i])
  } 
  return(v)   
}

augenSpike <- function(x, n=20) {
  prchg <- c(NA, diff(x))
  lgchg <- c(NA, diff(log(x)))
  stdevlgchg <- slideapply(lgchg, n, sd)
  stdpr <- x * stdevlgchg
  #shuffle things up one
  stdpr <- c(NA, stdpr[-length(stdpr)])
  spike <- prchg / stdpr
  return(spike)
}
myenv <- new.env()
# environment used to store tickers
tickers <- c("PBR", "AAPL", "MSFT", "GOOG")
getSymbols(tickers, env= myenv)
sp <-tickers['2013/2014']
asp <- augenSpike(as.vector(Cl(sp)))
sp$spike <- asp


## Create a vector of colors selected based on whether x is <0 or >0
## (FALSE + 1 -> 1 -> "blue";    TRUE + 1 -> 2 -> "red")
cols <- c("blue", "red" ) [(sp$spike > 0) +  1]

barplot(sp['2013-2014']$spike, col= cols, main="Augen Price Spike", xlab="Time Daily",ylab="Price Spike in Std Dev")
abline(h = 2, col = "red")
abline(h = 0, col = "black")
abline(h = -2, col = "red")
2
  • Can you provide a reduced example of this problem? Commented Jan 25, 2014 at 14:32
  • I want to repeat the slideapply and AugenSpike function over the tickers I have stored in myenv. The issue is that I do not know how to properly use eapply to the function and over my tickers. Thanks Commented Jan 25, 2014 at 18:52

1 Answer 1

1

In the provided code snippet, sp is NA, which causes Cl(sp) to fail. The reason for this is that tickers is still a vector of strings and not the xts object representing the stock symbol. The xts objects are inaccessible because of the use of the custom environment. In the absence of that environment, new variables named after the stock symbols are added to scope. You can make a secondary vector of the xts objects, and then subscript that with '2013/2014'. The following script should do what you want:

library(quantmod)

slideapply <- function(x, n, FUN=sd) {
  v <- c(rep(NA, length(x)))
  for (i in n:length(x) ) {
    v[i] <- FUN(x[(i-n+1):i])
  } 
  return(v)   
}

augenSpike <- function(x, n=20) {
  prchg <- c(NA, diff(x))
  lgchg <- c(NA, diff(log(x)))
  stdevlgchg <- slideapply(lgchg, n, sd)
  stdpr <- x * stdevlgchg
  #shuffle things up one
  stdpr <- c(NA, stdpr[-length(stdpr)])
  spike <- prchg / stdpr
  return(spike)
}

tickers <- c("PBR", "AAPL", "MSFT", "GOOG")
getSymbols(tickers)
ticker_symbols <- c(PBR, AAPL, MSFT, GOOG)

sp <-ticker_symbols['2013/2014']
asp <- augenSpike(as.vector(Cl(sp)))
sp$spike <- asp


## Create a vector of colors selected based on whether x is <0 or >0
## (FALSE + 1 -> 1 -> "blue";    TRUE + 1 -> 2 -> "red")
cols <- c("blue", "red" ) [(sp$spike > 0) +  1]

barplot(sp['2013-2014']$spike, col= cols, main="Augen Price Spike", xlab="Time Daily",ylab="Price Spike in Std Dev")
abline(h = 2, col = "red")
abline(h = 0, col = "black")
abline(h = -2, col = "red")

Which produces this lovely graphic:Output from R script

Sign up to request clarification or add additional context in comments.

4 Comments

it cannot be NA within the code above, the code runs perfect except for the fact that it is not reading all the tickers within the environment. thanks anyway
I've updated this post to include a modified version of the script that should do what you want.
@weitzner: This is a great answer, and there was definitely a lot of effort that went into it! Great contribution. I gave a +1. I can't believe it had a net score of zero! And you updated the answer to fulfill the OP's follow-up question, which was very kind of you. I looked at your profile and see that you started the Protein Modeling stack exchange proposal. I started one on Modeling in chemistry and physics, and very much welcome bio-chemical modeling like protein modeling too. We would be delighted to invite you to participate in the Private Beta! I'll put a link in the next comment:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.