Scanning across a universe of stocks for triggers in R -
first of all, apologize if basic question, searched here , unable find answer i'm trying. i'm not new programming, new r (and programming bit rusty, i've changed career focus bit).
what i'm trying simple (famous last words):
- every morning
- scan across entire universe of stocks (or s&p 500)
- show me stock within x% of given indicator (eg, approaching lower bollinger band)
this how i'm trying proof of concept, , use launching pad developing own indicators. appreciate , helpful comments, links, etc. in advance all!
library(quantmod) # vector of stock tickers @ s <- c("aa", "axp", "ba", "bac", "cat", "csco", "cvx", "dd", "dis", "ge", "hd", "hpq", "ibm", "intc", "jnj", "jpm", "ko", "mcd", "mmm", "mrk", "msft", "pfe", "pg", "t", "trv", "unh", "utx", "vz", "wmt", "xom") e <- new.env() # environment hold our data getsymbols(s, from=sys.date()-50, src="yahoo", env=e) # download stock prices # create parameter pct <- 0.01 # close prices lower 1% above lower bband. # eapply loops on every object in environment , applies function it. # our function calculates value of lower bband increased "pct" # returns true or false depending on whether stock price below that. # eapply returns list, can `unlist` named vector near.low.band <- unlist(eapply(e, function(x) { bband.dn <- as.numeric(last(bbands(hlc(x))$dn)) as.numeric(last(cl(x))) < bband.dn * (1 + pct) })) # names value true names(near.low.band)[near.low.band] # [1] "xom" "jnj" "jpm" "vz" "utx" "intc" "mmm" "mcd" "csco" "pfe" #[11] "ge" "t" "bac" "cvx" "mrk" "trv" "ko" "pg" "wmt" "dis" #[21] "unh" "hd" "ba" "ibm" # , ones not below our threshold? names(near.low.band)[!near.low.band] #[1] "dd" "hpq" "axp" "aa" "cat" "msft"
Comments
Post a Comment