ohlcv-indicators 3.3.0 → 3.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ohlcv-indicators",
|
|
3
|
-
"version": "3.3.
|
|
3
|
+
"version": "3.3.1",
|
|
4
4
|
"description": "A JavaScript library featuring optimized trading indicators for OHLCV arrays [{open, high, low, close, value}]. This library is optimized for backtesting and trading bot development.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -26,7 +26,8 @@ export const movingAverages = (main, index, indicatorName, size, { target, diff
|
|
|
26
26
|
|
|
27
27
|
// Create the main moving average instance.
|
|
28
28
|
instances[keyName] = {
|
|
29
|
-
maInstance: new indicatorClasses[indicatorName](size)
|
|
29
|
+
maInstance: new indicatorClasses[indicatorName](size),
|
|
30
|
+
arrayChunk: {[diffKeyName]: []}
|
|
30
31
|
};
|
|
31
32
|
|
|
32
33
|
verticalOhlcv[keyName] = [...nullArray];
|
|
@@ -63,7 +64,7 @@ export const movingAverages = (main, index, indicatorName, size, { target, diff
|
|
|
63
64
|
|
|
64
65
|
// Retrieve the current price value.
|
|
65
66
|
const value = verticalOhlcv[target][index];
|
|
66
|
-
const { maInstance, diffInstance } = instances[keyName];
|
|
67
|
+
const { maInstance, diffInstance, arrayChunk } = instances[keyName];
|
|
67
68
|
|
|
68
69
|
// Update the moving average instance.
|
|
69
70
|
maInstance.update(value, lastIndexReplace);
|
|
@@ -82,6 +83,7 @@ export const movingAverages = (main, index, indicatorName, size, { target, diff
|
|
|
82
83
|
for (const targetKey of diff.targets) {
|
|
83
84
|
// Compute diffValue only if we have a valid MA value.
|
|
84
85
|
let diffValue = null;
|
|
86
|
+
let zScore = null
|
|
85
87
|
if (currMa !== null && typeof currMa === 'number') {
|
|
86
88
|
diffValue = verticalOhlcv[targetKey][index] - currMa;
|
|
87
89
|
}
|
|
@@ -89,7 +91,7 @@ export const movingAverages = (main, index, indicatorName, size, { target, diff
|
|
|
89
91
|
// Update the diff indicator only if diffValue is a valid number.
|
|
90
92
|
if (diffValue !== null && typeof diffValue === 'number') {
|
|
91
93
|
diffInstance.update(Math.abs(diffValue), lastIndexReplace);
|
|
92
|
-
|
|
94
|
+
zScore = calcZScore(arrayChunk, diffKeyName, diff.size, diffValue, lastIndexReplace)
|
|
93
95
|
}
|
|
94
96
|
|
|
95
97
|
let diffBoll = null;
|
|
@@ -103,7 +105,7 @@ export const movingAverages = (main, index, indicatorName, size, { target, diff
|
|
|
103
105
|
main.pushToMain({
|
|
104
106
|
index,
|
|
105
107
|
key: `${diffKeyName}_${targetKey}`,
|
|
106
|
-
value:
|
|
108
|
+
value: zScore
|
|
107
109
|
});
|
|
108
110
|
}
|
|
109
111
|
}
|
|
@@ -50,28 +50,26 @@ export const classifyBoll = (value, bollingerBands, scale = 0.05, autoMinMax = f
|
|
|
50
50
|
|
|
51
51
|
|
|
52
52
|
export const calcZScore = (arrayChunk, key, size, difference, lastIndexReplace) => {
|
|
53
|
+
const positive = difference > 0
|
|
54
|
+
const absValue = Math.abs(difference)
|
|
53
55
|
|
|
54
|
-
if(lastIndexReplace)
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
else
|
|
59
|
-
{
|
|
60
|
-
arrayChunk[key].push(difference)
|
|
56
|
+
if (lastIndexReplace) {
|
|
57
|
+
arrayChunk[key][arrayChunk[key].length - 1] = absValue
|
|
58
|
+
} else {
|
|
59
|
+
arrayChunk[key].push(absValue)
|
|
61
60
|
}
|
|
62
|
-
|
|
63
61
|
|
|
64
62
|
if (arrayChunk[key].length > size) {
|
|
65
|
-
|
|
63
|
+
arrayChunk[key].shift() // Maintain array size
|
|
66
64
|
}
|
|
67
65
|
|
|
68
|
-
if(arrayChunk.length < size) return null
|
|
66
|
+
if (arrayChunk[key].length < size) return null
|
|
69
67
|
|
|
70
68
|
const mean = arrayChunk[key].reduce((sum, value) => sum + value, 0) / size
|
|
71
|
-
|
|
72
69
|
const stdDev = Math.sqrt(
|
|
73
|
-
|
|
70
|
+
arrayChunk[key].reduce((sum, value) => sum + (value - mean) ** 2, 0) / size
|
|
74
71
|
)
|
|
75
72
|
|
|
76
|
-
|
|
73
|
+
const zScore = (absValue - mean) / stdDev
|
|
74
|
+
return (positive) ? zScore : -zScore
|
|
77
75
|
}
|