-
-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathstrategyStats.ts
More file actions
131 lines (113 loc) · 3.03 KB
/
Copy pathstrategyStats.ts
File metadata and controls
131 lines (113 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright (c) 2022 Onur Cinar. All Rights Reserved.
// https://github.com/cinar/indicatorts
import { CompanyResult } from './companyResult';
import { StrategyInfo } from './strategyInfo';
import { StrategyResult } from './strategyResult';
/**
* Strategy stats.
*/
export interface StrategyStats {
strategyInfo: StrategyInfo;
score: number;
minGain: number;
maxGain: number;
averageGain: number;
}
/**
* New strategy stats from strategy result.
*
* @param result strategy result.
* @return strategy stats.
*/
function newStrategyStats(result: StrategyResult): StrategyStats {
return {
strategyInfo: result.info,
score: 1,
minGain: result.gain,
maxGain: result.gain,
averageGain: result.gain,
};
}
/**
* Updates strategy stats using strategy result.
*
* @param stats strategy stats.
* @param result strategy result.
*/
function updateStrategyStats(stats: StrategyStats, result: StrategyResult) {
stats.score++;
stats.minGain = Math.min(stats.minGain, result.gain);
stats.maxGain = Math.max(stats.maxGain, result.gain);
stats.averageGain = (stats.averageGain + result.gain) / 2;
}
/**
* Computes the strategy stats.
*
* @param companyResults company results.
* @return stats array.
*/
export function computeStrategyStats(
companyResults: CompanyResult[]
): StrategyStats[] {
const statsMap = new Map<string, StrategyStats>();
for (const companyResult of companyResults) {
const strategyResult = companyResult.strategyResults[0];
const strategyStats = statsMap.get(strategyResult.info.name);
if (strategyStats !== undefined) {
updateStrategyStats(strategyStats, strategyResult);
} else {
statsMap.set(strategyResult.info.name, newStrategyStats(strategyResult));
}
}
const statsArray = Array.from(statsMap.values());
statsArray.sort((a, b) => b.score - a.score);
return statsArray;
}
/**
* Strategy stats sort by.
*/
export enum StrategyStatsSortBy {
STRATEGY,
SCORE,
MIN,
MAX,
AVERAGE,
}
/**
* Sorts the strategy stats.
*
* @param strategyStats strategy stats.
* @param sortBy sort by.
* @param ascending ascending toggle.
* @return sorted stats.
*/
export function sortStrategyStats(
strategyStats: StrategyStats[],
sortBy: StrategyStatsSortBy,
ascending: boolean
): StrategyStats[] {
let sorted: StrategyStats[] = [];
switch (sortBy) {
case StrategyStatsSortBy.STRATEGY:
sorted = strategyStats.sort((a, b) =>
a.strategyInfo.name.localeCompare(b.strategyInfo.name)
);
break;
case StrategyStatsSortBy.SCORE:
sorted = strategyStats.sort((a, b) => a.score - b.score);
break;
case StrategyStatsSortBy.MIN:
sorted = strategyStats.sort((a, b) => a.minGain - b.minGain);
break;
case StrategyStatsSortBy.MAX:
sorted = strategyStats.sort((a, b) => a.maxGain - b.maxGain);
break;
case StrategyStatsSortBy.AVERAGE:
sorted = strategyStats.sort((a, b) => a.averageGain - b.averageGain);
break;
}
if (!ascending) {
sorted = sorted.reverse();
}
return sorted;
}