@steerprotocol/strategy-utils 2.0.0 → 2.0.2
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.
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
export class SlidingWindow<T> {
|
|
2
|
+
private data: Array<T>;
|
|
3
|
+
private windowSize: i32;
|
|
4
|
+
private formula: (window: Array<T>) => T;
|
|
5
|
+
private cursor: i32;
|
|
6
|
+
|
|
7
|
+
constructor(windowSize: i32, formula: (window: Array<T>) => T) {
|
|
8
|
+
if (windowSize < 1) {
|
|
9
|
+
throw new Error("windowSize must be greater than 0");
|
|
10
|
+
}
|
|
11
|
+
if (formula === null) {
|
|
12
|
+
throw new Error("formula function must be provided");
|
|
13
|
+
}
|
|
14
|
+
this.windowSize = windowSize;
|
|
15
|
+
this.formula = formula;
|
|
16
|
+
this.data = new Array<T>(windowSize);
|
|
17
|
+
this.cursor = 0;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
addValue(value: T): void {
|
|
21
|
+
this.data[this.cursor] = value;
|
|
22
|
+
this.cursor = (this.cursor + 1) % this.windowSize;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
getLastValue(): T {
|
|
26
|
+
let index = this.cursor === 0 ? this.windowSize - 1 : this.cursor - 1;
|
|
27
|
+
return this.data[index];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
clear(): void {
|
|
31
|
+
this.data.fill(null);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
setWindowSize(size: i32): void {
|
|
35
|
+
if (size < 1) {
|
|
36
|
+
throw new Error("windowSize must be greater than 0");
|
|
37
|
+
}
|
|
38
|
+
this.windowSize = size;
|
|
39
|
+
this.data = new Array<T>(size);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
getWindow(): Array<T> {
|
|
43
|
+
let result = new Array<T>(this.windowSize);
|
|
44
|
+
for (let i = 0; i < this.windowSize; i++) {
|
|
45
|
+
let index = (this.cursor + i) % this.windowSize;
|
|
46
|
+
result[i] = this.data[index];
|
|
47
|
+
}
|
|
48
|
+
return result;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
getFormulaResult(): T {
|
|
52
|
+
let window = this.getWindow();
|
|
53
|
+
return this.formula(window);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import * as JSON from '@serial-as/json'
|
|
2
|
-
import {console} from '../console'
|
|
3
2
|
|
|
4
3
|
@serializable
|
|
5
4
|
export class Candle{
|
|
@@ -34,8 +33,5 @@ export function parsePrices(_data: string): Array<Candle> {
|
|
|
34
33
|
export function parseCandles(_data: string): Array<Candle> {
|
|
35
34
|
// Parse an object using the JSON object
|
|
36
35
|
let parsed: Array<Candle> = JSON.parse<Array<Candle>>(_data);
|
|
37
|
-
|
|
38
|
-
console.log('parsed: ' + parsed.toString())
|
|
39
|
-
|
|
40
36
|
return parsed;
|
|
41
37
|
}
|