@barchart/portfolio-api-common 1.0.1 → 1.0.5

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,16 @@
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'),
3
+ numberFormatter = require('@barchart/common-js/lang/formatter');
2
4
 
3
- const ValuationType = require('./../data/ValuationType');
5
+ const TransactionType = require('./../data/TransactionType');
4
6
 
5
7
  module.exports = (() => {
6
8
  'use strict';
7
9
 
8
10
  /**
9
- * Static utilities for formatting {@link Transaction} objects for
10
- * display to humans.
11
+ * Static configuration data.
11
12
  *
12
13
  * @public
13
- * @extends {Enum}
14
14
  */
15
15
  class TransactionFormatter {
16
16
  constructor(schema) {
@@ -18,14 +18,54 @@ module.exports = (() => {
18
18
  }
19
19
 
20
20
  /**
21
- * Formats transactions for display to humans.
21
+ * Formats transactions by adding "formatted" attribute
22
+ * and "instrument" attribute from position
22
23
  *
23
24
  * @public
24
25
  * @static
25
- * @param {Array.<Transaction>} transactions
26
+ * @param {Array} transactions
27
+ * @param {Array} positions
28
+ * @param {String} returnType - Whether the return values should be appended to the transaction
29
+ *
30
+ * @returns {Array}
26
31
  */
27
- static format(transactions) {
28
- return [ ];
32
+ static format(transactions, positions, returnType) {
33
+ assert.argumentIsRequired(transactions, 'transactions', Array);
34
+ assert.argumentIsRequired(positions, 'positions', Array);
35
+
36
+ const positionMap = {};
37
+
38
+ positions.map((p) => positionMap[p.position] = p.instrument);
39
+
40
+ return transactions.filter((t) => positionMap[t.position]).map((transaction) => {
41
+ transaction.instrument = positionMap[transaction.position];
42
+
43
+ let formatted = getBasicTransaction(transaction);
44
+
45
+ if (formatters.has(transaction.type)) {
46
+ const formatterFunction = formatters.get(transaction.type);
47
+
48
+ const formattedTransaction = formatterFunction(transaction);
49
+
50
+ Object.keys(formattedTransaction).map((key) => {
51
+ if (!is.undefined(formattedTransaction[key]) && is.fn(formattedTransaction[key].toFloat)) {
52
+ const precision = transaction.instrument.currency.precision;
53
+
54
+ formattedTransaction[key] = numberFormatter.numberToString(formattedTransaction[key].toFloat(), precision, ',');
55
+ }
56
+ });
57
+
58
+ formatted = Object.assign({}, formatted, formattedTransaction);
59
+ }
60
+
61
+ if (returnType === 'append') {
62
+ transaction.formatted = formatted;
63
+
64
+ return transaction;
65
+ }
66
+
67
+ return formatted;
68
+ });
29
69
  }
30
70
 
31
71
  toString() {
@@ -33,5 +73,130 @@ module.exports = (() => {
33
73
  }
34
74
  }
35
75
 
76
+ const getBasicTransaction = (t) => {
77
+ return {
78
+ date: t.date,
79
+ type: t.type,
80
+ sequence: t.sequence,
81
+ instrument: t.instrument
82
+ };
83
+ };
84
+
85
+ const formatters = new Map();
86
+
87
+ const buySellFormatter = (t) => {
88
+ return {
89
+ boughtSold: t.quantity,
90
+ price: t.trade.price,
91
+ fee: t.fee,
92
+ total: t.amount
93
+ };
94
+ };
95
+ formatters.set(TransactionType.BUY, buySellFormatter);
96
+ formatters.set(TransactionType.SELL, buySellFormatter);
97
+ formatters.set(TransactionType.BUY_SHORT, buySellFormatter);
98
+ formatters.set(TransactionType.SELL_SHORT, buySellFormatter);
99
+
100
+ formatters.set(TransactionType.DIVIDEND, (t) => {
101
+ return {
102
+ shares: t.snapshot.open,
103
+ total: t.dividend.amout,
104
+ rate: t.dividend.rate
105
+ };
106
+ });
107
+
108
+ formatters.set(TransactionType.DIVIDEND_STOCK, (t) => {
109
+ const shares = t.snapshot.open.subtract(t.quantity);
110
+ const rate = (is.object(t.dividend) && is.string(t.dividend.rate)) || '';
111
+
112
+ return {
113
+ boughtSold: t.quantity,
114
+ shares: shares,
115
+ rate: rate
116
+ };
117
+ });
118
+
119
+ formatters.set(TransactionType.DISTRIBUTION_CASH, (t) => {
120
+ return {
121
+ shares: t.snapshot.open,
122
+ total: t.dividend.amount,
123
+ rate: t.dividend.rate
124
+ };
125
+ });
126
+
127
+ formatters.set(TransactionType.DIVIDEND_REINVEST, (t) => {
128
+ return {
129
+ shares: t.snapshot.open.subtract(t.quantity),
130
+ price: t.dividend.price,
131
+ fee: t.fee,
132
+ total: t.quantity,
133
+ rate: t.dividend.rate
134
+ }
135
+ });
136
+
137
+ formatters.set(TransactionType.DISTRIBUTION_FUND, (t) => {
138
+ return {
139
+ shares: t.snapshot.open.subtract(t.quantity),
140
+ fee: t.fee
141
+ }
142
+ });
143
+
144
+ formatters.set(TransactionType.INCOME, (t) => {
145
+ return {
146
+ total: t.income.amount
147
+ };
148
+ });
149
+
150
+ formatters.set(TransactionType.FEE, (t) => {
151
+ return {
152
+ total: t.charge.amount
153
+ };
154
+ });
155
+
156
+ formatters.set(TransactionType.FEE_UNITS, (t) => {
157
+ return {
158
+ total: t.charge.amount,
159
+ boughtSold: t.quantity
160
+ };
161
+ });
162
+
163
+ formatters.set(TransactionType.SPLIT, (t) => {
164
+ return {
165
+ shares: t.quantity,
166
+ rate: t.split.numerator.divide(t.split.denominator)
167
+ };
168
+ });
169
+
170
+ formatters.set(TransactionType.VALUATION, (t) => {
171
+ return {
172
+ price: t.valuation.value
173
+ };
174
+ });
175
+
176
+ const cashFormatter = (t) => {
177
+ return {
178
+ total: t.quantity
179
+ };
180
+ };
181
+
182
+ formatters.set(TransactionType.DEPOSIT, cashFormatter);
183
+ formatters.set(TransactionType.WITHDRAWAL, cashFormatter);
184
+
185
+ formatters.set(TransactionType.DEBIT, (t) => {
186
+ const formatted = cashFormatter(t);
187
+
188
+ formatted.description = t.description;
189
+
190
+ return formatted;
191
+ });
192
+
193
+ formatters.set(TransactionType.CREDIT, (t) => {
194
+ const formatted = cashFormatter(t);
195
+
196
+ formatted.description = t.description;
197
+
198
+ return formatted;
199
+ });
200
+
36
201
  return TransactionFormatter;
37
- })();
202
+ })();
@@ -52,10 +52,42 @@ module.exports = (() => {
52
52
  }
53
53
 
54
54
  const complete = new TransactionSchema(SchemaBuilder.withName('Complete')
55
+ .withField('portfolio', DataType.STRING)
56
+ .withField('position', DataType.STRING)
55
57
  .withField('sequence', DataType.NUMBER)
58
+ .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
56
59
  .withField('date', DataType.DAY)
57
- .withField('description', DataType.STRING)
60
+ .withField('description', DataType.STRING, true)
58
61
  .withField('amount', DataType.DECIMAL)
62
+ .withField('quantity', DataType.DECIMAL)
63
+ .withField('fee', DataType.DECIMAL, true)
64
+ .withField('reference.position', DataType.STRING, true)
65
+ .withField('reference.sequence', DataType.NUMBER, true)
66
+ .withField('snapshot.open', DataType.DECIMAL)
67
+ .withField('snapshot.buys', DataType.DECIMAL)
68
+ .withField('snapshot.sells', DataType.DECIMAL)
69
+ .withField('snapshot.gain', DataType.DECIMAL)
70
+ .withField('snapshot.basis', DataType.DECIMAL)
71
+ .withField('snapshot.income', DataType.DECIMAL)
72
+ .withField('snapshot.value', DataType.DECIMAL)
73
+ .withField('legacy.system', DataType.STRING, true)
74
+ .withField('legacy.user', DataType.STRING, true)
75
+ .withField('legacy.portfolio', DataType.STRING)
76
+ .withField('legacy.position', DataType.STRING, true)
77
+ .withField('legacy.transaction', DataType.STRING, true)
78
+ .withField('trade.price', DataType.DECIMAL, true)
79
+ .withField('dividend.rate', DataType.DECIMAL, true)
80
+ .withField('dividend.effective', DataType.DAY, true)
81
+ .withField('dividend.price', DataType.DECIMAL, true)
82
+ .withField('dividend.amount', DataType.DECIMAL, true)
83
+ .withField('dividend.reference', DataType.STRING, true)
84
+ .withField('split.numerator', DataType.DECIMAL, true)
85
+ .withField('split.denominator', DataType.DECIMAL, true)
86
+ .withField('split.effective', DataType.DAY, true)
87
+ .withField('split.reference', DataType.STRING, true)
88
+ .withField('charge.amount', DataType.DECIMAL, true)
89
+ .withField('income.amount', DataType.DECIMAL, true)
90
+ .withField('valuation.value', DataType.DECIMAL, true)
59
91
  .schema
60
92
  );
61
93
 
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.5",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",
@@ -9,7 +9,7 @@
9
9
  },
10
10
  "scripts": {},
11
11
  "dependencies": {
12
- "@barchart/common-js": "3.2.35"
12
+ "@barchart/common-js": "~3.2.0"
13
13
  },
14
14
  "devDependencies": {
15
15
  "babel-core": "^6.26.0",