@barchart/portfolio-api-common 1.2.140 → 1.2.144

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.
@@ -137,6 +137,7 @@ module.exports = (() => {
137
137
  this._dataActual.marketChange = null;
138
138
  this._dataActual.marketChangePercent = null;
139
139
  this._dataActual.cashTotal = null;
140
+ this._dataActual.totalDivisor = null;
140
141
  this._dataActual.periodDivisorCurrent = null;
141
142
  this._dataActual.periodDivisorPrevious = null;
142
143
  this._dataActual.periodDivisorPrevious2 = null;
@@ -144,6 +145,7 @@ module.exports = (() => {
144
145
  this._dataFormat.currentPrice = null;
145
146
  this._dataFormat.basis = null;
146
147
  this._dataFormat.realized = null;
148
+ this._dataFormat.realizedPercent = null;
147
149
  this._dataFormat.income = null;
148
150
  this._dataFormat.market = null;
149
151
  this._dataFormat.marketPercent = null;
@@ -183,10 +185,12 @@ module.exports = (() => {
183
185
  this._dataFormat.periodRealized = null;
184
186
  this._dataFormat.periodUnrealized = null;
185
187
 
188
+ this._dataActual.totalPercent = null;
186
189
  this._dataActual.periodPercent = null;
187
190
  this._dataActual.periodPercentPrevious = null;
188
191
  this._dataActual.periodPercentPrevious2 = null;
189
192
 
193
+ this._dataFormat.totalPercent = null;
190
194
  this._dataFormat.periodPercent = null;
191
195
  this._dataFormat.periodPercentPrevious = null;
192
196
  this._dataFormat.periodPercentPrevious2 = null;
@@ -695,6 +699,7 @@ module.exports = (() => {
695
699
  updates.cashTotal = updates.cashTotal.add(translate(item, item.data.market));
696
700
  }
697
701
 
702
+ updates.totalDivisor = updates.periodDivisorCurrent.add(translate(item, item.data.totalDivisor));
698
703
  updates.periodDivisorCurrent = updates.periodDivisorCurrent.add(translate(item, item.data.periodDivisor));
699
704
  updates.periodDivisorPrevious = updates.periodDivisorPrevious.add(translate(item, item.data.periodDivisorPrevious));
700
705
  updates.periodDivisorPrevious2 = updates.periodDivisorPrevious2.add(translate(item, item.data.periodDivisorPrevious2));
@@ -714,6 +719,7 @@ module.exports = (() => {
714
719
  periodUnrealized: Decimal.ZERO,
715
720
  periodIncome: Decimal.ZERO,
716
721
  cashTotal: Decimal.ZERO,
722
+ totalDivisor: Decimal.ZERO,
717
723
  periodDivisorCurrent: Decimal.ZERO,
718
724
  periodDivisorPrevious: Decimal.ZERO,
719
725
  periodDivisorPrevious2: Decimal.ZERO
@@ -732,6 +738,7 @@ module.exports = (() => {
732
738
  actual.periodUnrealized = updates.periodUnrealized;
733
739
  actual.periodIncome = updates.periodIncome;
734
740
  actual.cashTotal = updates.cashTotal;
741
+ actual.totalDivisor = updates.totalDivisor;
735
742
  actual.periodDivisorCurrent = updates.periodDivisorCurrent;
736
743
  actual.periodDivisorPrevious = updates.periodDivisorPrevious;
737
744
  actual.periodDivisorPrevious2 = updates.periodDivisorPrevious2;
@@ -753,8 +760,17 @@ module.exports = (() => {
753
760
  format.periodIncome = formatCurrency(updates.periodIncome, currency);
754
761
  format.cashTotal = formatCurrency(updates.cashTotal, currency);
755
762
 
763
+ calculateRealizedPercent(group);
756
764
  calculateUnrealizedPercent(group);
757
765
 
766
+ actual.periodPercent = calculateGainPercent(actual.summaryTotalCurrent, actual.periodDivisorCurrent);
767
+ actual.periodPercentPrevious = calculateGainPercent(actual.summaryTotalPrevious, actual.periodDivisorPrevious);
768
+ actual.periodPercentPrevious2 = calculateGainPercent(actual.summaryTotalPrevious2, actual.periodDivisorPrevious2);
769
+
770
+ format.periodPercent = formatPercent(actual.periodPercent, 2);
771
+ format.periodPercentPrevious = formatPercent(actual.periodPercentPrevious, 2);
772
+ format.periodPercentPrevious2 = formatPercent(actual.periodPercentPrevious2, 2);
773
+
758
774
  const groupItems = group._items;
759
775
 
760
776
  if (group.single && groupItems.length === 1) {
@@ -762,28 +778,20 @@ module.exports = (() => {
762
778
 
763
779
  actual.quantity = item.data.quantity;
764
780
  actual.quantityPrevious = item.data.quantityPrevious;
765
-
766
- actual.basisPrice = item.data.basisPrice;
767
-
768
- actual.periodPrice = item.data.periodPrice;
769
- actual.periodPricePrevious = item.data.periodPricePrevious;
770
781
 
771
782
  format.quantity = formatDecimal(actual.quantity, 2);
772
783
  format.quantityPrevious = formatDecimal(actual.quantityPrevious, 2);
773
784
 
785
+ actual.basisPrice = item.data.basisPrice;
786
+
774
787
  format.basisPrice = formatCurrency(actual.basisPrice, currency);
775
788
 
789
+ actual.periodPrice = item.data.periodPrice;
790
+ actual.periodPricePrevious = item.data.periodPricePrevious;
791
+
776
792
  format.periodPrice = formatCurrency(actual.periodPrice, currency);
777
793
  format.periodPricePrevious = formatCurrency(actual.periodPricePrevious, currency);
778
794
 
779
- actual.periodPercent = calculatePeriodPercent(actual.summaryTotalCurrent, actual.periodDivisorCurrent);
780
- actual.periodPercentPrevious = calculatePeriodPercent(actual.summaryTotalPrevious, actual.periodDivisorPrevious);
781
- actual.periodPercentPrevious2 = calculatePeriodPercent(actual.summaryTotalPrevious2, actual.periodDivisorPrevious2);
782
-
783
- format.periodPercent = formatPercent(actual.periodPercent, 2);
784
- format.periodPercentPrevious = formatPercent(actual.periodPercentPrevious, 2);
785
- format.periodPercentPrevious2 = formatPercent(actual.periodPercentPrevious2, 2);
786
-
787
795
  format.invalid = definition.type === PositionLevelType.POSITION && item.invalid;
788
796
  format.locked = definition.type === PositionLevelType.POSITION && item.data.locked;
789
797
  }
@@ -864,7 +872,9 @@ module.exports = (() => {
864
872
  actual.unrealized = updates.unrealized;
865
873
  actual.unrealizedToday = updates.unrealizedToday;
866
874
  actual.summaryTotalCurrent = updates.summaryTotalCurrent;
875
+
867
876
  actual.total = updates.unrealized.add(actual.realized).add(actual.income);
877
+ actual.totalPercent = calculateGainPercent(actual.total, actual.totalDivisor);
868
878
 
869
879
  let marketChange = updates.market.subtract(actual.marketPrevious);
870
880
  let marketChangePercent;
@@ -902,17 +912,19 @@ module.exports = (() => {
902
912
 
903
913
  format.total = formatCurrency(actual.total, currency);
904
914
  format.totalNegative = actual.total.getIsNegative();
915
+ format.totalPercent = formatPercent(actual.totalPercent, 2);
905
916
 
906
917
  format.marketChange = formatCurrency(actual.marketChange, currency);
907
918
  format.marketChangePercent = formatPercent(actual.marketChangePercent, 2);
908
919
 
920
+ calculateRealizedPercent(group);
909
921
  calculateUnrealizedPercent(group);
910
922
 
923
+ actual.periodPercent = calculateGainPercent(actual.summaryTotalCurrent, actual.periodDivisorCurrent);
924
+ format.periodPercent = formatPercent(actual.periodPercent, 2);
925
+
911
926
  if (group.single && item) {
912
927
  actual.periodUnrealized = item.data.periodUnrealized;
913
-
914
- actual.periodPercent = calculatePeriodPercent(actual.summaryTotalCurrent, actual.periodDivisorCurrent);
915
- format.periodPercent = formatPercent(actual.periodPercent, 2);
916
928
  }
917
929
  }
918
930
 
@@ -959,6 +971,24 @@ module.exports = (() => {
959
971
  }
960
972
  }
961
973
 
974
+ function calculateRealizedPercent(group) {
975
+ const actual = group._dataActual;
976
+ const format = group._dataFormat;
977
+
978
+ const openBasis = actual.basis;
979
+ const totalBasis = actual.totalDivisor;
980
+
981
+ const closedBasis = totalBasis.subtract(openBasis);
982
+
983
+ if (closedBasis.getIsZero()) {
984
+ actual.realizedPercent = null;
985
+ format.realizedPercent = '—';
986
+ } else {
987
+ actual.realizedPercent = actual.realized.divide(closedBasis);
988
+ format.realizedPercent = formatPercent(actual.realizedPercent, 2);
989
+ }
990
+ }
991
+
962
992
  function calculateUnrealizedPercent(group) {
963
993
  const actual = group._dataActual;
964
994
  const format = group._dataFormat;
@@ -972,8 +1002,8 @@ module.exports = (() => {
972
1002
  }
973
1003
  }
974
1004
 
975
- function calculatePeriodPercent(periodSummaryTotal, periodDivisor) {
976
- return periodDivisor.getIsZero() ? Decimal.ZERO : periodSummaryTotal.divide(periodDivisor);
1005
+ function calculateGainPercent(gain, basis) {
1006
+ return basis.getIsZero() ? Decimal.ZERO : gain.divide(basis);
977
1007
  }
978
1008
 
979
1009
  const unchanged = { up: false, down: false };
@@ -94,6 +94,8 @@ module.exports = (() => {
94
94
 
95
95
  this._data.initiate = null;
96
96
 
97
+ this._data.totalDivisor = null;
98
+
97
99
  this._data.newsExists = false;
98
100
  this._data.fundamental = { };
99
101
  this._data.locked = getIsLocked(position);
@@ -422,13 +424,13 @@ module.exports = (() => {
422
424
  data.periodRealized = currentSummary !== null ? currentSummary.period.realized : Decimal.ZERO;
423
425
  data.periodUnrealized = currentSummary !== null ? currentSummary.period.unrealized : Decimal.ZERO;
424
426
 
425
- data.periodGain = calculatePeriodGain(data.initiate, currentSummary, previousSummary1);
426
- data.periodGainPrevious = calculatePeriodGain(data.initiate, previousSummary1, previousSummary2);
427
- data.periodGainPrevious2 = calculatePeriodGain(data.initiate, previousSummary2, previousSummary3);
427
+ data.periodGain = calculatePeriodGain(position.instrument.type, data.initiate, currentSummary, previousSummary1);
428
+ data.periodGainPrevious = calculatePeriodGain(position.instrument.type, data.initiate, previousSummary1, previousSummary2);
429
+ data.periodGainPrevious2 = calculatePeriodGain(position.instrument.type, data.initiate, previousSummary2, previousSummary3);
428
430
 
429
- data.periodDivisor = calculatePeriodDivisor(data.initiate, currentSummary, previousSummary1);
430
- data.periodDivisorPrevious = calculatePeriodDivisor(data.initiate, previousSummary1, previousSummary2);
431
- data.periodDivisorPrevious2 = calculatePeriodDivisor(data.initiate, previousSummary2, previousSummary3);
431
+ data.periodDivisor = calculatePeriodDivisor(position.instrument.type, data.initiate, currentSummary, previousSummary1);
432
+ data.periodDivisorPrevious = calculatePeriodDivisor(position.instrument.type, data.initiate, previousSummary1, previousSummary2);
433
+ data.periodDivisorPrevious2 = calculatePeriodDivisor(position.instrument.type, data.initiate, previousSummary2, previousSummary3);
432
434
 
433
435
  if (snapshot.open.getIsZero()) {
434
436
  data.basisPrice = Decimal.ZERO;
@@ -447,6 +449,8 @@ module.exports = (() => {
447
449
  } else {
448
450
  data.periodPricePrevious = null;
449
451
  }
452
+
453
+ data.totalDivisor = calculateTotalDivisor(position.instrument.type, data.initiate, currentSummary);
450
454
  }
451
455
 
452
456
  function calculatePriceData(item, price) {
@@ -540,7 +544,7 @@ module.exports = (() => {
540
544
  data.unrealized = unrealized;
541
545
  data.unrealizedChange = unrealizedChange;
542
546
 
543
- let periodGain = calculatePeriodGain(data.initiate, currentSummary, previousSummary, priceToUse);
547
+ let periodGain = calculatePeriodGain(position.instrument.type, data.initiate, currentSummary, previousSummary, priceToUse);
544
548
  let periodGainChange;
545
549
 
546
550
  if (data.periodGain !== null) {
@@ -585,10 +589,10 @@ module.exports = (() => {
585
589
  return direction || PositionDirection.LONG;
586
590
  }
587
591
 
588
- function calculatePeriodGain(direction, currentSummary, previousSummary, overridePrice) {
592
+ function calculatePeriodGain(type, direction, currentSummary, previousSummary, overridePrice) {
589
593
  let returnRef;
590
594
 
591
- if (currentSummary) {
595
+ if (currentSummary && type !== InstrumentType.CASH) {
592
596
  let startValue;
593
597
 
594
598
  if (previousSummary) {
@@ -617,10 +621,10 @@ module.exports = (() => {
617
621
  return returnRef;
618
622
  }
619
623
 
620
- function calculatePeriodDivisor(direction, currentSummary, previousSummary) {
624
+ function calculatePeriodDivisor(type, direction, currentSummary, previousSummary) {
621
625
  let returnRef;
622
626
 
623
- if (currentSummary) {
627
+ if (currentSummary && type !== InstrumentType.CASH) {
624
628
  let startValue;
625
629
 
626
630
  if (previousSummary) {
@@ -641,6 +645,22 @@ module.exports = (() => {
641
645
  return returnRef;
642
646
  }
643
647
 
648
+ function calculateTotalDivisor(type, direction, finalSummary) {
649
+ let returnRef;
650
+
651
+ if (finalSummary && type !== InstrumentType.CASH) {
652
+ if (direction === PositionDirection.SHORT) {
653
+ returnRef = finalSummary.period.sells;
654
+ } else {
655
+ returnRef = finalSummary.period.buys.opposite();
656
+ }
657
+ } else {
658
+ returnRef = Decimal.ZERO;
659
+ }
660
+
661
+ return returnRef;
662
+ }
663
+
644
664
  function getPreviousSummary(previousSummaries, count) {
645
665
  const index = previousSummaries.length - count;
646
666
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.2.140",
3
+ "version": "1.2.144",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",
@@ -2659,6 +2659,7 @@ module.exports = (() => {
2659
2659
  this._dataActual.marketChange = null;
2660
2660
  this._dataActual.marketChangePercent = null;
2661
2661
  this._dataActual.cashTotal = null;
2662
+ this._dataActual.totalDivisor = null;
2662
2663
  this._dataActual.periodDivisorCurrent = null;
2663
2664
  this._dataActual.periodDivisorPrevious = null;
2664
2665
  this._dataActual.periodDivisorPrevious2 = null;
@@ -2666,6 +2667,7 @@ module.exports = (() => {
2666
2667
  this._dataFormat.currentPrice = null;
2667
2668
  this._dataFormat.basis = null;
2668
2669
  this._dataFormat.realized = null;
2670
+ this._dataFormat.realizedPercent = null;
2669
2671
  this._dataFormat.income = null;
2670
2672
  this._dataFormat.market = null;
2671
2673
  this._dataFormat.marketPercent = null;
@@ -2705,10 +2707,12 @@ module.exports = (() => {
2705
2707
  this._dataFormat.periodRealized = null;
2706
2708
  this._dataFormat.periodUnrealized = null;
2707
2709
 
2710
+ this._dataActual.totalPercent = null;
2708
2711
  this._dataActual.periodPercent = null;
2709
2712
  this._dataActual.periodPercentPrevious = null;
2710
2713
  this._dataActual.periodPercentPrevious2 = null;
2711
2714
 
2715
+ this._dataFormat.totalPercent = null;
2712
2716
  this._dataFormat.periodPercent = null;
2713
2717
  this._dataFormat.periodPercentPrevious = null;
2714
2718
  this._dataFormat.periodPercentPrevious2 = null;
@@ -3217,6 +3221,7 @@ module.exports = (() => {
3217
3221
  updates.cashTotal = updates.cashTotal.add(translate(item, item.data.market));
3218
3222
  }
3219
3223
 
3224
+ updates.totalDivisor = updates.periodDivisorCurrent.add(translate(item, item.data.totalDivisor));
3220
3225
  updates.periodDivisorCurrent = updates.periodDivisorCurrent.add(translate(item, item.data.periodDivisor));
3221
3226
  updates.periodDivisorPrevious = updates.periodDivisorPrevious.add(translate(item, item.data.periodDivisorPrevious));
3222
3227
  updates.periodDivisorPrevious2 = updates.periodDivisorPrevious2.add(translate(item, item.data.periodDivisorPrevious2));
@@ -3236,6 +3241,7 @@ module.exports = (() => {
3236
3241
  periodUnrealized: Decimal.ZERO,
3237
3242
  periodIncome: Decimal.ZERO,
3238
3243
  cashTotal: Decimal.ZERO,
3244
+ totalDivisor: Decimal.ZERO,
3239
3245
  periodDivisorCurrent: Decimal.ZERO,
3240
3246
  periodDivisorPrevious: Decimal.ZERO,
3241
3247
  periodDivisorPrevious2: Decimal.ZERO
@@ -3254,6 +3260,7 @@ module.exports = (() => {
3254
3260
  actual.periodUnrealized = updates.periodUnrealized;
3255
3261
  actual.periodIncome = updates.periodIncome;
3256
3262
  actual.cashTotal = updates.cashTotal;
3263
+ actual.totalDivisor = updates.totalDivisor;
3257
3264
  actual.periodDivisorCurrent = updates.periodDivisorCurrent;
3258
3265
  actual.periodDivisorPrevious = updates.periodDivisorPrevious;
3259
3266
  actual.periodDivisorPrevious2 = updates.periodDivisorPrevious2;
@@ -3275,8 +3282,17 @@ module.exports = (() => {
3275
3282
  format.periodIncome = formatCurrency(updates.periodIncome, currency);
3276
3283
  format.cashTotal = formatCurrency(updates.cashTotal, currency);
3277
3284
 
3285
+ calculateRealizedPercent(group);
3278
3286
  calculateUnrealizedPercent(group);
3279
3287
 
3288
+ actual.periodPercent = calculateGainPercent(actual.summaryTotalCurrent, actual.periodDivisorCurrent);
3289
+ actual.periodPercentPrevious = calculateGainPercent(actual.summaryTotalPrevious, actual.periodDivisorPrevious);
3290
+ actual.periodPercentPrevious2 = calculateGainPercent(actual.summaryTotalPrevious2, actual.periodDivisorPrevious2);
3291
+
3292
+ format.periodPercent = formatPercent(actual.periodPercent, 2);
3293
+ format.periodPercentPrevious = formatPercent(actual.periodPercentPrevious, 2);
3294
+ format.periodPercentPrevious2 = formatPercent(actual.periodPercentPrevious2, 2);
3295
+
3280
3296
  const groupItems = group._items;
3281
3297
 
3282
3298
  if (group.single && groupItems.length === 1) {
@@ -3284,28 +3300,20 @@ module.exports = (() => {
3284
3300
 
3285
3301
  actual.quantity = item.data.quantity;
3286
3302
  actual.quantityPrevious = item.data.quantityPrevious;
3287
-
3288
- actual.basisPrice = item.data.basisPrice;
3289
-
3290
- actual.periodPrice = item.data.periodPrice;
3291
- actual.periodPricePrevious = item.data.periodPricePrevious;
3292
3303
 
3293
3304
  format.quantity = formatDecimal(actual.quantity, 2);
3294
3305
  format.quantityPrevious = formatDecimal(actual.quantityPrevious, 2);
3295
3306
 
3307
+ actual.basisPrice = item.data.basisPrice;
3308
+
3296
3309
  format.basisPrice = formatCurrency(actual.basisPrice, currency);
3297
3310
 
3311
+ actual.periodPrice = item.data.periodPrice;
3312
+ actual.periodPricePrevious = item.data.periodPricePrevious;
3313
+
3298
3314
  format.periodPrice = formatCurrency(actual.periodPrice, currency);
3299
3315
  format.periodPricePrevious = formatCurrency(actual.periodPricePrevious, currency);
3300
3316
 
3301
- actual.periodPercent = calculatePeriodPercent(actual.summaryTotalCurrent, actual.periodDivisorCurrent);
3302
- actual.periodPercentPrevious = calculatePeriodPercent(actual.summaryTotalPrevious, actual.periodDivisorPrevious);
3303
- actual.periodPercentPrevious2 = calculatePeriodPercent(actual.summaryTotalPrevious2, actual.periodDivisorPrevious2);
3304
-
3305
- format.periodPercent = formatPercent(actual.periodPercent, 2);
3306
- format.periodPercentPrevious = formatPercent(actual.periodPercentPrevious, 2);
3307
- format.periodPercentPrevious2 = formatPercent(actual.periodPercentPrevious2, 2);
3308
-
3309
3317
  format.invalid = definition.type === PositionLevelType.POSITION && item.invalid;
3310
3318
  format.locked = definition.type === PositionLevelType.POSITION && item.data.locked;
3311
3319
  }
@@ -3386,7 +3394,9 @@ module.exports = (() => {
3386
3394
  actual.unrealized = updates.unrealized;
3387
3395
  actual.unrealizedToday = updates.unrealizedToday;
3388
3396
  actual.summaryTotalCurrent = updates.summaryTotalCurrent;
3397
+
3389
3398
  actual.total = updates.unrealized.add(actual.realized).add(actual.income);
3399
+ actual.totalPercent = calculateGainPercent(actual.total, actual.totalDivisor);
3390
3400
 
3391
3401
  let marketChange = updates.market.subtract(actual.marketPrevious);
3392
3402
  let marketChangePercent;
@@ -3424,17 +3434,19 @@ module.exports = (() => {
3424
3434
 
3425
3435
  format.total = formatCurrency(actual.total, currency);
3426
3436
  format.totalNegative = actual.total.getIsNegative();
3437
+ format.totalPercent = formatPercent(actual.totalPercent, 2);
3427
3438
 
3428
3439
  format.marketChange = formatCurrency(actual.marketChange, currency);
3429
3440
  format.marketChangePercent = formatPercent(actual.marketChangePercent, 2);
3430
3441
 
3442
+ calculateRealizedPercent(group);
3431
3443
  calculateUnrealizedPercent(group);
3432
3444
 
3445
+ actual.periodPercent = calculateGainPercent(actual.summaryTotalCurrent, actual.periodDivisorCurrent);
3446
+ format.periodPercent = formatPercent(actual.periodPercent, 2);
3447
+
3433
3448
  if (group.single && item) {
3434
3449
  actual.periodUnrealized = item.data.periodUnrealized;
3435
-
3436
- actual.periodPercent = calculatePeriodPercent(actual.summaryTotalCurrent, actual.periodDivisorCurrent);
3437
- format.periodPercent = formatPercent(actual.periodPercent, 2);
3438
3450
  }
3439
3451
  }
3440
3452
 
@@ -3481,6 +3493,24 @@ module.exports = (() => {
3481
3493
  }
3482
3494
  }
3483
3495
 
3496
+ function calculateRealizedPercent(group) {
3497
+ const actual = group._dataActual;
3498
+ const format = group._dataFormat;
3499
+
3500
+ const openBasis = actual.basis;
3501
+ const totalBasis = actual.totalDivisor;
3502
+
3503
+ const closedBasis = totalBasis.subtract(openBasis);
3504
+
3505
+ if (closedBasis.getIsZero()) {
3506
+ actual.realizedPercent = null;
3507
+ format.realizedPercent = '—';
3508
+ } else {
3509
+ actual.realizedPercent = actual.realized.divide(closedBasis);
3510
+ format.realizedPercent = formatPercent(actual.realizedPercent, 2);
3511
+ }
3512
+ }
3513
+
3484
3514
  function calculateUnrealizedPercent(group) {
3485
3515
  const actual = group._dataActual;
3486
3516
  const format = group._dataFormat;
@@ -3494,8 +3524,8 @@ module.exports = (() => {
3494
3524
  }
3495
3525
  }
3496
3526
 
3497
- function calculatePeriodPercent(periodSummaryTotal, periodDivisor) {
3498
- return periodDivisor.getIsZero() ? Decimal.ZERO : periodSummaryTotal.divide(periodDivisor);
3527
+ function calculateGainPercent(gain, basis) {
3528
+ return basis.getIsZero() ? Decimal.ZERO : gain.divide(basis);
3499
3529
  }
3500
3530
 
3501
3531
  const unchanged = { up: false, down: false };
@@ -3600,6 +3630,8 @@ module.exports = (() => {
3600
3630
 
3601
3631
  this._data.initiate = null;
3602
3632
 
3633
+ this._data.totalDivisor = null;
3634
+
3603
3635
  this._data.newsExists = false;
3604
3636
  this._data.fundamental = { };
3605
3637
  this._data.locked = getIsLocked(position);
@@ -3928,13 +3960,13 @@ module.exports = (() => {
3928
3960
  data.periodRealized = currentSummary !== null ? currentSummary.period.realized : Decimal.ZERO;
3929
3961
  data.periodUnrealized = currentSummary !== null ? currentSummary.period.unrealized : Decimal.ZERO;
3930
3962
 
3931
- data.periodGain = calculatePeriodGain(data.initiate, currentSummary, previousSummary1);
3932
- data.periodGainPrevious = calculatePeriodGain(data.initiate, previousSummary1, previousSummary2);
3933
- data.periodGainPrevious2 = calculatePeriodGain(data.initiate, previousSummary2, previousSummary3);
3963
+ data.periodGain = calculatePeriodGain(position.instrument.type, data.initiate, currentSummary, previousSummary1);
3964
+ data.periodGainPrevious = calculatePeriodGain(position.instrument.type, data.initiate, previousSummary1, previousSummary2);
3965
+ data.periodGainPrevious2 = calculatePeriodGain(position.instrument.type, data.initiate, previousSummary2, previousSummary3);
3934
3966
 
3935
- data.periodDivisor = calculatePeriodDivisor(data.initiate, currentSummary, previousSummary1);
3936
- data.periodDivisorPrevious = calculatePeriodDivisor(data.initiate, previousSummary1, previousSummary2);
3937
- data.periodDivisorPrevious2 = calculatePeriodDivisor(data.initiate, previousSummary2, previousSummary3);
3967
+ data.periodDivisor = calculatePeriodDivisor(position.instrument.type, data.initiate, currentSummary, previousSummary1);
3968
+ data.periodDivisorPrevious = calculatePeriodDivisor(position.instrument.type, data.initiate, previousSummary1, previousSummary2);
3969
+ data.periodDivisorPrevious2 = calculatePeriodDivisor(position.instrument.type, data.initiate, previousSummary2, previousSummary3);
3938
3970
 
3939
3971
  if (snapshot.open.getIsZero()) {
3940
3972
  data.basisPrice = Decimal.ZERO;
@@ -3953,6 +3985,8 @@ module.exports = (() => {
3953
3985
  } else {
3954
3986
  data.periodPricePrevious = null;
3955
3987
  }
3988
+
3989
+ data.totalDivisor = calculateTotalDivisor(position.instrument.type, data.initiate, currentSummary);
3956
3990
  }
3957
3991
 
3958
3992
  function calculatePriceData(item, price) {
@@ -4046,7 +4080,7 @@ module.exports = (() => {
4046
4080
  data.unrealized = unrealized;
4047
4081
  data.unrealizedChange = unrealizedChange;
4048
4082
 
4049
- let periodGain = calculatePeriodGain(data.initiate, currentSummary, previousSummary, priceToUse);
4083
+ let periodGain = calculatePeriodGain(position.instrument.type, data.initiate, currentSummary, previousSummary, priceToUse);
4050
4084
  let periodGainChange;
4051
4085
 
4052
4086
  if (data.periodGain !== null) {
@@ -4091,10 +4125,10 @@ module.exports = (() => {
4091
4125
  return direction || PositionDirection.LONG;
4092
4126
  }
4093
4127
 
4094
- function calculatePeriodGain(direction, currentSummary, previousSummary, overridePrice) {
4128
+ function calculatePeriodGain(type, direction, currentSummary, previousSummary, overridePrice) {
4095
4129
  let returnRef;
4096
4130
 
4097
- if (currentSummary) {
4131
+ if (currentSummary && type !== InstrumentType.CASH) {
4098
4132
  let startValue;
4099
4133
 
4100
4134
  if (previousSummary) {
@@ -4123,10 +4157,10 @@ module.exports = (() => {
4123
4157
  return returnRef;
4124
4158
  }
4125
4159
 
4126
- function calculatePeriodDivisor(direction, currentSummary, previousSummary) {
4160
+ function calculatePeriodDivisor(type, direction, currentSummary, previousSummary) {
4127
4161
  let returnRef;
4128
4162
 
4129
- if (currentSummary) {
4163
+ if (currentSummary && type !== InstrumentType.CASH) {
4130
4164
  let startValue;
4131
4165
 
4132
4166
  if (previousSummary) {
@@ -4147,6 +4181,22 @@ module.exports = (() => {
4147
4181
  return returnRef;
4148
4182
  }
4149
4183
 
4184
+ function calculateTotalDivisor(type, direction, finalSummary) {
4185
+ let returnRef;
4186
+
4187
+ if (finalSummary && type !== InstrumentType.CASH) {
4188
+ if (direction === PositionDirection.SHORT) {
4189
+ returnRef = finalSummary.period.sells;
4190
+ } else {
4191
+ returnRef = finalSummary.period.buys.opposite();
4192
+ }
4193
+ } else {
4194
+ returnRef = Decimal.ZERO;
4195
+ }
4196
+
4197
+ return returnRef;
4198
+ }
4199
+
4150
4200
  function getPreviousSummary(previousSummaries, count) {
4151
4201
  const index = previousSummaries.length - count;
4152
4202