@barchart/portfolio-api-common 1.2.144 → 1.3.1

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.
@@ -691,9 +691,9 @@ module.exports = (() => {
691
691
  updates.marketPrevious = updates.marketPrevious.add(translate(item, item.data.marketPrevious));
692
692
  updates.marketPrevious2 = updates.marketPrevious2.add(translate(item, item.data.marketPrevious2));
693
693
 
694
+ updates.periodIncome = updates.periodIncome.add(translate(item, item.data.periodIncome));
694
695
  updates.periodRealized = updates.periodRealized.add(translate(item, item.data.periodRealized));
695
696
  updates.periodUnrealized = updates.periodUnrealized.add(translate(item, item.data.periodUnrealized));
696
- updates.periodIncome = updates.periodIncome.add(translate(item, item.data.periodIncome));
697
697
 
698
698
  if (item.position.instrument.type === InstrumentType.CASH) {
699
699
  updates.cashTotal = updates.cashTotal.add(translate(item, item.data.market));
@@ -715,9 +715,9 @@ module.exports = (() => {
715
715
  summaryTotalPrevious2: Decimal.ZERO,
716
716
  marketPrevious: Decimal.ZERO,
717
717
  marketPrevious2: Decimal.ZERO,
718
+ periodIncome: Decimal.ZERO,
718
719
  periodRealized: Decimal.ZERO,
719
720
  periodUnrealized: Decimal.ZERO,
720
- periodIncome: Decimal.ZERO,
721
721
  cashTotal: Decimal.ZERO,
722
722
  totalDivisor: Decimal.ZERO,
723
723
  periodDivisorCurrent: Decimal.ZERO,
@@ -734,9 +734,9 @@ module.exports = (() => {
734
734
  actual.summaryTotalPrevious2 = updates.summaryTotalPrevious2;
735
735
  actual.marketPrevious = updates.marketPrevious;
736
736
  actual.marketPrevious2 = updates.marketPrevious2;
737
+ actual.periodIncome = updates.periodIncome;
737
738
  actual.periodRealized = updates.periodRealized;
738
739
  actual.periodUnrealized = updates.periodUnrealized;
739
- actual.periodIncome = updates.periodIncome;
740
740
  actual.cashTotal = updates.cashTotal;
741
741
  actual.totalDivisor = updates.totalDivisor;
742
742
  actual.periodDivisorCurrent = updates.periodDivisorCurrent;
@@ -755,9 +755,9 @@ module.exports = (() => {
755
755
  format.summaryTotalPrevious2Negative = updates.summaryTotalPrevious2.getIsNegative();
756
756
  format.marketPrevious = formatCurrency(updates.marketPrevious, currency);
757
757
  format.marketPrevious2 = formatCurrency(updates.marketPrevious2, currency);
758
+ format.periodIncome = formatCurrency(updates.periodIncome, currency);
758
759
  format.periodRealized = formatCurrency(updates.periodRealized, currency);
759
760
  format.periodUnrealized = formatCurrency(updates.periodUnrealized, currency);
760
- format.periodIncome = formatCurrency(updates.periodIncome, currency);
761
761
  format.cashTotal = formatCurrency(updates.cashTotal, currency);
762
762
 
763
763
  calculateRealizedPercent(group);
@@ -846,6 +846,7 @@ module.exports = (() => {
846
846
  updates.unrealized = updates.unrealized.add(translate(item, item.data.unrealized));
847
847
  updates.unrealizedToday = updates.unrealizedToday.add(translate(item, item.data.unrealizedToday));
848
848
  updates.summaryTotalCurrent = updates.summaryTotalCurrent.add(translate(item, item.data.periodGain));
849
+ updates.periodUnrealized = updates.periodUnrealized.add(translate(item, item.data.periodUnrealized));
849
850
 
850
851
  return updates;
851
852
  }, {
@@ -854,7 +855,8 @@ module.exports = (() => {
854
855
  marketDirection: unchanged,
855
856
  unrealized: Decimal.ZERO,
856
857
  unrealizedToday: Decimal.ZERO,
857
- summaryTotalCurrent: Decimal.ZERO
858
+ summaryTotalCurrent: Decimal.ZERO,
859
+ periodUnrealized: Decimal.ZERO
858
860
  });
859
861
  } else {
860
862
  updates = {
@@ -863,7 +865,8 @@ module.exports = (() => {
863
865
  marketDirection: { up: item.data.marketChange.getIsPositive(), down: item.data.marketChange.getIsNegative() },
864
866
  unrealized: actual.unrealized.add(translate(item, item.data.unrealizedChange)),
865
867
  unrealizedToday: actual.unrealizedToday.add(translate(item, item.data.unrealizedTodayChange)),
866
- 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))
867
870
  };
868
871
  }
869
872
 
@@ -872,6 +875,7 @@ module.exports = (() => {
872
875
  actual.unrealized = updates.unrealized;
873
876
  actual.unrealizedToday = updates.unrealizedToday;
874
877
  actual.summaryTotalCurrent = updates.summaryTotalCurrent;
878
+ actual.periodUnrealized = updates.periodUnrealized;
875
879
 
876
880
  actual.total = updates.unrealized.add(actual.realized).add(actual.income);
877
881
  actual.totalPercent = calculateGainPercent(actual.total, actual.totalDivisor);
@@ -910,6 +914,8 @@ module.exports = (() => {
910
914
  format.summaryTotalCurrent = formatCurrency(actual.summaryTotalCurrent, currency);
911
915
  format.summaryTotalCurrentNegative = actual.summaryTotalCurrent.getIsNegative();
912
916
 
917
+ format.periodUnrealized = formatCurrency(actual.periodUnrealized, currency);
918
+
913
919
  format.total = formatCurrency(actual.total, currency);
914
920
  format.totalNegative = actual.total.getIsNegative();
915
921
  format.totalPercent = formatPercent(actual.totalPercent, 2);
@@ -922,10 +928,6 @@ module.exports = (() => {
922
928
 
923
929
  actual.periodPercent = calculateGainPercent(actual.summaryTotalCurrent, actual.periodDivisorCurrent);
924
930
  format.periodPercent = formatPercent(actual.periodPercent, 2);
925
-
926
- if (group.single && item) {
927
- actual.periodUnrealized = item.data.periodUnrealized;
928
- }
929
931
  }
930
932
 
931
933
  function calculateMarketPercent(group, rates, parentGroup, portfolioGroup) {
@@ -978,28 +980,32 @@ module.exports = (() => {
978
980
  const openBasis = actual.basis;
979
981
  const totalBasis = actual.totalDivisor;
980
982
 
981
- const closedBasis = totalBasis.subtract(openBasis);
983
+ const numerator = actual.realized;
984
+ const denominator = totalBasis.subtract(openBasis);
982
985
 
983
- if (closedBasis.getIsZero()) {
984
- actual.realizedPercent = null;
985
- format.realizedPercent = '—';
986
+ if (denominator.getIsZero()) {
987
+ actual.realizedPercent = Decimal.ZERO;
986
988
  } else {
987
- actual.realizedPercent = actual.realized.divide(closedBasis);
988
- format.realizedPercent = formatPercent(actual.realizedPercent, 2);
989
+ actual.realizedPercent = numerator.divide(denominator);
989
990
  }
991
+
992
+ format.realizedPercent = formatPercent(actual.realizedPercent, 2);
990
993
  }
991
994
 
992
995
  function calculateUnrealizedPercent(group) {
993
996
  const actual = group._dataActual;
994
997
  const format = group._dataFormat;
995
998
 
996
- if (actual.basis.getIsZero()) {
997
- actual.unrealizedPercent = null;
998
- format.unrealizedPercent = '—';
999
+ const numerator = actual.unrealized;
1000
+ const denominator = actual.basis;
1001
+
1002
+ if (denominator.getIsZero()) {
1003
+ actual.unrealizedPercent = Decimal.ZERO;
999
1004
  } else {
1000
- actual.unrealizedPercent = actual.unrealized.divide(actual.basis);
1001
- format.unrealizedPercent = formatPercent(actual.unrealizedPercent, 2);
1005
+ actual.unrealizedPercent = numerator.divide(denominator);
1002
1006
  }
1007
+
1008
+ format.unrealizedPercent = formatPercent(actual.unrealizedPercent, 2);
1003
1009
  }
1004
1010
 
1005
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.144",
3
+ "version": "1.3.1",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",
@@ -9,7 +9,7 @@
9
9
  },
10
10
  "scripts": {},
11
11
  "dependencies": {
12
- "@barchart/common-js": "~3.2.0",
12
+ "@barchart/common-js": "~3.3.0",
13
13
  "uuid": "3.1.0"
14
14
  },
15
15
  "devDependencies": {
@@ -3213,9 +3213,9 @@ module.exports = (() => {
3213
3213
  updates.marketPrevious = updates.marketPrevious.add(translate(item, item.data.marketPrevious));
3214
3214
  updates.marketPrevious2 = updates.marketPrevious2.add(translate(item, item.data.marketPrevious2));
3215
3215
 
3216
+ updates.periodIncome = updates.periodIncome.add(translate(item, item.data.periodIncome));
3216
3217
  updates.periodRealized = updates.periodRealized.add(translate(item, item.data.periodRealized));
3217
3218
  updates.periodUnrealized = updates.periodUnrealized.add(translate(item, item.data.periodUnrealized));
3218
- updates.periodIncome = updates.periodIncome.add(translate(item, item.data.periodIncome));
3219
3219
 
3220
3220
  if (item.position.instrument.type === InstrumentType.CASH) {
3221
3221
  updates.cashTotal = updates.cashTotal.add(translate(item, item.data.market));
@@ -3237,9 +3237,9 @@ module.exports = (() => {
3237
3237
  summaryTotalPrevious2: Decimal.ZERO,
3238
3238
  marketPrevious: Decimal.ZERO,
3239
3239
  marketPrevious2: Decimal.ZERO,
3240
+ periodIncome: Decimal.ZERO,
3240
3241
  periodRealized: Decimal.ZERO,
3241
3242
  periodUnrealized: Decimal.ZERO,
3242
- periodIncome: Decimal.ZERO,
3243
3243
  cashTotal: Decimal.ZERO,
3244
3244
  totalDivisor: Decimal.ZERO,
3245
3245
  periodDivisorCurrent: Decimal.ZERO,
@@ -3256,9 +3256,9 @@ module.exports = (() => {
3256
3256
  actual.summaryTotalPrevious2 = updates.summaryTotalPrevious2;
3257
3257
  actual.marketPrevious = updates.marketPrevious;
3258
3258
  actual.marketPrevious2 = updates.marketPrevious2;
3259
+ actual.periodIncome = updates.periodIncome;
3259
3260
  actual.periodRealized = updates.periodRealized;
3260
3261
  actual.periodUnrealized = updates.periodUnrealized;
3261
- actual.periodIncome = updates.periodIncome;
3262
3262
  actual.cashTotal = updates.cashTotal;
3263
3263
  actual.totalDivisor = updates.totalDivisor;
3264
3264
  actual.periodDivisorCurrent = updates.periodDivisorCurrent;
@@ -3277,9 +3277,9 @@ module.exports = (() => {
3277
3277
  format.summaryTotalPrevious2Negative = updates.summaryTotalPrevious2.getIsNegative();
3278
3278
  format.marketPrevious = formatCurrency(updates.marketPrevious, currency);
3279
3279
  format.marketPrevious2 = formatCurrency(updates.marketPrevious2, currency);
3280
+ format.periodIncome = formatCurrency(updates.periodIncome, currency);
3280
3281
  format.periodRealized = formatCurrency(updates.periodRealized, currency);
3281
3282
  format.periodUnrealized = formatCurrency(updates.periodUnrealized, currency);
3282
- format.periodIncome = formatCurrency(updates.periodIncome, currency);
3283
3283
  format.cashTotal = formatCurrency(updates.cashTotal, currency);
3284
3284
 
3285
3285
  calculateRealizedPercent(group);
@@ -3368,6 +3368,7 @@ module.exports = (() => {
3368
3368
  updates.unrealized = updates.unrealized.add(translate(item, item.data.unrealized));
3369
3369
  updates.unrealizedToday = updates.unrealizedToday.add(translate(item, item.data.unrealizedToday));
3370
3370
  updates.summaryTotalCurrent = updates.summaryTotalCurrent.add(translate(item, item.data.periodGain));
3371
+ updates.periodUnrealized = updates.periodUnrealized.add(translate(item, item.data.periodUnrealized));
3371
3372
 
3372
3373
  return updates;
3373
3374
  }, {
@@ -3376,7 +3377,8 @@ module.exports = (() => {
3376
3377
  marketDirection: unchanged,
3377
3378
  unrealized: Decimal.ZERO,
3378
3379
  unrealizedToday: Decimal.ZERO,
3379
- summaryTotalCurrent: Decimal.ZERO
3380
+ summaryTotalCurrent: Decimal.ZERO,
3381
+ periodUnrealized: Decimal.ZERO
3380
3382
  });
3381
3383
  } else {
3382
3384
  updates = {
@@ -3385,7 +3387,8 @@ module.exports = (() => {
3385
3387
  marketDirection: { up: item.data.marketChange.getIsPositive(), down: item.data.marketChange.getIsNegative() },
3386
3388
  unrealized: actual.unrealized.add(translate(item, item.data.unrealizedChange)),
3387
3389
  unrealizedToday: actual.unrealizedToday.add(translate(item, item.data.unrealizedTodayChange)),
3388
- 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))
3389
3392
  };
3390
3393
  }
3391
3394
 
@@ -3394,6 +3397,7 @@ module.exports = (() => {
3394
3397
  actual.unrealized = updates.unrealized;
3395
3398
  actual.unrealizedToday = updates.unrealizedToday;
3396
3399
  actual.summaryTotalCurrent = updates.summaryTotalCurrent;
3400
+ actual.periodUnrealized = updates.periodUnrealized;
3397
3401
 
3398
3402
  actual.total = updates.unrealized.add(actual.realized).add(actual.income);
3399
3403
  actual.totalPercent = calculateGainPercent(actual.total, actual.totalDivisor);
@@ -3432,6 +3436,8 @@ module.exports = (() => {
3432
3436
  format.summaryTotalCurrent = formatCurrency(actual.summaryTotalCurrent, currency);
3433
3437
  format.summaryTotalCurrentNegative = actual.summaryTotalCurrent.getIsNegative();
3434
3438
 
3439
+ format.periodUnrealized = formatCurrency(actual.periodUnrealized, currency);
3440
+
3435
3441
  format.total = formatCurrency(actual.total, currency);
3436
3442
  format.totalNegative = actual.total.getIsNegative();
3437
3443
  format.totalPercent = formatPercent(actual.totalPercent, 2);
@@ -3444,10 +3450,6 @@ module.exports = (() => {
3444
3450
 
3445
3451
  actual.periodPercent = calculateGainPercent(actual.summaryTotalCurrent, actual.periodDivisorCurrent);
3446
3452
  format.periodPercent = formatPercent(actual.periodPercent, 2);
3447
-
3448
- if (group.single && item) {
3449
- actual.periodUnrealized = item.data.periodUnrealized;
3450
- }
3451
3453
  }
3452
3454
 
3453
3455
  function calculateMarketPercent(group, rates, parentGroup, portfolioGroup) {
@@ -3500,28 +3502,32 @@ module.exports = (() => {
3500
3502
  const openBasis = actual.basis;
3501
3503
  const totalBasis = actual.totalDivisor;
3502
3504
 
3503
- const closedBasis = totalBasis.subtract(openBasis);
3505
+ const numerator = actual.realized;
3506
+ const denominator = totalBasis.subtract(openBasis);
3504
3507
 
3505
- if (closedBasis.getIsZero()) {
3506
- actual.realizedPercent = null;
3507
- format.realizedPercent = '—';
3508
+ if (denominator.getIsZero()) {
3509
+ actual.realizedPercent = Decimal.ZERO;
3508
3510
  } else {
3509
- actual.realizedPercent = actual.realized.divide(closedBasis);
3510
- format.realizedPercent = formatPercent(actual.realizedPercent, 2);
3511
+ actual.realizedPercent = numerator.divide(denominator);
3511
3512
  }
3513
+
3514
+ format.realizedPercent = formatPercent(actual.realizedPercent, 2);
3512
3515
  }
3513
3516
 
3514
3517
  function calculateUnrealizedPercent(group) {
3515
3518
  const actual = group._dataActual;
3516
3519
  const format = group._dataFormat;
3517
3520
 
3518
- if (actual.basis.getIsZero()) {
3519
- actual.unrealizedPercent = null;
3520
- format.unrealizedPercent = '—';
3521
+ const numerator = actual.unrealized;
3522
+ const denominator = actual.basis;
3523
+
3524
+ if (denominator.getIsZero()) {
3525
+ actual.unrealizedPercent = Decimal.ZERO;
3521
3526
  } else {
3522
- actual.unrealizedPercent = actual.unrealized.divide(actual.basis);
3523
- format.unrealizedPercent = formatPercent(actual.unrealizedPercent, 2);
3527
+ actual.unrealizedPercent = numerator.divide(denominator);
3524
3528
  }
3529
+
3530
+ format.unrealizedPercent = formatPercent(actual.unrealizedPercent, 2);
3525
3531
  }
3526
3532
 
3527
3533
  function calculateGainPercent(gain, basis) {
@@ -3611,7 +3617,9 @@ module.exports = (() => {
3611
3617
 
3612
3618
  this._data.periodIncome = null;
3613
3619
  this._data.periodRealized = null;
3620
+
3614
3621
  this._data.periodUnrealized = null;
3622
+ this._data.periodUnrealizedChange = null;
3615
3623
 
3616
3624
  this._data.periodPrice = null;
3617
3625
  this._data.periodPricePrevious = null;
@@ -3956,14 +3964,14 @@ module.exports = (() => {
3956
3964
  data.marketPrevious2 = previousSummary2 === null ? Decimal.ZERO : previousSummary2.end.value;
3957
3965
  data.quantityPrevious = previousSummary1 === null ? Decimal.ZERO : previousSummary1.end.open;
3958
3966
 
3959
- data.periodIncome = currentSummary !== null ? currentSummary.period.income : Decimal.ZERO;
3960
- data.periodRealized = currentSummary !== null ? currentSummary.period.realized : Decimal.ZERO;
3961
- data.periodUnrealized = currentSummary !== null ? currentSummary.period.unrealized : Decimal.ZERO;
3962
-
3963
3967
  data.periodGain = calculatePeriodGain(position.instrument.type, data.initiate, currentSummary, previousSummary1);
3964
3968
  data.periodGainPrevious = calculatePeriodGain(position.instrument.type, data.initiate, previousSummary1, previousSummary2);
3965
3969
  data.periodGainPrevious2 = calculatePeriodGain(position.instrument.type, data.initiate, previousSummary2, previousSummary3);
3966
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
+
3967
3975
  data.periodDivisor = calculatePeriodDivisor(position.instrument.type, data.initiate, currentSummary, previousSummary1);
3968
3976
  data.periodDivisorPrevious = calculatePeriodDivisor(position.instrument.type, data.initiate, previousSummary1, previousSummary2);
3969
3977
  data.periodDivisorPrevious2 = calculatePeriodDivisor(position.instrument.type, data.initiate, previousSummary2, previousSummary3);
@@ -3986,7 +3994,7 @@ module.exports = (() => {
3986
3994
  data.periodPricePrevious = null;
3987
3995
  }
3988
3996
 
3989
- data.totalDivisor = calculateTotalDivisor(position.instrument.type, data.initiate, currentSummary);
3997
+ data.totalDivisor = calculateTotalDivisor(position.instrument.type, data.initiate, currentSummary, position);
3990
3998
  }
3991
3999
 
3992
4000
  function calculatePriceData(item, price) {
@@ -4091,6 +4099,18 @@ module.exports = (() => {
4091
4099
 
4092
4100
  data.periodGain = periodGain;
4093
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;
4094
4114
  } else {
4095
4115
  data.unrealized = Decimal.ZERO;
4096
4116
  data.unrealizedChange = Decimal.ZERO;
@@ -4181,14 +4201,30 @@ module.exports = (() => {
4181
4201
  return returnRef;
4182
4202
  }
4183
4203
 
4184
- function calculateTotalDivisor(type, direction, finalSummary) {
4204
+ function calculatePeriodUnrealized(type, periodGain, periodRealized, periodIncome) {
4185
4205
  let returnRef;
4186
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
+
4187
4223
  if (finalSummary && type !== InstrumentType.CASH) {
4188
4224
  if (direction === PositionDirection.SHORT) {
4189
- returnRef = finalSummary.period.sells;
4225
+ returnRef = position.snapshot.sells;
4190
4226
  } else {
4191
- returnRef = finalSummary.period.buys.opposite();
4227
+ returnRef = position.snapshot.buys.opposite();
4192
4228
  }
4193
4229
  } else {
4194
4230
  returnRef = Decimal.ZERO;
@@ -5249,7 +5285,7 @@ module.exports = function () {
5249
5285
  *
5250
5286
  * @public
5251
5287
  * @param {*} value - The value of the node.
5252
- * @param {Tree} parent - The parent node. If not supplied, this will be the root node.
5288
+ * @param {Tree=} parent - The parent node. If not supplied, this will be the root node.
5253
5289
  */
5254
5290
 
5255
5291
  var Tree = function () {
@@ -6395,7 +6431,20 @@ module.exports = function () {
6395
6431
  }
6396
6432
 
6397
6433
  /**
6398
- * Returns a new Day instance for the end of the month of the current instance.
6434
+ * Returns a new {@link Day} instance for the start of the month referenced by the current instance.
6435
+ *
6436
+ * @public
6437
+ * @returns {Day}
6438
+ */
6439
+
6440
+ }, {
6441
+ key: 'getStartOfMonth',
6442
+ value: function getStartOfMonth() {
6443
+ return new Day(this.year, this.month, 1);
6444
+ }
6445
+
6446
+ /**
6447
+ * Returns a new instance for the {@link Day} end of the month referenced by the current instance.
6399
6448
  *
6400
6449
  * @public
6401
6450
  * @returns {Day}
@@ -6547,7 +6596,7 @@ module.exports = function () {
6547
6596
  return this._month;
6548
6597
  }
6549
6598
 
6550
- /**
6599
+ /**day
6551
6600
  * The day of the month.
6552
6601
  *
6553
6602
  * @public
@@ -6568,7 +6617,7 @@ module.exports = function () {
6568
6617
  }
6569
6618
 
6570
6619
  /**
6571
- * Converts a string (which matches the output of {@link Day#format} into
6620
+ * Converts a string (which matches the output of {@link Day#format}) into
6572
6621
  * a {@link Day} instance.
6573
6622
  *
6574
6623
  * @public
@@ -6839,6 +6888,24 @@ module.exports = function () {
6839
6888
  return new Decimal(this._big.div(getBig(other)));
6840
6889
  }
6841
6890
 
6891
+ /**
6892
+ * Returns a new {@link Decimal} instance with a value that results
6893
+ * from raising the current instance to the power of the exponent
6894
+ * provided.
6895
+ *
6896
+ * @public
6897
+ * @param {Decimal|Number|String} exponent
6898
+ * @returns {Decimal}
6899
+ */
6900
+
6901
+ }, {
6902
+ key: 'raise',
6903
+ value: function raise(exponent) {
6904
+ assert.argumentIsRequired(exponent, 'exponent', Number);
6905
+
6906
+ return new Decimal(this._big.pow(exponent));
6907
+ }
6908
+
6842
6909
  /**
6843
6910
  * Returns a new {@link Decimal} with a value resulting from a rounding
6844
6911
  * operation on the current value.
@@ -7003,6 +7070,28 @@ module.exports = function () {
7003
7070
  return this._big.eq(getBig(other));
7004
7071
  }
7005
7072
 
7073
+ /**
7074
+ * Returns true is close to another value.
7075
+ *
7076
+ * @public
7077
+ * @param {Decimal|Number|String} other - The value to compare.
7078
+ * @param {Number} places - The significant digits.
7079
+ * @returns {Boolean}
7080
+ */
7081
+
7082
+ }, {
7083
+ key: 'getIsApproximate',
7084
+ value: function getIsApproximate(other, places) {
7085
+ if (places === 0) {
7086
+ return this.getIsEqual(other);
7087
+ }
7088
+
7089
+ var difference = this.subtract(other).absolute();
7090
+ var tolerance = Decimal.ONE.divide(new Decimal(10).raise(places));
7091
+
7092
+ return difference.getIsLessThan(tolerance);
7093
+ }
7094
+
7006
7095
  /**
7007
7096
  * Returns true if the current instance is an integer (i.e. has no decimal
7008
7097
  * component).
@@ -8686,8 +8775,62 @@ module.exports = function () {
8686
8775
  }
8687
8776
 
8688
8777
  return found;
8778
+ },
8779
+
8780
+
8781
+ /**
8782
+ * Inserts an item into an array using a binary search is used to determine the
8783
+ * proper point for insertion and returns the same array.
8784
+ *
8785
+ * @static
8786
+ * @public
8787
+ * @param {Array} a
8788
+ * @param {*} item
8789
+ * @param {Function} comparator
8790
+ * @returns {Array}
8791
+ */
8792
+ insert: function insert(a, item, comparator) {
8793
+ assert.argumentIsArray(a, 'a');
8794
+ assert.argumentIsRequired(comparator, 'comparator', Function);
8795
+
8796
+ if (a.length === 0 || !(comparator(item, a[a.length - 1]) < 0)) {
8797
+ a.push(item);
8798
+ } else if (comparator(item, a[0]) < 0) {
8799
+ a.unshift(item);
8800
+ } else {
8801
+ a.splice(binarySearch(a, item, comparator, 0, a.length - 1), 0, item);
8802
+ }
8803
+
8804
+ return a;
8689
8805
  }
8690
8806
  };
8807
+
8808
+ function binarySearch(array, item, comparator, start, end) {
8809
+ var size = end - start;
8810
+
8811
+ var midpointIndex = start + Math.floor(size / 2);
8812
+ var midpointItem = array[midpointIndex];
8813
+
8814
+ var comparison = comparator(item, midpointItem) > 0;
8815
+
8816
+ if (size < 2) {
8817
+ if (comparison > 0) {
8818
+ var finalIndex = array.length - 1;
8819
+
8820
+ if (end === finalIndex && comparator(item, array[finalIndex]) > 0) {
8821
+ return end + 1;
8822
+ } else {
8823
+ return end;
8824
+ }
8825
+ } else {
8826
+ return start;
8827
+ }
8828
+ } else if (comparison > 0) {
8829
+ return binarySearch(array, item, comparator, midpointIndex, end);
8830
+ } else {
8831
+ return binarySearch(array, item, comparator, start, midpointIndex);
8832
+ }
8833
+ }
8691
8834
  }();
8692
8835
 
8693
8836
  },{"./assert":29,"./is":33}],29:[function(require,module,exports){
@@ -9208,7 +9351,7 @@ module.exports = function () {
9208
9351
  * @static
9209
9352
  * @public
9210
9353
  * @param {*} candidate
9211
- * @returns {*|boolean}
9354
+ * @returns {boolean}
9212
9355
  */
9213
9356
  negative: function negative(candidate) {
9214
9357
  return this.number(candidate) && candidate < 0;
@@ -18465,7 +18608,9 @@ describe('When a position container data is gathered', () => {
18465
18608
  value: new Decimal(456),
18466
18609
  open: new Decimal(1),
18467
18610
  income: new Decimal(0),
18468
- gain: new Decimal(0)
18611
+ gain: new Decimal(0),
18612
+ buys: new Decimal(50),
18613
+ sells: new Decimal(0)
18469
18614
  }
18470
18615
  };
18471
18616
  }
@@ -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
  }