@barchart/portfolio-api-common 1.2.103 → 1.2.107

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;
@@ -704,6 +708,9 @@ module.exports = (() => {
704
708
  actual.summaryTotalPrevious2 = updates.summaryTotalPrevious2;
705
709
  actual.marketPrevious = updates.marketPrevious;
706
710
  actual.marketPrevious2 = updates.marketPrevious2;
711
+ actual.periodRealized = updates.periodRealized;
712
+ actual.periodUnrealized = updates.periodUnrealized;
713
+ actual.periodIncome = updates.periodIncome;
707
714
  actual.cashTotal = updates.cashTotal;
708
715
 
709
716
  format.basis = formatCurrency(actual.basis, currency);
@@ -717,6 +724,9 @@ module.exports = (() => {
717
724
  format.summaryTotalPrevious2Negative = updates.summaryTotalPrevious2.getIsNegative();
718
725
  format.marketPrevious = formatCurrency(updates.marketPrevious, currency);
719
726
  format.marketPrevious2 = formatCurrency(updates.marketPrevious2, currency);
727
+ format.periodRealized = formatCurrency(updates.periodRealized, currency);
728
+ format.periodUnrealized = formatCurrency(updates.periodUnrealized, currency);
729
+ format.periodIncome = formatCurrency(updates.periodIncome, currency);
720
730
  format.cashTotal = formatCurrency(updates.cashTotal, currency);
721
731
 
722
732
  calculateUnrealizedPercent(group);
@@ -729,11 +739,17 @@ module.exports = (() => {
729
739
 
730
740
  actual.basisPrice = item.data.basisPrice;
731
741
 
742
+ actual.periodPrice = item.data.periodPrice;
743
+ actual.periodPricePrevious = item.data.periodPricePrevious;
744
+
732
745
  format.quantity = formatDecimal(actual.quantity, 2);
733
746
  format.quantityPrevious = formatDecimal(actual.quantityPrevious, 2);
734
-
747
+
735
748
  format.basisPrice = formatCurrency(actual.basisPrice, currency);
736
749
 
750
+ format.periodPrice = formatCurrency(actual.periodPrice, currency);
751
+ format.periodPricePrevious = formatCurrency(actual.periodPricePrevious, currency);
752
+
737
753
  format.invalid = definition.type === PositionLevelType.POSITION && item.invalid;
738
754
  format.locked = definition.type === PositionLevelType.POSITION && item.data.locked;
739
755
  }
@@ -78,8 +78,12 @@ module.exports = (() => {
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,13 +374,14 @@ 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
 
@@ -414,6 +419,18 @@ module.exports = (() => {
414
419
  } else {
415
420
  data.basisPrice = basis.divide(snapshot.open);
416
421
  }
422
+
423
+ if (currentSummary && !currentSummary.end.open.getIsZero()) {
424
+ data.periodPrice = currentSummary.end.value.divide(currentSummary.end.open);
425
+ } else {
426
+ data.periodPrice = null;
427
+ }
428
+
429
+ if (previousSummary1 && !previousSummary1.end.open.getIsZero()) {
430
+ data.periodPricePrevious = previousSummary1.end.value.divide(previousSummary1.end.open);
431
+ } else {
432
+ data.periodPricePrevious = null;
433
+ }
417
434
  }
418
435
 
419
436
  function calculatePriceData(item, price) {
@@ -556,7 +573,7 @@ module.exports = (() => {
556
573
  if (currentSummary) {
557
574
  const period = currentSummary.period;
558
575
 
559
- returnRef = period.unrealized;
576
+ returnRef = period.realized;
560
577
  } else {
561
578
  returnRef = Decimal.ZERO;
562
579
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.2.103",
3
+ "version": "1.2.107",
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;
@@ -3148,6 +3168,9 @@ module.exports = (() => {
3148
3168
  actual.summaryTotalPrevious2 = updates.summaryTotalPrevious2;
3149
3169
  actual.marketPrevious = updates.marketPrevious;
3150
3170
  actual.marketPrevious2 = updates.marketPrevious2;
3171
+ actual.periodRealized = updates.periodRealized;
3172
+ actual.periodUnrealized = updates.periodUnrealized;
3173
+ actual.periodIncome = updates.periodIncome;
3151
3174
  actual.cashTotal = updates.cashTotal;
3152
3175
 
3153
3176
  format.basis = formatCurrency(actual.basis, currency);
@@ -3161,6 +3184,9 @@ module.exports = (() => {
3161
3184
  format.summaryTotalPrevious2Negative = updates.summaryTotalPrevious2.getIsNegative();
3162
3185
  format.marketPrevious = formatCurrency(updates.marketPrevious, currency);
3163
3186
  format.marketPrevious2 = formatCurrency(updates.marketPrevious2, currency);
3187
+ format.periodRealized = formatCurrency(updates.periodRealized, currency);
3188
+ format.periodUnrealized = formatCurrency(updates.periodUnrealized, currency);
3189
+ format.periodIncome = formatCurrency(updates.periodIncome, currency);
3164
3190
  format.cashTotal = formatCurrency(updates.cashTotal, currency);
3165
3191
 
3166
3192
  calculateUnrealizedPercent(group);
@@ -3173,11 +3199,17 @@ module.exports = (() => {
3173
3199
 
3174
3200
  actual.basisPrice = item.data.basisPrice;
3175
3201
 
3202
+ actual.periodPrice = item.data.periodPrice;
3203
+ actual.periodPricePrevious = item.data.periodPricePrevious;
3204
+
3176
3205
  format.quantity = formatDecimal(actual.quantity, 2);
3177
3206
  format.quantityPrevious = formatDecimal(actual.quantityPrevious, 2);
3178
-
3207
+
3179
3208
  format.basisPrice = formatCurrency(actual.basisPrice, currency);
3180
3209
 
3210
+ format.periodPrice = formatCurrency(actual.periodPrice, currency);
3211
+ format.periodPricePrevious = formatCurrency(actual.periodPricePrevious, currency);
3212
+
3181
3213
  format.invalid = definition.type === PositionLevelType.POSITION && item.invalid;
3182
3214
  format.locked = definition.type === PositionLevelType.POSITION && item.data.locked;
3183
3215
  }
@@ -3447,8 +3479,12 @@ module.exports = (() => {
3447
3479
  this._data.income = null;
3448
3480
  this._data.basisPrice = null;
3449
3481
 
3450
- this._data.realizedPeriod = null;
3451
- 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;
3452
3488
 
3453
3489
  this._data.newsExists = false;
3454
3490
  this._data.fundamental = { };
@@ -3739,13 +3775,14 @@ module.exports = (() => {
3739
3775
 
3740
3776
  function calculateStaticData(item) {
3741
3777
  const position = item.position;
3742
- const snapshot = getSnapshot(position, item.currentSummary, item._reporting);
3743
3778
 
3744
- const previousSummaries = item.previousSummaries;
3779
+ const currentSummary = item.currentSummary;
3745
3780
 
3746
- const previousSummary1 = getPreviousSummary(previousSummaries, 1);
3747
- const previousSummary2 = getPreviousSummary(previousSummaries, 2);
3748
- 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);
3749
3786
 
3750
3787
  const data = item._data;
3751
3788
 
@@ -3783,6 +3820,18 @@ module.exports = (() => {
3783
3820
  } else {
3784
3821
  data.basisPrice = basis.divide(snapshot.open);
3785
3822
  }
3823
+
3824
+ if (currentSummary && !currentSummary.end.open.getIsZero()) {
3825
+ data.periodPrice = currentSummary.end.value.divide(currentSummary.end.open);
3826
+ } else {
3827
+ data.periodPrice = null;
3828
+ }
3829
+
3830
+ if (previousSummary1 && !previousSummary1.end.open.getIsZero()) {
3831
+ data.periodPricePrevious = previousSummary1.end.value.divide(previousSummary1.end.open);
3832
+ } else {
3833
+ data.periodPricePrevious = null;
3834
+ }
3786
3835
  }
3787
3836
 
3788
3837
  function calculatePriceData(item, price) {
@@ -3925,7 +3974,7 @@ module.exports = (() => {
3925
3974
  if (currentSummary) {
3926
3975
  const period = currentSummary.period;
3927
3976
 
3928
- returnRef = period.unrealized;
3977
+ returnRef = period.realized;
3929
3978
  } else {
3930
3979
  returnRef = Decimal.ZERO;
3931
3980
  }
@@ -6250,12 +6299,11 @@ module.exports = function () {
6250
6299
  }
6251
6300
 
6252
6301
  /**
6253
- * Converts a string (which matches the output of {@link Day#format} into
6254
- * a {@link Day} instance.
6302
+ * Clones a {@link Day} instance.
6255
6303
  *
6256
6304
  * @public
6257
6305
  * @static
6258
- * @param {String} value
6306
+ * @param {Day} value
6259
6307
  * @returns {Day}
6260
6308
  */
6261
6309
 
@@ -6296,6 +6344,24 @@ module.exports = function () {
6296
6344
  return this._day;
6297
6345
  }
6298
6346
  }], [{
6347
+ key: 'clone',
6348
+ value: function clone(value) {
6349
+ assert.argumentIsRequired(value, 'value', Day, 'Day');
6350
+
6351
+ return new Day(value.year, value.month, value.day);
6352
+ }
6353
+
6354
+ /**
6355
+ * Converts a string (which matches the output of {@link Day#format} into
6356
+ * a {@link Day} instance.
6357
+ *
6358
+ * @public
6359
+ * @static
6360
+ * @param {String} value
6361
+ * @returns {Day}
6362
+ */
6363
+
6364
+ }, {
6299
6365
  key: 'parse',
6300
6366
  value: function parse(value) {
6301
6367
  assert.argumentIsRequired(value, 'value', String);
@@ -6612,15 +6678,17 @@ module.exports = function () {
6612
6678
  *
6613
6679
  * @public
6614
6680
  * @param {Boolean=} approximate
6681
+ * @param {Number=} places
6615
6682
  * @returns {Boolean}
6616
6683
  */
6617
6684
 
6618
6685
  }, {
6619
6686
  key: 'getIsZero',
6620
- value: function getIsZero(approximate) {
6687
+ value: function getIsZero(approximate, places) {
6621
6688
  assert.argumentIsOptional(approximate, 'approximate', Boolean);
6689
+ assert.argumentIsOptional(places, 'places', Number);
6622
6690
 
6623
- return this._big.eq(zero) || is.boolean(approximate) && approximate && this.round(20, RoundingMode.NORMAL).getIsZero();
6691
+ return this._big.eq(zero) || is.boolean(approximate) && approximate && this.round(places || Big.DP, RoundingMode.NORMAL).getIsZero();
6624
6692
  }
6625
6693
 
6626
6694
  /**
@@ -6719,6 +6787,43 @@ module.exports = function () {
6719
6787
  return this._big.eq(getBig(other));
6720
6788
  }
6721
6789
 
6790
+ /**
6791
+ * Returns true if the current instance is an integer (i.e. has no decimal
6792
+ * component).
6793
+ *
6794
+ * @public
6795
+ * @return {Boolean}
6796
+ */
6797
+
6798
+ }, {
6799
+ key: 'getIsInteger',
6800
+ value: function getIsInteger() {
6801
+ return this.getIsEqual(this.round(0));
6802
+ }
6803
+
6804
+ /**
6805
+ * Returns the number of decimal places used.
6806
+ *
6807
+ * @public
6808
+ * @returns {Number}
6809
+ */
6810
+
6811
+ }, {
6812
+ key: 'getDecimalPlaces',
6813
+ value: function getDecimalPlaces() {
6814
+ var matches = this.toFixed().match(/-?\d*\.(\d*)/);
6815
+
6816
+ var returnVal = void 0;
6817
+
6818
+ if (matches === null) {
6819
+ returnVal = 0;
6820
+ } else {
6821
+ returnVal = matches[1].length;
6822
+ }
6823
+
6824
+ return returnVal;
6825
+ }
6826
+
6722
6827
  /**
6723
6828
  * Emits a floating point value that approximates the value of the current
6724
6829
  * instance.
@@ -6767,10 +6872,11 @@ module.exports = function () {
6767
6872
  }
6768
6873
 
6769
6874
  /**
6770
- * Parses the value emitted by {@link Decimal#toJSON}.
6875
+ * Clones a {@link Decimal} instance.
6771
6876
  *
6772
6877
  * @public
6773
- * @param {String} value
6878
+ * @static
6879
+ * @param {Decimal} value
6774
6880
  * @returns {Decimal}
6775
6881
  */
6776
6882
 
@@ -6780,6 +6886,22 @@ module.exports = function () {
6780
6886
  return '[Decimal]';
6781
6887
  }
6782
6888
  }], [{
6889
+ key: 'clone',
6890
+ value: function clone(value) {
6891
+ assert.argumentIsRequired(value, 'value', Decimal, 'Decimal');
6892
+
6893
+ return new Decimal(value._big);
6894
+ }
6895
+
6896
+ /**
6897
+ * Parses the value emitted by {@link Decimal#toJSON}.
6898
+ *
6899
+ * @public
6900
+ * @param {String} value
6901
+ * @returns {Decimal}
6902
+ */
6903
+
6904
+ }, {
6783
6905
  key: 'parse',
6784
6906
  value: function parse(value) {
6785
6907
  return new Decimal(value);
@@ -7801,10 +7923,11 @@ module.exports = function () {
7801
7923
  }
7802
7924
 
7803
7925
  /**
7804
- * Parses the value emitted by {@link Timestamp#toJSON}.
7926
+ * Clones a {@link Timestamp} instance.
7805
7927
  *
7806
7928
  * @public
7807
- * @param {Number} value
7929
+ * @static
7930
+ * @param {Timestamp} value
7808
7931
  * @returns {Timestamp}
7809
7932
  */
7810
7933
 
@@ -7840,6 +7963,22 @@ module.exports = function () {
7840
7963
  return this._moment;
7841
7964
  }
7842
7965
  }], [{
7966
+ key: 'clone',
7967
+ value: function clone(value) {
7968
+ assert.argumentIsRequired(value, 'value', Timestamp, 'Timestamp');
7969
+
7970
+ return new Timestamp(value._timestamp, value._timezone);
7971
+ }
7972
+
7973
+ /**
7974
+ * Parses the value emitted by {@link Timestamp#toJSON}.
7975
+ *
7976
+ * @public
7977
+ * @param {Number} value
7978
+ * @returns {Timestamp}
7979
+ */
7980
+
7981
+ }, {
7843
7982
  key: 'parse',
7844
7983
  value: function parse(value) {
7845
7984
  return new Timestamp(value);