ohlcv-indicators 3.4.8 → 3.4.9

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,44 @@ 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.tp, a = n.sl, u = t.verticalOhlcv, l = t.instances, c = "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 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);
2913
2919
  }
2914
2920
  // 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];
2921
+ var g = {
2922
+ high: u.high[e],
2923
+ low: u.low[e],
2924
+ close: u.close[e]
2925
+ }, w = l[c];
2920
2926
  // Update the moving average instance.
2921
- h.update(p);
2922
- var v = NaN;
2927
+ w.update(g);
2928
+ var O = NaN;
2923
2929
  try {
2924
- v = h.getResult();
2930
+ O = w.getResult();
2925
2931
  } 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
2941
  // Always push the MA value (even if NaN).
2927
- return t.pushToMain({
2942
+ t.pushToMain({
2928
2943
  index: e,
2929
- key: u,
2930
- value: v
2944
+ key: c,
2945
+ value: O
2931
2946
  }), !0;
2932
2947
  },
2933
2948
  ema: Ce,
@@ -3931,15 +3946,25 @@ var OHLCV_INDICATORS;
3931
3946
  max: this.len,
3932
3947
  allowDecimals: !1
3933
3948
  }, "size", r), s(e, "options", r);
3934
- var n = e.lag, o = void 0 === n ? 0 : n;
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
3950
  return p(o, {
3936
3951
  min: 0,
3937
3952
  max: this.len,
3938
3953
  allowDecimals: !1
3939
- }, "options.lag", r), this.inputParams.push({
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
3963
  key: r,
3941
3964
  params: [ t, {
3942
- lag: o
3965
+ lag: o,
3966
+ tp: a,
3967
+ sl: c
3943
3968
  } ]
3944
3969
  }), this;
3945
3970
  }
package/index.js CHANGED
@@ -268,11 +268,14 @@ 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, tp = null, sl = null} = options
272
272
 
273
273
  validateNumber(lag, {min: 0, max: this.len, allowDecimals: false}, 'options.lag', methodName)
274
274
 
275
- this.inputParams.push({key: methodName, params: [size, {lag}]})
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
279
 
277
280
  return this
278
281
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ohlcv-indicators",
3
- "version": "3.4.8",
3
+ "version": "3.4.9",
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,7 +1,7 @@
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, tp, sl}) => {
5
5
  const { verticalOhlcv, instances } = main
6
6
  const keyName = `atr_${size}`
7
7
 
@@ -10,20 +10,35 @@ export const atr = (main, index, size, {lag}) => {
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)
13
+
14
+ const keyNames = [keyName]
14
15
 
15
- priceBased.add(keyName)
16
+ if(tp !== null)
17
+ {
18
+ keyNames.push(`${keyName}_tp`)
19
+ }
16
20
 
17
- if(lag > 0)
21
+ if(sl !== null)
18
22
  {
19
- main.lag([keyName], lag)
23
+ keyNames.push(`${keyName}_sl`)
20
24
  }
21
25
 
22
- arrayTypes[keyName] = 'Float64Array'
26
+ for(const k of keyNames)
27
+ {
28
+ priceBased.add(k)
29
+ verticalOhlcv[k] = new Float64Array(len).fill(NaN)
30
+ arrayTypes[k] = 'Float64Array'
31
+ }
32
+
33
+
34
+ if(lag > 0)
35
+ {
36
+ main.lag(keyNames, lag)
37
+ }
23
38
  }
24
39
 
25
40
  // Retrieve the current price value.
26
- const value = {
41
+ const curr = {
27
42
  high: verticalOhlcv.high[index],
28
43
  low: verticalOhlcv.low[index],
29
44
  close: verticalOhlcv.close[index],
@@ -31,16 +46,26 @@ export const atr = (main, index, size, {lag}) => {
31
46
  const instance = instances[keyName]
32
47
 
33
48
  // Update the moving average instance.
34
- instance.update(value);
35
- let currMa = NaN;
49
+ instance.update(curr);
50
+ let currAtr = NaN;
36
51
  try {
37
- currMa = instance.getResult();
52
+ currAtr = instance.getResult();
38
53
  } catch (err) {
39
54
 
40
55
  }
41
56
 
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)
63
+ {
64
+ main.pushToMain({ index, key: `${keyName}_sl`, value: (!Number.isNaN(currAtr)) ? curr.close - (currAtr*sl) : NaN });
65
+ }
66
+
42
67
  // Always push the MA value (even if NaN).
43
- main.pushToMain({ index, key: keyName, value: currMa });
68
+ main.pushToMain({ index, key: keyName, value: currAtr });
44
69
 
45
70
  return true;
46
71
  }