ohlcv-indicators 1.8.4 → 1.8.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ohlcv-indicators",
3
- "version": "1.8.4",
3
+ "version": "1.8.5",
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,16 +1,18 @@
1
1
  import {FasterSMA} from 'trading-signals'
2
2
  import { classifyChange, classifySize } from '../utilities/classification.js'
3
3
 
4
- export const candlesStudies = (main, period, classify) => {
4
+ export const candlesStudies = (main, period, classify, classificationLevels) => {
5
5
 
6
6
  const {inputOhlcv, len} = main
7
7
 
8
- const cols = getCandlesStudies(inputOhlcv, period, len, classify)
8
+ const cols = getCandlesStudies(inputOhlcv, period, len, classify, classificationLevels)
9
9
  return cols
10
10
  }
11
11
 
12
- const getCandlesStudies = (inputOhlcv, period = 20, len, classify = true) => {
12
+ const getCandlesStudies = (inputOhlcv, period = 20, len, classify = true, classificationLevels = {}) => {
13
13
 
14
+ const {changeLevel = 7, sizeLevel = 5} = classificationLevels
15
+
14
16
  //close > open
15
17
  let candle_body_size = new Array(len).fill(null) //scaled candle direction close - open
16
18
  let candle_top_size = new Array(len).fill(null) //scaled top size (0, 0.5 and 1)
@@ -64,8 +66,8 @@ const getCandlesStudies = (inputOhlcv, period = 20, len, classify = true) => {
64
66
  const shadowSize = curr.high - curr.low
65
67
 
66
68
  //sizes
67
- const topSize = (candleBodySize > 0) ? high - close : high - open
68
- const bottomSize = (candleBodySize > 0) ? open - low : close - low
69
+ const topSize = high - Math.max(open, close)
70
+ const bottomSize = Math.min(open, close) - low
69
71
  const gapSize = prev.close - curr.open
70
72
  const position = curr.close - prev.close
71
73
 
@@ -101,14 +103,14 @@ const getCandlesStudies = (inputOhlcv, period = 20, len, classify = true) => {
101
103
  }
102
104
 
103
105
  //negatives and positives
104
- candle_body_size[x] = classifyChange(candleBodySize, bodySizeMean, 0.5)
105
- candle_gap_size[x] = classifyChange(gapSize, gapMean, 0.5)
106
- candle_position[x] = classifyChange(position, positionMean, 0.5)
106
+ candle_body_size[x] = classifyChange(candleBodySize, bodySizeMean, 0.5, changeLevel)
107
+ candle_gap_size[x] = classifyChange(gapSize, gapMean, 0.5, changeLevel)
108
+ candle_position[x] = classifyChange(position, positionMean, 0.5, changeLevel)
107
109
 
108
110
  //positives only
109
- candle_top_size[x] = classifySize(topSize, topSizeMean, 0.5)
110
- candle_bottom_size[x] = classifySize(bottomSize, bottomSizeMean, 0.5)
111
- candle_shadow_size[x] = classifySize(shadowSize, shadowSizeMean, 0.5)
111
+ candle_top_size[x] = classifySize(topSize, topSizeMean, 0.5, sizeLevel)
112
+ candle_bottom_size[x] = classifySize(bottomSize, bottomSizeMean, 0.5, sizeLevel)
113
+ candle_shadow_size[x] = classifySize(shadowSize, shadowSizeMean, 0.5, sizeLevel)
112
114
  }
113
115
  else{
114
116
  //negatives and positives
@@ -1,49 +1,44 @@
1
+ export const classifySize = (value, mean, standardDeviation = 1.5, numLevels = 5) => {
2
+ if (value === null || mean === null || standardDeviation === null) return null
3
+ if (numLevels < 2) throw new Error("numLevels must be at least 2")
1
4
 
2
- export const classifySize = (value, mean, standardDeviation = 1.5) => {
5
+ // Calculate the range for each level
6
+ const step = (4 * standardDeviation) / (numLevels - 1)
7
+ const thresholds = []
8
+
9
+ for (let i = 0; i < numLevels - 1; i++) {
10
+ thresholds.push(mean - (2 * standardDeviation) + (i * step))
11
+ }
12
+
13
+ // Classify the value based on calculated thresholds
14
+ for (let i = thresholds.length - 1; i >= 0; i--) {
15
+ if (value > thresholds[i]) {
16
+ return (i + 1) / (numLevels - 1)
17
+ }
18
+ }
19
+
20
+ return 0
21
+ }
22
+
23
+
24
+ export const classifyChange = (value, mean, standardDeviation = 0.5, numLevels = 7) => {
3
25
  if (value === null || mean === null || standardDeviation === null) return null;
26
+ if (numLevels < 2) throw new Error("numLevels must be at least 2");
4
27
 
5
- const largeThreshold = mean + (2 * standardDeviation); // Large (2σ above mean)
6
- const mediumThreshold = mean + (1 * standardDeviation); // Medium (1σ above mean)
7
- const smallThreshold = mean - (1 * standardDeviation); // Small (1σ below mean)
8
- const verySmallThreshold = mean - (1.5 * standardDeviation); // Very small (1.5σ below mean)
28
+ // Calculate the range for each level
29
+ const step = (4 * standardDeviation) / (numLevels - 1);
30
+ const thresholds = [];
9
31
 
10
- if (value > largeThreshold) {
11
- return 1; // Large
12
- } else if (value > mediumThreshold) {
13
- return 0.75; // Medium
14
- } else if (value > smallThreshold) {
15
- return 0.5; // Small
16
- } else if (value > verySmallThreshold) {
17
- return 0.25; // Very small
18
- } else {
19
- return 0; // Insignificant
32
+ for (let i = 0; i < numLevels - 1; i++) {
33
+ thresholds.push(mean - (2 * standardDeviation) + (i * step));
20
34
  }
21
- };
22
35
 
23
-
24
- export const classifyChange = (value, mean, standardDeviation = 0.5) => {
25
- if (value === null || mean === null || standardDeviation === null) return null;
26
-
27
- const largePositiveThreshold = mean + (2 * standardDeviation); // Large positive
28
- const mediumPositiveThreshold = mean + (1.5 * standardDeviation); // Medium positive (1.5σ)
29
- const smallPositiveThreshold = mean + (0.5 * standardDeviation); // Small positive (0.5σ)
30
- const smallNegativeThreshold = mean - (0.5 * standardDeviation); // Small negative (-0.5σ)
31
- const mediumNegativeThreshold = mean - (1.5 * standardDeviation); // Medium negative (-1.5σ)
32
- const largeNegativeThreshold = mean - (2 * standardDeviation); // Large negative
33
-
34
- if (value >= largePositiveThreshold) {
35
- return 1; // Large positive
36
- } else if (value >= mediumPositiveThreshold) {
37
- return 0.5; // Medium positive
38
- } else if (value >= smallPositiveThreshold) {
39
- return 0.25; // Small positive
40
- } else if (value <= largeNegativeThreshold) {
41
- return -1; // Large negative
42
- } else if (value <= mediumNegativeThreshold) {
43
- return -0.5; // Medium negative
44
- } else if (value <= smallNegativeThreshold) {
45
- return -0.25; // Small negative
46
- } else {
47
- return 0; // No significant change
36
+ // Classify the value based on calculated thresholds
37
+ for (let i = thresholds.length - 1; i >= 0; i--) {
38
+ if (value > thresholds[i]) {
39
+ return (i - Math.floor((numLevels - 1) / 2)) / Math.floor((numLevels - 1) / 2);
40
+ }
48
41
  }
42
+
43
+ return -1;
49
44
  };