forked from n3m6/Sessa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
74 lines (61 loc) · 2.22 KB
/
main.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
const redis = require('redis');
const config = require('./config');
const engine = require('./engine').Engine;
const BitmexClient = require('./bitmexlib/bitmexlib.js');
const omonitor = require('./ordermon.js').OrderMonitor;
const pmonitor = require('./positionmon.js').PositionMonitor;
// Delta pushes messages into the pubsub, main reads them
const { port, host } = config.redis;
const pubsub = redis.createClient(port, host, { no_ready_check: true });
const pubsubOneMin = `${config.bitmex1MinPrefix}:pubsub`;
const pubsubFiveMin = `${config.bitmex5MinPrefix}:pubsub`;
const pubsubFifteenMin = `${config.bitmex15MinPrefix}:pubsub`;
const TimeEnum = {
ONE: 1,
FIVE: 5,
FIFTEEN: 15,
};
const timeframe = TimeEnum.FIFTEEN; // Change this to change time frame;
const client = new BitmexClient(config.bitmexConfig);
client.on('open', () => console.log('connection opened.'));
client.on('error', err => console.log('caught error', err));
client.on('close', () => console.log('connection closed.'));
client.on('initialize', () => console.log('initialized, waiting for data'));
/*
Main requires an active delta server that processes incoming trades
and attaches values like RSI and MACD to it.
Run delta server independently from main
*/
pubsub.on('error', (err) => {
console.error(`DB Error. Is DB available? ${err}`);
throw err;
});
engine.init();
/*
three pubsubs exist right now, 1 min pubsub, 5 min pubsub & 15 min pubsub
*/
switch (timeframe) {
case TimeEnum.ONE:
pubsub.subscribe(pubsubOneMin);
pubsub.on('message', (channel, message) => {
engine.oneMinuteProcessing(message);
});
break;
case TimeEnum.FIVE:
pubsub.subscribe(pubsubFiveMin);
pubsub.on('message', (channel, message) => {
engine.fiveMinuteProcessing(message);
});
break;
case TimeEnum.FIFTEEN:
pubsub.subscribe(pubsubFifteenMin);
pubsub.on('message', (channel, message) => {
engine.fifteenMinuteProcessing(message);
});
break;
default:
throw console.error('No strategy time frame defined');
}
// Order Monitoring (will trigger stop losses, remove liquidated orders etc)
client.addStream('XBTUSD', 'order', data => omonitor.monitor(data));
client.addStream('XBTUSD', 'position', data => pmonitor.monitor(data));