ohlcv-indicators 3.4.8 → 3.5.0

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.
@@ -2905,29 +2905,30 @@ var OHLCV_INDICATORS;
2905
2905
  });
2906
2906
  },
2907
2907
  atr: function(t, e, r, n) {
2908
- var o = n.lag, i = t.verticalOhlcv, a = t.instances, u = "atr_".concat(r);
2908
+ var o = n.lag, i = n.type, a = t.verticalOhlcv, u = t.instances, l = "percentage" === i ? "atr_".concat(r, "_percentage") : "atr_".concat(r);
2909
2909
  if (0 === e) {
2910
- var l = t.instances, c = t.verticalOhlcv, s = t.arrayTypes, f = t.len, y = t.priceBased;
2911
- l[u] = new Yt(r, jt), c[u] = new Float64Array(f).fill(NaN), y.add(u), o > 0 && t.lag([ u ], o),
2912
- s[u] = "Float64Array";
2910
+ var c = t.instances, s = t.verticalOhlcv, f = t.arrayTypes, y = t.len, p = t.priceBased;
2911
+ c[l] = new Yt(r, jt), "price" === i && p.add(l), s[l] = new Float64Array(y).fill(NaN),
2912
+ f[l] = "Float64Array", o > 0 && t.lag([ l ], o);
2913
2913
  }
2914
2914
  // Retrieve the current price value.
2915
- var p = {
2916
- high: i.high[e],
2917
- low: i.low[e],
2918
- close: i.close[e]
2919
- }, h = a[u];
2915
+ var h = {
2916
+ high: a.high[e],
2917
+ low: a.low[e],
2918
+ close: a.close[e]
2919
+ }, v = u[l];
2920
2920
  // Update the moving average instance.
2921
- h.update(p);
2922
- var v = NaN;
2921
+ v.update(h);
2922
+ var m = NaN;
2923
2923
  try {
2924
- v = h.getResult();
2924
+ m = v.getResult();
2925
2925
  } catch (t) {}
2926
+ return "percentage" === i && (m = Number.isNaN(m) ? NaN : m / h.close),
2926
2927
  // Always push the MA value (even if NaN).
2927
- return t.pushToMain({
2928
+ t.pushToMain({
2928
2929
  index: e,
2929
- key: u,
2930
- value: v
2930
+ key: l,
2931
+ value: m
2931
2932
  }), !0;
2932
2933
  },
2933
2934
  ema: Ce,
@@ -3931,15 +3932,16 @@ var OHLCV_INDICATORS;
3931
3932
  max: this.len,
3932
3933
  allowDecimals: !1
3933
3934
  }, "size", r), s(e, "options", r);
3934
- var n = e.lag, o = void 0 === n ? 0 : n;
3935
+ var n = e.lag, o = void 0 === n ? 0 : n, i = e.type, a = void 0 === i ? "price" : i;
3935
3936
  return p(o, {
3936
3937
  min: 0,
3937
3938
  max: this.len,
3938
3939
  allowDecimals: !1
3939
- }, "options.lag", r), this.inputParams.push({
3940
+ }, "options.lag", r), f([ "price", "percentage" ], a, "options.type", r), this.inputParams.push({
3940
3941
  key: r,
3941
3942
  params: [ t, {
3942
- lag: o
3943
+ lag: o,
3944
+ type: a
3943
3945
  } ]
3944
3946
  }), this;
3945
3947
  }
package/index.js CHANGED
@@ -268,11 +268,12 @@ export default class OHLCV_INDICATORS {
268
268
  validateNumber(size, {min: 1, max: this.len, allowDecimals: false}, 'size', methodName)
269
269
  validateObject(options, 'options', methodName)
270
270
 
271
- const {lag = 0} = options
271
+ const {lag = 0, type = 'price'} = options
272
272
 
273
273
  validateNumber(lag, {min: 0, max: this.len, allowDecimals: false}, 'options.lag', methodName)
274
+ validateArrayOptions(['price', 'percentage'], type, 'options.type', methodName)
274
275
 
275
- this.inputParams.push({key: methodName, params: [size, {lag}]})
276
+ this.inputParams.push({key: methodName, params: [size, {lag, type}]})
276
277
 
277
278
  return this
278
279
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ohlcv-indicators",
3
- "version": "3.4.8",
3
+ "version": "3.5.0",
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",
@@ -1,29 +1,32 @@
1
1
 
2
2
  import {FasterATR, FasterWSMA} from 'trading-signals';
3
3
 
4
- export const atr = (main, index, size, {lag}) => {
4
+ export const atr = (main, index, size, {lag, type}) => {
5
5
  const { verticalOhlcv, instances } = main
6
- const keyName = `atr_${size}`
6
+ const keyName = (type === 'percentage') ? `atr_${size}_percentage` : `atr_${size}`
7
7
 
8
8
  if (index === 0) {
9
9
 
10
10
  const {instances, verticalOhlcv, arrayTypes, len, priceBased} = main
11
11
 
12
12
  instances[keyName] = new FasterATR(size, FasterWSMA)
13
- verticalOhlcv[keyName] = new Float64Array(len).fill(NaN)
14
-
15
- priceBased.add(keyName)
16
13
 
14
+ if(type === 'price')
15
+ {
16
+ priceBased.add(keyName)
17
+ }
18
+
19
+ verticalOhlcv[keyName] = new Float64Array(len).fill(NaN)
20
+ arrayTypes[keyName] = 'Float64Array'
21
+
17
22
  if(lag > 0)
18
23
  {
19
24
  main.lag([keyName], lag)
20
25
  }
21
-
22
- arrayTypes[keyName] = 'Float64Array'
23
26
  }
24
27
 
25
28
  // Retrieve the current price value.
26
- const value = {
29
+ const curr = {
27
30
  high: verticalOhlcv.high[index],
28
31
  low: verticalOhlcv.low[index],
29
32
  close: verticalOhlcv.close[index],
@@ -31,16 +34,21 @@ export const atr = (main, index, size, {lag}) => {
31
34
  const instance = instances[keyName]
32
35
 
33
36
  // Update the moving average instance.
34
- instance.update(value);
35
- let currMa = NaN;
37
+ instance.update(curr);
38
+ let currAtr = NaN;
36
39
  try {
37
- currMa = instance.getResult();
40
+ currAtr = instance.getResult();
38
41
  } catch (err) {
39
42
 
40
43
  }
41
44
 
45
+ if(type === 'percentage')
46
+ {
47
+ currAtr = (Number.isNaN(currAtr)) ? NaN : (currAtr / curr.close)
48
+ }
49
+
42
50
  // Always push the MA value (even if NaN).
43
- main.pushToMain({ index, key: keyName, value: currMa });
51
+ main.pushToMain({ index, key: keyName, value: currAtr });
44
52
 
45
53
  return true;
46
54
  }