-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathThinkNum
More file actions
71 lines (52 loc) · 2.28 KB
/
Copy pathThinkNum
File metadata and controls
71 lines (52 loc) · 2.28 KB
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
install.packages(“ThinkNum”)
library(“Thinknum”)
goog <- Thinknum(“goog”)
plot(goog, type=“l”)
mThinknum <- function(command, tall=F) {
require(“Thinknum”)
# Break the command into seperate calls
think.list <- strsplit(command, “;”)[[1]]
# Look through each element of the think.list
for (i in 1:length(think.list)) {
cat(paste(“Reading:”,think.list[i])) # Display feedback
if (!tall) {
if (i==1) returner <- Thinknum(think.list[1])
if (i>1) returner <- merge(returner,Thinknum(think.list[i])
, by.x=“date_time”, by.y=“date_time”)
}
if (tall) {
tempdat <- Thinknum(think.list[i])
names(tempdat) <- c(“date_time”, “value”)
if (i==1) returner <- data.frame(tempdat, call=think.list[i])
if (i>1) returner <- rbind(returner, data.frame(tempdat, call=think.list[i]))
names(returner) <- c(“date_time”, “value”, “call”)
}
cat(paste(rep(” “, max(1,20–nchar(think.list[i]))), collapse=“”))
# Insert spaces
cat(paste(“Dimensions:”, paste(dim(returner), collapse=“x”), “\n“))
# Show dimensions
}
# Ensure the return file has appropriate column names
if (!tall) names(returner) <- c(“date_time”, think.list)
data.frame(returner)
}
test <- mThinknum(“^spx;sma(^spx,30);sma(goog*2,30);(goog*2)”)
head(test)
# Let’s try plotting with the base package
plot(x=c(min(test$date_time), max(test$date_time)),
y=c(min(test[,2:5]),max(test[,2:5])), type=“n”,
main=“Plot in R”)
lines(test$date_time, test[,2], type=“l”)
lines(test$date_time, test[,3], type=“l”, col=“red”)
lines(test$date_time, test[,4], type=“l”, col=“blue”)
lines(test$date_time, test[,5], type=“l”, col=“darkgreen”)
# Let’s do the same but now let’s use ggplot2
# In order to do this let’s use the tall option for mThinknum
test2 <- mThinknum(“^spx;sma(^spx,180);sma(goog*2,180);(goog*2)”, tall=T)
# I have extended the running average to be 180 days rather than 30 because the scale is
# so large.
head(test2)
library(“ggplot2″)
ggplot(data=test2[test2$date_time>as.Date(“2004-01-01″),],
aes(x=date_time, y=value, group=call)) +
geom_line(aes(colour = call))