forked from n3m6/Sessa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.js
138 lines (124 loc) · 4.2 KB
/
engine.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
const trade = require('./trade').Trade;
const strategy = require('./strategy').Strategy;
const db = require('./db').Db;
const orderlog = require('./orderlog').OrderLog;
const stopfunc = require('./stoploss.js');
const Engine = function Engine() {};
Engine.prototype.init = function init() {
trade.init();
};
function enterTrade(timestamp, activeTrade, orderType, close, atr) {
return new Promise((resolve, reject) => {
trade
.openPosition(orderType, close, atr)
.then(orderID => db.enterTrade(orderID, activeTrade, orderType))
.then(reply => resolve(reply))
.catch(reply => reject(reply));
});
}
function exitTrade(timestamp, close) {
return new Promise((resolve, reject) => {
db
.getOrderID()
.then((orderID) => {
trade
.closePosition(orderID, close)
.then(() => db.exitTrade())
.then(reply => resolve(reply))
.catch(reply => reject(reply));
})
.catch(reply => reject(reply));
});
}
Engine.prototype.processTrade = function processTrade(lastCandle) {
// eslint-disable-next-line
const [timestamp, open, high, low, close, sma1, sma2, rsi, macd, tr, atr] = lastCandle;
const args = {
timestamp,
open,
high,
low,
close,
sma1,
sma2,
rsi,
macd,
tr,
atr,
};
db
.getActiveTrade() // check if there's an active trade in the db
.then((activeTrade) => {
if (activeTrade === 'true') {
// check whether we should end the trade
// console.log('active trade found');
db
.getOrderType()
.then((orderType) => {
args.orderType = orderType;
// console.log(`order type${orderType}`);
if (strategy.exit(args)) {
// console.log('exit signal received');
exitTrade(timestamp, close)
.then(() => {
// Chck whether we need to enter a new trade after exiting previous trade
// console.log('checking whether we should enter a new trade');
const [at, ot] = strategy.enter(args);
// if an order needs to be placed
if (at === true) {
// console.log('new trade entry signal received');
enterTrade(timestamp, at, ot, close, atr).catch(console.error);
}
})
.catch(reply => console.error(reply));
} else {
// console.log('no exit signal, moving stop loss if necessary');
db
.getStopLoss()
.then((dbStop) => {
// Determine whether we need to move stop loss
const [stopMove, newPrice] = stopfunc.stop(dbStop, args);
if (stopMove) {
// console.log('moving stop loss');
trade.amendStoploss(newPrice).catch(console.error);
}
})
.catch(console.error);
// LOG update
orderlog.update(timestamp, '-', orderType, close);
}
})
.catch(console.error);
} else {
// check whether we should enter a trade
// console.log('no active trade');
const [at, ot] = strategy.enter(args);
// console.log(`strategy active trade: ${at} order type: ${ot}`);
// if an order needs to be placed
if (at === true) {
// console.log('enter signal recieved');
enterTrade(timestamp, at, ot, close, atr).catch(console.error);
}
}
})
.catch(reply => console.error(`${reply}`));
};
Engine.prototype.oneMinuteProcessing = function oneMinuteProcessing(timestamp) {
db
.getOneCandle(timestamp)
.then(reply => this.processTrade(reply))
.catch(console.error);
};
Engine.prototype.fiveMinuteProcessing = function fiveMinuteProcessing(timestamp) {
db
.getFiveCandle(timestamp)
.then(reply => this.processTrade(reply))
.catch(console.error);
};
Engine.prototype.fifteenMinuteProcessing = function fifteenMinuteProcessing(timestamp) {
db
.getFifteenCandle(timestamp)
.then(reply => this.processTrade(reply))
.catch(console.error);
};
exports.Engine = new Engine();