@barchart/portfolio-api-common 1.2.56 → 1.2.60
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.
|
@@ -26,10 +26,11 @@ module.exports = (() => {
|
|
|
26
26
|
* @public
|
|
27
27
|
* @static
|
|
28
28
|
* @param {Array.<Object>} transactions
|
|
29
|
+
* @param {Boolean=} strict
|
|
29
30
|
* @returns {Boolean}
|
|
30
31
|
*/
|
|
31
|
-
static validateOrder(transactions) {
|
|
32
|
-
return TransactionValidator.getInvalidIndex(transactions) < 0;
|
|
32
|
+
static validateOrder(transactions, strict) {
|
|
33
|
+
return TransactionValidator.getInvalidIndex(transactions, strict) < 0;
|
|
33
34
|
}
|
|
34
35
|
|
|
35
36
|
/**
|
|
@@ -77,14 +78,14 @@ module.exports = (() => {
|
|
|
77
78
|
* @public
|
|
78
79
|
* @static
|
|
79
80
|
* @param {Array.<Object>} transactions
|
|
81
|
+
* @param {Boolean=} strict
|
|
80
82
|
* @returns {Number}
|
|
81
83
|
*/
|
|
82
|
-
static getInvalidIndex(transactions) {
|
|
84
|
+
static getInvalidIndex(transactions, strict) {
|
|
83
85
|
assert.argumentIsArray(transactions, 'transactions');
|
|
86
|
+
assert.argumentIsOptional(strict, 'strict', Boolean);
|
|
84
87
|
|
|
85
|
-
return transactions.findIndex((t, i, a) => t.sequence !== (i + 1) || (i !== 0 && t.date.getIsBefore(a[ i - 1 ].date)));
|
|
86
|
-
|
|
87
|
-
//return transactions.findIndex((t, i, a) => t.sequence !== (i + 1) || (i !== 0 && t.date.getIsBefore(a[ i - 1 ].date)) || (i !== 0 && t.date.getIsEqual(a[i - 1].date) && t.type.sequence < a[i - 1].type.sequence));
|
|
88
|
+
return transactions.findIndex((t, i, a) => t.sequence !== (i + 1) || (i !== 0 && t.date.getIsBefore(a[ i - 1 ].date)) || (i !== 0 && is.boolean(strict) && strict && t.date.getIsEqual(a[i - 1].date) && t.type.sequence < a[i - 1].type.sequence));
|
|
88
89
|
}
|
|
89
90
|
|
|
90
91
|
/**
|
|
@@ -6,7 +6,6 @@ const array = require('@barchart/common-js/lang/array'),
|
|
|
6
6
|
Decimal = require('@barchart/common-js/lang/Decimal'),
|
|
7
7
|
DisposableStack = require('@barchart/common-js/collections/specialized/DisposableStack'),
|
|
8
8
|
Event = require('@barchart/common-js/messaging/Event'),
|
|
9
|
-
is = require('@barchart/common-js/lang/is'),
|
|
10
9
|
Rate = require('@barchart/common-js/lang/Rate'),
|
|
11
10
|
Tree = require('@barchart/common-js/collections/Tree');
|
|
12
11
|
|
|
@@ -311,7 +311,6 @@ module.exports = (() => {
|
|
|
311
311
|
}
|
|
312
312
|
|
|
313
313
|
function calculateStaticData(item) {
|
|
314
|
-
const portfolio = item.portfolio;
|
|
315
314
|
const position = item.position;
|
|
316
315
|
const snapshot = item.position.snapshot;
|
|
317
316
|
const previousSummaries = item.previousSummaries;
|
|
@@ -335,9 +334,9 @@ module.exports = (() => {
|
|
|
335
334
|
|
|
336
335
|
data.income = snapshot.income;
|
|
337
336
|
|
|
338
|
-
data.summaryTotalCurrent = calculateSummaryTotal(item.currentSummary);
|
|
339
|
-
data.summaryTotalPrevious = calculateSummaryTotal(getPreviousSummary(previousSummaries, 1));
|
|
340
|
-
data.summaryTotalPrevious2 = calculateSummaryTotal(getPreviousSummary(previousSummaries, 2));
|
|
337
|
+
data.summaryTotalCurrent = calculateSummaryTotal(item.currentSummary, getPreviousSummary(previousSummaries, 1));
|
|
338
|
+
data.summaryTotalPrevious = calculateSummaryTotal(getPreviousSummary(previousSummaries, 1), getPreviousSummary(previousSummaries, 2));
|
|
339
|
+
data.summaryTotalPrevious2 = calculateSummaryTotal(getPreviousSummary(previousSummaries, 2), getPreviousSummary(previousSummaries, 3));
|
|
341
340
|
|
|
342
341
|
if (snapshot.open.getIsZero()) {
|
|
343
342
|
data.basisPrice = Decimal.ZERO;
|
|
@@ -349,6 +348,7 @@ module.exports = (() => {
|
|
|
349
348
|
function calculatePriceData(item, price) {
|
|
350
349
|
const position = item.position;
|
|
351
350
|
const snapshot = item.position.snapshot;
|
|
351
|
+
const previousSummaries = item.previousSummaries;
|
|
352
352
|
|
|
353
353
|
const data = item._data;
|
|
354
354
|
|
|
@@ -408,25 +408,27 @@ module.exports = (() => {
|
|
|
408
408
|
data.unrealizedToday = unrealizedToday;
|
|
409
409
|
data.unrealizedTodayChange = unrealizedTodayChange;
|
|
410
410
|
|
|
411
|
-
const
|
|
411
|
+
const currentSummary = item.currentSummary;
|
|
412
|
+
|
|
413
|
+
if (currentSummary && position.instrument.type !== InstrumentType.CASH) {
|
|
414
|
+
const previousSummary = getPreviousSummary(previousSummaries, 1);
|
|
412
415
|
|
|
413
|
-
if (summary && position.instrument.type !== InstrumentType.CASH) {
|
|
414
416
|
let priceToUse;
|
|
415
417
|
|
|
416
418
|
if (price) {
|
|
417
419
|
priceToUse = price;
|
|
418
420
|
} else if (data.previousPrice) {
|
|
419
421
|
priceToUse = new Decimal(data.previousPrice);
|
|
420
|
-
} else if (!
|
|
421
|
-
priceToUse =
|
|
422
|
+
} else if (!currentSummary.end.open.getIsZero()) {
|
|
423
|
+
priceToUse = currentSummary.end.value.divide(currentSummary.end.open);
|
|
422
424
|
} else {
|
|
423
425
|
priceToUse = null;
|
|
424
426
|
}
|
|
425
427
|
|
|
426
428
|
if (priceToUse !== null) {
|
|
427
|
-
const period =
|
|
429
|
+
const period = currentSummary.period;
|
|
428
430
|
|
|
429
|
-
let unrealized =
|
|
431
|
+
let unrealized = currentSummary.end.open.multiply(priceToUse).add(currentSummary.end.basis);
|
|
430
432
|
let unrealizedChange;
|
|
431
433
|
|
|
432
434
|
if (data.unrealized !== null) {
|
|
@@ -435,7 +437,7 @@ module.exports = (() => {
|
|
|
435
437
|
unrealizedChange = Decimal.ZERO;
|
|
436
438
|
}
|
|
437
439
|
|
|
438
|
-
let summaryTotalCurrent = period.realized.add(period.income).add(unrealized);
|
|
440
|
+
let summaryTotalCurrent = period.realized.add(period.income).add(unrealized).subtract(previousSummary !== null ? previousSummary.period.unrealized : Decimal.ZERO);
|
|
439
441
|
let summaryTotalCurrentChange;
|
|
440
442
|
|
|
441
443
|
if (data.summaryTotalCurrent !== null) {
|
|
@@ -463,13 +465,13 @@ module.exports = (() => {
|
|
|
463
465
|
}
|
|
464
466
|
}
|
|
465
467
|
|
|
466
|
-
function calculateSummaryTotal(
|
|
468
|
+
function calculateSummaryTotal(currentSummary, previousSummary) {
|
|
467
469
|
let returnRef;
|
|
468
470
|
|
|
469
|
-
if (
|
|
470
|
-
const period =
|
|
471
|
+
if (currentSummary) {
|
|
472
|
+
const period = currentSummary.period;
|
|
471
473
|
|
|
472
|
-
returnRef = period.realized.add(period.income).add(period.unrealized);
|
|
474
|
+
returnRef = period.realized.add(period.income).add(period.unrealized).subtract(previousSummary !== null ? previousSummary.period.unrealized : Decimal.ZERO);
|
|
473
475
|
} else {
|
|
474
476
|
returnRef = Decimal.ZERO;
|
|
475
477
|
}
|
package/package.json
CHANGED
package/test/SpecRunner.js
CHANGED
|
@@ -1083,10 +1083,11 @@ module.exports = (() => {
|
|
|
1083
1083
|
* @public
|
|
1084
1084
|
* @static
|
|
1085
1085
|
* @param {Array.<Object>} transactions
|
|
1086
|
+
* @param {Boolean=} strict
|
|
1086
1087
|
* @returns {Boolean}
|
|
1087
1088
|
*/
|
|
1088
|
-
static validateOrder(transactions) {
|
|
1089
|
-
return TransactionValidator.getInvalidIndex(transactions) < 0;
|
|
1089
|
+
static validateOrder(transactions, strict) {
|
|
1090
|
+
return TransactionValidator.getInvalidIndex(transactions, strict) < 0;
|
|
1090
1091
|
}
|
|
1091
1092
|
|
|
1092
1093
|
/**
|
|
@@ -1134,14 +1135,14 @@ module.exports = (() => {
|
|
|
1134
1135
|
* @public
|
|
1135
1136
|
* @static
|
|
1136
1137
|
* @param {Array.<Object>} transactions
|
|
1138
|
+
* @param {Boolean=} strict
|
|
1137
1139
|
* @returns {Number}
|
|
1138
1140
|
*/
|
|
1139
|
-
static getInvalidIndex(transactions) {
|
|
1141
|
+
static getInvalidIndex(transactions, strict) {
|
|
1140
1142
|
assert.argumentIsArray(transactions, 'transactions');
|
|
1143
|
+
assert.argumentIsOptional(strict, 'strict', Boolean);
|
|
1141
1144
|
|
|
1142
|
-
return transactions.findIndex((t, i, a) => t.sequence !== (i + 1) || (i !== 0 && t.date.getIsBefore(a[ i - 1 ].date)));
|
|
1143
|
-
|
|
1144
|
-
//return transactions.findIndex((t, i, a) => t.sequence !== (i + 1) || (i !== 0 && t.date.getIsBefore(a[ i - 1 ].date)) || (i !== 0 && t.date.getIsEqual(a[i - 1].date) && t.type.sequence < a[i - 1].type.sequence));
|
|
1145
|
+
return transactions.findIndex((t, i, a) => t.sequence !== (i + 1) || (i !== 0 && t.date.getIsBefore(a[ i - 1 ].date)) || (i !== 0 && is.boolean(strict) && strict && t.date.getIsEqual(a[i - 1].date) && t.type.sequence < a[i - 1].type.sequence));
|
|
1145
1146
|
}
|
|
1146
1147
|
|
|
1147
1148
|
/**
|
|
@@ -1339,7 +1340,6 @@ const array = require('@barchart/common-js/lang/array'),
|
|
|
1339
1340
|
Decimal = require('@barchart/common-js/lang/Decimal'),
|
|
1340
1341
|
DisposableStack = require('@barchart/common-js/collections/specialized/DisposableStack'),
|
|
1341
1342
|
Event = require('@barchart/common-js/messaging/Event'),
|
|
1342
|
-
is = require('@barchart/common-js/lang/is'),
|
|
1343
1343
|
Rate = require('@barchart/common-js/lang/Rate'),
|
|
1344
1344
|
Tree = require('@barchart/common-js/collections/Tree');
|
|
1345
1345
|
|
|
@@ -2273,7 +2273,7 @@ module.exports = (() => {
|
|
|
2273
2273
|
return PositionContainer;
|
|
2274
2274
|
})();
|
|
2275
2275
|
|
|
2276
|
-
},{"./../data/PositionSummaryFrame":3,"./PositionGroup":7,"./PositionItem":8,"./definitions/PositionLevelDefinition":9,"./definitions/PositionLevelType":10,"./definitions/PositionTreeDefinition":11,"@barchart/common-js/collections/Tree":15,"@barchart/common-js/collections/sorting/ComparatorBuilder":16,"@barchart/common-js/collections/sorting/comparators":17,"@barchart/common-js/collections/specialized/DisposableStack":18,"@barchart/common-js/lang/Currency":20,"@barchart/common-js/lang/Decimal":22,"@barchart/common-js/lang/Rate":26,"@barchart/common-js/lang/array":28,"@barchart/common-js/lang/assert":29,"@barchart/common-js/
|
|
2276
|
+
},{"./../data/PositionSummaryFrame":3,"./PositionGroup":7,"./PositionItem":8,"./definitions/PositionLevelDefinition":9,"./definitions/PositionLevelType":10,"./definitions/PositionTreeDefinition":11,"@barchart/common-js/collections/Tree":15,"@barchart/common-js/collections/sorting/ComparatorBuilder":16,"@barchart/common-js/collections/sorting/comparators":17,"@barchart/common-js/collections/specialized/DisposableStack":18,"@barchart/common-js/lang/Currency":20,"@barchart/common-js/lang/Decimal":22,"@barchart/common-js/lang/Rate":26,"@barchart/common-js/lang/array":28,"@barchart/common-js/lang/assert":29,"@barchart/common-js/messaging/Event":35}],7:[function(require,module,exports){
|
|
2277
2277
|
const array = require('@barchart/common-js/lang/array'),
|
|
2278
2278
|
assert = require('@barchart/common-js/lang/assert'),
|
|
2279
2279
|
Currency = require('@barchart/common-js/lang/Currency'),
|
|
@@ -3447,7 +3447,6 @@ module.exports = (() => {
|
|
|
3447
3447
|
}
|
|
3448
3448
|
|
|
3449
3449
|
function calculateStaticData(item) {
|
|
3450
|
-
const portfolio = item.portfolio;
|
|
3451
3450
|
const position = item.position;
|
|
3452
3451
|
const snapshot = item.position.snapshot;
|
|
3453
3452
|
const previousSummaries = item.previousSummaries;
|
|
@@ -3471,9 +3470,9 @@ module.exports = (() => {
|
|
|
3471
3470
|
|
|
3472
3471
|
data.income = snapshot.income;
|
|
3473
3472
|
|
|
3474
|
-
data.summaryTotalCurrent = calculateSummaryTotal(item.currentSummary);
|
|
3475
|
-
data.summaryTotalPrevious = calculateSummaryTotal(getPreviousSummary(previousSummaries, 1));
|
|
3476
|
-
data.summaryTotalPrevious2 = calculateSummaryTotal(getPreviousSummary(previousSummaries, 2));
|
|
3473
|
+
data.summaryTotalCurrent = calculateSummaryTotal(item.currentSummary, getPreviousSummary(previousSummaries, 1));
|
|
3474
|
+
data.summaryTotalPrevious = calculateSummaryTotal(getPreviousSummary(previousSummaries, 1), getPreviousSummary(previousSummaries, 2));
|
|
3475
|
+
data.summaryTotalPrevious2 = calculateSummaryTotal(getPreviousSummary(previousSummaries, 2), getPreviousSummary(previousSummaries, 3));
|
|
3477
3476
|
|
|
3478
3477
|
if (snapshot.open.getIsZero()) {
|
|
3479
3478
|
data.basisPrice = Decimal.ZERO;
|
|
@@ -3485,6 +3484,7 @@ module.exports = (() => {
|
|
|
3485
3484
|
function calculatePriceData(item, price) {
|
|
3486
3485
|
const position = item.position;
|
|
3487
3486
|
const snapshot = item.position.snapshot;
|
|
3487
|
+
const previousSummaries = item.previousSummaries;
|
|
3488
3488
|
|
|
3489
3489
|
const data = item._data;
|
|
3490
3490
|
|
|
@@ -3544,25 +3544,27 @@ module.exports = (() => {
|
|
|
3544
3544
|
data.unrealizedToday = unrealizedToday;
|
|
3545
3545
|
data.unrealizedTodayChange = unrealizedTodayChange;
|
|
3546
3546
|
|
|
3547
|
-
const
|
|
3547
|
+
const currentSummary = item.currentSummary;
|
|
3548
|
+
|
|
3549
|
+
if (currentSummary && position.instrument.type !== InstrumentType.CASH) {
|
|
3550
|
+
const previousSummary = getPreviousSummary(previousSummaries, 1);
|
|
3548
3551
|
|
|
3549
|
-
if (summary && position.instrument.type !== InstrumentType.CASH) {
|
|
3550
3552
|
let priceToUse;
|
|
3551
3553
|
|
|
3552
3554
|
if (price) {
|
|
3553
3555
|
priceToUse = price;
|
|
3554
3556
|
} else if (data.previousPrice) {
|
|
3555
3557
|
priceToUse = new Decimal(data.previousPrice);
|
|
3556
|
-
} else if (!
|
|
3557
|
-
priceToUse =
|
|
3558
|
+
} else if (!currentSummary.end.open.getIsZero()) {
|
|
3559
|
+
priceToUse = currentSummary.end.value.divide(currentSummary.end.open);
|
|
3558
3560
|
} else {
|
|
3559
3561
|
priceToUse = null;
|
|
3560
3562
|
}
|
|
3561
3563
|
|
|
3562
3564
|
if (priceToUse !== null) {
|
|
3563
|
-
const period =
|
|
3565
|
+
const period = currentSummary.period;
|
|
3564
3566
|
|
|
3565
|
-
let unrealized =
|
|
3567
|
+
let unrealized = currentSummary.end.open.multiply(priceToUse).add(currentSummary.end.basis);
|
|
3566
3568
|
let unrealizedChange;
|
|
3567
3569
|
|
|
3568
3570
|
if (data.unrealized !== null) {
|
|
@@ -3571,7 +3573,7 @@ module.exports = (() => {
|
|
|
3571
3573
|
unrealizedChange = Decimal.ZERO;
|
|
3572
3574
|
}
|
|
3573
3575
|
|
|
3574
|
-
let summaryTotalCurrent = period.realized.add(period.income).add(unrealized);
|
|
3576
|
+
let summaryTotalCurrent = period.realized.add(period.income).add(unrealized).subtract(previousSummary !== null ? previousSummary.period.unrealized : Decimal.ZERO);
|
|
3575
3577
|
let summaryTotalCurrentChange;
|
|
3576
3578
|
|
|
3577
3579
|
if (data.summaryTotalCurrent !== null) {
|
|
@@ -3599,13 +3601,13 @@ module.exports = (() => {
|
|
|
3599
3601
|
}
|
|
3600
3602
|
}
|
|
3601
3603
|
|
|
3602
|
-
function calculateSummaryTotal(
|
|
3604
|
+
function calculateSummaryTotal(currentSummary, previousSummary) {
|
|
3603
3605
|
let returnRef;
|
|
3604
3606
|
|
|
3605
|
-
if (
|
|
3606
|
-
const period =
|
|
3607
|
+
if (currentSummary) {
|
|
3608
|
+
const period = currentSummary.period;
|
|
3607
3609
|
|
|
3608
|
-
returnRef = period.realized.add(period.income).add(period.unrealized);
|
|
3610
|
+
returnRef = period.realized.add(period.income).add(period.unrealized).subtract(previousSummary !== null ? previousSummary.period.unrealized : Decimal.ZERO);
|
|
3609
3611
|
} else {
|
|
3610
3612
|
returnRef = Decimal.ZERO;
|
|
3611
3613
|
}
|
|
@@ -17246,15 +17248,17 @@ describe('When validating transaction order', () => {
|
|
|
17246
17248
|
expect(TransactionValidator.validateOrder([ build(1, '2018-04-30'), build(2, '2018-04-30'), build(3, '2018-04-30') ])).toEqual(true);
|
|
17247
17249
|
});
|
|
17248
17250
|
|
|
17249
|
-
/*
|
|
17250
17251
|
it('An array of transactions with ordered sequences, on the same day should be valid, where a dividend occurs last, should be valid', () => {
|
|
17251
17252
|
expect(TransactionValidator.validateOrder([ build(1, '2018-04-30'), build(2, '2018-04-30', TransactionType.DIVIDEND) ])).toEqual(true);
|
|
17252
17253
|
});
|
|
17253
17254
|
|
|
17254
|
-
it('An array of transactions with ordered sequences, on the same day should be valid, where a dividend occurs first, should not be valid', () => {
|
|
17255
|
-
expect(TransactionValidator.validateOrder([ build(1, '2018-04-30', TransactionType.DIVIDEND), build(2, '2018-04-30') ])).toEqual(false);
|
|
17255
|
+
it('An array of transactions with ordered sequences, on the same day should be valid, where a dividend occurs first, in strict mode, should not be valid', () => {
|
|
17256
|
+
expect(TransactionValidator.validateOrder([ build(1, '2018-04-30', TransactionType.DIVIDEND), build(2, '2018-04-30') ], true)).toEqual(false);
|
|
17257
|
+
});
|
|
17258
|
+
|
|
17259
|
+
it('An array of transactions with ordered sequences, on the same day should be valid, where a dividend occurs first, in non-strict mode, should be valid', () => {
|
|
17260
|
+
expect(TransactionValidator.validateOrder([ build(1, '2018-04-30', TransactionType.DIVIDEND), build(2, '2018-04-30') ], false)).toEqual(true);
|
|
17256
17261
|
});
|
|
17257
|
-
*/
|
|
17258
17262
|
|
|
17259
17263
|
it('An array of transactions with ordered sequences, on the sequential days should be valid', () => {
|
|
17260
17264
|
expect(TransactionValidator.validateOrder([ build(1, '2018-04-30'), build(2, '2018-05-01'), build(3, '2018-05-02', TransactionType.DIVIDEND) ])).toEqual(true);
|
|
@@ -18,15 +18,17 @@ describe('When validating transaction order', () => {
|
|
|
18
18
|
expect(TransactionValidator.validateOrder([ build(1, '2018-04-30'), build(2, '2018-04-30'), build(3, '2018-04-30') ])).toEqual(true);
|
|
19
19
|
});
|
|
20
20
|
|
|
21
|
-
/*
|
|
22
21
|
it('An array of transactions with ordered sequences, on the same day should be valid, where a dividend occurs last, should be valid', () => {
|
|
23
22
|
expect(TransactionValidator.validateOrder([ build(1, '2018-04-30'), build(2, '2018-04-30', TransactionType.DIVIDEND) ])).toEqual(true);
|
|
24
23
|
});
|
|
25
24
|
|
|
26
|
-
it('An array of transactions with ordered sequences, on the same day should be valid, where a dividend occurs first, should not be valid', () => {
|
|
27
|
-
expect(TransactionValidator.validateOrder([ build(1, '2018-04-30', TransactionType.DIVIDEND), build(2, '2018-04-30') ])).toEqual(false);
|
|
25
|
+
it('An array of transactions with ordered sequences, on the same day should be valid, where a dividend occurs first, in strict mode, should not be valid', () => {
|
|
26
|
+
expect(TransactionValidator.validateOrder([ build(1, '2018-04-30', TransactionType.DIVIDEND), build(2, '2018-04-30') ], true)).toEqual(false);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('An array of transactions with ordered sequences, on the same day should be valid, where a dividend occurs first, in non-strict mode, should be valid', () => {
|
|
30
|
+
expect(TransactionValidator.validateOrder([ build(1, '2018-04-30', TransactionType.DIVIDEND), build(2, '2018-04-30') ], false)).toEqual(true);
|
|
28
31
|
});
|
|
29
|
-
*/
|
|
30
32
|
|
|
31
33
|
it('An array of transactions with ordered sequences, on the sequential days should be valid', () => {
|
|
32
34
|
expect(TransactionValidator.validateOrder([ build(1, '2018-04-30'), build(2, '2018-05-01'), build(3, '2018-05-02', TransactionType.DIVIDEND) ])).toEqual(true);
|