Scrape data from StockTwits and see if any information can be used for predicting stock price trend.
sessionInfo()
## R version 3.1.2 (2014-10-31)
## Platform: x86_64-apple-darwin13.4.0 (64-bit)
##
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] quantmod_0.4-4 TTR_0.22-0 xts_0.9-7 zoo_1.7-12
## [5] RJSONIO_1.3-0 RCurl_1.95-4.5 bitops_1.0-6
##
## loaded via a namespace (and not attached):
## [1] digest_0.6.8 evaluate_0.5.5 formatR_1.1 grid_3.1.2
## [5] htmltools_0.2.6 knitr_1.9 lattice_0.20-30 rmarkdown_0.5.1
## [9] stringr_0.6.2 tools_3.1.2
#Have stored client_id, client_secret and access token in .Rprofile
We use stock_dir() to plot all the changes in price within a day at an interval of 5 minutes and meanwhile to select all extreme values (> 2sd from 0) from all the changes and calculate the sum.
stock_dir('WUBA',leg_pos = 'topright')
## [1] -0.02195645
‘WUBA’ had a 6% drop today (04-13-2015). We see that the empirical density curve is shifted left from the expected (m=0) normal density. In other words, the sum of extreme values carries information about the direction of the trend of a given stock. This leads to the following analyses.
extreme_sums <- sapply(tickers,stock_dir,plot=F)
today_ud <- function(ticker){
dat<-last(getSymbols(ticker,src = 'google',auto.assign = F))
as.numeric(Cl(dat)-Op(dat))
}
stock_updown <- sapply(tickers,today_ud)
I selected several stocks as a control group for comparison.
control_ticker = c('MSFT','ORCL','GOOG','PEP','COKE','T','AXP','F','WMT','GE','NKE','KATE','CAJ','KR','TM')
extreme_sums_ctrl <- sapply(control_ticker,stock_dir,plot=F)
stock_updown_ctrl <- sapply(control_ticker,today_ud)
model = lm(stock_updown~extreme_sums)
model_ctrl = lm(stock_updown_ctrl~extreme_sums_ctrl)
plot(extreme_sums,stock_updown,ylim=c(-4,4),xlim=c(-0.07,0.07),col=rgb(0,.8,.8,.7),pch=16,xlab='Sum of extremes',
ylab="Today's price delta(Open-Close)",main='Trending vs control stocks')
abline(model,col=rgb(0,.8,.8,1))
points(extreme_sums_ctrl,stock_updown_ctrl,ylim=c(-4,4),xlim=c(-0.07,0.07),col=rgb(1,0,0,.7),pch=4)
abline(model_ctrl,col='red')
legend('topleft',legend = c('Trending','Control'),col=c(rgb(0,.8,.8,1),rgb(1,0,0,1)),pch=c(16,4),cex=.8)
In the stock market, we want to follow big trends to make profit at a lower risk. The trending method of StokcTwits provides us with stocks that might be in big trends. Therefore, we may select a stock as a potential one to invest if (1) it’s a trending stock, and (2) it has a sum of extreme values greater than 0.