ohlcv-indicators 3.4.7 → 3.4.8
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/dist/ohlcv-indicators.min.js +778 -529
- package/index.js +18 -0
- package/package.json +1 -1
- package/src/core-functions/mainLoop.js +2 -0
- package/src/moving-averages/movingAverages.js +8 -7
- package/src/volatility/atr.js +46 -0
package/index.js
CHANGED
|
@@ -259,6 +259,24 @@ export default class OHLCV_INDICATORS {
|
|
|
259
259
|
return this
|
|
260
260
|
}
|
|
261
261
|
|
|
262
|
+
atr(size = 14, options = {}) {
|
|
263
|
+
|
|
264
|
+
const methodName = 'atr'
|
|
265
|
+
|
|
266
|
+
isAlreadyComputed(this)
|
|
267
|
+
|
|
268
|
+
validateNumber(size, {min: 1, max: this.len, allowDecimals: false}, 'size', methodName)
|
|
269
|
+
validateObject(options, 'options', methodName)
|
|
270
|
+
|
|
271
|
+
const {lag = 0} = options
|
|
272
|
+
|
|
273
|
+
validateNumber(lag, {min: 0, max: this.len, allowDecimals: false}, 'options.lag', methodName)
|
|
274
|
+
|
|
275
|
+
this.inputParams.push({key: methodName, params: [size, {lag}]})
|
|
276
|
+
|
|
277
|
+
return this
|
|
278
|
+
}
|
|
279
|
+
|
|
262
280
|
ema(size = 5, options = {}) {
|
|
263
281
|
|
|
264
282
|
const methodName = 'ema'
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ohlcv-indicators",
|
|
3
|
-
"version": "3.4.
|
|
3
|
+
"version": "3.4.8",
|
|
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",
|
|
@@ -10,6 +10,7 @@ import { lag } from "../studies/lag.js";
|
|
|
10
10
|
import { crossPairs } from "../studies/findCrosses.js";
|
|
11
11
|
import { dateTime } from "../studies/dateTime.js";
|
|
12
12
|
import { scaler } from "../studies/scaler.js";
|
|
13
|
+
import { atr } from "../volatility/atr.js";
|
|
13
14
|
|
|
14
15
|
import { buildArray } from "../utilities/assignTypes.js";
|
|
15
16
|
import { numberFormater } from "../utilities/numberUtilities.js";
|
|
@@ -19,6 +20,7 @@ import { dateFormaters } from "../utilities/dateUtilities.js";
|
|
|
19
20
|
const indicatorFunctions = {
|
|
20
21
|
dateTime,
|
|
21
22
|
rsi,
|
|
23
|
+
atr,
|
|
22
24
|
ema: movingAverages,
|
|
23
25
|
sma: movingAverages,
|
|
24
26
|
macd,
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
|
|
2
2
|
import {FasterEMA, FasterSMA} from 'trading-signals';
|
|
3
3
|
|
|
4
|
-
const indicatorClasses = {
|
|
4
|
+
const indicatorClasses = {
|
|
5
|
+
ema: FasterEMA,
|
|
6
|
+
sma: FasterSMA
|
|
7
|
+
}
|
|
5
8
|
|
|
6
9
|
export const movingAverages = (main, index, indicatorName, size, { target, lag, precomputed }) => {
|
|
7
10
|
const { verticalOhlcv, instances } = main
|
|
@@ -18,13 +21,13 @@ export const movingAverages = (main, index, indicatorName, size, { target, lag,
|
|
|
18
21
|
|
|
19
22
|
// Retrieve the current price value.
|
|
20
23
|
const value = verticalOhlcv[target][index]
|
|
21
|
-
const
|
|
24
|
+
const instance = instances[keyName]
|
|
22
25
|
|
|
23
26
|
// Update the moving average instance.
|
|
24
|
-
|
|
27
|
+
instance.update(value);
|
|
25
28
|
let currMa = NaN;
|
|
26
29
|
try {
|
|
27
|
-
currMa =
|
|
30
|
+
currMa = instance.getResult();
|
|
28
31
|
} catch (err) {
|
|
29
32
|
|
|
30
33
|
}
|
|
@@ -42,9 +45,7 @@ export const precomputeMovingAverages = ({main, size, target, lag, methodName})
|
|
|
42
45
|
const keyName = `${methodName}_${size}${suffix}`
|
|
43
46
|
|
|
44
47
|
// Create the main moving average instance.
|
|
45
|
-
instances[keyName] =
|
|
46
|
-
maInstance: new indicatorClasses[methodName](size)
|
|
47
|
-
}
|
|
48
|
+
instances[keyName] = new indicatorClasses[methodName](size)
|
|
48
49
|
|
|
49
50
|
verticalOhlcv[keyName] = new Float64Array(len).fill(NaN)
|
|
50
51
|
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
|
|
2
|
+
import {FasterATR, FasterWSMA} from 'trading-signals';
|
|
3
|
+
|
|
4
|
+
export const atr = (main, index, size, {lag}) => {
|
|
5
|
+
const { verticalOhlcv, instances } = main
|
|
6
|
+
const keyName = `atr_${size}`
|
|
7
|
+
|
|
8
|
+
if (index === 0) {
|
|
9
|
+
|
|
10
|
+
const {instances, verticalOhlcv, arrayTypes, len, priceBased} = main
|
|
11
|
+
|
|
12
|
+
instances[keyName] = new FasterATR(size, FasterWSMA)
|
|
13
|
+
verticalOhlcv[keyName] = new Float64Array(len).fill(NaN)
|
|
14
|
+
|
|
15
|
+
priceBased.add(keyName)
|
|
16
|
+
|
|
17
|
+
if(lag > 0)
|
|
18
|
+
{
|
|
19
|
+
main.lag([keyName], lag)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
arrayTypes[keyName] = 'Float64Array'
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Retrieve the current price value.
|
|
26
|
+
const value = {
|
|
27
|
+
high: verticalOhlcv.high[index],
|
|
28
|
+
low: verticalOhlcv.low[index],
|
|
29
|
+
close: verticalOhlcv.close[index],
|
|
30
|
+
}
|
|
31
|
+
const instance = instances[keyName]
|
|
32
|
+
|
|
33
|
+
// Update the moving average instance.
|
|
34
|
+
instance.update(value);
|
|
35
|
+
let currMa = NaN;
|
|
36
|
+
try {
|
|
37
|
+
currMa = instance.getResult();
|
|
38
|
+
} catch (err) {
|
|
39
|
+
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Always push the MA value (even if NaN).
|
|
43
|
+
main.pushToMain({ index, key: keyName, value: currMa });
|
|
44
|
+
|
|
45
|
+
return true;
|
|
46
|
+
}
|