-
Notifications
You must be signed in to change notification settings - Fork 5
/
analyse.R
93 lines (80 loc) · 2.76 KB
/
analyse.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
#####
#
# Quantifying Trading Behavior in Financial Markets Using Google Trends
#
# Copyright (C) 2013 Tobias Preis and Helen Susannah Moat
# http://www.tobiaspreis.de
# http://www.suzymoat.co.uk
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version
# 3 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, see
# http://www.gnu.org/licenses/.
#
# Related publication:
#
# Tobias Preis, Helen Susannah Moat, and H. Eugene Stanley,
# Scientific Reports 3, 1684 (2013)
# doi:10.1038/srep01684
#
#
# Read data file
read.csv(sep=" ","PreisMoatStanley_ScientificReports_3_1684_2013.dat")->dat
# Parameters
deltat<-3 # number of previous weeks of search volume to compare to
keyword<-"debt"
# Init trading account
r<-rep(1,nrow(dat))
# Analysis
for(i in 1:nrow(dat)) {
if(i>1) { # Not on first date (no previous search volume)
r[i]<-r[i-1] # Copy previous return, in case no trading
}
if(i>deltat) { # Wait for first window to pass,
# so we can calc past search volume
if(i<nrow(dat)) { # Not on last date (no future Dow Jones value)
now<-dat[[keyword]][i] # Google Trends search volume for keyword
# (e.g. "debt") for this week
previous<-0
# Calculate average search volume of last deltat weeks
for(t in 1:deltat) {
previous<-previous+dat[[keyword]][i-t]
}
previous<-(previous/deltat)
# Change in search volume
value<-(now-previous)
# DJIA closing price on the first trading day of the coming week
# *To check this, REFER TO FILE LAYOUT, which also includes dates
# for DJIA values*
index_now<-dat$DJIA.Closing.Price[i]
# DJIA closing price on the first trading day of the week
# after the coming week
index_next<-dat$DJIA.Closing.Price[i+1]
# Relative price change of the DJIA
index_r<-(index_next/index_now)
# Trading algorithm
if(value>0) { # search volume has gone up
# Short position
r[i]<-(r[i-1]/(index_r))
}
if(value<0) { # search volume has gone down
# Long position
r[i]<-(r[i-1]*(index_r))
}
}
}
}
# Print result
print(100*(r[nrow(dat)]-1))
# Plot result
plot(100*(r-1),type="l",col="blue",
xlab="Time, t [Weeks]", ylab="Profit and Loss [%]")