@barchart/portfolio-api-common 1.2.104 → 1.2.108

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.
@@ -104,7 +104,7 @@ module.exports = (() => {
104
104
  }, { });
105
105
 
106
106
  this._items = positions.reduce((items, position) => {
107
- const item = createPositionItem.call(this, position);
107
+ const item = createPositionItem.call(this, position, reportFrame ? true : false);
108
108
 
109
109
  if (item) {
110
110
  items.push(item);
@@ -313,7 +313,7 @@ module.exports = (() => {
313
313
  addSummaryPrevious(this._summariesPrevious, summary, this._previousSummaryFrame, this._previousSummaryRanges);
314
314
  });
315
315
 
316
- const item = createPositionItem.call(this, position);
316
+ const item = createPositionItem.call(this, position, false);
317
317
 
318
318
  addBarchartSymbol(this._symbols, item);
319
319
  addDisplaySymbol(this._symbolsDisplay, item);
@@ -498,6 +498,18 @@ module.exports = (() => {
498
498
  }
499
499
  }
500
500
 
501
+ /**
502
+ * Sets a historical forex quote.
503
+ *
504
+ * @public
505
+ * @param {Object} forexQuote
506
+ * @param {Day} date
507
+ */
508
+ setHistoricalForexQuote(forexQuote, date) {
509
+ assert.argumentIsRequired(forexQuote, 'forexQuote', Object);
510
+ assert.argumentIsRequired(date, 'date', Day, 'Day');
511
+ }
512
+
501
513
  /**
502
514
  * Returns all forex symbols that are required to do currency translations.
503
515
  *
@@ -947,7 +959,7 @@ module.exports = (() => {
947
959
  }
948
960
  }
949
961
 
950
- function createPositionItem(position, currentQuote, previousQuote) {
962
+ function createPositionItem(position, requireCurrentSummary) {
951
963
  const portfolio = this._portfolios[position.portfolio];
952
964
 
953
965
  let returnRef;
@@ -956,7 +968,11 @@ module.exports = (() => {
956
968
  const currentSummary = this._summariesCurrent[ position.position ] || null;
957
969
  const previousSummaries = this._summariesPrevious[ position.position ] || getSummaryArray(this._previousSummaryRanges);
958
970
 
959
- returnRef = new PositionItem(portfolio, position, currentSummary, previousSummaries, this._reporting);
971
+ if (!requireCurrentSummary || currentSummary !== null) {
972
+ returnRef = new PositionItem(portfolio, position, currentSummary, previousSummaries, this._reporting);
973
+ } else {
974
+ returnRef = null;
975
+ }
960
976
  } else {
961
977
  returnRef = null;
962
978
  }
@@ -166,10 +166,14 @@ module.exports = (() => {
166
166
  this._dataFormat.cashTotal = null;
167
167
  this._dataFormat.portfolioType = null;
168
168
 
169
+ this._dataActual.periodPrice = null;
170
+ this._dataActual.periodPricePrevious = null;
169
171
  this._dataActual.periodRealized = null;
170
172
  this._dataActual.periodUnrealized = null;
171
173
  this._dataActual.periodIncome = null;
172
174
 
175
+ this._dataFormat.periodPrice = null;
176
+ this._dataFormat.periodPricePrevious = null;
173
177
  this._dataFormat.periodRealized = null;
174
178
  this._dataFormat.periodUnrealized = null;
175
179
  this._dataFormat.periodIncome = null;
@@ -730,16 +734,22 @@ module.exports = (() => {
730
734
  if (group.single && group._items.length === 1) {
731
735
  const item = group._items[0];
732
736
 
733
- actual.quantity = item.position.snapshot.open;
737
+ actual.quantity = item.data.quantity;
734
738
  actual.quantityPrevious = item.data.quantityPrevious;
735
739
 
736
740
  actual.basisPrice = item.data.basisPrice;
737
741
 
742
+ actual.periodPrice = item.data.periodPrice;
743
+ actual.periodPricePrevious = item.data.periodPricePrevious;
744
+
738
745
  format.quantity = formatDecimal(actual.quantity, 2);
739
746
  format.quantityPrevious = formatDecimal(actual.quantityPrevious, 2);
740
-
747
+
741
748
  format.basisPrice = formatCurrency(actual.basisPrice, currency);
742
749
 
750
+ format.periodPrice = formatCurrency(actual.periodPrice, currency);
751
+ format.periodPricePrevious = formatCurrency(actual.periodPricePrevious, currency);
752
+
743
753
  format.invalid = definition.type === PositionLevelType.POSITION && item.invalid;
744
754
  format.locked = definition.type === PositionLevelType.POSITION && item.data.locked;
745
755
  }
@@ -71,15 +71,19 @@ module.exports = (() => {
71
71
  this._data.marketPrevious = null;
72
72
  this._data.marketPrevious2 = null;
73
73
 
74
+ this._data.quantity = null;
74
75
  this._data.quantityPrevious = null;
75
- this._data.quantityPrevious2 = null;
76
76
 
77
77
  this._data.realized = null;
78
78
  this._data.income = null;
79
79
  this._data.basisPrice = null;
80
80
 
81
- this._data.realizedPeriod = null;
82
- this._data.unrealizedPeriod = null;
81
+ this._data.periodRealized = null;
82
+ this._data.periodUnrealized = null;
83
+ this._data.periodIncome = null;
84
+
85
+ this._data.periodPrice = null;
86
+ this._data.periodPricePrevious = null;
83
87
 
84
88
  this._data.newsExists = false;
85
89
  this._data.fundamental = { };
@@ -370,16 +374,18 @@ module.exports = (() => {
370
374
 
371
375
  function calculateStaticData(item) {
372
376
  const position = item.position;
373
- const snapshot = getSnapshot(position, item.currentSummary, item._reporting);
374
377
 
375
- const previousSummaries = item.previousSummaries;
378
+ const currentSummary = item.currentSummary;
379
+
380
+ const previousSummary1 = getPreviousSummary(item.previousSummaries, 1);
381
+ const previousSummary2 = getPreviousSummary(item.previousSummaries, 2);
382
+ const previousSummary3 = getPreviousSummary(item.previousSummaries, 3);
376
383
 
377
- const previousSummary1 = getPreviousSummary(previousSummaries, 1);
378
- const previousSummary2 = getPreviousSummary(previousSummaries, 2);
379
- const previousSummary3 = getPreviousSummary(previousSummaries, 3);
384
+ const snapshot = getSnapshot(position, currentSummary, item._reporting);
380
385
 
381
386
  const data = item._data;
382
387
 
388
+ data.quantity = snapshot.open;
383
389
  data.previousPrice = position.previous || null;
384
390
 
385
391
  let basis;
@@ -414,6 +420,18 @@ module.exports = (() => {
414
420
  } else {
415
421
  data.basisPrice = basis.divide(snapshot.open);
416
422
  }
423
+
424
+ if (currentSummary && !currentSummary.end.open.getIsZero()) {
425
+ data.periodPrice = currentSummary.end.value.divide(currentSummary.end.open);
426
+ } else {
427
+ data.periodPrice = null;
428
+ }
429
+
430
+ if (previousSummary1 && !previousSummary1.end.open.getIsZero()) {
431
+ data.periodPricePrevious = previousSummary1.end.value.divide(previousSummary1.end.open);
432
+ } else {
433
+ data.periodPricePrevious = null;
434
+ }
417
435
  }
418
436
 
419
437
  function calculatePriceData(item, price) {
@@ -556,7 +574,7 @@ module.exports = (() => {
556
574
  if (currentSummary) {
557
575
  const period = currentSummary.period;
558
576
 
559
- returnRef = period.unrealized;
577
+ returnRef = period.realized;
560
578
  } else {
561
579
  returnRef = Decimal.ZERO;
562
580
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.2.104",
3
+ "version": "1.2.108",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",
@@ -1528,7 +1528,7 @@ module.exports = (() => {
1528
1528
  }, { });
1529
1529
 
1530
1530
  this._items = positions.reduce((items, position) => {
1531
- const item = createPositionItem.call(this, position);
1531
+ const item = createPositionItem.call(this, position, reportFrame ? true : false);
1532
1532
 
1533
1533
  if (item) {
1534
1534
  items.push(item);
@@ -1737,7 +1737,7 @@ module.exports = (() => {
1737
1737
  addSummaryPrevious(this._summariesPrevious, summary, this._previousSummaryFrame, this._previousSummaryRanges);
1738
1738
  });
1739
1739
 
1740
- const item = createPositionItem.call(this, position);
1740
+ const item = createPositionItem.call(this, position, false);
1741
1741
 
1742
1742
  addBarchartSymbol(this._symbols, item);
1743
1743
  addDisplaySymbol(this._symbolsDisplay, item);
@@ -1922,6 +1922,18 @@ module.exports = (() => {
1922
1922
  }
1923
1923
  }
1924
1924
 
1925
+ /**
1926
+ * Sets a historical forex quote.
1927
+ *
1928
+ * @public
1929
+ * @param {Object} forexQuote
1930
+ * @param {Day} date
1931
+ */
1932
+ setHistoricalForexQuote(forexQuote, date) {
1933
+ assert.argumentIsRequired(forexQuote, 'forexQuote', Object);
1934
+ assert.argumentIsRequired(date, 'date', Day, 'Day');
1935
+ }
1936
+
1925
1937
  /**
1926
1938
  * Returns all forex symbols that are required to do currency translations.
1927
1939
  *
@@ -2371,7 +2383,7 @@ module.exports = (() => {
2371
2383
  }
2372
2384
  }
2373
2385
 
2374
- function createPositionItem(position, currentQuote, previousQuote) {
2386
+ function createPositionItem(position, requireCurrentSummary) {
2375
2387
  const portfolio = this._portfolios[position.portfolio];
2376
2388
 
2377
2389
  let returnRef;
@@ -2380,7 +2392,11 @@ module.exports = (() => {
2380
2392
  const currentSummary = this._summariesCurrent[ position.position ] || null;
2381
2393
  const previousSummaries = this._summariesPrevious[ position.position ] || getSummaryArray(this._previousSummaryRanges);
2382
2394
 
2383
- returnRef = new PositionItem(portfolio, position, currentSummary, previousSummaries, this._reporting);
2395
+ if (!requireCurrentSummary || currentSummary !== null) {
2396
+ returnRef = new PositionItem(portfolio, position, currentSummary, previousSummaries, this._reporting);
2397
+ } else {
2398
+ returnRef = null;
2399
+ }
2384
2400
  } else {
2385
2401
  returnRef = null;
2386
2402
  }
@@ -2610,10 +2626,14 @@ module.exports = (() => {
2610
2626
  this._dataFormat.cashTotal = null;
2611
2627
  this._dataFormat.portfolioType = null;
2612
2628
 
2629
+ this._dataActual.periodPrice = null;
2630
+ this._dataActual.periodPricePrevious = null;
2613
2631
  this._dataActual.periodRealized = null;
2614
2632
  this._dataActual.periodUnrealized = null;
2615
2633
  this._dataActual.periodIncome = null;
2616
2634
 
2635
+ this._dataFormat.periodPrice = null;
2636
+ this._dataFormat.periodPricePrevious = null;
2617
2637
  this._dataFormat.periodRealized = null;
2618
2638
  this._dataFormat.periodUnrealized = null;
2619
2639
  this._dataFormat.periodIncome = null;
@@ -3174,16 +3194,22 @@ module.exports = (() => {
3174
3194
  if (group.single && group._items.length === 1) {
3175
3195
  const item = group._items[0];
3176
3196
 
3177
- actual.quantity = item.position.snapshot.open;
3197
+ actual.quantity = item.data.quantity;
3178
3198
  actual.quantityPrevious = item.data.quantityPrevious;
3179
3199
 
3180
3200
  actual.basisPrice = item.data.basisPrice;
3181
3201
 
3202
+ actual.periodPrice = item.data.periodPrice;
3203
+ actual.periodPricePrevious = item.data.periodPricePrevious;
3204
+
3182
3205
  format.quantity = formatDecimal(actual.quantity, 2);
3183
3206
  format.quantityPrevious = formatDecimal(actual.quantityPrevious, 2);
3184
-
3207
+
3185
3208
  format.basisPrice = formatCurrency(actual.basisPrice, currency);
3186
3209
 
3210
+ format.periodPrice = formatCurrency(actual.periodPrice, currency);
3211
+ format.periodPricePrevious = formatCurrency(actual.periodPricePrevious, currency);
3212
+
3187
3213
  format.invalid = definition.type === PositionLevelType.POSITION && item.invalid;
3188
3214
  format.locked = definition.type === PositionLevelType.POSITION && item.data.locked;
3189
3215
  }
@@ -3446,15 +3472,19 @@ module.exports = (() => {
3446
3472
  this._data.marketPrevious = null;
3447
3473
  this._data.marketPrevious2 = null;
3448
3474
 
3475
+ this._data.quantity = null;
3449
3476
  this._data.quantityPrevious = null;
3450
- this._data.quantityPrevious2 = null;
3451
3477
 
3452
3478
  this._data.realized = null;
3453
3479
  this._data.income = null;
3454
3480
  this._data.basisPrice = null;
3455
3481
 
3456
- this._data.realizedPeriod = null;
3457
- this._data.unrealizedPeriod = null;
3482
+ this._data.periodRealized = null;
3483
+ this._data.periodUnrealized = null;
3484
+ this._data.periodIncome = null;
3485
+
3486
+ this._data.periodPrice = null;
3487
+ this._data.periodPricePrevious = null;
3458
3488
 
3459
3489
  this._data.newsExists = false;
3460
3490
  this._data.fundamental = { };
@@ -3745,16 +3775,18 @@ module.exports = (() => {
3745
3775
 
3746
3776
  function calculateStaticData(item) {
3747
3777
  const position = item.position;
3748
- const snapshot = getSnapshot(position, item.currentSummary, item._reporting);
3749
3778
 
3750
- const previousSummaries = item.previousSummaries;
3779
+ const currentSummary = item.currentSummary;
3751
3780
 
3752
- const previousSummary1 = getPreviousSummary(previousSummaries, 1);
3753
- const previousSummary2 = getPreviousSummary(previousSummaries, 2);
3754
- const previousSummary3 = getPreviousSummary(previousSummaries, 3);
3781
+ const previousSummary1 = getPreviousSummary(item.previousSummaries, 1);
3782
+ const previousSummary2 = getPreviousSummary(item.previousSummaries, 2);
3783
+ const previousSummary3 = getPreviousSummary(item.previousSummaries, 3);
3784
+
3785
+ const snapshot = getSnapshot(position, currentSummary, item._reporting);
3755
3786
 
3756
3787
  const data = item._data;
3757
3788
 
3789
+ data.quantity = snapshot.open;
3758
3790
  data.previousPrice = position.previous || null;
3759
3791
 
3760
3792
  let basis;
@@ -3789,6 +3821,18 @@ module.exports = (() => {
3789
3821
  } else {
3790
3822
  data.basisPrice = basis.divide(snapshot.open);
3791
3823
  }
3824
+
3825
+ if (currentSummary && !currentSummary.end.open.getIsZero()) {
3826
+ data.periodPrice = currentSummary.end.value.divide(currentSummary.end.open);
3827
+ } else {
3828
+ data.periodPrice = null;
3829
+ }
3830
+
3831
+ if (previousSummary1 && !previousSummary1.end.open.getIsZero()) {
3832
+ data.periodPricePrevious = previousSummary1.end.value.divide(previousSummary1.end.open);
3833
+ } else {
3834
+ data.periodPricePrevious = null;
3835
+ }
3792
3836
  }
3793
3837
 
3794
3838
  function calculatePriceData(item, price) {
@@ -3931,7 +3975,7 @@ module.exports = (() => {
3931
3975
  if (currentSummary) {
3932
3976
  const period = currentSummary.period;
3933
3977
 
3934
- returnRef = period.unrealized;
3978
+ returnRef = period.realized;
3935
3979
  } else {
3936
3980
  returnRef = Decimal.ZERO;
3937
3981
  }
@@ -6256,12 +6300,11 @@ module.exports = function () {
6256
6300
  }
6257
6301
 
6258
6302
  /**
6259
- * Converts a string (which matches the output of {@link Day#format} into
6260
- * a {@link Day} instance.
6303
+ * Clones a {@link Day} instance.
6261
6304
  *
6262
6305
  * @public
6263
6306
  * @static
6264
- * @param {String} value
6307
+ * @param {Day} value
6265
6308
  * @returns {Day}
6266
6309
  */
6267
6310
 
@@ -6302,6 +6345,24 @@ module.exports = function () {
6302
6345
  return this._day;
6303
6346
  }
6304
6347
  }], [{
6348
+ key: 'clone',
6349
+ value: function clone(value) {
6350
+ assert.argumentIsRequired(value, 'value', Day, 'Day');
6351
+
6352
+ return new Day(value.year, value.month, value.day);
6353
+ }
6354
+
6355
+ /**
6356
+ * Converts a string (which matches the output of {@link Day#format} into
6357
+ * a {@link Day} instance.
6358
+ *
6359
+ * @public
6360
+ * @static
6361
+ * @param {String} value
6362
+ * @returns {Day}
6363
+ */
6364
+
6365
+ }, {
6305
6366
  key: 'parse',
6306
6367
  value: function parse(value) {
6307
6368
  assert.argumentIsRequired(value, 'value', String);
@@ -6618,15 +6679,17 @@ module.exports = function () {
6618
6679
  *
6619
6680
  * @public
6620
6681
  * @param {Boolean=} approximate
6682
+ * @param {Number=} places
6621
6683
  * @returns {Boolean}
6622
6684
  */
6623
6685
 
6624
6686
  }, {
6625
6687
  key: 'getIsZero',
6626
- value: function getIsZero(approximate) {
6688
+ value: function getIsZero(approximate, places) {
6627
6689
  assert.argumentIsOptional(approximate, 'approximate', Boolean);
6690
+ assert.argumentIsOptional(places, 'places', Number);
6628
6691
 
6629
- return this._big.eq(zero) || is.boolean(approximate) && approximate && this.round(20, RoundingMode.NORMAL).getIsZero();
6692
+ return this._big.eq(zero) || is.boolean(approximate) && approximate && this.round(places || Big.DP, RoundingMode.NORMAL).getIsZero();
6630
6693
  }
6631
6694
 
6632
6695
  /**
@@ -6725,6 +6788,43 @@ module.exports = function () {
6725
6788
  return this._big.eq(getBig(other));
6726
6789
  }
6727
6790
 
6791
+ /**
6792
+ * Returns true if the current instance is an integer (i.e. has no decimal
6793
+ * component).
6794
+ *
6795
+ * @public
6796
+ * @return {Boolean}
6797
+ */
6798
+
6799
+ }, {
6800
+ key: 'getIsInteger',
6801
+ value: function getIsInteger() {
6802
+ return this.getIsEqual(this.round(0));
6803
+ }
6804
+
6805
+ /**
6806
+ * Returns the number of decimal places used.
6807
+ *
6808
+ * @public
6809
+ * @returns {Number}
6810
+ */
6811
+
6812
+ }, {
6813
+ key: 'getDecimalPlaces',
6814
+ value: function getDecimalPlaces() {
6815
+ var matches = this.toFixed().match(/-?\d*\.(\d*)/);
6816
+
6817
+ var returnVal = void 0;
6818
+
6819
+ if (matches === null) {
6820
+ returnVal = 0;
6821
+ } else {
6822
+ returnVal = matches[1].length;
6823
+ }
6824
+
6825
+ return returnVal;
6826
+ }
6827
+
6728
6828
  /**
6729
6829
  * Emits a floating point value that approximates the value of the current
6730
6830
  * instance.
@@ -6773,10 +6873,11 @@ module.exports = function () {
6773
6873
  }
6774
6874
 
6775
6875
  /**
6776
- * Parses the value emitted by {@link Decimal#toJSON}.
6876
+ * Clones a {@link Decimal} instance.
6777
6877
  *
6778
6878
  * @public
6779
- * @param {String} value
6879
+ * @static
6880
+ * @param {Decimal} value
6780
6881
  * @returns {Decimal}
6781
6882
  */
6782
6883
 
@@ -6786,6 +6887,22 @@ module.exports = function () {
6786
6887
  return '[Decimal]';
6787
6888
  }
6788
6889
  }], [{
6890
+ key: 'clone',
6891
+ value: function clone(value) {
6892
+ assert.argumentIsRequired(value, 'value', Decimal, 'Decimal');
6893
+
6894
+ return new Decimal(value._big);
6895
+ }
6896
+
6897
+ /**
6898
+ * Parses the value emitted by {@link Decimal#toJSON}.
6899
+ *
6900
+ * @public
6901
+ * @param {String} value
6902
+ * @returns {Decimal}
6903
+ */
6904
+
6905
+ }, {
6789
6906
  key: 'parse',
6790
6907
  value: function parse(value) {
6791
6908
  return new Decimal(value);
@@ -7807,10 +7924,11 @@ module.exports = function () {
7807
7924
  }
7808
7925
 
7809
7926
  /**
7810
- * Parses the value emitted by {@link Timestamp#toJSON}.
7927
+ * Clones a {@link Timestamp} instance.
7811
7928
  *
7812
7929
  * @public
7813
- * @param {Number} value
7930
+ * @static
7931
+ * @param {Timestamp} value
7814
7932
  * @returns {Timestamp}
7815
7933
  */
7816
7934
 
@@ -7846,6 +7964,22 @@ module.exports = function () {
7846
7964
  return this._moment;
7847
7965
  }
7848
7966
  }], [{
7967
+ key: 'clone',
7968
+ value: function clone(value) {
7969
+ assert.argumentIsRequired(value, 'value', Timestamp, 'Timestamp');
7970
+
7971
+ return new Timestamp(value._timestamp, value._timezone);
7972
+ }
7973
+
7974
+ /**
7975
+ * Parses the value emitted by {@link Timestamp#toJSON}.
7976
+ *
7977
+ * @public
7978
+ * @param {Number} value
7979
+ * @returns {Timestamp}
7980
+ */
7981
+
7982
+ }, {
7849
7983
  key: 'parse',
7850
7984
  value: function parse(value) {
7851
7985
  return new Timestamp(value);