@barchart/portfolio-api-common 1.0.39 → 1.0.44

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.
@@ -10,16 +10,22 @@ module.exports = (() => {
10
10
  * @public
11
11
  * @extends {Enum}
12
12
  * @param {String} description
13
+ * @param {String} alternateDescription
13
14
  * @param {String} code
14
15
  * @param {Boolean} canReinvest
15
16
  */
16
17
  class InstrumentType extends Enum {
17
- constructor(code, description, canReinvest) {
18
+ constructor(code, description, alternateDescription, canReinvest) {
18
19
  super(code, description);
19
20
 
21
+ this._alternateDescription = alternateDescription;
20
22
  this._canReinvest = canReinvest;
21
23
  }
22
24
 
25
+ get alternateDescription() {
26
+ return this._alternateDescription;
27
+ }
28
+
23
29
  /**
24
30
  * Indicates if the instrument type allows automatic reinvestment.
25
31
  *
@@ -74,10 +80,10 @@ module.exports = (() => {
74
80
  }
75
81
  }
76
82
 
77
- const cash = new InstrumentType('CASH', 'cash', false);
78
- const equity = new InstrumentType('EQUITY', 'equity', true);
79
- const fund = new InstrumentType('FUND', 'mutual fund', true);
80
- const other = new InstrumentType('OTHER', 'other', false);
83
+ const cash = new InstrumentType('CASH', 'cash', 'Cash', false);
84
+ const equity = new InstrumentType('EQUITY', 'equity', 'Equities', true);
85
+ const fund = new InstrumentType('FUND', 'mutual fund', 'Funds', true);
86
+ const other = new InstrumentType('OTHER', 'other', 'Other', false);
81
87
 
82
88
  return InstrumentType;
83
89
  })();
@@ -98,6 +98,10 @@ module.exports = (() => {
98
98
  createGroups(this._tree, this._items, this._definitions);
99
99
  }
100
100
 
101
+ getSymbols() {
102
+ return Object.keys(this._symbols);
103
+ }
104
+
101
105
  setPrice(symbol, price) {
102
106
  if (this._symbols.hasOwnProperty(symbol)) {
103
107
  this._symbols.forEach(item.setPrice(price));
@@ -1,4 +1,5 @@
1
1
  const assert = require('@barchart/common-js/lang/assert'),
2
+ Decimal = require('@barchart/common-js/lang/Decimal'),
2
3
  is = require('@barchart/common-js/lang/is');
3
4
 
4
5
  module.exports = (() => {
@@ -18,16 +19,21 @@ module.exports = (() => {
18
19
 
19
20
  this._data.description = this._description;
20
21
 
21
- this._data.previous = null;
22
22
  this._data.current = null;
23
+ this._data.previous = null;
24
+
25
+ this._data.basis = null;
26
+ this._data.market = null;
23
27
 
24
28
  this._items.forEach((item) => {
25
- item.registerPriceChangeHandler((price, sender) => {
29
+ item.registerPriceChangeHandler((data, sender) => {
26
30
  if (this._single) {
27
- data.current = price;
31
+ data.current = data.current;
28
32
  } else {
29
33
  data.current = null;
30
34
  }
35
+
36
+ calculateVariablePriceData(this, item, price);
31
37
  });
32
38
  });
33
39
 
@@ -46,6 +52,10 @@ module.exports = (() => {
46
52
  return this._items;
47
53
  }
48
54
 
55
+ get single() {
56
+ return this._single;
57
+ }
58
+
49
59
  toString() {
50
60
  return '[PositionGroup]';
51
61
  }
@@ -55,10 +65,29 @@ module.exports = (() => {
55
65
  const items = group._items;
56
66
  const data = group._data;
57
67
 
58
- const updates = items.reduce(function(updates, item) {
68
+ let updates;
69
+
70
+ if (group.single) {
71
+ const item = items[0];
59
72
 
60
- }, { });
73
+ updates.basis = item.basis;
74
+ } else {
75
+ updates = items.reduce(function(updates, item) {
76
+ const position = item.position;
77
+ const snapshot = item.position.snapshot;
78
+
79
+ updates.value = updates.basis.add(snapshot.basis);
80
+
81
+ return updates;
82
+ }, {
83
+ basis: Decimal.ZERO
84
+ });
85
+ }
86
+
87
+ data.basis = updates.basis;
88
+ }
61
89
 
90
+ function calculateVariablePriceData(group, item, price) {
62
91
 
63
92
  }
64
93
 
@@ -14,7 +14,11 @@ module.exports = (() => {
14
14
  this._position = position;
15
15
  this._summaries = summaries || [ ];
16
16
 
17
- this._price = null;
17
+ this._data = { };
18
+
19
+ this._data.current = null;
20
+ this._data.previous = position.previous || null;
21
+
18
22
  this._priceChangeEvent = new Event(this);
19
23
  }
20
24
 
@@ -31,8 +35,10 @@ module.exports = (() => {
31
35
  }
32
36
 
33
37
  setPrice(price) {
34
- if (this._price !== price) {
35
- this._priceChangeEvent.fire(this._price = price);
38
+ if (this._data.price !== price) {
39
+ this._data.price = price;
40
+
41
+ this._priceChangeEvent.fire(this._data);
36
42
  }
37
43
  }
38
44
 
@@ -6,7 +6,8 @@ const assert = require('@barchart/common-js/lang/assert'),
6
6
  Schema = require('@barchart/common-js/serialization/json/Schema'),
7
7
  SchemaBuilder = require('@barchart/common-js/serialization/json/builders/SchemaBuilder');
8
8
 
9
- const ValuationType = require('./../data/ValuationType');
9
+ const InstrumentType = require('./../data/InstrumentType'),
10
+ ValuationType = require('./../data/ValuationType');
10
11
 
11
12
  module.exports = (() => {
12
13
  'use strict';
@@ -67,7 +68,7 @@ module.exports = (() => {
67
68
  .withField('sequence', DataType.NUMBER)
68
69
  .withField('instrument.id', DataType.STRING)
69
70
  .withField('instrument.name', DataType.STRING)
70
- .withField('instrument.type', DataType.STRING)
71
+ .withField('instrument.type', DataType.forEnum(InstrumentType, 'InstrumentType'))
71
72
  .withField('instrument.currency', DataType.forEnum(Currency, 'Currency'))
72
73
  .withField('instrument.delist', DataType.DAY, true)
73
74
  .withField('instrument.symbol.barchart', DataType.STRING, true)
@@ -99,7 +100,7 @@ module.exports = (() => {
99
100
  .withField('sequence', DataType.NUMBER)
100
101
  .withField('instrument.id', DataType.STRING)
101
102
  .withField('instrument.name', DataType.STRING)
102
- .withField('instrument.type', DataType.STRING)
103
+ .withField('instrument.type', DataType.forEnum(InstrumentType, 'InstrumentType'))
103
104
  .withField('instrument.currency', DataType.forEnum(Currency, 'Currency'))
104
105
  .withField('instrument.delist', DataType.DAY, true)
105
106
  .withField('instrument.symbol.barchart', DataType.STRING, true)
@@ -117,6 +118,7 @@ module.exports = (() => {
117
118
  .withField('snapshot.basis', DataType.DECIMAL)
118
119
  .withField('snapshot.income', DataType.DECIMAL)
119
120
  .withField('snapshot.value', DataType.DECIMAL)
121
+ .withField('previous', DataType.DECIMAL, true)
120
122
  .schema
121
123
  );
122
124
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.0.39",
3
+ "version": "1.0.44",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",
@@ -613,6 +613,10 @@ module.exports = (() => {
613
613
  createGroups(this._tree, this._items, this._definitions);
614
614
  }
615
615
 
616
+ getSymbols() {
617
+ return Object.keys(this._symbols);
618
+ }
619
+
616
620
  setPrice(symbol, price) {
617
621
  if (this._symbols.hasOwnProperty(symbol)) {
618
622
  this._symbols.forEach(item.setPrice(price));
@@ -649,6 +653,7 @@ module.exports = (() => {
649
653
 
650
654
  },{"./PositionGroup":4,"./PositionGroupDefinition":5,"./PositionItem":6,"@barchart/common-js/collections/Tree":7,"@barchart/common-js/lang/array":14,"@barchart/common-js/lang/assert":15,"@barchart/common-js/lang/is":16}],4:[function(require,module,exports){
651
655
  const assert = require('@barchart/common-js/lang/assert'),
656
+ Decimal = require('@barchart/common-js/lang/Decimal'),
652
657
  is = require('@barchart/common-js/lang/is');
653
658
 
654
659
  module.exports = (() => {
@@ -668,16 +673,21 @@ module.exports = (() => {
668
673
 
669
674
  this._data.description = this._description;
670
675
 
671
- this._data.previous = null;
672
676
  this._data.current = null;
677
+ this._data.previous = null;
678
+
679
+ this._data.basis = null;
680
+ this._data.market = null;
673
681
 
674
682
  this._items.forEach((item) => {
675
- item.registerPriceChangeHandler((price, sender) => {
683
+ item.registerPriceChangeHandler((data, sender) => {
676
684
  if (this._single) {
677
- data.current = price;
685
+ data.current = data.current;
678
686
  } else {
679
687
  data.current = null;
680
688
  }
689
+
690
+ calculateVariablePriceData(this, item, price);
681
691
  });
682
692
  });
683
693
 
@@ -696,6 +706,10 @@ module.exports = (() => {
696
706
  return this._items;
697
707
  }
698
708
 
709
+ get single() {
710
+ return this._single;
711
+ }
712
+
699
713
  toString() {
700
714
  return '[PositionGroup]';
701
715
  }
@@ -705,17 +719,36 @@ module.exports = (() => {
705
719
  const items = group._items;
706
720
  const data = group._data;
707
721
 
708
- const updates = items.reduce(function(updates, item) {
722
+ let updates;
709
723
 
710
- }, { });
724
+ if (group.single) {
725
+ const item = items[0];
711
726
 
727
+ updates.basis = item.basis;
728
+ } else {
729
+ updates = items.reduce(function(updates, item) {
730
+ const position = item.position;
731
+ const snapshot = item.position.snapshot;
732
+
733
+ updates.value = updates.basis.add(snapshot.basis);
734
+
735
+ return updates;
736
+ }, {
737
+ basis: Decimal.ZERO
738
+ });
739
+ }
740
+
741
+ data.basis = updates.basis;
742
+ }
743
+
744
+ function calculateVariablePriceData(group, item, price) {
712
745
 
713
746
  }
714
747
 
715
748
  return PositionGroup;
716
749
  })();
717
750
 
718
- },{"@barchart/common-js/lang/assert":15,"@barchart/common-js/lang/is":16}],5:[function(require,module,exports){
751
+ },{"@barchart/common-js/lang/Decimal":11,"@barchart/common-js/lang/assert":15,"@barchart/common-js/lang/is":16}],5:[function(require,module,exports){
719
752
  const assert = require('@barchart/common-js/lang/assert'),
720
753
  is = require('@barchart/common-js/lang/is');
721
754
 
@@ -777,7 +810,11 @@ module.exports = (() => {
777
810
  this._position = position;
778
811
  this._summaries = summaries || [ ];
779
812
 
780
- this._price = null;
813
+ this._data = { };
814
+
815
+ this._data.current = null;
816
+ this._data.previous = position.previous || null;
817
+
781
818
  this._priceChangeEvent = new Event(this);
782
819
  }
783
820
 
@@ -794,8 +831,10 @@ module.exports = (() => {
794
831
  }
795
832
 
796
833
  setPrice(price) {
797
- if (this._price !== price) {
798
- this._priceChangeEvent.fire(this._price = price);
834
+ if (this._data.price !== price) {
835
+ this._data.price = price;
836
+
837
+ this._priceChangeEvent.fire(this._data);
799
838
  }
800
839
  }
801
840
 
@@ -4988,12 +5027,31 @@ describe('After the PositionSummaryFrame enumeration is initialized', () => {
4988
5027
  });
4989
5028
 
4990
5029
  },{"./../../../lib/data/PositionSummaryFrame":1,"./../../../lib/data/TransactionType":2,"@barchart/common-js/lang/Day":10,"@barchart/common-js/lang/Decimal":11}],20:[function(require,module,exports){
5030
+ const Decimal = require('@barchart/common-js/lang/Decimal');
5031
+
4991
5032
  const PositionContainer = require('./../../../lib/processing/PositionContainer'),
4992
5033
  PositionGroupDefinition = require('./../../../lib/processing/PositionGroupDefinition');
4993
5034
 
4994
5035
  describe('When a position container data is gathered', () => {
4995
5036
  'use strict';
4996
5037
 
5038
+ let positionCounter = 0;
5039
+
5040
+ function getPosition(portfolio, symbol) {
5041
+ return {
5042
+ portfolio: portfolio,
5043
+ position: (positionCounter++).toString(),
5044
+ instrument: {
5045
+ symbol: {
5046
+ barchart: symbol
5047
+ }
5048
+ },
5049
+ snapshot: {
5050
+ basis: new Decimal(123)
5051
+ }
5052
+ }
5053
+ }
5054
+
4997
5055
  describe('for two portfolios, each with the same position, and the second portfolio with an additonal position', () => {
4998
5056
  let portfolios;
4999
5057
  let positions;
@@ -5011,31 +5069,9 @@ describe('When a position container data is gathered', () => {
5011
5069
  ];
5012
5070
 
5013
5071
  positions = [
5014
- {
5015
- portfolio: 'a',
5016
- position: '1',
5017
- instrument: {
5018
- symbol: {
5019
- barchart: 'AAPL'
5020
- }
5021
- },
5022
- }, {
5023
- portfolio: 'b',
5024
- position: '2',
5025
- instrument: {
5026
- symbol: {
5027
- barchart: 'AAPL'
5028
- }
5029
- }
5030
- }, {
5031
- portfolio: 'b',
5032
- position: '3',
5033
- instrument: {
5034
- symbol: {
5035
- barchart: 'TSLA'
5036
- }
5037
- }
5038
- }
5072
+ getPosition('a', 'AAPL'),
5073
+ getPosition('b', 'AAPL'),
5074
+ getPosition('b', 'TSLA')
5039
5075
  ];
5040
5076
 
5041
5077
  summaries = [ ];
@@ -5086,4 +5122,4 @@ describe('When a position container data is gathered', () => {
5086
5122
  });
5087
5123
  });
5088
5124
 
5089
- },{"./../../../lib/processing/PositionContainer":3,"./../../../lib/processing/PositionGroupDefinition":5}]},{},[19,20]);
5125
+ },{"./../../../lib/processing/PositionContainer":3,"./../../../lib/processing/PositionGroupDefinition":5,"@barchart/common-js/lang/Decimal":11}]},{},[19,20]);
@@ -1,9 +1,28 @@
1
+ const Decimal = require('@barchart/common-js/lang/Decimal');
2
+
1
3
  const PositionContainer = require('./../../../lib/processing/PositionContainer'),
2
4
  PositionGroupDefinition = require('./../../../lib/processing/PositionGroupDefinition');
3
5
 
4
6
  describe('When a position container data is gathered', () => {
5
7
  'use strict';
6
8
 
9
+ let positionCounter = 0;
10
+
11
+ function getPosition(portfolio, symbol) {
12
+ return {
13
+ portfolio: portfolio,
14
+ position: (positionCounter++).toString(),
15
+ instrument: {
16
+ symbol: {
17
+ barchart: symbol
18
+ }
19
+ },
20
+ snapshot: {
21
+ basis: new Decimal(123)
22
+ }
23
+ }
24
+ }
25
+
7
26
  describe('for two portfolios, each with the same position, and the second portfolio with an additonal position', () => {
8
27
  let portfolios;
9
28
  let positions;
@@ -21,31 +40,9 @@ describe('When a position container data is gathered', () => {
21
40
  ];
22
41
 
23
42
  positions = [
24
- {
25
- portfolio: 'a',
26
- position: '1',
27
- instrument: {
28
- symbol: {
29
- barchart: 'AAPL'
30
- }
31
- },
32
- }, {
33
- portfolio: 'b',
34
- position: '2',
35
- instrument: {
36
- symbol: {
37
- barchart: 'AAPL'
38
- }
39
- }
40
- }, {
41
- portfolio: 'b',
42
- position: '3',
43
- instrument: {
44
- symbol: {
45
- barchart: 'TSLA'
46
- }
47
- }
48
- }
43
+ getPosition('a', 'AAPL'),
44
+ getPosition('b', 'AAPL'),
45
+ getPosition('b', 'TSLA')
49
46
  ];
50
47
 
51
48
  summaries = [ ];