@barchart/portfolio-api-common 1.2.143 → 1.2.147

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.
@@ -145,6 +145,7 @@ module.exports = (() => {
145
145
  this._dataFormat.currentPrice = null;
146
146
  this._dataFormat.basis = null;
147
147
  this._dataFormat.realized = null;
148
+ this._dataFormat.realizedPercent = null;
148
149
  this._dataFormat.income = null;
149
150
  this._dataFormat.market = null;
150
151
  this._dataFormat.marketPercent = null;
@@ -690,9 +691,9 @@ module.exports = (() => {
690
691
  updates.marketPrevious = updates.marketPrevious.add(translate(item, item.data.marketPrevious));
691
692
  updates.marketPrevious2 = updates.marketPrevious2.add(translate(item, item.data.marketPrevious2));
692
693
 
694
+ updates.periodIncome = updates.periodIncome.add(translate(item, item.data.periodIncome));
693
695
  updates.periodRealized = updates.periodRealized.add(translate(item, item.data.periodRealized));
694
696
  updates.periodUnrealized = updates.periodUnrealized.add(translate(item, item.data.periodUnrealized));
695
- updates.periodIncome = updates.periodIncome.add(translate(item, item.data.periodIncome));
696
697
 
697
698
  if (item.position.instrument.type === InstrumentType.CASH) {
698
699
  updates.cashTotal = updates.cashTotal.add(translate(item, item.data.market));
@@ -714,9 +715,9 @@ module.exports = (() => {
714
715
  summaryTotalPrevious2: Decimal.ZERO,
715
716
  marketPrevious: Decimal.ZERO,
716
717
  marketPrevious2: Decimal.ZERO,
718
+ periodIncome: Decimal.ZERO,
717
719
  periodRealized: Decimal.ZERO,
718
720
  periodUnrealized: Decimal.ZERO,
719
- periodIncome: Decimal.ZERO,
720
721
  cashTotal: Decimal.ZERO,
721
722
  totalDivisor: Decimal.ZERO,
722
723
  periodDivisorCurrent: Decimal.ZERO,
@@ -733,9 +734,9 @@ module.exports = (() => {
733
734
  actual.summaryTotalPrevious2 = updates.summaryTotalPrevious2;
734
735
  actual.marketPrevious = updates.marketPrevious;
735
736
  actual.marketPrevious2 = updates.marketPrevious2;
737
+ actual.periodIncome = updates.periodIncome;
736
738
  actual.periodRealized = updates.periodRealized;
737
739
  actual.periodUnrealized = updates.periodUnrealized;
738
- actual.periodIncome = updates.periodIncome;
739
740
  actual.cashTotal = updates.cashTotal;
740
741
  actual.totalDivisor = updates.totalDivisor;
741
742
  actual.periodDivisorCurrent = updates.periodDivisorCurrent;
@@ -754,11 +755,12 @@ module.exports = (() => {
754
755
  format.summaryTotalPrevious2Negative = updates.summaryTotalPrevious2.getIsNegative();
755
756
  format.marketPrevious = formatCurrency(updates.marketPrevious, currency);
756
757
  format.marketPrevious2 = formatCurrency(updates.marketPrevious2, currency);
758
+ format.periodIncome = formatCurrency(updates.periodIncome, currency);
757
759
  format.periodRealized = formatCurrency(updates.periodRealized, currency);
758
760
  format.periodUnrealized = formatCurrency(updates.periodUnrealized, currency);
759
- format.periodIncome = formatCurrency(updates.periodIncome, currency);
760
761
  format.cashTotal = formatCurrency(updates.cashTotal, currency);
761
762
 
763
+ calculateRealizedPercent(group);
762
764
  calculateUnrealizedPercent(group);
763
765
 
764
766
  actual.periodPercent = calculateGainPercent(actual.summaryTotalCurrent, actual.periodDivisorCurrent);
@@ -844,6 +846,7 @@ module.exports = (() => {
844
846
  updates.unrealized = updates.unrealized.add(translate(item, item.data.unrealized));
845
847
  updates.unrealizedToday = updates.unrealizedToday.add(translate(item, item.data.unrealizedToday));
846
848
  updates.summaryTotalCurrent = updates.summaryTotalCurrent.add(translate(item, item.data.periodGain));
849
+ updates.periodUnrealized = updates.periodUnrealized.add(translate(item, item.data.periodUnrealized));
847
850
 
848
851
  return updates;
849
852
  }, {
@@ -852,7 +855,8 @@ module.exports = (() => {
852
855
  marketDirection: unchanged,
853
856
  unrealized: Decimal.ZERO,
854
857
  unrealizedToday: Decimal.ZERO,
855
- summaryTotalCurrent: Decimal.ZERO
858
+ summaryTotalCurrent: Decimal.ZERO,
859
+ periodUnrealized: Decimal.ZERO
856
860
  });
857
861
  } else {
858
862
  updates = {
@@ -861,7 +865,8 @@ module.exports = (() => {
861
865
  marketDirection: { up: item.data.marketChange.getIsPositive(), down: item.data.marketChange.getIsNegative() },
862
866
  unrealized: actual.unrealized.add(translate(item, item.data.unrealizedChange)),
863
867
  unrealizedToday: actual.unrealizedToday.add(translate(item, item.data.unrealizedTodayChange)),
864
- summaryTotalCurrent: actual.summaryTotalCurrent.add(translate(item, item.data.periodGainChange))
868
+ summaryTotalCurrent: actual.summaryTotalCurrent.add(translate(item, item.data.periodGainChange)),
869
+ periodUnrealized: actual.periodUnrealized.add(translate(item, item.data.periodUnrealizedChange))
865
870
  };
866
871
  }
867
872
 
@@ -870,6 +875,7 @@ module.exports = (() => {
870
875
  actual.unrealized = updates.unrealized;
871
876
  actual.unrealizedToday = updates.unrealizedToday;
872
877
  actual.summaryTotalCurrent = updates.summaryTotalCurrent;
878
+ actual.periodUnrealized = updates.periodUnrealized;
873
879
 
874
880
  actual.total = updates.unrealized.add(actual.realized).add(actual.income);
875
881
  actual.totalPercent = calculateGainPercent(actual.total, actual.totalDivisor);
@@ -908,6 +914,8 @@ module.exports = (() => {
908
914
  format.summaryTotalCurrent = formatCurrency(actual.summaryTotalCurrent, currency);
909
915
  format.summaryTotalCurrentNegative = actual.summaryTotalCurrent.getIsNegative();
910
916
 
917
+ format.periodUnrealized = formatCurrency(actual.periodUnrealized, currency);
918
+
911
919
  format.total = formatCurrency(actual.total, currency);
912
920
  format.totalNegative = actual.total.getIsNegative();
913
921
  format.totalPercent = formatPercent(actual.totalPercent, 2);
@@ -915,14 +923,11 @@ module.exports = (() => {
915
923
  format.marketChange = formatCurrency(actual.marketChange, currency);
916
924
  format.marketChangePercent = formatPercent(actual.marketChangePercent, 2);
917
925
 
926
+ calculateRealizedPercent(group);
918
927
  calculateUnrealizedPercent(group);
919
928
 
920
929
  actual.periodPercent = calculateGainPercent(actual.summaryTotalCurrent, actual.periodDivisorCurrent);
921
930
  format.periodPercent = formatPercent(actual.periodPercent, 2);
922
-
923
- if (group.single && item) {
924
- actual.periodUnrealized = item.data.periodUnrealized;
925
- }
926
931
  }
927
932
 
928
933
  function calculateMarketPercent(group, rates, parentGroup, portfolioGroup) {
@@ -968,17 +973,39 @@ module.exports = (() => {
968
973
  }
969
974
  }
970
975
 
976
+ function calculateRealizedPercent(group) {
977
+ const actual = group._dataActual;
978
+ const format = group._dataFormat;
979
+
980
+ const openBasis = actual.basis;
981
+ const totalBasis = actual.totalDivisor;
982
+
983
+ const numerator = actual.realized;
984
+ const denominator = totalBasis.subtract(openBasis);
985
+
986
+ if (denominator.getIsZero()) {
987
+ actual.realizedPercent = Decimal.ZERO;
988
+ } else {
989
+ actual.realizedPercent = numerator.divide(denominator);
990
+ }
991
+
992
+ format.realizedPercent = formatPercent(actual.realizedPercent, 2);
993
+ }
994
+
971
995
  function calculateUnrealizedPercent(group) {
972
996
  const actual = group._dataActual;
973
997
  const format = group._dataFormat;
974
998
 
975
- if (actual.basis.getIsZero()) {
976
- actual.unrealizedPercent = null;
977
- format.unrealizedPercent = '—';
999
+ const numerator = actual.unrealized;
1000
+ const denominator = actual.basis;
1001
+
1002
+ if (denominator.getIsZero()) {
1003
+ actual.unrealizedPercent = Decimal.ZERO;
978
1004
  } else {
979
- actual.unrealizedPercent = actual.unrealized.divide(actual.basis);
980
- format.unrealizedPercent = formatPercent(actual.unrealizedPercent, 2);
1005
+ actual.unrealizedPercent = numerator.divide(denominator);
981
1006
  }
1007
+
1008
+ format.unrealizedPercent = formatPercent(actual.unrealizedPercent, 2);
982
1009
  }
983
1010
 
984
1011
  function calculateGainPercent(gain, basis) {
@@ -75,7 +75,9 @@ module.exports = (() => {
75
75
 
76
76
  this._data.periodIncome = null;
77
77
  this._data.periodRealized = null;
78
+
78
79
  this._data.periodUnrealized = null;
80
+ this._data.periodUnrealizedChange = null;
79
81
 
80
82
  this._data.periodPrice = null;
81
83
  this._data.periodPricePrevious = null;
@@ -420,14 +422,14 @@ module.exports = (() => {
420
422
  data.marketPrevious2 = previousSummary2 === null ? Decimal.ZERO : previousSummary2.end.value;
421
423
  data.quantityPrevious = previousSummary1 === null ? Decimal.ZERO : previousSummary1.end.open;
422
424
 
423
- data.periodIncome = currentSummary !== null ? currentSummary.period.income : Decimal.ZERO;
424
- data.periodRealized = currentSummary !== null ? currentSummary.period.realized : Decimal.ZERO;
425
- data.periodUnrealized = currentSummary !== null ? currentSummary.period.unrealized : Decimal.ZERO;
426
-
427
425
  data.periodGain = calculatePeriodGain(position.instrument.type, data.initiate, currentSummary, previousSummary1);
428
426
  data.periodGainPrevious = calculatePeriodGain(position.instrument.type, data.initiate, previousSummary1, previousSummary2);
429
427
  data.periodGainPrevious2 = calculatePeriodGain(position.instrument.type, data.initiate, previousSummary2, previousSummary3);
430
428
 
429
+ data.periodIncome = currentSummary !== null ? currentSummary.period.income : Decimal.ZERO;
430
+ data.periodRealized = currentSummary !== null ? currentSummary.period.realized : Decimal.ZERO;
431
+ data.periodUnrealized = calculatePeriodUnrealized(position.instrument.type, data.periodGain, data.periodRealized, data.periodIncome);
432
+
431
433
  data.periodDivisor = calculatePeriodDivisor(position.instrument.type, data.initiate, currentSummary, previousSummary1);
432
434
  data.periodDivisorPrevious = calculatePeriodDivisor(position.instrument.type, data.initiate, previousSummary1, previousSummary2);
433
435
  data.periodDivisorPrevious2 = calculatePeriodDivisor(position.instrument.type, data.initiate, previousSummary2, previousSummary3);
@@ -450,7 +452,7 @@ module.exports = (() => {
450
452
  data.periodPricePrevious = null;
451
453
  }
452
454
 
453
- data.totalDivisor = calculateTotalDivisor(position.instrument.type, data.initiate, currentSummary);
455
+ data.totalDivisor = calculateTotalDivisor(position.instrument.type, data.initiate, currentSummary, position);
454
456
  }
455
457
 
456
458
  function calculatePriceData(item, price) {
@@ -555,6 +557,18 @@ module.exports = (() => {
555
557
 
556
558
  data.periodGain = periodGain;
557
559
  data.periodGainChange = periodGainChange;
560
+
561
+ let periodUnrealized = calculatePeriodUnrealized(position.instrument.type, data.periodGain, data.periodRealized, data.periodIncome);
562
+ let periodUnrealizedChange;
563
+
564
+ if (data.periodUnrealized !== null) {
565
+ periodUnrealizedChange = periodUnrealized.subtract(data.periodUnrealized);
566
+ } else {
567
+ periodUnrealizedChange = Decimal.ZERO;
568
+ }
569
+
570
+ data.periodUnrealized = periodUnrealized;
571
+ data.periodUnrealizedChange = periodUnrealizedChange;
558
572
  } else {
559
573
  data.unrealized = Decimal.ZERO;
560
574
  data.unrealizedChange = Decimal.ZERO;
@@ -645,14 +659,30 @@ module.exports = (() => {
645
659
  return returnRef;
646
660
  }
647
661
 
648
- function calculateTotalDivisor(type, direction, finalSummary) {
662
+ function calculatePeriodUnrealized(type, periodGain, periodRealized, periodIncome) {
649
663
  let returnRef;
650
664
 
665
+ if (type !== InstrumentType.CASH) {
666
+ returnRef = periodRealized.add(periodIncome).subtract(periodGain).opposite();
667
+ } else {
668
+ returnRef = Decimal.ZERO;
669
+ }
670
+
671
+ return returnRef;
672
+ }
673
+
674
+ function calculateTotalDivisor(type, direction, finalSummary, position) {
675
+ let returnRef;
676
+
677
+ // 2019-06-05, BRI. We should be reading from the summary -- in case we are
678
+ // running for a previous period. However, the summary does not have buy and
679
+ // sell totals for the entire history. Could be added.
680
+
651
681
  if (finalSummary && type !== InstrumentType.CASH) {
652
682
  if (direction === PositionDirection.SHORT) {
653
- returnRef = finalSummary.period.sells;
683
+ returnRef = position.snapshot.sells;
654
684
  } else {
655
- returnRef = finalSummary.period.buys.opposite();
685
+ returnRef = position.snapshot.buys.opposite();
656
686
  }
657
687
  } else {
658
688
  returnRef = Decimal.ZERO;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.2.143",
3
+ "version": "1.2.147",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",
@@ -2667,6 +2667,7 @@ module.exports = (() => {
2667
2667
  this._dataFormat.currentPrice = null;
2668
2668
  this._dataFormat.basis = null;
2669
2669
  this._dataFormat.realized = null;
2670
+ this._dataFormat.realizedPercent = null;
2670
2671
  this._dataFormat.income = null;
2671
2672
  this._dataFormat.market = null;
2672
2673
  this._dataFormat.marketPercent = null;
@@ -3212,9 +3213,9 @@ module.exports = (() => {
3212
3213
  updates.marketPrevious = updates.marketPrevious.add(translate(item, item.data.marketPrevious));
3213
3214
  updates.marketPrevious2 = updates.marketPrevious2.add(translate(item, item.data.marketPrevious2));
3214
3215
 
3216
+ updates.periodIncome = updates.periodIncome.add(translate(item, item.data.periodIncome));
3215
3217
  updates.periodRealized = updates.periodRealized.add(translate(item, item.data.periodRealized));
3216
3218
  updates.periodUnrealized = updates.periodUnrealized.add(translate(item, item.data.periodUnrealized));
3217
- updates.periodIncome = updates.periodIncome.add(translate(item, item.data.periodIncome));
3218
3219
 
3219
3220
  if (item.position.instrument.type === InstrumentType.CASH) {
3220
3221
  updates.cashTotal = updates.cashTotal.add(translate(item, item.data.market));
@@ -3236,9 +3237,9 @@ module.exports = (() => {
3236
3237
  summaryTotalPrevious2: Decimal.ZERO,
3237
3238
  marketPrevious: Decimal.ZERO,
3238
3239
  marketPrevious2: Decimal.ZERO,
3240
+ periodIncome: Decimal.ZERO,
3239
3241
  periodRealized: Decimal.ZERO,
3240
3242
  periodUnrealized: Decimal.ZERO,
3241
- periodIncome: Decimal.ZERO,
3242
3243
  cashTotal: Decimal.ZERO,
3243
3244
  totalDivisor: Decimal.ZERO,
3244
3245
  periodDivisorCurrent: Decimal.ZERO,
@@ -3255,9 +3256,9 @@ module.exports = (() => {
3255
3256
  actual.summaryTotalPrevious2 = updates.summaryTotalPrevious2;
3256
3257
  actual.marketPrevious = updates.marketPrevious;
3257
3258
  actual.marketPrevious2 = updates.marketPrevious2;
3259
+ actual.periodIncome = updates.periodIncome;
3258
3260
  actual.periodRealized = updates.periodRealized;
3259
3261
  actual.periodUnrealized = updates.periodUnrealized;
3260
- actual.periodIncome = updates.periodIncome;
3261
3262
  actual.cashTotal = updates.cashTotal;
3262
3263
  actual.totalDivisor = updates.totalDivisor;
3263
3264
  actual.periodDivisorCurrent = updates.periodDivisorCurrent;
@@ -3276,11 +3277,12 @@ module.exports = (() => {
3276
3277
  format.summaryTotalPrevious2Negative = updates.summaryTotalPrevious2.getIsNegative();
3277
3278
  format.marketPrevious = formatCurrency(updates.marketPrevious, currency);
3278
3279
  format.marketPrevious2 = formatCurrency(updates.marketPrevious2, currency);
3280
+ format.periodIncome = formatCurrency(updates.periodIncome, currency);
3279
3281
  format.periodRealized = formatCurrency(updates.periodRealized, currency);
3280
3282
  format.periodUnrealized = formatCurrency(updates.periodUnrealized, currency);
3281
- format.periodIncome = formatCurrency(updates.periodIncome, currency);
3282
3283
  format.cashTotal = formatCurrency(updates.cashTotal, currency);
3283
3284
 
3285
+ calculateRealizedPercent(group);
3284
3286
  calculateUnrealizedPercent(group);
3285
3287
 
3286
3288
  actual.periodPercent = calculateGainPercent(actual.summaryTotalCurrent, actual.periodDivisorCurrent);
@@ -3366,6 +3368,7 @@ module.exports = (() => {
3366
3368
  updates.unrealized = updates.unrealized.add(translate(item, item.data.unrealized));
3367
3369
  updates.unrealizedToday = updates.unrealizedToday.add(translate(item, item.data.unrealizedToday));
3368
3370
  updates.summaryTotalCurrent = updates.summaryTotalCurrent.add(translate(item, item.data.periodGain));
3371
+ updates.periodUnrealized = updates.periodUnrealized.add(translate(item, item.data.periodUnrealized));
3369
3372
 
3370
3373
  return updates;
3371
3374
  }, {
@@ -3374,7 +3377,8 @@ module.exports = (() => {
3374
3377
  marketDirection: unchanged,
3375
3378
  unrealized: Decimal.ZERO,
3376
3379
  unrealizedToday: Decimal.ZERO,
3377
- summaryTotalCurrent: Decimal.ZERO
3380
+ summaryTotalCurrent: Decimal.ZERO,
3381
+ periodUnrealized: Decimal.ZERO
3378
3382
  });
3379
3383
  } else {
3380
3384
  updates = {
@@ -3383,7 +3387,8 @@ module.exports = (() => {
3383
3387
  marketDirection: { up: item.data.marketChange.getIsPositive(), down: item.data.marketChange.getIsNegative() },
3384
3388
  unrealized: actual.unrealized.add(translate(item, item.data.unrealizedChange)),
3385
3389
  unrealizedToday: actual.unrealizedToday.add(translate(item, item.data.unrealizedTodayChange)),
3386
- summaryTotalCurrent: actual.summaryTotalCurrent.add(translate(item, item.data.periodGainChange))
3390
+ summaryTotalCurrent: actual.summaryTotalCurrent.add(translate(item, item.data.periodGainChange)),
3391
+ periodUnrealized: actual.periodUnrealized.add(translate(item, item.data.periodUnrealizedChange))
3387
3392
  };
3388
3393
  }
3389
3394
 
@@ -3392,6 +3397,7 @@ module.exports = (() => {
3392
3397
  actual.unrealized = updates.unrealized;
3393
3398
  actual.unrealizedToday = updates.unrealizedToday;
3394
3399
  actual.summaryTotalCurrent = updates.summaryTotalCurrent;
3400
+ actual.periodUnrealized = updates.periodUnrealized;
3395
3401
 
3396
3402
  actual.total = updates.unrealized.add(actual.realized).add(actual.income);
3397
3403
  actual.totalPercent = calculateGainPercent(actual.total, actual.totalDivisor);
@@ -3430,6 +3436,8 @@ module.exports = (() => {
3430
3436
  format.summaryTotalCurrent = formatCurrency(actual.summaryTotalCurrent, currency);
3431
3437
  format.summaryTotalCurrentNegative = actual.summaryTotalCurrent.getIsNegative();
3432
3438
 
3439
+ format.periodUnrealized = formatCurrency(actual.periodUnrealized, currency);
3440
+
3433
3441
  format.total = formatCurrency(actual.total, currency);
3434
3442
  format.totalNegative = actual.total.getIsNegative();
3435
3443
  format.totalPercent = formatPercent(actual.totalPercent, 2);
@@ -3437,14 +3445,11 @@ module.exports = (() => {
3437
3445
  format.marketChange = formatCurrency(actual.marketChange, currency);
3438
3446
  format.marketChangePercent = formatPercent(actual.marketChangePercent, 2);
3439
3447
 
3448
+ calculateRealizedPercent(group);
3440
3449
  calculateUnrealizedPercent(group);
3441
3450
 
3442
3451
  actual.periodPercent = calculateGainPercent(actual.summaryTotalCurrent, actual.periodDivisorCurrent);
3443
3452
  format.periodPercent = formatPercent(actual.periodPercent, 2);
3444
-
3445
- if (group.single && item) {
3446
- actual.periodUnrealized = item.data.periodUnrealized;
3447
- }
3448
3453
  }
3449
3454
 
3450
3455
  function calculateMarketPercent(group, rates, parentGroup, portfolioGroup) {
@@ -3490,17 +3495,39 @@ module.exports = (() => {
3490
3495
  }
3491
3496
  }
3492
3497
 
3498
+ function calculateRealizedPercent(group) {
3499
+ const actual = group._dataActual;
3500
+ const format = group._dataFormat;
3501
+
3502
+ const openBasis = actual.basis;
3503
+ const totalBasis = actual.totalDivisor;
3504
+
3505
+ const numerator = actual.realized;
3506
+ const denominator = totalBasis.subtract(openBasis);
3507
+
3508
+ if (denominator.getIsZero()) {
3509
+ actual.realizedPercent = Decimal.ZERO;
3510
+ } else {
3511
+ actual.realizedPercent = numerator.divide(denominator);
3512
+ }
3513
+
3514
+ format.realizedPercent = formatPercent(actual.realizedPercent, 2);
3515
+ }
3516
+
3493
3517
  function calculateUnrealizedPercent(group) {
3494
3518
  const actual = group._dataActual;
3495
3519
  const format = group._dataFormat;
3496
3520
 
3497
- if (actual.basis.getIsZero()) {
3498
- actual.unrealizedPercent = null;
3499
- format.unrealizedPercent = '—';
3521
+ const numerator = actual.unrealized;
3522
+ const denominator = actual.basis;
3523
+
3524
+ if (denominator.getIsZero()) {
3525
+ actual.unrealizedPercent = Decimal.ZERO;
3500
3526
  } else {
3501
- actual.unrealizedPercent = actual.unrealized.divide(actual.basis);
3502
- format.unrealizedPercent = formatPercent(actual.unrealizedPercent, 2);
3527
+ actual.unrealizedPercent = numerator.divide(denominator);
3503
3528
  }
3529
+
3530
+ format.unrealizedPercent = formatPercent(actual.unrealizedPercent, 2);
3504
3531
  }
3505
3532
 
3506
3533
  function calculateGainPercent(gain, basis) {
@@ -3590,7 +3617,9 @@ module.exports = (() => {
3590
3617
 
3591
3618
  this._data.periodIncome = null;
3592
3619
  this._data.periodRealized = null;
3620
+
3593
3621
  this._data.periodUnrealized = null;
3622
+ this._data.periodUnrealizedChange = null;
3594
3623
 
3595
3624
  this._data.periodPrice = null;
3596
3625
  this._data.periodPricePrevious = null;
@@ -3935,14 +3964,14 @@ module.exports = (() => {
3935
3964
  data.marketPrevious2 = previousSummary2 === null ? Decimal.ZERO : previousSummary2.end.value;
3936
3965
  data.quantityPrevious = previousSummary1 === null ? Decimal.ZERO : previousSummary1.end.open;
3937
3966
 
3938
- data.periodIncome = currentSummary !== null ? currentSummary.period.income : Decimal.ZERO;
3939
- data.periodRealized = currentSummary !== null ? currentSummary.period.realized : Decimal.ZERO;
3940
- data.periodUnrealized = currentSummary !== null ? currentSummary.period.unrealized : Decimal.ZERO;
3941
-
3942
3967
  data.periodGain = calculatePeriodGain(position.instrument.type, data.initiate, currentSummary, previousSummary1);
3943
3968
  data.periodGainPrevious = calculatePeriodGain(position.instrument.type, data.initiate, previousSummary1, previousSummary2);
3944
3969
  data.periodGainPrevious2 = calculatePeriodGain(position.instrument.type, data.initiate, previousSummary2, previousSummary3);
3945
3970
 
3971
+ data.periodIncome = currentSummary !== null ? currentSummary.period.income : Decimal.ZERO;
3972
+ data.periodRealized = currentSummary !== null ? currentSummary.period.realized : Decimal.ZERO;
3973
+ data.periodUnrealized = calculatePeriodUnrealized(position.instrument.type, data.periodGain, data.periodRealized, data.periodIncome);
3974
+
3946
3975
  data.periodDivisor = calculatePeriodDivisor(position.instrument.type, data.initiate, currentSummary, previousSummary1);
3947
3976
  data.periodDivisorPrevious = calculatePeriodDivisor(position.instrument.type, data.initiate, previousSummary1, previousSummary2);
3948
3977
  data.periodDivisorPrevious2 = calculatePeriodDivisor(position.instrument.type, data.initiate, previousSummary2, previousSummary3);
@@ -3965,7 +3994,7 @@ module.exports = (() => {
3965
3994
  data.periodPricePrevious = null;
3966
3995
  }
3967
3996
 
3968
- data.totalDivisor = calculateTotalDivisor(position.instrument.type, data.initiate, currentSummary);
3997
+ data.totalDivisor = calculateTotalDivisor(position.instrument.type, data.initiate, currentSummary, position);
3969
3998
  }
3970
3999
 
3971
4000
  function calculatePriceData(item, price) {
@@ -4070,6 +4099,18 @@ module.exports = (() => {
4070
4099
 
4071
4100
  data.periodGain = periodGain;
4072
4101
  data.periodGainChange = periodGainChange;
4102
+
4103
+ let periodUnrealized = calculatePeriodUnrealized(position.instrument.type, data.periodGain, data.periodRealized, data.periodIncome);
4104
+ let periodUnrealizedChange;
4105
+
4106
+ if (data.periodUnrealized !== null) {
4107
+ periodUnrealizedChange = periodUnrealized.subtract(data.periodUnrealized);
4108
+ } else {
4109
+ periodUnrealizedChange = Decimal.ZERO;
4110
+ }
4111
+
4112
+ data.periodUnrealized = periodUnrealized;
4113
+ data.periodUnrealizedChange = periodUnrealizedChange;
4073
4114
  } else {
4074
4115
  data.unrealized = Decimal.ZERO;
4075
4116
  data.unrealizedChange = Decimal.ZERO;
@@ -4160,14 +4201,30 @@ module.exports = (() => {
4160
4201
  return returnRef;
4161
4202
  }
4162
4203
 
4163
- function calculateTotalDivisor(type, direction, finalSummary) {
4204
+ function calculatePeriodUnrealized(type, periodGain, periodRealized, periodIncome) {
4164
4205
  let returnRef;
4165
4206
 
4207
+ if (type !== InstrumentType.CASH) {
4208
+ returnRef = periodRealized.add(periodIncome).subtract(periodGain).opposite();
4209
+ } else {
4210
+ returnRef = Decimal.ZERO;
4211
+ }
4212
+
4213
+ return returnRef;
4214
+ }
4215
+
4216
+ function calculateTotalDivisor(type, direction, finalSummary, position) {
4217
+ let returnRef;
4218
+
4219
+ // 2019-06-05, BRI. We should be reading from the summary -- in case we are
4220
+ // running for a previous period. However, the summary does not have buy and
4221
+ // sell totals for the entire history. Could be added.
4222
+
4166
4223
  if (finalSummary && type !== InstrumentType.CASH) {
4167
4224
  if (direction === PositionDirection.SHORT) {
4168
- returnRef = finalSummary.period.sells;
4225
+ returnRef = position.snapshot.sells;
4169
4226
  } else {
4170
- returnRef = finalSummary.period.buys.opposite();
4227
+ returnRef = position.snapshot.buys.opposite();
4171
4228
  }
4172
4229
  } else {
4173
4230
  returnRef = Decimal.ZERO;
@@ -18444,7 +18501,9 @@ describe('When a position container data is gathered', () => {
18444
18501
  value: new Decimal(456),
18445
18502
  open: new Decimal(1),
18446
18503
  income: new Decimal(0),
18447
- gain: new Decimal(0)
18504
+ gain: new Decimal(0),
18505
+ buys: new Decimal(50),
18506
+ sells: new Decimal(0)
18448
18507
  }
18449
18508
  };
18450
18509
  }
@@ -30,7 +30,9 @@ describe('When a position container data is gathered', () => {
30
30
  value: new Decimal(456),
31
31
  open: new Decimal(1),
32
32
  income: new Decimal(0),
33
- gain: new Decimal(0)
33
+ gain: new Decimal(0),
34
+ buys: new Decimal(50),
35
+ sells: new Decimal(0)
34
36
  }
35
37
  };
36
38
  }