@barchart/portfolio-api-common 1.0.1 → 1.0.2

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.
@@ -304,7 +304,7 @@ module.exports = (() => {
304
304
  const buyShort = new TransactionType('BS', 'Buy To Cover', true, false, false, false, true);
305
305
  const sellShort = new TransactionType('SS', 'Sell Short', false, true, false, true, false);
306
306
  const dividend = new TransactionType('DV', 'Dividend', false, false, true, false, false);
307
- const dividendReinvest = new TransactionType('DR', 'Dividend (Reinvested)', false, false, false, true, false);
307
+ const dividendReinvest = new TransactionType('DX', 'Dividend (Reinvested)', false, false, false, true, false);
308
308
  const dividendStock = new TransactionType('DS', 'Dividend (Stock)', false, false, false, true, false);
309
309
  const split = new TransactionType('SP', 'Split', false, false, false, true, false);
310
310
  const fee = new TransactionType('F', 'Fee', false, false, false, true, false);
@@ -1,16 +1,15 @@
1
- const assert = require('@barchart/common-js/lang/assert');
1
+ const assert = require('@barchart/common-js/lang/assert'),
2
+ is = require('@barchart/common-js/lang/is');
2
3
 
3
- const ValuationType = require('./../data/ValuationType');
4
+ const TransactionType = require('./../data/TransactionType');
4
5
 
5
6
  module.exports = (() => {
6
7
  'use strict';
7
8
 
8
9
  /**
9
- * Static utilities for formatting {@link Transaction} objects for
10
- * display to humans.
10
+ * Static configuration data.
11
11
  *
12
12
  * @public
13
- * @extends {Enum}
14
13
  */
15
14
  class TransactionFormatter {
16
15
  constructor(schema) {
@@ -18,14 +17,44 @@ module.exports = (() => {
18
17
  }
19
18
 
20
19
  /**
21
- * Formats transactions for display to humans.
20
+ * Formats transactions by adding "formatted" attribute
21
+ * and "instrument" attribute from position
22
22
  *
23
23
  * @public
24
24
  * @static
25
- * @param {Array.<Transaction>} transactions
25
+ * @param {Array} transactions
26
+ * @param {Array} positions
27
+ * @param {String} returnType - Whether the return values should be appended to the transaction
28
+ *
29
+ * @returns {Array}
26
30
  */
27
- static format(transactions) {
28
- return [ ];
31
+ static format(transactions, positions, returnType) {
32
+ assert.argumentIsRequired(transactions, 'transactions', Array);
33
+ assert.argumentIsRequired(positions, 'positions', Array);
34
+
35
+ const positionMap = {};
36
+
37
+ positions.map((p) => positionMap[p.position] = p.instrument);
38
+
39
+ return transactions.map((transaction) => {
40
+ transaction.instrument = positionMap[transaction.position];
41
+
42
+ let formatted = getBasicTransaction(transaction);
43
+
44
+ if (formatters.has(transaction.type)) {
45
+ const formatterFunction = formatters.get(transaction.type);
46
+
47
+ formatted = Object.assign({}, formatted, formatterFunction(transaction));
48
+ }
49
+
50
+ if (returnType === 'append') {
51
+ transaction.formatted = formatted;
52
+
53
+ return transaction;
54
+ }
55
+
56
+ return formatted;
57
+ });
29
58
  }
30
59
 
31
60
  toString() {
@@ -33,5 +62,130 @@ module.exports = (() => {
33
62
  }
34
63
  }
35
64
 
65
+ const getBasicTransaction = (t) => {
66
+ return {
67
+ date: t.date,
68
+ type: t.type,
69
+ sequence: t.sequence,
70
+ instrument: t.instrument
71
+ };
72
+ };
73
+
74
+ const formatters = new Map();
75
+
76
+ const buySellFormatter = (t) => {
77
+ return {
78
+ boughtSold: t.quantity,
79
+ price: t.trade.price,
80
+ fee: t.fee,
81
+ total: t.amount
82
+ };
83
+ };
84
+ formatters.set(TransactionType.BUY, buySellFormatter);
85
+ formatters.set(TransactionType.SELL, buySellFormatter);
86
+ formatters.set(TransactionType.BUY_SHORT, buySellFormatter);
87
+ formatters.set(TransactionType.SELL_SHORT, buySellFormatter);
88
+
89
+ formatters.set(TransactionType.DIVIDEND, (t) => {
90
+ return {
91
+ shares: t.snapshot.open,
92
+ total: t.dividend.amout,
93
+ rate: t.dividend.rate
94
+ };
95
+ });
96
+
97
+ formatters.set(TransactionType.DIVIDEND_STOCK, (t) => {
98
+ const shares = t.snapshot.open.subtract(t.quantity);
99
+ const rate = (is.object(t.dividend) && is.string(t.dividend.rate)) || '';
100
+
101
+ return {
102
+ boughtSold: t.quantity,
103
+ shares: shares,
104
+ rate: rate
105
+ };
106
+ });
107
+
108
+ formatters.set(TransactionType.DISTRIBUTION_CASH, (t) => {
109
+ return {
110
+ shares: t.snapshot.open,
111
+ total: t.dividend.amount,
112
+ rate: t.dividend.rate
113
+ };
114
+ });
115
+
116
+ formatters.set(TransactionType.DIVIDEND_REINVEST, (t) => {
117
+ return {
118
+ shares: t.snapshot.open.subtract(t.quantity),
119
+ price: t.dividend.price,
120
+ fee: t.fee,
121
+ total: t.quantity,
122
+ rate: t.dividend.rate
123
+ }
124
+ });
125
+
126
+ formatters.set(TransactionType.DISTRIBUTION_FUND, (t) => {
127
+ return {
128
+ shares: t.snapshot.open.subtract(t.quantity),
129
+ fee: t.fee
130
+ }
131
+ });
132
+
133
+ formatters.set(TransactionType.INCOME, (t) => {
134
+ return {
135
+ total: t.income.amount
136
+ };
137
+ });
138
+
139
+ formatters.set(TransactionType.FEE, (t) => {
140
+ return {
141
+ total: t.charge.amount
142
+ };
143
+ });
144
+
145
+ formatters.set(TransactionType.FEE_UNITS, (t) => {
146
+ return {
147
+ total: t.charge.amount,
148
+ boughtSold: t.quantity
149
+ };
150
+ });
151
+
152
+ formatters.set(TransactionType.SPLIT, (t) => {
153
+ return {
154
+ shares: t.quantity,
155
+ rate: t.split.numerator.divide(t.split.denominator)
156
+ };
157
+ });
158
+
159
+ formatters.set(TransactionType.VALUATION, (t) => {
160
+ return {
161
+ price: t.valuation.value
162
+ };
163
+ });
164
+
165
+ const cashFormatter = (t) => {
166
+ return {
167
+ total: t.quantity
168
+ };
169
+ };
170
+
171
+ formatters.set(TransactionType.DEPOSIT, cashFormatter);
172
+ formatters.set(TransactionType.WITHDRAWAL, cashFormatter);
173
+
174
+ formatters.set(TransactionType.DEBIT, (t) => {
175
+ const formatted = cashFormatter(t);
176
+
177
+ formatted.description = t.description;
178
+
179
+ return formatted;
180
+ });
181
+
182
+ formatters.set(TransactionType.CREDIT, (t) => {
183
+ const formatted = cashFormatter(t);
184
+
185
+ formatted.description = t.description;
186
+
187
+ return formatted;
188
+ });
189
+
36
190
  return TransactionFormatter;
37
- })();
191
+ })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",