-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
89 lines (76 loc) · 2.68 KB
/
index.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
const Poloniex = require('poloniex-api-node');
const moment = require('moment');
const config = require('./config');
const mailer = require('./src/mailer');
const logger = require('./src/logger');
const { sortCandlesByDate, getTimestampInSeconds, compareTwoNumbers } = require('./src/utils');
const { calculateLine } = require('./src/ichimoku');
const poloniex = new Poloniex;
const signals = {};
/**
* Actual code
*/
run();
setInterval(run, config.iteratingInterval);
function run() {
config.tickers.forEach(ticker => {
iterate(ticker.toUpperCase());
});
};
function iterate(ticker) {
poloniex
.returnChartData(ticker, config.candleIntervalInSeconds, getTimestampInSeconds() - (config.warmupTimeInMinutes * 60 * 2), getTimestampInSeconds())
.then(response => {
logger.log('info', `Poloniex API request made for ${ ticker.toUpperCase() }`, {
date: moment().format('MMMM Do YYYY, HH:mm:ss')
});
// sort chart data, latest come first
response.sort(sortCandlesByDate);
// calculate ichimoku points
const ichimokus = [];
for (let i = 0; i < response.length / 2; ++i) {
const conversionLine = calculateLine(response.slice(i, i + config.conversionLinePeriods));
const baseLine = calculateLine(response.slice(i, i + config.baseLinePeriods));
ichimokus.push({ index: i, baseLine, conversionLine });
}
// define entry point
let state = null;
for (let i = 0; i < 5; ++i) {
const conversionLine = ichimokus[i].conversionLine;
const baseLine = ichimokus[i].baseLine;
const currentState = compareTwoNumbers(conversionLine, baseLine);
if (state === null) {
state = compareTwoNumbers(conversionLine, baseLine);
continue;
}
if (currentState !== state && currentState !== 0) {
if (signals[ticker] && signals[ticker].last && signals[ticker].last.state === currentState) {
break;
}
// notify
signals[ticker] = {
last: {
state: currentState
}
};
mailer.sendMail({
from: config.mail_user,
to: config.notify_emails.join(', '),
subject: 'Jbbot cross point notification',
conversionLine,
baseLine,
text: `${ ticker.toUpperCase() }, cross point just happened!`
});
logger.log('info', 'Cross point', {
date: moment().format('MMMM Do YYYY, HH:mm:ss'),
ticker: ticker.toUpperCase(),
conversionLine,
baseLine
});
break;
}
}
}).catch(err => {
console.log(err.message);
});
}