@barchart/portfolio-api-common 1.0.27 → 1.0.32
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,71 @@
|
|
|
1
|
+
const array = require('@barchart/common-js/lang/array'),
|
|
2
|
+
assert = require('@barchart/common-js/lang/assert'),
|
|
3
|
+
Decimal = require('@barchart/common-js/lang/Decimal'),
|
|
4
|
+
formatter = require('@barchart/common-js/lang/formatter');
|
|
5
|
+
|
|
6
|
+
module.exports = (() => {
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Formats Position Summary records into groups based on instrument type
|
|
10
|
+
*
|
|
11
|
+
*/
|
|
12
|
+
class PositionSummaryFormatter{
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The formatter
|
|
16
|
+
*
|
|
17
|
+
* @param {Array} summaries
|
|
18
|
+
* @returns {Object}
|
|
19
|
+
*/
|
|
20
|
+
static format(summaries) {
|
|
21
|
+
assert.argumentIsRequired(summaries, 'summaries', Object);
|
|
22
|
+
|
|
23
|
+
const summaryGroups = array.groupBy(summaries, summary => summary.instrument.type.code);
|
|
24
|
+
|
|
25
|
+
const total = {
|
|
26
|
+
start: Decimal.ZERO,
|
|
27
|
+
change: Decimal.ZERO,
|
|
28
|
+
end: Decimal.ZERO
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const formattedSummaryGroups = Object.keys(summaryGroups).map((group) => {
|
|
32
|
+
const positions = summaryGroups[group];
|
|
33
|
+
|
|
34
|
+
let start = Decimal.ZERO;
|
|
35
|
+
let end = Decimal.ZERO;
|
|
36
|
+
|
|
37
|
+
positions.map((position) => {
|
|
38
|
+
start = start.add(position.start.value);
|
|
39
|
+
end = end.add(position.end.value);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const change = end.subtract(start);
|
|
43
|
+
|
|
44
|
+
total.start = total.start.add(start);
|
|
45
|
+
total.change = total.change.add(change);
|
|
46
|
+
total.end = total.end.add(end);
|
|
47
|
+
|
|
48
|
+
return summaryGroups[group] = {
|
|
49
|
+
name: group,
|
|
50
|
+
start: formatNumber(start.toFloat()),
|
|
51
|
+
end: formatNumber(end.toFloat()),
|
|
52
|
+
change: formatNumber(change.toFloat())
|
|
53
|
+
};
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
Object.keys(total).map(key => total[key] = formatNumber(total[key].toFloat()));
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
groups: formattedSummaryGroups,
|
|
60
|
+
total
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const formatNumber = (float) => {
|
|
66
|
+
return formatter.numberToString(float, 2, ',');
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
return PositionSummaryFormatter;
|
|
70
|
+
|
|
71
|
+
})();
|
|
@@ -272,6 +272,11 @@ module.exports = (() => {
|
|
|
272
272
|
.withField('portfolio', DataType.STRING)
|
|
273
273
|
.withField('position', DataType.STRING)
|
|
274
274
|
.withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
|
|
275
|
+
.withField('instrument.name', DataType.STRING, true)
|
|
276
|
+
.withField('instrument.type', DataType.STRING, true)
|
|
277
|
+
.withField('instrument.currency', DataType.forEnum(Currency, 'Currency'), true)
|
|
278
|
+
.withField('instrument.symbol.barchart', DataType.STRING, true)
|
|
279
|
+
.withField('instrument.symbol.display', DataType.STRING, true)
|
|
275
280
|
.withField('date', DataType.DAY)
|
|
276
281
|
.withField('price', DataType.DECIMAL)
|
|
277
282
|
.withField('quantity', DataType.DECIMAL)
|
|
@@ -283,11 +288,6 @@ module.exports = (() => {
|
|
|
283
288
|
.withField('portfolio', DataType.STRING)
|
|
284
289
|
.withField('position', DataType.STRING)
|
|
285
290
|
.withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
|
|
286
|
-
.withField('instrument.name', DataType.STRING, true)
|
|
287
|
-
.withField('instrument.type', DataType.STRING, true)
|
|
288
|
-
.withField('instrument.currency', DataType.forEnum(Currency, 'Currency'), true)
|
|
289
|
-
.withField('instrument.symbol.barchart', DataType.STRING, true)
|
|
290
|
-
.withField('instrument.symbol.display', DataType.STRING, true)
|
|
291
291
|
.withField('date', DataType.DAY)
|
|
292
292
|
.withField('price', DataType.DECIMAL)
|
|
293
293
|
.withField('quantity', DataType.DECIMAL)
|
|
@@ -301,8 +301,7 @@ module.exports = (() => {
|
|
|
301
301
|
.withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
|
|
302
302
|
.withField('date', DataType.DAY)
|
|
303
303
|
.withField('rate', DataType.DECIMAL)
|
|
304
|
-
.withField('
|
|
305
|
-
.withField('effective', DataType.DAY, true)
|
|
304
|
+
.withField('effective', DataType.DAY)
|
|
306
305
|
.withField('fee', DataType.DECIMAL, true)
|
|
307
306
|
.schema
|
|
308
307
|
);
|
|
@@ -313,8 +312,7 @@ module.exports = (() => {
|
|
|
313
312
|
.withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
|
|
314
313
|
.withField('date', DataType.DAY)
|
|
315
314
|
.withField('rate', DataType.DECIMAL)
|
|
316
|
-
.withField('
|
|
317
|
-
.withField('effective', DataType.DAY, true)
|
|
315
|
+
.withField('effective', DataType.DAY)
|
|
318
316
|
.withField('price', DataType.DECIMAL)
|
|
319
317
|
.withField('fee', DataType.DECIMAL, true)
|
|
320
318
|
.schema
|
|
@@ -326,8 +324,7 @@ module.exports = (() => {
|
|
|
326
324
|
.withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
|
|
327
325
|
.withField('date', DataType.DAY)
|
|
328
326
|
.withField('rate', DataType.DECIMAL)
|
|
329
|
-
.withField('
|
|
330
|
-
.withField('effective', DataType.DAY, true)
|
|
327
|
+
.withField('effective', DataType.DAY)
|
|
331
328
|
.withField('price', DataType.DECIMAL)
|
|
332
329
|
.withField('fee', DataType.DECIMAL, true)
|
|
333
330
|
.schema
|
|
@@ -339,8 +336,7 @@ module.exports = (() => {
|
|
|
339
336
|
.withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
|
|
340
337
|
.withField('date', DataType.DAY)
|
|
341
338
|
.withField('rate', DataType.DECIMAL)
|
|
342
|
-
.withField('
|
|
343
|
-
.withField('effective', DataType.DAY, true)
|
|
339
|
+
.withField('effective', DataType.DAY)
|
|
344
340
|
.withField('fee', DataType.DECIMAL, true)
|
|
345
341
|
.schema
|
|
346
342
|
);
|
|
@@ -351,8 +347,8 @@ module.exports = (() => {
|
|
|
351
347
|
.withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
|
|
352
348
|
.withField('date', DataType.DAY)
|
|
353
349
|
.withField('rate', DataType.DECIMAL)
|
|
354
|
-
.withField('
|
|
355
|
-
.withField('
|
|
350
|
+
.withField('effective', DataType.DAY)
|
|
351
|
+
.withField('price', DataType.DECIMAL)
|
|
356
352
|
.withField('fee', DataType.DECIMAL, true)
|
|
357
353
|
.schema
|
|
358
354
|
);
|
|
@@ -364,7 +360,7 @@ module.exports = (() => {
|
|
|
364
360
|
.withField('date', DataType.DAY)
|
|
365
361
|
.withField('numerator', DataType.DECIMAL)
|
|
366
362
|
.withField('denominator', DataType.DECIMAL)
|
|
367
|
-
.withField('effective', DataType.DAY
|
|
363
|
+
.withField('effective', DataType.DAY)
|
|
368
364
|
.withField('fee', DataType.DECIMAL, true)
|
|
369
365
|
.schema
|
|
370
366
|
);
|