@barchart/portfolio-api-common 1.0.150 → 1.0.154

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.
@@ -181,7 +181,7 @@ module.exports = (() => {
181
181
  const items = populatedObjects[key];
182
182
  const first = items[0];
183
183
 
184
- list.push(new PositionGroup(this, parent, items, levelDefinition.currencySelector(first), key, levelDefinition.descriptionSelector(first), levelDefinition.single && items.length === 1));
184
+ list.push(new PositionGroup(this, parent, items, levelDefinition.currencySelector(first), key, levelDefinition.descriptionSelector(first), levelDefinition.single && items.length === 1, levelDefinition.aggregateCash));
185
185
 
186
186
  return list;
187
187
  }, [ ]);
@@ -523,6 +523,5 @@ module.exports = (() => {
523
523
  }
524
524
  }
525
525
 
526
-
527
526
  return PositionContainer;
528
527
  })();
@@ -8,6 +8,8 @@ const array = require('@barchart/common-js/lang/array'),
8
8
  is = require('@barchart/common-js/lang/is'),
9
9
  Rate = require('@barchart/common-js/lang/Rate');
10
10
 
11
+ const InstrumentType = require('./../data/InstrumentType');
12
+
11
13
  module.exports = (() => {
12
14
  'use strict';
13
15
 
@@ -25,9 +27,10 @@ module.exports = (() => {
25
27
  * @param {String} key
26
28
  * @param {String} description
27
29
  * @param {Boolean=} single
30
+ * @param {Boolean=} aggregateCash
28
31
  */
29
32
  class PositionGroup {
30
- constructor(container, parent, items, currency, key, description, single) {
33
+ constructor(container, parent, items, currency, key, description, single, aggregateCash) {
31
34
  this._id = counter++;
32
35
  this._container = container;
33
36
  this._parent = parent || null;
@@ -40,6 +43,7 @@ module.exports = (() => {
40
43
  this._description = description;
41
44
 
42
45
  this._single = is.boolean(single) && single;
46
+ this._aggregateCash = is.boolean(aggregateCash) && aggregateCash;
43
47
 
44
48
  this._excluded = false;
45
49
  this._suspended = false;
@@ -102,6 +106,8 @@ module.exports = (() => {
102
106
  this._dataFormat.quoteChangePercent = null;
103
107
  this._dataFormat.quoteTime = null;
104
108
  this._dataFormat.quoteVolume = null;
109
+ this._dataFormat.quoteChangeDirection = unchanged;
110
+ this._dataFormat.quoteChangeNegative = false;
105
111
 
106
112
  this._dataActual.currentPrice = null;
107
113
  this._dataActual.previousPrice = null;
@@ -115,6 +121,7 @@ module.exports = (() => {
115
121
  this._dataActual.total = null;
116
122
  this._dataActual.summaryTotalCurrent = null;
117
123
  this._dataActual.summaryTotalPrevious = null;
124
+ this._dataActual.cashTotal = null;
118
125
 
119
126
  this._dataFormat.currentPrice = null;
120
127
  this._dataFormat.previousPrice = null;
@@ -135,6 +142,7 @@ module.exports = (() => {
135
142
  this._dataActual.summaryTotalCurrentNegative = false;
136
143
  this._dataFormat.summaryTotalPrevious = null;
137
144
  this._dataFormat.summaryTotalPreviousNegative = false;
145
+ this._dataFormat.cashTotal = null;
138
146
 
139
147
  this._items.forEach((item) => {
140
148
  this._disposeStack.push(item.registerQuoteChangeHandler((quote, sender) => {
@@ -161,6 +169,9 @@ module.exports = (() => {
161
169
  this._dataFormat.quoteChangePercent = formatPercent(new Decimal(this._dataActual.quoteChangePercent || 0), 2);
162
170
  this._dataFormat.quoteTime = this._dataActual.quoteTime;
163
171
  this._dataFormat.quoteVolume = formatNumber(this._dataActual.quoteVolume, 0);
172
+
173
+ setTimeout(() => this._dataFormat.quoteChangeDirection = { up: this._dataActual.quoteChange.getIsPositive(), down: this._dataActual.quoteChange.getIsNegative() }, 0);
174
+ this._dataFormat.quoteChangeNegative = this._dataActual.quoteChange.getIsNegative();
164
175
  } else {
165
176
  this._dataActual.currentPrice = null;
166
177
  this._dataFormat.currentPrice = null;
@@ -185,7 +196,7 @@ module.exports = (() => {
185
196
  }
186
197
 
187
198
  /**
188
- * A unique (and otherwise meaningless) idenfitifer for the group.
199
+ * A unique (and otherwise meaningless) identifier for the group.
189
200
  *
190
201
  * @public
191
202
  * @returns {Number}
@@ -312,6 +323,13 @@ module.exports = (() => {
312
323
  }
313
324
  }
314
325
 
326
+ /**
327
+ * Set a flag to indicate if parent groups should exclude this group's
328
+ * items from their calculations.
329
+ *
330
+ * @public
331
+ * @param {Boolean} value
332
+ */
315
333
  setExcluded(value) {
316
334
  assert.argumentIsRequired(value, 'value', Boolean);
317
335
 
@@ -450,6 +468,10 @@ module.exports = (() => {
450
468
  updates.summaryTotalCurrent = updates.summaryTotalCurrent.add(translate(item, item.data.summaryTotalCurrent));
451
469
  updates.summaryTotalPrevious = updates.summaryTotalPrevious.add(translate(item, item.data.summaryTotalPrevious));
452
470
 
471
+ if (item.position.type === InstrumentType.CASH) {
472
+ updates.cashTotal = updates.cashTotal.add(translate(item, item.data.market));
473
+ }
474
+
453
475
  return updates;
454
476
  }, {
455
477
  basis: Decimal.ZERO,
@@ -457,7 +479,8 @@ module.exports = (() => {
457
479
  unrealized: Decimal.ZERO,
458
480
  income: Decimal.ZERO,
459
481
  summaryTotalCurrent: Decimal.ZERO,
460
- summaryTotalPrevious: Decimal.ZERO
482
+ summaryTotalPrevious: Decimal.ZERO,
483
+ cashTotal: Decimal.ZERO
461
484
  });
462
485
 
463
486
  actual.basis = updates.basis;
@@ -466,6 +489,7 @@ module.exports = (() => {
466
489
  actual.income = updates.income;
467
490
  actual.summaryTotalCurrent = updates.summaryTotalCurrent;
468
491
  actual.summaryTotalPrevious = updates.summaryTotalPrevious;
492
+ actual.cashTotal = updates.cashTotal;
469
493
 
470
494
  format.basis = formatCurrency(actual.basis, currency);
471
495
  format.realized = formatCurrency(actual.basis, currency);
@@ -474,6 +498,7 @@ module.exports = (() => {
474
498
  format.summaryTotalCurrent = formatCurrency(updates.summaryTotalCurrent, currency);
475
499
  format.summaryTotalPrevious = formatCurrency(updates.summaryTotalPrevious, currency);
476
500
  format.summaryTotalPreviousNegative = updates.summaryTotalPrevious.getIsNegative();
501
+ format.cashTotal = formatCurrency(updates.cashTotal, currency);
477
502
 
478
503
  calculateUnrealizedPercent(group);
479
504
 
@@ -545,18 +570,6 @@ module.exports = (() => {
545
570
  summaryTotalCurrent: actual.summaryTotalCurrent.add(translate(item, item.data.summaryTotalCurrentChange))
546
571
  };
547
572
  }
548
-
549
- if (parent !== null) {
550
- const parentData = parent._dataActual;
551
-
552
- if (parentData.market !== null && !parentData.market.getIsZero()) {
553
- updates.marketPercent = updates.market.divide(parentData.market);
554
- } else {
555
- updates.marketPercent = null;
556
- }
557
- } else {
558
- updates.marketPercent = null;
559
- }
560
573
 
561
574
  actual.market = updates.market;
562
575
  actual.unrealized = updates.unrealized;
@@ -593,13 +606,14 @@ module.exports = (() => {
593
606
  }
594
607
 
595
608
  const parent = group._parent;
609
+ const excluded = group._excluded;
596
610
 
597
611
  const actual = group._dataActual;
598
612
  const format = group._dataFormat;
599
613
 
600
614
  let marketPercent;
601
615
 
602
- if (parent !== null) {
616
+ if (parent !== null && !excluded) {
603
617
  const parentData = parent._dataActual;
604
618
 
605
619
  if (parentData.market !== null && !parentData.market.getIsZero()) {
@@ -35,9 +35,7 @@ module.exports = (() => {
35
35
  this._data.basis = null;
36
36
 
37
37
  this._currentQuote = null;
38
-
39
38
  this._currentPrice = null;
40
- this._previousPrice = null;
41
39
 
42
40
  this._data.currentPrice = null;
43
41
  this._data.currentPricePrevious = null;
@@ -15,10 +15,12 @@ module.exports = (() => {
15
15
  * @param {PositionLevelDefinition~descriptionSelector} descriptionSelector
16
16
  * @param {PositionLevelDefinition~currencySelector} currencySelector
17
17
  * @param {Array.<PositionLevelDefinition~RequiredGroup>=} requiredGroups
18
+ * @param {Array.<PositionLevelDefinition~RequiredGroup>=} requiredGroups
18
19
  * @param {Boolean=} single
20
+ * @param {Boolean=} aggregateCash
19
21
  */
20
22
  class PositionLevelDefinition {
21
- constructor(name, keySelector, descriptionSelector, currencySelector, requiredGroups, single) {
23
+ constructor(name, keySelector, descriptionSelector, currencySelector, requiredGroups, single, aggregateCash) {
22
24
  assert.argumentIsRequired(name, 'name', String);
23
25
  assert.argumentIsRequired(keySelector, 'keySelector', Function);
24
26
  assert.argumentIsRequired(descriptionSelector, 'descriptionSelector', Function);
@@ -29,6 +31,7 @@ module.exports = (() => {
29
31
  }
30
32
 
31
33
  assert.argumentIsOptional(single, 'single', Boolean);
34
+ assert.argumentIsOptional(aggregateCash, 'aggregateCash', Boolean);
32
35
 
33
36
  this._name = name;
34
37
 
@@ -38,6 +41,7 @@ module.exports = (() => {
38
41
 
39
42
  this._requiredGroups = requiredGroups || [ ];
40
43
  this._single = is.boolean(single) && single;
44
+ this._aggregateCash = is.boolean(aggregateCash) && aggregateCash;
41
45
  }
42
46
 
43
47
  /**
@@ -104,6 +108,16 @@ module.exports = (() => {
104
108
  return this._single;
105
109
  }
106
110
 
111
+ /**
112
+ * Indicates if the grouping level should aggregate cash positions.
113
+ *
114
+ * @public
115
+ * @returns {Boolean}
116
+ */
117
+ get aggregateCash() {
118
+ return this._aggregateCash;
119
+ }
120
+
107
121
  toString() {
108
122
  return '[PositionLevelDefinition]';
109
123
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.0.150",
3
+ "version": "1.0.154",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",
@@ -897,7 +897,7 @@ module.exports = (() => {
897
897
  const items = populatedObjects[key];
898
898
  const first = items[0];
899
899
 
900
- list.push(new PositionGroup(this, parent, items, levelDefinition.currencySelector(first), key, levelDefinition.descriptionSelector(first), levelDefinition.single && items.length === 1));
900
+ list.push(new PositionGroup(this, parent, items, levelDefinition.currencySelector(first), key, levelDefinition.descriptionSelector(first), levelDefinition.single && items.length === 1, levelDefinition.aggregateCash));
901
901
 
902
902
  return list;
903
903
  }, [ ]);
@@ -1239,7 +1239,6 @@ module.exports = (() => {
1239
1239
  }
1240
1240
  }
1241
1241
 
1242
-
1243
1242
  return PositionContainer;
1244
1243
  })();
1245
1244
 
@@ -1254,6 +1253,8 @@ const array = require('@barchart/common-js/lang/array'),
1254
1253
  is = require('@barchart/common-js/lang/is'),
1255
1254
  Rate = require('@barchart/common-js/lang/Rate');
1256
1255
 
1256
+ const InstrumentType = require('./../data/InstrumentType');
1257
+
1257
1258
  module.exports = (() => {
1258
1259
  'use strict';
1259
1260
 
@@ -1271,9 +1272,10 @@ module.exports = (() => {
1271
1272
  * @param {String} key
1272
1273
  * @param {String} description
1273
1274
  * @param {Boolean=} single
1275
+ * @param {Boolean=} aggregateCash
1274
1276
  */
1275
1277
  class PositionGroup {
1276
- constructor(container, parent, items, currency, key, description, single) {
1278
+ constructor(container, parent, items, currency, key, description, single, aggregateCash) {
1277
1279
  this._id = counter++;
1278
1280
  this._container = container;
1279
1281
  this._parent = parent || null;
@@ -1286,6 +1288,7 @@ module.exports = (() => {
1286
1288
  this._description = description;
1287
1289
 
1288
1290
  this._single = is.boolean(single) && single;
1291
+ this._aggregateCash = is.boolean(aggregateCash) && aggregateCash;
1289
1292
 
1290
1293
  this._excluded = false;
1291
1294
  this._suspended = false;
@@ -1348,6 +1351,8 @@ module.exports = (() => {
1348
1351
  this._dataFormat.quoteChangePercent = null;
1349
1352
  this._dataFormat.quoteTime = null;
1350
1353
  this._dataFormat.quoteVolume = null;
1354
+ this._dataFormat.quoteChangeDirection = unchanged;
1355
+ this._dataFormat.quoteChangeNegative = false;
1351
1356
 
1352
1357
  this._dataActual.currentPrice = null;
1353
1358
  this._dataActual.previousPrice = null;
@@ -1361,6 +1366,7 @@ module.exports = (() => {
1361
1366
  this._dataActual.total = null;
1362
1367
  this._dataActual.summaryTotalCurrent = null;
1363
1368
  this._dataActual.summaryTotalPrevious = null;
1369
+ this._dataActual.cashTotal = null;
1364
1370
 
1365
1371
  this._dataFormat.currentPrice = null;
1366
1372
  this._dataFormat.previousPrice = null;
@@ -1381,6 +1387,7 @@ module.exports = (() => {
1381
1387
  this._dataActual.summaryTotalCurrentNegative = false;
1382
1388
  this._dataFormat.summaryTotalPrevious = null;
1383
1389
  this._dataFormat.summaryTotalPreviousNegative = false;
1390
+ this._dataFormat.cashTotal = null;
1384
1391
 
1385
1392
  this._items.forEach((item) => {
1386
1393
  this._disposeStack.push(item.registerQuoteChangeHandler((quote, sender) => {
@@ -1407,6 +1414,9 @@ module.exports = (() => {
1407
1414
  this._dataFormat.quoteChangePercent = formatPercent(new Decimal(this._dataActual.quoteChangePercent || 0), 2);
1408
1415
  this._dataFormat.quoteTime = this._dataActual.quoteTime;
1409
1416
  this._dataFormat.quoteVolume = formatNumber(this._dataActual.quoteVolume, 0);
1417
+
1418
+ setTimeout(() => this._dataFormat.quoteChangeDirection = { up: this._dataActual.quoteChange.getIsPositive(), down: this._dataActual.quoteChange.getIsNegative() }, 0);
1419
+ this._dataFormat.quoteChangeNegative = this._dataActual.quoteChange.getIsNegative();
1410
1420
  } else {
1411
1421
  this._dataActual.currentPrice = null;
1412
1422
  this._dataFormat.currentPrice = null;
@@ -1431,7 +1441,7 @@ module.exports = (() => {
1431
1441
  }
1432
1442
 
1433
1443
  /**
1434
- * A unique (and otherwise meaningless) idenfitifer for the group.
1444
+ * A unique (and otherwise meaningless) identifier for the group.
1435
1445
  *
1436
1446
  * @public
1437
1447
  * @returns {Number}
@@ -1558,6 +1568,13 @@ module.exports = (() => {
1558
1568
  }
1559
1569
  }
1560
1570
 
1571
+ /**
1572
+ * Set a flag to indicate if parent groups should exclude this group's
1573
+ * items from their calculations.
1574
+ *
1575
+ * @public
1576
+ * @param {Boolean} value
1577
+ */
1561
1578
  setExcluded(value) {
1562
1579
  assert.argumentIsRequired(value, 'value', Boolean);
1563
1580
 
@@ -1696,6 +1713,10 @@ module.exports = (() => {
1696
1713
  updates.summaryTotalCurrent = updates.summaryTotalCurrent.add(translate(item, item.data.summaryTotalCurrent));
1697
1714
  updates.summaryTotalPrevious = updates.summaryTotalPrevious.add(translate(item, item.data.summaryTotalPrevious));
1698
1715
 
1716
+ if (item.position.type === InstrumentType.CASH) {
1717
+ updates.cashTotal = updates.cashTotal.add(translate(item, item.data.market));
1718
+ }
1719
+
1699
1720
  return updates;
1700
1721
  }, {
1701
1722
  basis: Decimal.ZERO,
@@ -1703,7 +1724,8 @@ module.exports = (() => {
1703
1724
  unrealized: Decimal.ZERO,
1704
1725
  income: Decimal.ZERO,
1705
1726
  summaryTotalCurrent: Decimal.ZERO,
1706
- summaryTotalPrevious: Decimal.ZERO
1727
+ summaryTotalPrevious: Decimal.ZERO,
1728
+ cashTotal: Decimal.ZERO
1707
1729
  });
1708
1730
 
1709
1731
  actual.basis = updates.basis;
@@ -1712,6 +1734,7 @@ module.exports = (() => {
1712
1734
  actual.income = updates.income;
1713
1735
  actual.summaryTotalCurrent = updates.summaryTotalCurrent;
1714
1736
  actual.summaryTotalPrevious = updates.summaryTotalPrevious;
1737
+ actual.cashTotal = updates.cashTotal;
1715
1738
 
1716
1739
  format.basis = formatCurrency(actual.basis, currency);
1717
1740
  format.realized = formatCurrency(actual.basis, currency);
@@ -1720,6 +1743,7 @@ module.exports = (() => {
1720
1743
  format.summaryTotalCurrent = formatCurrency(updates.summaryTotalCurrent, currency);
1721
1744
  format.summaryTotalPrevious = formatCurrency(updates.summaryTotalPrevious, currency);
1722
1745
  format.summaryTotalPreviousNegative = updates.summaryTotalPrevious.getIsNegative();
1746
+ format.cashTotal = formatCurrency(updates.cashTotal, currency);
1723
1747
 
1724
1748
  calculateUnrealizedPercent(group);
1725
1749
 
@@ -1791,18 +1815,6 @@ module.exports = (() => {
1791
1815
  summaryTotalCurrent: actual.summaryTotalCurrent.add(translate(item, item.data.summaryTotalCurrentChange))
1792
1816
  };
1793
1817
  }
1794
-
1795
- if (parent !== null) {
1796
- const parentData = parent._dataActual;
1797
-
1798
- if (parentData.market !== null && !parentData.market.getIsZero()) {
1799
- updates.marketPercent = updates.market.divide(parentData.market);
1800
- } else {
1801
- updates.marketPercent = null;
1802
- }
1803
- } else {
1804
- updates.marketPercent = null;
1805
- }
1806
1818
 
1807
1819
  actual.market = updates.market;
1808
1820
  actual.unrealized = updates.unrealized;
@@ -1839,13 +1851,14 @@ module.exports = (() => {
1839
1851
  }
1840
1852
 
1841
1853
  const parent = group._parent;
1854
+ const excluded = group._excluded;
1842
1855
 
1843
1856
  const actual = group._dataActual;
1844
1857
  const format = group._dataFormat;
1845
1858
 
1846
1859
  let marketPercent;
1847
1860
 
1848
- if (parent !== null) {
1861
+ if (parent !== null && !excluded) {
1849
1862
  const parentData = parent._dataActual;
1850
1863
 
1851
1864
  if (parentData.market !== null && !parentData.market.getIsZero()) {
@@ -1892,7 +1905,7 @@ module.exports = (() => {
1892
1905
  return PositionGroup;
1893
1906
  })();
1894
1907
 
1895
- },{"@barchart/common-js/collections/specialized/DisposableStack":13,"@barchart/common-js/lang/Currency":14,"@barchart/common-js/lang/Decimal":16,"@barchart/common-js/lang/Rate":19,"@barchart/common-js/lang/array":20,"@barchart/common-js/lang/assert":21,"@barchart/common-js/lang/formatter":22,"@barchart/common-js/lang/is":23,"@barchart/common-js/messaging/Event":25}],6:[function(require,module,exports){
1908
+ },{"./../data/InstrumentType":1,"@barchart/common-js/collections/specialized/DisposableStack":13,"@barchart/common-js/lang/Currency":14,"@barchart/common-js/lang/Decimal":16,"@barchart/common-js/lang/Rate":19,"@barchart/common-js/lang/array":20,"@barchart/common-js/lang/assert":21,"@barchart/common-js/lang/formatter":22,"@barchart/common-js/lang/is":23,"@barchart/common-js/messaging/Event":25}],6:[function(require,module,exports){
1896
1909
  const array = require('@barchart/common-js/lang/array'),
1897
1910
  assert = require('@barchart/common-js/lang/assert'),
1898
1911
  Currency = require('@barchart/common-js/lang/Currency'),
@@ -1930,9 +1943,7 @@ module.exports = (() => {
1930
1943
  this._data.basis = null;
1931
1944
 
1932
1945
  this._currentQuote = null;
1933
-
1934
1946
  this._currentPrice = null;
1935
- this._previousPrice = null;
1936
1947
 
1937
1948
  this._data.currentPrice = null;
1938
1949
  this._data.currentPricePrevious = null;
@@ -2279,10 +2290,12 @@ module.exports = (() => {
2279
2290
  * @param {PositionLevelDefinition~descriptionSelector} descriptionSelector
2280
2291
  * @param {PositionLevelDefinition~currencySelector} currencySelector
2281
2292
  * @param {Array.<PositionLevelDefinition~RequiredGroup>=} requiredGroups
2293
+ * @param {Array.<PositionLevelDefinition~RequiredGroup>=} requiredGroups
2282
2294
  * @param {Boolean=} single
2295
+ * @param {Boolean=} aggregateCash
2283
2296
  */
2284
2297
  class PositionLevelDefinition {
2285
- constructor(name, keySelector, descriptionSelector, currencySelector, requiredGroups, single) {
2298
+ constructor(name, keySelector, descriptionSelector, currencySelector, requiredGroups, single, aggregateCash) {
2286
2299
  assert.argumentIsRequired(name, 'name', String);
2287
2300
  assert.argumentIsRequired(keySelector, 'keySelector', Function);
2288
2301
  assert.argumentIsRequired(descriptionSelector, 'descriptionSelector', Function);
@@ -2293,6 +2306,7 @@ module.exports = (() => {
2293
2306
  }
2294
2307
 
2295
2308
  assert.argumentIsOptional(single, 'single', Boolean);
2309
+ assert.argumentIsOptional(aggregateCash, 'aggregateCash', Boolean);
2296
2310
 
2297
2311
  this._name = name;
2298
2312
 
@@ -2302,6 +2316,7 @@ module.exports = (() => {
2302
2316
 
2303
2317
  this._requiredGroups = requiredGroups || [ ];
2304
2318
  this._single = is.boolean(single) && single;
2319
+ this._aggregateCash = is.boolean(aggregateCash) && aggregateCash;
2305
2320
  }
2306
2321
 
2307
2322
  /**
@@ -2368,6 +2383,16 @@ module.exports = (() => {
2368
2383
  return this._single;
2369
2384
  }
2370
2385
 
2386
+ /**
2387
+ * Indicates if the grouping level should aggregate cash positions.
2388
+ *
2389
+ * @public
2390
+ * @returns {Boolean}
2391
+ */
2392
+ get aggregateCash() {
2393
+ return this._aggregateCash;
2394
+ }
2395
+
2371
2396
  toString() {
2372
2397
  return '[PositionLevelDefinition]';
2373
2398
  }