@tradingaction/indicators 2.0.1
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/LICENSE +24 -0
- package/README.md +5 -0
- package/lib/calculator/atr.d.ts +18 -0
- package/lib/calculator/atr.js +51 -0
- package/lib/calculator/atr.js.map +1 -0
- package/lib/calculator/bollingerband.d.ts +14 -0
- package/lib/calculator/bollingerband.js +72 -0
- package/lib/calculator/bollingerband.js.map +1 -0
- package/lib/calculator/change.d.ts +14 -0
- package/lib/calculator/change.js +30 -0
- package/lib/calculator/change.js.map +1 -0
- package/lib/calculator/compare.d.ts +10 -0
- package/lib/calculator/compare.js +50 -0
- package/lib/calculator/compare.js.map +1 -0
- package/lib/calculator/defaultOptionsForComputation.d.ts +91 -0
- package/lib/calculator/defaultOptionsForComputation.js +100 -0
- package/lib/calculator/defaultOptionsForComputation.js.map +1 -0
- package/lib/calculator/elderRay.d.ts +23 -0
- package/lib/calculator/elderRay.js +69 -0
- package/lib/calculator/elderRay.js.map +1 -0
- package/lib/calculator/ema.d.ts +12 -0
- package/lib/calculator/ema.js +73 -0
- package/lib/calculator/ema.js.map +1 -0
- package/lib/calculator/forceIndex.d.ts +12 -0
- package/lib/calculator/forceIndex.js +27 -0
- package/lib/calculator/forceIndex.js.map +1 -0
- package/lib/calculator/heikinAshi.d.ts +4 -0
- package/lib/calculator/heikinAshi.js +30 -0
- package/lib/calculator/heikinAshi.js.map +1 -0
- package/lib/calculator/index.d.ts +19 -0
- package/lib/calculator/index.js +20 -0
- package/lib/calculator/index.js.map +1 -0
- package/lib/calculator/kagi.d.ts +11 -0
- package/lib/calculator/kagi.js +200 -0
- package/lib/calculator/kagi.js.map +1 -0
- package/lib/calculator/macd.d.ts +18 -0
- package/lib/calculator/macd.js +57 -0
- package/lib/calculator/macd.js.map +1 -0
- package/lib/calculator/pointAndFigure.d.ts +10 -0
- package/lib/calculator/pointAndFigure.js +215 -0
- package/lib/calculator/pointAndFigure.js.map +1 -0
- package/lib/calculator/renko.d.ts +12 -0
- package/lib/calculator/renko.js +170 -0
- package/lib/calculator/renko.js.map +1 -0
- package/lib/calculator/rsi.d.ts +12 -0
- package/lib/calculator/rsi.js +87 -0
- package/lib/calculator/rsi.js.map +1 -0
- package/lib/calculator/sar.d.ts +12 -0
- package/lib/calculator/sar.js +87 -0
- package/lib/calculator/sar.js.map +1 -0
- package/lib/calculator/sma.d.ts +12 -0
- package/lib/calculator/sma.js +27 -0
- package/lib/calculator/sma.js.map +1 -0
- package/lib/calculator/smoothedForceIndex.d.ts +13 -0
- package/lib/calculator/smoothedForceIndex.js +40 -0
- package/lib/calculator/smoothedForceIndex.js.map +1 -0
- package/lib/calculator/sto.d.ts +19 -0
- package/lib/calculator/sto.js +91 -0
- package/lib/calculator/sto.js.map +1 -0
- package/lib/calculator/tma.d.ts +12 -0
- package/lib/calculator/tma.js +66 -0
- package/lib/calculator/tma.js.map +1 -0
- package/lib/calculator/wma.d.ts +12 -0
- package/lib/calculator/wma.js +33 -0
- package/lib/calculator/wma.js.map +1 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +2 -0
- package/lib/index.js.map +1 -0
- package/lib/indicator/algorithm.d.ts +10 -0
- package/lib/indicator/algorithm.js +35 -0
- package/lib/indicator/algorithm.js.map +1 -0
- package/lib/indicator/atr.d.ts +26 -0
- package/lib/indicator/atr.js +27 -0
- package/lib/indicator/atr.js.map +1 -0
- package/lib/indicator/baseIndicator.d.ts +16 -0
- package/lib/indicator/baseIndicator.js +59 -0
- package/lib/indicator/baseIndicator.js.map +1 -0
- package/lib/indicator/bollingerBand.d.ts +24 -0
- package/lib/indicator/bollingerBand.js +27 -0
- package/lib/indicator/bollingerBand.js.map +1 -0
- package/lib/indicator/change.d.ts +24 -0
- package/lib/indicator/change.js +25 -0
- package/lib/indicator/change.js.map +1 -0
- package/lib/indicator/compare.d.ts +24 -0
- package/lib/indicator/compare.js +29 -0
- package/lib/indicator/compare.js.map +1 -0
- package/lib/indicator/defaultOptionsForAppearance.d.ts +105 -0
- package/lib/indicator/defaultOptionsForAppearance.js +77 -0
- package/lib/indicator/defaultOptionsForAppearance.js.map +1 -0
- package/lib/indicator/elderImpulse.d.ts +7 -0
- package/lib/indicator/elderImpulse.js +61 -0
- package/lib/indicator/elderImpulse.js.map +1 -0
- package/lib/indicator/elderRay.d.ts +24 -0
- package/lib/indicator/elderRay.js +29 -0
- package/lib/indicator/elderRay.js.map +1 -0
- package/lib/indicator/ema.d.ts +24 -0
- package/lib/indicator/ema.js +29 -0
- package/lib/indicator/ema.js.map +1 -0
- package/lib/indicator/forceIndex.d.ts +24 -0
- package/lib/indicator/forceIndex.js +29 -0
- package/lib/indicator/forceIndex.js.map +1 -0
- package/lib/indicator/heikinAshi.d.ts +3 -0
- package/lib/indicator/heikinAshi.js +28 -0
- package/lib/indicator/heikinAshi.js.map +1 -0
- package/lib/indicator/index.d.ts +23 -0
- package/lib/indicator/index.js +24 -0
- package/lib/indicator/index.js.map +1 -0
- package/lib/indicator/kagi.d.ts +11 -0
- package/lib/indicator/kagi.js +13 -0
- package/lib/indicator/kagi.js.map +1 -0
- package/lib/indicator/macd.d.ts +24 -0
- package/lib/indicator/macd.js +32 -0
- package/lib/indicator/macd.js.map +1 -0
- package/lib/indicator/pointAndFigure.d.ts +10 -0
- package/lib/indicator/pointAndFigure.js +13 -0
- package/lib/indicator/pointAndFigure.js.map +1 -0
- package/lib/indicator/renko.d.ts +12 -0
- package/lib/indicator/renko.js +13 -0
- package/lib/indicator/renko.js.map +1 -0
- package/lib/indicator/rsi.d.ts +24 -0
- package/lib/indicator/rsi.js +29 -0
- package/lib/indicator/rsi.js.map +1 -0
- package/lib/indicator/sar.d.ts +24 -0
- package/lib/indicator/sar.js +29 -0
- package/lib/indicator/sar.js.map +1 -0
- package/lib/indicator/sma.d.ts +24 -0
- package/lib/indicator/sma.js +29 -0
- package/lib/indicator/sma.js.map +1 -0
- package/lib/indicator/stochasticOscillator.d.ts +24 -0
- package/lib/indicator/stochasticOscillator.js +27 -0
- package/lib/indicator/stochasticOscillator.js.map +1 -0
- package/lib/indicator/tma.d.ts +24 -0
- package/lib/indicator/tma.js +29 -0
- package/lib/indicator/tma.js.map +1 -0
- package/lib/indicator/wma.d.ts +24 -0
- package/lib/indicator/wma.js +29 -0
- package/lib/indicator/wma.js.map +1 -0
- package/lib/utils/functor.d.ts +1 -0
- package/lib/utils/functor.js +4 -0
- package/lib/utils/functor.js.map +1 -0
- package/lib/utils/identity.d.ts +1 -0
- package/lib/utils/identity.js +2 -0
- package/lib/utils/identity.js.map +1 -0
- package/lib/utils/index.d.ts +8 -0
- package/lib/utils/index.js +9 -0
- package/lib/utils/index.js.map +1 -0
- package/lib/utils/mappedSlidingWindow.d.ts +15 -0
- package/lib/utils/mappedSlidingWindow.js +77 -0
- package/lib/utils/mappedSlidingWindow.js.map +1 -0
- package/lib/utils/merge.d.ts +11 -0
- package/lib/utils/merge.js +70 -0
- package/lib/utils/merge.js.map +1 -0
- package/lib/utils/path.d.ts +1 -0
- package/lib/utils/path.js +15 -0
- package/lib/utils/path.js.map +1 -0
- package/lib/utils/rebind.d.ts +1 -0
- package/lib/utils/rebind.js +18 -0
- package/lib/utils/rebind.js.map +1 -0
- package/lib/utils/slidingWindow.d.ts +19 -0
- package/lib/utils/slidingWindow.js +111 -0
- package/lib/utils/slidingWindow.js.map +1 -0
- package/lib/utils/zipper.d.ts +7 -0
- package/lib/utils/zipper.js +36 -0
- package/lib/utils/zipper.js.map +1 -0
- package/package.json +49 -0
- package/src/calculator/atr.ts +88 -0
- package/src/calculator/bollingerband.ts +102 -0
- package/src/calculator/change.ts +53 -0
- package/src/calculator/compare.ts +68 -0
- package/src/calculator/defaultOptionsForComputation.ts +117 -0
- package/src/calculator/elderRay.ts +90 -0
- package/src/calculator/ema.ts +92 -0
- package/src/calculator/forceIndex.ts +43 -0
- package/src/calculator/heikinAshi.ts +38 -0
- package/src/calculator/index.ts +19 -0
- package/src/calculator/kagi.ts +249 -0
- package/src/calculator/macd.ts +90 -0
- package/src/calculator/pointAndFigure.ts +255 -0
- package/src/calculator/renko.ts +227 -0
- package/src/calculator/rsi.ts +114 -0
- package/src/calculator/sar.ts +117 -0
- package/src/calculator/sma.ts +41 -0
- package/src/calculator/smoothedForceIndex.ts +53 -0
- package/src/calculator/sto.ts +118 -0
- package/src/calculator/tma.ts +82 -0
- package/src/calculator/wma.ts +49 -0
- package/src/index.ts +1 -0
- package/src/indicator/algorithm.ts +53 -0
- package/src/indicator/atr.ts +58 -0
- package/src/indicator/baseIndicator.ts +97 -0
- package/src/indicator/bollingerBand.ts +55 -0
- package/src/indicator/change.ts +51 -0
- package/src/indicator/compare.ts +57 -0
- package/src/indicator/defaultOptionsForAppearance.ts +77 -0
- package/src/indicator/elderImpulse.ts +73 -0
- package/src/indicator/elderRay.ts +56 -0
- package/src/indicator/ema.ts +58 -0
- package/src/indicator/forceIndex.ts +57 -0
- package/src/indicator/heikinAshi.ts +35 -0
- package/src/indicator/index.ts +25 -0
- package/src/indicator/kagi.ts +18 -0
- package/src/indicator/macd.ts +60 -0
- package/src/indicator/pointAndFigure.ts +18 -0
- package/src/indicator/renko.ts +18 -0
- package/src/indicator/rsi.ts +58 -0
- package/src/indicator/sar.ts +56 -0
- package/src/indicator/sma.ts +57 -0
- package/src/indicator/stochasticOscillator.ts +56 -0
- package/src/indicator/tma.ts +57 -0
- package/src/indicator/wma.ts +58 -0
- package/src/utils/functor.ts +3 -0
- package/src/utils/identity.ts +1 -0
- package/src/utils/index.ts +8 -0
- package/src/utils/mappedSlidingWindow.ts +95 -0
- package/src/utils/merge.ts +93 -0
- package/src/utils/path.ts +16 -0
- package/src/utils/rebind.ts +19 -0
- package/src/utils/slidingWindow.ts +142 -0
- package/src/utils/zipper.ts +45 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { merge, rebind } from "../utils";
|
|
2
|
+
import { wma } from "../calculator";
|
|
3
|
+
import { WMAOptions } from "../calculator/wma";
|
|
4
|
+
import baseIndicator from "./baseIndicator";
|
|
5
|
+
|
|
6
|
+
const ALGORITHM_TYPE = "WMA";
|
|
7
|
+
|
|
8
|
+
interface WMAIndicator {
|
|
9
|
+
(data: any[], options?: { merge: boolean }): any;
|
|
10
|
+
id(): number;
|
|
11
|
+
id(x: number): WMAIndicator;
|
|
12
|
+
accessor(): any;
|
|
13
|
+
accessor(x: any): WMAIndicator;
|
|
14
|
+
stroke(): string | any;
|
|
15
|
+
stroke(x: string | any): WMAIndicator;
|
|
16
|
+
fill(): string | any;
|
|
17
|
+
fill(x: string | any): WMAIndicator;
|
|
18
|
+
echo(): any;
|
|
19
|
+
echo(x: any): WMAIndicator;
|
|
20
|
+
type(): string;
|
|
21
|
+
type(x: string): WMAIndicator;
|
|
22
|
+
merge(): any;
|
|
23
|
+
merge(newMerge: any): WMAIndicator;
|
|
24
|
+
options(): WMAOptions;
|
|
25
|
+
options(newOptions: WMAOptions): WMAIndicator;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export default function () {
|
|
29
|
+
const base = baseIndicator()
|
|
30
|
+
.type(ALGORITHM_TYPE)
|
|
31
|
+
.accessor((d: any) => d.wma);
|
|
32
|
+
|
|
33
|
+
const underlyingAlgorithm = wma();
|
|
34
|
+
|
|
35
|
+
const mergedAlgorithm = merge()
|
|
36
|
+
.algorithm(underlyingAlgorithm)
|
|
37
|
+
.merge((datum: any, i: number) => {
|
|
38
|
+
datum.wma = i;
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const indicator = (data: any[], options = { merge: true }) => {
|
|
42
|
+
if (options.merge) {
|
|
43
|
+
if (!base.accessor()) {
|
|
44
|
+
throw new Error(`Set an accessor to ${ALGORITHM_TYPE} before calculating`);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return mergedAlgorithm(data);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return underlyingAlgorithm(data);
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
rebind(indicator, base, "id", "accessor", "stroke", "fill", "echo", "type");
|
|
54
|
+
rebind(indicator, underlyingAlgorithm, "options", "undefinedLength");
|
|
55
|
+
rebind(indicator, mergedAlgorithm, "merge", "skipUndefined");
|
|
56
|
+
|
|
57
|
+
return indicator as WMAIndicator;
|
|
58
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const identity = <T>(d: T) => d;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from "./functor";
|
|
2
|
+
export * from "./identity";
|
|
3
|
+
export { default as mappedSlidingWindow } from "./mappedSlidingWindow";
|
|
4
|
+
export { default as merge } from "./merge";
|
|
5
|
+
export * from "./path";
|
|
6
|
+
export { default as rebind } from "./rebind";
|
|
7
|
+
export { default as slidingWindow } from "./slidingWindow";
|
|
8
|
+
export { default as zipper } from "./zipper";
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { functor } from "./functor";
|
|
2
|
+
import { identity } from "./identity";
|
|
3
|
+
|
|
4
|
+
interface MappedSlidingWindow {
|
|
5
|
+
(data: any[]): any;
|
|
6
|
+
accumulator(): any;
|
|
7
|
+
accumulator(x: any): MappedSlidingWindow;
|
|
8
|
+
skipInitial(): number;
|
|
9
|
+
skipInitial(x: number): MappedSlidingWindow;
|
|
10
|
+
source(): any;
|
|
11
|
+
source(x: any): MappedSlidingWindow;
|
|
12
|
+
undefinedValue(): any;
|
|
13
|
+
undefinedValue(x: any): MappedSlidingWindow;
|
|
14
|
+
windowSize(): number;
|
|
15
|
+
windowSize(x: number): MappedSlidingWindow;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export default function () {
|
|
19
|
+
let undefinedValue: any;
|
|
20
|
+
let windowSize = 10;
|
|
21
|
+
let accumulator = () => {
|
|
22
|
+
/** Do Nothing */
|
|
23
|
+
};
|
|
24
|
+
let source = identity;
|
|
25
|
+
let skipInitial = 0;
|
|
26
|
+
|
|
27
|
+
const mappedSlidingWindow = function (data: any[]) {
|
|
28
|
+
// @ts-ignore
|
|
29
|
+
const size = functor(windowSize).apply(this, arguments);
|
|
30
|
+
const windowData: any[] = [];
|
|
31
|
+
let accumulatorIdx = 0;
|
|
32
|
+
const undef = functor(undefinedValue);
|
|
33
|
+
const result: any[] = [];
|
|
34
|
+
data.forEach(function (d, i) {
|
|
35
|
+
let mapped;
|
|
36
|
+
if (i < skipInitial + size - 1) {
|
|
37
|
+
mapped = undef(d, i);
|
|
38
|
+
result.push(mapped);
|
|
39
|
+
windowData.push(mapped);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (i >= skipInitial + size) {
|
|
43
|
+
windowData.shift();
|
|
44
|
+
}
|
|
45
|
+
// @ts-ignore
|
|
46
|
+
windowData.push(source(d, i));
|
|
47
|
+
|
|
48
|
+
// @ts-ignore
|
|
49
|
+
mapped = accumulator(windowData, i, accumulatorIdx++);
|
|
50
|
+
result.push(mapped);
|
|
51
|
+
windowData.pop();
|
|
52
|
+
windowData.push(mapped);
|
|
53
|
+
return;
|
|
54
|
+
});
|
|
55
|
+
return result;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
mappedSlidingWindow.undefinedValue = function (x: any) {
|
|
59
|
+
if (!arguments.length) {
|
|
60
|
+
return undefinedValue;
|
|
61
|
+
}
|
|
62
|
+
undefinedValue = x;
|
|
63
|
+
return mappedSlidingWindow;
|
|
64
|
+
};
|
|
65
|
+
mappedSlidingWindow.windowSize = function (x: number) {
|
|
66
|
+
if (!arguments.length) {
|
|
67
|
+
return windowSize;
|
|
68
|
+
}
|
|
69
|
+
windowSize = x;
|
|
70
|
+
return mappedSlidingWindow;
|
|
71
|
+
};
|
|
72
|
+
mappedSlidingWindow.accumulator = function (x: any) {
|
|
73
|
+
if (!arguments.length) {
|
|
74
|
+
return accumulator;
|
|
75
|
+
}
|
|
76
|
+
accumulator = x;
|
|
77
|
+
return mappedSlidingWindow;
|
|
78
|
+
};
|
|
79
|
+
mappedSlidingWindow.skipInitial = function (x: number) {
|
|
80
|
+
if (!arguments.length) {
|
|
81
|
+
return skipInitial;
|
|
82
|
+
}
|
|
83
|
+
skipInitial = x;
|
|
84
|
+
return mappedSlidingWindow;
|
|
85
|
+
};
|
|
86
|
+
mappedSlidingWindow.source = function (x: any) {
|
|
87
|
+
if (!arguments.length) {
|
|
88
|
+
return source;
|
|
89
|
+
}
|
|
90
|
+
source = x;
|
|
91
|
+
return mappedSlidingWindow;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
return mappedSlidingWindow as MappedSlidingWindow;
|
|
95
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/*
|
|
2
|
+
https://github.com/ScottLogic/d3fc/blob/master/src/indicator/algorithm/merge.js
|
|
3
|
+
|
|
4
|
+
The MIT License (MIT)
|
|
5
|
+
|
|
6
|
+
Copyright (c) 2014-2015 Scott Logic Ltd.
|
|
7
|
+
|
|
8
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
9
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
10
|
+
in the Software without restriction, including without limitation the rights
|
|
11
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
12
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
13
|
+
furnished to do so, subject to the following conditions:
|
|
14
|
+
|
|
15
|
+
The above copyright notice and this permission notice shall be included in
|
|
16
|
+
all copies or substantial portions of the Software.
|
|
17
|
+
|
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
24
|
+
THE SOFTWARE.
|
|
25
|
+
*/
|
|
26
|
+
import zipper from "./zipper";
|
|
27
|
+
import { identity } from "./identity";
|
|
28
|
+
|
|
29
|
+
interface Merge {
|
|
30
|
+
(data: any[]): any;
|
|
31
|
+
algorithm(): any;
|
|
32
|
+
algorithm(newAlgorithm: any): Merge;
|
|
33
|
+
merge(): any;
|
|
34
|
+
merge(newMerge: any): Merge;
|
|
35
|
+
skipUndefined(): boolean;
|
|
36
|
+
skipUndefined(newSkipUndefined: boolean): Merge;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// applies an algorithm to an array, merging the result back into
|
|
40
|
+
// the source array using the given merge function.
|
|
41
|
+
export default function () {
|
|
42
|
+
let algorithm = identity;
|
|
43
|
+
let skipUndefined = true;
|
|
44
|
+
let merge = () => {
|
|
45
|
+
/** Do Nothing */
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const mergeCompute = (data: any[]) => {
|
|
49
|
+
const zip = zipper().combine((datum: any, indicator: any) => {
|
|
50
|
+
const result =
|
|
51
|
+
skipUndefined && indicator === undefined
|
|
52
|
+
? datum
|
|
53
|
+
: // @ts-ignore
|
|
54
|
+
merge(datum, indicator);
|
|
55
|
+
return result === undefined ? datum : result;
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// @ts-ignore
|
|
59
|
+
return zip(data, algorithm(data));
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
mergeCompute.algorithm = (newAlgorithm?: any) => {
|
|
63
|
+
if (newAlgorithm === undefined) {
|
|
64
|
+
return algorithm;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
algorithm = newAlgorithm;
|
|
68
|
+
|
|
69
|
+
return mergeCompute;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
mergeCompute.merge = (newMerge?: any) => {
|
|
73
|
+
if (newMerge === undefined) {
|
|
74
|
+
return merge;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
merge = newMerge;
|
|
78
|
+
|
|
79
|
+
return mergeCompute;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
mergeCompute.skipUndefined = (newSkipUndefined?: boolean) => {
|
|
83
|
+
if (newSkipUndefined === undefined) {
|
|
84
|
+
return skipUndefined;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
skipUndefined = newSkipUndefined;
|
|
88
|
+
|
|
89
|
+
return mergeCompute;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
return mergeCompute as Merge;
|
|
93
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export const path = (loc: any | any[] = []) => {
|
|
2
|
+
const key = Array.isArray(loc) ? loc : [loc];
|
|
3
|
+
const length = key.length;
|
|
4
|
+
|
|
5
|
+
return function (obj: any, defaultValue?: any) {
|
|
6
|
+
if (length === 0) {
|
|
7
|
+
return obj !== undefined && obj !== null ? obj : defaultValue;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
let index = 0;
|
|
11
|
+
while (obj != null && index < length) {
|
|
12
|
+
obj = obj[key[index++]];
|
|
13
|
+
}
|
|
14
|
+
return index === length ? obj : defaultValue;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// copied from https://github.com/d3fc/d3fc-rebind/blob/master/src/rebind.js
|
|
2
|
+
|
|
3
|
+
function createReboundMethod(target: any, source: any, name: string) {
|
|
4
|
+
const method = source[name];
|
|
5
|
+
if (typeof method !== "function") {
|
|
6
|
+
throw new Error(`Attempt to rebind ${name} which isn't a function on the source object`);
|
|
7
|
+
}
|
|
8
|
+
return (...args: any[]) => {
|
|
9
|
+
const value = method.apply(source, args);
|
|
10
|
+
return value === source ? target : value;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export default function rebind(target: any, source: any, ...names: string[]) {
|
|
15
|
+
for (const name of names) {
|
|
16
|
+
target[name] = createReboundMethod(target, source, name);
|
|
17
|
+
}
|
|
18
|
+
return target;
|
|
19
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/*
|
|
2
|
+
|
|
3
|
+
Taken from https://github.com/ScottLogic/d3fc/blob/master/src/indicator/algorithm/calculator/slidingWindow.js
|
|
4
|
+
|
|
5
|
+
The MIT License (MIT)
|
|
6
|
+
|
|
7
|
+
Copyright (c) 2014-2015 Scott Logic Ltd.
|
|
8
|
+
|
|
9
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
in the Software without restriction, including without limitation the rights
|
|
12
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
furnished to do so, subject to the following conditions:
|
|
15
|
+
|
|
16
|
+
The above copyright notice and this permission notice shall be included in
|
|
17
|
+
all copies or substantial portions of the Software.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
25
|
+
THE SOFTWARE.
|
|
26
|
+
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import { functor } from "./functor";
|
|
30
|
+
import { path } from "./path";
|
|
31
|
+
|
|
32
|
+
interface SlidingWindow {
|
|
33
|
+
(data: any[]): any[];
|
|
34
|
+
misc(): any;
|
|
35
|
+
misc(x: any): SlidingWindow;
|
|
36
|
+
accumulator(): any;
|
|
37
|
+
accumulator(x: any): SlidingWindow;
|
|
38
|
+
skipInitial(): number;
|
|
39
|
+
skipInitial(x: number): SlidingWindow;
|
|
40
|
+
source(): any;
|
|
41
|
+
source(source: any): SlidingWindow;
|
|
42
|
+
sourcePath(): any;
|
|
43
|
+
sourcePath(x: any): SlidingWindow;
|
|
44
|
+
windowSize(): number;
|
|
45
|
+
windowSize(windowSize: number): SlidingWindow;
|
|
46
|
+
undefinedValue(): any;
|
|
47
|
+
undefinedValue(x: any): SlidingWindow;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export default function () {
|
|
51
|
+
let undefinedValue: any;
|
|
52
|
+
let windowSize = 10;
|
|
53
|
+
let accumulator = () => {
|
|
54
|
+
/** Do nothin */
|
|
55
|
+
};
|
|
56
|
+
let sourcePath: any;
|
|
57
|
+
let source: any;
|
|
58
|
+
let skipInitial = 0;
|
|
59
|
+
let misc: any;
|
|
60
|
+
|
|
61
|
+
const slidingWindow = (data: any[]) => {
|
|
62
|
+
const sourceFunction = source || path(sourcePath);
|
|
63
|
+
|
|
64
|
+
// @ts-ignore
|
|
65
|
+
const size = functor(windowSize).apply(this, arguments);
|
|
66
|
+
const windowData = data.slice(skipInitial, size + skipInitial).map(sourceFunction);
|
|
67
|
+
|
|
68
|
+
let accumulatorIdx = 0;
|
|
69
|
+
const undef = functor(undefinedValue);
|
|
70
|
+
return data.map((d, i) => {
|
|
71
|
+
if (i < skipInitial + size - 1) {
|
|
72
|
+
return undef(sourceFunction(d), i, misc);
|
|
73
|
+
}
|
|
74
|
+
if (i >= skipInitial + size) {
|
|
75
|
+
// Treat windowData as FIFO rolling buffer
|
|
76
|
+
windowData.shift();
|
|
77
|
+
windowData.push(sourceFunction(d, i));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// @ts-ignore
|
|
81
|
+
return accumulator(windowData, i, accumulatorIdx++, misc);
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
slidingWindow.undefinedValue = function (x: any) {
|
|
86
|
+
if (!arguments.length) {
|
|
87
|
+
return undefinedValue;
|
|
88
|
+
}
|
|
89
|
+
undefinedValue = x;
|
|
90
|
+
return slidingWindow;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
slidingWindow.windowSize = function (x: any) {
|
|
94
|
+
if (!arguments.length) {
|
|
95
|
+
return windowSize;
|
|
96
|
+
}
|
|
97
|
+
windowSize = x;
|
|
98
|
+
return slidingWindow;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
slidingWindow.misc = function (x: any) {
|
|
102
|
+
if (!arguments.length) {
|
|
103
|
+
return misc;
|
|
104
|
+
}
|
|
105
|
+
misc = x;
|
|
106
|
+
return slidingWindow;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
slidingWindow.accumulator = function (x: any) {
|
|
110
|
+
if (!arguments.length) {
|
|
111
|
+
return accumulator;
|
|
112
|
+
}
|
|
113
|
+
accumulator = x;
|
|
114
|
+
return slidingWindow;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
slidingWindow.skipInitial = function (x: any) {
|
|
118
|
+
if (!arguments.length) {
|
|
119
|
+
return skipInitial;
|
|
120
|
+
}
|
|
121
|
+
skipInitial = x;
|
|
122
|
+
return slidingWindow;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
slidingWindow.sourcePath = function (x: any) {
|
|
126
|
+
if (!arguments.length) {
|
|
127
|
+
return sourcePath;
|
|
128
|
+
}
|
|
129
|
+
sourcePath = x;
|
|
130
|
+
return slidingWindow;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
slidingWindow.source = function (x: any) {
|
|
134
|
+
if (!arguments.length) {
|
|
135
|
+
return source;
|
|
136
|
+
}
|
|
137
|
+
source = x;
|
|
138
|
+
return slidingWindow;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
return slidingWindow as SlidingWindow;
|
|
142
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/* an extension to d3.zip so we call a function instead of an array */
|
|
2
|
+
|
|
3
|
+
import { min } from "d3-array";
|
|
4
|
+
import { identity } from "./identity";
|
|
5
|
+
|
|
6
|
+
interface Zip {
|
|
7
|
+
(...args: any[]): any[];
|
|
8
|
+
combine(): any;
|
|
9
|
+
combine(x: any): Zip;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export default function zipper() {
|
|
13
|
+
let combine = identity;
|
|
14
|
+
|
|
15
|
+
function zip() {
|
|
16
|
+
const n = arguments.length;
|
|
17
|
+
if (n === 0) {
|
|
18
|
+
return [];
|
|
19
|
+
}
|
|
20
|
+
const m = min(arguments, d3_zipLength) ?? 0;
|
|
21
|
+
|
|
22
|
+
const zips = new Array(m);
|
|
23
|
+
for (let i = -1; ++i < m; ) {
|
|
24
|
+
// tslint:disable-next-line: no-shadowed-variable
|
|
25
|
+
for (let j = -1, zip = (zips[i] = new Array(n)); ++j < n; ) {
|
|
26
|
+
zip[j] = arguments[j][i];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// @ts-ignore
|
|
30
|
+
zips[i] = combine.apply(this, zips[i]);
|
|
31
|
+
}
|
|
32
|
+
return zips;
|
|
33
|
+
}
|
|
34
|
+
function d3_zipLength(d: any[]) {
|
|
35
|
+
return d.length;
|
|
36
|
+
}
|
|
37
|
+
zip.combine = function (x: any) {
|
|
38
|
+
if (!arguments.length) {
|
|
39
|
+
return combine;
|
|
40
|
+
}
|
|
41
|
+
combine = x;
|
|
42
|
+
return zip;
|
|
43
|
+
};
|
|
44
|
+
return zip as Zip;
|
|
45
|
+
}
|