ohlcv-indicators 3.4.6 → 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/index.js CHANGED
@@ -15,7 +15,7 @@ import { pushToMain } from './src/core-functions/pushToMain.js'
15
15
  import { assignTypes } from './src/utilities/assignTypes.js'
16
16
  import { calcPrecisionMultiplier } from './src/utilities/precisionMultiplier.js'
17
17
  import { buildArray } from './src/utilities/assignTypes.js'
18
-
18
+ import { dateOutputFormaters } from './src/utilities/dateUtilities.js'
19
19
 
20
20
  //precomputed
21
21
  import { precomputeMovingAverages } from './src/moving-averages/movingAverages.js'
@@ -86,8 +86,13 @@ export default class OHLCV_INDICATORS {
86
86
  }
87
87
 
88
88
 
89
- getDataAsCols(skipNull = true) {
89
+ getDataAsCols(options = {}) {
90
90
  this.compute();
91
+
92
+ const {skipNull = true, dateFormat = 'string'} = options
93
+
94
+ validateArrayOptions(Object.keys(dateOutputFormaters), dateFormat, 'dateFormat', 'getDataAsCols')
95
+ validateObject(options, 'options', 'getDataAsCols')
91
96
 
92
97
  const {
93
98
  precisionMultiplier,
@@ -112,9 +117,17 @@ export default class OHLCV_INDICATORS {
112
117
 
113
118
  for (let x = startIndex; x < len; x++)
114
119
  {
115
- result[key][x] = (shouldApplyPrecision)
116
- ? arr[x] / precisionMultiplier
117
- : arr[x]
120
+ if(shouldApplyPrecision)
121
+ {
122
+ result[key][x] = arr[x] / precisionMultiplier
123
+ }
124
+ else if(key === 'date')
125
+ {
126
+ result[key][x] = dateOutputFormaters[dateFormat](arr[x])
127
+ }
128
+ else{
129
+ result[key][x] = arr[x]
130
+ }
118
131
  }
119
132
 
120
133
  }
@@ -123,20 +136,38 @@ export default class OHLCV_INDICATORS {
123
136
  }
124
137
 
125
138
 
126
- getData(skipNull = true) {
139
+ getData(options = {}) {
127
140
 
128
141
  //getData method returns the last object (row) of the new OHLCV with indicators: {open, high, low, close, rsi_14, bollinger_bands_upper}
129
142
  this.compute()
130
143
 
131
- return verticalToHorizontal(skipNull, this, 0)
144
+ const {skipNull = true, dateFormat = 'string'} = options
145
+
146
+ validateArrayOptions(Object.keys(dateOutputFormaters), dateFormat, 'dateFormat', 'getData')
147
+ validateObject(options, 'options', 'getData')
148
+
149
+ return verticalToHorizontal({
150
+ main: this,
151
+ skipNull,
152
+ startIndex: 0,
153
+ dateFormat
154
+ })
132
155
  }
133
156
 
134
157
 
135
- getLastValues(){
158
+ getLastValues(options = {}){
136
159
 
137
160
  this.compute()
138
161
 
139
- return verticalToHorizontal(false, this, this.len - 1)[0]
162
+ const {dateFormat = 'string'} = options
163
+ validateArrayOptions(Object.keys(dateOutputFormaters), dateFormat, 'dateFormat', 'getData')
164
+
165
+ return verticalToHorizontal({
166
+ skipNull: false,
167
+ main: this,
168
+ startIndex: this.len - 1,
169
+ dateFormat
170
+ })[0]
140
171
  }
141
172
 
142
173
  compute() {
@@ -228,6 +259,24 @@ export default class OHLCV_INDICATORS {
228
259
  return this
229
260
  }
230
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
+
231
280
  ema(size = 5, options = {}) {
232
281
 
233
282
  const methodName = 'ema'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ohlcv-indicators",
3
- "version": "3.4.6",
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 = {ema: FasterEMA, sma: FasterSMA}
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 { maInstance } = instances[keyName]
24
+ const instance = instances[keyName]
22
25
 
23
26
  // Update the moving average instance.
24
- maInstance.update(value);
27
+ instance.update(value);
25
28
  let currMa = NaN;
26
29
  try {
27
- currMa = maInstance.getResult();
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
 
@@ -1,3 +1,10 @@
1
+ export const dateOutputFormaters = {
2
+ string: d => d.toISOString(),
3
+ milliseconds: d => d.getTime(),
4
+ seconds: d => d.getTime() / 1000,
5
+ object: d => d
6
+ }
7
+
1
8
  export const dateFormaters = {
2
9
  dateObject: d => d,
3
10
  dateMilliseconds: d => new Date(d),
@@ -1,8 +1,6 @@
1
- export const verticalToHorizontal = (
2
- skipNull = false,
3
- main,
4
- startIndex = 0
5
- ) => {
1
+ import { dateOutputFormaters } from "./dateUtilities.js"
2
+
3
+ export const verticalToHorizontal = ({main, skipNull = false, startIndex = 0, dateFormat}) => {
6
4
 
7
5
  const {precisionMultiplier, priceBased, precision, verticalOhlcv, invalidValueIndex, len, verticalOhlcvKeyNames, verticalOhlcvTempCols} = main
8
6
 
@@ -19,9 +17,19 @@ export const verticalToHorizontal = (
19
17
 
20
18
  for (let i = maxStartIndex; i < len; i++)
21
19
  {
22
- result[i - maxStartIndex][key] = (shouldApplyPrecision)
23
- ? arr[i] / precisionMultiplier
24
- : arr[i]
20
+ if(shouldApplyPrecision)
21
+ {
22
+ result[i - maxStartIndex][key] = arr[i] / precisionMultiplier
23
+
24
+ }
25
+ else if(key === 'date')
26
+ {
27
+ result[i - maxStartIndex][key] = dateOutputFormaters[dateFormat](arr[i])
28
+ }
29
+ else
30
+ {
31
+ result[i - maxStartIndex][key] = arr[i]
32
+ }
25
33
  }
26
34
  }
27
35
 
@@ -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
+ }