ohlcv-indicators 3.4.9 → 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,44 +2905,30 @@ var OHLCV_INDICATORS;
2905
2905
  });
2906
2906
  },
2907
2907
  atr: function(t, e, r, n) {
2908
- var o = n.lag, i = n.tp, a = n.sl, u = t.verticalOhlcv, l = t.instances, c = "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 s = t.instances, f = t.verticalOhlcv, y = t.arrayTypes, p = t.len, h = t.priceBased;
2911
- s[c] = new Yt(r, jt);
2912
- var v = [ c ];
2913
- null !== i && v.push("".concat(c, "_tp")), null !== a && v.push("".concat(c, "_sl"));
2914
- for (var m = 0, d = v; m < d.length; m++) {
2915
- var b = d[m];
2916
- h.add(b), f[b] = new Float64Array(p).fill(NaN), y[b] = "Float64Array";
2917
- }
2918
- o > 0 && t.lag(v, o);
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);
2919
2913
  }
2920
2914
  // Retrieve the current price value.
2921
- var g = {
2922
- high: u.high[e],
2923
- low: u.low[e],
2924
- close: u.close[e]
2925
- }, w = l[c];
2915
+ var h = {
2916
+ high: a.high[e],
2917
+ low: a.low[e],
2918
+ close: a.close[e]
2919
+ }, v = u[l];
2926
2920
  // Update the moving average instance.
2927
- w.update(g);
2928
- var O = NaN;
2921
+ v.update(h);
2922
+ var m = NaN;
2929
2923
  try {
2930
- O = w.getResult();
2924
+ m = v.getResult();
2931
2925
  } catch (t) {}
2932
- return null !== i && t.pushToMain({
2933
- index: e,
2934
- key: "".concat(c, "_tp"),
2935
- value: Number.isNaN(O) ? NaN : g.close + O * i
2936
- }), null !== a && t.pushToMain({
2937
- index: e,
2938
- key: "".concat(c, "_sl"),
2939
- value: Number.isNaN(O) ? NaN : g.close - O * a
2940
- }),
2926
+ return "percentage" === i && (m = Number.isNaN(m) ? NaN : m / h.close),
2941
2927
  // Always push the MA value (even if NaN).
2942
2928
  t.pushToMain({
2943
2929
  index: e,
2944
- key: c,
2945
- value: O
2930
+ key: l,
2931
+ value: m
2946
2932
  }), !0;
2947
2933
  },
2948
2934
  ema: Ce,
@@ -3946,25 +3932,16 @@ var OHLCV_INDICATORS;
3946
3932
  max: this.len,
3947
3933
  allowDecimals: !1
3948
3934
  }, "size", r), s(e, "options", r);
3949
- var n = e.lag, o = void 0 === n ? 0 : n, i = e.tp, a = void 0 === i ? null : i, u = e.sl, c = void 0 === u ? null : u;
3935
+ var n = e.lag, o = void 0 === n ? 0 : n, i = e.type, a = void 0 === i ? "price" : i;
3950
3936
  return p(o, {
3951
3937
  min: 0,
3952
3938
  max: this.len,
3953
3939
  allowDecimals: !1
3954
- }, "options.lag", r), null !== a && p(a, {
3955
- min: .01,
3956
- max: 100,
3957
- allowDecimals: !0
3958
- }, "options.tp", r), null !== c && p(c, {
3959
- min: .01,
3960
- max: 100,
3961
- allowDecimals: !0
3962
- }, "options.tp", r), this.inputParams.push({
3940
+ }, "options.lag", r), f([ "price", "percentage" ], a, "options.type", r), this.inputParams.push({
3963
3941
  key: r,
3964
3942
  params: [ t, {
3965
3943
  lag: o,
3966
- tp: a,
3967
- sl: c
3944
+ type: a
3968
3945
  } ]
3969
3946
  }), this;
3970
3947
  }
package/index.js CHANGED
@@ -268,14 +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, tp = null, sl = null} = 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
- if(tp !== null) validateNumber(tp, {min: 0.01, max: 100, allowDecimals: true}, 'options.tp', methodName)
276
- if(sl !== null) validateNumber(sl, {min: 0.01, max: 100, allowDecimals: true}, 'options.tp', methodName)
277
-
278
- this.inputParams.push({key: methodName, params: [size, {lag, tp, sl}]})
276
+ this.inputParams.push({key: methodName, params: [size, {lag, type}]})
279
277
 
280
278
  return this
281
279
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ohlcv-indicators",
3
- "version": "3.4.9",
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,39 +1,27 @@
1
1
 
2
2
  import {FasterATR, FasterWSMA} from 'trading-signals';
3
3
 
4
- export const atr = (main, index, size, {lag, tp, sl}) => {
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
-
14
- const keyNames = [keyName]
15
-
16
- if(tp !== null)
17
- {
18
- keyNames.push(`${keyName}_tp`)
19
- }
20
-
21
- if(sl !== null)
22
- {
23
- keyNames.push(`${keyName}_sl`)
24
- }
25
13
 
26
- for(const k of keyNames)
14
+ if(type === 'price')
27
15
  {
28
- priceBased.add(k)
29
- verticalOhlcv[k] = new Float64Array(len).fill(NaN)
30
- arrayTypes[k] = 'Float64Array'
16
+ priceBased.add(keyName)
31
17
  }
32
18
 
33
-
19
+ verticalOhlcv[keyName] = new Float64Array(len).fill(NaN)
20
+ arrayTypes[keyName] = 'Float64Array'
21
+
34
22
  if(lag > 0)
35
23
  {
36
- main.lag(keyNames, lag)
24
+ main.lag([keyName], lag)
37
25
  }
38
26
  }
39
27
 
@@ -54,14 +42,9 @@ export const atr = (main, index, size, {lag, tp, sl}) => {
54
42
 
55
43
  }
56
44
 
57
- if(tp !== null)
58
- {
59
- main.pushToMain({ index, key: `${keyName}_tp`, value: (!Number.isNaN(currAtr)) ? curr.close + (currAtr*tp) : NaN });
60
- }
61
-
62
- if(sl !== null)
45
+ if(type === 'percentage')
63
46
  {
64
- main.pushToMain({ index, key: `${keyName}_sl`, value: (!Number.isNaN(currAtr)) ? curr.close - (currAtr*sl) : NaN });
47
+ currAtr = (Number.isNaN(currAtr)) ? NaN : (currAtr / curr.close)
65
48
  }
66
49
 
67
50
  // Always push the MA value (even if NaN).