/
readHotCopper.R
136 lines (119 loc) · 5.53 KB
/
readHotCopper.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
readHotCopper <- function(Ticker = "ZIP"){
##Gather company names, ticker, and market cap for a metal
library(rvest)
library(plyr)
library(XML)
library(lubridate)
##Write URL based on Ticker
url <- paste(sep = "","http://hotcopper.com.au/asx/",Ticker)
##Read in HTML and get Nodes for post dates
html <- rvest::html(url)
dateNodes <- html_nodes(x = html, css = ".medium-only")
authorNodes <- html_nodes (x = html, css = ".author a" )
dateVector <- html_text(dateNodes, trim = TRUE)
authorVector <- html_text(authorNodes,trim=TRUE)
postDates <- data.frame(datetime = dateVector,author = authorVector)
i=2
while(i<= 5){
url <- paste(sep = "","http://hotcopper.com.au/asx/",Ticker,"/page-",i)
html <- rvest::html(url)
dateNodes <- html_nodes(x = html, css = ".medium-only")
authorNodes <- html_nodes (x = html, css = ".author a" )
dateVector <- html_text(dateNodes, trim = TRUE)
authorVector <- html_text(authorNodes,trim=TRUE)
dateDataTable <- data.frame(datetime = dateVector,author = authorVector)
postDates <- rbind(postDates,dateDataTable)
i = i+1
}
postDates$Stripped <- strptime(x = postDates$datetime,format = "%d/%m/%y")
postDates$Stripped[is.na(postDates$Stripped)] <- strptime(Sys.Date(),format = "%Y-%m-%d")
postDates$Date <- as.Date(postDates$Stripped)
unique <- postDates[,c(4,2)]
unique <- as.data.frame(table(unique))
unique$Freq <- ifelse(unique$Freq > 0,1,0)
final <- count(unique[,c(1,3)])
final <- final[final$Freq==1,c(1,3)]
colnames(final) <- c("date","unique")
countDay <- postDates[,4]
frequency <- count(countDay)
colnames(frequency) <- c("date","count")
frequency$date <- as.character(frequency$date)
final$date <- as.character(final$date)
output <- merge(x = frequency,y = final,by = "date")
output$date <- as.Date(output$date)
output
}
plotHotCopper <- function(Ticker = "ZIP"){
library(rCharts)
library(quantmod)
library(plyr)
library(lubridate)
data <- readHotCopper(Ticker)
range <- range(data$date)
dataSP <- getSymbols.yahoo(paste(Ticker,".AX",sep = ""),
env = .GlobalEnv,return.class = "data.frame",
auto.assign=FALSE,from = range[1],to = range[2])
dataSP <- data.frame(date = as.Date(rownames(dataSP)),close = dataSP[,4],volume = dataSP[,5])
data <- merge(x = data,y = dataSP,by = "date",all = TRUE)
i = 1
while(i<= nrow(data)){
if(is.na(data$close[i])) {
data$close[i]=data$close[i-1]
}
if(is.na(data$count[i])){
data$count[i] = 0
}
if(is.na(data$volume[i])){
data$volume[i] = 0
}
if(is.na(data$unique[i])){
data$unique[i] = 0
}
i = i+1
}
data <- transform(data, date = as.numeric(date) * 86400000)
# a <- hPlot(data=data,freq~x,type = "column")
a <- rCharts::Highcharts$new()
a$yAxis(list(list(title = list(text = "Post Count"),min = 0,max = max(data$count)*1.05),
list(title = list(text = "Share Price"),min = 0,max = max (data$close)*1.05,opposite = TRUE),
list(title = list(text = "Volume"),min = 0,max = max (data$volume)*1.05,opposite = TRUE),
list(title = list(text = "Unique Posters"),min = 0,max = max (data$unique)*1.05,opposite = FALSE)
)
)
a$series(type = "column",name = "Volume",
data = toJSONArray2(data[,c(1,5)],json = F,names = F),
yAxis =2
)
a$series(type = "column",name = "Number of Posts",
data = toJSONArray2(data[,c(1,2)],json = F,names = F)
)
a$series(type = "column",name = "Unique Posters",
data = toJSONArray2(data[,c(1,3)],json = F,names = F),
yAxis =3
)
a$series(type = "line",name = "Share Price",
data = toJSONArray2(data[,c(1,4)],json = F,names = F),
yAxis =1
)
a$colors('#2f7ed8', '#0d233a',"#9d233a",'darkorange')
a$tooltip(crosshairs = list(T,F),
shared=TRUE,
formatter = "#! function() {
var d = new Date(this.x);
d = d.toLocaleDateString();
d = '<strong>' + d + '</strong>';
$.each(this.points,function(){
d += '<br>' + this.series.name + ': ' + '<strong>' + this.y + '</strong>';
})
return (d);
}!#"
)
a$legend(align = "center left",verticalAlign = "top",
layout = "vertical",itemMarginTop = 10,x = 120,
floating = TRUE,backgroundColor = "white")
a$xAxis(type = "datetime")
a$plotOptions(series = list(pointPadding = 0,groupPadding = 0.2))
a$chart(zoomType = 'x')
a$set(width = 1000, height = 600)
a
}