-
Notifications
You must be signed in to change notification settings - Fork 35
/
sentiment.js
70 lines (60 loc) · 2.21 KB
/
sentiment.js
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
// Sentiment Survey
var dates = require('./isodate.js');
var Sentiment = module.exports = {
download:
function(cb) {
console.log("Downloading sentiment survey...");
global.quandl.dataset({ source: "AAII", table: "AAII_SENTIMENT" },
{ start_date: dates.two_years_ago },
function(err, response) {
if (err) return cb(err);
var data = JSON.parse(response);
if (data.quandl_error !== undefined) {
console.error(data.quandl_error.code + " " + data.quandl_error.message);
setTimeout(function() {
Sentiment.download(cb);
}, 10000);
} else {
_data = data;
setTimeout(cb, 1000);
}
})
},
analyse: function() {
return _analyse();
}
}
var result;
var _data;
function _analyse() {
let cBullish = _data.dataset.column_names.indexOf("Bullish");
let cBullishPlus = _data.dataset.column_names.indexOf("Bullish Average + St. Dev");
let cBullishMinus = _data.dataset.column_names.indexOf("Bullish Average - St. Dev");
let signal;
let data = _data.dataset.data.slice();
if (global.backtest) {
while (new Date(data[0][0]) > new Date(dates.today)) {
data = data.slice(1);
}
}
for (let i=0; i<data.length; i++) {
if (data[i+1][cBullish] > data[i][cBullish]) {
if ((i > 0) && (data[i][cBullish] < data[i-1][cBullish]))
break;
if ((data[i+1][cBullish] > data[i+1][cBullishPlus]) &&
(data[i][cBullish] < data[i][cBullishPlus])) {
signal = "SELL";
break;
}
} else {
if ((i > 0) && (data[i][cBullish] > data[i-1][cBullish]))
break;
if ((data[i+1][cBullish] < data[i+1][cBullishMinus]) &&
(data[i][cBullish] > data[i][cBullishMinus])) {
signal = "BUY";
break;
}
}
}
return { signal: signal, value: data[0][cBullish] };
}