@barchart/portfolio-api-common 1.0.172 → 1.0.176

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.
@@ -44,12 +44,6 @@ module.exports = (() => {
44
44
  assert.argumentIsArray(positions, 'positions');
45
45
  assert.argumentIsArray(summaries, 'summaries');
46
46
 
47
- const previousSummaryFrame = PositionSummaryFrame.YEARLY;
48
- const previousSummaryRanges = previousSummaryFrame.getRecentRanges(0);
49
-
50
- const currentSummaryFrame = PositionSummaryFrame.YTD;
51
- const currentSummaryRange = array.last(currentSummaryFrame.getRecentRanges(0));
52
-
53
47
  this._definitions = definitions;
54
48
 
55
49
  this._groupBindings = { };
@@ -60,71 +54,42 @@ module.exports = (() => {
60
54
  return map;
61
55
  }, { });
62
56
 
63
- this._summariesCurrent = summaries.reduce((map, summary) => {
64
- if (summary.frame === currentSummaryFrame && currentSummaryRange.start.getIsEqual(summary.start.date) && currentSummaryRange.end.getIsEqual(summary.end.date)) {
65
- const key = summary.position;
57
+ this._currentSummaryFrame = PositionSummaryFrame.YTD;
58
+ this._currentSummaryRange = array.last(this._currentSummaryFrame.getRecentRanges(0));
66
59
 
67
- map[key] = summary;
68
- }
60
+ this._summariesCurrent = summaries.reduce((map, summary) => {
61
+ addSummaryCurrent(map, summary, this._currentSummaryFrame, this._currentSummaryRange);
69
62
 
70
63
  return map;
71
64
  }, { });
72
-
73
- this._summariesPrevious = summaries.reduce((map, summary) => {
74
- if (summary.frame === previousSummaryFrame) {
75
- const key = summary.position;
76
65
 
77
- if (!map.hasOwnProperty(key)) {
78
- map[key] = getSummaryArray(previousSummaryRanges);
79
- }
66
+ this._previousSummaryFrame = PositionSummaryFrame.YEARLY;
67
+ this._previousSummaryRanges = this._previousSummaryFrame.getRecentRanges(0);
80
68
 
81
- const index = previousSummaryRanges.findIndex(r => r.start.getIsEqual(summary.start.date) && r.end.getIsEqual(summary.end.date));
82
-
83
- if (!(index < 0)) {
84
- map[key][index] = summary;
85
- }
86
- }
69
+ this._summariesPrevious = summaries.reduce((map, summary) => {
70
+ addSummaryPrevious(map, summary, this._previousSummaryFrame, this._previousSummaryRanges);
87
71
 
88
72
  return map;
89
73
  }, { });
90
74
 
91
75
  this._items = positions.reduce((items, position) => {
92
- const portfolio = this._portfolios[position.portfolio];
93
-
94
- if (position) {
95
- const currentSummary = this._summariesCurrent[position.position] || null;
96
- const previousSummaries = this._summariesPrevious[position.position] || getSummaryArray(previousSummaryRanges);
76
+ const item = createPositionItem.call(this, position);
97
77
 
98
- items.push(new PositionItem(portfolio, position, currentSummary, previousSummaries));
78
+ if (item) {
79
+ items.push(item);
99
80
  }
100
81
 
101
82
  return items;
102
83
  }, [ ]);
103
84
 
104
85
  this._symbols = this._items.reduce((map, item) => {
105
- const symbol = extractSymbolForBarchart(item.position);
106
-
107
- if (symbol) {
108
- if (!map.hasOwnProperty(symbol)) {
109
- map[symbol] = [ ];
110
- }
111
-
112
- map[symbol].push(item);
113
- }
86
+ addBarchartSymbol(map, item);
114
87
 
115
88
  return map;
116
89
  }, { });
117
90
 
118
91
  this._symbolsDisplay = this._items.reduce((map, item) => {
119
- const symbol = extractSymbolForDisplay(item.position);
120
-
121
- if (symbol) {
122
- if (!map.hasOwnProperty(symbol)) {
123
- map[symbol] = [ ];
124
- }
125
-
126
- map[symbol].push(item);
127
- }
92
+ addDisplaySymbol(map, item);
128
93
 
129
94
  return map;
130
95
  }, { });
@@ -257,12 +222,40 @@ module.exports = (() => {
257
222
  });
258
223
  }
259
224
 
260
- mutatePosition(position, summary) {
225
+ /**
226
+ * Adds a new position to the container or updates an existing position already
227
+ * in the container.
228
+ *
229
+ * @public
230
+ * @param {Object} position
231
+ * @param {Array.<Object>} summaries
232
+ */
233
+ updatePosition(position, summaries) {
234
+ assert.argumentIsRequired(position, 'position', Object);
235
+ assert.argumentIsRequired(position.position, 'position.position', String);
236
+ assert.argumentIsRequired(position.portfolio, 'position.portfolio', String);
237
+ assert.argumentIsArray(summaries, 'summaries');
238
+
239
+ if (!this._portfolios.hasOwnProperty(position.portfolio)) {
240
+ return;
241
+ }
261
242
 
243
+ this.startTransaction(() => {
244
+ this.removePosition(position);
245
+ });
262
246
  }
263
247
 
248
+ /**
249
+ * Removes a single position from the container.
250
+ *
251
+ * @public
252
+ * @param {Object} position
253
+ */
264
254
  removePosition(position) {
265
- removePositionItem.call(this, this._items.find((item) => item.position.position === position));
255
+ assert.argumentIsRequired(position, 'position', Object);
256
+ assert.argumentIsRequired(position.position, 'position.position', String);
257
+
258
+ removePositionItem.call(this, this._items.find((item) => item.position.position === position.position));
266
259
  }
267
260
 
268
261
  /**
@@ -503,10 +496,6 @@ module.exports = (() => {
503
496
  return keys.reduce((tree, key) => tree.findChild(group => group.key === key), tree);
504
497
  }
505
498
 
506
- function getSummaryArray(ranges) {
507
- return ranges.map(range => null);
508
- }
509
-
510
499
  function extractSymbolForBarchart(position) {
511
500
  if (position.instrument && position.instrument.symbol && position.instrument.symbol.barchart) {
512
501
  return position.instrument.symbol.barchart;
@@ -670,6 +659,75 @@ module.exports = (() => {
670
659
  }, [ ]);
671
660
  }
672
661
 
662
+ function getSummaryArray(ranges) {
663
+ return ranges.map(range => null);
664
+ }
665
+
666
+ function addSummaryCurrent(map, summary, currentSummaryFrame, currentSummaryRange) {
667
+ if (summary.frame === currentSummaryFrame && currentSummaryRange.start.getIsEqual(summary.start.date) && currentSummaryRange.end.getIsEqual(summary.end.date)) {
668
+ const key = summary.position;
669
+
670
+ map[key] = summary;
671
+ }
672
+ }
673
+
674
+ function addSummaryPrevious(map, summary, previousSummaryFrame, previousSummaryRanges) {
675
+ if (summary.frame === previousSummaryFrame) {
676
+ const key = summary.position;
677
+
678
+ if (!map.hasOwnProperty(key)) {
679
+ map[key] = getSummaryArray(previousSummaryRanges);
680
+ }
681
+
682
+ const index = previousSummaryRanges.findIndex(r => r.start.getIsEqual(summary.start.date) && r.end.getIsEqual(summary.end.date));
683
+
684
+ if (!(index < 0)) {
685
+ map[key][index] = summary;
686
+ }
687
+ }
688
+ }
689
+
690
+ function addBarchartSymbol(map, item) {
691
+ const barchartSymbol = extractSymbolForBarchart(item.position);
692
+
693
+ if (barchartSymbol) {
694
+ if (!map.hasOwnProperty(barchartSymbol)) {
695
+ map[barchartSymbol] = [ ];
696
+ }
697
+
698
+ map[barchartSymbol].push(item);
699
+ }
700
+ }
701
+
702
+ function addDisplaySymbol(map, item) {
703
+ const displaySymbol = extractSymbolForDisplay(item.position);
704
+
705
+ if (displaySymbol) {
706
+ if (!map.hasOwnProperty(displaySymbol)) {
707
+ map[displaySymbol] = [ ];
708
+ }
709
+
710
+ map[displaySymbol].push(item);
711
+ }
712
+ }
713
+
714
+ function createPositionItem(position) {
715
+ const portfolio = this._portfolios[position.portfolio];
716
+
717
+ let returnRef;
718
+
719
+ if (portfolio) {
720
+ const currentSummary = this._summariesCurrent[ position.position ] || null;
721
+ const previousSummaries = this._summariesPrevious[ position.position ] || getSummaryArray(this._previousSummaryRanges);
722
+
723
+ returnRef = new PositionItem(portfolio, position, currentSummary, previousSummaries);
724
+ } else {
725
+ returnRef = null;
726
+ }
727
+
728
+ return returnRef;
729
+ }
730
+
673
731
  function removePositionItem(positionItem) {
674
732
  if (!positionItem) {
675
733
  return;
@@ -698,6 +756,12 @@ module.exports = (() => {
698
756
  array.remove(this._currencies[currency.code], i => i === positionItem);
699
757
  }
700
758
 
759
+ this._trees[key].walk((group, groupNode) => {
760
+ if (group.definition.type === PositionLevelType.POSITION && group.key === positionItem.position.position) {
761
+ groupNode.sever();
762
+ }
763
+ }, true, false);
764
+
701
765
  positionItem.dispose();
702
766
  }
703
767
 
@@ -627,7 +627,7 @@ module.exports = (() => {
627
627
 
628
628
  updates = items.reduce((updates, item) => {
629
629
  updates.market = updates.market.add(translate(item, item.data.market));
630
- updates.marketAbsolute = updates.marketAbsolute.add(translate(item, item.data.market));
630
+ updates.marketAbsolute = updates.marketAbsolute.add(translate(item, item.data.marketAbsolute));
631
631
  updates.unrealized = updates.unrealized.add(translate(item, item.data.unrealized));
632
632
  updates.unrealizedToday = updates.unrealizedToday.add(translate(item, item.data.unrealizedToday));
633
633
  updates.summaryTotalCurrent = updates.summaryTotalCurrent.add(translate(item, item.data.summaryTotalCurrent));
@@ -702,7 +702,7 @@ module.exports = (() => {
702
702
  let numerator;
703
703
 
704
704
  if (group.currency !== parent.currency) {
705
- numerator = Rate.convert(actual.market, group.currency, parent.currency, ...rates);
705
+ numerator = Rate.convert(actual.marketAbsolute, group.currency, parent.currency, ...rates);
706
706
  } else {
707
707
  numerator = actual.marketAbsolute;
708
708
  }
@@ -341,8 +341,8 @@ module.exports = (() => {
341
341
  marketAbsoluteChange = marketAbsolute.subtract(data.marketAbsolute);
342
342
  }
343
343
 
344
- data.marketAbsolute = market;
345
- data.marketAbsoluteChange = marketChange;
344
+ data.marketAbsolute = marketAbsolute;
345
+ data.marketAbsoluteChange = marketAbsoluteChange;
346
346
 
347
347
  let unrealizedToday;
348
348
  let unrealizedTodayChange;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.0.172",
3
+ "version": "1.0.176",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",
@@ -760,12 +760,6 @@ module.exports = (() => {
760
760
  assert.argumentIsArray(positions, 'positions');
761
761
  assert.argumentIsArray(summaries, 'summaries');
762
762
 
763
- const previousSummaryFrame = PositionSummaryFrame.YEARLY;
764
- const previousSummaryRanges = previousSummaryFrame.getRecentRanges(0);
765
-
766
- const currentSummaryFrame = PositionSummaryFrame.YTD;
767
- const currentSummaryRange = array.last(currentSummaryFrame.getRecentRanges(0));
768
-
769
763
  this._definitions = definitions;
770
764
 
771
765
  this._groupBindings = { };
@@ -776,71 +770,42 @@ module.exports = (() => {
776
770
  return map;
777
771
  }, { });
778
772
 
779
- this._summariesCurrent = summaries.reduce((map, summary) => {
780
- if (summary.frame === currentSummaryFrame && currentSummaryRange.start.getIsEqual(summary.start.date) && currentSummaryRange.end.getIsEqual(summary.end.date)) {
781
- const key = summary.position;
773
+ this._currentSummaryFrame = PositionSummaryFrame.YTD;
774
+ this._currentSummaryRange = array.last(this._currentSummaryFrame.getRecentRanges(0));
782
775
 
783
- map[key] = summary;
784
- }
776
+ this._summariesCurrent = summaries.reduce((map, summary) => {
777
+ addSummaryCurrent(map, summary, this._currentSummaryFrame, this._currentSummaryRange);
785
778
 
786
779
  return map;
787
780
  }, { });
788
-
789
- this._summariesPrevious = summaries.reduce((map, summary) => {
790
- if (summary.frame === previousSummaryFrame) {
791
- const key = summary.position;
792
-
793
- if (!map.hasOwnProperty(key)) {
794
- map[key] = getSummaryArray(previousSummaryRanges);
795
- }
796
781
 
797
- const index = previousSummaryRanges.findIndex(r => r.start.getIsEqual(summary.start.date) && r.end.getIsEqual(summary.end.date));
782
+ this._previousSummaryFrame = PositionSummaryFrame.YEARLY;
783
+ this._previousSummaryRanges = this._previousSummaryFrame.getRecentRanges(0);
798
784
 
799
- if (!(index < 0)) {
800
- map[key][index] = summary;
801
- }
802
- }
785
+ this._summariesPrevious = summaries.reduce((map, summary) => {
786
+ addSummaryPrevious(map, summary, this._previousSummaryFrame, this._previousSummaryRanges);
803
787
 
804
788
  return map;
805
789
  }, { });
806
790
 
807
791
  this._items = positions.reduce((items, position) => {
808
- const portfolio = this._portfolios[position.portfolio];
809
-
810
- if (position) {
811
- const currentSummary = this._summariesCurrent[position.position] || null;
812
- const previousSummaries = this._summariesPrevious[position.position] || getSummaryArray(previousSummaryRanges);
792
+ const item = createPositionItem.call(this, position);
813
793
 
814
- items.push(new PositionItem(portfolio, position, currentSummary, previousSummaries));
794
+ if (item) {
795
+ items.push(item);
815
796
  }
816
797
 
817
798
  return items;
818
799
  }, [ ]);
819
800
 
820
801
  this._symbols = this._items.reduce((map, item) => {
821
- const symbol = extractSymbolForBarchart(item.position);
822
-
823
- if (symbol) {
824
- if (!map.hasOwnProperty(symbol)) {
825
- map[symbol] = [ ];
826
- }
827
-
828
- map[symbol].push(item);
829
- }
802
+ addBarchartSymbol(map, item);
830
803
 
831
804
  return map;
832
805
  }, { });
833
806
 
834
807
  this._symbolsDisplay = this._items.reduce((map, item) => {
835
- const symbol = extractSymbolForDisplay(item.position);
836
-
837
- if (symbol) {
838
- if (!map.hasOwnProperty(symbol)) {
839
- map[symbol] = [ ];
840
- }
841
-
842
- map[symbol].push(item);
843
- }
808
+ addDisplaySymbol(map, item);
844
809
 
845
810
  return map;
846
811
  }, { });
@@ -973,12 +938,40 @@ module.exports = (() => {
973
938
  });
974
939
  }
975
940
 
976
- mutatePosition(position, summary) {
941
+ /**
942
+ * Adds a new position to the container or updates an existing position already
943
+ * in the container.
944
+ *
945
+ * @public
946
+ * @param {Object} position
947
+ * @param {Array.<Object>} summaries
948
+ */
949
+ updatePosition(position, summaries) {
950
+ assert.argumentIsRequired(position, 'position', Object);
951
+ assert.argumentIsRequired(position.position, 'position.position', String);
952
+ assert.argumentIsRequired(position.portfolio, 'position.portfolio', String);
953
+ assert.argumentIsArray(summaries, 'summaries');
977
954
 
955
+ if (!this._portfolios.hasOwnProperty(position.portfolio)) {
956
+ return;
957
+ }
958
+
959
+ this.startTransaction(() => {
960
+ this.removePosition(position);
961
+ });
978
962
  }
979
963
 
964
+ /**
965
+ * Removes a single position from the container.
966
+ *
967
+ * @public
968
+ * @param {Object} position
969
+ */
980
970
  removePosition(position) {
981
- removePositionItem.call(this, this._items.find((item) => item.position.position === position));
971
+ assert.argumentIsRequired(position, 'position', Object);
972
+ assert.argumentIsRequired(position.position, 'position.position', String);
973
+
974
+ removePositionItem.call(this, this._items.find((item) => item.position.position === position.position));
982
975
  }
983
976
 
984
977
  /**
@@ -1219,10 +1212,6 @@ module.exports = (() => {
1219
1212
  return keys.reduce((tree, key) => tree.findChild(group => group.key === key), tree);
1220
1213
  }
1221
1214
 
1222
- function getSummaryArray(ranges) {
1223
- return ranges.map(range => null);
1224
- }
1225
-
1226
1215
  function extractSymbolForBarchart(position) {
1227
1216
  if (position.instrument && position.instrument.symbol && position.instrument.symbol.barchart) {
1228
1217
  return position.instrument.symbol.barchart;
@@ -1386,6 +1375,75 @@ module.exports = (() => {
1386
1375
  }, [ ]);
1387
1376
  }
1388
1377
 
1378
+ function getSummaryArray(ranges) {
1379
+ return ranges.map(range => null);
1380
+ }
1381
+
1382
+ function addSummaryCurrent(map, summary, currentSummaryFrame, currentSummaryRange) {
1383
+ if (summary.frame === currentSummaryFrame && currentSummaryRange.start.getIsEqual(summary.start.date) && currentSummaryRange.end.getIsEqual(summary.end.date)) {
1384
+ const key = summary.position;
1385
+
1386
+ map[key] = summary;
1387
+ }
1388
+ }
1389
+
1390
+ function addSummaryPrevious(map, summary, previousSummaryFrame, previousSummaryRanges) {
1391
+ if (summary.frame === previousSummaryFrame) {
1392
+ const key = summary.position;
1393
+
1394
+ if (!map.hasOwnProperty(key)) {
1395
+ map[key] = getSummaryArray(previousSummaryRanges);
1396
+ }
1397
+
1398
+ const index = previousSummaryRanges.findIndex(r => r.start.getIsEqual(summary.start.date) && r.end.getIsEqual(summary.end.date));
1399
+
1400
+ if (!(index < 0)) {
1401
+ map[key][index] = summary;
1402
+ }
1403
+ }
1404
+ }
1405
+
1406
+ function addBarchartSymbol(map, item) {
1407
+ const barchartSymbol = extractSymbolForBarchart(item.position);
1408
+
1409
+ if (barchartSymbol) {
1410
+ if (!map.hasOwnProperty(barchartSymbol)) {
1411
+ map[barchartSymbol] = [ ];
1412
+ }
1413
+
1414
+ map[barchartSymbol].push(item);
1415
+ }
1416
+ }
1417
+
1418
+ function addDisplaySymbol(map, item) {
1419
+ const displaySymbol = extractSymbolForDisplay(item.position);
1420
+
1421
+ if (displaySymbol) {
1422
+ if (!map.hasOwnProperty(displaySymbol)) {
1423
+ map[displaySymbol] = [ ];
1424
+ }
1425
+
1426
+ map[displaySymbol].push(item);
1427
+ }
1428
+ }
1429
+
1430
+ function createPositionItem(position) {
1431
+ const portfolio = this._portfolios[position.portfolio];
1432
+
1433
+ let returnRef;
1434
+
1435
+ if (portfolio) {
1436
+ const currentSummary = this._summariesCurrent[ position.position ] || null;
1437
+ const previousSummaries = this._summariesPrevious[ position.position ] || getSummaryArray(this._previousSummaryRanges);
1438
+
1439
+ returnRef = new PositionItem(portfolio, position, currentSummary, previousSummaries);
1440
+ } else {
1441
+ returnRef = null;
1442
+ }
1443
+
1444
+ return returnRef;
1445
+ }
1446
+
1389
1447
  function removePositionItem(positionItem) {
1390
1448
  if (!positionItem) {
1391
1449
  return;
@@ -1414,6 +1472,12 @@ module.exports = (() => {
1414
1472
  array.remove(this._currencies[currency.code], i => i === positionItem);
1415
1473
  }
1416
1474
 
1475
+ this._trees[key].walk((group, groupNode) => {
1476
+ if (group.definition.type === PositionLevelType.POSITION && group.key === positionItem.position.position) {
1477
+ groupNode.sever();
1478
+ }
1479
+ }, true, false);
1480
+
1417
1481
  positionItem.dispose();
1418
1482
  }
1419
1483
 
@@ -2050,7 +2114,7 @@ module.exports = (() => {
2050
2114
 
2051
2115
  updates = items.reduce((updates, item) => {
2052
2116
  updates.market = updates.market.add(translate(item, item.data.market));
2053
- updates.marketAbsolute = updates.marketAbsolute.add(translate(item, item.data.market));
2117
+ updates.marketAbsolute = updates.marketAbsolute.add(translate(item, item.data.marketAbsolute));
2054
2118
  updates.unrealized = updates.unrealized.add(translate(item, item.data.unrealized));
2055
2119
  updates.unrealizedToday = updates.unrealizedToday.add(translate(item, item.data.unrealizedToday));
2056
2120
  updates.summaryTotalCurrent = updates.summaryTotalCurrent.add(translate(item, item.data.summaryTotalCurrent));
@@ -2125,7 +2189,7 @@ module.exports = (() => {
2125
2189
  let numerator;
2126
2190
 
2127
2191
  if (group.currency !== parent.currency) {
2128
- numerator = Rate.convert(actual.market, group.currency, parent.currency, ...rates);
2192
+ numerator = Rate.convert(actual.marketAbsolute, group.currency, parent.currency, ...rates);
2129
2193
  } else {
2130
2194
  numerator = actual.marketAbsolute;
2131
2195
  }
@@ -2509,8 +2573,8 @@ module.exports = (() => {
2509
2573
  marketAbsoluteChange = marketAbsolute.subtract(data.marketAbsolute);
2510
2574
  }
2511
2575
 
2512
- data.marketAbsolute = market;
2513
- data.marketAbsoluteChange = marketChange;
2576
+ data.marketAbsolute = marketAbsolute;
2577
+ data.marketAbsoluteChange = marketAbsoluteChange;
2514
2578
 
2515
2579
  let unrealizedToday;
2516
2580
  let unrealizedTodayChange;