ohlcv-indicators 1.9.8 → 2.0.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.
- package/dist/ohlcv-indicators.min.js +1 -1
- package/index.js +114 -62
- package/package.json +1 -1
- package/src/moving-averages/bollingerBands.js +40 -31
- package/src/moving-averages/donchianChannel.js +4 -8
- package/src/moving-averages/ema.js +2 -2
- package/src/moving-averages/macd.js +4 -4
- package/src/moving-averages/relativeVolume.js +13 -8
- package/src/moving-averages/sma.js +2 -2
- package/src/oscillators/rsi.js +4 -4
- package/src/oscillators/volumeOscillator.js +3 -3
- package/src/studies/candleStudies.js +20 -20
- package/src/studies/dateTime.js +86 -0
- package/src/studies/findCrosses.js +34 -22
- package/src/studies/lag.js +31 -22
- package/src/studies/priceVariations.js +22 -0
- package/src/utilities/parsing-utilities.js +48 -215
- package/src/utilities/setIndicatorsFromInputParams.js +2 -2
- package/src/utilities/validators.js +20 -0
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
import { FasterSMA } from 'trading-signals';
|
|
2
2
|
import { classifySize } from '../utilities/classification.js';
|
|
3
3
|
|
|
4
|
-
export const
|
|
4
|
+
export const candleStudies = (main, index, size, classify = true, classificationLevels = {}) => {
|
|
5
5
|
|
|
6
6
|
const { changeLevel = 7, sizeLevel = 5 } = classificationLevels;
|
|
7
7
|
|
|
8
8
|
if(index === 0)
|
|
9
9
|
{
|
|
10
10
|
Object.assign(main.verticalOhlcv, {
|
|
11
|
-
candle_gap_size:
|
|
12
|
-
candle_body_size:
|
|
13
|
-
candle_top_size:
|
|
14
|
-
candle_bottom_size:
|
|
15
|
-
candle_shadow_size:
|
|
16
|
-
candle_close_direction:
|
|
17
|
-
candle_high_direction:
|
|
18
|
-
candle_low_direction:
|
|
19
|
-
candle_open_direction:
|
|
11
|
+
candle_gap_size: [...main.nullArray],
|
|
12
|
+
candle_body_size: [...main.nullArray],
|
|
13
|
+
candle_top_size: [...main.nullArray],
|
|
14
|
+
candle_bottom_size: [...main.nullArray],
|
|
15
|
+
candle_shadow_size: [...main.nullArray],
|
|
16
|
+
candle_close_direction: [...main.nullArray],
|
|
17
|
+
candle_high_direction: [...main.nullArray],
|
|
18
|
+
candle_low_direction: [...main.nullArray],
|
|
19
|
+
candle_open_direction: [...main.nullArray],
|
|
20
20
|
})
|
|
21
21
|
}
|
|
22
22
|
|
|
@@ -44,7 +44,7 @@ export const candlesStudies = (main, index, size, classify = true, classificatio
|
|
|
44
44
|
const prevLow = main.verticalOhlcv.low[index-1]
|
|
45
45
|
const prevClose = main.verticalOhlcv.close[index-1]
|
|
46
46
|
|
|
47
|
-
if (
|
|
47
|
+
if (index === 0) return true;
|
|
48
48
|
|
|
49
49
|
const currOpen = main.verticalOhlcv.open[index]
|
|
50
50
|
const currHigh = main.verticalOhlcv.high[index]
|
|
@@ -88,15 +88,15 @@ export const candlesStudies = (main, index, size, classify = true, classificatio
|
|
|
88
88
|
} = main.instances.candleStudies;
|
|
89
89
|
|
|
90
90
|
// Update instances with current absolute values
|
|
91
|
-
topInstance.update(Math.abs(topSize));
|
|
92
|
-
bottomInstance.update(Math.abs(bottomSize));
|
|
93
|
-
shadowSizeInstance.update(Math.abs(shadowSize));
|
|
94
|
-
gapInstance.update(gapSize);
|
|
95
|
-
bodySizeInstance.update(candleBodySize);
|
|
96
|
-
closeDirectionInstance.update(closeDirection);
|
|
97
|
-
highDirectionInstance.update(highDirection);
|
|
98
|
-
lowDirectionInstance.update(lowDirection);
|
|
99
|
-
openDirectionInstance.update(openDirection);
|
|
91
|
+
topInstance.update(Math.abs(topSize), main.lastIndexReplace);
|
|
92
|
+
bottomInstance.update(Math.abs(bottomSize), main.lastIndexReplace);
|
|
93
|
+
shadowSizeInstance.update(Math.abs(shadowSize), main.lastIndexReplace);
|
|
94
|
+
gapInstance.update(gapSize, main.lastIndexReplace);
|
|
95
|
+
bodySizeInstance.update(candleBodySize, main.lastIndexReplace);
|
|
96
|
+
closeDirectionInstance.update(closeDirection, main.lastIndexReplace);
|
|
97
|
+
highDirectionInstance.update(highDirection, main.lastIndexReplace);
|
|
98
|
+
lowDirectionInstance.update(lowDirection, main.lastIndexReplace);
|
|
99
|
+
openDirectionInstance.update(openDirection, main.lastIndexReplace);
|
|
100
100
|
|
|
101
101
|
try {
|
|
102
102
|
topSizeMean = topInstance.getResult();
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
export const dateTime = (main, index) => {
|
|
2
|
+
|
|
3
|
+
if(index === 0)
|
|
4
|
+
{
|
|
5
|
+
|
|
6
|
+
if(!main.isValidDate) throw Error('dateTime method found and invalid "date" in input ohlcv')
|
|
7
|
+
|
|
8
|
+
const index0DateStr = main.verticalOhlcv.date[0].slice(0, 10)
|
|
9
|
+
|
|
10
|
+
Object.assign(main.instances, {
|
|
11
|
+
dateTime: {
|
|
12
|
+
prevDateStr: index0DateStr,
|
|
13
|
+
sessionDailyIndexCount: 0,
|
|
14
|
+
sessionIntradayIndexCount: 0,
|
|
15
|
+
cachedDayInfo: index0DateStr
|
|
16
|
+
}
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
Object.assign(main.verticalOhlcv, {
|
|
20
|
+
day_of_the_week: [...main.nullArray],
|
|
21
|
+
day_of_the_month: [...main.nullArray],
|
|
22
|
+
week_of_the_month: [...main.nullArray],
|
|
23
|
+
hour: [...main.nullArray],
|
|
24
|
+
month: [...main.nullArray],
|
|
25
|
+
year: [...main.nullArray],
|
|
26
|
+
session_daily_index: [...main.nullArray],
|
|
27
|
+
session_intraday_index: [...main.nullArray],
|
|
28
|
+
})
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const currDate = main.verticalOhlcv.date[index]
|
|
32
|
+
const {
|
|
33
|
+
day_of_the_week,
|
|
34
|
+
day_of_the_month,
|
|
35
|
+
week_of_the_month,
|
|
36
|
+
month,
|
|
37
|
+
year,
|
|
38
|
+
hour,
|
|
39
|
+
} = getDateInfo(currDate)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
const currDateStr = currDate.slice(0, 10)
|
|
43
|
+
|
|
44
|
+
if(currDateStr !== main.instances.dateTime.prevDateStr)
|
|
45
|
+
{
|
|
46
|
+
main.instances.dateTime.prevDateStr = currDateStr
|
|
47
|
+
main.instances.dateTime.sessionDailyIndexCount++
|
|
48
|
+
main.instances.dateTime.sessionIntradayIndexCount = 0
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
main.verticalOhlcv.session_daily_index[index] = main.instances.dateTime.sessionDailyIndexCount
|
|
53
|
+
main.verticalOhlcv.session_intraday_index[index] = main.instances.dateTime.sessionIntradayIndexCount
|
|
54
|
+
main.verticalOhlcv.day_of_the_week[index] = day_of_the_week
|
|
55
|
+
main.verticalOhlcv.day_of_the_month[index] = day_of_the_month
|
|
56
|
+
main.verticalOhlcv.week_of_the_month[index] = week_of_the_month
|
|
57
|
+
main.verticalOhlcv.month[index] = month
|
|
58
|
+
main.verticalOhlcv.year[index] = year
|
|
59
|
+
main.verticalOhlcv.hour[index] = hour
|
|
60
|
+
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
const getDateInfo = dateString => {
|
|
66
|
+
const date = new Date(dateString)
|
|
67
|
+
const month = date.getMonth()
|
|
68
|
+
const year = date.getFullYear()
|
|
69
|
+
const hour = date.getHours()
|
|
70
|
+
const minute = date.getMinutes()
|
|
71
|
+
const day_of_the_week = date.getDay();
|
|
72
|
+
const day_of_the_month = date.getDate();
|
|
73
|
+
const firstDayOfMonth = new Date(date.getFullYear(), date.getMonth(), 1)
|
|
74
|
+
const dayOffset = firstDayOfMonth.getDay();
|
|
75
|
+
const week_of_the_month = Math.ceil((day_of_the_month + dayOffset) / 7)
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
month,
|
|
79
|
+
year,
|
|
80
|
+
hour,
|
|
81
|
+
minute,
|
|
82
|
+
day_of_the_week,
|
|
83
|
+
day_of_the_month,
|
|
84
|
+
week_of_the_month,
|
|
85
|
+
}
|
|
86
|
+
}
|
|
@@ -12,18 +12,20 @@ class CrossInstance {
|
|
|
12
12
|
|
|
13
13
|
constructor()
|
|
14
14
|
{
|
|
15
|
-
this
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
15
|
+
Object.assign(this, {
|
|
16
|
+
interval: 0,
|
|
17
|
+
prevState: 0.5,
|
|
18
|
+
prevFast: null,
|
|
19
|
+
prevSlow: null,
|
|
20
|
+
prevHigh: null,
|
|
21
|
+
prevLow: null,
|
|
22
|
+
areHighAndLowUndefined: false,
|
|
23
|
+
index: 0,
|
|
24
|
+
crossIndexes: {
|
|
25
|
+
up: [],
|
|
26
|
+
down: []
|
|
27
|
+
}
|
|
28
|
+
})
|
|
27
29
|
|
|
28
30
|
}
|
|
29
31
|
update({fast, high, slow, low})
|
|
@@ -113,12 +115,14 @@ class CrossInstance {
|
|
|
113
115
|
if(this.interval === -1) this.crossIndexes.down.push(this.index)
|
|
114
116
|
|
|
115
117
|
//save prev state
|
|
116
|
-
this
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
118
|
+
Object.assign(this, {
|
|
119
|
+
index: this.index + 1,
|
|
120
|
+
prevState: currState,
|
|
121
|
+
prevFast: fast,
|
|
122
|
+
prevSlow: slow,
|
|
123
|
+
prevHigh: high,
|
|
124
|
+
prevLow: low
|
|
125
|
+
})
|
|
122
126
|
}
|
|
123
127
|
|
|
124
128
|
getResult()
|
|
@@ -128,11 +132,19 @@ class CrossInstance {
|
|
|
128
132
|
|
|
129
133
|
}
|
|
130
134
|
|
|
131
|
-
export const crossPairs = (main, index
|
|
135
|
+
export const crossPairs = (main, index) => {
|
|
132
136
|
|
|
133
|
-
if(!arr) return
|
|
134
137
|
|
|
135
|
-
|
|
138
|
+
if(index === 0)
|
|
139
|
+
{
|
|
140
|
+
const findParams = main.inputParams.filter(o => o.key === 'crossPairs')
|
|
141
|
+
|
|
142
|
+
if(typeof findParams !== 'object') return
|
|
143
|
+
|
|
144
|
+
main.instances.crossPairs = {crossPairsList: findParams.flatMap(o => o.params.flat())}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
for (const { fast, slow } of main.instances.crossPairs.crossPairsList)
|
|
136
148
|
{
|
|
137
149
|
if (!fast || !slow) continue
|
|
138
150
|
|
|
@@ -150,7 +162,7 @@ export const crossPairs = (main, index, arr) => {
|
|
|
150
162
|
|
|
151
163
|
|
|
152
164
|
main.instances[crossName] = new CrossInstance()
|
|
153
|
-
main.verticalOhlcv[crossName] =
|
|
165
|
+
main.verticalOhlcv[crossName] = [...main.nullArray]
|
|
154
166
|
}
|
|
155
167
|
|
|
156
168
|
let fastValue
|
package/src/studies/lag.js
CHANGED
|
@@ -1,34 +1,43 @@
|
|
|
1
|
-
export const lag = (main, index
|
|
1
|
+
export const lag = (main, index) => {
|
|
2
2
|
|
|
3
|
-
if(
|
|
4
|
-
|
|
5
|
-
for(let p = 0; p < params.length; p++)
|
|
3
|
+
if(index === 0)
|
|
6
4
|
{
|
|
7
|
-
const
|
|
5
|
+
const findParams = main.inputParams.filter(o => o.key === 'lag')
|
|
8
6
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
7
|
+
if(typeof findParams !== 'object') return
|
|
8
|
+
|
|
9
|
+
const params = findParams.map(o => o.params)
|
|
10
|
+
|
|
11
|
+
main.instances.lag = {
|
|
12
|
+
lagParams: params
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
for (const [colKeys, lags] of main.instances.lag.lagParams) {
|
|
17
|
+
|
|
18
|
+
for (const colKey of colKeys) {
|
|
19
|
+
const currentColumn = main.verticalOhlcv[colKey];
|
|
20
|
+
|
|
21
|
+
// Initialize lagged arrays only on the first index
|
|
22
|
+
if (index === 0) {
|
|
23
|
+
for (let lag = 1; lag <= lags; lag++) {
|
|
24
|
+
const key = `${colKey}_lag_${lag}`;
|
|
25
|
+
main.verticalOhlcv[key] = [...main.nullArray];
|
|
16
26
|
}
|
|
27
|
+
}
|
|
17
28
|
|
|
18
|
-
|
|
29
|
+
// Populate lagged values
|
|
30
|
+
for (let lag = 1; lag <= lags; lag++) {
|
|
31
|
+
const key = `${colKey}_lag_${lag}`;
|
|
19
32
|
const laggedIndex = index - lag;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
: null;
|
|
25
|
-
|
|
26
|
-
main.verticalOhlcv[key][index] = value;
|
|
33
|
+
|
|
34
|
+
if(laggedIndex <= 0) break
|
|
35
|
+
|
|
36
|
+
main.verticalOhlcv[key][index] = currentColumn[laggedIndex]
|
|
27
37
|
}
|
|
28
38
|
}
|
|
29
39
|
}
|
|
30
|
-
|
|
31
|
-
|
|
40
|
+
|
|
32
41
|
|
|
33
42
|
|
|
34
43
|
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export const priceVariations = (main, index) => {
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
if(index === 0)
|
|
5
|
+
{
|
|
6
|
+
main.priceBased = [...new Set([...main.priceBased, 'mid_price_open_close', 'mid_price_high_low'])]
|
|
7
|
+
|
|
8
|
+
Object.assign(main.verticalOhlcv, {
|
|
9
|
+
mid_price_open_close: [...main.nullArray],
|
|
10
|
+
mid_price_high_low: [...main.nullArray]
|
|
11
|
+
})
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const open = main.verticalOhlcv.open[index]
|
|
15
|
+
const high = main.verticalOhlcv.high[index]
|
|
16
|
+
const low = main.verticalOhlcv.low[index]
|
|
17
|
+
const close = main.verticalOhlcv.close[index]
|
|
18
|
+
|
|
19
|
+
main.verticalOhlcv.mid_price_open_close[index] = (open + close) / 2
|
|
20
|
+
main.verticalOhlcv.mid_price_high_low[index] = (high + low) / 2
|
|
21
|
+
|
|
22
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { validateInputParams } from "./validators.js"
|
|
1
2
|
import { rsi } from "../oscillators/rsi.js"
|
|
2
3
|
import { sma } from "../moving-averages/sma.js"
|
|
3
4
|
import { ema } from "../moving-averages/ema.js"
|
|
@@ -6,11 +7,16 @@ import { relativeVolume } from "../moving-averages/relativeVolume.js"
|
|
|
6
7
|
import { donchianChannels } from "../moving-averages/donchianChannel.js"
|
|
7
8
|
import { bollingerBands } from "../moving-averages/bollingerBands.js"
|
|
8
9
|
import { volumeOscillator } from "../oscillators/volumeOscillator.js"
|
|
9
|
-
import {
|
|
10
|
+
import { candleStudies } from "../studies/candleStudies.js"
|
|
10
11
|
import { lag } from "../studies/lag.js"
|
|
11
12
|
import {crossPairs} from "../studies/findCrosses.js"
|
|
13
|
+
import { dateTime } from "../studies/dateTime.js"
|
|
14
|
+
import { priceVariations } from "../studies/priceVariations.js"
|
|
15
|
+
|
|
12
16
|
|
|
13
17
|
const indicatorFunctions = {
|
|
18
|
+
dateTime,
|
|
19
|
+
priceVariations,
|
|
14
20
|
rsi,
|
|
15
21
|
sma,
|
|
16
22
|
ema,
|
|
@@ -19,259 +25,86 @@ const indicatorFunctions = {
|
|
|
19
25
|
donchianChannels,
|
|
20
26
|
bollingerBands,
|
|
21
27
|
volumeOscillator,
|
|
22
|
-
|
|
23
|
-
lag,
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const validateFirstDate = arr => arr[0].hasOwnProperty('date') && typeof arr[0].date === 'string' && /^\d{4}-\d{2}-\d{2}/.test(arr[0].date)
|
|
27
|
-
|
|
28
|
-
export const defaultStudyOptions = {
|
|
29
|
-
midPriceOpenClose: true,
|
|
30
|
-
midPriceHighLow: true,
|
|
31
|
-
sessionIndex: true,
|
|
32
|
-
sessionIntradayIndex: true,
|
|
33
|
-
dayOfTheWeek: true,
|
|
34
|
-
dayOfTheMonth: true,
|
|
35
|
-
weekOfTheMonth: true
|
|
28
|
+
candleStudies
|
|
36
29
|
}
|
|
37
30
|
|
|
38
|
-
const getDateInfo = (dateString) => {
|
|
39
|
-
const date = new Date(dateString);
|
|
40
|
-
const day_of_the_week = date.getDay();
|
|
41
|
-
const day_of_the_month = date.getDate();
|
|
42
|
-
const firstDayOfMonth = new Date(date.getFullYear(), date.getMonth(), 1);
|
|
43
|
-
const dayOffset = firstDayOfMonth.getDay();
|
|
44
|
-
const week_of_the_month = Math.ceil((day_of_the_month + dayOffset) / 7);
|
|
45
|
-
|
|
46
|
-
return {
|
|
47
|
-
day_of_the_week,
|
|
48
|
-
day_of_the_month,
|
|
49
|
-
week_of_the_month
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
31
|
|
|
53
32
|
export const parseOhlcvToVertical = (input, main, startIndex = 0) => {
|
|
54
|
-
const { len,
|
|
55
|
-
|
|
33
|
+
const { len, inputParams } = main
|
|
56
34
|
|
|
35
|
+
main.nullArray = new Array(len).fill(null)
|
|
57
36
|
|
|
37
|
+
if (startIndex === 0) {
|
|
58
38
|
|
|
59
|
-
|
|
60
|
-
const studyOptionKeys = Object.keys(studyOptions)
|
|
61
|
-
for (let i = 0; i < studyOptionKeys.length; i++) {
|
|
62
|
-
const k = studyOptionKeys[i]
|
|
63
|
-
if (!defaultStudyOptions.hasOwnProperty(k)) {
|
|
64
|
-
throw new Error(`Key ${k} not found in defaultStudyOptions. Available options: ${Object.keys(defaultStudyOptions).join(', ')}`)
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const {
|
|
70
|
-
midPriceOpenClose,
|
|
71
|
-
midPriceHighLow,
|
|
72
|
-
sessionIndex,
|
|
73
|
-
sessionIntradayIndex,
|
|
74
|
-
dayOfTheWeek,
|
|
75
|
-
dayOfTheMonth,
|
|
76
|
-
weekOfTheMonth
|
|
77
|
-
} = studyOptions
|
|
78
|
-
|
|
79
|
-
const numberColsKeys = ['open', 'high', 'low', 'close', 'volume']
|
|
80
|
-
if (midPriceOpenClose) numberColsKeys.push('mid_price_open_close')
|
|
81
|
-
if (midPriceHighLow) numberColsKeys.push('mid_price_high_low')
|
|
39
|
+
validateInputParams(main)
|
|
82
40
|
|
|
83
|
-
|
|
84
|
-
|
|
41
|
+
const baseKeys = ['open', 'high', 'low', 'close', 'volume'];
|
|
42
|
+
const baseKeysSet = new Set(baseKeys);
|
|
85
43
|
|
|
86
|
-
let isValidDate = false
|
|
87
|
-
if (startIndex === 0) {
|
|
88
44
|
// Fresh initialization
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
vert[numberColsKeys[i]] = new Array(len)
|
|
45
|
+
for (let i = 0; i < baseKeys.length; i++) {
|
|
46
|
+
main.verticalOhlcv[baseKeys[i]] = [...main.nullArray]
|
|
92
47
|
}
|
|
93
48
|
|
|
94
|
-
|
|
95
|
-
const
|
|
96
|
-
const otherKeys = []
|
|
49
|
+
const inputKeys = Object.keys(input[0]);
|
|
50
|
+
const otherKeys = [];
|
|
97
51
|
for (let i = 0; i < inputKeys.length; i++) {
|
|
98
52
|
const key = inputKeys[i]
|
|
99
|
-
if (!
|
|
100
|
-
|
|
53
|
+
if (!baseKeysSet.has(key)) {
|
|
54
|
+
main.verticalOhlcv[key] = [...main.nullArray]
|
|
101
55
|
otherKeys.push(key)
|
|
102
56
|
}
|
|
103
57
|
}
|
|
104
|
-
main.otherKeys = otherKeys
|
|
58
|
+
main.otherKeys = otherKeys;
|
|
105
59
|
|
|
106
|
-
isValidDate = validateFirstDate(input)
|
|
107
60
|
|
|
108
|
-
if (isValidDate) {
|
|
109
|
-
if (sessionIndex) {
|
|
110
|
-
vert['session_index'] = new Array(len)
|
|
111
|
-
}
|
|
112
|
-
if (sessionIntradayIndex) {
|
|
113
|
-
vert['session_intraday_index'] = new Array(len)
|
|
114
|
-
}
|
|
115
|
-
if (dayOfTheWeek) {
|
|
116
|
-
vert['day_of_the_week'] = new Array(len)
|
|
117
|
-
}
|
|
118
|
-
if (dayOfTheMonth) {
|
|
119
|
-
vert['day_of_the_month'] = new Array(len)
|
|
120
|
-
}
|
|
121
|
-
if (weekOfTheMonth) {
|
|
122
|
-
vert['week_of_the_month'] = new Array(len)
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
main.prevDateStr = input[0].date.slice(0, 10)
|
|
126
|
-
main.sessionIndexCount = 0
|
|
127
|
-
main.sessionIntradayIndexCount = 0
|
|
128
|
-
main.cachedDayInfo = getDateInfo(main.prevDateStr)
|
|
129
|
-
} else {
|
|
130
|
-
// If no valid date, ensure these are null
|
|
131
|
-
main.prevDateStr = null
|
|
132
|
-
main.sessionIndexCount = null
|
|
133
|
-
main.sessionIntradayIndexCount = null
|
|
134
|
-
main.cachedDayInfo = null
|
|
135
|
-
}
|
|
136
61
|
} else {
|
|
137
|
-
// startIndex > 0
|
|
138
|
-
// We assume arrays and keys are already set
|
|
139
62
|
if (!main.otherKeys) {
|
|
140
|
-
throw new Error("otherKeys not found in main. Ensure you run with startIndex=0 first to initialize.")
|
|
63
|
+
throw new Error("otherKeys not found in main. Ensure you run with startIndex=0 first to initialize.");
|
|
141
64
|
}
|
|
142
|
-
|
|
143
|
-
// Validate that state variables needed are present
|
|
144
|
-
if (!main.hasOwnProperty('prevDateStr')) {
|
|
145
|
-
throw new Error("prevDateStr missing in main. Make sure to store state after initial run.")
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
// If we had a valid date initially, we continue with date computations
|
|
149
|
-
isValidDate = main.prevDateStr !== null
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
// Pre-process indicator params
|
|
153
|
-
const indicatorCalls = processIndicatorCalls(inputParams)
|
|
154
|
-
|
|
155
|
-
// Store references for numeric arrays to avoid repeated dictionary lookups
|
|
156
|
-
const vo_open = vert.open
|
|
157
|
-
const vo_high = vert.high
|
|
158
|
-
const vo_low = vert.low
|
|
159
|
-
const vo_close = vert.close
|
|
160
|
-
const vo_volume = vert.volume
|
|
161
|
-
const vo_mid_oc = midPriceOpenClose ? vert.mid_price_open_close : null
|
|
162
|
-
const vo_mid_hl = midPriceHighLow ? vert.mid_price_high_low : null
|
|
163
|
-
|
|
164
|
-
// References for other keys arrays
|
|
165
|
-
const otherArrays = {}
|
|
166
|
-
for (let i = 0; i < main.otherKeys.length; i++) {
|
|
167
|
-
const k = main.otherKeys[i]
|
|
168
|
-
otherArrays[k] = vert[k]
|
|
169
65
|
}
|
|
170
66
|
|
|
171
|
-
|
|
172
|
-
let prevDateStr = main.prevDateStr
|
|
173
|
-
let sessionIndexCount = main.sessionIndexCount
|
|
174
|
-
let sessionIntradayIndexCount = main.sessionIntradayIndexCount
|
|
175
|
-
let cachedDayInfo = main.cachedDayInfo
|
|
176
|
-
|
|
67
|
+
const indicatorCalls = processIndicatorCalls(inputParams);
|
|
177
68
|
|
|
178
69
|
|
|
179
|
-
|
|
180
|
-
// Compute indicators and fill data from startIndex to len
|
|
181
70
|
for (let x = startIndex; x < len; x++) {
|
|
182
|
-
const current = input[x]
|
|
71
|
+
const current = input[x];
|
|
183
72
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
vo_volume[x] = current.volume
|
|
73
|
+
main.verticalOhlcv.open[x] = current.open;
|
|
74
|
+
main.verticalOhlcv.high[x] = current.high;
|
|
75
|
+
main.verticalOhlcv.low[x] = current.low;
|
|
76
|
+
main.verticalOhlcv.close[x] = current.close;
|
|
77
|
+
main.verticalOhlcv.volume[x] = current.volume;
|
|
190
78
|
|
|
191
|
-
// Mid-price calculations
|
|
192
|
-
if (vo_mid_oc) {
|
|
193
|
-
vo_mid_oc[x] = (current.open + current.close) / 2
|
|
194
|
-
}
|
|
195
|
-
if (vo_mid_hl) {
|
|
196
|
-
vo_mid_hl[x] = (current.high + current.low) / 2
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
// Other keys
|
|
200
79
|
for (let i = 0; i < main.otherKeys.length; i++) {
|
|
201
|
-
const k = main.otherKeys[i]
|
|
202
|
-
|
|
80
|
+
const k = main.otherKeys[i];
|
|
81
|
+
main.verticalOhlcv[k][x] = current[k];
|
|
203
82
|
}
|
|
204
83
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
if (thisDateStr !== prevDateStr) {
|
|
209
|
-
prevDateStr = thisDateStr
|
|
210
|
-
sessionIndexCount++
|
|
211
|
-
sessionIntradayIndexCount = 0
|
|
212
|
-
cachedDayInfo = getDateInfo(thisDateStr)
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
if (dayOfTheWeek) {
|
|
216
|
-
vert.day_of_the_week[x] = cachedDayInfo.day_of_the_week
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
if (dayOfTheMonth) {
|
|
220
|
-
vert.day_of_the_month[x] = cachedDayInfo.day_of_the_month
|
|
221
|
-
}
|
|
84
|
+
for (let i = 0; i < indicatorCalls.length; i++) {
|
|
85
|
+
const { fn, args, key } = indicatorCalls[i];
|
|
86
|
+
if (key === 'crossPairs' || key === 'lag') continue;
|
|
222
87
|
|
|
223
|
-
|
|
224
|
-
vert.week_of_the_month[x] = cachedDayInfo.week_of_the_month
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
if (sessionIndex) {
|
|
228
|
-
vert.session_index[x] = sessionIndexCount
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
if (sessionIntradayIndex) {
|
|
232
|
-
vert.session_intraday_index[x] = sessionIntradayIndexCount
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
sessionIntradayIndexCount++
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
// Run indicator functions
|
|
239
|
-
for (let i = 0; i < indicatorCalls.length; i++) {
|
|
240
|
-
const { fn, args, key } = indicatorCalls[i]
|
|
241
|
-
if(key === 'crossPairs' || key === 'lag') continue
|
|
242
|
-
fn(main, x, ...args)
|
|
243
|
-
}
|
|
88
|
+
fn(main, x, ...args)
|
|
244
89
|
}
|
|
245
90
|
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
main.lastComputedIndex++
|
|
91
|
+
lag(main, x);
|
|
92
|
+
crossPairs(main, x);
|
|
93
|
+
main.lastComputedIndex++;
|
|
250
94
|
}
|
|
95
|
+
};
|
|
251
96
|
|
|
252
|
-
// Store current state so that we can continue from here in subsequent calls
|
|
253
|
-
main.prevDateStr = prevDateStr
|
|
254
|
-
main.sessionIndexCount = sessionIndexCount
|
|
255
|
-
main.sessionIntradayIndexCount = sessionIntradayIndexCount
|
|
256
|
-
main.cachedDayInfo = cachedDayInfo
|
|
257
|
-
}
|
|
258
97
|
|
|
259
|
-
const processIndicatorCalls =
|
|
260
|
-
if (!inputParams || typeof inputParams !== 'object') return []
|
|
98
|
+
const processIndicatorCalls = inputParams => {
|
|
99
|
+
if (!inputParams || typeof inputParams !== 'object') return []
|
|
261
100
|
|
|
262
|
-
const indicatorCalls = []
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
key,
|
|
269
|
-
fn: indicatorFunctions[key],
|
|
270
|
-
args: params,
|
|
271
|
-
});
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
}
|
|
101
|
+
const indicatorCalls = []
|
|
102
|
+
|
|
103
|
+
for(let x = 0; x < inputParams.length; x++)
|
|
104
|
+
{
|
|
105
|
+
const {key, params} = inputParams[x]
|
|
106
|
+
indicatorCalls.push({key, fn: indicatorFunctions[key], args: params})
|
|
275
107
|
}
|
|
108
|
+
|
|
276
109
|
return indicatorCalls;
|
|
277
110
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export const setIndicatorsFromInputParams = ({ input, inputParams, OHLCV_INDICATORS
|
|
2
|
-
const indicators = new OHLCV_INDICATORS({ input
|
|
1
|
+
export const setIndicatorsFromInputParams = ({ input, inputParams, OHLCV_INDICATORS }) => {
|
|
2
|
+
const indicators = new OHLCV_INDICATORS({ input });
|
|
3
3
|
|
|
4
4
|
for (const [key, technical] of Object.entries(inputParams)) {
|
|
5
5
|
|