@barchart/portfolio-api-common 1.16.0 → 1.17.1

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.
package/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # @barchart/portfolio-api-common
2
2
 
3
3
  [![AWS CodeBuild](https://codebuild.us-east-1.amazonaws.com/badges?uuid=eyJlbmNyeXB0ZWREYXRhIjoiSXBJTDZKWVFmSGVOSG9XQUgwbzB3Um5BZ0JsN2h1U3NQNWNhZTRHMlJKTVpEVVlVMENPaEFmR0NYS29rSStZWmZ5M1d0YVh2eXErVGhiekFtdHBpVmhJPSIsIml2UGFyYW1ldGVyU3BlYyI6ImROM3ZLMURwaXFyekltdDYiLCJtYXRlcmlhbFNldFNlcmlhbCI6MX0%3D&branch=master)](https://github.com/barchart/portfolio-api-common)
4
+ [![NPM](https://img.shields.io/npm/v/@barchart/portfolio-api-common)](https://www.npmjs.com/package/@barchart/portfolio-api-common)
4
5
 
5
6
  A *public* library of shared JavaScript code used by Barchart's paper-trading portfolio system.
6
7
 
@@ -44,13 +44,14 @@ module.exports = (() => {
44
44
  * @public
45
45
  * @param {PositionTreeDefinition[]} definitions
46
46
  * @param {Object[]} portfolios - The portfolios.
47
- * @param {Object[]} positions - The positions (for all of portfolios).
48
- * @param {Object[]} summaries - The positions summaries (for all of positions).
47
+ * @param {Object[]} positions - The positions (for all portfolios).
48
+ * @param {Object[]} summaries - The positions summaries (for all positions).
49
49
  * @param {PositionSummaryFrame=} reportFrame - If specified, locks the current (and previous) periods to a specific frame, use for reporting.
50
50
  * @param {Day=} reportDate - The end date for the report frame.
51
+ * @param {Array[]=} currencyPairs - The currency pairs.
51
52
  */
52
53
  class PositionContainer {
53
- constructor(definitions, portfolios, positions, summaries, reportFrame, reportDate) {
54
+ constructor(definitions, portfolios, positions, summaries, reportFrame, reportDate, currencyPairs) {
54
55
  assert.argumentIsArray(definitions, 'definitions', PositionTreeDefinition, 'PositionTreeDefinition');
55
56
  assert.argumentIsArray(portfolios, 'portfolios');
56
57
  assert.argumentIsArray(positions, 'positions');
@@ -61,6 +62,15 @@ module.exports = (() => {
61
62
  assert.argumentIsRequired(reportDate, 'reportDate', Day, 'Day');
62
63
  }
63
64
 
65
+ if (currencyPairs) {
66
+ assert.argumentIsArray(currencyPairs, 'currencyPairs');
67
+
68
+ currencyPairs.forEach((currencyPair) => {
69
+ assert.argumentIsArray(currencyPair, 'currencyPair', Currency, 'Currency');
70
+ assert.argumentIsValid(currencyPair.length, 'currencyPair.length', l => l === 2, 'has two items');
71
+ });
72
+ }
73
+
64
74
  this._definitions = definitions;
65
75
 
66
76
  this._groupBindings = { };
@@ -144,15 +154,25 @@ module.exports = (() => {
144
154
  return map;
145
155
  }, { });
146
156
 
147
- const forexCurrencyCodes = array.unique(Object.keys(this._currencies).concat(REQUIRED_CURRENCIES.map(c => c.code)));
157
+ if (is.array(currencyPairs)) {
158
+ currencyPairs.forEach((currencyPair) => {
159
+ currencyPair.sort((a, b) => comparators.compareStrings(a.code, b.code));
160
+ });
148
161
 
149
- this._forexSymbols = forexCurrencyCodes.reduce((symbols, code) => {
150
- if (code !== DEFAULT_CURRENCY.code) {
151
- symbols.push(`^${code}${DEFAULT_CURRENCY.code}`);
152
- }
162
+ this._forexSymbols = array.unique(currencyPairs.map((currencyPair) => {
163
+ return `^${currencyPair[0].code}${currencyPair[1].code}`;
164
+ }));
165
+ } else {
166
+ const forexCurrencyCodes = array.unique(Object.keys(this._currencies).concat(REQUIRED_CURRENCIES.map(c => c.code)));
153
167
 
154
- return symbols;
155
- }, [ ]);
168
+ this._forexSymbols = forexCurrencyCodes.reduce((symbols, code) => {
169
+ if (code !== DEFAULT_CURRENCY.code) {
170
+ symbols.push(`^${code}${DEFAULT_CURRENCY.code}`);
171
+ }
172
+
173
+ return symbols;
174
+ }, [ ]);
175
+ }
156
176
 
157
177
  this._forexQuotes = this._forexSymbols.map((symbol) => {
158
178
  return Rate.fromPair(Decimal.ONE, symbol);
@@ -304,6 +304,22 @@ module.exports = (() => {
304
304
  return this._excluded;
305
305
  }
306
306
 
307
+ /**
308
+ * Changes the group currency.
309
+ *
310
+ * @public
311
+ * @param {Currency} currency
312
+ */
313
+ changeCurrency(currency) {
314
+ assert.argumentIsRequired(currency, 'currency', Currency, 'Currency');
315
+
316
+ if (this._currency !== currency) {
317
+ this._currency = currency;
318
+
319
+ this.refresh();
320
+ }
321
+ }
322
+
307
323
  /**
308
324
  * Sets the immediate parent group (allowing for calculation of relative
309
325
  * percentages).
@@ -442,6 +458,8 @@ module.exports = (() => {
442
458
  }
443
459
 
444
460
  this._dataFormat.portfolioType = portfolioType;
461
+
462
+ this.changeCurrency(this._definition.currencySelector({ portfolio }));
445
463
  }
446
464
 
447
465
  /**
@@ -605,6 +623,14 @@ module.exports = (() => {
605
623
 
606
624
  this._dataActual.description = this._description;
607
625
  this._dataFormat.description = this._description;
626
+
627
+ if (this._definition.type !== PositionLevelType.PORTFOLIO || this._key !== PositionLevelDefinition.getKeyForPortfolioGroup(portfolio)) {
628
+ return;
629
+ }
630
+
631
+ const currencySelector = this._definition.currencySelector;
632
+
633
+ this.changeCurrency(currencySelector({ portfolio }));
608
634
  }));
609
635
 
610
636
  this._disposeStack.push(fundamentalBinding);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.16.0",
3
+ "version": "1.17.1",
4
4
  "description": "Common JavaScript code used by Barchart's Portfolio Service",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",
@@ -15,7 +15,7 @@
15
15
  "url": "git+ssh://git@github.com/barchart/portfolio-api-common.git"
16
16
  },
17
17
  "dependencies": {
18
- "@barchart/common-js": "^4.23.0",
18
+ "@barchart/common-js": "^4.27.0",
19
19
  "uuid": "^8.3.2"
20
20
  },
21
21
  "devDependencies": {