trading-signals 7.2.0 → 7.4.3

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/index.js DELETED
@@ -1,1809 +0,0 @@
1
- // src/error/NotEnoughDataError.ts
2
- var NotEnoughDataError = class extends Error {
3
- constructor(requiredAmount) {
4
- super(`Not enough data. A minimum of "${requiredAmount}" inputs is required to perform the computation.`);
5
- Object.setPrototypeOf(this, new.target.prototype);
6
- this.name = "NotEnoughDataError";
7
- }
8
- };
9
-
10
- // src/types/Indicator.ts
11
- var TradingSignal = {
12
- BEARISH: "BEARISH",
13
- BULLISH: "BULLISH",
14
- SIDEWAYS: "SIDEWAYS",
15
- UNKNOWN: "UNKNOWN"
16
- };
17
- var TechnicalIndicator = class {
18
- result;
19
- getResult() {
20
- try {
21
- return this.getResultOrThrow();
22
- } catch {
23
- return null;
24
- }
25
- }
26
- getResultOrThrow() {
27
- if (this.result === void 0) {
28
- throw new NotEnoughDataError(this.getRequiredInputs());
29
- }
30
- return this.result;
31
- }
32
- get isStable() {
33
- return this.result !== void 0;
34
- }
35
- add(input) {
36
- return this.update(input, false);
37
- }
38
- replace(input) {
39
- return this.update(input, true);
40
- }
41
- updates(inputs, replace = false) {
42
- return inputs.map((input) => this.update(input, replace));
43
- }
44
- };
45
- var IndicatorSeries = class extends TechnicalIndicator {
46
- previousResult;
47
- setResult(value, replace) {
48
- if (replace) {
49
- this.result = this.previousResult;
50
- }
51
- this.previousResult = this.result;
52
- return this.result = value;
53
- }
54
- };
55
- var TrendIndicatorSeries = class extends IndicatorSeries {
56
- previousSignalState;
57
- setResult(value, replace) {
58
- if (replace && this.previousResult !== void 0) {
59
- this.previousSignalState = this.calculateSignalState(this.previousResult);
60
- } else if (!replace) {
61
- this.previousSignalState = this.calculateSignalState(this.result);
62
- }
63
- return super.setResult(value, replace);
64
- }
65
- getSignal() {
66
- const currentState = this.calculateSignalState(this.getResult());
67
- const hasChanged = this.previousSignalState !== void 0 && this.previousSignalState !== currentState;
68
- return {
69
- hasChanged,
70
- state: currentState
71
- };
72
- }
73
- };
74
-
75
- // src/trend/MA/MovingAverage.ts
76
- var MovingAverage = class extends IndicatorSeries {
77
- constructor(interval) {
78
- super();
79
- this.interval = interval;
80
- }
81
- };
82
-
83
- // src/util/getAverage.ts
84
- function getAverage(values) {
85
- return values.length ? values.reduce((sum, x) => sum + x, 0) / values.length : 0;
86
- }
87
-
88
- // src/util/pushUpdate.ts
89
- function pushUpdate(array, replace, item, maxLength) {
90
- if (array.length > 0 && replace === true) {
91
- array[array.length - 1] = item;
92
- } else {
93
- array.push(item);
94
- }
95
- if (array.length > maxLength) {
96
- return array.shift();
97
- }
98
- return null;
99
- }
100
-
101
- // src/trend/SMA/SMA.ts
102
- var SMA = class extends MovingAverage {
103
- prices = [];
104
- getRequiredInputs() {
105
- return this.interval;
106
- }
107
- update(price, replace) {
108
- pushUpdate(this.prices, replace, price, this.interval);
109
- if (this.prices.length === this.interval) {
110
- return this.setResult(getAverage(this.prices), replace);
111
- }
112
- return null;
113
- }
114
- };
115
-
116
- // src/momentum/AO/AO.ts
117
- var AO = class extends TrendIndicatorSeries {
118
- constructor(shortInterval, longInterval, SmoothingIndicator = SMA) {
119
- super();
120
- this.shortInterval = shortInterval;
121
- this.longInterval = longInterval;
122
- this.short = new SmoothingIndicator(shortInterval);
123
- this.long = new SmoothingIndicator(longInterval);
124
- }
125
- long;
126
- short;
127
- getRequiredInputs() {
128
- return this.long.getRequiredInputs();
129
- }
130
- update({ low, high }, replace) {
131
- const medianPrice = (low + high) / 2;
132
- this.short.update(medianPrice, replace);
133
- this.long.update(medianPrice, replace);
134
- if (this.long.isStable) {
135
- return this.setResult(this.short.getResultOrThrow() - this.long.getResultOrThrow(), replace);
136
- }
137
- return null;
138
- }
139
- calculateSignalState(result) {
140
- const hasResult = result !== null && result !== void 0;
141
- const isBullish = hasResult && result > 0;
142
- const isBearish = hasResult && result < 0;
143
- switch (true) {
144
- case !hasResult:
145
- return TradingSignal.UNKNOWN;
146
- case isBullish:
147
- return TradingSignal.BULLISH;
148
- case isBearish:
149
- return TradingSignal.BEARISH;
150
- default:
151
- return TradingSignal.SIDEWAYS;
152
- }
153
- }
154
- };
155
-
156
- // src/momentum/MOM/MOM.ts
157
- var MOM = class extends TrendIndicatorSeries {
158
- constructor(interval) {
159
- super();
160
- this.interval = interval;
161
- this.historyLength = interval + 1;
162
- this.history = [];
163
- }
164
- history;
165
- historyLength;
166
- getRequiredInputs() {
167
- return this.historyLength;
168
- }
169
- update(value, replace) {
170
- pushUpdate(this.history, replace, value, this.historyLength);
171
- if (this.history.length === this.historyLength) {
172
- return this.setResult(value - this.history[0], replace);
173
- }
174
- return null;
175
- }
176
- calculateSignalState(result) {
177
- const hasResult = result !== null && result !== void 0;
178
- if (!hasResult) {
179
- return TradingSignal.UNKNOWN;
180
- }
181
- if (result > 0) {
182
- return TradingSignal.BULLISH;
183
- }
184
- if (result < 0) {
185
- return TradingSignal.BEARISH;
186
- }
187
- return TradingSignal.SIDEWAYS;
188
- }
189
- };
190
-
191
- // src/momentum/AC/AC.ts
192
- var AC = class extends TrendIndicatorSeries {
193
- constructor(shortAO, longAO, signalInterval) {
194
- super();
195
- this.shortAO = shortAO;
196
- this.longAO = longAO;
197
- this.signalInterval = signalInterval;
198
- this.ao = new AO(shortAO, longAO);
199
- this.momentum = new MOM(1);
200
- this.signal = new SMA(signalInterval);
201
- }
202
- ao;
203
- momentum;
204
- signal;
205
- getRequiredInputs() {
206
- return this.ao.getRequiredInputs() + this.signal.getRequiredInputs();
207
- }
208
- update(input, replace) {
209
- const ao = this.ao.update(input, replace);
210
- if (ao) {
211
- this.signal.update(ao, replace);
212
- if (this.signal.isStable) {
213
- const result = this.setResult(ao - this.signal.getResultOrThrow(), replace);
214
- this.momentum.update(result, replace);
215
- return result;
216
- }
217
- }
218
- return null;
219
- }
220
- calculateSignalState(result) {
221
- const hasResult = result !== null && result !== void 0;
222
- const isPositive = hasResult && result > 0;
223
- switch (true) {
224
- case !hasResult:
225
- return TradingSignal.UNKNOWN;
226
- case isPositive:
227
- return TradingSignal.BULLISH;
228
- default:
229
- return TradingSignal.BEARISH;
230
- }
231
- }
232
- };
233
-
234
- // src/util/getGrid.ts
235
- function getGrid({ lower, upper, levels, tickSize, spacing }) {
236
- const prices = [];
237
- if (spacing === "arithmetic") {
238
- const step = (upper - lower) / (levels - 1);
239
- for (let i = 0; i < levels; i++) {
240
- prices.push(lower + step * i);
241
- }
242
- } else {
243
- const ratio = Math.pow(upper / lower, 1 / (levels - 1));
244
- for (let i = 0; i < levels; i++) {
245
- prices.push(lower * Math.pow(ratio, i));
246
- }
247
- }
248
- if (tickSize) {
249
- const roundToTick = (x) => Math.round(x / tickSize) * tickSize;
250
- return prices.map(roundToTick);
251
- }
252
- return prices;
253
- }
254
-
255
- // src/util/getMaximum.ts
256
- function getMaximum(values) {
257
- let max = Number.MIN_SAFE_INTEGER;
258
- for (const value of values) {
259
- if (max < value) {
260
- max = value;
261
- }
262
- }
263
- return max;
264
- }
265
-
266
- // src/util/getMedian.ts
267
- function getMedian(values) {
268
- const n = values.length;
269
- if (n === 0) {
270
- throw new Error("Cannot calculate median of empty array");
271
- }
272
- if (n % 2 === 0) {
273
- return (values[n / 2 - 1] + values[n / 2]) / 2;
274
- }
275
- return values[Math.floor(n / 2)];
276
- }
277
-
278
- // src/util/getMinimum.ts
279
- function getMinimum(values) {
280
- let min = Number.MAX_SAFE_INTEGER;
281
- for (const value of values) {
282
- if (min > value) {
283
- min = value;
284
- }
285
- }
286
- return min;
287
- }
288
-
289
- // src/util/getQuartile.ts
290
- function getQuartile(values, q) {
291
- const sorted = [...values].sort((a, b) => a - b);
292
- const n = sorted.length;
293
- const medianIndex = Math.floor(n / 2);
294
- if (q === 0.25) {
295
- return getMedian(sorted.slice(0, medianIndex));
296
- } else if (q === 0.75) {
297
- if (n % 2 === 0) {
298
- return getMedian(sorted.slice(medianIndex));
299
- }
300
- return getMedian(sorted.slice(medianIndex + 1));
301
- }
302
- const pos = (sorted.length - 1) * q;
303
- const base = Math.floor(pos);
304
- const rest = pos - base;
305
- return sorted[base] + (sorted[base + 1] - sorted[base]) * rest;
306
- }
307
-
308
- // src/util/getStandardDeviation.ts
309
- function getStandardDeviation(values, average) {
310
- const middle = average || getAverage(values);
311
- const squaredDifferences = values.map((value) => value - middle).map((value) => value * value);
312
- const averageDifference = getAverage(squaredDifferences);
313
- return Math.sqrt(averageDifference);
314
- }
315
-
316
- // src/util/getStreaks.ts
317
- function getStreaks(prices, keepSide) {
318
- const streaks = [];
319
- let currentStreak = 0;
320
- function saveStreak(i) {
321
- const endPrice = prices[i - 1];
322
- const startPrice = prices[i - currentStreak - 1];
323
- const percentage = (endPrice - startPrice) / startPrice * 100;
324
- streaks.push({ length: currentStreak, percentage });
325
- }
326
- for (let i = 1; i < prices.length; i++) {
327
- const isUpward = keepSide === "up" && prices[i] > prices[i - 1];
328
- const isDownward = keepSide === "down" && prices[i] < prices[i - 1];
329
- if (isUpward || isDownward) {
330
- currentStreak++;
331
- } else {
332
- if (currentStreak > 0) {
333
- saveStreak(i);
334
- }
335
- currentStreak = 0;
336
- }
337
- }
338
- if (currentStreak > 0) {
339
- saveStreak(prices.length);
340
- }
341
- return streaks;
342
- }
343
-
344
- // src/util/getWeekday.ts
345
- function getWeekday(timezone, date = new Date(Date.now())) {
346
- return new Intl.DateTimeFormat("en-US", {
347
- timeZone: timezone,
348
- weekday: "long"
349
- }).format(date);
350
- }
351
- function isMonday(timezone, date = new Date(Date.now())) {
352
- return getWeekday(timezone, date) === "Monday";
353
- }
354
- function isTuesday(timezone, date = new Date(Date.now())) {
355
- return getWeekday(timezone, date) === "Tuesday";
356
- }
357
- function isWednesday(timezone, date = new Date(Date.now())) {
358
- return getWeekday(timezone, date) === "Wednesday";
359
- }
360
- function isThursday(timezone, date = new Date(Date.now())) {
361
- return getWeekday(timezone, date) === "Thursday";
362
- }
363
- function isFriday(timezone, date = new Date(Date.now())) {
364
- return getWeekday(timezone, date) === "Friday";
365
- }
366
- function isSaturday(timezone, date = new Date(Date.now())) {
367
- return getWeekday(timezone, date) === "Saturday";
368
- }
369
- function isSunday(timezone, date = new Date(Date.now())) {
370
- return getWeekday(timezone, date) === "Sunday";
371
- }
372
-
373
- // src/volatility/MAD/MAD.ts
374
- var MAD = class extends IndicatorSeries {
375
- constructor(interval) {
376
- super();
377
- this.interval = interval;
378
- }
379
- prices = [];
380
- getRequiredInputs() {
381
- return this.interval;
382
- }
383
- update(price, replace) {
384
- pushUpdate(this.prices, replace, price, this.interval);
385
- if (this.prices.length === this.interval) {
386
- const mean = getAverage(this.prices);
387
- let sum = 0;
388
- for (let i = 0; i < this.interval; i++) {
389
- const deviation = Math.abs(this.prices[i] - mean);
390
- sum += deviation;
391
- }
392
- return this.setResult(sum / this.interval, replace);
393
- }
394
- return null;
395
- }
396
- static getResultFromBatch(prices, average) {
397
- if (prices.length === 0) {
398
- return 0;
399
- }
400
- const mean = average || getAverage(prices);
401
- let sum = 0;
402
- for (let i = 0; i < prices.length; i++) {
403
- const deviation = Math.abs(prices[i] - mean);
404
- sum += deviation;
405
- }
406
- return sum / prices.length;
407
- }
408
- };
409
-
410
- // src/momentum/CCI/CCI.ts
411
- var CCI = class extends TrendIndicatorSeries {
412
- constructor(interval) {
413
- super();
414
- this.interval = interval;
415
- this.sma = new SMA(this.interval);
416
- this.typicalPrices = [];
417
- }
418
- sma;
419
- typicalPrices;
420
- getRequiredInputs() {
421
- return this.sma.getRequiredInputs();
422
- }
423
- update(candle, replace) {
424
- const typicalPrice = this.cacheTypicalPrice(candle, replace);
425
- this.sma.update(typicalPrice, replace);
426
- if (this.sma.isStable) {
427
- const mean = this.sma.getResultOrThrow();
428
- const meanDeviation = MAD.getResultFromBatch(this.typicalPrices, mean);
429
- const numerator = typicalPrice - mean;
430
- const denominator = 0.015 * meanDeviation;
431
- return this.setResult(numerator / denominator, replace);
432
- }
433
- return null;
434
- }
435
- cacheTypicalPrice({ high, low, close }, replace) {
436
- const typicalPrice = (high + low + close) / 3;
437
- pushUpdate(this.typicalPrices, replace, typicalPrice, this.interval);
438
- return typicalPrice;
439
- }
440
- calculateSignalState(result) {
441
- const hasResult = result !== null && result !== void 0;
442
- const isOversold = hasResult && result <= -100;
443
- const isOverbought = hasResult && result >= 100;
444
- switch (true) {
445
- case !hasResult:
446
- return TradingSignal.UNKNOWN;
447
- case isOversold:
448
- return TradingSignal.BEARISH;
449
- case isOverbought:
450
- return TradingSignal.BULLISH;
451
- default:
452
- return TradingSignal.SIDEWAYS;
453
- }
454
- }
455
- };
456
-
457
- // src/momentum/CG/CG.ts
458
- var CG = class extends TrendIndicatorSeries {
459
- constructor(interval, signalInterval) {
460
- super();
461
- this.interval = interval;
462
- this.signalInterval = signalInterval;
463
- this.signal = new SMA(signalInterval);
464
- }
465
- signal;
466
- prices = [];
467
- get isStable() {
468
- return this.signal.isStable;
469
- }
470
- getRequiredInputs() {
471
- return this.signal.getRequiredInputs();
472
- }
473
- calculateSignalState(result) {
474
- const hasResult = result !== null && result !== void 0;
475
- const signalValue = this.signal.getResult();
476
- const hasSignal = signalValue !== null;
477
- const isAboveSignal = hasResult && hasSignal && result > signalValue;
478
- const isBelowSignal = hasResult && hasSignal && result < signalValue;
479
- switch (true) {
480
- case (!hasResult || !hasSignal):
481
- return TradingSignal.UNKNOWN;
482
- case isAboveSignal:
483
- return TradingSignal.BULLISH;
484
- case isBelowSignal:
485
- return TradingSignal.BEARISH;
486
- default:
487
- return TradingSignal.SIDEWAYS;
488
- }
489
- }
490
- update(price, replace) {
491
- pushUpdate(this.prices, replace, price, this.interval);
492
- let nominator = 0;
493
- let denominator = 0;
494
- for (let i = 0; i < this.prices.length; ++i) {
495
- const price2 = this.prices[i];
496
- nominator = nominator + price2 * (i + 1);
497
- denominator = denominator + price2;
498
- }
499
- const cg = denominator > 0 ? nominator / denominator : 0;
500
- this.signal.update(cg, replace);
501
- if (this.signal.isStable) {
502
- return this.setResult(cg, replace);
503
- }
504
- return null;
505
- }
506
- };
507
-
508
- // src/momentum/MACD/MACD.ts
509
- var MACD = class extends TechnicalIndicator {
510
- constructor(short, long, signal) {
511
- super();
512
- this.short = short;
513
- this.long = long;
514
- this.signal = signal;
515
- }
516
- prices = [];
517
- previousResult;
518
- getRequiredInputs() {
519
- return this.long.getRequiredInputs();
520
- }
521
- update(price, replace) {
522
- pushUpdate(this.prices, replace, price, this.long.interval);
523
- const short = this.short.update(price, replace);
524
- const long = this.long.update(price, replace);
525
- if (this.prices.length === this.long.interval) {
526
- const macd = short - long;
527
- const signal = this.signal.update(macd, replace);
528
- if (replace) {
529
- this.result = this.previousResult;
530
- }
531
- this.previousResult = this.result;
532
- return this.result = {
533
- histogram: macd - signal,
534
- macd,
535
- signal
536
- };
537
- }
538
- return null;
539
- }
540
- calculateSignal(result) {
541
- const hasResult = result !== null && result !== void 0;
542
- const isBullish = hasResult && result.histogram > 0;
543
- const isBearish = hasResult && result.histogram < 0;
544
- switch (true) {
545
- case !hasResult:
546
- return TradingSignal.UNKNOWN;
547
- case isBullish:
548
- return TradingSignal.BULLISH;
549
- case isBearish:
550
- default:
551
- return TradingSignal.BEARISH;
552
- }
553
- }
554
- getSignal() {
555
- const previousState = this.calculateSignal(this.previousResult);
556
- const state = this.calculateSignal(this.getResult());
557
- const hasChanged = previousState !== state;
558
- return {
559
- hasChanged,
560
- state
561
- };
562
- }
563
- };
564
-
565
- // src/momentum/OBV/OBV.ts
566
- var OBV = class extends TrendIndicatorSeries {
567
- constructor(interval) {
568
- super();
569
- this.interval = interval;
570
- }
571
- candles = [];
572
- getRequiredInputs() {
573
- return this.interval;
574
- }
575
- update(candle, replace) {
576
- pushUpdate(this.candles, replace, candle, this.getRequiredInputs());
577
- if (this.candles.length < this.getRequiredInputs()) {
578
- return null;
579
- }
580
- const prevCandle = this.candles[this.candles.length - 2];
581
- const prevPrice = prevCandle.close;
582
- const prevResult = this.result ?? 0;
583
- const currentPrice = candle.close;
584
- const nextResult = currentPrice > prevPrice ? candle.volume : currentPrice < prevPrice ? -candle.volume : 0;
585
- return this.setResult(prevResult + nextResult, false);
586
- }
587
- calculateSignalState(result) {
588
- const hasResult = result !== null && result !== void 0;
589
- const previousResult = this.previousResult;
590
- const hasPreviousResult = previousResult !== void 0;
591
- const isBullish = hasResult && hasPreviousResult && result > previousResult;
592
- const isBearish = hasResult && hasPreviousResult && result < previousResult;
593
- switch (true) {
594
- case !hasResult:
595
- return TradingSignal.UNKNOWN;
596
- case isBullish:
597
- return TradingSignal.BULLISH;
598
- case isBearish:
599
- return TradingSignal.BEARISH;
600
- default:
601
- return TradingSignal.SIDEWAYS;
602
- }
603
- }
604
- };
605
-
606
- // src/momentum/REI/REI.ts
607
- var REI = class extends TrendIndicatorSeries {
608
- constructor(interval) {
609
- super();
610
- this.interval = interval;
611
- }
612
- highs = [];
613
- lows = [];
614
- closes = [];
615
- calculateSignalState(result) {
616
- const hasResult = result !== null && result !== void 0;
617
- const isOverbought = hasResult && result > 60;
618
- const isOversold = hasResult && result < -60;
619
- switch (true) {
620
- case !hasResult:
621
- return TradingSignal.UNKNOWN;
622
- case isOverbought:
623
- return TradingSignal.BULLISH;
624
- case isOversold:
625
- return TradingSignal.BEARISH;
626
- default:
627
- return TradingSignal.SIDEWAYS;
628
- }
629
- }
630
- getRequiredInputs() {
631
- return this.interval + 8;
632
- }
633
- calculateN(j) {
634
- if (this.highs[j - 2] < this.closes[j - 7] && this.highs[j - 2] < this.closes[j - 8] && this.highs[j] < this.highs[j - 5] && this.highs[j] < this.highs[j - 6]) {
635
- return 0;
636
- }
637
- return 1;
638
- }
639
- calculateM(j) {
640
- if (this.lows[j - 2] > this.closes[j - 7] && this.lows[j - 2] > this.closes[j - 8] && this.lows[j] > this.lows[j - 5] && this.lows[j] > this.lows[j - 6]) {
641
- return 0;
642
- }
643
- return 1;
644
- }
645
- update(candle, replace) {
646
- if (replace) {
647
- this.highs.pop();
648
- this.lows.pop();
649
- this.closes.pop();
650
- }
651
- this.highs.push(candle.high);
652
- this.lows.push(candle.low);
653
- this.closes.push(candle.close);
654
- if (this.highs.length < this.getRequiredInputs()) {
655
- return null;
656
- }
657
- let subValueSum = 0;
658
- let absValueSum = 0;
659
- const limitIndex = this.highs.length - 1;
660
- for (let j = limitIndex; j > this.interval; j--) {
661
- const diffHighs = this.highs[j] - this.highs[j - 2];
662
- const diffLows = this.lows[j] - this.lows[j - 2];
663
- const n = this.calculateN(j);
664
- const m = this.calculateM(j);
665
- const s = diffHighs + diffLows;
666
- const subValue = n * m * s;
667
- const absDailyValue = Math.abs(diffHighs) + Math.abs(diffLows);
668
- subValueSum += subValue;
669
- absValueSum += absDailyValue;
670
- }
671
- if (absValueSum === 0) {
672
- return this.setResult(0, replace);
673
- }
674
- const rei = subValueSum / absValueSum * 100;
675
- return this.setResult(rei, replace);
676
- }
677
- };
678
-
679
- // src/momentum/ROC/ROC.ts
680
- var ROC = class extends TrendIndicatorSeries {
681
- constructor(interval) {
682
- super();
683
- this.interval = interval;
684
- }
685
- prices = [];
686
- getRequiredInputs() {
687
- return this.interval;
688
- }
689
- update(price, replace) {
690
- const comparePrice = pushUpdate(this.prices, replace, price, this.interval);
691
- if (comparePrice) {
692
- return this.setResult((price - comparePrice) / comparePrice, replace);
693
- }
694
- return null;
695
- }
696
- calculateSignalState(result) {
697
- const hasResult = result !== null && result !== void 0;
698
- const isBearish = hasResult && result < 0;
699
- switch (true) {
700
- case !hasResult:
701
- return TradingSignal.UNKNOWN;
702
- case isBearish:
703
- return TradingSignal.BEARISH;
704
- default:
705
- return TradingSignal.BULLISH;
706
- }
707
- }
708
- };
709
-
710
- // src/trend/WSMA/WSMA.ts
711
- var WSMA = class extends IndicatorSeries {
712
- constructor(interval) {
713
- super();
714
- this.interval = interval;
715
- this.indicator = new SMA(interval);
716
- this.smoothingFactor = 1 / this.interval;
717
- }
718
- indicator;
719
- smoothingFactor;
720
- getRequiredInputs() {
721
- return this.interval;
722
- }
723
- update(price, replace) {
724
- const sma = this.indicator.update(price, replace);
725
- if (replace && this.previousResult !== void 0) {
726
- const smoothed = (price - this.previousResult) * this.smoothingFactor;
727
- return this.setResult(smoothed + this.previousResult, replace);
728
- } else if (!replace && this.result !== void 0) {
729
- const smoothed = (price - this.result) * this.smoothingFactor;
730
- return this.setResult(smoothed + this.result, replace);
731
- } else if (this.result === void 0 && sma !== null) {
732
- return this.setResult(sma, replace);
733
- }
734
- return null;
735
- }
736
- };
737
-
738
- // src/momentum/RSI/RSI.ts
739
- var RSI = class extends TrendIndicatorSeries {
740
- constructor(interval, SmoothingIndicator = WSMA) {
741
- super();
742
- this.interval = interval;
743
- this.avgGain = new SmoothingIndicator(this.interval);
744
- this.avgLoss = new SmoothingIndicator(this.interval);
745
- }
746
- previousPrices = [];
747
- avgGain;
748
- avgLoss;
749
- maxValue = 100;
750
- getRequiredInputs() {
751
- return this.avgGain.getRequiredInputs();
752
- }
753
- update(price, replace) {
754
- pushUpdate(this.previousPrices, replace, price, this.interval);
755
- if (this.previousPrices.length < 2) {
756
- return null;
757
- }
758
- const currentPrice = price;
759
- const previousPrice = this.previousPrices[this.previousPrices.length - 2];
760
- if (currentPrice > previousPrice) {
761
- this.avgLoss.update(0, replace);
762
- this.avgGain.update(price - previousPrice, replace);
763
- } else {
764
- this.avgLoss.update(previousPrice - currentPrice, replace);
765
- this.avgGain.update(0, replace);
766
- }
767
- if (this.avgGain.isStable) {
768
- const avgLoss = this.avgLoss.getResultOrThrow();
769
- if (avgLoss === 0) {
770
- return this.setResult(100, replace);
771
- }
772
- const relativeStrength = this.avgGain.getResultOrThrow() / avgLoss;
773
- return this.setResult(this.maxValue - this.maxValue / (relativeStrength + 1), replace);
774
- }
775
- return null;
776
- }
777
- calculateSignalState(result) {
778
- const hasResult = result !== null && result !== void 0;
779
- const isOversold = hasResult && result <= 30;
780
- const isOverbought = hasResult && result >= 70;
781
- switch (true) {
782
- case !hasResult:
783
- return TradingSignal.UNKNOWN;
784
- case isOversold:
785
- return TradingSignal.BEARISH;
786
- case isOverbought:
787
- return TradingSignal.BULLISH;
788
- default:
789
- return TradingSignal.SIDEWAYS;
790
- }
791
- }
792
- };
793
-
794
- // src/momentum/STOCH/StochasticOscillator.ts
795
- var StochasticOscillator = class extends TechnicalIndicator {
796
- /**
797
- * @param n The %k period
798
- * @param m The %k slowing period
799
- * @param p The %d period
800
- */
801
- constructor(n, m, p) {
802
- super();
803
- this.n = n;
804
- this.m = m;
805
- this.p = p;
806
- this.periodM = new SMA(m);
807
- this.periodP = new SMA(p);
808
- }
809
- candles = [];
810
- periodM;
811
- periodP;
812
- previousResult;
813
- getRequiredInputs() {
814
- return this.n + this.p + 1;
815
- }
816
- update(candle, replace) {
817
- pushUpdate(this.candles, replace, candle, this.n);
818
- if (this.candles.length === this.n) {
819
- const highest = Math.max(...this.candles.map((candle2) => candle2.high));
820
- const lowest = Math.min(...this.candles.map((candle2) => candle2.low));
821
- const divisor = highest - lowest;
822
- let fastK = (candle.close - lowest) * 100;
823
- fastK = fastK / (divisor === 0 ? 1 : divisor);
824
- const stochK = this.periodM.update(fastK, replace);
825
- const stochD = stochK && this.periodP.update(stochK, replace);
826
- if (stochK !== null && stochD !== null) {
827
- if (replace) {
828
- this.result = this.previousResult;
829
- }
830
- this.previousResult = this.result;
831
- return this.result = {
832
- stochD,
833
- stochK
834
- };
835
- }
836
- }
837
- return null;
838
- }
839
- calculateSignal(result) {
840
- const hasResult = result !== null && result !== void 0;
841
- const isOversold = hasResult && result.stochK <= 20;
842
- const isOverbought = hasResult && result.stochK >= 80;
843
- switch (true) {
844
- case !hasResult:
845
- return TradingSignal.UNKNOWN;
846
- case isOversold:
847
- return TradingSignal.BEARISH;
848
- case isOverbought:
849
- return TradingSignal.BULLISH;
850
- default:
851
- return TradingSignal.SIDEWAYS;
852
- }
853
- }
854
- getSignal() {
855
- const previousState = this.calculateSignal(this.previousResult);
856
- const state = this.calculateSignal(this.getResult());
857
- const hasChanged = previousState !== state;
858
- return {
859
- hasChanged,
860
- state
861
- };
862
- }
863
- };
864
-
865
- // src/types/Period.ts
866
- var Period = class extends TechnicalIndicator {
867
- constructor(interval) {
868
- super();
869
- this.interval = interval;
870
- this.values = [];
871
- }
872
- values;
873
- /** Highest return value during the current period. */
874
- _highest;
875
- /** Lowest return value during the current period. */
876
- _lowest;
877
- get highest() {
878
- return this._highest;
879
- }
880
- get lowest() {
881
- return this._lowest;
882
- }
883
- getRequiredInputs() {
884
- return this.interval;
885
- }
886
- update(value, replace) {
887
- pushUpdate(this.values, replace, value, this.interval);
888
- if (this.values.length === this.interval) {
889
- this._lowest = Math.min(...this.values);
890
- this._highest = Math.max(...this.values);
891
- return this.result = {
892
- highest: this._highest,
893
- lowest: this._lowest
894
- };
895
- }
896
- return null;
897
- }
898
- };
899
-
900
- // src/momentum/STOCHRSI/StochasticRSI.ts
901
- var StochasticRSI = class extends TrendIndicatorSeries {
902
- constructor(interval, SmoothingRSI = WSMA, smoothing = {
903
- d: new SMA(3),
904
- k: new SMA(3)
905
- }) {
906
- super();
907
- this.interval = interval;
908
- this.smoothing = smoothing;
909
- this.period = new Period(interval);
910
- this.rsi = new RSI(interval, SmoothingRSI);
911
- }
912
- period;
913
- rsi;
914
- getRequiredInputs() {
915
- return this.rsi.getRequiredInputs() + this.period.getRequiredInputs();
916
- }
917
- update(price, replace) {
918
- const rsiResult = this.rsi.update(price, replace);
919
- if (rsiResult) {
920
- const periodResult = this.period.update(rsiResult, replace);
921
- if (periodResult) {
922
- const min = periodResult.lowest;
923
- const max = periodResult.highest;
924
- const denominator = max - min;
925
- if (denominator === 0) {
926
- return this.setResult(100, replace);
927
- }
928
- const numerator = rsiResult - min;
929
- const stochRSI = numerator / denominator;
930
- const k = this.smoothing.k.update(stochRSI, replace);
931
- if (k) {
932
- this.smoothing.d.update(k, replace);
933
- }
934
- return this.setResult(stochRSI, replace);
935
- }
936
- }
937
- return null;
938
- }
939
- calculateSignalState(result) {
940
- const hasResult = result !== null && result !== void 0;
941
- const isOversold = hasResult && result <= 0.2;
942
- const isOverbought = hasResult && result >= 0.8;
943
- switch (true) {
944
- case !hasResult:
945
- return TradingSignal.UNKNOWN;
946
- case isOversold:
947
- return TradingSignal.BEARISH;
948
- case isOverbought:
949
- return TradingSignal.BULLISH;
950
- default:
951
- return TradingSignal.SIDEWAYS;
952
- }
953
- }
954
- };
955
-
956
- // src/momentum/TDS/TDS.ts
957
- var TDS = class extends TrendIndicatorSeries {
958
- closes = [];
959
- setupCount = 0;
960
- setupDirection = null;
961
- getRequiredInputs() {
962
- return 9;
963
- }
964
- update(close, replace) {
965
- if (replace) {
966
- this.closes.pop();
967
- }
968
- this.closes.push(close);
969
- if (this.closes.length < 5) {
970
- return null;
971
- }
972
- if (this.closes.length > 13) {
973
- this.closes.shift();
974
- }
975
- const index = this.closes.length - 1;
976
- const prev4 = this.closes[index - 4];
977
- if (close > prev4) {
978
- if (this.setupDirection === "bearish") {
979
- this.setupCount = 1;
980
- this.setupDirection = "bullish";
981
- } else {
982
- this.setupCount++;
983
- this.setupDirection = "bullish";
984
- }
985
- } else if (close < prev4) {
986
- if (this.setupDirection === "bullish") {
987
- this.setupCount = 1;
988
- this.setupDirection = "bearish";
989
- } else {
990
- this.setupCount++;
991
- this.setupDirection = "bearish";
992
- }
993
- }
994
- if (this.setupCount >= this.getRequiredInputs()) {
995
- const result = this.setupDirection === "bullish" ? 1 : -1;
996
- this.setupCount = 0;
997
- this.setupDirection = null;
998
- return this.setResult(result, replace);
999
- }
1000
- return null;
1001
- }
1002
- calculateSignalState(result) {
1003
- const hasResult = result !== null && result !== void 0;
1004
- const isOverbought = hasResult && result === 1;
1005
- switch (true) {
1006
- case !hasResult:
1007
- return TradingSignal.UNKNOWN;
1008
- case isOverbought:
1009
- return TradingSignal.BULLISH;
1010
- default:
1011
- return TradingSignal.BEARISH;
1012
- }
1013
- }
1014
- };
1015
-
1016
- // src/momentum/WILLR/WilliamsR.ts
1017
- var WilliamsR = class extends TrendIndicatorSeries {
1018
- constructor(interval) {
1019
- super();
1020
- this.interval = interval;
1021
- }
1022
- candles = [];
1023
- getRequiredInputs() {
1024
- return this.interval;
1025
- }
1026
- update(candle, replace) {
1027
- pushUpdate(this.candles, replace, candle, this.interval);
1028
- if (this.candles.length === this.interval) {
1029
- let highest = this.candles[0].high;
1030
- let lowest = this.candles[0].low;
1031
- for (let i = 1; i < this.candles.length; i++) {
1032
- if (this.candles[i].high > highest) {
1033
- highest = this.candles[i].high;
1034
- }
1035
- if (this.candles[i].low < lowest) {
1036
- lowest = this.candles[i].low;
1037
- }
1038
- }
1039
- const divisor = highest - lowest;
1040
- if (divisor === 0) {
1041
- return this.setResult(-100, replace);
1042
- }
1043
- const willR = (highest - candle.close) / divisor * -100;
1044
- return this.setResult(willR, replace);
1045
- }
1046
- return null;
1047
- }
1048
- calculateSignalState(result) {
1049
- const hasResult = result !== null && result !== void 0;
1050
- const isOverbought = hasResult && result >= -20;
1051
- const isOversold = hasResult && result <= -80;
1052
- switch (true) {
1053
- case !hasResult:
1054
- return TradingSignal.UNKNOWN;
1055
- case isOverbought:
1056
- return TradingSignal.BULLISH;
1057
- case isOversold:
1058
- return TradingSignal.BEARISH;
1059
- default:
1060
- return TradingSignal.SIDEWAYS;
1061
- }
1062
- }
1063
- };
1064
-
1065
- // src/volatility/TR/TR.ts
1066
- var TR = class extends IndicatorSeries {
1067
- previousCandle;
1068
- twoPreviousCandle;
1069
- getRequiredInputs() {
1070
- return 2;
1071
- }
1072
- update(candle, replace) {
1073
- const { high, low } = candle;
1074
- const highLow = high - low;
1075
- if (this.previousCandle && replace) {
1076
- this.previousCandle = this.twoPreviousCandle;
1077
- }
1078
- if (this.previousCandle) {
1079
- const highClose = Math.abs(high - this.previousCandle.close);
1080
- const lowClose = Math.abs(low - this.previousCandle.close);
1081
- this.twoPreviousCandle = this.previousCandle;
1082
- this.previousCandle = candle;
1083
- return this.setResult(Math.max(highLow, highClose, lowClose), replace);
1084
- }
1085
- this.twoPreviousCandle = this.previousCandle;
1086
- this.previousCandle = candle;
1087
- return this.setResult(highLow, replace);
1088
- }
1089
- };
1090
-
1091
- // src/volatility/ATR/ATR.ts
1092
- var ATR = class extends IndicatorSeries {
1093
- constructor(interval, SmoothingIndicator = WSMA) {
1094
- super();
1095
- this.interval = interval;
1096
- this.tr = new TR();
1097
- this.smoothing = new SmoothingIndicator(interval);
1098
- }
1099
- tr;
1100
- smoothing;
1101
- getRequiredInputs() {
1102
- return this.smoothing.getRequiredInputs();
1103
- }
1104
- update(candle, replace) {
1105
- const trueRange = this.tr.update(candle, replace);
1106
- this.smoothing.update(trueRange, replace);
1107
- if (this.smoothing.isStable) {
1108
- return this.setResult(this.smoothing.getResultOrThrow(), replace);
1109
- }
1110
- return null;
1111
- }
1112
- };
1113
-
1114
- // src/trend/DX/DX.ts
1115
- var DX = class extends IndicatorSeries {
1116
- constructor(interval, SmoothingIndicator = WSMA) {
1117
- super();
1118
- this.interval = interval;
1119
- this.atr = new ATR(this.interval, SmoothingIndicator);
1120
- this.movesDown = new SmoothingIndicator(this.interval);
1121
- this.movesUp = new SmoothingIndicator(this.interval);
1122
- }
1123
- movesUp;
1124
- movesDown;
1125
- previousCandle;
1126
- secondLastCandle;
1127
- atr;
1128
- mdi;
1129
- pdi;
1130
- updateState(candle, pdm, mdm, replace) {
1131
- this.atr.update(candle, replace);
1132
- this.movesUp.update(pdm, replace);
1133
- this.movesDown.update(mdm, replace);
1134
- if (this.previousCandle) {
1135
- this.secondLastCandle = this.previousCandle;
1136
- }
1137
- this.previousCandle = candle;
1138
- }
1139
- getRequiredInputs() {
1140
- return this.movesUp.getRequiredInputs();
1141
- }
1142
- update(candle, replace) {
1143
- if (!this.previousCandle) {
1144
- this.updateState(candle, 0, 0, replace);
1145
- return null;
1146
- }
1147
- if (this.secondLastCandle && replace) {
1148
- this.previousCandle = this.secondLastCandle;
1149
- }
1150
- const currentHigh = candle.high;
1151
- const previousHigh = this.previousCandle.high;
1152
- const currentLow = candle.low;
1153
- const previousLow = this.previousCandle.low;
1154
- const higherHigh = currentHigh - previousHigh;
1155
- const lowerLow = previousLow - currentLow;
1156
- const noHigherHighs = higherHigh < 0;
1157
- const lowsRise = higherHigh < lowerLow;
1158
- const pdm = noHigherHighs || lowsRise ? 0 : higherHigh;
1159
- const noLowerLows = lowerLow < 0;
1160
- const highsRise = lowerLow < higherHigh;
1161
- const mdm = noLowerLows || highsRise ? 0 : lowerLow;
1162
- this.updateState(candle, pdm, mdm, replace);
1163
- if (this.movesUp.isStable) {
1164
- this.pdi = this.movesUp.getResultOrThrow() / this.atr.getResultOrThrow();
1165
- this.mdi = this.movesDown.getResultOrThrow() / this.atr.getResultOrThrow();
1166
- const dmDiff = Math.abs(this.pdi - this.mdi);
1167
- const dmSum = this.pdi + this.mdi;
1168
- if (dmSum === 0) {
1169
- return this.setResult(0, replace);
1170
- }
1171
- return this.setResult(dmDiff / dmSum * 100, replace);
1172
- }
1173
- return null;
1174
- }
1175
- };
1176
-
1177
- // src/trend/ADX/ADX.ts
1178
- var ADX = class extends IndicatorSeries {
1179
- constructor(interval, SmoothingIndicator = WSMA) {
1180
- super();
1181
- this.interval = interval;
1182
- this.smoothed = new SmoothingIndicator(this.interval);
1183
- this.dx = new DX(interval, SmoothingIndicator);
1184
- }
1185
- dx;
1186
- smoothed;
1187
- get mdi() {
1188
- return this.dx.mdi;
1189
- }
1190
- get pdi() {
1191
- return this.dx.pdi;
1192
- }
1193
- getRequiredInputs() {
1194
- return this.interval * 2 - 1;
1195
- }
1196
- update(candle, replace) {
1197
- const result = this.dx.update(candle, replace);
1198
- if (result !== null) {
1199
- this.smoothed.update(result, replace);
1200
- }
1201
- if (this.smoothed.isStable) {
1202
- return this.setResult(this.smoothed.getResultOrThrow(), replace);
1203
- }
1204
- return null;
1205
- }
1206
- };
1207
-
1208
- // src/trend/EMA/EMA.ts
1209
- var EMA = class extends MovingAverage {
1210
- constructor(interval) {
1211
- super(interval);
1212
- this.interval = interval;
1213
- this.weightFactor = 2 / (this.interval + 1);
1214
- }
1215
- pricesCounter = 0;
1216
- weightFactor;
1217
- getRequiredInputs() {
1218
- return this.interval;
1219
- }
1220
- update(price, replace) {
1221
- if (!replace) {
1222
- this.pricesCounter++;
1223
- } else if (replace && this.pricesCounter === 0) {
1224
- this.pricesCounter++;
1225
- }
1226
- if (replace && this.previousResult !== void 0) {
1227
- return this.setResult(price * this.weightFactor + this.previousResult * (1 - this.weightFactor), replace);
1228
- }
1229
- return this.setResult(
1230
- price * this.weightFactor + (this.result !== void 0 ? this.result : price) * (1 - this.weightFactor),
1231
- replace
1232
- );
1233
- }
1234
- getResultOrThrow() {
1235
- if (this.pricesCounter < this.interval) {
1236
- throw new NotEnoughDataError(this.getRequiredInputs());
1237
- }
1238
- return this.result;
1239
- }
1240
- get isStable() {
1241
- try {
1242
- this.getResultOrThrow();
1243
- return true;
1244
- } catch {
1245
- return false;
1246
- }
1247
- }
1248
- };
1249
-
1250
- // src/trend/DEMA/DEMA.ts
1251
- var DEMA = class extends IndicatorSeries {
1252
- constructor(interval) {
1253
- super();
1254
- this.interval = interval;
1255
- this.inner = new EMA(interval);
1256
- this.outer = new EMA(interval);
1257
- }
1258
- inner;
1259
- outer;
1260
- getRequiredInputs() {
1261
- return this.outer.getRequiredInputs();
1262
- }
1263
- update(price, replace) {
1264
- const innerResult = this.inner.update(price, replace);
1265
- const outerResult = this.outer.update(innerResult, replace);
1266
- return this.setResult(innerResult * 2 - outerResult, replace);
1267
- }
1268
- get isStable() {
1269
- return this.outer.isStable;
1270
- }
1271
- };
1272
-
1273
- // src/trend/DMA/DMA.ts
1274
- var DMA = class extends TechnicalIndicator {
1275
- short;
1276
- long;
1277
- constructor(short, long, SmoothingIndicator = SMA) {
1278
- super();
1279
- this.short = new SmoothingIndicator(short);
1280
- this.long = new SmoothingIndicator(long);
1281
- }
1282
- get isStable() {
1283
- return this.long.isStable;
1284
- }
1285
- getRequiredInputs() {
1286
- return this.long.getRequiredInputs();
1287
- }
1288
- update(price, replace) {
1289
- this.short.update(price, replace);
1290
- this.long.update(price, replace);
1291
- if (this.isStable) {
1292
- return this.result = {
1293
- long: this.long.getResultOrThrow(),
1294
- short: this.short.getResultOrThrow()
1295
- };
1296
- }
1297
- return null;
1298
- }
1299
- };
1300
-
1301
- // src/trend/LINREG/LinearRegression.ts
1302
- var LinearRegression = class extends TechnicalIndicator {
1303
- constructor(interval) {
1304
- super();
1305
- this.interval = interval;
1306
- }
1307
- prices = [];
1308
- getRequiredInputs() {
1309
- return this.interval;
1310
- }
1311
- calculateRegression(prices) {
1312
- const n = prices.length;
1313
- const isPerfectLinearTrend = prices.every((price, i) => {
1314
- if (i === 0) {
1315
- return true;
1316
- }
1317
- return Math.abs(price - prices[i - 1] - 1) < 1e-10;
1318
- });
1319
- if (isPerfectLinearTrend) {
1320
- const slope2 = 1;
1321
- const intercept2 = prices[0] - slope2;
1322
- const nextX = n;
1323
- const prediction2 = slope2 * nextX + intercept2;
1324
- return { intercept: intercept2, prediction: prediction2, slope: slope2 };
1325
- }
1326
- const sumX = (n - 1) * n / 2;
1327
- const sumY = prices.reduce((a, b) => a + b, 0);
1328
- let sumXY = 0;
1329
- let sumXX = 0;
1330
- for (let i = 0; i < n; i++) {
1331
- sumXY += i * prices[i];
1332
- sumXX += i * i;
1333
- }
1334
- const slope = (n * sumXY - sumX * sumY) / (n * sumXX - sumX * sumX);
1335
- const intercept = (sumY - slope * sumX) / n;
1336
- const prediction = slope * n + intercept;
1337
- return {
1338
- intercept,
1339
- prediction,
1340
- slope
1341
- };
1342
- }
1343
- update(price, replace) {
1344
- pushUpdate(this.prices, replace, price, this.interval);
1345
- if (this.prices.length < this.interval) {
1346
- return null;
1347
- }
1348
- return this.result = this.calculateRegression(this.prices);
1349
- }
1350
- get isStable() {
1351
- try {
1352
- this.getResultOrThrow();
1353
- return true;
1354
- } catch {
1355
- return false;
1356
- }
1357
- }
1358
- };
1359
-
1360
- // src/trend/PSAR/PSAR.ts
1361
- var PSAR = class extends IndicatorSeries {
1362
- accelerationStep;
1363
- accelerationMax;
1364
- acceleration = 0;
1365
- extreme = null;
1366
- lastSar = null;
1367
- isLong = null;
1368
- previousCandle = null;
1369
- prePreviousCandle = null;
1370
- constructor(config) {
1371
- super();
1372
- this.accelerationStep = config.accelerationStep;
1373
- this.accelerationMax = config.accelerationMax;
1374
- if (this.accelerationStep <= 0) {
1375
- throw new Error("Acceleration step must be greater than 0");
1376
- }
1377
- if (this.accelerationMax <= this.accelerationStep) {
1378
- throw new Error("Acceleration max must be greater than acceleration step");
1379
- }
1380
- }
1381
- get isStable() {
1382
- return this.lastSar !== null;
1383
- }
1384
- getRequiredInputs() {
1385
- return 2;
1386
- }
1387
- update(candle, replace) {
1388
- const { high, low } = candle;
1389
- const notEnoughData = !this.previousCandle || this.lastSar === null;
1390
- if (replace && notEnoughData) {
1391
- this.previousCandle = candle;
1392
- return null;
1393
- }
1394
- if (!this.previousCandle) {
1395
- this.previousCandle = candle;
1396
- return null;
1397
- }
1398
- if (this.lastSar === null) {
1399
- const currentMidpoint = (high + low) / 2;
1400
- const previousMidpoint = (this.previousCandle.high + this.previousCandle.low) / 2;
1401
- this.isLong = currentMidpoint >= previousMidpoint;
1402
- if (this.isLong) {
1403
- this.extreme = high;
1404
- this.lastSar = this.previousCandle.low;
1405
- } else {
1406
- this.extreme = low;
1407
- this.lastSar = this.previousCandle.high;
1408
- }
1409
- this.acceleration = this.accelerationStep;
1410
- this.prePreviousCandle = this.previousCandle;
1411
- this.previousCandle = candle;
1412
- return this.setResult(this.lastSar, replace);
1413
- }
1414
- let sar = (this.extreme - this.lastSar) * this.acceleration + this.lastSar;
1415
- if (this.isLong) {
1416
- const hasPrevPrev = this.prePreviousCandle != null;
1417
- if (hasPrevPrev && low < sar) {
1418
- if (this.prePreviousCandle.low < sar) {
1419
- sar = this.prePreviousCandle.low;
1420
- }
1421
- sar = this.previousCandle.low < sar ? this.previousCandle.low : sar;
1422
- } else if (this.previousCandle.low < sar) {
1423
- sar = this.previousCandle.low;
1424
- }
1425
- if (high > this.extreme) {
1426
- this.extreme = high;
1427
- if (this.acceleration < this.accelerationMax) {
1428
- this.acceleration += this.accelerationStep;
1429
- if (this.acceleration > this.accelerationMax) {
1430
- this.acceleration = this.accelerationMax;
1431
- }
1432
- }
1433
- }
1434
- if (low < sar) {
1435
- this.isLong = false;
1436
- sar = this.extreme;
1437
- this.extreme = low;
1438
- this.acceleration = this.accelerationStep;
1439
- }
1440
- } else {
1441
- const hasPrevPrev = this.prePreviousCandle != null;
1442
- if (hasPrevPrev && high > sar) {
1443
- if (this.prePreviousCandle.high > sar) {
1444
- sar = this.prePreviousCandle.high;
1445
- }
1446
- sar = this.previousCandle.high > sar ? this.previousCandle.high : sar;
1447
- } else if (this.previousCandle.high > sar) {
1448
- sar = this.previousCandle.high;
1449
- }
1450
- if (low < this.extreme) {
1451
- this.extreme = low;
1452
- this.acceleration += this.accelerationStep;
1453
- if (this.acceleration > this.accelerationMax) {
1454
- this.acceleration = this.accelerationMax;
1455
- }
1456
- }
1457
- if (high > sar) {
1458
- this.isLong = true;
1459
- sar = this.extreme;
1460
- this.extreme = high;
1461
- this.acceleration = this.accelerationStep;
1462
- if (sar >= low) {
1463
- sar = low - 0.01;
1464
- }
1465
- }
1466
- }
1467
- this.lastSar = sar;
1468
- this.prePreviousCandle = this.previousCandle;
1469
- this.previousCandle = candle;
1470
- return this.setResult(sar, replace);
1471
- }
1472
- getResultOrThrow() {
1473
- return super.getResultOrThrow();
1474
- }
1475
- };
1476
-
1477
- // src/trend/RMA/RMA.ts
1478
- var RMA = class extends MovingAverage {
1479
- constructor(interval) {
1480
- super(interval);
1481
- this.interval = interval;
1482
- this.weightFactor = 1 / this.interval;
1483
- }
1484
- pricesCounter = 0;
1485
- weightFactor;
1486
- getRequiredInputs() {
1487
- return this.interval;
1488
- }
1489
- update(price, replace) {
1490
- if (!replace) {
1491
- this.pricesCounter++;
1492
- } else if (replace && this.pricesCounter === 0) {
1493
- this.pricesCounter++;
1494
- }
1495
- if (replace && this.previousResult !== void 0) {
1496
- return this.setResult(price * this.weightFactor + this.previousResult * (1 - this.weightFactor), replace);
1497
- }
1498
- return this.setResult(
1499
- price * this.weightFactor + (this.result !== void 0 ? this.result : price) * (1 - this.weightFactor),
1500
- replace
1501
- );
1502
- }
1503
- getResultOrThrow() {
1504
- if (this.pricesCounter < this.interval) {
1505
- throw new NotEnoughDataError(this.getRequiredInputs());
1506
- }
1507
- return this.result;
1508
- }
1509
- get isStable() {
1510
- try {
1511
- this.getResultOrThrow();
1512
- return true;
1513
- } catch {
1514
- return false;
1515
- }
1516
- }
1517
- };
1518
-
1519
- // src/trend/SMA15/SMA15.ts
1520
- var SMA15 = class _SMA15 extends MovingAverage {
1521
- prices = [];
1522
- static WEIGHTS = [-3, -6, -5, 3, 21, 46, 67, 74, 67, 46, 21, 3, -5, -6, -3];
1523
- static WEIGHT_SUM = 320;
1524
- getRequiredInputs() {
1525
- return _SMA15.WEIGHTS.length;
1526
- }
1527
- update(price, replace) {
1528
- pushUpdate(this.prices, replace, price, this.getRequiredInputs());
1529
- if (this.prices.length === this.getRequiredInputs()) {
1530
- let weightedPricesSum = 0;
1531
- for (let i = 0; i < this.getRequiredInputs(); i++) {
1532
- const weightedPrice = this.prices[i] * _SMA15.WEIGHTS[i];
1533
- weightedPricesSum += weightedPrice;
1534
- }
1535
- const weightedAverage = weightedPricesSum / _SMA15.WEIGHT_SUM;
1536
- return this.setResult(weightedAverage, replace);
1537
- }
1538
- return null;
1539
- }
1540
- };
1541
-
1542
- // src/trend/VWAP/VWAP.ts
1543
- var VWAP = class extends IndicatorSeries {
1544
- cumulativeTypicalPriceVolume = 0;
1545
- cumulativeVolume = 0;
1546
- lastCandle = null;
1547
- constructor() {
1548
- super();
1549
- }
1550
- calculateTypicalPriceVolume(data) {
1551
- const hlc3 = (data.high + data.low + data.close) / 3;
1552
- return hlc3 * data.volume;
1553
- }
1554
- getRequiredInputs() {
1555
- return 2;
1556
- }
1557
- update(candle, replace) {
1558
- if (candle.volume === 0) {
1559
- return null;
1560
- }
1561
- if (replace && this.lastCandle !== null) {
1562
- const lastTypicalPriceVolume = this.calculateTypicalPriceVolume(this.lastCandle);
1563
- this.cumulativeTypicalPriceVolume = this.cumulativeTypicalPriceVolume - lastTypicalPriceVolume;
1564
- this.cumulativeVolume = this.cumulativeVolume - this.lastCandle.volume;
1565
- }
1566
- this.lastCandle = candle;
1567
- const typicalPriceVolume = this.calculateTypicalPriceVolume(candle);
1568
- this.cumulativeTypicalPriceVolume = this.cumulativeTypicalPriceVolume + typicalPriceVolume;
1569
- this.cumulativeVolume = this.cumulativeVolume + candle.volume;
1570
- const vwap = this.cumulativeTypicalPriceVolume / this.cumulativeVolume;
1571
- return this.setResult(vwap, replace);
1572
- }
1573
- };
1574
-
1575
- // src/trend/WMA/WMA.ts
1576
- var WMA = class extends MovingAverage {
1577
- constructor(interval) {
1578
- super(interval);
1579
- this.interval = interval;
1580
- }
1581
- prices = [];
1582
- getRequiredInputs() {
1583
- return this.interval;
1584
- }
1585
- update(price, replace) {
1586
- pushUpdate(this.prices, replace, price, this.interval);
1587
- if (this.prices.length === this.interval) {
1588
- const weightedPricesSum = this.prices.reduce((acc, price2, index) => {
1589
- const weightedPrice = price2 * (index + 1);
1590
- return acc + weightedPrice;
1591
- }, 0);
1592
- const weightBase = this.interval * (this.interval + 1) / 2;
1593
- const weightedMa = weightedPricesSum / weightBase;
1594
- return this.setResult(weightedMa, replace);
1595
- }
1596
- return null;
1597
- }
1598
- };
1599
-
1600
- // src/trend/ZIGZAG/ZigZag.ts
1601
- var ZigZag = class extends IndicatorSeries {
1602
- deviation;
1603
- isUp = false;
1604
- highestExtreme = null;
1605
- lowestExtreme = null;
1606
- constructor(config) {
1607
- super();
1608
- this.deviation = config.deviation;
1609
- }
1610
- getRequiredInputs() {
1611
- return 1;
1612
- }
1613
- update(candle, replace) {
1614
- const low = candle.low;
1615
- const high = candle.high;
1616
- if (this.lowestExtreme === null) {
1617
- this.lowestExtreme = low;
1618
- }
1619
- if (this.highestExtreme === null) {
1620
- this.highestExtreme = high;
1621
- }
1622
- if (this.isUp) {
1623
- const uptrendReversal = this.lowestExtreme + (this.highestExtreme - this.lowestExtreme) * (100 - this.deviation) / 100;
1624
- if (high > this.highestExtreme) {
1625
- this.highestExtreme = high;
1626
- } else if (low < uptrendReversal) {
1627
- this.isUp = false;
1628
- this.lowestExtreme = low;
1629
- return this.setResult(this.highestExtreme, replace);
1630
- }
1631
- } else {
1632
- const downtrendReversal = low + (this.highestExtreme - low) * this.deviation / 100;
1633
- if (low < this.lowestExtreme) {
1634
- this.lowestExtreme = low;
1635
- } else if (high > downtrendReversal) {
1636
- this.isUp = true;
1637
- this.highestExtreme = high;
1638
- return this.setResult(this.lowestExtreme, replace);
1639
- }
1640
- }
1641
- return null;
1642
- }
1643
- };
1644
-
1645
- // src/volatility/ABANDS/AccelerationBands.ts
1646
- var AccelerationBands = class extends TechnicalIndicator {
1647
- constructor(interval, width, SmoothingIndicator = SMA) {
1648
- super();
1649
- this.interval = interval;
1650
- this.width = width;
1651
- this.lowerBand = new SmoothingIndicator(interval);
1652
- this.middleBand = new SmoothingIndicator(interval);
1653
- this.upperBand = new SmoothingIndicator(interval);
1654
- }
1655
- lowerBand;
1656
- middleBand;
1657
- upperBand;
1658
- getRequiredInputs() {
1659
- return this.middleBand.getRequiredInputs();
1660
- }
1661
- update({ high, low, close }, replace) {
1662
- const highPlusLow = high + low;
1663
- const coefficient = highPlusLow === 0 ? 0 : (high - low) / highPlusLow * this.width;
1664
- this.lowerBand.update(low * (1 - coefficient), replace);
1665
- this.middleBand.update(close, replace);
1666
- this.upperBand.update(high * (1 + coefficient), replace);
1667
- if (this.isStable) {
1668
- return this.result = {
1669
- lower: this.lowerBand.getResultOrThrow(),
1670
- middle: this.middleBand.getResultOrThrow(),
1671
- upper: this.upperBand.getResultOrThrow()
1672
- };
1673
- }
1674
- return null;
1675
- }
1676
- get isStable() {
1677
- return this.middleBand.isStable;
1678
- }
1679
- };
1680
-
1681
- // src/volatility/BBANDS/BollingerBands.ts
1682
- var BollingerBands = class extends TechnicalIndicator {
1683
- constructor(interval, deviationMultiplier = 2) {
1684
- super();
1685
- this.interval = interval;
1686
- this.deviationMultiplier = deviationMultiplier;
1687
- }
1688
- prices = [];
1689
- getRequiredInputs() {
1690
- return this.interval;
1691
- }
1692
- update(price, replace) {
1693
- const dropOut = pushUpdate(this.prices, replace, price, this.interval);
1694
- if (dropOut) {
1695
- const middle = getAverage(this.prices);
1696
- const standardDeviation = getStandardDeviation(this.prices, middle);
1697
- return this.result = {
1698
- lower: middle - standardDeviation * this.deviationMultiplier,
1699
- middle,
1700
- upper: middle + standardDeviation * this.deviationMultiplier
1701
- };
1702
- }
1703
- return null;
1704
- }
1705
- };
1706
-
1707
- // src/volatility/BBW/BollingerBandsWidth.ts
1708
- var BollingerBandsWidth = class extends IndicatorSeries {
1709
- constructor(bollingerBands) {
1710
- super();
1711
- this.bollingerBands = bollingerBands;
1712
- }
1713
- getRequiredInputs() {
1714
- return this.bollingerBands.getRequiredInputs();
1715
- }
1716
- update(price, replace) {
1717
- const result = this.bollingerBands.update(price, replace);
1718
- if (result) {
1719
- return this.setResult((result.upper - result.lower) / result.middle, replace);
1720
- }
1721
- return null;
1722
- }
1723
- };
1724
-
1725
- // src/volatility/IQR/IQR.ts
1726
- var IQR = class extends IndicatorSeries {
1727
- constructor(interval) {
1728
- super();
1729
- this.interval = interval;
1730
- }
1731
- values = [];
1732
- getRequiredInputs() {
1733
- return this.interval;
1734
- }
1735
- update(value, replace) {
1736
- if (replace) {
1737
- this.values.pop();
1738
- }
1739
- this.values.push(value);
1740
- if (this.values.length > this.interval) {
1741
- this.values.shift();
1742
- }
1743
- if (this.values.length < this.interval) {
1744
- return null;
1745
- }
1746
- const q1 = getQuartile(this.values, 0.25);
1747
- const q3 = getQuartile(this.values, 0.75);
1748
- return this.setResult(q3 - q1, replace);
1749
- }
1750
- };
1751
- export {
1752
- AC,
1753
- ADX,
1754
- AO,
1755
- ATR,
1756
- AccelerationBands,
1757
- BollingerBands,
1758
- BollingerBandsWidth,
1759
- CCI,
1760
- CG,
1761
- DEMA,
1762
- DMA,
1763
- DX,
1764
- EMA,
1765
- IQR,
1766
- IndicatorSeries,
1767
- LinearRegression,
1768
- MACD,
1769
- MAD,
1770
- MOM,
1771
- NotEnoughDataError,
1772
- OBV,
1773
- PSAR,
1774
- Period,
1775
- REI,
1776
- RMA,
1777
- ROC,
1778
- RSI,
1779
- SMA,
1780
- SMA15,
1781
- StochasticOscillator,
1782
- StochasticRSI,
1783
- TDS,
1784
- TR,
1785
- TechnicalIndicator,
1786
- TradingSignal,
1787
- TrendIndicatorSeries,
1788
- VWAP,
1789
- WMA,
1790
- WSMA,
1791
- WilliamsR,
1792
- ZigZag,
1793
- getAverage,
1794
- getGrid,
1795
- getMaximum,
1796
- getMedian,
1797
- getMinimum,
1798
- getQuartile,
1799
- getStandardDeviation,
1800
- getStreaks,
1801
- isFriday,
1802
- isMonday,
1803
- isSaturday,
1804
- isSunday,
1805
- isThursday,
1806
- isTuesday,
1807
- isWednesday,
1808
- pushUpdate
1809
- };