@barchart/portfolio-api-common 1.0.25 → 1.0.29

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.
@@ -0,0 +1,71 @@
1
+ const array = require('@barchart/common-js/lang/array')
2
+ assert = require('@barchart/common-js/lang/assert'),
3
+ Decimal = require('@barchart/common-js/lang/Decimal'),
4
+ formatter = require('@barchart/common-js/lang/formatter');
5
+
6
+ module.exports = (() => {
7
+
8
+ /**
9
+ * Formats Position Summary records into groups based on instrument type
10
+ *
11
+ */
12
+ class PositionSummaryFormatter{
13
+
14
+ /**
15
+ * The formatter
16
+ *
17
+ * @param {Array} summaries
18
+ * @returns {Object}
19
+ */
20
+ static format(summaries) {
21
+ assert.argumentIsRequired(summaries, 'summaries', Object);
22
+
23
+ const summaryGroups = array.groupBy(summaries, summary => summary.instrument.type.code);
24
+
25
+ const total = {
26
+ start: Decimal.ZERO,
27
+ change: Decimal.ZERO,
28
+ end: Decimal.ZERO
29
+ };
30
+
31
+ const formattedSummaryGroups = Object.keys(summaryGroups).map((group) => {
32
+ const positions = summaryGroups[group];
33
+
34
+ let start = Decimal.ZERO;
35
+ let end = Decimal.ZERO;
36
+
37
+ positions.map((position) => {
38
+ start = start.add(position.start.value);
39
+ end = end.add(position.end.value);
40
+ });
41
+
42
+ const change = end.subtract(start);
43
+
44
+ total.start = total.start.add(start);
45
+ total.change = total.change.add(change);
46
+ total.end = total.end.add(end);
47
+
48
+ return summaryGroups[group] = {
49
+ name: group,
50
+ start: formatNumber(start.toFloat()),
51
+ end: formatNumber(end.toFloat()),
52
+ change: formatNumber(change.toFloat())
53
+ };
54
+ });
55
+
56
+ Object.keys(total).map(key => total[key] = formatNumber(total[key].toFloat()));
57
+
58
+ return {
59
+ groups: formattedSummaryGroups,
60
+ total
61
+ };
62
+ }
63
+ }
64
+
65
+ const formatNumber = (float) => {
66
+ return formatter.numberToString(float, 2, ',');
67
+ };
68
+
69
+ return PositionSummaryFormatter;
70
+
71
+ })();
@@ -1,4 +1,5 @@
1
1
  const assert = require('@barchart/common-js/lang/assert'),
2
+ is = require('@barchart/common-js/lang/is'),
2
3
  Currency = require('@barchart/common-js/lang/Currency'),
3
4
  DataType = require('@barchart/common-js/serialization/json/DataType'),
4
5
  Enum = require('@barchart/common-js/lang/Enum'),
@@ -33,6 +34,35 @@ module.exports = (() => {
33
34
  return this._schema;
34
35
  }
35
36
 
37
+ /**
38
+ * Returns the appropriate schema for creating a transaction of the
39
+ * supplied type.
40
+ *
41
+ * @public
42
+ * @static
43
+ * @param {String|TransactionType} transactionType
44
+ * @returns {TransactionSchema|null}
45
+ */
46
+ static forCreate(transactionType) {
47
+ let code;
48
+
49
+ if (transactionType instanceof TransactionType) {
50
+ code = transactionType.code;
51
+ } else {
52
+ code = transactionType;
53
+ }
54
+
55
+ let schema;
56
+
57
+ if (is.string(code)) {
58
+ schema = Enum.fromCode(TransactionSchema, code);
59
+ } else {
60
+ schema = null;
61
+ }
62
+
63
+ return schema;
64
+ }
65
+
36
66
  /**
37
67
  * The complete transaction schema.
38
68
  *
@@ -210,9 +240,10 @@ module.exports = (() => {
210
240
  .schema
211
241
  );
212
242
 
213
- const buy = new TransactionSchema(SchemaBuilder.withName('B')
243
+ const buy = new TransactionSchema(SchemaBuilder.withName(TransactionType.BUY.code)
244
+ .withField('portfolio', DataType.STRING)
245
+ .withField('position', DataType.STRING)
214
246
  .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
215
- .withField('position', DataType.STRING, true)
216
247
  .withField('instrument.name', DataType.STRING, true)
217
248
  .withField('instrument.type', DataType.STRING, true)
218
249
  .withField('instrument.currency', DataType.forEnum(Currency, 'Currency'), true)
@@ -226,10 +257,10 @@ module.exports = (() => {
226
257
  .schema
227
258
  );
228
259
 
229
- const sell = new TransactionSchema(SchemaBuilder.withName('S')
260
+ const sell = new TransactionSchema(SchemaBuilder.withName(TransactionType.SELL.code)
261
+ .withField('portfolio', DataType.STRING)
262
+ .withField('position', DataType.STRING)
230
263
  .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
231
- .withField('position', DataType.STRING, true)
232
- .withField('currency', DataType.forEnum(Currency, 'Currency'))
233
264
  .withField('date', DataType.DAY)
234
265
  .withField('price', DataType.DECIMAL)
235
266
  .withField('quantity', DataType.DECIMAL)
@@ -237,10 +268,15 @@ module.exports = (() => {
237
268
  .schema
238
269
  );
239
270
 
240
- const buyShort = new TransactionSchema(SchemaBuilder.withName('BS')
271
+ const buyShort = new TransactionSchema(SchemaBuilder.withName(TransactionType.BUY_SHORT.code)
272
+ .withField('portfolio', DataType.STRING)
273
+ .withField('position', DataType.STRING)
241
274
  .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
242
- .withField('position', DataType.STRING, true)
243
- .withField('currency', DataType.forEnum(Currency, 'Currency'))
275
+ .withField('instrument.name', DataType.STRING, true)
276
+ .withField('instrument.type', DataType.STRING, true)
277
+ .withField('instrument.currency', DataType.forEnum(Currency, 'Currency'), true)
278
+ .withField('instrument.symbol.barchart', DataType.STRING, true)
279
+ .withField('instrument.symbol.display', DataType.STRING, true)
244
280
  .withField('date', DataType.DAY)
245
281
  .withField('price', DataType.DECIMAL)
246
282
  .withField('quantity', DataType.DECIMAL)
@@ -248,10 +284,10 @@ module.exports = (() => {
248
284
  .schema
249
285
  );
250
286
 
251
- const sellShort = new TransactionSchema(SchemaBuilder.withName('SS')
287
+ const sellShort = new TransactionSchema(SchemaBuilder.withName(TransactionType.SELL_SHORT.code)
288
+ .withField('portfolio', DataType.STRING)
289
+ .withField('position', DataType.STRING)
252
290
  .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
253
- .withField('position', DataType.STRING, true)
254
- .withField('currency', DataType.forEnum(Currency, 'Currency'))
255
291
  .withField('date', DataType.DAY)
256
292
  .withField('price', DataType.DECIMAL)
257
293
  .withField('quantity', DataType.DECIMAL)
@@ -259,10 +295,10 @@ module.exports = (() => {
259
295
  .schema
260
296
  );
261
297
 
262
- const dividend = new TransactionSchema(SchemaBuilder.withName('DV')
298
+ const dividend = new TransactionSchema(SchemaBuilder.withName(TransactionType.DIVIDEND.code)
299
+ .withField('portfolio', DataType.STRING)
300
+ .withField('position', DataType.STRING)
263
301
  .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
264
- .withField('position', DataType.STRING, true)
265
- .withField('currency', DataType.forEnum(Currency, 'Currency'))
266
302
  .withField('date', DataType.DAY)
267
303
  .withField('rate', DataType.DECIMAL)
268
304
  .withField('open', DataType.DECIMAL, true)
@@ -271,10 +307,10 @@ module.exports = (() => {
271
307
  .schema
272
308
  );
273
309
 
274
- const dividendReinvest = new TransactionSchema(SchemaBuilder.withName('DX')
310
+ const dividendReinvest = new TransactionSchema(SchemaBuilder.withName(TransactionType.DIVIDEND_REINVEST.code)
311
+ .withField('portfolio', DataType.STRING)
312
+ .withField('position', DataType.STRING)
275
313
  .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
276
- .withField('position', DataType.STRING, true)
277
- .withField('currency', DataType.forEnum(Currency, 'Currency'))
278
314
  .withField('date', DataType.DAY)
279
315
  .withField('rate', DataType.DECIMAL)
280
316
  .withField('open', DataType.DECIMAL, true)
@@ -284,10 +320,10 @@ module.exports = (() => {
284
320
  .schema
285
321
  );
286
322
 
287
- const dividendStock = new TransactionSchema(SchemaBuilder.withName('DS')
323
+ const dividendStock = new TransactionSchema(SchemaBuilder.withName(TransactionType.DIVIDEND_STOCK.code)
324
+ .withField('portfolio', DataType.STRING)
325
+ .withField('position', DataType.STRING)
288
326
  .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
289
- .withField('position', DataType.STRING, true)
290
- .withField('currency', DataType.forEnum(Currency, 'Currency'))
291
327
  .withField('date', DataType.DAY)
292
328
  .withField('rate', DataType.DECIMAL)
293
329
  .withField('open', DataType.DECIMAL, true)
@@ -297,10 +333,10 @@ module.exports = (() => {
297
333
  .schema
298
334
  );
299
335
 
300
- const distributionCash = new TransactionSchema(SchemaBuilder.withName('DC')
336
+ const distributionCash = new TransactionSchema(SchemaBuilder.withName(TransactionType.DISTRIBUTION_CASH.code)
337
+ .withField('portfolio', DataType.STRING)
338
+ .withField('position', DataType.STRING)
301
339
  .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
302
- .withField('position', DataType.STRING, true)
303
- .withField('currency', DataType.forEnum(Currency, 'Currency'))
304
340
  .withField('date', DataType.DAY)
305
341
  .withField('rate', DataType.DECIMAL)
306
342
  .withField('open', DataType.DECIMAL, true)
@@ -309,10 +345,10 @@ module.exports = (() => {
309
345
  .schema
310
346
  );
311
347
 
312
- const distributionFund = new TransactionSchema(SchemaBuilder.withName('DF')
348
+ const distributionFund = new TransactionSchema(SchemaBuilder.withName(TransactionType.DISTRIBUTION_FUND.code)
349
+ .withField('portfolio', DataType.STRING)
350
+ .withField('position', DataType.STRING)
313
351
  .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
314
- .withField('position', DataType.STRING, true)
315
- .withField('currency', DataType.forEnum(Currency, 'Currency'))
316
352
  .withField('date', DataType.DAY)
317
353
  .withField('rate', DataType.DECIMAL)
318
354
  .withField('open', DataType.DECIMAL, true)
@@ -321,10 +357,10 @@ module.exports = (() => {
321
357
  .schema
322
358
  );
323
359
 
324
- const split = new TransactionSchema(SchemaBuilder.withName('SP')
360
+ const split = new TransactionSchema(SchemaBuilder.withName(TransactionType.SPLIT.code)
361
+ .withField('portfolio', DataType.STRING)
362
+ .withField('position', DataType.STRING)
325
363
  .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
326
- .withField('position', DataType.STRING, true)
327
- .withField('currency', DataType.forEnum(Currency, 'Currency'))
328
364
  .withField('date', DataType.DAY)
329
365
  .withField('numerator', DataType.DECIMAL)
330
366
  .withField('denominator', DataType.DECIMAL)
@@ -333,84 +369,110 @@ module.exports = (() => {
333
369
  .schema
334
370
  );
335
371
 
336
- const fee = new TransactionSchema(SchemaBuilder.withName('F')
372
+ const fee = new TransactionSchema(SchemaBuilder.withName(TransactionType.FEE.code)
373
+ .withField('portfolio', DataType.STRING)
374
+ .withField('position', DataType.STRING)
337
375
  .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
338
- .withField('position', DataType.STRING, true)
339
- .withField('currency', DataType.forEnum(Currency, 'Currency'))
340
376
  .withField('date', DataType.DAY)
341
377
  .withField('fee', DataType.DECIMAL)
342
378
  .schema
343
379
  );
344
380
 
345
- const feeUnits = new TransactionSchema(SchemaBuilder.withName('FU')
381
+ const feeUnits = new TransactionSchema(SchemaBuilder.withName(TransactionType.FEE_UNITS.code)
382
+ .withField('portfolio', DataType.STRING)
383
+ .withField('position', DataType.STRING)
346
384
  .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
347
- .withField('position', DataType.STRING, true)
348
- .withField('currency', DataType.forEnum(Currency, 'Currency'))
349
385
  .withField('date', DataType.DAY)
350
386
  .withField('fee', DataType.DECIMAL)
351
387
  .withField('price', DataType.DECIMAL)
352
388
  .schema
353
389
  );
354
390
 
355
- const deposit = new TransactionSchema(SchemaBuilder.withName('D')
391
+ const deposit = new TransactionSchema(SchemaBuilder.withName(TransactionType.DEPOSIT.code)
392
+ .withField('portfolio', DataType.STRING)
393
+ .withField('position', DataType.STRING)
356
394
  .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
357
- .withField('position', DataType.STRING, true)
358
- .withField('currency', DataType.forEnum(Currency, 'Currency'))
395
+ .withField('instrument.name', DataType.STRING, true)
396
+ .withField('instrument.type', DataType.STRING, true)
397
+ .withField('instrument.currency', DataType.forEnum(Currency, 'Currency'), true)
398
+ .withField('instrument.symbol.barchart', DataType.STRING, true)
399
+ .withField('instrument.symbol.display', DataType.STRING, true)
359
400
  .withField('date', DataType.DAY)
360
401
  .withField('amount', DataType.DECIMAL)
361
402
  .withField('fee', DataType.DECIMAL, true)
362
403
  .schema
363
404
  );
364
405
 
365
- const withdrawal = new TransactionSchema(SchemaBuilder.withName('W')
406
+ const withdrawal = new TransactionSchema(SchemaBuilder.withName(TransactionType.WITHDRAWAL.code)
407
+ .withField('portfolio', DataType.STRING)
408
+ .withField('position', DataType.STRING)
366
409
  .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
367
- .withField('position', DataType.STRING, true)
368
- .withField('currency', DataType.forEnum(Currency, 'Currency'))
369
410
  .withField('date', DataType.DAY)
370
411
  .withField('amount', DataType.DECIMAL)
371
412
  .withField('fee', DataType.DECIMAL, true)
372
413
  .schema
373
414
  );
374
415
 
375
- const debit = new TransactionSchema(SchemaBuilder.withName('DR')
416
+ const debit = new TransactionSchema(SchemaBuilder.withName(TransactionType.DEBIT.code)
417
+ .withField('portfolio', DataType.STRING)
418
+ .withField('position', DataType.STRING)
376
419
  .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
377
- .withField('position', DataType.STRING, true)
378
- .withField('currency', DataType.forEnum(Currency, 'Currency'))
379
420
  .withField('date', DataType.DAY)
380
421
  .withField('amount', DataType.DECIMAL)
381
422
  .withField('fee', DataType.DECIMAL, true)
382
423
  .schema
383
424
  );
384
425
 
385
- const credit = new TransactionSchema(SchemaBuilder.withName('CR')
426
+ const credit = new TransactionSchema(SchemaBuilder.withName(TransactionType.CREDIT.code)
427
+ .withField('portfolio', DataType.STRING)
428
+ .withField('position', DataType.STRING)
386
429
  .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
387
- .withField('position', DataType.STRING, true)
388
- .withField('currency', DataType.forEnum(Currency, 'Currency'))
389
430
  .withField('date', DataType.DAY)
390
431
  .withField('amount', DataType.DECIMAL)
391
432
  .withField('fee', DataType.DECIMAL, true)
392
433
  .schema
393
434
  );
394
435
 
395
- const valuation = new TransactionSchema(SchemaBuilder.withName('V')
436
+ const valuation = new TransactionSchema(SchemaBuilder.withName(TransactionType.VALUATION.code)
437
+ .withField('portfolio', DataType.STRING)
438
+ .withField('position', DataType.STRING)
396
439
  .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
397
- .withField('position', DataType.STRING, true)
398
- .withField('currency', DataType.forEnum(Currency, 'Currency'))
399
440
  .withField('date', DataType.DAY)
400
441
  .withField('value', DataType.DECIMAL)
401
442
  .withField('fee', DataType.DECIMAL, true)
402
443
  .schema
403
444
  );
404
445
 
405
- const income = new TransactionSchema(SchemaBuilder.withName('I')
446
+ const income = new TransactionSchema(SchemaBuilder.withName(TransactionType.INCOME.code)
447
+ .withField('portfolio', DataType.STRING)
448
+ .withField('position', DataType.STRING)
406
449
  .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
407
- .withField('position', DataType.STRING, true)
408
- .withField('currency', DataType.forEnum(Currency, 'Currency'))
409
450
  .withField('date', DataType.DAY)
410
451
  .withField('income', DataType.DECIMAL)
411
452
  .withField('fee', DataType.DECIMAL, true)
412
453
  .schema
413
454
  );
414
455
 
456
+ const map = { };
457
+
458
+ function addSchemaToMap(type, schema) {
459
+ map[type.code] = schema;
460
+ }
461
+
462
+ addSchemaToMap(TransactionType.BUY, buy);
463
+ addSchemaToMap(TransactionType.SELL, sell);
464
+ addSchemaToMap(TransactionType.BUY_SHORT, buyShort);
465
+ addSchemaToMap(TransactionType.SELL_SHORT, sellShort);
466
+ addSchemaToMap(TransactionType.DIVIDEND, dividend);
467
+ addSchemaToMap(TransactionType.DIVIDEND_STOCK, dividendStock);
468
+ addSchemaToMap(TransactionType.DIVIDEND_REINVEST, dividendReinvest);
469
+ addSchemaToMap(TransactionType.SPLIT, split);
470
+ addSchemaToMap(TransactionType.FEE, fee);
471
+ addSchemaToMap(TransactionType.FEE_UNITS, feeUnits);
472
+ addSchemaToMap(TransactionType.DEPOSIT, deposit);
473
+ addSchemaToMap(TransactionType.WITHDRAWAL, withdrawal);
474
+ addSchemaToMap(TransactionType.VALUATION, valuation);
475
+ addSchemaToMap(TransactionType.INCOME, income);
476
+
415
477
  return TransactionSchema;
416
478
  })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.0.25",
3
+ "version": "1.0.29",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",