-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexamples_highstock.R
75 lines (58 loc) · 2.23 KB
/
examples_highstock.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#rm(list=ls())
require(rCharts)
require(quantmod)
require(plyr)
# Specify period of time we are interested in
startDate <- as.Date("2010-01-01")
endDate <- as.Date("2013-12-31")
# Define the tickers we are interested in
tickers <- c('AAPL', 'MSFT', 'MMM', 'V', 'HD', 'GE')
getSymbols(tickers, src = "google", from = startDate, to = endDate)
getQData <- function(fun)
{
myfunc <- function(name)
{
# Remoe stock name from columns
dt <- get(name)
colnames(dt) <- gsub(paste(name, ".", sep=""), "", colnames(dt), fixed=T)
# Call quantmod function
tmp <- fun(dt)
stock <- data.frame(t=index(tmp), coredata(tmp))
stock$date <- as.numeric(as.POSIXct(stock$t, origin="1970-01-01")) * 1000
stock$stock <- name
return( stock )
}
return( myfunc )
}
stocks <- do.call(rbind, lapply(tickers, getQData(OHLCV)))
##
## Standard plots
# One stock
aapl <- subset(stocks, stock=='AAPL')
sPlot(Close ~ date, data = aapl, title='AAPL') # Default is line
sPlot(Close ~ date, data = aapl, title='AAPL', type='column') # Default is line
sPlot(Close ~ date, data = aapl, title='AAPL', type='area') # Default is line
sPlot(Close ~ date, data = aapl, title='AAPL', type='area', navigator=F) # Default is line
# Try switching off some highstock features
s <- sPlot(Close ~ date, data = aapl, title='AAPL', type='area'); s
s$rangeSelector(enabled = FALSE, replace = T); s
s$exporting(enabled = FALSE, replace = T); s
s$navigator(enabled = FALSE); s
s$scrollbar(enabled = FALSE); s
s$params
# Multiple stocks
# The legend is clickable
s <- sPlot(Close ~ date, data = stocks, title = 'A few stocks', group = 'stock') # Default is line
s
s$legend(enabled = T, align = 'left', verticalAlign = 'middle', layout = 'vertical', replace = T)
s
# Try modifying the geometry of the container
options(RCHART_WIDTH = 1200, RCHART_HEIGHT = 900)
s <- sPlot(Close ~ date, data = stocks, title='A few stocks', group='stock') # Default is line
s
s$exporting(sourceWidth = 800, sourceHeight = 600, scale = 2); s
##
## OHLC / Candlestick plots
# One stock only
ohlcvPlot(data = aapl, name='AAPL' )
ohlcvPlot(data = aapl, name='AAPL', type='ohlc')