@barchart/portfolio-api-common 1.2.95 → 1.2.99

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.
@@ -42,20 +42,27 @@ module.exports = (() => {
42
42
  * @param {Array.<Object>} portfolios - The portfolios.
43
43
  * @param {Array.<Object>} positions - The positions (for all of the portfolios).
44
44
  * @param {Array.<Object>} summaries - The positions summaries (for all of the positions).
45
- * @param {PositionSummaryFrame=} - If specified, locks the current (and previous) periods to a specific frame, use for reporting.
45
+ * @param {PositionSummaryFrame=} reportFrame - If specified, locks the current (and previous) periods to a specific frame, use for reporting.
46
+ * @param {Day=} reportDate - The end date for the report frame.
46
47
  */
47
48
  class PositionContainer {
48
- constructor(definitions, portfolios, positions, summaries, frame) {
49
+ constructor(definitions, portfolios, positions, summaries, reportFrame, reportDate) {
49
50
  assert.argumentIsArray(definitions, 'definitions', PositionTreeDefinition, 'PositionTreeDefinition');
50
51
  assert.argumentIsArray(portfolios, 'portfolios');
51
52
  assert.argumentIsArray(positions, 'positions');
52
53
  assert.argumentIsArray(summaries, 'summaries');
53
- assert.argumentIsOptional(frame, 'frame', PositionSummaryFrame, 'PositionSummaryFrame');
54
+ assert.argumentIsOptional(reportFrame, 'reportFrame', PositionSummaryFrame, 'PositionSummaryFrame');
55
+
56
+ if (reportFrame) {
57
+ assert.argumentIsRequired(reportDate, 'reportDate', Day, 'Day');
58
+ }
54
59
 
55
60
  this._definitions = definitions;
56
61
 
57
62
  this._groupBindings = { };
58
63
 
64
+ this._reporting = reportFrame instanceof PositionSummaryFrame;
65
+
59
66
  this._positionSymbolAddedEvent = new Event(this);
60
67
  this._positionSymbolRemovedEvent = new Event(this);
61
68
 
@@ -65,15 +72,21 @@ module.exports = (() => {
65
72
  return map;
66
73
  }, { });
67
74
 
68
- this._currentSummaryFrame = frame || PositionSummaryFrame.YTD;
69
- this._currentSummaryRange = array.last(this._currentSummaryFrame.getRecentRanges(0));
75
+ if (reportFrame) {
76
+ this._currentSummaryFrame = reportFrame;
77
+ this._currentSummaryRange = array.last(this._currentSummaryFrame.getPriorRanges(reportDate, 0));
70
78
 
71
- this._previousSummaryFrame = frame || PositionSummaryFrame.YEARLY;
72
- this._previousSummaryRanges = this._previousSummaryFrame.getRecentRanges(3);
79
+ this._previousSummaryFrame = reportFrame;
80
+ this._previousSummaryRanges = this._currentSummaryFrame.getPriorRanges(reportDate, 3);
73
81
 
74
- if (this._currentSummaryFrame === this._previousSummaryFrame) {
75
82
  this._previousSummaryRanges.pop();
76
83
  } else {
84
+ this._currentSummaryFrame = PositionSummaryFrame.YTD;
85
+ this._currentSummaryRange = array.first(this._currentSummaryFrame.getRecentRanges(0));
86
+
87
+ this._previousSummaryFrame = PositionSummaryFrame.YEARLY;
88
+ this._previousSummaryRanges = this._previousSummaryFrame.getRecentRanges(3);
89
+
77
90
  this._previousSummaryRanges.shift();
78
91
  }
79
92
 
@@ -942,7 +955,7 @@ module.exports = (() => {
942
955
  const currentSummary = this._summariesCurrent[ position.position ] || null;
943
956
  const previousSummaries = this._summariesPrevious[ position.position ] || getSummaryArray(this._previousSummaryRanges);
944
957
 
945
- returnRef = new PositionItem(portfolio, position, currentSummary, previousSummaries);
958
+ returnRef = new PositionItem(portfolio, position, currentSummary, previousSummaries, this._reporting);
946
959
  } else {
947
960
  returnRef = null;
948
961
  }
@@ -20,9 +20,10 @@ module.exports = (() => {
20
20
  * @param {Object} position
21
21
  * @param {Object} currentSummary
22
22
  * @param {Array.<Object>} previousSummaries
23
+ * @param {Boolean} reporting
23
24
  */
24
25
  class PositionItem extends Disposable {
25
- constructor(portfolio, position, currentSummary, previousSummaries) {
26
+ constructor(portfolio, position, currentSummary, previousSummaries, reporting) {
26
27
  super();
27
28
 
28
29
  this._portfolio = portfolio;
@@ -36,6 +37,8 @@ module.exports = (() => {
36
37
  this._currentSummary = currentSummary || null;
37
38
  this._previousSummaries = previousSummaries || [ ];
38
39
 
40
+ this._reporting = reporting;
41
+
39
42
  this._data = { };
40
43
 
41
44
  this._data.basis = null;
@@ -364,7 +367,7 @@ module.exports = (() => {
364
367
 
365
368
  function calculateStaticData(item) {
366
369
  const position = item.position;
367
- const snapshot = item.position.snapshot;
370
+ const snapshot = getSnapshot(position, item.currentSummary, item._reporting);
368
371
  const previousSummaries = item.previousSummaries;
369
372
 
370
373
  const data = item._data;
@@ -408,7 +411,7 @@ module.exports = (() => {
408
411
 
409
412
  function calculatePriceData(item, price) {
410
413
  const position = item.position;
411
- const snapshot = item.position.snapshot;
414
+ const snapshot = getSnapshot(position, item.currentSummary, item._reporting);
412
415
  const previousSummaries = item.previousSummaries;
413
416
 
414
417
  const data = item._data;
@@ -560,5 +563,27 @@ module.exports = (() => {
560
563
  return is.object(position.system) && is.boolean(position.system.locked) && position.system.locked;
561
564
  }
562
565
 
566
+ function getSnapshot(position, currentSummary, reporting) {
567
+ let snapshot;
568
+
569
+ if (reporting) {
570
+ snapshot = { };
571
+
572
+ snapshot.date = currentSummary.end.date;
573
+ snapshot.open = currentSummary.end.open;
574
+ snapshot.direction = currentSummary.end.direction;
575
+ snapshot.buys = currentSummary.period.buys;
576
+ snapshot.sells = currentSummary.period.sells;
577
+ snapshot.gain = currentSummary.period.realized;
578
+ snapshot.basis = currentSummary.end.basis;
579
+ snapshot.income = currentSummary.period.income;
580
+ snapshot.value = currentSummary.end.value;
581
+ } else {
582
+ snapshot = position.snapshot;
583
+ }
584
+
585
+ return snapshot;
586
+ }
587
+
563
588
  return PositionItem;
564
589
  })();
@@ -3,18 +3,18 @@ const DataType = require('@barchart/common-js/serialization/json/DataType'),
3
3
  Schema = require('@barchart/common-js/serialization/json/Schema'),
4
4
  SchemaBuilder = require('@barchart/common-js/serialization/json/builders/SchemaBuilder');
5
5
 
6
- const PositionSummaryFrame = require('./../data/PositionSummaryFrame');
6
+ const PositionSummaryFrame = require('./../../data/PositionSummaryFrame');
7
7
 
8
8
  module.exports = (() => {
9
9
  'use strict';
10
10
 
11
11
  /**
12
- * The schemas which can be used to represent position summary objects.
12
+ * Schema used to represent availability of a brokerage report.
13
13
  *
14
14
  * @public
15
15
  * @extends {Enum}
16
16
  */
17
- class PositionSummaryDefinitionSchema extends Enum {
17
+ class BrokerageReportAvailabilitySchema extends Enum {
18
18
  constructor(schema) {
19
19
  super(schema.name, schema.name);
20
20
 
@@ -36,23 +36,23 @@ module.exports = (() => {
36
36
  *
37
37
  * @static
38
38
  * @public
39
- * @returns {PositionSummaryDefinitionSchema}
39
+ * @returns {BrokerageReportAvailabilitySchema}
40
40
  */
41
41
  static get COMPLETE() {
42
42
  return complete;
43
43
  }
44
44
 
45
45
  toString() {
46
- return '[PositionSummaryDefinitionSchema]';
46
+ return '[BrokerageReportAvailabilitySchema]';
47
47
  }
48
48
  }
49
49
 
50
- const complete = new PositionSummaryDefinitionSchema(SchemaBuilder.withName('complete')
50
+ const complete = new BrokerageReportAvailabilitySchema(SchemaBuilder.withName('complete')
51
51
  .withField('start', DataType.DAY)
52
52
  .withField('end', DataType.DAY)
53
53
  .withField('frame', DataType.forEnum(PositionSummaryFrame, 'PositionSummaryFrame'))
54
54
  .schema
55
55
  );
56
56
 
57
- return PositionSummaryDefinitionSchema;
57
+ return BrokerageReportAvailabilitySchema;
58
58
  })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.2.95",
3
+ "version": "1.2.99",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",
@@ -1466,20 +1466,27 @@ module.exports = (() => {
1466
1466
  * @param {Array.<Object>} portfolios - The portfolios.
1467
1467
  * @param {Array.<Object>} positions - The positions (for all of the portfolios).
1468
1468
  * @param {Array.<Object>} summaries - The positions summaries (for all of the positions).
1469
- * @param {PositionSummaryFrame=} - If specified, locks the current (and previous) periods to a specific frame, use for reporting.
1469
+ * @param {PositionSummaryFrame=} reportFrame - If specified, locks the current (and previous) periods to a specific frame, use for reporting.
1470
+ * @param {Day=} reportDate - The end date for the report frame.
1470
1471
  */
1471
1472
  class PositionContainer {
1472
- constructor(definitions, portfolios, positions, summaries, frame) {
1473
+ constructor(definitions, portfolios, positions, summaries, reportFrame, reportDate) {
1473
1474
  assert.argumentIsArray(definitions, 'definitions', PositionTreeDefinition, 'PositionTreeDefinition');
1474
1475
  assert.argumentIsArray(portfolios, 'portfolios');
1475
1476
  assert.argumentIsArray(positions, 'positions');
1476
1477
  assert.argumentIsArray(summaries, 'summaries');
1477
- assert.argumentIsOptional(frame, 'frame', PositionSummaryFrame, 'PositionSummaryFrame');
1478
+ assert.argumentIsOptional(reportFrame, 'reportFrame', PositionSummaryFrame, 'PositionSummaryFrame');
1479
+
1480
+ if (reportFrame) {
1481
+ assert.argumentIsRequired(reportDate, 'reportDate', Day, 'Day');
1482
+ }
1478
1483
 
1479
1484
  this._definitions = definitions;
1480
1485
 
1481
1486
  this._groupBindings = { };
1482
1487
 
1488
+ this._reporting = reportFrame instanceof PositionSummaryFrame;
1489
+
1483
1490
  this._positionSymbolAddedEvent = new Event(this);
1484
1491
  this._positionSymbolRemovedEvent = new Event(this);
1485
1492
 
@@ -1489,15 +1496,21 @@ module.exports = (() => {
1489
1496
  return map;
1490
1497
  }, { });
1491
1498
 
1492
- this._currentSummaryFrame = frame || PositionSummaryFrame.YTD;
1493
- this._currentSummaryRange = array.last(this._currentSummaryFrame.getRecentRanges(0));
1499
+ if (reportFrame) {
1500
+ this._currentSummaryFrame = reportFrame;
1501
+ this._currentSummaryRange = array.last(this._currentSummaryFrame.getPriorRanges(reportDate, 0));
1494
1502
 
1495
- this._previousSummaryFrame = frame || PositionSummaryFrame.YEARLY;
1496
- this._previousSummaryRanges = this._previousSummaryFrame.getRecentRanges(3);
1503
+ this._previousSummaryFrame = reportFrame;
1504
+ this._previousSummaryRanges = this._currentSummaryFrame.getPriorRanges(reportDate, 3);
1497
1505
 
1498
- if (this._currentSummaryFrame === this._previousSummaryFrame) {
1499
1506
  this._previousSummaryRanges.pop();
1500
1507
  } else {
1508
+ this._currentSummaryFrame = PositionSummaryFrame.YTD;
1509
+ this._currentSummaryRange = array.first(this._currentSummaryFrame.getRecentRanges(0));
1510
+
1511
+ this._previousSummaryFrame = PositionSummaryFrame.YEARLY;
1512
+ this._previousSummaryRanges = this._previousSummaryFrame.getRecentRanges(3);
1513
+
1501
1514
  this._previousSummaryRanges.shift();
1502
1515
  }
1503
1516
 
@@ -2366,7 +2379,7 @@ module.exports = (() => {
2366
2379
  const currentSummary = this._summariesCurrent[ position.position ] || null;
2367
2380
  const previousSummaries = this._summariesPrevious[ position.position ] || getSummaryArray(this._previousSummaryRanges);
2368
2381
 
2369
- returnRef = new PositionItem(portfolio, position, currentSummary, previousSummaries);
2382
+ returnRef = new PositionItem(portfolio, position, currentSummary, previousSummaries, this._reporting);
2370
2383
  } else {
2371
2384
  returnRef = null;
2372
2385
  }
@@ -3360,9 +3373,10 @@ module.exports = (() => {
3360
3373
  * @param {Object} position
3361
3374
  * @param {Object} currentSummary
3362
3375
  * @param {Array.<Object>} previousSummaries
3376
+ * @param {Boolean} reporting
3363
3377
  */
3364
3378
  class PositionItem extends Disposable {
3365
- constructor(portfolio, position, currentSummary, previousSummaries) {
3379
+ constructor(portfolio, position, currentSummary, previousSummaries, reporting) {
3366
3380
  super();
3367
3381
 
3368
3382
  this._portfolio = portfolio;
@@ -3376,6 +3390,8 @@ module.exports = (() => {
3376
3390
  this._currentSummary = currentSummary || null;
3377
3391
  this._previousSummaries = previousSummaries || [ ];
3378
3392
 
3393
+ this._reporting = reporting;
3394
+
3379
3395
  this._data = { };
3380
3396
 
3381
3397
  this._data.basis = null;
@@ -3704,7 +3720,7 @@ module.exports = (() => {
3704
3720
 
3705
3721
  function calculateStaticData(item) {
3706
3722
  const position = item.position;
3707
- const snapshot = item.position.snapshot;
3723
+ const snapshot = getSnapshot(position, item.currentSummary, item._reporting);
3708
3724
  const previousSummaries = item.previousSummaries;
3709
3725
 
3710
3726
  const data = item._data;
@@ -3748,7 +3764,7 @@ module.exports = (() => {
3748
3764
 
3749
3765
  function calculatePriceData(item, price) {
3750
3766
  const position = item.position;
3751
- const snapshot = item.position.snapshot;
3767
+ const snapshot = getSnapshot(position, item.currentSummary, item._reporting);
3752
3768
  const previousSummaries = item.previousSummaries;
3753
3769
 
3754
3770
  const data = item._data;
@@ -3900,6 +3916,28 @@ module.exports = (() => {
3900
3916
  return is.object(position.system) && is.boolean(position.system.locked) && position.system.locked;
3901
3917
  }
3902
3918
 
3919
+ function getSnapshot(position, currentSummary, reporting) {
3920
+ let snapshot;
3921
+
3922
+ if (reporting) {
3923
+ snapshot = { };
3924
+
3925
+ snapshot.date = currentSummary.end.date;
3926
+ snapshot.open = currentSummary.end.open;
3927
+ snapshot.direction = currentSummary.end.direction;
3928
+ snapshot.buys = currentSummary.period.buys;
3929
+ snapshot.sells = currentSummary.period.sells;
3930
+ snapshot.gain = currentSummary.period.realized;
3931
+ snapshot.basis = currentSummary.end.basis;
3932
+ snapshot.income = currentSummary.period.income;
3933
+ snapshot.value = currentSummary.end.value;
3934
+ } else {
3935
+ snapshot = position.snapshot;
3936
+ }
3937
+
3938
+ return snapshot;
3939
+ }
3940
+
3903
3941
  return PositionItem;
3904
3942
  })();
3905
3943