forked from yura505/robinbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
markets.js
224 lines (198 loc) · 7.82 KB
/
markets.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
var series = require("run-series");
var n = require('numbro');
var pad = require('pad');
var colors = require('colors');
var request = require('request');
var ta = require('./technical.js');
var dates = require('./isodate.js');
var quotes = require('./quotes.js');
var sentiment = require('./sentiment.js');
var conf = require('./conf.js');
var compq_index = [ ];
var nasdaq_adv, nasdaq_dec;
var Markets = module.exports = {
breadth: "BUY",
_hours: [ { next_open_date: null } ],
download: function(cb) {
let tasks = [ download_hours, download_index ];
if (conf.market_breadth) {
tasks.push(download_adv, download_dec, sentiment.download);
}
series(tasks, (err, result) => cb(err, result));
},
download_index: function(cb) {
download_index(cb);
},
get isOpen() {
let now = new Date();
return this._hours.forEach.reduce(function(total, cur) {
return total && (cur.is_open && (now < cur.closes_at) && (now > cur.opens_at));
}, true);
},
get nextDate() {
return this._hours[0].next_open_date;
},
get prevDate() {
return this._hours[0].previous_open_date;
},
get compqIndex() {
return compq_index;
},
analyse: function() {
let nasdaq_composite = (global.backtest_offset !== undefined) ?
compq_index.slice(global.backtest_offset) : compq_index;
let nasi = NASI();
let sent = sentiment.analyse(); // do the analysis
let macdh = ta.MACDH(nasdaq_composite);
let sma100 = ta.SMA(nasdaq_composite, 100);
let buy = 0, sell = 0;
// determine BUY strength
if (nasdaq_composite[0].close > sma100) buy++;
if (macdh > 0) buy++;
if (sent.signal == "BUY") buy++;
if ((nasi.msi > nasi.ema5) && (nasi.macd > nasi.signal)) buy++;
// determine SELL strength
if (nasdaq_composite[0].close < sma100) sell++;
if (macdh < 0) sell++;
if (sent.signal == "SELL") sell++;
if ((nasi.msi < nasi.ema5) && (nasi.macd < nasi.signal)) sell++;
// determine the signal string, BUY or SELL
let signal = (buy >= 3) ? "BUY" : (sell >= 3) ? "SELL" : "HOLD";
// log the results
let log = "MARKET: ";
log += "close: "+n(nasdaq_composite[0].close).format("0")[nasdaq_composite[0].close >= nasdaq_composite[1].close ? "green" : "red"] + " ";
log += "sentiment: "+n(sent.value).format("0.00")[sent.signal == "BUY" ? "green" : sent.signal == "SELL" ? "red" : "reset"] + " ";
log += "nasi: (msi:"+n(nasi.msi).format("0.0")[nasi.msi > nasi.msi0 ? "green" : "red"]+
", ema:"+n(nasi.ema5).format("0.0")[nasi.ema5 < nasi.msi ? "green" : "red"]+
", macdh:"+n(nasi.macd-nasi.signal).format("0.0")[nasi.signal < nasi.macd ? "green" : "red"]+") ";
log += "macdh: "+n(macdh).format("0.0")[macdh > 0 ? "green" : "red"]+" ";
log += "sma: "+n(sma100).format("0")[sma100 < nasdaq_composite[0].close ? "green" : "red"] + " ";
log += "signal: "+(signal ? signal : "")[(signal == "BUY") ? 'green' : (signal == "SELL") ? 'red' : 'reset'];
console.log("");
console.log(log);
console.log("");
this.breadth = signal;
}
}
// prices advancing (from quandl)
function download_adv(cb) {
console.log("Downloading number of stocks with Prices Advancing...");
global.quandl.dataset({ source: "URC", table: "NASDAQ_ADV" },
{ start_date: dates.two_years_ago },
function(err, response) {
if (err) return cb(err);
let data = JSON.parse(response);
if (data.quandl_error !== undefined) {
console.error(data.quandl_error.code + " " + data.quandl_error.message);
setTimeout(function() {
download_adv(cb);
}, 10000);
} else {
nasdaq_adv = data;
setTimeout(cb, 1000);
}
})
}
// prices declining (from quandl)
function download_dec(cb) {
console.log("Downloading number of stocks with Prices Declining...");
global.quandl.dataset({ source: "URC", table: "NASDAQ_DEC" },
{ start_date: dates.two_years_ago },
function(err, response) {
if (err) return cb(err);
let data = JSON.parse(response);
if (data.quandl_error !== undefined) {
console.error(data.quandl_error.code + " " + data.quandl_error.message);
setTimeout(function() {
download_dec(cb);
}, 10000);
} else {
nasdaq_dec = data;
setTimeout(cb, 1000);
}
})
}
// composite index (from iextrading)
function download_index(cb) {
console.log("Downloading NASDAQ Composite index...")
let url = "https://api.iextrading.com/1.0/stock/ONEQ/batch?types=quote,chart&range=2y";
request(url, function(err, resp, body) {
if (err) {
console.error(err);
return setTimeout(download_index, 10000);
}
let jbody = JSON.parse(body);
let quote = jbody.quote;
let chart = jbody.chart;
compq_index = chart.sort(function(a, b) {
return new Date(b.date) - new Date(a.date);
});
quote.close = n(quote.latestPrice).value();
compq_index.unshift(quote);
cb();
})
}
// date/time (from robinhood)
function download_hours(cb) {
if (global.Robinhood === undefined) {
return cb();
}
console.log("Downloading markets date/time...");
global.Robinhood.markets(function(err, resp, body){
if (err) return cb(err);
let tasks = [];
body.results.forEach(function(result) {
tasks.push(function(cb) {
request(result.todays_hours, function(err, resp, body) {
if (err) return cb(err);
let jbody = JSON.parse(body);
let hours = {
is_open: jbody.is_open,
closes_at: Date.parse(jbody.closes_at),
opens_at: Date.parse(jbody.opens_at),
previous_open_date: parseDateUrl(jbody.previous_open_hours),
next_open_date: parseDateUrl(jbody.next_open_hours)
};
cb(null, hours);
})
})
});
series(tasks, function(err, result) {
if (err) cb(err);
Markets._hours = result;
cb();
});
});
}
function NASI() {
let length = Math.min(nasdaq_adv.dataset.data.length, nasdaq_dec.dataset.data.length);
let rana = [ ];
let signal;
for (let i=0; i<length; i++) {
if (nasdaq_adv.dataset.data[i][0] === nasdaq_dec.dataset.data[i][0]) {
let value = 1000 * (nasdaq_adv.dataset.data[i][1] - nasdaq_dec.dataset.data[i][1]) /
(nasdaq_adv.dataset.data[i][1] + nasdaq_dec.dataset.data[i][1]);
if (!isNaN(value)) rana.push({ date: nasdaq_adv.dataset.data[i][0], close: value });
}
}
ta.EMA(rana, 19, 'e1');
ta.EMA(rana, 39, 'e2');
rana.forEach(function(period) {
if (period['e1'] !== undefined && period['e2'] !== undefined)
period['mo'] = period['e1'] - period['e2'];
});
for (let i=rana.length-2; i>=0; i--) {
if (rana[i]['mo'] !== undefined) {
rana[i]['msi'] = ((rana[i+1]['msi'] === undefined) ? 0 : rana[i+1]['msi']) + rana[i]['mo'];
}
rana[i]['msi0'] = rana[i+1]['msi'];
}
ta.EMA(rana, 5, 'ema5', 'msi');
ta.MACDH(rana, 0, 'msi');
return (global.backtest_offset !== undefined) ?
rana[global.backtest_offset] : rana[0];
}
function parseDateUrl(url) {
let parts = url.split('/');
return parts.pop() || parts.pop();
}