@barchart/portfolio-api-common 1.2.128 → 1.2.132

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.
@@ -11,9 +11,10 @@ module.exports = (() => {
11
11
  *
12
12
  * @public
13
13
  * @extends {Enum}
14
+ * @param {String} code
14
15
  * @param {String} description
15
16
  * @param {String} alternateDescription
16
- * @param {String} code
17
+ * @param {Boolean} canExistEmpty
17
18
  * @param {Boolean} canReinvest
18
19
  * @param {Boolean} canShort
19
20
  * @param {Boolean} canSwitchDirection
@@ -25,10 +26,11 @@ module.exports = (() => {
25
26
  * @param {Function} generator
26
27
  */
27
28
  class InstrumentType extends Enum {
28
- constructor(code, description, alternateDescription, canReinvest, canShort, canSwitchDirection, usesSymbols, hasCorporateActions, closeFractional, roundQuantity, strictOrdering, generator) {
29
+ constructor(code, description, alternateDescription, canExistEmpty, canReinvest, canShort, canSwitchDirection, usesSymbols, hasCorporateActions, closeFractional, roundQuantity, strictOrdering, generator) {
29
30
  super(code, description);
30
31
 
31
32
  assert.argumentIsRequired(alternateDescription, 'alternateDescription', String);
33
+ assert.argumentIsRequired(canExistEmpty, 'canExistEmpty', Boolean);
32
34
  assert.argumentIsRequired(canReinvest, 'canReinvest', Boolean);
33
35
  assert.argumentIsRequired(canShort, 'canShort', Boolean);
34
36
  assert.argumentIsRequired(canSwitchDirection, 'canSwitchDirection', Boolean);
@@ -40,6 +42,8 @@ module.exports = (() => {
40
42
  assert.argumentIsRequired(generator, 'generator', Function);
41
43
 
42
44
  this._alternateDescription = alternateDescription;
45
+
46
+ this._canExistEmpty = canExistEmpty;
43
47
  this._canReinvest = canReinvest;
44
48
  this._canShort = canShort;
45
49
  this._canSwitchDirection = canSwitchDirection;
@@ -62,6 +66,16 @@ module.exports = (() => {
62
66
  return this._alternateDescription;
63
67
  }
64
68
 
69
+ /**
70
+ * Indicates if the position can exist without any associated transactions.
71
+ *
72
+ * @public
73
+ * @returns {Boolean}
74
+ */
75
+ get canExistEmpty() {
76
+ return this._canExistEmpty;
77
+ }
78
+
65
79
  /**
66
80
  * Indicates if the instrument type allows automatic reinvestment.
67
81
  *
@@ -242,10 +256,10 @@ module.exports = (() => {
242
256
  }
243
257
  }
244
258
 
245
- const cash = new InstrumentType('CASH', 'cash', 'Cash', false, false, true, false, false, false, false, false, (instrument) => `BARCHART-${instrument.type.code}-${instrument.currency.code}`);
246
- const equity = new InstrumentType('EQUITY', 'equity', 'Equities', true, true, false, true, true, true, true, true, (instrument) => `BARCHART-${instrument.type.code}-${instrument.symbol.barchart}`);
247
- const fund = new InstrumentType('FUND', 'mutual fund', 'Funds', true, false, false, true, true, false, true, true, (instrument) => `BARCHART-${instrument.type.code}-${instrument.symbol.barchart}`);
248
- const other = new InstrumentType('OTHER', 'other', 'Other', false, false, false, false, false, false, true, true, (instrument) => `BARCHART-${instrument.type.code}-${uuid.v4()}`);
259
+ const cash = new InstrumentType('CASH', 'cash', 'Cash', true, false, false, true, false, false, false, false, false, (instrument) => `BARCHART-${instrument.type.code}-${instrument.currency.code}`);
260
+ const equity = new InstrumentType('EQUITY', 'equity', 'Equities', false, true, true, false, true, true, true, true, true, (instrument) => `BARCHART-${instrument.type.code}-${instrument.symbol.barchart}`);
261
+ const fund = new InstrumentType('FUND', 'mutual fund', 'Funds', false, true, false, false, true, true, false, true, true, (instrument) => `BARCHART-${instrument.type.code}-${instrument.symbol.barchart}`);
262
+ const other = new InstrumentType('OTHER', 'other', 'Other', false, false, false, false, false, false, false, true, true, (instrument) => `BARCHART-${instrument.type.code}-${uuid.v4()}`);
249
263
 
250
264
  const map = { };
251
265
 
@@ -25,7 +25,7 @@ module.exports = (() => {
25
25
  * @param {Boolean} system
26
26
  */
27
27
  class TransactionType extends Enum {
28
- constructor(code, description, display, sequence, purchase, sale, income, opening, closing, fee, corporateAction, initial, significant, system) {
28
+ constructor(code, description, display, sequence, purchase, sale, income, opening, closing, fee, corporateAction, initial, significant) {
29
29
  super(code, description);
30
30
 
31
31
  assert.argumentIsRequired(display, 'display', String);
@@ -39,7 +39,6 @@ module.exports = (() => {
39
39
  assert.argumentIsRequired(corporateAction, 'corporateAction', Boolean);
40
40
  assert.argumentIsRequired(initial, 'initial', Boolean);
41
41
  assert.argumentIsRequired(significant, 'significant', Boolean);
42
- assert.argumentIsRequired(system, 'system', Boolean);
43
42
 
44
43
  this._display = display;
45
44
  this._sequence = sequence;
@@ -52,7 +51,6 @@ module.exports = (() => {
52
51
  this._corporateAction = corporateAction;
53
52
  this._initial = initial;
54
53
  this._significant = significant;
55
- this._system = system;
56
54
  }
57
55
 
58
56
  /**
@@ -179,16 +177,6 @@ module.exports = (() => {
179
177
  return this._significant;
180
178
  }
181
179
 
182
- /**
183
- * System transactions are generated automatically.
184
- *
185
- * @public
186
- * @returns {Boolean}
187
- */
188
- get system() {
189
- return this._system;
190
- }
191
-
192
180
  /**
193
181
  * A purchase.
194
182
  *
@@ -414,29 +402,29 @@ module.exports = (() => {
414
402
  }
415
403
  }
416
404
 
417
- const buy = new TransactionType('B', 'Buy', 'Buy', 0, true, false, false, true, false, false, false, true, true, false);
418
- const sell = new TransactionType('S', 'Sell', 'Sell', 0, false, true, false, false, true, false, false, false, true, false);
419
- const buyShort = new TransactionType('BS', 'Buy To Cover', 'Buy To Cover', 0, true, false, false, false, true, false, false, false, true, false);
420
- const sellShort = new TransactionType('SS', 'Sell Short', 'Sell Short', 0, false, true, false, true, false, false, false, true, true, false);
421
- const dividend = new TransactionType('DV', 'Dividend', 'Dividend', 1, false, false, true, false, false, false, true, false, false, true);
422
- const dividendReinvest = new TransactionType('DX', 'Dividend (Reinvested)', 'Dividend Reinvest', 1, false, false, false, true, false, false, true, false, false, true);
423
- const dividendStock = new TransactionType('DS', 'Dividend (Stock)', 'Dividend Stock', 1, false, false, false, true, false, false, true, false, false, true);
424
- const split = new TransactionType('SP', 'Split', 'Split', 1, false, false, false, true, false, false, true, false, false, true);
425
- const fee = new TransactionType('F', 'Fee', 'Fee', 0, false, false, false, false, false, true, false, false, false, false);
426
- const feeUnits = new TransactionType('FU', 'Fee Units', 'Fee', 0, false, false, false, false, true, false, false, false, false, false);
427
- const delist = new TransactionType('DL', 'Delist', 'Delist', 1, false, false, false, false, false, false, true, false, false, true);
428
-
429
- const distributionCash = new TransactionType('DC', 'Distribution (Cash)', 'Cash Distribution', 1, false, false, true, false, false, false, true, false, false, true);
430
- const distributionReinvest = new TransactionType('DY', 'Distribution (Reinvested)', 'Distribution Reinvest', 1, false, false, false, true, false, false, true, false, false, true);
431
- const distributionFund = new TransactionType('DF', 'Distribution (Units)', 'Unit Distribution', 1, false, false, false, true, false, false, true, false, false, true);
432
-
433
- const deposit = new TransactionType('D', 'Deposit', 'Deposit', 0, false, false, false, false, false, false, false, true, true, false);
434
- const withdrawal = new TransactionType('W', 'Withdrawal', 'Withdrawal', 0, false, false, false, false, false, false, false, true, true, false);
435
- const debit = new TransactionType('DR', 'Debit', 'Debit', 0, false, false, false, false, false, false, false, true, true, true);
436
- const credit = new TransactionType('CR', 'Credit', 'Credit', 0, false, false, false, false, false, false, false, true, true, true);
437
-
438
- const valuation = new TransactionType('V', 'Valuation', 'Valuation', 0, false, false, false, false, false, false, false, false, false, false);
439
- const income = new TransactionType('I', 'Income', 'Income', 0, false, false, true, false, false, false, false, false, false, false);
405
+ const buy = new TransactionType('B', 'Buy', 'Buy', 0, true, false, false, true, false, false, false, true, true);
406
+ const sell = new TransactionType('S', 'Sell', 'Sell', 0, false, true, false, false, true, false, false, false, true);
407
+ const buyShort = new TransactionType('BS', 'Buy To Cover', 'Buy To Cover', 0, true, false, false, false, true, false, false, false, true);
408
+ const sellShort = new TransactionType('SS', 'Sell Short', 'Sell Short', 0, false, true, false, true, false, false, false, true, true);
409
+ const dividend = new TransactionType('DV', 'Dividend', 'Dividend', 1, false, false, true, false, false, false, true, false, false);
410
+ const dividendReinvest = new TransactionType('DX', 'Dividend (Reinvested)', 'Dividend Reinvest', 1, false, false, false, true, false, false, true, false, false);
411
+ const dividendStock = new TransactionType('DS', 'Dividend (Stock)', 'Dividend Stock', 1, false, false, false, true, false, false, true, false, false);
412
+ const split = new TransactionType('SP', 'Split', 'Split', 1, false, false, false, true, false, false, true, false, false);
413
+ const fee = new TransactionType('F', 'Fee', 'Fee', 0, false, false, false, false, false, true, false, false, false);
414
+ const feeUnits = new TransactionType('FU', 'Fee Units', 'Fee', 0, false, false, false, false, true, false, false, false, false);
415
+ const delist = new TransactionType('DL', 'Delist', 'Delist', 1, false, false, false, false, false, false, true, false, false);
416
+
417
+ const distributionCash = new TransactionType('DC', 'Distribution (Cash)', 'Cash Distribution', 1, false, false, true, false, false, false, true, false, false);
418
+ const distributionReinvest = new TransactionType('DY', 'Distribution (Reinvested)', 'Distribution Reinvest', 1, false, false, false, true, false, false, true, false, false);
419
+ const distributionFund = new TransactionType('DF', 'Distribution (Units)', 'Unit Distribution', 1, false, false, false, true, false, false, true, false, false);
420
+
421
+ const deposit = new TransactionType('D', 'Deposit', 'Deposit', 0, false, false, false, false, false, false, false, true, true);
422
+ const withdrawal = new TransactionType('W', 'Withdrawal', 'Withdrawal', 0, false, false, false, false, false, false, false, true, true);
423
+ const debit = new TransactionType('DR', 'Debit', 'Debit', 0, false, false, false, false, false, false, false, true, true);
424
+ const credit = new TransactionType('CR', 'Credit', 'Credit', 0, false, false, false, false, false, false, false, true, true);
425
+
426
+ const valuation = new TransactionType('V', 'Valuation', 'Valuation', 0, false, false, false, false, false, false, false, false, false);
427
+ const income = new TransactionType('I', 'Income', 'Income', 0, false, false, true, false, false, false, false, false, false);
440
428
 
441
429
  return TransactionType;
442
430
  })();
@@ -137,6 +137,9 @@ module.exports = (() => {
137
137
  this._dataActual.marketChange = null;
138
138
  this._dataActual.marketChangePercent = null;
139
139
  this._dataActual.cashTotal = null;
140
+ this._dataActual.periodDivisorCurrent = null;
141
+ this._dataActual.periodDivisorPrevious = null;
142
+ this._dataActual.periodDivisorPrevious2 = null;
140
143
 
141
144
  this._dataFormat.currentPrice = null;
142
145
  this._dataFormat.basis = null;
@@ -168,33 +171,25 @@ module.exports = (() => {
168
171
 
169
172
  this._dataActual.periodPrice = null;
170
173
  this._dataActual.periodPricePrevious = null;
171
- this._dataActual.periodRealized = null;
172
- this._dataActual.periodRealizedPrevious = null;
173
- this._dataActual.periodRealizedPrevious2 = null;
174
- this._dataActual.periodRealizedBasis = null;
175
- this._dataActual.periodRealizedBasisPrevious = null;
176
- this._dataActual.periodRealizedBasisPrevious2 = null;
177
- this._dataActual.periodUnrealized = null;
178
- this._dataActual.periodUnrealizedPrevious = null;
179
- this._dataActual.periodUnrealizedPrevious2 = null;
180
- this._dataActual.periodUnrealizedBasis = null;
181
- this._dataActual.periodUnrealizedBasisPrevious = null;
182
- this._dataActual.periodUnrealizedBasisPrevious2 = null;
183
- this._dataActual.periodIncome = null;
184
174
 
185
175
  this._dataFormat.periodPrice = null;
186
176
  this._dataFormat.periodPricePrevious = null;
177
+
178
+ this._dataActual.periodIncome = null;
179
+ this._dataActual.periodRealized = null;
180
+ this._dataActual.periodUnrealized = null;
181
+
182
+ this._dataFormat.periodIncome = null;
187
183
  this._dataFormat.periodRealized = null;
188
184
  this._dataFormat.periodUnrealized = null;
189
- this._dataFormat.periodIncome = null;
190
185
 
191
186
  this._dataActual.periodPercent = null;
192
- this._dataActual.periodPreviousPercent = null;
193
- this._dataActual.periodPrevious2Percent = null;
187
+ this._dataActual.periodPercentPrevious = null;
188
+ this._dataActual.periodPercentPrevious2 = null;
194
189
 
195
190
  this._dataFormat.periodPercent = null;
196
- this._dataFormat.periodPreviousPercent = null;
197
- this._dataFormat.periodPrevious2Percent = null;
191
+ this._dataFormat.periodPercentPrevious = null;
192
+ this._dataFormat.periodPercentPrevious2 = null;
198
193
 
199
194
  this._items.forEach((item) => {
200
195
  bindItem.call(this, item);
@@ -686,9 +681,9 @@ module.exports = (() => {
686
681
  updates.realized = updates.realized.add(translate(item, item.data.realized));
687
682
  updates.unrealized = updates.unrealized.add(translate(item, item.data.unrealized));
688
683
  updates.income = updates.income.add(translate(item, item.data.income));
689
- updates.summaryTotalCurrent = updates.summaryTotalCurrent.add(translate(item, item.data.summaryTotalCurrent));
690
- updates.summaryTotalPrevious = updates.summaryTotalPrevious.add(translate(item, item.data.summaryTotalPrevious));
691
- updates.summaryTotalPrevious2 = updates.summaryTotalPrevious2.add(translate(item, item.data.summaryTotalPrevious2));
684
+ updates.summaryTotalCurrent = updates.summaryTotalCurrent.add(translate(item, item.data.periodGain));
685
+ updates.summaryTotalPrevious = updates.summaryTotalPrevious.add(translate(item, item.data.periodGainPrevious));
686
+ updates.summaryTotalPrevious2 = updates.summaryTotalPrevious2.add(translate(item, item.data.periodGainPrevious2));
692
687
  updates.marketPrevious = updates.marketPrevious.add(translate(item, item.data.marketPrevious));
693
688
  updates.marketPrevious2 = updates.marketPrevious2.add(translate(item, item.data.marketPrevious2));
694
689
 
@@ -700,6 +695,10 @@ module.exports = (() => {
700
695
  updates.cashTotal = updates.cashTotal.add(translate(item, item.data.market));
701
696
  }
702
697
 
698
+ updates.periodDivisorCurrent = updates.periodDivisorCurrent.add(translate(item, item.data.periodDivisor));
699
+ updates.periodDivisorPrevious = updates.periodDivisorPrevious.add(translate(item, item.data.periodDivisorPrevious));
700
+ updates.periodDivisorPrevious2 = updates.periodDivisorPrevious2.add(translate(item, item.data.periodDivisorPrevious2));
701
+
703
702
  return updates;
704
703
  }, {
705
704
  basis: Decimal.ZERO,
@@ -715,6 +714,9 @@ module.exports = (() => {
715
714
  periodUnrealized: Decimal.ZERO,
716
715
  periodIncome: Decimal.ZERO,
717
716
  cashTotal: Decimal.ZERO,
717
+ periodDivisorCurrent: Decimal.ZERO,
718
+ periodDivisorPrevious: Decimal.ZERO,
719
+ periodDivisorPrevious2: Decimal.ZERO
718
720
  });
719
721
 
720
722
  actual.basis = updates.basis;
@@ -730,12 +732,16 @@ module.exports = (() => {
730
732
  actual.periodUnrealized = updates.periodUnrealized;
731
733
  actual.periodIncome = updates.periodIncome;
732
734
  actual.cashTotal = updates.cashTotal;
735
+ actual.periodDivisorCurrent = updates.periodDivisorCurrent;
736
+ actual.periodDivisorPrevious = updates.periodDivisorPrevious;
737
+ actual.periodDivisorPrevious2 = updates.periodDivisorPrevious2;
733
738
 
734
739
  format.basis = formatCurrency(actual.basis, currency);
735
740
  format.realized = formatCurrency(actual.realized, currency);
736
741
  format.unrealized = formatCurrency(actual.unrealized, currency);
737
742
  format.income = formatCurrency(actual.income, currency);
738
743
  format.summaryTotalCurrent = formatCurrency(updates.summaryTotalCurrent, currency);
744
+ format.summaryTotalCurrentNegative = updates.summaryTotalCurrent.getIsNegative();
739
745
  format.summaryTotalPrevious = formatCurrency(updates.summaryTotalPrevious, currency);
740
746
  format.summaryTotalPreviousNegative = updates.summaryTotalPrevious.getIsNegative();
741
747
  format.summaryTotalPrevious2 = formatCurrency(updates.summaryTotalPrevious2, currency);
@@ -770,25 +776,9 @@ module.exports = (() => {
770
776
  format.periodPrice = formatCurrency(actual.periodPrice, currency);
771
777
  format.periodPricePrevious = formatCurrency(actual.periodPricePrevious, currency);
772
778
 
773
- actual.periodRealized = item.data.periodRealized;
774
- actual.periodRealizedPrevious = item.data.periodRealizedPrevious;
775
- actual.periodRealizedPrevious2 = item.data.periodRealizedPrevious2;
776
-
777
- actual.periodRealizedBasis = item.data.periodRealizedBasis;
778
- actual.periodRealizedBasisPrevious = item.data.periodRealizedBasisPrevious;
779
- actual.periodRealizedBasisPrevious2 = item.data.periodRealizedBasisPrevious2;
780
-
781
- actual.periodUnrealized = item.data.periodUnrealized;
782
- actual.periodUnrealizedPrevious = item.data.periodUnrealizedPrevious;
783
- actual.periodUnrealizedPrevious2 = item.data.periodUnrealizedPrevious2;
784
-
785
- actual.periodUnrealizedBasis = item.data.periodUnrealizedBasis;
786
- actual.periodUnrealizedBasisPrevious = item.data.periodUnrealizedBasisPrevious;
787
- actual.periodUnrealizedBasisPrevious2 = item.data.periodUnrealizedBasisPrevious2;
788
-
789
- actual.periodPercent = calculatePeriodPercent(actual.summaryTotalCurrent, actual.periodRealizedBasis, actual.periodUnrealizedBasis);
790
- actual.periodPercentPrevious = calculatePeriodPercent(actual.summaryTotalPrevious, actual.periodRealizedBasisPrevious, actual.periodUnrealizedBasisPrevious);
791
- actual.periodPercentPrevious2 = calculatePeriodPercent(actual.summaryTotalPrevious2, actual.periodRealizedBasisPrevious2, actual.periodUnrealizedBasisPrevious2);
779
+ actual.periodPercent = calculatePeriodPercent(actual.summaryTotalCurrent, actual.periodDivisorCurrent);
780
+ actual.periodPercentPrevious = calculatePeriodPercent(actual.summaryTotalPrevious, actual.periodDivisorPrevious);
781
+ actual.periodPercentPrevious2 = calculatePeriodPercent(actual.summaryTotalPrevious2, actual.periodDivisorPrevious2);
792
782
 
793
783
  format.periodPercent = formatPercent(actual.periodPercent, 2);
794
784
  format.periodPercentPrevious = formatPercent(actual.periodPercentPrevious, 2);
@@ -847,7 +837,7 @@ module.exports = (() => {
847
837
  updates.marketAbsolute = updates.marketAbsolute.add(translate(item, item.data.marketAbsolute));
848
838
  updates.unrealized = updates.unrealized.add(translate(item, item.data.unrealized));
849
839
  updates.unrealizedToday = updates.unrealizedToday.add(translate(item, item.data.unrealizedToday));
850
- updates.summaryTotalCurrent = updates.summaryTotalCurrent.add(translate(item, item.data.summaryTotalCurrent));
840
+ updates.summaryTotalCurrent = updates.summaryTotalCurrent.add(translate(item, item.data.periodGain));
851
841
 
852
842
  return updates;
853
843
  }, {
@@ -865,7 +855,7 @@ module.exports = (() => {
865
855
  marketDirection: { up: item.data.marketChange.getIsPositive(), down: item.data.marketChange.getIsNegative() },
866
856
  unrealized: actual.unrealized.add(translate(item, item.data.unrealizedChange)),
867
857
  unrealizedToday: actual.unrealizedToday.add(translate(item, item.data.unrealizedTodayChange)),
868
- summaryTotalCurrent: actual.summaryTotalCurrent.add(translate(item, item.data.summaryTotalCurrentChange))
858
+ summaryTotalCurrent: actual.summaryTotalCurrent.add(translate(item, item.data.periodGainChange))
869
859
  };
870
860
  }
871
861
 
@@ -921,7 +911,7 @@ module.exports = (() => {
921
911
  if (group.single && item) {
922
912
  actual.periodUnrealized = item.data.periodUnrealized;
923
913
 
924
- actual.periodPercent = calculatePeriodPercent(actual.summaryTotalCurrent, actual.periodRealizedBasis, actual.periodUnrealizedBasis);
914
+ actual.periodPercent = calculatePeriodPercent(actual.summaryTotalCurrent, actual.periodDivisorCurrent);
925
915
  format.periodPercent = formatPercent(actual.periodPercent, 2);
926
916
  }
927
917
  }
@@ -982,11 +972,8 @@ module.exports = (() => {
982
972
  }
983
973
  }
984
974
 
985
- function calculatePeriodPercent(periodSummaryTotal, realizedBasis, unrealizedBasis) {
986
- const numerator = periodSummaryTotal;
987
- const denominator = realizedBasis.add(unrealizedBasis);
988
-
989
- return denominator.getIsZero() ? Decimal.ZERO : numerator.divide(denominator);
975
+ function calculatePeriodPercent(periodSummaryTotal, periodDivisor) {
976
+ return periodDivisor.getIsZero() ? Decimal.ZERO : periodSummaryTotal.divide(periodDivisor);
990
977
  }
991
978
 
992
979
  const unchanged = { up: false, down: false };
@@ -62,12 +62,6 @@ module.exports = (() => {
62
62
  this._data.unrealized = null;
63
63
  this._data.unrealizedChange = null;
64
64
 
65
- this._data.summaryTotalCurrent = null;
66
- this._data.summaryTotalCurrentChange = null;
67
-
68
- this._data.summaryTotalPrevious = null;
69
- this._data.summaryTotalPrevious2 = null;
70
-
71
65
  this._data.marketPrevious = null;
72
66
  this._data.marketPrevious2 = null;
73
67
 
@@ -78,26 +72,24 @@ module.exports = (() => {
78
72
  this._data.income = null;
79
73
  this._data.basisPrice = null;
80
74
 
75
+ this._data.periodIncome = null;
81
76
  this._data.periodRealized = null;
82
- this._data.periodRealizedPrevious = null;
83
- this._data.periodRealizedPrevious2 = null;
77
+ this._data.periodUnrealized = null;
84
78
 
85
- this._data.periodRealizedBasis = null;
86
- this._data.periodRealizedBasisPrevious = null;
87
- this._data.periodRealizedBasisPrevious2 = null;
79
+ this._data.periodPrice = null;
80
+ this._data.periodPricePrevious = null;
88
81
 
89
- this._data.periodUnrealized = null;
90
- this._data.periodUnrealizedPrevious = null;
91
- this._data.periodUnrealizedPrevious2 = null;
82
+ this._data.periodGain = null;
83
+ this._data.periodGainChange = null;
92
84
 
93
- this._data.periodUnrealizedBasis = null;
94
- this._data.periodUnrealizedBasisPrevious = null;
95
- this._data.periodUnrealizedBasisPrevious2 = null;
85
+ this._data.periodGainPrevious = null;
86
+ this._data.periodGainPrevious2 = null;
96
87
 
97
- this._data.periodIncome = null;
88
+ this._data.periodDivisor = null;
89
+ this._data.periodDivisorChange = null;
98
90
 
99
- this._data.periodPrice = null;
100
- this._data.periodPricePrevious = null;
91
+ this._data.periodDivisorPrevious = null;
92
+ this._data.periodDivisorPrevious2 = null;
101
93
 
102
94
  this._data.newsExists = false;
103
95
  this._data.fundamental = { };
@@ -393,7 +385,6 @@ module.exports = (() => {
393
385
 
394
386
  const previousSummary1 = getPreviousSummary(item.previousSummaries, 1);
395
387
  const previousSummary2 = getPreviousSummary(item.previousSummaries, 2);
396
- const previousSummary3 = getPreviousSummary(item.previousSummaries, 3);
397
388
 
398
389
  const snapshot = getSnapshot(position, currentSummary, item._reporting);
399
390
 
@@ -417,31 +408,21 @@ module.exports = (() => {
417
408
 
418
409
  data.income = snapshot.income;
419
410
 
420
- data.summaryTotalCurrent = calculateSummaryTotal(item.currentSummary, previousSummary1);
421
- data.summaryTotalPrevious = calculateSummaryTotal(previousSummary1, previousSummary2);
422
- data.summaryTotalPrevious2 = calculateSummaryTotal(previousSummary2, previousSummary3);
423
-
424
411
  data.marketPrevious = previousSummary1 === null ? Decimal.ZERO : previousSummary1.end.value;
425
412
  data.marketPrevious2 = previousSummary2 === null ? Decimal.ZERO : previousSummary2.end.value;
426
413
  data.quantityPrevious = previousSummary1 === null ? Decimal.ZERO : previousSummary1.end.open;
427
414
 
428
- data.periodRealized = calculatePeriodRealized(item.currentSummary, previousSummary1);
429
- data.periodRealizedPrevious = calculatePeriodRealized(previousSummary1, previousSummary2);
430
- data.periodRealizedPrevious2 = calculatePeriodRealized(previousSummary2, previousSummary3);
431
-
432
- data.periodRealizedBasis = calculatePeriodRealizedBasis(item.currentSummary, previousSummary1);
433
- data.periodRealizedBasisPrevious = calculatePeriodRealizedBasis(previousSummary1, previousSummary2);
434
- data.periodRealizedBasisPrevious2 = calculatePeriodRealizedBasis(previousSummary2, previousSummary3);
435
-
436
- data.periodUnrealized = calculatePeriodUnrealized(item.currentSummary, previousSummary1);
437
- data.periodUnrealizedPrevious = calculatePeriodUnrealized(previousSummary1, previousSummary2);
438
- data.periodUnrealizedPrevious2 = calculatePeriodUnrealized(previousSummary2, previousSummary3);
439
-
440
- data.periodUnrealizedBasis = calculatePeriodUnrealizedBasis(item.currentSummary, previousSummary1);
441
- data.periodUnrealizedBasisPrevious = calculatePeriodUnrealizedBasis(previousSummary1, previousSummary2);
442
- data.periodUnrealizedBasisPrevious2 = calculatePeriodUnrealizedBasis(previousSummary2, previousSummary3);
443
-
444
- data.periodIncome = calculatePeriodIncome(item.currentSummary, previousSummary1);
415
+ data.periodIncome = currentSummary !== null ? currentSummary.period.income : Decimal.ZERO;
416
+ data.periodRealized = currentSummary !== null ? currentSummary.period.realized : Decimal.ZERO;
417
+ data.periodUnrealized = currentSummary !== null ? currentSummary.period.unrealized : Decimal.ZERO;
418
+
419
+ data.periodGain = calculatePeriodGain(currentSummary);
420
+ data.periodGainPrevious = calculatePeriodGain(previousSummary1);
421
+ data.periodGainPrevious2 = calculatePeriodGain(previousSummary2);
422
+
423
+ data.periodDivisor = calculatePeriodDivisor(currentSummary);
424
+ data.periodDivisorPrevious = calculatePeriodDivisor(previousSummary1);
425
+ data.periodDivisorPrevious2 = calculatePeriodDivisor(previousSummary2);
445
426
 
446
427
  if (snapshot.open.getIsZero()) {
447
428
  data.basisPrice = Decimal.ZERO;
@@ -465,7 +446,6 @@ module.exports = (() => {
465
446
  function calculatePriceData(item, price) {
466
447
  const position = item.position;
467
448
  const snapshot = getSnapshot(position, item.currentSummary, item._reporting);
468
- const previousSummaries = item.previousSummaries;
469
449
 
470
450
  const data = item._data;
471
451
 
@@ -528,8 +508,6 @@ module.exports = (() => {
528
508
  const currentSummary = item.currentSummary;
529
509
 
530
510
  if (currentSummary && position.instrument.type !== InstrumentType.CASH) {
531
- const previousSummary = getPreviousSummary(previousSummaries, 1);
532
-
533
511
  let priceToUse;
534
512
 
535
513
  if (price) {
@@ -543,8 +521,6 @@ module.exports = (() => {
543
521
  }
544
522
 
545
523
  if (priceToUse !== null) {
546
- const period = currentSummary.period;
547
-
548
524
  let unrealized = currentSummary.end.open.multiply(priceToUse).add(currentSummary.end.basis);
549
525
  let unrealizedChange;
550
526
 
@@ -554,101 +530,51 @@ module.exports = (() => {
554
530
  unrealizedChange = Decimal.ZERO;
555
531
  }
556
532
 
557
- let summaryTotalCurrent = period.realized.add(period.income).add(unrealized).subtract(previousSummary !== null ? previousSummary.period.unrealized : Decimal.ZERO);
558
- let summaryTotalCurrentChange;
533
+ data.unrealized = unrealized;
534
+ data.unrealizedChange = unrealizedChange;
559
535
 
560
- if (data.summaryTotalCurrent !== null) {
561
- summaryTotalCurrentChange = summaryTotalCurrent.subtract(data.summaryTotalCurrent);
536
+ let periodGain = calculatePeriodGain(currentSummary, priceToUse);
537
+ let periodGainChange;
538
+
539
+ if (data.periodGain !== null) {
540
+ periodGainChange = periodGain.subtract(data.periodGain);
562
541
  } else {
563
- summaryTotalCurrentChange = Decimal.ZERO;
542
+ periodGainChange = Decimal.ZERO;
564
543
  }
565
544
 
566
- data.summaryTotalCurrent = summaryTotalCurrent;
567
- data.summaryTotalCurrentChange = summaryTotalCurrentChange;
568
-
569
- data.unrealized = unrealized;
570
- data.unrealizedChange = unrealizedChange;
571
-
572
- data.periodUnrealized = calculatePeriodUnrealized(item.currentSummary, previousSummary, data.unrealized);
573
- data.periodUnrealizedChange = unrealizedChange;
545
+ data.periodGain = periodGain;
546
+ data.periodGainChange = periodGainChange;
574
547
  } else {
575
- data.summaryTotalCurrentChange = Decimal.ZERO;
576
-
577
548
  data.unrealized = Decimal.ZERO;
578
549
  data.unrealizedChange = Decimal.ZERO;
579
550
 
580
- data.periodUnrealizedChange = Decimal.ZERO;
551
+ data.periodGainChange = Decimal.ZERO;
581
552
  }
582
553
  } else {
583
- data.summaryTotalCurrentChange = Decimal.ZERO;
584
-
585
554
  data.unrealized = Decimal.ZERO;
586
555
  data.unrealizedChange = Decimal.ZERO;
587
- }
588
- }
589
-
590
- function calculateSummaryTotal(currentSummary, previousSummary) {
591
- let returnRef;
592
-
593
- if (currentSummary) {
594
- const period = currentSummary.period;
595
556
 
596
- returnRef = period.realized.add(period.income).add(period.unrealized).subtract(previousSummary !== null ? previousSummary.period.unrealized : Decimal.ZERO);
597
- } else {
598
- returnRef = Decimal.ZERO;
557
+ data.periodGainChange = Decimal.ZERO;
599
558
  }
600
-
601
- return returnRef;
602
- }
603
-
604
- function calculatePeriodRealized(currentSummary, previousSummary) {
605
- let returnRef;
606
-
607
- if (currentSummary) {
608
- const period = currentSummary.period;
609
-
610
- returnRef = period.realized;
611
- } else {
612
- returnRef = Decimal.ZERO;
613
- }
614
-
615
- return returnRef;
616
559
  }
617
560
 
618
- function calculatePeriodRealizedBasis(currentSummary, previousSummary) {
619
- let returnRef;
620
-
621
- if (currentSummary) {
622
- const period = currentSummary.period;
623
-
624
- returnRef = period.sells.subtract(calculatePeriodRealized(currentSummary, previousSummary));
625
- } else {
626
- returnRef = Decimal.ZERO;
627
- }
628
-
629
- return returnRef;
630
- }
631
-
632
- function calculatePeriodUnrealized(currentSummary, previousSummary, override) {
561
+ function calculatePeriodGain(currentSummary, overridePrice) {
633
562
  let returnRef;
634
563
 
635
564
  if (currentSummary) {
636
- const period = currentSummary.period;
637
- const unrealized = override || period.unrealized;
565
+ let endValue;
638
566
 
639
- returnRef = unrealized.subtract(previousSummary !== null ? previousSummary.period.unrealized : Decimal.ZERO);
640
- } else {
641
- returnRef = Decimal.ZERO;
642
- }
567
+ if (overridePrice) {
568
+ endValue = currentSummary.end.open.multiply(overridePrice);
569
+ } else {
570
+ endValue = currentSummary.end.value;
571
+ }
643
572
 
644
- return returnRef;
645
- }
646
-
647
- function calculatePeriodUnrealizedBasis(currentSummary, previousSummary) {
648
- let returnRef;
573
+ const valueChange = endValue.subtract(currentSummary.start.value);
574
+ const tradeChange = currentSummary.period.sells.subtract(currentSummary.period.buys);
575
+ const incomeChange = currentSummary.period.income;
649
576
 
650
- if (currentSummary) {
651
- returnRef = currentSummary.end.basis.absolute();
577
+ returnRef = valueChange.add(tradeChange).add(incomeChange);
652
578
  } else {
653
579
  returnRef = Decimal.ZERO;
654
580
  }
@@ -656,13 +582,11 @@ module.exports = (() => {
656
582
  return returnRef;
657
583
  }
658
584
 
659
- function calculatePeriodIncome(currentSummary, previousSummary) {
585
+ function calculatePeriodDivisor(currentSummary) {
660
586
  let returnRef;
661
587
 
662
588
  if (currentSummary) {
663
- const period = currentSummary.period;
664
-
665
- returnRef = period.income;
589
+ returnRef = currentSummary.start.value.add(currentSummary.period.buys);
666
590
  } else {
667
591
  returnRef = Decimal.ZERO;
668
592
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.2.128",
3
+ "version": "1.2.132",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",
@@ -12,9 +12,10 @@ module.exports = (() => {
12
12
  *
13
13
  * @public
14
14
  * @extends {Enum}
15
+ * @param {String} code
15
16
  * @param {String} description
16
17
  * @param {String} alternateDescription
17
- * @param {String} code
18
+ * @param {Boolean} canExistEmpty
18
19
  * @param {Boolean} canReinvest
19
20
  * @param {Boolean} canShort
20
21
  * @param {Boolean} canSwitchDirection
@@ -26,10 +27,11 @@ module.exports = (() => {
26
27
  * @param {Function} generator
27
28
  */
28
29
  class InstrumentType extends Enum {
29
- constructor(code, description, alternateDescription, canReinvest, canShort, canSwitchDirection, usesSymbols, hasCorporateActions, closeFractional, roundQuantity, strictOrdering, generator) {
30
+ constructor(code, description, alternateDescription, canExistEmpty, canReinvest, canShort, canSwitchDirection, usesSymbols, hasCorporateActions, closeFractional, roundQuantity, strictOrdering, generator) {
30
31
  super(code, description);
31
32
 
32
33
  assert.argumentIsRequired(alternateDescription, 'alternateDescription', String);
34
+ assert.argumentIsRequired(canExistEmpty, 'canExistEmpty', Boolean);
33
35
  assert.argumentIsRequired(canReinvest, 'canReinvest', Boolean);
34
36
  assert.argumentIsRequired(canShort, 'canShort', Boolean);
35
37
  assert.argumentIsRequired(canSwitchDirection, 'canSwitchDirection', Boolean);
@@ -41,6 +43,8 @@ module.exports = (() => {
41
43
  assert.argumentIsRequired(generator, 'generator', Function);
42
44
 
43
45
  this._alternateDescription = alternateDescription;
46
+
47
+ this._canExistEmpty = canExistEmpty;
44
48
  this._canReinvest = canReinvest;
45
49
  this._canShort = canShort;
46
50
  this._canSwitchDirection = canSwitchDirection;
@@ -63,6 +67,16 @@ module.exports = (() => {
63
67
  return this._alternateDescription;
64
68
  }
65
69
 
70
+ /**
71
+ * Indicates if the position can exist without any associated transactions.
72
+ *
73
+ * @public
74
+ * @returns {Boolean}
75
+ */
76
+ get canExistEmpty() {
77
+ return this._canExistEmpty;
78
+ }
79
+
66
80
  /**
67
81
  * Indicates if the instrument type allows automatic reinvestment.
68
82
  *
@@ -243,10 +257,10 @@ module.exports = (() => {
243
257
  }
244
258
  }
245
259
 
246
- const cash = new InstrumentType('CASH', 'cash', 'Cash', false, false, true, false, false, false, false, false, (instrument) => `BARCHART-${instrument.type.code}-${instrument.currency.code}`);
247
- const equity = new InstrumentType('EQUITY', 'equity', 'Equities', true, true, false, true, true, true, true, true, (instrument) => `BARCHART-${instrument.type.code}-${instrument.symbol.barchart}`);
248
- const fund = new InstrumentType('FUND', 'mutual fund', 'Funds', true, false, false, true, true, false, true, true, (instrument) => `BARCHART-${instrument.type.code}-${instrument.symbol.barchart}`);
249
- const other = new InstrumentType('OTHER', 'other', 'Other', false, false, false, false, false, false, true, true, (instrument) => `BARCHART-${instrument.type.code}-${uuid.v4()}`);
260
+ const cash = new InstrumentType('CASH', 'cash', 'Cash', true, false, false, true, false, false, false, false, false, (instrument) => `BARCHART-${instrument.type.code}-${instrument.currency.code}`);
261
+ const equity = new InstrumentType('EQUITY', 'equity', 'Equities', false, true, true, false, true, true, true, true, true, (instrument) => `BARCHART-${instrument.type.code}-${instrument.symbol.barchart}`);
262
+ const fund = new InstrumentType('FUND', 'mutual fund', 'Funds', false, true, false, false, true, true, false, true, true, (instrument) => `BARCHART-${instrument.type.code}-${instrument.symbol.barchart}`);
263
+ const other = new InstrumentType('OTHER', 'other', 'Other', false, false, false, false, false, false, false, true, true, (instrument) => `BARCHART-${instrument.type.code}-${uuid.v4()}`);
250
264
 
251
265
  const map = { };
252
266
 
@@ -766,7 +780,7 @@ module.exports = (() => {
766
780
  * @param {Boolean} system
767
781
  */
768
782
  class TransactionType extends Enum {
769
- constructor(code, description, display, sequence, purchase, sale, income, opening, closing, fee, corporateAction, initial, significant, system) {
783
+ constructor(code, description, display, sequence, purchase, sale, income, opening, closing, fee, corporateAction, initial, significant) {
770
784
  super(code, description);
771
785
 
772
786
  assert.argumentIsRequired(display, 'display', String);
@@ -780,7 +794,6 @@ module.exports = (() => {
780
794
  assert.argumentIsRequired(corporateAction, 'corporateAction', Boolean);
781
795
  assert.argumentIsRequired(initial, 'initial', Boolean);
782
796
  assert.argumentIsRequired(significant, 'significant', Boolean);
783
- assert.argumentIsRequired(system, 'system', Boolean);
784
797
 
785
798
  this._display = display;
786
799
  this._sequence = sequence;
@@ -793,7 +806,6 @@ module.exports = (() => {
793
806
  this._corporateAction = corporateAction;
794
807
  this._initial = initial;
795
808
  this._significant = significant;
796
- this._system = system;
797
809
  }
798
810
 
799
811
  /**
@@ -920,16 +932,6 @@ module.exports = (() => {
920
932
  return this._significant;
921
933
  }
922
934
 
923
- /**
924
- * System transactions are generated automatically.
925
- *
926
- * @public
927
- * @returns {Boolean}
928
- */
929
- get system() {
930
- return this._system;
931
- }
932
-
933
935
  /**
934
936
  * A purchase.
935
937
  *
@@ -1155,29 +1157,29 @@ module.exports = (() => {
1155
1157
  }
1156
1158
  }
1157
1159
 
1158
- const buy = new TransactionType('B', 'Buy', 'Buy', 0, true, false, false, true, false, false, false, true, true, false);
1159
- const sell = new TransactionType('S', 'Sell', 'Sell', 0, false, true, false, false, true, false, false, false, true, false);
1160
- const buyShort = new TransactionType('BS', 'Buy To Cover', 'Buy To Cover', 0, true, false, false, false, true, false, false, false, true, false);
1161
- const sellShort = new TransactionType('SS', 'Sell Short', 'Sell Short', 0, false, true, false, true, false, false, false, true, true, false);
1162
- const dividend = new TransactionType('DV', 'Dividend', 'Dividend', 1, false, false, true, false, false, false, true, false, false, true);
1163
- const dividendReinvest = new TransactionType('DX', 'Dividend (Reinvested)', 'Dividend Reinvest', 1, false, false, false, true, false, false, true, false, false, true);
1164
- const dividendStock = new TransactionType('DS', 'Dividend (Stock)', 'Dividend Stock', 1, false, false, false, true, false, false, true, false, false, true);
1165
- const split = new TransactionType('SP', 'Split', 'Split', 1, false, false, false, true, false, false, true, false, false, true);
1166
- const fee = new TransactionType('F', 'Fee', 'Fee', 0, false, false, false, false, false, true, false, false, false, false);
1167
- const feeUnits = new TransactionType('FU', 'Fee Units', 'Fee', 0, false, false, false, false, true, false, false, false, false, false);
1168
- const delist = new TransactionType('DL', 'Delist', 'Delist', 1, false, false, false, false, false, false, true, false, false, true);
1169
-
1170
- const distributionCash = new TransactionType('DC', 'Distribution (Cash)', 'Cash Distribution', 1, false, false, true, false, false, false, true, false, false, true);
1171
- const distributionReinvest = new TransactionType('DY', 'Distribution (Reinvested)', 'Distribution Reinvest', 1, false, false, false, true, false, false, true, false, false, true);
1172
- const distributionFund = new TransactionType('DF', 'Distribution (Units)', 'Unit Distribution', 1, false, false, false, true, false, false, true, false, false, true);
1173
-
1174
- const deposit = new TransactionType('D', 'Deposit', 'Deposit', 0, false, false, false, false, false, false, false, true, true, false);
1175
- const withdrawal = new TransactionType('W', 'Withdrawal', 'Withdrawal', 0, false, false, false, false, false, false, false, true, true, false);
1176
- const debit = new TransactionType('DR', 'Debit', 'Debit', 0, false, false, false, false, false, false, false, true, true, true);
1177
- const credit = new TransactionType('CR', 'Credit', 'Credit', 0, false, false, false, false, false, false, false, true, true, true);
1178
-
1179
- const valuation = new TransactionType('V', 'Valuation', 'Valuation', 0, false, false, false, false, false, false, false, false, false, false);
1180
- const income = new TransactionType('I', 'Income', 'Income', 0, false, false, true, false, false, false, false, false, false, false);
1160
+ const buy = new TransactionType('B', 'Buy', 'Buy', 0, true, false, false, true, false, false, false, true, true);
1161
+ const sell = new TransactionType('S', 'Sell', 'Sell', 0, false, true, false, false, true, false, false, false, true);
1162
+ const buyShort = new TransactionType('BS', 'Buy To Cover', 'Buy To Cover', 0, true, false, false, false, true, false, false, false, true);
1163
+ const sellShort = new TransactionType('SS', 'Sell Short', 'Sell Short', 0, false, true, false, true, false, false, false, true, true);
1164
+ const dividend = new TransactionType('DV', 'Dividend', 'Dividend', 1, false, false, true, false, false, false, true, false, false);
1165
+ const dividendReinvest = new TransactionType('DX', 'Dividend (Reinvested)', 'Dividend Reinvest', 1, false, false, false, true, false, false, true, false, false);
1166
+ const dividendStock = new TransactionType('DS', 'Dividend (Stock)', 'Dividend Stock', 1, false, false, false, true, false, false, true, false, false);
1167
+ const split = new TransactionType('SP', 'Split', 'Split', 1, false, false, false, true, false, false, true, false, false);
1168
+ const fee = new TransactionType('F', 'Fee', 'Fee', 0, false, false, false, false, false, true, false, false, false);
1169
+ const feeUnits = new TransactionType('FU', 'Fee Units', 'Fee', 0, false, false, false, false, true, false, false, false, false);
1170
+ const delist = new TransactionType('DL', 'Delist', 'Delist', 1, false, false, false, false, false, false, true, false, false);
1171
+
1172
+ const distributionCash = new TransactionType('DC', 'Distribution (Cash)', 'Cash Distribution', 1, false, false, true, false, false, false, true, false, false);
1173
+ const distributionReinvest = new TransactionType('DY', 'Distribution (Reinvested)', 'Distribution Reinvest', 1, false, false, false, true, false, false, true, false, false);
1174
+ const distributionFund = new TransactionType('DF', 'Distribution (Units)', 'Unit Distribution', 1, false, false, false, true, false, false, true, false, false);
1175
+
1176
+ const deposit = new TransactionType('D', 'Deposit', 'Deposit', 0, false, false, false, false, false, false, false, true, true);
1177
+ const withdrawal = new TransactionType('W', 'Withdrawal', 'Withdrawal', 0, false, false, false, false, false, false, false, true, true);
1178
+ const debit = new TransactionType('DR', 'Debit', 'Debit', 0, false, false, false, false, false, false, false, true, true);
1179
+ const credit = new TransactionType('CR', 'Credit', 'Credit', 0, false, false, false, false, false, false, false, true, true);
1180
+
1181
+ const valuation = new TransactionType('V', 'Valuation', 'Valuation', 0, false, false, false, false, false, false, false, false, false);
1182
+ const income = new TransactionType('I', 'Income', 'Income', 0, false, false, true, false, false, false, false, false, false);
1181
1183
 
1182
1184
  return TransactionType;
1183
1185
  })();
@@ -2657,6 +2659,9 @@ module.exports = (() => {
2657
2659
  this._dataActual.marketChange = null;
2658
2660
  this._dataActual.marketChangePercent = null;
2659
2661
  this._dataActual.cashTotal = null;
2662
+ this._dataActual.periodDivisorCurrent = null;
2663
+ this._dataActual.periodDivisorPrevious = null;
2664
+ this._dataActual.periodDivisorPrevious2 = null;
2660
2665
 
2661
2666
  this._dataFormat.currentPrice = null;
2662
2667
  this._dataFormat.basis = null;
@@ -2688,33 +2693,25 @@ module.exports = (() => {
2688
2693
 
2689
2694
  this._dataActual.periodPrice = null;
2690
2695
  this._dataActual.periodPricePrevious = null;
2691
- this._dataActual.periodRealized = null;
2692
- this._dataActual.periodRealizedPrevious = null;
2693
- this._dataActual.periodRealizedPrevious2 = null;
2694
- this._dataActual.periodRealizedBasis = null;
2695
- this._dataActual.periodRealizedBasisPrevious = null;
2696
- this._dataActual.periodRealizedBasisPrevious2 = null;
2697
- this._dataActual.periodUnrealized = null;
2698
- this._dataActual.periodUnrealizedPrevious = null;
2699
- this._dataActual.periodUnrealizedPrevious2 = null;
2700
- this._dataActual.periodUnrealizedBasis = null;
2701
- this._dataActual.periodUnrealizedBasisPrevious = null;
2702
- this._dataActual.periodUnrealizedBasisPrevious2 = null;
2703
- this._dataActual.periodIncome = null;
2704
2696
 
2705
2697
  this._dataFormat.periodPrice = null;
2706
2698
  this._dataFormat.periodPricePrevious = null;
2699
+
2700
+ this._dataActual.periodIncome = null;
2701
+ this._dataActual.periodRealized = null;
2702
+ this._dataActual.periodUnrealized = null;
2703
+
2704
+ this._dataFormat.periodIncome = null;
2707
2705
  this._dataFormat.periodRealized = null;
2708
2706
  this._dataFormat.periodUnrealized = null;
2709
- this._dataFormat.periodIncome = null;
2710
2707
 
2711
2708
  this._dataActual.periodPercent = null;
2712
- this._dataActual.periodPreviousPercent = null;
2713
- this._dataActual.periodPrevious2Percent = null;
2709
+ this._dataActual.periodPercentPrevious = null;
2710
+ this._dataActual.periodPercentPrevious2 = null;
2714
2711
 
2715
2712
  this._dataFormat.periodPercent = null;
2716
- this._dataFormat.periodPreviousPercent = null;
2717
- this._dataFormat.periodPrevious2Percent = null;
2713
+ this._dataFormat.periodPercentPrevious = null;
2714
+ this._dataFormat.periodPercentPrevious2 = null;
2718
2715
 
2719
2716
  this._items.forEach((item) => {
2720
2717
  bindItem.call(this, item);
@@ -3206,9 +3203,9 @@ module.exports = (() => {
3206
3203
  updates.realized = updates.realized.add(translate(item, item.data.realized));
3207
3204
  updates.unrealized = updates.unrealized.add(translate(item, item.data.unrealized));
3208
3205
  updates.income = updates.income.add(translate(item, item.data.income));
3209
- updates.summaryTotalCurrent = updates.summaryTotalCurrent.add(translate(item, item.data.summaryTotalCurrent));
3210
- updates.summaryTotalPrevious = updates.summaryTotalPrevious.add(translate(item, item.data.summaryTotalPrevious));
3211
- updates.summaryTotalPrevious2 = updates.summaryTotalPrevious2.add(translate(item, item.data.summaryTotalPrevious2));
3206
+ updates.summaryTotalCurrent = updates.summaryTotalCurrent.add(translate(item, item.data.periodGain));
3207
+ updates.summaryTotalPrevious = updates.summaryTotalPrevious.add(translate(item, item.data.periodGainPrevious));
3208
+ updates.summaryTotalPrevious2 = updates.summaryTotalPrevious2.add(translate(item, item.data.periodGainPrevious2));
3212
3209
  updates.marketPrevious = updates.marketPrevious.add(translate(item, item.data.marketPrevious));
3213
3210
  updates.marketPrevious2 = updates.marketPrevious2.add(translate(item, item.data.marketPrevious2));
3214
3211
 
@@ -3220,6 +3217,10 @@ module.exports = (() => {
3220
3217
  updates.cashTotal = updates.cashTotal.add(translate(item, item.data.market));
3221
3218
  }
3222
3219
 
3220
+ updates.periodDivisorCurrent = updates.periodDivisorCurrent.add(translate(item, item.data.periodDivisor));
3221
+ updates.periodDivisorPrevious = updates.periodDivisorPrevious.add(translate(item, item.data.periodDivisorPrevious));
3222
+ updates.periodDivisorPrevious2 = updates.periodDivisorPrevious2.add(translate(item, item.data.periodDivisorPrevious2));
3223
+
3223
3224
  return updates;
3224
3225
  }, {
3225
3226
  basis: Decimal.ZERO,
@@ -3235,6 +3236,9 @@ module.exports = (() => {
3235
3236
  periodUnrealized: Decimal.ZERO,
3236
3237
  periodIncome: Decimal.ZERO,
3237
3238
  cashTotal: Decimal.ZERO,
3239
+ periodDivisorCurrent: Decimal.ZERO,
3240
+ periodDivisorPrevious: Decimal.ZERO,
3241
+ periodDivisorPrevious2: Decimal.ZERO
3238
3242
  });
3239
3243
 
3240
3244
  actual.basis = updates.basis;
@@ -3250,12 +3254,16 @@ module.exports = (() => {
3250
3254
  actual.periodUnrealized = updates.periodUnrealized;
3251
3255
  actual.periodIncome = updates.periodIncome;
3252
3256
  actual.cashTotal = updates.cashTotal;
3257
+ actual.periodDivisorCurrent = updates.periodDivisorCurrent;
3258
+ actual.periodDivisorPrevious = updates.periodDivisorPrevious;
3259
+ actual.periodDivisorPrevious2 = updates.periodDivisorPrevious2;
3253
3260
 
3254
3261
  format.basis = formatCurrency(actual.basis, currency);
3255
3262
  format.realized = formatCurrency(actual.realized, currency);
3256
3263
  format.unrealized = formatCurrency(actual.unrealized, currency);
3257
3264
  format.income = formatCurrency(actual.income, currency);
3258
3265
  format.summaryTotalCurrent = formatCurrency(updates.summaryTotalCurrent, currency);
3266
+ format.summaryTotalCurrentNegative = updates.summaryTotalCurrent.getIsNegative();
3259
3267
  format.summaryTotalPrevious = formatCurrency(updates.summaryTotalPrevious, currency);
3260
3268
  format.summaryTotalPreviousNegative = updates.summaryTotalPrevious.getIsNegative();
3261
3269
  format.summaryTotalPrevious2 = formatCurrency(updates.summaryTotalPrevious2, currency);
@@ -3290,25 +3298,9 @@ module.exports = (() => {
3290
3298
  format.periodPrice = formatCurrency(actual.periodPrice, currency);
3291
3299
  format.periodPricePrevious = formatCurrency(actual.periodPricePrevious, currency);
3292
3300
 
3293
- actual.periodRealized = item.data.periodRealized;
3294
- actual.periodRealizedPrevious = item.data.periodRealizedPrevious;
3295
- actual.periodRealizedPrevious2 = item.data.periodRealizedPrevious2;
3296
-
3297
- actual.periodRealizedBasis = item.data.periodRealizedBasis;
3298
- actual.periodRealizedBasisPrevious = item.data.periodRealizedBasisPrevious;
3299
- actual.periodRealizedBasisPrevious2 = item.data.periodRealizedBasisPrevious2;
3300
-
3301
- actual.periodUnrealized = item.data.periodUnrealized;
3302
- actual.periodUnrealizedPrevious = item.data.periodUnrealizedPrevious;
3303
- actual.periodUnrealizedPrevious2 = item.data.periodUnrealizedPrevious2;
3304
-
3305
- actual.periodUnrealizedBasis = item.data.periodUnrealizedBasis;
3306
- actual.periodUnrealizedBasisPrevious = item.data.periodUnrealizedBasisPrevious;
3307
- actual.periodUnrealizedBasisPrevious2 = item.data.periodUnrealizedBasisPrevious2;
3308
-
3309
- actual.periodPercent = calculatePeriodPercent(actual.summaryTotalCurrent, actual.periodRealizedBasis, actual.periodUnrealizedBasis);
3310
- actual.periodPercentPrevious = calculatePeriodPercent(actual.summaryTotalPrevious, actual.periodRealizedBasisPrevious, actual.periodUnrealizedBasisPrevious);
3311
- actual.periodPercentPrevious2 = calculatePeriodPercent(actual.summaryTotalPrevious2, actual.periodRealizedBasisPrevious2, actual.periodUnrealizedBasisPrevious2);
3301
+ actual.periodPercent = calculatePeriodPercent(actual.summaryTotalCurrent, actual.periodDivisorCurrent);
3302
+ actual.periodPercentPrevious = calculatePeriodPercent(actual.summaryTotalPrevious, actual.periodDivisorPrevious);
3303
+ actual.periodPercentPrevious2 = calculatePeriodPercent(actual.summaryTotalPrevious2, actual.periodDivisorPrevious2);
3312
3304
 
3313
3305
  format.periodPercent = formatPercent(actual.periodPercent, 2);
3314
3306
  format.periodPercentPrevious = formatPercent(actual.periodPercentPrevious, 2);
@@ -3367,7 +3359,7 @@ module.exports = (() => {
3367
3359
  updates.marketAbsolute = updates.marketAbsolute.add(translate(item, item.data.marketAbsolute));
3368
3360
  updates.unrealized = updates.unrealized.add(translate(item, item.data.unrealized));
3369
3361
  updates.unrealizedToday = updates.unrealizedToday.add(translate(item, item.data.unrealizedToday));
3370
- updates.summaryTotalCurrent = updates.summaryTotalCurrent.add(translate(item, item.data.summaryTotalCurrent));
3362
+ updates.summaryTotalCurrent = updates.summaryTotalCurrent.add(translate(item, item.data.periodGain));
3371
3363
 
3372
3364
  return updates;
3373
3365
  }, {
@@ -3385,7 +3377,7 @@ module.exports = (() => {
3385
3377
  marketDirection: { up: item.data.marketChange.getIsPositive(), down: item.data.marketChange.getIsNegative() },
3386
3378
  unrealized: actual.unrealized.add(translate(item, item.data.unrealizedChange)),
3387
3379
  unrealizedToday: actual.unrealizedToday.add(translate(item, item.data.unrealizedTodayChange)),
3388
- summaryTotalCurrent: actual.summaryTotalCurrent.add(translate(item, item.data.summaryTotalCurrentChange))
3380
+ summaryTotalCurrent: actual.summaryTotalCurrent.add(translate(item, item.data.periodGainChange))
3389
3381
  };
3390
3382
  }
3391
3383
 
@@ -3441,7 +3433,7 @@ module.exports = (() => {
3441
3433
  if (group.single && item) {
3442
3434
  actual.periodUnrealized = item.data.periodUnrealized;
3443
3435
 
3444
- actual.periodPercent = calculatePeriodPercent(actual.summaryTotalCurrent, actual.periodRealizedBasis, actual.periodUnrealizedBasis);
3436
+ actual.periodPercent = calculatePeriodPercent(actual.summaryTotalCurrent, actual.periodDivisorCurrent);
3445
3437
  format.periodPercent = formatPercent(actual.periodPercent, 2);
3446
3438
  }
3447
3439
  }
@@ -3502,11 +3494,8 @@ module.exports = (() => {
3502
3494
  }
3503
3495
  }
3504
3496
 
3505
- function calculatePeriodPercent(periodSummaryTotal, realizedBasis, unrealizedBasis) {
3506
- const numerator = periodSummaryTotal;
3507
- const denominator = realizedBasis.add(unrealizedBasis);
3508
-
3509
- return denominator.getIsZero() ? Decimal.ZERO : numerator.divide(denominator);
3497
+ function calculatePeriodPercent(periodSummaryTotal, periodDivisor) {
3498
+ return periodDivisor.getIsZero() ? Decimal.ZERO : periodSummaryTotal.divide(periodDivisor);
3510
3499
  }
3511
3500
 
3512
3501
  const unchanged = { up: false, down: false };
@@ -3579,12 +3568,6 @@ module.exports = (() => {
3579
3568
  this._data.unrealized = null;
3580
3569
  this._data.unrealizedChange = null;
3581
3570
 
3582
- this._data.summaryTotalCurrent = null;
3583
- this._data.summaryTotalCurrentChange = null;
3584
-
3585
- this._data.summaryTotalPrevious = null;
3586
- this._data.summaryTotalPrevious2 = null;
3587
-
3588
3571
  this._data.marketPrevious = null;
3589
3572
  this._data.marketPrevious2 = null;
3590
3573
 
@@ -3595,26 +3578,24 @@ module.exports = (() => {
3595
3578
  this._data.income = null;
3596
3579
  this._data.basisPrice = null;
3597
3580
 
3581
+ this._data.periodIncome = null;
3598
3582
  this._data.periodRealized = null;
3599
- this._data.periodRealizedPrevious = null;
3600
- this._data.periodRealizedPrevious2 = null;
3583
+ this._data.periodUnrealized = null;
3601
3584
 
3602
- this._data.periodRealizedBasis = null;
3603
- this._data.periodRealizedBasisPrevious = null;
3604
- this._data.periodRealizedBasisPrevious2 = null;
3585
+ this._data.periodPrice = null;
3586
+ this._data.periodPricePrevious = null;
3605
3587
 
3606
- this._data.periodUnrealized = null;
3607
- this._data.periodUnrealizedPrevious = null;
3608
- this._data.periodUnrealizedPrevious2 = null;
3588
+ this._data.periodGain = null;
3589
+ this._data.periodGainChange = null;
3609
3590
 
3610
- this._data.periodUnrealizedBasis = null;
3611
- this._data.periodUnrealizedBasisPrevious = null;
3612
- this._data.periodUnrealizedBasisPrevious2 = null;
3591
+ this._data.periodGainPrevious = null;
3592
+ this._data.periodGainPrevious2 = null;
3613
3593
 
3614
- this._data.periodIncome = null;
3594
+ this._data.periodDivisor = null;
3595
+ this._data.periodDivisorChange = null;
3615
3596
 
3616
- this._data.periodPrice = null;
3617
- this._data.periodPricePrevious = null;
3597
+ this._data.periodDivisorPrevious = null;
3598
+ this._data.periodDivisorPrevious2 = null;
3618
3599
 
3619
3600
  this._data.newsExists = false;
3620
3601
  this._data.fundamental = { };
@@ -3910,7 +3891,6 @@ module.exports = (() => {
3910
3891
 
3911
3892
  const previousSummary1 = getPreviousSummary(item.previousSummaries, 1);
3912
3893
  const previousSummary2 = getPreviousSummary(item.previousSummaries, 2);
3913
- const previousSummary3 = getPreviousSummary(item.previousSummaries, 3);
3914
3894
 
3915
3895
  const snapshot = getSnapshot(position, currentSummary, item._reporting);
3916
3896
 
@@ -3934,31 +3914,21 @@ module.exports = (() => {
3934
3914
 
3935
3915
  data.income = snapshot.income;
3936
3916
 
3937
- data.summaryTotalCurrent = calculateSummaryTotal(item.currentSummary, previousSummary1);
3938
- data.summaryTotalPrevious = calculateSummaryTotal(previousSummary1, previousSummary2);
3939
- data.summaryTotalPrevious2 = calculateSummaryTotal(previousSummary2, previousSummary3);
3940
-
3941
3917
  data.marketPrevious = previousSummary1 === null ? Decimal.ZERO : previousSummary1.end.value;
3942
3918
  data.marketPrevious2 = previousSummary2 === null ? Decimal.ZERO : previousSummary2.end.value;
3943
3919
  data.quantityPrevious = previousSummary1 === null ? Decimal.ZERO : previousSummary1.end.open;
3944
3920
 
3945
- data.periodRealized = calculatePeriodRealized(item.currentSummary, previousSummary1);
3946
- data.periodRealizedPrevious = calculatePeriodRealized(previousSummary1, previousSummary2);
3947
- data.periodRealizedPrevious2 = calculatePeriodRealized(previousSummary2, previousSummary3);
3948
-
3949
- data.periodRealizedBasis = calculatePeriodRealizedBasis(item.currentSummary, previousSummary1);
3950
- data.periodRealizedBasisPrevious = calculatePeriodRealizedBasis(previousSummary1, previousSummary2);
3951
- data.periodRealizedBasisPrevious2 = calculatePeriodRealizedBasis(previousSummary2, previousSummary3);
3952
-
3953
- data.periodUnrealized = calculatePeriodUnrealized(item.currentSummary, previousSummary1);
3954
- data.periodUnrealizedPrevious = calculatePeriodUnrealized(previousSummary1, previousSummary2);
3955
- data.periodUnrealizedPrevious2 = calculatePeriodUnrealized(previousSummary2, previousSummary3);
3921
+ data.periodIncome = currentSummary !== null ? currentSummary.period.income : Decimal.ZERO;
3922
+ data.periodRealized = currentSummary !== null ? currentSummary.period.realized : Decimal.ZERO;
3923
+ data.periodUnrealized = currentSummary !== null ? currentSummary.period.unrealized : Decimal.ZERO;
3956
3924
 
3957
- data.periodUnrealizedBasis = calculatePeriodUnrealizedBasis(item.currentSummary, previousSummary1);
3958
- data.periodUnrealizedBasisPrevious = calculatePeriodUnrealizedBasis(previousSummary1, previousSummary2);
3959
- data.periodUnrealizedBasisPrevious2 = calculatePeriodUnrealizedBasis(previousSummary2, previousSummary3);
3960
-
3961
- data.periodIncome = calculatePeriodIncome(item.currentSummary, previousSummary1);
3925
+ data.periodGain = calculatePeriodGain(currentSummary);
3926
+ data.periodGainPrevious = calculatePeriodGain(previousSummary1);
3927
+ data.periodGainPrevious2 = calculatePeriodGain(previousSummary2);
3928
+
3929
+ data.periodDivisor = calculatePeriodDivisor(currentSummary);
3930
+ data.periodDivisorPrevious = calculatePeriodDivisor(previousSummary1);
3931
+ data.periodDivisorPrevious2 = calculatePeriodDivisor(previousSummary2);
3962
3932
 
3963
3933
  if (snapshot.open.getIsZero()) {
3964
3934
  data.basisPrice = Decimal.ZERO;
@@ -3982,7 +3952,6 @@ module.exports = (() => {
3982
3952
  function calculatePriceData(item, price) {
3983
3953
  const position = item.position;
3984
3954
  const snapshot = getSnapshot(position, item.currentSummary, item._reporting);
3985
- const previousSummaries = item.previousSummaries;
3986
3955
 
3987
3956
  const data = item._data;
3988
3957
 
@@ -4045,8 +4014,6 @@ module.exports = (() => {
4045
4014
  const currentSummary = item.currentSummary;
4046
4015
 
4047
4016
  if (currentSummary && position.instrument.type !== InstrumentType.CASH) {
4048
- const previousSummary = getPreviousSummary(previousSummaries, 1);
4049
-
4050
4017
  let priceToUse;
4051
4018
 
4052
4019
  if (price) {
@@ -4060,8 +4027,6 @@ module.exports = (() => {
4060
4027
  }
4061
4028
 
4062
4029
  if (priceToUse !== null) {
4063
- const period = currentSummary.period;
4064
-
4065
4030
  let unrealized = currentSummary.end.open.multiply(priceToUse).add(currentSummary.end.basis);
4066
4031
  let unrealizedChange;
4067
4032
 
@@ -4071,101 +4036,51 @@ module.exports = (() => {
4071
4036
  unrealizedChange = Decimal.ZERO;
4072
4037
  }
4073
4038
 
4074
- let summaryTotalCurrent = period.realized.add(period.income).add(unrealized).subtract(previousSummary !== null ? previousSummary.period.unrealized : Decimal.ZERO);
4075
- let summaryTotalCurrentChange;
4039
+ data.unrealized = unrealized;
4040
+ data.unrealizedChange = unrealizedChange;
4041
+
4042
+ let periodGain = calculatePeriodGain(currentSummary, priceToUse);
4043
+ let periodGainChange;
4076
4044
 
4077
- if (data.summaryTotalCurrent !== null) {
4078
- summaryTotalCurrentChange = summaryTotalCurrent.subtract(data.summaryTotalCurrent);
4045
+ if (data.periodGain !== null) {
4046
+ periodGainChange = periodGain.subtract(data.periodGain);
4079
4047
  } else {
4080
- summaryTotalCurrentChange = Decimal.ZERO;
4048
+ periodGainChange = Decimal.ZERO;
4081
4049
  }
4082
4050
 
4083
- data.summaryTotalCurrent = summaryTotalCurrent;
4084
- data.summaryTotalCurrentChange = summaryTotalCurrentChange;
4085
-
4086
- data.unrealized = unrealized;
4087
- data.unrealizedChange = unrealizedChange;
4088
-
4089
- data.periodUnrealized = calculatePeriodUnrealized(item.currentSummary, previousSummary, data.unrealized);
4090
- data.periodUnrealizedChange = unrealizedChange;
4051
+ data.periodGain = periodGain;
4052
+ data.periodGainChange = periodGainChange;
4091
4053
  } else {
4092
- data.summaryTotalCurrentChange = Decimal.ZERO;
4093
-
4094
4054
  data.unrealized = Decimal.ZERO;
4095
4055
  data.unrealizedChange = Decimal.ZERO;
4096
4056
 
4097
- data.periodUnrealizedChange = Decimal.ZERO;
4057
+ data.periodGainChange = Decimal.ZERO;
4098
4058
  }
4099
4059
  } else {
4100
- data.summaryTotalCurrentChange = Decimal.ZERO;
4101
-
4102
4060
  data.unrealized = Decimal.ZERO;
4103
4061
  data.unrealizedChange = Decimal.ZERO;
4104
- }
4105
- }
4106
-
4107
- function calculateSummaryTotal(currentSummary, previousSummary) {
4108
- let returnRef;
4109
-
4110
- if (currentSummary) {
4111
- const period = currentSummary.period;
4112
4062
 
4113
- returnRef = period.realized.add(period.income).add(period.unrealized).subtract(previousSummary !== null ? previousSummary.period.unrealized : Decimal.ZERO);
4114
- } else {
4115
- returnRef = Decimal.ZERO;
4063
+ data.periodGainChange = Decimal.ZERO;
4116
4064
  }
4117
-
4118
- return returnRef;
4119
4065
  }
4120
4066
 
4121
- function calculatePeriodRealized(currentSummary, previousSummary) {
4067
+ function calculatePeriodGain(currentSummary, overridePrice) {
4122
4068
  let returnRef;
4123
4069
 
4124
4070
  if (currentSummary) {
4125
- const period = currentSummary.period;
4071
+ let endValue;
4126
4072
 
4127
- returnRef = period.realized;
4128
- } else {
4129
- returnRef = Decimal.ZERO;
4130
- }
4131
-
4132
- return returnRef;
4133
- }
4134
-
4135
- function calculatePeriodRealizedBasis(currentSummary, previousSummary) {
4136
- let returnRef;
4137
-
4138
- if (currentSummary) {
4139
- const period = currentSummary.period;
4140
-
4141
- returnRef = period.sells.subtract(calculatePeriodRealized(currentSummary, previousSummary));
4142
- } else {
4143
- returnRef = Decimal.ZERO;
4144
- }
4145
-
4146
- return returnRef;
4147
- }
4148
-
4149
- function calculatePeriodUnrealized(currentSummary, previousSummary, override) {
4150
- let returnRef;
4151
-
4152
- if (currentSummary) {
4153
- const period = currentSummary.period;
4154
- const unrealized = override || period.unrealized;
4155
-
4156
- returnRef = unrealized.subtract(previousSummary !== null ? previousSummary.period.unrealized : Decimal.ZERO);
4157
- } else {
4158
- returnRef = Decimal.ZERO;
4159
- }
4073
+ if (overridePrice) {
4074
+ endValue = currentSummary.end.open.multiply(overridePrice);
4075
+ } else {
4076
+ endValue = currentSummary.end.value;
4077
+ }
4160
4078
 
4161
- return returnRef;
4162
- }
4163
-
4164
- function calculatePeriodUnrealizedBasis(currentSummary, previousSummary) {
4165
- let returnRef;
4079
+ const valueChange = endValue.subtract(currentSummary.start.value);
4080
+ const tradeChange = currentSummary.period.sells.subtract(currentSummary.period.buys);
4081
+ const incomeChange = currentSummary.period.income;
4166
4082
 
4167
- if (currentSummary) {
4168
- returnRef = currentSummary.end.basis.absolute();
4083
+ returnRef = valueChange.add(tradeChange).add(incomeChange);
4169
4084
  } else {
4170
4085
  returnRef = Decimal.ZERO;
4171
4086
  }
@@ -4173,13 +4088,11 @@ module.exports = (() => {
4173
4088
  return returnRef;
4174
4089
  }
4175
4090
 
4176
- function calculatePeriodIncome(currentSummary, previousSummary) {
4091
+ function calculatePeriodDivisor(currentSummary) {
4177
4092
  let returnRef;
4178
4093
 
4179
4094
  if (currentSummary) {
4180
- const period = currentSummary.period;
4181
-
4182
- returnRef = period.income;
4095
+ returnRef = currentSummary.start.value.add(currentSummary.period.buys);
4183
4096
  } else {
4184
4097
  returnRef = Decimal.ZERO;
4185
4098
  }