@barchart/portfolio-api-common 1.0.84 → 1.0.89

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.
@@ -11,6 +11,7 @@ module.exports = (() => {
11
11
  * @extends {Enum}
12
12
  * @param {String} code
13
13
  * @param {String} description
14
+ * @param {String} display
14
15
  * @param {Boolean} purchase
15
16
  * @param {Boolean} sale
16
17
  * @param {Boolean} income
@@ -18,15 +19,17 @@ module.exports = (() => {
18
19
  * @param {Boolean} closing
19
20
  */
20
21
  class TransactionType extends Enum {
21
- constructor(code, description, purchase, sale, income, opening, closing) {
22
+ constructor(code, description, display, purchase, sale, income, opening, closing) {
22
23
  super(code, description);
23
24
 
25
+ assert.argumentIsRequired(display, 'display', String);
24
26
  assert.argumentIsRequired(purchase, 'purchase', Boolean);
25
27
  assert.argumentIsRequired(sale, 'sale', Boolean);
26
28
  assert.argumentIsRequired(income, 'income', Boolean);
27
29
  assert.argumentIsRequired(opening, 'opening', Boolean);
28
30
  assert.argumentIsRequired(closing, 'closing', Boolean);
29
31
 
32
+ this._display = display;
30
33
  this._purchase = purchase;
31
34
  this._sale = sale;
32
35
  this._income = income;
@@ -34,6 +37,16 @@ module.exports = (() => {
34
37
  this._closing = closing;
35
38
  }
36
39
 
40
+ /**
41
+ * A human-readable description of the transaction type.
42
+ *
43
+ * @public
44
+ * @returns {String}
45
+ */
46
+ get display() {
47
+ return this._display;
48
+ }
49
+
37
50
  /**
38
51
  * Indicates if the transaction was a trade.
39
52
  *
@@ -299,27 +312,27 @@ module.exports = (() => {
299
312
  }
300
313
  }
301
314
 
302
- const buy = new TransactionType('B', 'Buy', true, false, false, true, false);
303
- const sell = new TransactionType('S', 'Sell', false, true, false, false, true);
304
- const buyShort = new TransactionType('BS', 'Buy To Cover', true, false, false, false, true);
305
- const sellShort = new TransactionType('SS', 'Sell Short', false, true, false, true, false);
306
- const dividend = new TransactionType('DV', 'Dividend', false, false, true, false, false);
307
- const dividendReinvest = new TransactionType('DX', 'Dividend (Reinvested)', false, false, false, true, false);
308
- const dividendStock = new TransactionType('DS', 'Dividend (Stock)', false, false, false, true, false);
309
- const split = new TransactionType('SP', 'Split', false, false, false, true, false);
310
- const fee = new TransactionType('F', 'Fee', false, false, false, true, false);
311
- const feeUnits = new TransactionType('FU', 'Fee', false, false, false, false, false);
312
-
313
- const distributionCash = new TransactionType('DC', 'Distribution (Cash)', false, false, true, false, false);
314
- const distributionFund = new TransactionType('DF', 'Distribution (Units)', false, false, false, true, false);
315
-
316
- const deposit = new TransactionType('D', 'Deposit', false, false, false, true, false);
317
- const withdrawal = new TransactionType('W', 'Withdrawal', false, false, false, false, true);
318
- const debit = new TransactionType('DR', 'Debit', false, false, false, false, true);
319
- const credit = new TransactionType('CR', 'Credit', false, false, false, true, false);
320
-
321
- const valuation = new TransactionType('V', 'Valuation', false, false, false, false, false);
322
- const income = new TransactionType('I', 'Income', false, false, true, false, false);
315
+ const buy = new TransactionType('B', 'Buy', 'Buy', true, false, false, true, false);
316
+ const sell = new TransactionType('S', 'Sell', 'Sell', false, true, false, false, true);
317
+ const buyShort = new TransactionType('BS', 'Buy To Cover', 'Buy To Cover', true, false, false, false, true);
318
+ const sellShort = new TransactionType('SS', 'Sell Short', 'Sell Short', false, true, false, true, false);
319
+ const dividend = new TransactionType('DV', 'Dividend', 'Dividend', false, false, true, false, false);
320
+ const dividendReinvest = new TransactionType('DX', 'Dividend (Reinvested)', 'Dividend Reinvest', false, false, false, true, false);
321
+ const dividendStock = new TransactionType('DS', 'Dividend (Stock)', 'Dividend Stock', false, false, false, true, false);
322
+ const split = new TransactionType('SP', 'Split', 'Split', false, false, false, true, false);
323
+ const fee = new TransactionType('F', 'Fee', 'Fee', false, false, false, true, false);
324
+ const feeUnits = new TransactionType('FU', 'Fee Units', 'Fee', false, false, false, false, false);
325
+
326
+ const distributionCash = new TransactionType('DC', 'Distribution (Cash)', 'Cash Distribution', false, false, true, false, false);
327
+ const distributionFund = new TransactionType('DF', 'Distribution (Units)', 'Unit Distribution', false, false, false, true, false);
328
+
329
+ const deposit = new TransactionType('D', 'Deposit', 'Deposit', false, false, false, true, false);
330
+ const withdrawal = new TransactionType('W', 'Withdrawal', 'Withdrawal', false, false, false, false, true);
331
+ const debit = new TransactionType('DR', 'Debit', 'Debit', false, false, false, false, true);
332
+ const credit = new TransactionType('CR', 'Credit', 'Credit', false, false, false, true, false);
333
+
334
+ const valuation = new TransactionType('V', 'Valuation', 'Valuation', false, false, false, false, false);
335
+ const income = new TransactionType('I', 'Income', 'Income', false, false, true, false, false);
323
336
 
324
337
  return TransactionType;
325
338
  })();
@@ -35,9 +35,9 @@ module.exports = (() => {
35
35
 
36
36
  const positionMap = {};
37
37
 
38
- positions.map((p) => positionMap[p.position] = p.instrument);
38
+ positions.map(p => positionMap[p.position] = p.instrument);
39
39
 
40
- return transactions.filter((t) => positionMap[t.position]).map((transaction) => {
40
+ return transactions.filter(t => positionMap[t.position]).map((transaction) => {
41
41
  transaction.instrument = positionMap[transaction.position];
42
42
 
43
43
  let formatted = getBasicTransaction(transaction);
@@ -76,7 +76,7 @@ module.exports = (() => {
76
76
  const getBasicTransaction = (t) => {
77
77
  const basic = {
78
78
  date: t.date,
79
- type: t.type,
79
+ type: t.type.display,
80
80
  sequence: t.sequence,
81
81
  instrument: t.instrument
82
82
  };
@@ -98,6 +98,7 @@ module.exports = (() => {
98
98
  total: t.amount
99
99
  };
100
100
  };
101
+
101
102
  formatters.set(TransactionType.BUY, buySellFormatter);
102
103
  formatters.set(TransactionType.SELL, buySellFormatter);
103
104
  formatters.set(TransactionType.BUY_SHORT, buySellFormatter);
@@ -153,6 +153,10 @@ module.exports = (() => {
153
153
  compositeGroups.forEach((group) => {
154
154
  const child = tree.addChild(group);
155
155
 
156
+ group.registerMarketPercentChangeHandler(() => {
157
+ this._tree.walk((childGroup) => childGroup.refreshMarketPercent());
158
+ });
159
+
156
160
  createGroups(child, group.items, additionalDefinitions);
157
161
  });
158
162
  };
@@ -1,6 +1,7 @@
1
1
  const assert = require('@barchart/common-js/lang/assert'),
2
2
  Currency = require('@barchart/common-js/lang/Currency'),
3
3
  Decimal = require('@barchart/common-js/lang/Decimal'),
4
+ Event = require('@barchart/common-js/messaging/Event'),
4
5
  formatter = require('@barchart/common-js/lang/formatter'),
5
6
  is = require('@barchart/common-js/lang/is');
6
7
 
@@ -25,6 +26,8 @@ module.exports = (() => {
25
26
  this._excluded = false;
26
27
  this._suspended = false;
27
28
 
29
+ this._marketPercentChangeEvent = new Event(this);
30
+
28
31
  this._dataFormat = { };
29
32
  this._dataActual = { };
30
33
 
@@ -135,6 +138,14 @@ module.exports = (() => {
135
138
  calculatePriceData(this, null, true);
136
139
  }
137
140
 
141
+ refreshMarketPercent() {
142
+ calculateMarketPercent(this, true);
143
+ }
144
+
145
+ registerMarketPercentChangeHandler(handler) {
146
+ this._marketPercentChangeEvent.register(handler);
147
+ }
148
+
138
149
  toString() {
139
150
  return '[PositionGroup]';
140
151
  }
@@ -252,12 +263,11 @@ module.exports = (() => {
252
263
  }
253
264
 
254
265
  actual.market = updates.market;
255
- actual.marketPercent = updates.marketPercent;
256
266
  actual.unrealizedToday = updates.unrealizedToday;
257
267
  actual.total = updates.unrealizedToday.add(actual.realized).add(actual.income);
268
+
258
269
  format.market = formatCurrency(actual.market, currency);
259
- format.marketPercent = formatPercent(actual.marketPercent, 2);
260
-
270
+
261
271
  if (updates.marketDirection.up || updates.marketDirection.down) {
262
272
  format.marketDirection = unchanged;
263
273
  setTimeout(() => format.marketDirection = updates.marketDirection, 0);
@@ -267,10 +277,41 @@ module.exports = (() => {
267
277
  format.unrealizedTodayNegative = actual.unrealizedToday.getIsNegative();
268
278
  format.total = formatCurrency(actual.total, currency);
269
279
  format.totalNegative = actual.total.getIsNegative();
280
+
281
+ calculateMarketPercent(group, false);
270
282
  }
271
283
 
272
- function calculatePercent() {
284
+ function calculateMarketPercent(group, silent) {
285
+ if (group.suspended) {
286
+ return;
287
+ }
288
+
289
+ const parent = group._parent;
290
+
291
+ const actual = group._dataActual;
292
+ const format = group._dataFormat;
293
+
294
+ let marketPercent;
295
+
296
+ if (parent !== null) {
297
+ const parentData = parent._dataActual;
298
+
299
+ if (parentData.market !== null && !parentData.market.getIsZero()) {
300
+ marketPercent = actual.market.divide(parentData.market);
301
+ } else {
302
+ marketPercent = null;
303
+ }
304
+ } else {
305
+ marketPercent = null;
306
+ }
273
307
 
308
+ actual.marketPercent = marketPercent;
309
+
310
+ format.marketPercent = formatPercent(actual.marketPercent, 2);
311
+
312
+ if (!silent) {
313
+ group._marketPercentChangeEvent.fire(group);
314
+ }
274
315
  }
275
316
 
276
317
  const unchanged = { up: false, down: false };
@@ -159,7 +159,7 @@ module.exports = (() => {
159
159
  }
160
160
 
161
161
  toString() {
162
- return '[TransactionSchema]';
162
+ return `[TransactionSchema (code=${this.code})]`;
163
163
  }
164
164
  }
165
165
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.0.84",
3
+ "version": "1.0.89",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",
@@ -388,6 +388,7 @@ module.exports = (() => {
388
388
  * @extends {Enum}
389
389
  * @param {String} code
390
390
  * @param {String} description
391
+ * @param {String} display
391
392
  * @param {Boolean} purchase
392
393
  * @param {Boolean} sale
393
394
  * @param {Boolean} income
@@ -395,15 +396,17 @@ module.exports = (() => {
395
396
  * @param {Boolean} closing
396
397
  */
397
398
  class TransactionType extends Enum {
398
- constructor(code, description, purchase, sale, income, opening, closing) {
399
+ constructor(code, description, display, purchase, sale, income, opening, closing) {
399
400
  super(code, description);
400
401
 
402
+ assert.argumentIsRequired(display, 'display', String);
401
403
  assert.argumentIsRequired(purchase, 'purchase', Boolean);
402
404
  assert.argumentIsRequired(sale, 'sale', Boolean);
403
405
  assert.argumentIsRequired(income, 'income', Boolean);
404
406
  assert.argumentIsRequired(opening, 'opening', Boolean);
405
407
  assert.argumentIsRequired(closing, 'closing', Boolean);
406
408
 
409
+ this._display = display;
407
410
  this._purchase = purchase;
408
411
  this._sale = sale;
409
412
  this._income = income;
@@ -411,6 +414,16 @@ module.exports = (() => {
411
414
  this._closing = closing;
412
415
  }
413
416
 
417
+ /**
418
+ * A human-readable description of the transaction type.
419
+ *
420
+ * @public
421
+ * @returns {String}
422
+ */
423
+ get display() {
424
+ return this._display;
425
+ }
426
+
414
427
  /**
415
428
  * Indicates if the transaction was a trade.
416
429
  *
@@ -676,27 +689,27 @@ module.exports = (() => {
676
689
  }
677
690
  }
678
691
 
679
- const buy = new TransactionType('B', 'Buy', true, false, false, true, false);
680
- const sell = new TransactionType('S', 'Sell', false, true, false, false, true);
681
- const buyShort = new TransactionType('BS', 'Buy To Cover', true, false, false, false, true);
682
- const sellShort = new TransactionType('SS', 'Sell Short', false, true, false, true, false);
683
- const dividend = new TransactionType('DV', 'Dividend', false, false, true, false, false);
684
- const dividendReinvest = new TransactionType('DX', 'Dividend (Reinvested)', false, false, false, true, false);
685
- const dividendStock = new TransactionType('DS', 'Dividend (Stock)', false, false, false, true, false);
686
- const split = new TransactionType('SP', 'Split', false, false, false, true, false);
687
- const fee = new TransactionType('F', 'Fee', false, false, false, true, false);
688
- const feeUnits = new TransactionType('FU', 'Fee', false, false, false, false, false);
692
+ const buy = new TransactionType('B', 'Buy', 'Buy', true, false, false, true, false);
693
+ const sell = new TransactionType('S', 'Sell', 'Sell', false, true, false, false, true);
694
+ const buyShort = new TransactionType('BS', 'Buy To Cover', 'Buy To Cover', true, false, false, false, true);
695
+ const sellShort = new TransactionType('SS', 'Sell Short', 'Sell Short', false, true, false, true, false);
696
+ const dividend = new TransactionType('DV', 'Dividend', 'Dividend', false, false, true, false, false);
697
+ const dividendReinvest = new TransactionType('DX', 'Dividend (Reinvested)', 'Dividend Reinvest', false, false, false, true, false);
698
+ const dividendStock = new TransactionType('DS', 'Dividend (Stock)', 'Dividend Stock', false, false, false, true, false);
699
+ const split = new TransactionType('SP', 'Split', 'Split', false, false, false, true, false);
700
+ const fee = new TransactionType('F', 'Fee', 'Fee', false, false, false, true, false);
701
+ const feeUnits = new TransactionType('FU', 'Fee Units', 'Fee', false, false, false, false, false);
689
702
 
690
- const distributionCash = new TransactionType('DC', 'Distribution (Cash)', false, false, true, false, false);
691
- const distributionFund = new TransactionType('DF', 'Distribution (Units)', false, false, false, true, false);
703
+ const distributionCash = new TransactionType('DC', 'Distribution (Cash)', 'Cash Distribution', false, false, true, false, false);
704
+ const distributionFund = new TransactionType('DF', 'Distribution (Units)', 'Unit Distribution', false, false, false, true, false);
692
705
 
693
- const deposit = new TransactionType('D', 'Deposit', false, false, false, true, false);
694
- const withdrawal = new TransactionType('W', 'Withdrawal', false, false, false, false, true);
695
- const debit = new TransactionType('DR', 'Debit', false, false, false, false, true);
696
- const credit = new TransactionType('CR', 'Credit', false, false, false, true, false);
706
+ const deposit = new TransactionType('D', 'Deposit', 'Deposit', false, false, false, true, false);
707
+ const withdrawal = new TransactionType('W', 'Withdrawal', 'Withdrawal', false, false, false, false, true);
708
+ const debit = new TransactionType('DR', 'Debit', 'Debit', false, false, false, false, true);
709
+ const credit = new TransactionType('CR', 'Credit', 'Credit', false, false, false, true, false);
697
710
 
698
- const valuation = new TransactionType('V', 'Valuation', false, false, false, false, false);
699
- const income = new TransactionType('I', 'Income', false, false, true, false, false);
711
+ const valuation = new TransactionType('V', 'Valuation', 'Valuation', false, false, false, false, false);
712
+ const income = new TransactionType('I', 'Income', 'Income', false, false, true, false, false);
700
713
 
701
714
  return TransactionType;
702
715
  })();
@@ -857,6 +870,10 @@ module.exports = (() => {
857
870
  compositeGroups.forEach((group) => {
858
871
  const child = tree.addChild(group);
859
872
 
873
+ group.registerMarketPercentChangeHandler(() => {
874
+ this._tree.walk((childGroup) => childGroup.refreshMarketPercent());
875
+ });
876
+
860
877
  createGroups(child, group.items, additionalDefinitions);
861
878
  });
862
879
  };
@@ -940,6 +957,7 @@ module.exports = (() => {
940
957
  const assert = require('@barchart/common-js/lang/assert'),
941
958
  Currency = require('@barchart/common-js/lang/Currency'),
942
959
  Decimal = require('@barchart/common-js/lang/Decimal'),
960
+ Event = require('@barchart/common-js/messaging/Event'),
943
961
  formatter = require('@barchart/common-js/lang/formatter'),
944
962
  is = require('@barchart/common-js/lang/is');
945
963
 
@@ -964,6 +982,8 @@ module.exports = (() => {
964
982
  this._excluded = false;
965
983
  this._suspended = false;
966
984
 
985
+ this._marketPercentChangeEvent = new Event(this);
986
+
967
987
  this._dataFormat = { };
968
988
  this._dataActual = { };
969
989
 
@@ -1074,6 +1094,14 @@ module.exports = (() => {
1074
1094
  calculatePriceData(this, null, true);
1075
1095
  }
1076
1096
 
1097
+ refreshMarketPercent() {
1098
+ calculateMarketPercent(this, true);
1099
+ }
1100
+
1101
+ registerMarketPercentChangeHandler(handler) {
1102
+ this._marketPercentChangeEvent.register(handler);
1103
+ }
1104
+
1077
1105
  toString() {
1078
1106
  return '[PositionGroup]';
1079
1107
  }
@@ -1191,12 +1219,11 @@ module.exports = (() => {
1191
1219
  }
1192
1220
 
1193
1221
  actual.market = updates.market;
1194
- actual.marketPercent = updates.marketPercent;
1195
1222
  actual.unrealizedToday = updates.unrealizedToday;
1196
1223
  actual.total = updates.unrealizedToday.add(actual.realized).add(actual.income);
1224
+
1197
1225
  format.market = formatCurrency(actual.market, currency);
1198
- format.marketPercent = formatPercent(actual.marketPercent, 2);
1199
-
1226
+
1200
1227
  if (updates.marketDirection.up || updates.marketDirection.down) {
1201
1228
  format.marketDirection = unchanged;
1202
1229
  setTimeout(() => format.marketDirection = updates.marketDirection, 0);
@@ -1206,10 +1233,41 @@ module.exports = (() => {
1206
1233
  format.unrealizedTodayNegative = actual.unrealizedToday.getIsNegative();
1207
1234
  format.total = formatCurrency(actual.total, currency);
1208
1235
  format.totalNegative = actual.total.getIsNegative();
1236
+
1237
+ calculateMarketPercent(group, false);
1209
1238
  }
1210
1239
 
1211
- function calculatePercent() {
1240
+ function calculateMarketPercent(group, silent) {
1241
+ if (group.suspended) {
1242
+ return;
1243
+ }
1212
1244
 
1245
+ const parent = group._parent;
1246
+
1247
+ const actual = group._dataActual;
1248
+ const format = group._dataFormat;
1249
+
1250
+ let marketPercent;
1251
+
1252
+ if (parent !== null) {
1253
+ const parentData = parent._dataActual;
1254
+
1255
+ if (parentData.market !== null && !parentData.market.getIsZero()) {
1256
+ marketPercent = actual.market.divide(parentData.market);
1257
+ } else {
1258
+ marketPercent = null;
1259
+ }
1260
+ } else {
1261
+ marketPercent = null;
1262
+ }
1263
+
1264
+ actual.marketPercent = marketPercent;
1265
+
1266
+ format.marketPercent = formatPercent(actual.marketPercent, 2);
1267
+
1268
+ if (!silent) {
1269
+ group._marketPercentChangeEvent.fire(group);
1270
+ }
1213
1271
  }
1214
1272
 
1215
1273
  const unchanged = { up: false, down: false };
@@ -1217,7 +1275,7 @@ module.exports = (() => {
1217
1275
  return PositionGroup;
1218
1276
  })();
1219
1277
 
1220
- },{"@barchart/common-js/lang/Currency":11,"@barchart/common-js/lang/Decimal":13,"@barchart/common-js/lang/assert":17,"@barchart/common-js/lang/formatter":18,"@barchart/common-js/lang/is":19}],6:[function(require,module,exports){
1278
+ },{"@barchart/common-js/lang/Currency":11,"@barchart/common-js/lang/Decimal":13,"@barchart/common-js/lang/assert":17,"@barchart/common-js/lang/formatter":18,"@barchart/common-js/lang/is":19,"@barchart/common-js/messaging/Event":20}],6:[function(require,module,exports){
1221
1279
  const assert = require('@barchart/common-js/lang/assert'),
1222
1280
  is = require('@barchart/common-js/lang/is');
1223
1281
 
@@ -5871,10 +5929,10 @@ describe('When a position container data is gathered', () => {
5871
5929
  income: new Decimal(0),
5872
5930
  gain: new Decimal(0)
5873
5931
  }
5874
- }
5932
+ };
5875
5933
  }
5876
5934
 
5877
- describe('for two portfolios, each with the same position, and the second portfolio with an additonal position', () => {
5935
+ describe('for two portfolios, each with the same position, and the second portfolio with an addition position', () => {
5878
5936
  let portfolios;
5879
5937
  let positions;
5880
5938
  let summaries;
@@ -29,10 +29,10 @@ describe('When a position container data is gathered', () => {
29
29
  income: new Decimal(0),
30
30
  gain: new Decimal(0)
31
31
  }
32
- }
32
+ };
33
33
  }
34
34
 
35
- describe('for two portfolios, each with the same position, and the second portfolio with an additonal position', () => {
35
+ describe('for two portfolios, each with the same position, and the second portfolio with an addition position', () => {
36
36
  let portfolios;
37
37
  let positions;
38
38
  let summaries;