-
Notifications
You must be signed in to change notification settings - Fork 2
/
bbandScanner_Function.R
44 lines (33 loc) · 1.12 KB
/
bbandScanner_Function.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
library('quantmod')
setwd("~/GitHub/quantstuff")
######################################################
bbandScanner <- function(a){
# if(exists("signalList")==F) {
signalList <- as.data.frame(matrix(NA,nrow=0,ncol=3))
names(signalList) <- c("stock","pctB","signal")
row<-0
# }
for(i in 1:length(a)){
mySymbol <- toupper(a[i])
print(mySymbol)
# If the quote object doesn't exist. Get it from Yahoo.
# If it already exists, this code will be skipped.
if(exists(mySymbol)==F) {
getSymbols(mySymbol,src="yahoo")
}
myStock <- eval(parse(text=mySymbol))
bands <- BBands(Cl(myStock))$pctB
bands <- tail(bands, n=1)
bands$signal <- NA
bands$signal <- ifelse(bands$pctB > 1, "Sell",
ifelse(bands$pctB < 0,"Buy",
"Wait"))
row <- nrow(signalList) + 1
signalList[row,1] <- mySymbol
signalList[row,2] <- bands$pctB
signalList[row,3] <- bands$signal
}
}
########################################################
do.call(bbandScanner,as.list((c("AAPL"))))
###################################################