@barchart/portfolio-api-common 1.2.102 → 1.2.103

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.
@@ -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;
@@ -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.unrealized;
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.102",
3
+ "version": "1.2.103",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",
@@ -2610,6 +2610,14 @@ module.exports = (() => {
2610
2610
  this._dataFormat.cashTotal = null;
2611
2611
  this._dataFormat.portfolioType = null;
2612
2612
 
2613
+ this._dataActual.periodRealized = null;
2614
+ this._dataActual.periodUnrealized = null;
2615
+ this._dataActual.periodIncome = null;
2616
+
2617
+ this._dataFormat.periodRealized = null;
2618
+ this._dataFormat.periodUnrealized = null;
2619
+ this._dataFormat.periodIncome = null;
2620
+
2613
2621
  this._items.forEach((item) => {
2614
2622
  bindItem.call(this, item);
2615
2623
  });
@@ -3106,6 +3114,10 @@ module.exports = (() => {
3106
3114
  updates.marketPrevious = updates.marketPrevious.add(translate(item, item.data.marketPrevious));
3107
3115
  updates.marketPrevious2 = updates.marketPrevious2.add(translate(item, item.data.marketPrevious2));
3108
3116
 
3117
+ updates.periodRealized = updates.periodRealized.add(translate(item, item.data.periodRealized));
3118
+ updates.periodUnrealized = updates.periodUnrealized.add(translate(item, item.data.periodUnrealized));
3119
+ updates.periodIncome = updates.periodIncome.add(translate(item, item.data.periodIncome));
3120
+
3109
3121
  if (item.position.instrument.type === InstrumentType.CASH) {
3110
3122
  updates.cashTotal = updates.cashTotal.add(translate(item, item.data.market));
3111
3123
  }
@@ -3121,7 +3133,10 @@ module.exports = (() => {
3121
3133
  summaryTotalPrevious2: Decimal.ZERO,
3122
3134
  marketPrevious: Decimal.ZERO,
3123
3135
  marketPrevious2: Decimal.ZERO,
3124
- cashTotal: Decimal.ZERO
3136
+ periodRealized: Decimal.ZERO,
3137
+ periodUnrealized: Decimal.ZERO,
3138
+ periodIncome: Decimal.ZERO,
3139
+ cashTotal: Decimal.ZERO,
3125
3140
  });
3126
3141
 
3127
3142
  actual.basis = updates.basis;
@@ -3432,6 +3447,9 @@ module.exports = (() => {
3432
3447
  this._data.income = null;
3433
3448
  this._data.basisPrice = null;
3434
3449
 
3450
+ this._data.realizedPeriod = null;
3451
+ this._data.unrealizedPeriod = null;
3452
+
3435
3453
  this._data.newsExists = false;
3436
3454
  this._data.fundamental = { };
3437
3455
  this._data.locked = getIsLocked(position);
@@ -3722,8 +3740,13 @@ module.exports = (() => {
3722
3740
  function calculateStaticData(item) {
3723
3741
  const position = item.position;
3724
3742
  const snapshot = getSnapshot(position, item.currentSummary, item._reporting);
3743
+
3725
3744
  const previousSummaries = item.previousSummaries;
3726
3745
 
3746
+ const previousSummary1 = getPreviousSummary(previousSummaries, 1);
3747
+ const previousSummary2 = getPreviousSummary(previousSummaries, 2);
3748
+ const previousSummary3 = getPreviousSummary(previousSummaries, 3);
3749
+
3727
3750
  const data = item._data;
3728
3751
 
3729
3752
  data.previousPrice = position.previous || null;
@@ -3743,19 +3766,18 @@ module.exports = (() => {
3743
3766
 
3744
3767
  data.income = snapshot.income;
3745
3768
 
3746
- const previousSummary1 = getPreviousSummary(previousSummaries, 1);
3747
- const previousSummary2 = getPreviousSummary(previousSummaries, 2);
3748
- const previousSummary3 = getPreviousSummary(previousSummaries, 3);
3749
-
3750
3769
  data.summaryTotalCurrent = calculateSummaryTotal(item.currentSummary, previousSummary1);
3751
3770
  data.summaryTotalPrevious = calculateSummaryTotal(previousSummary1, previousSummary2);
3752
3771
  data.summaryTotalPrevious2 = calculateSummaryTotal(previousSummary2, previousSummary3);
3753
3772
 
3754
3773
  data.marketPrevious = previousSummary1 === null ? Decimal.ZERO : previousSummary1.end.value;
3755
3774
  data.marketPrevious2 = previousSummary2 === null ? Decimal.ZERO : previousSummary2.end.value;
3756
-
3757
3775
  data.quantityPrevious = previousSummary1 === null ? Decimal.ZERO : previousSummary1.end.open;
3758
3776
 
3777
+ data.periodRealized = calculateRealizedPeriod(item.currentSummary, previousSummary1);
3778
+ data.periodUnrealized = calculateUnrealizedPeriod(item.currentSummary, previousSummary1);
3779
+ data.periodIncome = calculateIncomePeriod(item.currentSummary, previousSummary1);
3780
+
3759
3781
  if (snapshot.open.getIsZero()) {
3760
3782
  data.basisPrice = Decimal.ZERO;
3761
3783
  } else {
@@ -3897,6 +3919,48 @@ module.exports = (() => {
3897
3919
  return returnRef;
3898
3920
  }
3899
3921
 
3922
+ function calculateRealizedPeriod(currentSummary, previousSummary) {
3923
+ let returnRef;
3924
+
3925
+ if (currentSummary) {
3926
+ const period = currentSummary.period;
3927
+
3928
+ returnRef = period.unrealized;
3929
+ } else {
3930
+ returnRef = Decimal.ZERO;
3931
+ }
3932
+
3933
+ return returnRef;
3934
+ }
3935
+
3936
+ function calculateUnrealizedPeriod(currentSummary, previousSummary) {
3937
+ let returnRef;
3938
+
3939
+ if (currentSummary) {
3940
+ const period = currentSummary.period;
3941
+
3942
+ returnRef = period.unrealized.subtract(previousSummary !== null ? previousSummary.period.unrealized : Decimal.ZERO);
3943
+ } else {
3944
+ returnRef = Decimal.ZERO;
3945
+ }
3946
+
3947
+ return returnRef;
3948
+ }
3949
+
3950
+ function calculateIncomePeriod(currentSummary, previousSummary) {
3951
+ let returnRef;
3952
+
3953
+ if (currentSummary) {
3954
+ const period = currentSummary.period;
3955
+
3956
+ returnRef = period.income;
3957
+ } else {
3958
+ returnRef = Decimal.ZERO;
3959
+ }
3960
+
3961
+ return returnRef;
3962
+ }
3963
+
3900
3964
  function getPreviousSummary(previousSummaries, count) {
3901
3965
  const index = previousSummaries.length - count;
3902
3966
 
@@ -6186,11 +6250,12 @@ module.exports = function () {
6186
6250
  }
6187
6251
 
6188
6252
  /**
6189
- * Clones a {@link Day} instance.
6253
+ * Converts a string (which matches the output of {@link Day#format} into
6254
+ * a {@link Day} instance.
6190
6255
  *
6191
6256
  * @public
6192
6257
  * @static
6193
- * @param {Day} value
6258
+ * @param {String} value
6194
6259
  * @returns {Day}
6195
6260
  */
6196
6261
 
@@ -6231,24 +6296,6 @@ module.exports = function () {
6231
6296
  return this._day;
6232
6297
  }
6233
6298
  }], [{
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
6299
  key: 'parse',
6253
6300
  value: function parse(value) {
6254
6301
  assert.argumentIsRequired(value, 'value', String);
@@ -6565,17 +6612,15 @@ module.exports = function () {
6565
6612
  *
6566
6613
  * @public
6567
6614
  * @param {Boolean=} approximate
6568
- * @param {Number=} places
6569
6615
  * @returns {Boolean}
6570
6616
  */
6571
6617
 
6572
6618
  }, {
6573
6619
  key: 'getIsZero',
6574
- value: function getIsZero(approximate, places) {
6620
+ value: function getIsZero(approximate) {
6575
6621
  assert.argumentIsOptional(approximate, 'approximate', Boolean);
6576
- assert.argumentIsOptional(places, 'places', Number);
6577
6622
 
6578
- return this._big.eq(zero) || is.boolean(approximate) && approximate && this.round(places || Big.DP, RoundingMode.NORMAL).getIsZero();
6623
+ return this._big.eq(zero) || is.boolean(approximate) && approximate && this.round(20, RoundingMode.NORMAL).getIsZero();
6579
6624
  }
6580
6625
 
6581
6626
  /**
@@ -6674,43 +6719,6 @@ module.exports = function () {
6674
6719
  return this._big.eq(getBig(other));
6675
6720
  }
6676
6721
 
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
6722
  /**
6715
6723
  * Emits a floating point value that approximates the value of the current
6716
6724
  * instance.
@@ -6759,11 +6767,10 @@ module.exports = function () {
6759
6767
  }
6760
6768
 
6761
6769
  /**
6762
- * Clones a {@link Decimal} instance.
6770
+ * Parses the value emitted by {@link Decimal#toJSON}.
6763
6771
  *
6764
6772
  * @public
6765
- * @static
6766
- * @param {Decimal} value
6773
+ * @param {String} value
6767
6774
  * @returns {Decimal}
6768
6775
  */
6769
6776
 
@@ -6773,22 +6780,6 @@ module.exports = function () {
6773
6780
  return '[Decimal]';
6774
6781
  }
6775
6782
  }], [{
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
6783
  key: 'parse',
6793
6784
  value: function parse(value) {
6794
6785
  return new Decimal(value);
@@ -7810,11 +7801,10 @@ module.exports = function () {
7810
7801
  }
7811
7802
 
7812
7803
  /**
7813
- * Clones a {@link Timestamp} instance.
7804
+ * Parses the value emitted by {@link Timestamp#toJSON}.
7814
7805
  *
7815
7806
  * @public
7816
- * @static
7817
- * @param {Timestamp} value
7807
+ * @param {Number} value
7818
7808
  * @returns {Timestamp}
7819
7809
  */
7820
7810
 
@@ -7850,22 +7840,6 @@ module.exports = function () {
7850
7840
  return this._moment;
7851
7841
  }
7852
7842
  }], [{
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
7843
  key: 'parse',
7870
7844
  value: function parse(value) {
7871
7845
  return new Timestamp(value);