@barchart/portfolio-api-common 1.2.101 → 1.2.105

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.
@@ -286,7 +286,7 @@ module.exports = (() => {
286
286
  }
287
287
 
288
288
  function getYearlyRangeDescription(start, end) {
289
- return `Year ended ${end.year.format()}`;
289
+ return `Year ended ${end.format()}`;
290
290
  }
291
291
 
292
292
  function getQuarterlyRangeDescription(start, end) {
@@ -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
  *
@@ -166,6 +166,14 @@ module.exports = (() => {
166
166
  this._dataFormat.cashTotal = null;
167
167
  this._dataFormat.portfolioType = null;
168
168
 
169
+ this._dataActual.periodRealized = null;
170
+ this._dataActual.periodUnrealized = null;
171
+ this._dataActual.periodIncome = null;
172
+
173
+ this._dataFormat.periodRealized = null;
174
+ this._dataFormat.periodUnrealized = null;
175
+ this._dataFormat.periodIncome = null;
176
+
169
177
  this._items.forEach((item) => {
170
178
  bindItem.call(this, item);
171
179
  });
@@ -662,6 +670,10 @@ module.exports = (() => {
662
670
  updates.marketPrevious = updates.marketPrevious.add(translate(item, item.data.marketPrevious));
663
671
  updates.marketPrevious2 = updates.marketPrevious2.add(translate(item, item.data.marketPrevious2));
664
672
 
673
+ updates.periodRealized = updates.periodRealized.add(translate(item, item.data.periodRealized));
674
+ updates.periodUnrealized = updates.periodUnrealized.add(translate(item, item.data.periodUnrealized));
675
+ updates.periodIncome = updates.periodIncome.add(translate(item, item.data.periodIncome));
676
+
665
677
  if (item.position.instrument.type === InstrumentType.CASH) {
666
678
  updates.cashTotal = updates.cashTotal.add(translate(item, item.data.market));
667
679
  }
@@ -677,7 +689,10 @@ module.exports = (() => {
677
689
  summaryTotalPrevious2: Decimal.ZERO,
678
690
  marketPrevious: Decimal.ZERO,
679
691
  marketPrevious2: Decimal.ZERO,
680
- cashTotal: Decimal.ZERO
692
+ periodRealized: Decimal.ZERO,
693
+ periodUnrealized: Decimal.ZERO,
694
+ periodIncome: Decimal.ZERO,
695
+ cashTotal: Decimal.ZERO,
681
696
  });
682
697
 
683
698
  actual.basis = updates.basis;
@@ -689,6 +704,9 @@ module.exports = (() => {
689
704
  actual.summaryTotalPrevious2 = updates.summaryTotalPrevious2;
690
705
  actual.marketPrevious = updates.marketPrevious;
691
706
  actual.marketPrevious2 = updates.marketPrevious2;
707
+ actual.periodRealized = updates.periodRealized;
708
+ actual.periodUnrealized = updates.periodUnrealized;
709
+ actual.periodIncome = updates.periodIncome;
692
710
  actual.cashTotal = updates.cashTotal;
693
711
 
694
712
  format.basis = formatCurrency(actual.basis, currency);
@@ -702,6 +720,9 @@ module.exports = (() => {
702
720
  format.summaryTotalPrevious2Negative = updates.summaryTotalPrevious2.getIsNegative();
703
721
  format.marketPrevious = formatCurrency(updates.marketPrevious, currency);
704
722
  format.marketPrevious2 = formatCurrency(updates.marketPrevious2, currency);
723
+ format.periodRealized = formatCurrency(updates.periodRealized, currency);
724
+ format.periodUnrealized = formatCurrency(updates.periodUnrealized, currency);
725
+ format.periodIncome = formatCurrency(updates.periodIncome, currency);
705
726
  format.cashTotal = formatCurrency(updates.cashTotal, currency);
706
727
 
707
728
  calculateUnrealizedPercent(group);
@@ -78,6 +78,9 @@ 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;
83
+
81
84
  this._data.newsExists = false;
82
85
  this._data.fundamental = { };
83
86
  this._data.locked = getIsLocked(position);
@@ -368,8 +371,13 @@ module.exports = (() => {
368
371
  function calculateStaticData(item) {
369
372
  const position = item.position;
370
373
  const snapshot = getSnapshot(position, item.currentSummary, item._reporting);
374
+
371
375
  const previousSummaries = item.previousSummaries;
372
376
 
377
+ const previousSummary1 = getPreviousSummary(previousSummaries, 1);
378
+ const previousSummary2 = getPreviousSummary(previousSummaries, 2);
379
+ const previousSummary3 = getPreviousSummary(previousSummaries, 3);
380
+
373
381
  const data = item._data;
374
382
 
375
383
  data.previousPrice = position.previous || null;
@@ -389,19 +397,18 @@ module.exports = (() => {
389
397
 
390
398
  data.income = snapshot.income;
391
399
 
392
- const previousSummary1 = getPreviousSummary(previousSummaries, 1);
393
- const previousSummary2 = getPreviousSummary(previousSummaries, 2);
394
- const previousSummary3 = getPreviousSummary(previousSummaries, 3);
395
-
396
400
  data.summaryTotalCurrent = calculateSummaryTotal(item.currentSummary, previousSummary1);
397
401
  data.summaryTotalPrevious = calculateSummaryTotal(previousSummary1, previousSummary2);
398
402
  data.summaryTotalPrevious2 = calculateSummaryTotal(previousSummary2, previousSummary3);
399
403
 
400
404
  data.marketPrevious = previousSummary1 === null ? Decimal.ZERO : previousSummary1.end.value;
401
405
  data.marketPrevious2 = previousSummary2 === null ? Decimal.ZERO : previousSummary2.end.value;
402
-
403
406
  data.quantityPrevious = previousSummary1 === null ? Decimal.ZERO : previousSummary1.end.open;
404
407
 
408
+ data.periodRealized = calculateRealizedPeriod(item.currentSummary, previousSummary1);
409
+ data.periodUnrealized = calculateUnrealizedPeriod(item.currentSummary, previousSummary1);
410
+ data.periodIncome = calculateIncomePeriod(item.currentSummary, previousSummary1);
411
+
405
412
  if (snapshot.open.getIsZero()) {
406
413
  data.basisPrice = Decimal.ZERO;
407
414
  } else {
@@ -543,6 +550,48 @@ module.exports = (() => {
543
550
  return returnRef;
544
551
  }
545
552
 
553
+ function calculateRealizedPeriod(currentSummary, previousSummary) {
554
+ let returnRef;
555
+
556
+ if (currentSummary) {
557
+ const period = currentSummary.period;
558
+
559
+ returnRef = period.realized;
560
+ } else {
561
+ returnRef = Decimal.ZERO;
562
+ }
563
+
564
+ return returnRef;
565
+ }
566
+
567
+ function calculateUnrealizedPeriod(currentSummary, previousSummary) {
568
+ let returnRef;
569
+
570
+ if (currentSummary) {
571
+ const period = currentSummary.period;
572
+
573
+ returnRef = period.unrealized.subtract(previousSummary !== null ? previousSummary.period.unrealized : Decimal.ZERO);
574
+ } else {
575
+ returnRef = Decimal.ZERO;
576
+ }
577
+
578
+ return returnRef;
579
+ }
580
+
581
+ function calculateIncomePeriod(currentSummary, previousSummary) {
582
+ let returnRef;
583
+
584
+ if (currentSummary) {
585
+ const period = currentSummary.period;
586
+
587
+ returnRef = period.income;
588
+ } else {
589
+ returnRef = Decimal.ZERO;
590
+ }
591
+
592
+ return returnRef;
593
+ }
594
+
546
595
  function getPreviousSummary(previousSummaries, count) {
547
596
  const index = previousSummaries.length - count;
548
597
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.2.101",
3
+ "version": "1.2.105",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",
@@ -677,7 +677,7 @@ module.exports = (() => {
677
677
  }
678
678
 
679
679
  function getYearlyRangeDescription(start, end) {
680
- return `Year ended ${end.year.format()}`;
680
+ return `Year ended ${end.format()}`;
681
681
  }
682
682
 
683
683
  function getQuarterlyRangeDescription(start, end) {
@@ -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
  *
@@ -2610,6 +2622,14 @@ module.exports = (() => {
2610
2622
  this._dataFormat.cashTotal = null;
2611
2623
  this._dataFormat.portfolioType = null;
2612
2624
 
2625
+ this._dataActual.periodRealized = null;
2626
+ this._dataActual.periodUnrealized = null;
2627
+ this._dataActual.periodIncome = null;
2628
+
2629
+ this._dataFormat.periodRealized = null;
2630
+ this._dataFormat.periodUnrealized = null;
2631
+ this._dataFormat.periodIncome = null;
2632
+
2613
2633
  this._items.forEach((item) => {
2614
2634
  bindItem.call(this, item);
2615
2635
  });
@@ -3106,6 +3126,10 @@ module.exports = (() => {
3106
3126
  updates.marketPrevious = updates.marketPrevious.add(translate(item, item.data.marketPrevious));
3107
3127
  updates.marketPrevious2 = updates.marketPrevious2.add(translate(item, item.data.marketPrevious2));
3108
3128
 
3129
+ updates.periodRealized = updates.periodRealized.add(translate(item, item.data.periodRealized));
3130
+ updates.periodUnrealized = updates.periodUnrealized.add(translate(item, item.data.periodUnrealized));
3131
+ updates.periodIncome = updates.periodIncome.add(translate(item, item.data.periodIncome));
3132
+
3109
3133
  if (item.position.instrument.type === InstrumentType.CASH) {
3110
3134
  updates.cashTotal = updates.cashTotal.add(translate(item, item.data.market));
3111
3135
  }
@@ -3121,7 +3145,10 @@ module.exports = (() => {
3121
3145
  summaryTotalPrevious2: Decimal.ZERO,
3122
3146
  marketPrevious: Decimal.ZERO,
3123
3147
  marketPrevious2: Decimal.ZERO,
3124
- cashTotal: Decimal.ZERO
3148
+ periodRealized: Decimal.ZERO,
3149
+ periodUnrealized: Decimal.ZERO,
3150
+ periodIncome: Decimal.ZERO,
3151
+ cashTotal: Decimal.ZERO,
3125
3152
  });
3126
3153
 
3127
3154
  actual.basis = updates.basis;
@@ -3133,6 +3160,9 @@ module.exports = (() => {
3133
3160
  actual.summaryTotalPrevious2 = updates.summaryTotalPrevious2;
3134
3161
  actual.marketPrevious = updates.marketPrevious;
3135
3162
  actual.marketPrevious2 = updates.marketPrevious2;
3163
+ actual.periodRealized = updates.periodRealized;
3164
+ actual.periodUnrealized = updates.periodUnrealized;
3165
+ actual.periodIncome = updates.periodIncome;
3136
3166
  actual.cashTotal = updates.cashTotal;
3137
3167
 
3138
3168
  format.basis = formatCurrency(actual.basis, currency);
@@ -3146,6 +3176,9 @@ module.exports = (() => {
3146
3176
  format.summaryTotalPrevious2Negative = updates.summaryTotalPrevious2.getIsNegative();
3147
3177
  format.marketPrevious = formatCurrency(updates.marketPrevious, currency);
3148
3178
  format.marketPrevious2 = formatCurrency(updates.marketPrevious2, currency);
3179
+ format.periodRealized = formatCurrency(updates.periodRealized, currency);
3180
+ format.periodUnrealized = formatCurrency(updates.periodUnrealized, currency);
3181
+ format.periodIncome = formatCurrency(updates.periodIncome, currency);
3149
3182
  format.cashTotal = formatCurrency(updates.cashTotal, currency);
3150
3183
 
3151
3184
  calculateUnrealizedPercent(group);
@@ -3432,6 +3465,9 @@ module.exports = (() => {
3432
3465
  this._data.income = null;
3433
3466
  this._data.basisPrice = null;
3434
3467
 
3468
+ this._data.realizedPeriod = null;
3469
+ this._data.unrealizedPeriod = null;
3470
+
3435
3471
  this._data.newsExists = false;
3436
3472
  this._data.fundamental = { };
3437
3473
  this._data.locked = getIsLocked(position);
@@ -3722,8 +3758,13 @@ module.exports = (() => {
3722
3758
  function calculateStaticData(item) {
3723
3759
  const position = item.position;
3724
3760
  const snapshot = getSnapshot(position, item.currentSummary, item._reporting);
3761
+
3725
3762
  const previousSummaries = item.previousSummaries;
3726
3763
 
3764
+ const previousSummary1 = getPreviousSummary(previousSummaries, 1);
3765
+ const previousSummary2 = getPreviousSummary(previousSummaries, 2);
3766
+ const previousSummary3 = getPreviousSummary(previousSummaries, 3);
3767
+
3727
3768
  const data = item._data;
3728
3769
 
3729
3770
  data.previousPrice = position.previous || null;
@@ -3743,19 +3784,18 @@ module.exports = (() => {
3743
3784
 
3744
3785
  data.income = snapshot.income;
3745
3786
 
3746
- const previousSummary1 = getPreviousSummary(previousSummaries, 1);
3747
- const previousSummary2 = getPreviousSummary(previousSummaries, 2);
3748
- const previousSummary3 = getPreviousSummary(previousSummaries, 3);
3749
-
3750
3787
  data.summaryTotalCurrent = calculateSummaryTotal(item.currentSummary, previousSummary1);
3751
3788
  data.summaryTotalPrevious = calculateSummaryTotal(previousSummary1, previousSummary2);
3752
3789
  data.summaryTotalPrevious2 = calculateSummaryTotal(previousSummary2, previousSummary3);
3753
3790
 
3754
3791
  data.marketPrevious = previousSummary1 === null ? Decimal.ZERO : previousSummary1.end.value;
3755
3792
  data.marketPrevious2 = previousSummary2 === null ? Decimal.ZERO : previousSummary2.end.value;
3756
-
3757
3793
  data.quantityPrevious = previousSummary1 === null ? Decimal.ZERO : previousSummary1.end.open;
3758
3794
 
3795
+ data.periodRealized = calculateRealizedPeriod(item.currentSummary, previousSummary1);
3796
+ data.periodUnrealized = calculateUnrealizedPeriod(item.currentSummary, previousSummary1);
3797
+ data.periodIncome = calculateIncomePeriod(item.currentSummary, previousSummary1);
3798
+
3759
3799
  if (snapshot.open.getIsZero()) {
3760
3800
  data.basisPrice = Decimal.ZERO;
3761
3801
  } else {
@@ -3897,6 +3937,48 @@ module.exports = (() => {
3897
3937
  return returnRef;
3898
3938
  }
3899
3939
 
3940
+ function calculateRealizedPeriod(currentSummary, previousSummary) {
3941
+ let returnRef;
3942
+
3943
+ if (currentSummary) {
3944
+ const period = currentSummary.period;
3945
+
3946
+ returnRef = period.realized;
3947
+ } else {
3948
+ returnRef = Decimal.ZERO;
3949
+ }
3950
+
3951
+ return returnRef;
3952
+ }
3953
+
3954
+ function calculateUnrealizedPeriod(currentSummary, previousSummary) {
3955
+ let returnRef;
3956
+
3957
+ if (currentSummary) {
3958
+ const period = currentSummary.period;
3959
+
3960
+ returnRef = period.unrealized.subtract(previousSummary !== null ? previousSummary.period.unrealized : Decimal.ZERO);
3961
+ } else {
3962
+ returnRef = Decimal.ZERO;
3963
+ }
3964
+
3965
+ return returnRef;
3966
+ }
3967
+
3968
+ function calculateIncomePeriod(currentSummary, previousSummary) {
3969
+ let returnRef;
3970
+
3971
+ if (currentSummary) {
3972
+ const period = currentSummary.period;
3973
+
3974
+ returnRef = period.income;
3975
+ } else {
3976
+ returnRef = Decimal.ZERO;
3977
+ }
3978
+
3979
+ return returnRef;
3980
+ }
3981
+
3900
3982
  function getPreviousSummary(previousSummaries, count) {
3901
3983
  const index = previousSummaries.length - count;
3902
3984
 
@@ -6186,11 +6268,12 @@ module.exports = function () {
6186
6268
  }
6187
6269
 
6188
6270
  /**
6189
- * Clones a {@link Day} instance.
6271
+ * Converts a string (which matches the output of {@link Day#format} into
6272
+ * a {@link Day} instance.
6190
6273
  *
6191
6274
  * @public
6192
6275
  * @static
6193
- * @param {Day} value
6276
+ * @param {String} value
6194
6277
  * @returns {Day}
6195
6278
  */
6196
6279
 
@@ -6231,24 +6314,6 @@ module.exports = function () {
6231
6314
  return this._day;
6232
6315
  }
6233
6316
  }], [{
6234
- key: 'clone',
6235
- value: function clone(value) {
6236
- assert.argumentIsRequired(value, 'value', Day, 'Day');
6237
-
6238
- return new Day(value.year, value.month, value.day);
6239
- }
6240
-
6241
- /**
6242
- * Converts a string (which matches the output of {@link Day#format} into
6243
- * a {@link Day} instance.
6244
- *
6245
- * @public
6246
- * @static
6247
- * @param {String} value
6248
- * @returns {Day}
6249
- */
6250
-
6251
- }, {
6252
6317
  key: 'parse',
6253
6318
  value: function parse(value) {
6254
6319
  assert.argumentIsRequired(value, 'value', String);
@@ -6565,17 +6630,15 @@ module.exports = function () {
6565
6630
  *
6566
6631
  * @public
6567
6632
  * @param {Boolean=} approximate
6568
- * @param {Number=} places
6569
6633
  * @returns {Boolean}
6570
6634
  */
6571
6635
 
6572
6636
  }, {
6573
6637
  key: 'getIsZero',
6574
- value: function getIsZero(approximate, places) {
6638
+ value: function getIsZero(approximate) {
6575
6639
  assert.argumentIsOptional(approximate, 'approximate', Boolean);
6576
- assert.argumentIsOptional(places, 'places', Number);
6577
6640
 
6578
- return this._big.eq(zero) || is.boolean(approximate) && approximate && this.round(places || Big.DP, RoundingMode.NORMAL).getIsZero();
6641
+ return this._big.eq(zero) || is.boolean(approximate) && approximate && this.round(20, RoundingMode.NORMAL).getIsZero();
6579
6642
  }
6580
6643
 
6581
6644
  /**
@@ -6674,43 +6737,6 @@ module.exports = function () {
6674
6737
  return this._big.eq(getBig(other));
6675
6738
  }
6676
6739
 
6677
- /**
6678
- * Returns true if the current instance is an integer (i.e. has no decimal
6679
- * component).
6680
- *
6681
- * @public
6682
- * @return {Boolean}
6683
- */
6684
-
6685
- }, {
6686
- key: 'getIsInteger',
6687
- value: function getIsInteger() {
6688
- return this.getIsEqual(this.round(0));
6689
- }
6690
-
6691
- /**
6692
- * Returns the number of decimal places used.
6693
- *
6694
- * @public
6695
- * @returns {Number}
6696
- */
6697
-
6698
- }, {
6699
- key: 'getDecimalPlaces',
6700
- value: function getDecimalPlaces() {
6701
- var matches = this.toFixed().match(/-?\d*\.(\d*)/);
6702
-
6703
- var returnVal = void 0;
6704
-
6705
- if (matches === null) {
6706
- returnVal = 0;
6707
- } else {
6708
- returnVal = matches[1].length;
6709
- }
6710
-
6711
- return returnVal;
6712
- }
6713
-
6714
6740
  /**
6715
6741
  * Emits a floating point value that approximates the value of the current
6716
6742
  * instance.
@@ -6759,11 +6785,10 @@ module.exports = function () {
6759
6785
  }
6760
6786
 
6761
6787
  /**
6762
- * Clones a {@link Decimal} instance.
6788
+ * Parses the value emitted by {@link Decimal#toJSON}.
6763
6789
  *
6764
6790
  * @public
6765
- * @static
6766
- * @param {Decimal} value
6791
+ * @param {String} value
6767
6792
  * @returns {Decimal}
6768
6793
  */
6769
6794
 
@@ -6773,22 +6798,6 @@ module.exports = function () {
6773
6798
  return '[Decimal]';
6774
6799
  }
6775
6800
  }], [{
6776
- key: 'clone',
6777
- value: function clone(value) {
6778
- assert.argumentIsRequired(value, 'value', Decimal, 'Decimal');
6779
-
6780
- return new Decimal(value._big);
6781
- }
6782
-
6783
- /**
6784
- * Parses the value emitted by {@link Decimal#toJSON}.
6785
- *
6786
- * @public
6787
- * @param {String} value
6788
- * @returns {Decimal}
6789
- */
6790
-
6791
- }, {
6792
6801
  key: 'parse',
6793
6802
  value: function parse(value) {
6794
6803
  return new Decimal(value);
@@ -7810,11 +7819,10 @@ module.exports = function () {
7810
7819
  }
7811
7820
 
7812
7821
  /**
7813
- * Clones a {@link Timestamp} instance.
7822
+ * Parses the value emitted by {@link Timestamp#toJSON}.
7814
7823
  *
7815
7824
  * @public
7816
- * @static
7817
- * @param {Timestamp} value
7825
+ * @param {Number} value
7818
7826
  * @returns {Timestamp}
7819
7827
  */
7820
7828
 
@@ -7850,22 +7858,6 @@ module.exports = function () {
7850
7858
  return this._moment;
7851
7859
  }
7852
7860
  }], [{
7853
- key: 'clone',
7854
- value: function clone(value) {
7855
- assert.argumentIsRequired(value, 'value', Timestamp, 'Timestamp');
7856
-
7857
- return new Timestamp(value._timestamp, value._timezone);
7858
- }
7859
-
7860
- /**
7861
- * Parses the value emitted by {@link Timestamp#toJSON}.
7862
- *
7863
- * @public
7864
- * @param {Number} value
7865
- * @returns {Timestamp}
7866
- */
7867
-
7868
- }, {
7869
7861
  key: 'parse',
7870
7862
  value: function parse(value) {
7871
7863
  return new Timestamp(value);