@barchart/portfolio-api-common 1.0.111 → 1.0.115

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.
@@ -59,6 +59,7 @@ module.exports = (() => {
59
59
  this._dataActual.income = null;
60
60
  this._dataActual.market = null;
61
61
  this._dataActual.marketPercent = null;
62
+ this._dataActual.unrealized = null;
62
63
  this._dataActual.unrealizedToday = null;
63
64
  this._dataActual.total = null;
64
65
  this._dataActual.summaryTotalCurrent = null;
@@ -72,6 +73,8 @@ module.exports = (() => {
72
73
  this._dataFormat.market = null;
73
74
  this._dataFormat.marketPercent = null;
74
75
  this._dataFormat.marketDirection = null;
76
+ this._dataFormat.unrealized = null;
77
+ this._dataFormat.unrealizedNegative = false;
75
78
  this._dataFormat.unrealizedToday = null;
76
79
  this._dataFormat.unrealizedTodayNegative = false;
77
80
  this._dataFormat.total = null;
@@ -94,7 +97,7 @@ module.exports = (() => {
94
97
  this._dataFormat.quoteHigh = formatNumber(quote.highPrice, precision);
95
98
  this._dataFormat.quoteLow = formatNumber(quote.lowPrice, precision);
96
99
  this._dataFormat.quoteChange = formatNumber(quote.priceChange, precision);
97
- //this._dataFormat.quoteChangePercent = formatPercent(new Decimal(quote.percentChange), 2);
100
+ this._dataFormat.quoteChangePercent = formatPercent(new Decimal(quote.percentChange || 0), 2);
98
101
  this._dataFormat.quoteTime = quote.timeDisplay;
99
102
  this._dataFormat.quoteVolume = formatNumber(quote.volume, 0);
100
103
  } else {
@@ -203,7 +206,7 @@ module.exports = (() => {
203
206
 
204
207
  function formatPercent(decimal, precision) {
205
208
  if (decimal !== null) {
206
- return formatDecimal(decimal.multiply(100), precision);
209
+ return formatDecimal(decimal.multiply(100), precision) + '%';
207
210
  } else {
208
211
  return '—';
209
212
  }
@@ -228,6 +231,7 @@ module.exports = (() => {
228
231
  let updates = items.reduce((updates, item) => {
229
232
  updates.basis = updates.basis.add(item.data.basis);
230
233
  updates.realized = updates.realized.add(item.data.realized);
234
+ updates.unrealized = updates.unrealized.add(item.data.unrealized);
231
235
  updates.income = updates.income.add(item.data.income);
232
236
  updates.summaryTotalCurrent = updates.summaryTotalCurrent.add(item.data.summaryTotalCurrent);
233
237
  updates.summaryTotalPrevious = updates.summaryTotalPrevious.add(item.data.summaryTotalPrevious);
@@ -236,6 +240,7 @@ module.exports = (() => {
236
240
  }, {
237
241
  basis: Decimal.ZERO,
238
242
  realized: Decimal.ZERO,
243
+ unrealized: Decimal.ZERO,
239
244
  income: Decimal.ZERO,
240
245
  summaryTotalCurrent: Decimal.ZERO,
241
246
  summaryTotalPrevious: Decimal.ZERO
@@ -243,19 +248,24 @@ module.exports = (() => {
243
248
 
244
249
  actual.basis = updates.basis;
245
250
  actual.realized = updates.realized;
251
+ actual.unrealized = updates.unrealized;
246
252
  actual.income = updates.income;
247
253
  actual.summaryTotalCurrent = updates.summaryTotalCurrent;
248
254
  actual.summaryTotalPrevious = updates.summaryTotalPrevious;
249
255
 
250
256
  format.basis = formatCurrency(actual.basis, currency);
251
257
  format.realized = formatCurrency(actual.basis, currency);
258
+ format.unrealized = formatCurrency(actual.unrealized, currency);
252
259
  format.income = formatCurrency(actual.income, currency);
253
260
  format.summaryTotalCurrent = formatCurrency(updates.summaryTotalCurrent, currency);
254
261
  format.summaryTotalPrevious = formatCurrency(updates.summaryTotalPrevious, currency);
255
262
  format.summaryTotalPreviousNegative = updates.summaryTotalPrevious.getIsNegative();
256
263
 
257
264
  if (group.single) {
258
- format.quantity = formatDecimal(group._items[0].position.snapshot.open, 2);
265
+ const item = group._items[0];
266
+
267
+ format.quantity = formatDecimal(item.position.snapshot.open, 2);
268
+ format.basisPrice = formatCurrency(item.basisPrice, currency);
259
269
  }
260
270
  }
261
271
 
@@ -280,6 +290,7 @@ module.exports = (() => {
280
290
 
281
291
  updates = items.reduce((updates, item) => {
282
292
  updates.market = updates.market.add(item.data.market);
293
+ updates.unrealized = updates.unrealized.add(item.data.unrealized);
283
294
  updates.unrealizedToday = updates.unrealizedToday.add(item.data.unrealizedToday);
284
295
  updates.summaryTotalCurrent = updates.summaryTotalCurrent.add(item.data.summaryTotalCurrent);
285
296
 
@@ -287,6 +298,7 @@ module.exports = (() => {
287
298
  }, {
288
299
  market: Decimal.ZERO,
289
300
  marketDirection: unchanged,
301
+ unrealized: Decimal.ZERO,
290
302
  unrealizedToday: Decimal.ZERO,
291
303
  summaryTotalCurrent: Decimal.ZERO
292
304
 
@@ -295,6 +307,7 @@ module.exports = (() => {
295
307
  updates = {
296
308
  market: actual.market.add(item.data.marketChange),
297
309
  marketDirection: { up: item.data.marketChange.getIsPositive(), down: item.data.marketChange.getIsNegative() },
310
+ unrealized: actual.unrealized.add(item.data.unrealizedChange),
298
311
  unrealizedToday: actual.unrealizedToday.add(item.data.unrealizedTodayChange),
299
312
  summaryTotalCurrent: actual.summaryTotalCurrent.add(item.data.summaryTotalCurrentChange)
300
313
  };
@@ -313,6 +326,7 @@ module.exports = (() => {
313
326
  }
314
327
 
315
328
  actual.market = updates.market;
329
+ actual.unrealized = updates.unrealized;
316
330
  actual.unrealizedToday = updates.unrealizedToday;
317
331
  actual.summaryTotalCurrent = updates.summaryTotalCurrent;
318
332
  actual.total = updates.unrealizedToday.add(actual.realized).add(actual.income);
@@ -324,6 +338,9 @@ module.exports = (() => {
324
338
  setTimeout(() => format.marketDirection = updates.marketDirection, 0);
325
339
  }
326
340
 
341
+ format.unrealized = formatCurrency(actual.unrealized, currency);
342
+ format.unrealizedNegative = actual.unrealized.getIsNegative();
343
+
327
344
  format.unrealizedToday = formatCurrency(actual.unrealizedToday, currency);
328
345
  format.unrealizedTodayNegative = actual.unrealizedToday.getIsNegative();
329
346
 
@@ -36,14 +36,18 @@ module.exports = (() => {
36
36
  this._data.unrealizedToday = null;
37
37
  this._data.unrealizedTodayChange = null;
38
38
 
39
- this._data.realized = null;
40
- this._data.income = null;
39
+ this._data.unrealized = null;
40
+ this._data.unrealizedChange = null;
41
41
 
42
42
  this._data.summaryTotalCurrent = null;
43
43
  this._data.summaryTotalCurrentChange = null;
44
44
 
45
45
  this._data.summaryTotalPrevious = null;
46
46
 
47
+ this._data.realized = null;
48
+ this._data.income = null;
49
+ this._data.basisPrice = null;
50
+
47
51
  this._excluded = false;
48
52
 
49
53
  calculateStaticData(this);
@@ -91,9 +95,9 @@ module.exports = (() => {
91
95
  this._currentQuote = quote;
92
96
 
93
97
  this._data.previousPrice = this._data.currentPrice;
94
- this._data.currentPrice = price;
98
+ this._data.currentPrice = quote.lastPrice;
95
99
 
96
- this._quoteChangedEvent.fire(this._data, this._currentQuote);
100
+ this._quoteChangedEvent.fire(this._currentQuote);
97
101
  }
98
102
  }
99
103
 
@@ -138,10 +142,14 @@ module.exports = (() => {
138
142
  data.basis = basis;
139
143
 
140
144
  data.realized = snapshot.gain;
145
+ data.unrealized = Decimal.ZERO;
146
+
141
147
  data.income = snapshot.income;
142
148
 
143
149
  data.summaryTotalCurrent = calculateSummaryTotal(item.currentSummary);
144
150
  data.summaryTotalPrevious = calculateSummaryTotal(array.last(previousSummaries));
151
+
152
+ data.basisPrice = basis.divide(snapshot.open);
145
153
  }
146
154
 
147
155
  function calculatePriceData(item, price) {
@@ -174,7 +182,7 @@ module.exports = (() => {
174
182
 
175
183
  data.market = market;
176
184
  data.marketChange = marketChange;
177
-
185
+
178
186
  let unrealizedToday;
179
187
  let unrealizedTodayChange;
180
188
 
@@ -199,9 +207,16 @@ module.exports = (() => {
199
207
  if (summary && price) {
200
208
  const period = summary.period;
201
209
 
202
- let unrealizedCurrent = summary.end.open.multiply(price).add(summary.end.basis);
210
+ let unrealized = summary.end.open.multiply(price).add(summary.end.basis);
211
+ let unrealizedChange;
203
212
 
204
- let summaryTotalCurrent = period.realized.add(period.income).add(unrealizedCurrent);
213
+ if (data.unrealizedToday !== null) {
214
+ unrealizedChange = unrealized.subtract(data.unrealized);
215
+ } else {
216
+ unrealizedChange = Decimal.ZERO;
217
+ }
218
+
219
+ let summaryTotalCurrent = period.realized.add(period.income).add(unrealized);
205
220
  let summaryTotalCurrentChange;
206
221
 
207
222
  if (data.summaryTotalCurrent !== null) {
@@ -212,8 +227,14 @@ module.exports = (() => {
212
227
 
213
228
  data.summaryTotalCurrent = summaryTotalCurrent;
214
229
  data.summaryTotalCurrentChange = summaryTotalCurrentChange;
230
+
231
+ data.unrealized = unrealized;
232
+ data.unrealizedChange = unrealizedChange;
215
233
  } else {
216
234
  data.summaryTotalCurrentChange = Decimal.ZERO;
235
+
236
+ data.unrealized = Decimal.ZERO;
237
+ data.unrealizedChange = Decimal.ZERO;
217
238
  }
218
239
  }
219
240
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.0.111",
3
+ "version": "1.0.115",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",
@@ -1064,6 +1064,7 @@ module.exports = (() => {
1064
1064
  this._dataActual.income = null;
1065
1065
  this._dataActual.market = null;
1066
1066
  this._dataActual.marketPercent = null;
1067
+ this._dataActual.unrealized = null;
1067
1068
  this._dataActual.unrealizedToday = null;
1068
1069
  this._dataActual.total = null;
1069
1070
  this._dataActual.summaryTotalCurrent = null;
@@ -1077,6 +1078,8 @@ module.exports = (() => {
1077
1078
  this._dataFormat.market = null;
1078
1079
  this._dataFormat.marketPercent = null;
1079
1080
  this._dataFormat.marketDirection = null;
1081
+ this._dataFormat.unrealized = null;
1082
+ this._dataFormat.unrealizedNegative = false;
1080
1083
  this._dataFormat.unrealizedToday = null;
1081
1084
  this._dataFormat.unrealizedTodayNegative = false;
1082
1085
  this._dataFormat.total = null;
@@ -1099,7 +1102,7 @@ module.exports = (() => {
1099
1102
  this._dataFormat.quoteHigh = formatNumber(quote.highPrice, precision);
1100
1103
  this._dataFormat.quoteLow = formatNumber(quote.lowPrice, precision);
1101
1104
  this._dataFormat.quoteChange = formatNumber(quote.priceChange, precision);
1102
- //this._dataFormat.quoteChangePercent = formatPercent(new Decimal(quote.percentChange), 2);
1105
+ this._dataFormat.quoteChangePercent = formatPercent(new Decimal(quote.percentChange || 0), 2);
1103
1106
  this._dataFormat.quoteTime = quote.timeDisplay;
1104
1107
  this._dataFormat.quoteVolume = formatNumber(quote.volume, 0);
1105
1108
  } else {
@@ -1208,7 +1211,7 @@ module.exports = (() => {
1208
1211
 
1209
1212
  function formatPercent(decimal, precision) {
1210
1213
  if (decimal !== null) {
1211
- return formatDecimal(decimal.multiply(100), precision);
1214
+ return formatDecimal(decimal.multiply(100), precision) + '%';
1212
1215
  } else {
1213
1216
  return '—';
1214
1217
  }
@@ -1233,6 +1236,7 @@ module.exports = (() => {
1233
1236
  let updates = items.reduce((updates, item) => {
1234
1237
  updates.basis = updates.basis.add(item.data.basis);
1235
1238
  updates.realized = updates.realized.add(item.data.realized);
1239
+ updates.unrealized = updates.unrealized.add(item.data.unrealized);
1236
1240
  updates.income = updates.income.add(item.data.income);
1237
1241
  updates.summaryTotalCurrent = updates.summaryTotalCurrent.add(item.data.summaryTotalCurrent);
1238
1242
  updates.summaryTotalPrevious = updates.summaryTotalPrevious.add(item.data.summaryTotalPrevious);
@@ -1241,6 +1245,7 @@ module.exports = (() => {
1241
1245
  }, {
1242
1246
  basis: Decimal.ZERO,
1243
1247
  realized: Decimal.ZERO,
1248
+ unrealized: Decimal.ZERO,
1244
1249
  income: Decimal.ZERO,
1245
1250
  summaryTotalCurrent: Decimal.ZERO,
1246
1251
  summaryTotalPrevious: Decimal.ZERO
@@ -1248,19 +1253,24 @@ module.exports = (() => {
1248
1253
 
1249
1254
  actual.basis = updates.basis;
1250
1255
  actual.realized = updates.realized;
1256
+ actual.unrealized = updates.unrealized;
1251
1257
  actual.income = updates.income;
1252
1258
  actual.summaryTotalCurrent = updates.summaryTotalCurrent;
1253
1259
  actual.summaryTotalPrevious = updates.summaryTotalPrevious;
1254
1260
 
1255
1261
  format.basis = formatCurrency(actual.basis, currency);
1256
1262
  format.realized = formatCurrency(actual.basis, currency);
1263
+ format.unrealized = formatCurrency(actual.unrealized, currency);
1257
1264
  format.income = formatCurrency(actual.income, currency);
1258
1265
  format.summaryTotalCurrent = formatCurrency(updates.summaryTotalCurrent, currency);
1259
1266
  format.summaryTotalPrevious = formatCurrency(updates.summaryTotalPrevious, currency);
1260
1267
  format.summaryTotalPreviousNegative = updates.summaryTotalPrevious.getIsNegative();
1261
1268
 
1262
1269
  if (group.single) {
1263
- format.quantity = formatDecimal(group._items[0].position.snapshot.open, 2);
1270
+ const item = group._items[0];
1271
+
1272
+ format.quantity = formatDecimal(item.position.snapshot.open, 2);
1273
+ format.basisPrice = formatCurrency(item.basisPrice, currency);
1264
1274
  }
1265
1275
  }
1266
1276
 
@@ -1285,6 +1295,7 @@ module.exports = (() => {
1285
1295
 
1286
1296
  updates = items.reduce((updates, item) => {
1287
1297
  updates.market = updates.market.add(item.data.market);
1298
+ updates.unrealized = updates.unrealized.add(item.data.unrealized);
1288
1299
  updates.unrealizedToday = updates.unrealizedToday.add(item.data.unrealizedToday);
1289
1300
  updates.summaryTotalCurrent = updates.summaryTotalCurrent.add(item.data.summaryTotalCurrent);
1290
1301
 
@@ -1292,6 +1303,7 @@ module.exports = (() => {
1292
1303
  }, {
1293
1304
  market: Decimal.ZERO,
1294
1305
  marketDirection: unchanged,
1306
+ unrealized: Decimal.ZERO,
1295
1307
  unrealizedToday: Decimal.ZERO,
1296
1308
  summaryTotalCurrent: Decimal.ZERO
1297
1309
 
@@ -1300,6 +1312,7 @@ module.exports = (() => {
1300
1312
  updates = {
1301
1313
  market: actual.market.add(item.data.marketChange),
1302
1314
  marketDirection: { up: item.data.marketChange.getIsPositive(), down: item.data.marketChange.getIsNegative() },
1315
+ unrealized: actual.unrealized.add(item.data.unrealizedChange),
1303
1316
  unrealizedToday: actual.unrealizedToday.add(item.data.unrealizedTodayChange),
1304
1317
  summaryTotalCurrent: actual.summaryTotalCurrent.add(item.data.summaryTotalCurrentChange)
1305
1318
  };
@@ -1318,6 +1331,7 @@ module.exports = (() => {
1318
1331
  }
1319
1332
 
1320
1333
  actual.market = updates.market;
1334
+ actual.unrealized = updates.unrealized;
1321
1335
  actual.unrealizedToday = updates.unrealizedToday;
1322
1336
  actual.summaryTotalCurrent = updates.summaryTotalCurrent;
1323
1337
  actual.total = updates.unrealizedToday.add(actual.realized).add(actual.income);
@@ -1329,6 +1343,9 @@ module.exports = (() => {
1329
1343
  setTimeout(() => format.marketDirection = updates.marketDirection, 0);
1330
1344
  }
1331
1345
 
1346
+ format.unrealized = formatCurrency(actual.unrealized, currency);
1347
+ format.unrealizedNegative = actual.unrealized.getIsNegative();
1348
+
1332
1349
  format.unrealizedToday = formatCurrency(actual.unrealizedToday, currency);
1333
1350
  format.unrealizedTodayNegative = actual.unrealizedToday.getIsNegative();
1334
1351
 
@@ -1418,14 +1435,18 @@ module.exports = (() => {
1418
1435
  this._data.unrealizedToday = null;
1419
1436
  this._data.unrealizedTodayChange = null;
1420
1437
 
1421
- this._data.realized = null;
1422
- this._data.income = null;
1438
+ this._data.unrealized = null;
1439
+ this._data.unrealizedChange = null;
1423
1440
 
1424
1441
  this._data.summaryTotalCurrent = null;
1425
1442
  this._data.summaryTotalCurrentChange = null;
1426
1443
 
1427
1444
  this._data.summaryTotalPrevious = null;
1428
1445
 
1446
+ this._data.realized = null;
1447
+ this._data.income = null;
1448
+ this._data.basisPrice = null;
1449
+
1429
1450
  this._excluded = false;
1430
1451
 
1431
1452
  calculateStaticData(this);
@@ -1473,9 +1494,9 @@ module.exports = (() => {
1473
1494
  this._currentQuote = quote;
1474
1495
 
1475
1496
  this._data.previousPrice = this._data.currentPrice;
1476
- this._data.currentPrice = price;
1497
+ this._data.currentPrice = quote.lastPrice;
1477
1498
 
1478
- this._quoteChangedEvent.fire(this._data, this._currentQuote);
1499
+ this._quoteChangedEvent.fire(this._currentQuote);
1479
1500
  }
1480
1501
  }
1481
1502
 
@@ -1520,10 +1541,14 @@ module.exports = (() => {
1520
1541
  data.basis = basis;
1521
1542
 
1522
1543
  data.realized = snapshot.gain;
1544
+ data.unrealized = Decimal.ZERO;
1545
+
1523
1546
  data.income = snapshot.income;
1524
1547
 
1525
1548
  data.summaryTotalCurrent = calculateSummaryTotal(item.currentSummary);
1526
1549
  data.summaryTotalPrevious = calculateSummaryTotal(array.last(previousSummaries));
1550
+
1551
+ data.basisPrice = basis.divide(snapshot.open);
1527
1552
  }
1528
1553
 
1529
1554
  function calculatePriceData(item, price) {
@@ -1556,7 +1581,7 @@ module.exports = (() => {
1556
1581
 
1557
1582
  data.market = market;
1558
1583
  data.marketChange = marketChange;
1559
-
1584
+
1560
1585
  let unrealizedToday;
1561
1586
  let unrealizedTodayChange;
1562
1587
 
@@ -1581,9 +1606,16 @@ module.exports = (() => {
1581
1606
  if (summary && price) {
1582
1607
  const period = summary.period;
1583
1608
 
1584
- let unrealizedCurrent = summary.end.open.multiply(price).add(summary.end.basis);
1609
+ let unrealized = summary.end.open.multiply(price).add(summary.end.basis);
1610
+ let unrealizedChange;
1611
+
1612
+ if (data.unrealizedToday !== null) {
1613
+ unrealizedChange = unrealized.subtract(data.unrealized);
1614
+ } else {
1615
+ unrealizedChange = Decimal.ZERO;
1616
+ }
1585
1617
 
1586
- let summaryTotalCurrent = period.realized.add(period.income).add(unrealizedCurrent);
1618
+ let summaryTotalCurrent = period.realized.add(period.income).add(unrealized);
1587
1619
  let summaryTotalCurrentChange;
1588
1620
 
1589
1621
  if (data.summaryTotalCurrent !== null) {
@@ -1594,8 +1626,14 @@ module.exports = (() => {
1594
1626
 
1595
1627
  data.summaryTotalCurrent = summaryTotalCurrent;
1596
1628
  data.summaryTotalCurrentChange = summaryTotalCurrentChange;
1629
+
1630
+ data.unrealized = unrealized;
1631
+ data.unrealizedChange = unrealizedChange;
1597
1632
  } else {
1598
1633
  data.summaryTotalCurrentChange = Decimal.ZERO;
1634
+
1635
+ data.unrealized = Decimal.ZERO;
1636
+ data.unrealizedChange = Decimal.ZERO;
1599
1637
  }
1600
1638
  }
1601
1639