@barchart/portfolio-api-common 1.24.0 → 1.25.0

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.
@@ -34,10 +34,18 @@ module.exports = (() => {
34
34
  } else if (basis instanceof Decimal) {
35
35
  basisToUse = basis;
36
36
  }
37
+
38
+ let quantityToUse = null;
39
+
40
+ if (is.number(basis)) {
41
+ quantityToUse = new Decimal(quantity);
42
+ } else if (basis instanceof Decimal) {
43
+ quantityToUse = quantity;
44
+ }
37
45
 
38
46
  const calculator = calculators.get(instrument.type);
39
47
 
40
- return calculator(instrument, basisToUse, quantity);
48
+ return calculator(instrument, basisToUse, quantityToUse);
41
49
  }
42
50
 
43
51
  toString() {
@@ -50,11 +58,7 @@ module.exports = (() => {
50
58
  }
51
59
 
52
60
  function calculateForEquity(instrument, basis, quantity) {
53
- if (basis === null) {
54
- return null;
55
- }
56
-
57
- if (quantity === Decimal.ZERO || quantity === 0) {
61
+ if (basis === null || quantity === null || quantity.getIsZero()) {
58
62
  return null;
59
63
  }
60
64
 
@@ -62,11 +66,7 @@ module.exports = (() => {
62
66
  }
63
67
 
64
68
  function calculateForEquityOption(instrument, basis, quantity) {
65
- if (basis === null) {
66
- return null;
67
- }
68
-
69
- if (quantity === Decimal.ZERO || quantity === 0) {
69
+ if (basis === null || quantity === null || quantity.getIsZero()) {
70
70
  return null;
71
71
  }
72
72
 
@@ -76,11 +76,7 @@ module.exports = (() => {
76
76
  }
77
77
 
78
78
  function calculateForFund(instrument, basis, quantity) {
79
- if (basis === null) {
80
- return null;
81
- }
82
-
83
- if (quantity === Decimal.ZERO || quantity === 0) {
79
+ if (basis === null || quantity === null || quantity.getIsZero()) {
84
80
  return null;
85
81
  }
86
82
 
@@ -88,11 +84,7 @@ module.exports = (() => {
88
84
  }
89
85
 
90
86
  function calculateForFuture(instrument, basis, quantity) {
91
- if (basis === null) {
92
- return null;
93
- }
94
-
95
- if (quantity === Decimal.ZERO || quantity === 0) {
87
+ if (basis === null || quantity === null || quantity.getIsZero()) {
96
88
  return null;
97
89
  }
98
90
 
@@ -103,11 +95,7 @@ module.exports = (() => {
103
95
  }
104
96
 
105
97
  function calculateForFutureOption(instrument, basis, quantity) {
106
- if (basis === null) {
107
- return null;
108
- }
109
-
110
- if (quantity === Decimal.ZERO || quantity === 0) {
98
+ if (basis === null || quantity === null || quantity.getIsZero()) {
111
99
  return null;
112
100
  }
113
101
 
@@ -118,11 +106,7 @@ module.exports = (() => {
118
106
  }
119
107
 
120
108
  function calculateForOther(instrument, basis, quantity) {
121
- if (basis === null) {
122
- return null;
123
- }
124
-
125
- if (quantity === Decimal.ZERO || quantity === 0) {
109
+ if (basis === null || quantity === null || quantity.getIsZero()) {
126
110
  return null;
127
111
  }
128
112
 
@@ -10,7 +10,7 @@ module.exports = (() => {
10
10
  *
11
11
  * @public
12
12
  */
13
- class PositionSummaryFormatter{
13
+ class PositionSummaryFormatter {
14
14
  /**
15
15
  * The formatter
16
16
  *
@@ -26,9 +26,9 @@ module.exports = (() => {
26
26
  * all the positions and performs currency translation, as necessary.
27
27
  *
28
28
  * @public
29
- * @param {PositionContainer} container
30
29
  * @param {PositionLevelDefinition} definition
31
30
  * @param {PositionItem[]} items
31
+ * @param {Rate[]} rates
32
32
  * @param {Currency} currency
33
33
  * @param {String} key
34
34
  * @param {String} description
@@ -80,6 +80,7 @@ module.exports = (() => {
80
80
  this._dataFormat.quantity = null;
81
81
  this._dataFormat.quantityPrevious = null;
82
82
  this._dataFormat.basisPrice = null;
83
+ this._dataFormat.unrealizedPrice = null;
83
84
 
84
85
  this._dataActual.key = this._key;
85
86
  this._dataActual.description = this._description;
@@ -87,6 +88,7 @@ module.exports = (() => {
87
88
  this._dataActual.quantity = null;
88
89
  this._dataActual.quantityPrevious = null;
89
90
  this._dataActual.basisPrice = null;
91
+ this._dataActual.unrealizedPrice = null;
90
92
 
91
93
  if (this._single && items.length === 1) {
92
94
  const item = items[0];
@@ -668,7 +670,7 @@ module.exports = (() => {
668
670
  function formatFraction(value, currency, instrument) {
669
671
  let decimal = value instanceof Decimal;
670
672
 
671
- if (instrument) {
673
+ if (instrument && value !== null) {
672
674
  const type = instrument.type;
673
675
  const code = instrument.code;
674
676
 
@@ -851,6 +853,7 @@ module.exports = (() => {
851
853
 
852
854
  if (group.single && groupItems.length === 1) {
853
855
  const item = group._items[0];
856
+ const instrument = item.position.instrument;
854
857
 
855
858
  actual.quantity = item.data.quantity;
856
859
  actual.quantityPrevious = item.data.quantityPrevious;
@@ -859,7 +862,7 @@ module.exports = (() => {
859
862
  format.quantityPrevious = formatDecimal(actual.quantityPrevious, 2);
860
863
 
861
864
  actual.basisPrice = item.data.basisPrice;
862
- format.basisPrice = formatFraction(actual.basisPrice, currency, item.position.instrument);
865
+ format.basisPrice = formatFraction(actual.basisPrice, currency, instrument);
863
866
 
864
867
  actual.periodPrice = item.data.periodPrice;
865
868
  actual.periodPricePrevious = item.data.periodPricePrevious;
@@ -867,6 +870,9 @@ module.exports = (() => {
867
870
  format.periodPrice = formatCurrency(actual.periodPrice, currency);
868
871
  format.periodPricePrevious = formatCurrency(actual.periodPricePrevious, currency);
869
872
 
873
+ actual.unrealizedPrice = item.data.unrealizedPrice;
874
+ format.unrealizedPrice = formatFraction(actual.unrealizedPrice, currency, instrument);
875
+
870
876
  format.invalid = definition.type === PositionLevelType.POSITION && item.invalid;
871
877
  format.locked = definition.type === PositionLevelType.POSITION && item.data.locked;
872
878
  format.calculating = definition.type === PositionLevelType.POSITION && item.data.calculating;
@@ -1021,6 +1027,11 @@ module.exports = (() => {
1021
1027
 
1022
1028
  actual.periodPercent = calculateGainPercent(actual.summaryTotalCurrent, actual.periodDivisorCurrent);
1023
1029
  format.periodPercent = formatPercent(actual.periodPercent, 2);
1030
+
1031
+ if (group.single && item) {
1032
+ actual.unrealizedPrice = item.data.unrealizedPrice;
1033
+ format.unrealizedPrice = formatFraction(actual.unrealizedPrice, currency, item.position.instrument);
1034
+ }
1024
1035
  }
1025
1036
 
1026
1037
  function calculateMarketPercent(group, rates, parentGroup, portfolioGroup) {
@@ -8,7 +8,8 @@ const assert = require('@barchart/common-js/lang/assert'),
8
8
  const InstrumentType = require('./../data/InstrumentType'),
9
9
  PositionDirection = require('./../data/PositionDirection');
10
10
 
11
- const ValuationCalculator = require('./../calculators/ValuationCalculator');
11
+ const AveragePriceCalculator = require('./../calculators/AveragePriceCalculator'),
12
+ ValuationCalculator = require('./../calculators/ValuationCalculator');
12
13
 
13
14
  module.exports = (() => {
14
15
  'use strict';
@@ -73,7 +74,9 @@ module.exports = (() => {
73
74
 
74
75
  this._data.realized = null;
75
76
  this._data.income = null;
77
+
76
78
  this._data.basisPrice = null;
79
+ this._data.unrealizedPrice = null;
77
80
 
78
81
  this._data.periodIncome = null;
79
82
  this._data.periodRealized = null;
@@ -478,16 +481,8 @@ module.exports = (() => {
478
481
  data.periodDivisorPrevious = calculatePeriodDivisor(position.instrument.type, data.initiate, previousSummary1, previousSummary2);
479
482
  data.periodDivisorPrevious2 = calculatePeriodDivisor(position.instrument.type, data.initiate, previousSummary2, previousSummary3);
480
483
 
481
- if (snapshot.open.getIsZero()) {
482
- data.basisPrice = Decimal.ZERO;
483
- } else if (position.instrument.type === InstrumentType.FUTURE) {
484
- const minimumTick = position.instrument.future.tick;
485
- const minimumTickValue = position.instrument.future.value;
486
-
487
- data.basisPrice = basis.divide(snapshot.open).divide(minimumTickValue).multiply(minimumTick);
488
- } else {
489
- data.basisPrice = basis.divide(snapshot.open);
490
- }
484
+ data.basisPrice = AveragePriceCalculator.calculate(position.instrument, data.basis, snapshot.open) || Decimal.ZERO;
485
+ data.basisPrice = data.basisPrice.opposite();
491
486
 
492
487
  if (currentSummary && !currentSummary.end.open.getIsZero()) {
493
488
  data.periodPrice = currentSummary.end.value.divide(currentSummary.end.open);
@@ -617,6 +612,12 @@ module.exports = (() => {
617
612
 
618
613
  data.periodUnrealized = periodUnrealized;
619
614
  data.periodUnrealizedChange = periodUnrealizedChange;
615
+
616
+ if (snapshot.open.getIsZero()) {
617
+ data.unrealizedPrice = null;
618
+ } else {
619
+ data.unrealizedPrice = data.basisPrice.opposite().add(priceToUse);
620
+ }
620
621
  } else {
621
622
  data.unrealizedChange = Decimal.ZERO;
622
623
  data.periodUnrealizedChange = Decimal.ZERO;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.24.0",
3
+ "version": "1.25.0",
4
4
  "description": "Common JavaScript code used by Barchart's Portfolio Service",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",