forked from n3m6/Sessa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstoploss.js
53 lines (43 loc) · 1.24 KB
/
stoploss.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
const config = require('./config.js');
// eslint-disable-next-line
function fixed(stopLoss, args) {
// fixed stop loss does nothing
return [false, stopLoss];
}
function trail(dbStop, args) {
const { open, close, orderType } = args;
// calc for LONG positions
if (orderType === 'LONG') {
if (close > open) {
const candleSize = parseFloat(close) - parseFloat(open);
const moveSize = Math.round(candleSize * config.stopTrail);
const newPrice = Math.round(parseFloat(dbStop) + moveSize);
return [true, newPrice];
}
return [false, dbStop];
}
// calc for SHORT positions
if (open > close) {
const candleSize = parseFloat(open) - parseFloat(close);
const moveSize = Math.round(candleSize * config.stopTrail);
const newPrice = Math.round(parseFloat(dbStop) - moveSize);
return [true, newPrice];
}
return [false, dbStop]; // move: true or false, newprice
}
function stop(stopLoss, args) {
let newStop = stopLoss;
switch (config.stop) {
case 'FIXED':
newStop = fixed(stopLoss, args);
break;
case 'TRAIL':
newStop = trail(stopLoss, args);
break;
default:
newStop = fixed(stopLoss, args);
break;
}
return newStop;
}
module.exports = { stop };