@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,11 @@
|
|
|
1
|
+
interface Merge {
|
|
2
|
+
(data: any[]): any;
|
|
3
|
+
algorithm(): any;
|
|
4
|
+
algorithm(newAlgorithm: any): Merge;
|
|
5
|
+
merge(): any;
|
|
6
|
+
merge(newMerge: any): Merge;
|
|
7
|
+
skipUndefined(): boolean;
|
|
8
|
+
skipUndefined(newSkipUndefined: boolean): Merge;
|
|
9
|
+
}
|
|
10
|
+
export default function (): Merge;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,70 @@
|
|
|
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
|
+
// applies an algorithm to an array, merging the result back into
|
|
29
|
+
// the source array using the given merge function.
|
|
30
|
+
export default function () {
|
|
31
|
+
let algorithm = identity;
|
|
32
|
+
let skipUndefined = true;
|
|
33
|
+
let merge = () => {
|
|
34
|
+
/** Do Nothing */
|
|
35
|
+
};
|
|
36
|
+
const mergeCompute = (data) => {
|
|
37
|
+
const zip = zipper().combine((datum, indicator) => {
|
|
38
|
+
const result = skipUndefined && indicator === undefined
|
|
39
|
+
? datum
|
|
40
|
+
: // @ts-ignore
|
|
41
|
+
merge(datum, indicator);
|
|
42
|
+
return result === undefined ? datum : result;
|
|
43
|
+
});
|
|
44
|
+
// @ts-ignore
|
|
45
|
+
return zip(data, algorithm(data));
|
|
46
|
+
};
|
|
47
|
+
mergeCompute.algorithm = (newAlgorithm) => {
|
|
48
|
+
if (newAlgorithm === undefined) {
|
|
49
|
+
return algorithm;
|
|
50
|
+
}
|
|
51
|
+
algorithm = newAlgorithm;
|
|
52
|
+
return mergeCompute;
|
|
53
|
+
};
|
|
54
|
+
mergeCompute.merge = (newMerge) => {
|
|
55
|
+
if (newMerge === undefined) {
|
|
56
|
+
return merge;
|
|
57
|
+
}
|
|
58
|
+
merge = newMerge;
|
|
59
|
+
return mergeCompute;
|
|
60
|
+
};
|
|
61
|
+
mergeCompute.skipUndefined = (newSkipUndefined) => {
|
|
62
|
+
if (newSkipUndefined === undefined) {
|
|
63
|
+
return skipUndefined;
|
|
64
|
+
}
|
|
65
|
+
skipUndefined = newSkipUndefined;
|
|
66
|
+
return mergeCompute;
|
|
67
|
+
};
|
|
68
|
+
return mergeCompute;
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=merge.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"merge.js","sourceRoot":"","sources":["../../src/utils/merge.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;EAwBE;AACF,OAAO,MAAM,MAAM,UAAU,CAAC;AAC9B,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAYtC,iEAAiE;AACjE,mDAAmD;AACnD,MAAM,CAAC,OAAO;IACV,IAAI,SAAS,GAAG,QAAQ,CAAC;IACzB,IAAI,aAAa,GAAG,IAAI,CAAC;IACzB,IAAI,KAAK,GAAG,GAAG,EAAE;QACb,iBAAiB;IACrB,CAAC,CAAC;IAEF,MAAM,YAAY,GAAG,CAAC,IAAW,EAAE,EAAE;QACjC,MAAM,GAAG,GAAG,MAAM,EAAE,CAAC,OAAO,CAAC,CAAC,KAAU,EAAE,SAAc,EAAE,EAAE;YACxD,MAAM,MAAM,GACR,aAAa,IAAI,SAAS,KAAK,SAAS;gBACpC,CAAC,CAAC,KAAK;gBACP,CAAC,CAAC,aAAa;oBACb,KAAK,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;YAClC,OAAO,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC;QACjD,CAAC,CAAC,CAAC;QAEH,aAAa;QACb,OAAO,GAAG,CAAC,IAAI,EAAE,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC;IACtC,CAAC,CAAC;IAEF,YAAY,CAAC,SAAS,GAAG,CAAC,YAAkB,EAAE,EAAE;QAC5C,IAAI,YAAY,KAAK,SAAS,EAAE;YAC5B,OAAO,SAAS,CAAC;SACpB;QAED,SAAS,GAAG,YAAY,CAAC;QAEzB,OAAO,YAAY,CAAC;IACxB,CAAC,CAAC;IAEF,YAAY,CAAC,KAAK,GAAG,CAAC,QAAc,EAAE,EAAE;QACpC,IAAI,QAAQ,KAAK,SAAS,EAAE;YACxB,OAAO,KAAK,CAAC;SAChB;QAED,KAAK,GAAG,QAAQ,CAAC;QAEjB,OAAO,YAAY,CAAC;IACxB,CAAC,CAAC;IAEF,YAAY,CAAC,aAAa,GAAG,CAAC,gBAA0B,EAAE,EAAE;QACxD,IAAI,gBAAgB,KAAK,SAAS,EAAE;YAChC,OAAO,aAAa,CAAC;SACxB;QAED,aAAa,GAAG,gBAAgB,CAAC;QAEjC,OAAO,YAAY,CAAC;IACxB,CAAC,CAAC;IAEF,OAAO,YAAqB,CAAC;AACjC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const path: (loc?: any | any[]) => (obj: any, defaultValue?: any) => any;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export const path = (loc = []) => {
|
|
2
|
+
const key = Array.isArray(loc) ? loc : [loc];
|
|
3
|
+
const length = key.length;
|
|
4
|
+
return function (obj, defaultValue) {
|
|
5
|
+
if (length === 0) {
|
|
6
|
+
return obj !== undefined && obj !== null ? obj : defaultValue;
|
|
7
|
+
}
|
|
8
|
+
let index = 0;
|
|
9
|
+
while (obj != null && index < length) {
|
|
10
|
+
obj = obj[key[index++]];
|
|
11
|
+
}
|
|
12
|
+
return index === length ? obj : defaultValue;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
//# sourceMappingURL=path.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"path.js","sourceRoot":"","sources":["../../src/utils/path.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,IAAI,GAAG,CAAC,MAAmB,EAAE,EAAE,EAAE;IAC1C,MAAM,GAAG,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;IAC7C,MAAM,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC;IAE1B,OAAO,UAAU,GAAQ,EAAE,YAAkB;QACzC,IAAI,MAAM,KAAK,CAAC,EAAE;YACd,OAAO,GAAG,KAAK,SAAS,IAAI,GAAG,KAAK,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,YAAY,CAAC;SACjE;QAED,IAAI,KAAK,GAAG,CAAC,CAAC;QACd,OAAO,GAAG,IAAI,IAAI,IAAI,KAAK,GAAG,MAAM,EAAE;YAClC,GAAG,GAAG,GAAG,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;SAC3B;QACD,OAAO,KAAK,KAAK,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,YAAY,CAAC;IACjD,CAAC,CAAC;AACN,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function rebind(target: any, source: any, ...names: string[]): any;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// copied from https://github.com/d3fc/d3fc-rebind/blob/master/src/rebind.js
|
|
2
|
+
function createReboundMethod(target, source, name) {
|
|
3
|
+
const method = source[name];
|
|
4
|
+
if (typeof method !== "function") {
|
|
5
|
+
throw new Error(`Attempt to rebind ${name} which isn't a function on the source object`);
|
|
6
|
+
}
|
|
7
|
+
return (...args) => {
|
|
8
|
+
const value = method.apply(source, args);
|
|
9
|
+
return value === source ? target : value;
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
export default function rebind(target, source, ...names) {
|
|
13
|
+
for (const name of names) {
|
|
14
|
+
target[name] = createReboundMethod(target, source, name);
|
|
15
|
+
}
|
|
16
|
+
return target;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=rebind.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rebind.js","sourceRoot":"","sources":["../../src/utils/rebind.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAE5E,SAAS,mBAAmB,CAAC,MAAW,EAAE,MAAW,EAAE,IAAY;IAC/D,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;IAC5B,IAAI,OAAO,MAAM,KAAK,UAAU,EAAE;QAC9B,MAAM,IAAI,KAAK,CAAC,qBAAqB,IAAI,8CAA8C,CAAC,CAAC;KAC5F;IACD,OAAO,CAAC,GAAG,IAAW,EAAE,EAAE;QACtB,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;QACzC,OAAO,KAAK,KAAK,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC;IAC7C,CAAC,CAAC;AACN,CAAC;AAED,MAAM,CAAC,OAAO,UAAU,MAAM,CAAC,MAAW,EAAE,MAAW,EAAE,GAAG,KAAe;IACvE,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE;QACtB,MAAM,CAAC,IAAI,CAAC,GAAG,mBAAmB,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC;KAC5D;IACD,OAAO,MAAM,CAAC;AAClB,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
interface SlidingWindow {
|
|
2
|
+
(data: any[]): any[];
|
|
3
|
+
misc(): any;
|
|
4
|
+
misc(x: any): SlidingWindow;
|
|
5
|
+
accumulator(): any;
|
|
6
|
+
accumulator(x: any): SlidingWindow;
|
|
7
|
+
skipInitial(): number;
|
|
8
|
+
skipInitial(x: number): SlidingWindow;
|
|
9
|
+
source(): any;
|
|
10
|
+
source(source: any): SlidingWindow;
|
|
11
|
+
sourcePath(): any;
|
|
12
|
+
sourcePath(x: any): SlidingWindow;
|
|
13
|
+
windowSize(): number;
|
|
14
|
+
windowSize(windowSize: number): SlidingWindow;
|
|
15
|
+
undefinedValue(): any;
|
|
16
|
+
undefinedValue(x: any): SlidingWindow;
|
|
17
|
+
}
|
|
18
|
+
export default function (): SlidingWindow;
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,111 @@
|
|
|
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
|
+
import { functor } from "./functor";
|
|
29
|
+
import { path } from "./path";
|
|
30
|
+
export default function () {
|
|
31
|
+
let undefinedValue;
|
|
32
|
+
let windowSize = 10;
|
|
33
|
+
let accumulator = () => {
|
|
34
|
+
/** Do nothin */
|
|
35
|
+
};
|
|
36
|
+
let sourcePath;
|
|
37
|
+
let source;
|
|
38
|
+
let skipInitial = 0;
|
|
39
|
+
let misc;
|
|
40
|
+
const slidingWindow = (data) => {
|
|
41
|
+
const sourceFunction = source || path(sourcePath);
|
|
42
|
+
// @ts-ignore
|
|
43
|
+
const size = functor(windowSize).apply(this, arguments);
|
|
44
|
+
const windowData = data.slice(skipInitial, size + skipInitial).map(sourceFunction);
|
|
45
|
+
let accumulatorIdx = 0;
|
|
46
|
+
const undef = functor(undefinedValue);
|
|
47
|
+
return data.map((d, i) => {
|
|
48
|
+
if (i < skipInitial + size - 1) {
|
|
49
|
+
return undef(sourceFunction(d), i, misc);
|
|
50
|
+
}
|
|
51
|
+
if (i >= skipInitial + size) {
|
|
52
|
+
// Treat windowData as FIFO rolling buffer
|
|
53
|
+
windowData.shift();
|
|
54
|
+
windowData.push(sourceFunction(d, i));
|
|
55
|
+
}
|
|
56
|
+
// @ts-ignore
|
|
57
|
+
return accumulator(windowData, i, accumulatorIdx++, misc);
|
|
58
|
+
});
|
|
59
|
+
};
|
|
60
|
+
slidingWindow.undefinedValue = function (x) {
|
|
61
|
+
if (!arguments.length) {
|
|
62
|
+
return undefinedValue;
|
|
63
|
+
}
|
|
64
|
+
undefinedValue = x;
|
|
65
|
+
return slidingWindow;
|
|
66
|
+
};
|
|
67
|
+
slidingWindow.windowSize = function (x) {
|
|
68
|
+
if (!arguments.length) {
|
|
69
|
+
return windowSize;
|
|
70
|
+
}
|
|
71
|
+
windowSize = x;
|
|
72
|
+
return slidingWindow;
|
|
73
|
+
};
|
|
74
|
+
slidingWindow.misc = function (x) {
|
|
75
|
+
if (!arguments.length) {
|
|
76
|
+
return misc;
|
|
77
|
+
}
|
|
78
|
+
misc = x;
|
|
79
|
+
return slidingWindow;
|
|
80
|
+
};
|
|
81
|
+
slidingWindow.accumulator = function (x) {
|
|
82
|
+
if (!arguments.length) {
|
|
83
|
+
return accumulator;
|
|
84
|
+
}
|
|
85
|
+
accumulator = x;
|
|
86
|
+
return slidingWindow;
|
|
87
|
+
};
|
|
88
|
+
slidingWindow.skipInitial = function (x) {
|
|
89
|
+
if (!arguments.length) {
|
|
90
|
+
return skipInitial;
|
|
91
|
+
}
|
|
92
|
+
skipInitial = x;
|
|
93
|
+
return slidingWindow;
|
|
94
|
+
};
|
|
95
|
+
slidingWindow.sourcePath = function (x) {
|
|
96
|
+
if (!arguments.length) {
|
|
97
|
+
return sourcePath;
|
|
98
|
+
}
|
|
99
|
+
sourcePath = x;
|
|
100
|
+
return slidingWindow;
|
|
101
|
+
};
|
|
102
|
+
slidingWindow.source = function (x) {
|
|
103
|
+
if (!arguments.length) {
|
|
104
|
+
return source;
|
|
105
|
+
}
|
|
106
|
+
source = x;
|
|
107
|
+
return slidingWindow;
|
|
108
|
+
};
|
|
109
|
+
return slidingWindow;
|
|
110
|
+
}
|
|
111
|
+
//# sourceMappingURL=slidingWindow.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"slidingWindow.js","sourceRoot":"","sources":["../../src/utils/slidingWindow.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/* an extension to d3.zip so we call a function instead of an array */
|
|
2
|
+
import { min } from "d3-array";
|
|
3
|
+
import { identity } from "./identity";
|
|
4
|
+
export default function zipper() {
|
|
5
|
+
let combine = identity;
|
|
6
|
+
function zip() {
|
|
7
|
+
var _a;
|
|
8
|
+
const n = arguments.length;
|
|
9
|
+
if (n === 0) {
|
|
10
|
+
return [];
|
|
11
|
+
}
|
|
12
|
+
const m = (_a = min(arguments, d3_zipLength)) !== null && _a !== void 0 ? _a : 0;
|
|
13
|
+
const zips = new Array(m);
|
|
14
|
+
for (let i = -1; ++i < m;) {
|
|
15
|
+
// tslint:disable-next-line: no-shadowed-variable
|
|
16
|
+
for (let j = -1, zip = (zips[i] = new Array(n)); ++j < n;) {
|
|
17
|
+
zip[j] = arguments[j][i];
|
|
18
|
+
}
|
|
19
|
+
// @ts-ignore
|
|
20
|
+
zips[i] = combine.apply(this, zips[i]);
|
|
21
|
+
}
|
|
22
|
+
return zips;
|
|
23
|
+
}
|
|
24
|
+
function d3_zipLength(d) {
|
|
25
|
+
return d.length;
|
|
26
|
+
}
|
|
27
|
+
zip.combine = function (x) {
|
|
28
|
+
if (!arguments.length) {
|
|
29
|
+
return combine;
|
|
30
|
+
}
|
|
31
|
+
combine = x;
|
|
32
|
+
return zip;
|
|
33
|
+
};
|
|
34
|
+
return zip;
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=zipper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zipper.js","sourceRoot":"","sources":["../../src/utils/zipper.ts"],"names":[],"mappings":"AAAA,sEAAsE;AAEtE,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAC/B,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAQtC,MAAM,CAAC,OAAO,UAAU,MAAM;IAC1B,IAAI,OAAO,GAAG,QAAQ,CAAC;IAEvB,SAAS,GAAG;;QACR,MAAM,CAAC,GAAG,SAAS,CAAC,MAAM,CAAC;QAC3B,IAAI,CAAC,KAAK,CAAC,EAAE;YACT,OAAO,EAAE,CAAC;SACb;QACD,MAAM,CAAC,GAAG,MAAA,GAAG,CAAC,SAAS,EAAE,YAAY,CAAC,mCAAI,CAAC,CAAC;QAE5C,MAAM,IAAI,GAAG,IAAI,KAAK,CAAC,CAAC,CAAC,CAAC;QAC1B,KAAK,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,GAAI;YACxB,iDAAiD;YACjD,KAAK,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,GAAG,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,IAAI,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,GAAI;gBACxD,GAAG,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;aAC5B;YAED,aAAa;YACb,IAAI,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;SAC1C;QACD,OAAO,IAAI,CAAC;IAChB,CAAC;IACD,SAAS,YAAY,CAAC,CAAQ;QAC1B,OAAO,CAAC,CAAC,MAAM,CAAC;IACpB,CAAC;IACD,GAAG,CAAC,OAAO,GAAG,UAAU,CAAM;QAC1B,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE;YACnB,OAAO,OAAO,CAAC;SAClB;QACD,OAAO,GAAG,CAAC,CAAC;QACZ,OAAO,GAAG,CAAC;IACf,CAAC,CAAC;IACF,OAAO,GAAU,CAAC;AACtB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tradingaction/indicators",
|
|
3
|
+
"version": "2.0.1",
|
|
4
|
+
"description": "Indicators for react-financial-charts",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"main": "./lib/index.js",
|
|
9
|
+
"typings": "./lib/index.d.ts",
|
|
10
|
+
"files": [
|
|
11
|
+
"lib",
|
|
12
|
+
"src"
|
|
13
|
+
],
|
|
14
|
+
"sideEffects": false,
|
|
15
|
+
"author": "Reactive Markets",
|
|
16
|
+
"keywords": [
|
|
17
|
+
"charts",
|
|
18
|
+
"charting",
|
|
19
|
+
"stockcharts",
|
|
20
|
+
"finance",
|
|
21
|
+
"financial",
|
|
22
|
+
"finance-chart",
|
|
23
|
+
"react",
|
|
24
|
+
"d3"
|
|
25
|
+
],
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "https://github.com/reactivemarkets/react-financial-charts.git"
|
|
30
|
+
},
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "https://github.com/reactivemarkets/react-financial-charts/issues"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "npm run clean && npm run compile",
|
|
36
|
+
"clean": "rimraf lib",
|
|
37
|
+
"compile": "tsc -p tsconfig.json",
|
|
38
|
+
"watch": "tsc -p tsconfig.json --watch --preserveWatchOutput"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"d3-array": "^2.9.1",
|
|
42
|
+
"d3-scale": "^3.2.3"
|
|
43
|
+
},
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"react": "^16.0.0 || ^17.0.0 || ^18.0.0",
|
|
46
|
+
"react-dom": "^16.0.0 || ^17.0.0 || ^18.0.0"
|
|
47
|
+
},
|
|
48
|
+
"gitHead": "4bc60a26c5958208e3dc87261d2588cece29dfa2"
|
|
49
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { sum } from "d3-array";
|
|
2
|
+
import { slidingWindow } from "../utils";
|
|
3
|
+
import { ATR as defaultOptions } from "./defaultOptionsForComputation";
|
|
4
|
+
|
|
5
|
+
export interface ATROptions {
|
|
6
|
+
readonly windowSize: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface ATRSource {
|
|
10
|
+
readonly close: number;
|
|
11
|
+
readonly high: number;
|
|
12
|
+
readonly low: number;
|
|
13
|
+
readonly open: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface ATRCalculator {
|
|
17
|
+
(data: any[]): any;
|
|
18
|
+
undefinedLength(): number;
|
|
19
|
+
options(): ATROptions;
|
|
20
|
+
options(newOptions: ATROptions): ATRCalculator;
|
|
21
|
+
source(): (d: any) => ATRSource;
|
|
22
|
+
source(newSource: (d: any) => ATRSource): ATRCalculator;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export default function () {
|
|
26
|
+
let options: ATROptions = defaultOptions;
|
|
27
|
+
let source = (d: any) => ({ open: d.open, high: d.high, low: d.low, close: d.close });
|
|
28
|
+
|
|
29
|
+
const calculator = (data: any[]) => {
|
|
30
|
+
const { windowSize } = options;
|
|
31
|
+
|
|
32
|
+
const trueRangeAlgorithm = slidingWindow()
|
|
33
|
+
.windowSize(2)
|
|
34
|
+
.source(source)
|
|
35
|
+
.undefinedValue((d: any) => d.high - d.low) // the first TR value is simply the High minus the Low
|
|
36
|
+
.accumulator((values: any) => {
|
|
37
|
+
const prev = values[0];
|
|
38
|
+
const d = values[1];
|
|
39
|
+
return Math.max(d.high - d.low, d.high - prev.close, d.low - prev.close);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
let prevATR: number | undefined;
|
|
43
|
+
|
|
44
|
+
const atrAlgorithm = slidingWindow()
|
|
45
|
+
.skipInitial(1) // trueRange starts from index 1 so ATR starts from 1
|
|
46
|
+
.windowSize(windowSize)
|
|
47
|
+
.accumulator((values: any[]) => {
|
|
48
|
+
const tr = values[values.length - 1];
|
|
49
|
+
const atr =
|
|
50
|
+
prevATR !== undefined ? (prevATR * (windowSize - 1) + tr) / windowSize : sum(values) / windowSize;
|
|
51
|
+
|
|
52
|
+
prevATR = atr;
|
|
53
|
+
return atr;
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
const newData = atrAlgorithm(trueRangeAlgorithm(data));
|
|
57
|
+
|
|
58
|
+
return newData;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
calculator.undefinedLength = () => {
|
|
62
|
+
const { windowSize } = options;
|
|
63
|
+
|
|
64
|
+
return windowSize - 1;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
calculator.options = (newOptions?: ATROptions) => {
|
|
68
|
+
if (newOptions === undefined) {
|
|
69
|
+
return options;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
options = { ...defaultOptions, ...newOptions };
|
|
73
|
+
|
|
74
|
+
return calculator;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
calculator.source = (newSource?: (d: any) => ATRSource) => {
|
|
78
|
+
if (newSource === undefined) {
|
|
79
|
+
return source;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
source = newSource;
|
|
83
|
+
|
|
84
|
+
return calculator;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
return calculator as ATRCalculator;
|
|
88
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/*
|
|
2
|
+
https://github.com/ScottLogic/d3fc/blob/master/src/indicator/algorithm/calculator/bollingerBands.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
|
+
|
|
27
|
+
import { deviation, mean } from "d3-array";
|
|
28
|
+
import { path, slidingWindow, zipper } from "../utils";
|
|
29
|
+
import ema from "./ema";
|
|
30
|
+
import { BollingerBand as defaultOptions } from "./defaultOptionsForComputation";
|
|
31
|
+
|
|
32
|
+
export interface BollingerBandOptions {
|
|
33
|
+
readonly windowSize: number;
|
|
34
|
+
readonly sourcePath: string;
|
|
35
|
+
readonly multiplier: number;
|
|
36
|
+
readonly movingAverageType: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
interface BollingerBandCalculator {
|
|
40
|
+
(data: any[]): any;
|
|
41
|
+
undefinedLength(): number;
|
|
42
|
+
options(): BollingerBandOptions;
|
|
43
|
+
options(newOptions: BollingerBandOptions): BollingerBandCalculator;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export default function () {
|
|
47
|
+
let options: BollingerBandOptions = defaultOptions;
|
|
48
|
+
|
|
49
|
+
const calculator = (data: any[]) => {
|
|
50
|
+
const { windowSize, multiplier, movingAverageType, sourcePath } = options;
|
|
51
|
+
|
|
52
|
+
const source = path(sourcePath);
|
|
53
|
+
|
|
54
|
+
const meanAlgorithm =
|
|
55
|
+
movingAverageType === "ema"
|
|
56
|
+
? ema().options({ windowSize, sourcePath })
|
|
57
|
+
: slidingWindow()
|
|
58
|
+
.windowSize(windowSize)
|
|
59
|
+
.accumulator((values: any) => mean(values))
|
|
60
|
+
.sourcePath(sourcePath);
|
|
61
|
+
|
|
62
|
+
const bollingerBandAlgorithm = slidingWindow()
|
|
63
|
+
.windowSize(windowSize)
|
|
64
|
+
.accumulator((values: any[]) => {
|
|
65
|
+
const avg = values[values.length - 1].mean;
|
|
66
|
+
const stdDev = deviation<any>(values, (each) => source(each.datum));
|
|
67
|
+
if (stdDev === undefined) {
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
top: avg + multiplier * stdDev,
|
|
73
|
+
middle: avg,
|
|
74
|
+
bottom: avg - multiplier * stdDev,
|
|
75
|
+
};
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
const zip = zipper().combine((datum: any, meanValue: number) => ({ datum, mean: meanValue }));
|
|
79
|
+
|
|
80
|
+
const tuples = zip(data, meanAlgorithm(data));
|
|
81
|
+
|
|
82
|
+
return bollingerBandAlgorithm(tuples);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
calculator.undefinedLength = () => {
|
|
86
|
+
const { windowSize } = options;
|
|
87
|
+
|
|
88
|
+
return windowSize - 1;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
calculator.options = (newOptions?: BollingerBandOptions) => {
|
|
92
|
+
if (newOptions === undefined) {
|
|
93
|
+
return options;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
options = { ...defaultOptions, ...newOptions };
|
|
97
|
+
|
|
98
|
+
return calculator;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
return calculator as BollingerBandCalculator;
|
|
102
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { slidingWindow } from "../utils";
|
|
2
|
+
import { Change as defaultOptions } from "./defaultOptionsForComputation";
|
|
3
|
+
|
|
4
|
+
export interface ChangeOptions {
|
|
5
|
+
readonly sourcePath: string;
|
|
6
|
+
readonly basePath: string;
|
|
7
|
+
readonly mainKeys: string[];
|
|
8
|
+
readonly compareKeys: string[];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface ChangeCalculator {
|
|
12
|
+
(data: any[]): any;
|
|
13
|
+
undefinedLength(): number;
|
|
14
|
+
options(): ChangeOptions;
|
|
15
|
+
options(newOptions: ChangeOptions): ChangeCalculator;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export default function () {
|
|
19
|
+
let options: ChangeOptions = defaultOptions;
|
|
20
|
+
|
|
21
|
+
const calculator = (data: any[]) => {
|
|
22
|
+
const { sourcePath } = options;
|
|
23
|
+
|
|
24
|
+
const algo = slidingWindow()
|
|
25
|
+
.windowSize(2)
|
|
26
|
+
.sourcePath(sourcePath)
|
|
27
|
+
.accumulator(([prev, curr]: any) => {
|
|
28
|
+
const absoluteChange = curr - prev;
|
|
29
|
+
const percentChange = (absoluteChange * 100) / prev;
|
|
30
|
+
return { absoluteChange, percentChange };
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const newData = algo(data);
|
|
34
|
+
|
|
35
|
+
return newData;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
calculator.undefinedLength = () => {
|
|
39
|
+
return 1;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
calculator.options = (newOptions?: ChangeOptions) => {
|
|
43
|
+
if (newOptions === undefined) {
|
|
44
|
+
return options;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
options = { ...defaultOptions, ...newOptions };
|
|
48
|
+
|
|
49
|
+
return calculator;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
return calculator as ChangeCalculator;
|
|
53
|
+
}
|