-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhelper.js
115 lines (94 loc) · 2.73 KB
/
helper.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
exports.trailingStopLoss = function() {
let _percentage = null;
let _prevPrice = null;
let _stopLoss = null;
let _isActive = false;
function initSettings(percentage, currentPrice) {
_percentage = percentage;
_prevPrice = currentPrice;
_stopLoss = calculateStopLoss(currentPrice);
_isActive = true;
};
function isTriggered(currentPrice) {
return (_stopLoss > currentPrice);
}
function calculateStopLoss(currentPrice) {
//return (((100 - _percentage)) / 100) * currentPrice;
return (_percentage * currentPrice);
};
function resetSettings() {
_percentage, _prevPrice, _percentage, _stopLoss = null;
_isActive = false;
};
function checkActiveState () {
return (_isActive)
}
function printVariables() {
console.log(`{"Percent": "${_percentage}", "PreviousPrice": "${_prevPrice}", "StopLoss": "${_stopLoss}", "isActive": "${_isActive}"}`)
};
return {
create: initSettings,
destroy: resetSettings,
update: calculateStopLoss,
log : printVariables,
triggered : isTriggered,
active : checkActiveState
}
};
exports.display = function() {
var emaTrend = function(currentEma, currentPrice) {
return (currentEma > currentPrice) ? 'uptrend' : 'downtrend';
}
var stochCondition = function(stochk, stochd, lowThreshold, highThreshold) {
if (stochk > highThreshold && stochd > highThreshold)
return 'overbought';
else if (stochk < lowThreshold && stochd < lowThreshold)
return 'oversold';
else
return 'middle';
}
return {
ema: emaTrend,
stoch: stochCondition,
}
}
/**
* We use this module to keep track of candle history.
*/
exports.candleHistory = function() {
var _limit = null;
var _candles = [];
var initialise = function(limit) {
_limit = limit - 1;
};
var _canAdd = function() {
return (_candles.length <= _limit);
};
var addCandleToarray = function(candle) {
if (_canAdd())
_candles.push(candle);
else {
_candles.shift();
_candles.push(candle);
}
};
var isFull = function() {
return (_candles.length > _limit);
};
var getCurrentCandles = function() {
return _candles;
};
var getCurrentSize = function() {
return _candles.length;
};
return {
init: initialise,
add: addCandleToarray,
get: getCurrentCandles,
full: isFull,
size: getCurrentSize
}
};
exports.trending = function isTrending(previousTrend, currentTrend) {
return (previousTrend === currentTrend);
};