wenay-common 1.0.185 → 1.0.188
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/lib/Common/Decorator.d.ts +8 -0
- package/lib/Common/Decorator.js +19 -0
- package/lib/Common/Math.d.ts +9 -2
- package/lib/Common/Math.js +45 -7
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare function Decorator<T extends (...arg: any[]) => any>(func: T, option?: {
|
|
2
|
+
parameters?: (...a: Parameters<T>) => any;
|
|
3
|
+
parametersModifier?: (...a: Parameters<T>) => Parameters<T>;
|
|
4
|
+
parametersAfter?: (...a: Parameters<T>) => any;
|
|
5
|
+
result?: (a: ReturnType<T>) => any;
|
|
6
|
+
resultModifier?: (a: ReturnType<T>) => ReturnType<T>;
|
|
7
|
+
}): (...arg: Parameters<T>) => ReturnType<T>;
|
|
8
|
+
export declare function Transformer<T extends (...arg: any[]) => any, R>(func: T, transformer: (a: [params: Parameters<T>, result: ReturnType<T>]) => R): (...arg: Parameters<T>) => R;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Decorator = Decorator;
|
|
4
|
+
exports.Transformer = Transformer;
|
|
5
|
+
function Decorator(func, option) {
|
|
6
|
+
return (...arg) => {
|
|
7
|
+
option?.parameters?.(...arg);
|
|
8
|
+
const r = func(...(option?.parametersModifier?.(...arg) ?? arg));
|
|
9
|
+
option?.parametersAfter?.(...arg);
|
|
10
|
+
option?.result?.(r);
|
|
11
|
+
return option?.resultModifier?.(r) ?? r;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
function Transformer(func, transformer) {
|
|
15
|
+
return (...arg) => {
|
|
16
|
+
const r = func(...arg);
|
|
17
|
+
return transformer([arg, r]);
|
|
18
|
+
};
|
|
19
|
+
}
|
package/lib/Common/Math.d.ts
CHANGED
|
@@ -9,20 +9,27 @@ export declare function CorrelationRollingByBuffer(data: tCorrelationByBuffer):
|
|
|
9
9
|
corr: number;
|
|
10
10
|
buffer?: tCorrBuffer;
|
|
11
11
|
};
|
|
12
|
+
corr2(a1: number, a2: number, k1: any, k2: any): {
|
|
13
|
+
corr: number;
|
|
14
|
+
buffer?: tCorrBuffer;
|
|
15
|
+
};
|
|
12
16
|
};
|
|
13
17
|
type tCorrBuffer = {
|
|
14
18
|
sum1: number;
|
|
15
19
|
sum2: number;
|
|
20
|
+
step: number;
|
|
16
21
|
pow1: number;
|
|
17
22
|
pow2: number;
|
|
18
23
|
last1: number;
|
|
19
24
|
last2: number;
|
|
25
|
+
first1: number;
|
|
26
|
+
first2: number;
|
|
20
27
|
mulSum: number;
|
|
21
28
|
};
|
|
22
29
|
export declare function CorrelationFunc(d1: number[], d2: number[], setting: {
|
|
23
|
-
rolling
|
|
30
|
+
rolling: number;
|
|
24
31
|
endBar?: number;
|
|
25
|
-
buffer
|
|
32
|
+
buffer: tCorrBuffer | null;
|
|
26
33
|
}): {
|
|
27
34
|
corr: number;
|
|
28
35
|
buffer?: tCorrBuffer;
|
package/lib/Common/Math.js
CHANGED
|
@@ -4,8 +4,9 @@ exports.CorrelationRollingByBuffer = CorrelationRollingByBuffer;
|
|
|
4
4
|
exports.CorrelationFunc = CorrelationFunc;
|
|
5
5
|
function CorrelationRollingByBuffer(data) {
|
|
6
6
|
let setting = { ...data };
|
|
7
|
+
let total = 0;
|
|
7
8
|
const map = new Map();
|
|
8
|
-
const defBuf = () => ({ last1: 0, last2: 0, mulSum: 0, pow1: 0, pow2: 0, sum2: 0, sum1: 0 });
|
|
9
|
+
const defBuf = () => ({ last1: 0, last2: 0, mulSum: 0, pow1: 0, pow2: 0, sum2: 0, sum1: 0, step: 0, first1: 0, first2: 0, total });
|
|
9
10
|
const getBuffer = (key1, key2) => {
|
|
10
11
|
let a1 = map.get(key1);
|
|
11
12
|
if (!a1)
|
|
@@ -15,17 +16,44 @@ function CorrelationRollingByBuffer(data) {
|
|
|
15
16
|
a1.set(key2, a2 = defBuf());
|
|
16
17
|
return a2;
|
|
17
18
|
};
|
|
19
|
+
const map2 = new Map();
|
|
20
|
+
const getBuffer2 = (key1, key2) => {
|
|
21
|
+
let a1 = map2.get(key1);
|
|
22
|
+
if (!a1)
|
|
23
|
+
map2.set(key1, a1 = new Map());
|
|
24
|
+
let a2 = a1.get(key2);
|
|
25
|
+
if (!a2)
|
|
26
|
+
a1.set(key2, a2 = { ar1: [], ar2: [] });
|
|
27
|
+
return a2;
|
|
28
|
+
};
|
|
18
29
|
let index = 0;
|
|
19
30
|
const clear = () => {
|
|
20
31
|
index = 0;
|
|
21
32
|
map.clear();
|
|
22
33
|
};
|
|
34
|
+
function checkSize(e) {
|
|
35
|
+
const a = e.length - setting.max;
|
|
36
|
+
if (a > 0)
|
|
37
|
+
e.slice(0, a);
|
|
38
|
+
}
|
|
23
39
|
return ({
|
|
24
40
|
init(data) { setting = { ...data }; },
|
|
25
41
|
clear(data) { clear(); data && this.init(data); },
|
|
26
42
|
corr(arr1, arr2, key1, key2) {
|
|
27
|
-
|
|
28
|
-
|
|
43
|
+
let [k1, k2] = [key1 ?? arr1, key2 ?? arr2];
|
|
44
|
+
const buffer = setting.bufferOn ? getBuffer(k1, k2) : null;
|
|
45
|
+
const a = CorrelationFunc(arr1, arr2, { rolling: setting.max, buffer });
|
|
46
|
+
return a;
|
|
47
|
+
},
|
|
48
|
+
corr2(a1, a2, k1, k2) {
|
|
49
|
+
const buffer = setting.bufferOn ? getBuffer(k1, k2) : null;
|
|
50
|
+
const l = getBuffer2(k1, k2);
|
|
51
|
+
l.ar1.push(a1);
|
|
52
|
+
l.ar2.push(a2);
|
|
53
|
+
checkSize(l.ar1);
|
|
54
|
+
checkSize(l.ar2);
|
|
55
|
+
const a = CorrelationFunc(l.ar1, l.ar2, { rolling: setting.max, buffer });
|
|
56
|
+
return a;
|
|
29
57
|
}
|
|
30
58
|
});
|
|
31
59
|
}
|
|
@@ -39,9 +67,12 @@ function CorrelationFunc(d1, d2, setting) {
|
|
|
39
67
|
const start = (rolling) ? (n - rolling > 0 ? n - rolling : 0) : 0;
|
|
40
68
|
const end = n;
|
|
41
69
|
const c = n - start;
|
|
70
|
+
let step = (setting.buffer?.step ?? 0);
|
|
71
|
+
if (rolling && step > rolling)
|
|
72
|
+
step = rolling;
|
|
42
73
|
const buffer = setting.buffer;
|
|
43
74
|
const [end1, end2] = [d1[end - 1], d2[end - 1]];
|
|
44
|
-
|
|
75
|
+
let [last1, last2] = [d1[start], d2[start]];
|
|
45
76
|
let sum1, sum2, pow1, pow2, mulSum;
|
|
46
77
|
if (buffer && 1) {
|
|
47
78
|
sum1 = buffer.sum1 - buffer.last1 + end1;
|
|
@@ -49,6 +80,11 @@ function CorrelationFunc(d1, d2, setting) {
|
|
|
49
80
|
pow1 = buffer.pow1 - pow(buffer.last1, 2) + pow(end1, 2);
|
|
50
81
|
pow2 = buffer.pow2 - pow(buffer.last2, 2) + pow(end2, 2);
|
|
51
82
|
mulSum = buffer.mulSum - (buffer.last1 * buffer.last2) + (end1 * end2);
|
|
83
|
+
if (buffer.step < setting.rolling) {
|
|
84
|
+
buffer.step++;
|
|
85
|
+
step++;
|
|
86
|
+
last1 = last2 = 0;
|
|
87
|
+
}
|
|
52
88
|
}
|
|
53
89
|
else {
|
|
54
90
|
const add = (a, b) => a + b;
|
|
@@ -61,8 +97,8 @@ function CorrelationFunc(d1, d2, setting) {
|
|
|
61
97
|
}
|
|
62
98
|
if (isNaN(sum1) || !sum1 ||
|
|
63
99
|
isNaN(sum2) || !sum2 ||
|
|
64
|
-
isNaN(last1) ||
|
|
65
|
-
isNaN(last2) ||
|
|
100
|
+
isNaN(last1) ||
|
|
101
|
+
isNaN(last2) ||
|
|
66
102
|
isNaN(mulSum) || !mulSum ||
|
|
67
103
|
isNaN(pow2) || !pow2 ||
|
|
68
104
|
isNaN(pow1) || !pow1) {
|
|
@@ -70,7 +106,9 @@ function CorrelationFunc(d1, d2, setting) {
|
|
|
70
106
|
(async () => { throw "nanCor"; })();
|
|
71
107
|
}
|
|
72
108
|
const dense = sqrt((pow1 - pow(sum1, 2) / c) * (pow2 - pow(sum2, 2) / c));
|
|
73
|
-
const buf = { sum1, sum2, last1, last2, mulSum, pow2, pow1 };
|
|
109
|
+
const buf = { sum1, sum2, last1, last2, mulSum, pow2, pow1, step, first2: 0, first1: 0 };
|
|
110
|
+
if (buffer)
|
|
111
|
+
Object.assign(buffer, buf);
|
|
74
112
|
if (dense == 0)
|
|
75
113
|
return { corr: 0 };
|
|
76
114
|
const result = (mulSum - (sum1 * sum2 / c)) / dense;
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -29,6 +29,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
29
29
|
exports.LoadCandles = exports.Params = exports.List = exports.Math = exports.ListNodeAnd = exports.Color = exports.Time = exports.Common = exports.BaseTypes = void 0;
|
|
30
30
|
exports.test = test;
|
|
31
31
|
__exportStar(require("./Common/node_console"), exports);
|
|
32
|
+
__exportStar(require("./Common/Decorator"), exports);
|
|
32
33
|
__exportStar(require("./Common/BaseTypes"), exports);
|
|
33
34
|
__exportStar(require("./Common/ByteStream"), exports);
|
|
34
35
|
__exportStar(require("./Common/Color"), exports);
|