@barchart/portfolio-api-common 1.0.17 → 1.0.21

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.
@@ -1,5 +1,8 @@
1
- const assert = require('@barchart/common-js/lang/assert'),
2
- Enum = require('@barchart/common-js/lang/Enum');
1
+ const array = require('@barchart/common-js/lang/array'),
2
+ assert = require('@barchart/common-js/lang/assert'),
3
+ Day = require('@barchart/common-js/lang/Day'),
4
+ Enum = require('@barchart/common-js/lang/Enum'),
5
+ is = require('@barchart/common-js/lang/is');
3
6
 
4
7
  module.exports = (() => {
5
8
  'use strict';
@@ -9,13 +12,23 @@ module.exports = (() => {
9
12
  *
10
13
  * @public
11
14
  * @extends {Enum}
12
- * @param {String} description
13
15
  * @param {String} code
14
- * @param {Boolean} canReinvest
16
+ * @param {String} description
17
+ * @param {Function} rangeCalculator
15
18
  */
16
19
  class PositionSummaryFrame extends Enum {
17
- constructor(code, description) {
20
+ constructor(code, description, rangeCalculator) {
18
21
  super(code, description);
22
+
23
+ assert.argumentIsRequired(rangeCalculator, 'rangeCalculator', Function);
24
+
25
+ this._rangeCalculator = rangeCalculator;
26
+ }
27
+
28
+ getRanges(transactions) {
29
+ assert.argumentIsArray(transactions, 'transactions');
30
+
31
+ return this._rangeCalculator(transactions);
19
32
  }
20
33
 
21
34
  /**
@@ -63,10 +76,71 @@ module.exports = (() => {
63
76
  }
64
77
  }
65
78
 
66
- const yearly = new PositionSummaryFrame('YEARL', 'year');
67
- const quarterly = new PositionSummaryFrame('QUARTER', 'quarter');
68
- const monthly = new PositionSummaryFrame('MONTH', 'month');
69
- const ytd = new PositionSummaryFrame('YTD', 'year-to-date');
79
+ const yearly = new PositionSummaryFrame('YEARLY', 'year', getYearlyRanges);
80
+ const quarterly = new PositionSummaryFrame('QUARTER', 'quarter', getQuarterlyRanges);
81
+ const monthly = new PositionSummaryFrame('MONTH', 'month', getMonthlyRanges);
82
+ const ytd = new PositionSummaryFrame('YTD', 'year-to-date', getYearToDateRanges);
83
+
84
+ function getRange(start, end) {
85
+ return {
86
+ start: start,
87
+ end: end
88
+ };
89
+ }
90
+
91
+ function getYearlyRanges(transactions) {
92
+ const ranges = [ ];
93
+
94
+ if (transactions.length !== 0) {
95
+ const first = array.first(transactions);
96
+ const last = array.last(transactions);
97
+
98
+ const firstDate = first.date;
99
+ const lastDate = last.date;
100
+
101
+ let lastYear;
102
+
103
+ if (last.snapshot.open.getIsZero()) {
104
+ lastYear = last.date.year + 1;
105
+ } else {
106
+ lastYear = Day.getToday().year;
107
+ }
108
+
109
+ for (let end = new Day(firstDate.year, 12, 31); end.year < lastYear; end = end.addYears(1)) {
110
+ ranges.push(getRange(end.subtractYears(1), end));
111
+ }
112
+ }
113
+
114
+ return ranges;
115
+ }
116
+
117
+ function getQuarterlyRanges(transactions) {
118
+ return [ ];
119
+ }
120
+
121
+ function getMonthlyRanges(transactions) {
122
+ return [ ];
123
+ }
124
+
125
+ function getYearToDateRanges(transactions) {
126
+ const ranges = [ ];
127
+
128
+ if (transactions.length !== 0) {
129
+ const first = array.first(transactions);
130
+ const last = array.last(transactions);
131
+
132
+ const currentYear = Day.getToday().year;
133
+
134
+ if (!last.snapshot.open.getIsZero() || last.date.year === currentYear) {
135
+ let end = new Day(Day.getToday().year, 12, 31);
136
+ let start = end.subtractYears(1);
137
+
138
+ ranges.push(getRange(start, end));
139
+ }
140
+ }
141
+
142
+ return ranges;
143
+ }
70
144
 
71
145
  return PositionSummaryFrame;
72
146
  })();
@@ -3,6 +3,7 @@ const assert = require('@barchart/common-js/lang/assert'),
3
3
  DataType = require('@barchart/common-js/serialization/json/DataType'),
4
4
  Enum = require('@barchart/common-js/lang/Enum'),
5
5
  is = require('@barchart/common-js/lang/is'),
6
+ Schema = require('@barchart/common-js/serialization/json/Schema'),
6
7
  SchemaBuilder = require('@barchart/common-js/serialization/json/builders/SchemaBuilder'),
7
8
  Timezones = require('@barchart/common-js/lang/Timezones');
8
9
 
@@ -24,18 +25,37 @@ module.exports = (() => {
24
25
  this._schema = schema;
25
26
  }
26
27
 
28
+ /**
29
+ * @public
30
+ * @returns {Schema}
31
+ */
27
32
  get schema() {
28
33
  return this._schema;
29
34
  }
30
35
 
36
+ /**
37
+ * @static
38
+ * @public
39
+ * @returns {PortfolioSchema}
40
+ */
31
41
  static get CREATE() {
32
42
  return create;
33
43
  }
34
44
 
45
+ /**
46
+ * @static
47
+ * @public
48
+ * @returns {PortfolioSchema}
49
+ */
35
50
  static get COMPLETE() {
36
51
  return complete;
37
52
  }
38
53
 
54
+ /**
55
+ * @static
56
+ * @public
57
+ * @returns {PortfolioSchema}
58
+ */
39
59
  static get UPDATE() {
40
60
  return update;
41
61
  }
@@ -61,7 +81,7 @@ module.exports = (() => {
61
81
  .withField('legacy.warnings', DataType.NUMBER, true)
62
82
  .withField('legacy.drops', DataType.NUMBER, true)
63
83
  .withField('system.version', DataType.NUMBER, true)
64
- .withField('data', DataType.OBJECT, true)
84
+ .withField('miscellany', DataType.AD_HOC, true)
65
85
  .schema
66
86
  );
67
87
 
@@ -73,7 +93,7 @@ module.exports = (() => {
73
93
  .withField('defaults.currency', DataType.forEnum(Currency, 'Currency'))
74
94
  .withField('defaults.reinvest', DataType.BOOLEAN, true)
75
95
  .withField('defaults.valuation', DataType.forEnum(ValuationType, 'ValuationType'))
76
- .withField('data', DataType.OBJECT, true)
96
+ .withField('miscellany', DataType.AD_HOC, true)
77
97
  .schema
78
98
  );
79
99
 
@@ -84,7 +104,7 @@ module.exports = (() => {
84
104
  .withField('defaults.currency', DataType.forEnum(Currency, 'Currency'))
85
105
  .withField('defaults.reinvest', DataType.BOOLEAN, true)
86
106
  .withField('defaults.valuation', DataType.forEnum(ValuationType, 'ValuationType'))
87
- .withField('data', DataType.OBJECT, true)
107
+ .withField('miscellany', DataType.AD_HOC, true)
88
108
  .schema
89
109
  );
90
110
 
@@ -3,6 +3,7 @@ const assert = require('@barchart/common-js/lang/assert'),
3
3
  DataType = require('@barchart/common-js/serialization/json/DataType'),
4
4
  Enum = require('@barchart/common-js/lang/Enum'),
5
5
  is = require('@barchart/common-js/lang/is'),
6
+ Schema = require('@barchart/common-js/serialization/json/Schema'),
6
7
  SchemaBuilder = require('@barchart/common-js/serialization/json/builders/SchemaBuilder');
7
8
 
8
9
  const ValuationType = require('./../data/ValuationType');
@@ -23,10 +24,19 @@ module.exports = (() => {
23
24
  this._schema = schema;
24
25
  }
25
26
 
27
+ /**
28
+ * @public
29
+ * @returns {Schema}
30
+ */
26
31
  get schema() {
27
32
  return this._schema;
28
33
  }
29
34
 
35
+ /**
36
+ * @static
37
+ * @public
38
+ * @returns {PositionSchema}
39
+ */
30
40
  static get COMPLETE() {
31
41
  return complete;
32
42
  }
@@ -2,6 +2,7 @@ const assert = require('@barchart/common-js/lang/assert'),
2
2
  Currency = require('@barchart/common-js/lang/Currency'),
3
3
  DataType = require('@barchart/common-js/serialization/json/DataType'),
4
4
  Enum = require('@barchart/common-js/lang/Enum'),
5
+ Schema = require('@barchart/common-js/serialization/json/Schema'),
5
6
  SchemaBuilder = require('@barchart/common-js/serialization/json/builders/SchemaBuilder');
6
7
 
7
8
  const TransactionType = require('./../data/TransactionType');
@@ -22,6 +23,10 @@ module.exports = (() => {
22
23
  this._schema = schema;
23
24
  }
24
25
 
26
+ /**
27
+ * @public
28
+ * @returns {Schema}
29
+ */
25
30
  get schema() {
26
31
  return this._schema;
27
32
  }
@@ -30,6 +35,78 @@ module.exports = (() => {
30
35
  return complete;
31
36
  }
32
37
 
38
+ static get BUY() {
39
+ return buy;
40
+ }
41
+
42
+ static get SELL() {
43
+ return sell;
44
+ }
45
+
46
+ static get BUY_SHORT() {
47
+ return buyShort;
48
+ }
49
+
50
+ static get SELL_SHORT() {
51
+ return sellShort;
52
+ }
53
+
54
+ static get DIVIDEND() {
55
+ return dividend;
56
+ }
57
+
58
+ static get DIVIDEND_REINVEST() {
59
+ return dividendReinvest;
60
+ }
61
+
62
+ static get DIVIDEND_STOCK() {
63
+ return dividendStock;
64
+ }
65
+
66
+ static get DISTRIBUTION_CASH() {
67
+ return distributionCash;
68
+ }
69
+
70
+ static get DISTRIBUTION_FUND() {
71
+ return distributionFund;
72
+ }
73
+
74
+ static get SPLIT() {
75
+ return split;
76
+ }
77
+
78
+ static get FEE() {
79
+ return fee;
80
+ }
81
+
82
+ static get FEE_UNITS() {
83
+ return feeUnits;
84
+ }
85
+
86
+ static get DEPOSIT() {
87
+ return deposit;
88
+ }
89
+
90
+ static get WITHDRAWAL() {
91
+ return withdrawal;
92
+ }
93
+
94
+ static get DEBIT() {
95
+ return debit;
96
+ }
97
+
98
+ static get CREDIT() {
99
+ return credit;
100
+ }
101
+
102
+ static get VALUATION() {
103
+ return valuation;
104
+ }
105
+
106
+ static get INCOME() {
107
+ return income;
108
+ }
109
+
33
110
  toString() {
34
111
  return '[TransactionSchema]';
35
112
  }
@@ -75,5 +152,208 @@ module.exports = (() => {
75
152
  .schema
76
153
  );
77
154
 
155
+ const buy = new TransactionSchema(SchemaBuilder.withName('B')
156
+ .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
157
+ .withField('position', DataType.STRING, true)
158
+ .withField('instrument.name', DataType.STRING, true)
159
+ .withField('instrument.type', DataType.STRING, true)
160
+ .withField('instrument.currency', DataType.forEnum(Currency, 'Currency'), true)
161
+ .withField('instrument.symbol.barchart', DataType.STRING, true)
162
+ .withField('instrument.symbol.display', DataType.STRING, true)
163
+ .withField('currency', DataType.forEnum(Currency, 'Currency'))
164
+ .withField('date', DataType.DAY)
165
+ .withField('price', DataType.DECIMAL)
166
+ .withField('quantity', DataType.DECIMAL)
167
+ .withField('fee', DataType.DECIMAL, true)
168
+ .schema
169
+ );
170
+
171
+ const sell = new TransactionSchema(SchemaBuilder.withName('S')
172
+ .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
173
+ .withField('position', DataType.STRING, true)
174
+ .withField('currency', DataType.forEnum(Currency, 'Currency'))
175
+ .withField('date', DataType.DAY)
176
+ .withField('price', DataType.DECIMAL)
177
+ .withField('quantity', DataType.DECIMAL)
178
+ .withField('fee', DataType.DECIMAL, true)
179
+ .schema
180
+ );
181
+
182
+ const buyShort = new TransactionSchema(SchemaBuilder.withName('BS')
183
+ .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
184
+ .withField('position', DataType.STRING, true)
185
+ .withField('currency', DataType.forEnum(Currency, 'Currency'))
186
+ .withField('date', DataType.DAY)
187
+ .withField('price', DataType.DECIMAL)
188
+ .withField('quantity', DataType.DECIMAL)
189
+ .withField('fee', DataType.DECIMAL, true)
190
+ .schema
191
+ );
192
+
193
+ const sellShort = new TransactionSchema(SchemaBuilder.withName('SS')
194
+ .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
195
+ .withField('position', DataType.STRING, true)
196
+ .withField('currency', DataType.forEnum(Currency, 'Currency'))
197
+ .withField('date', DataType.DAY)
198
+ .withField('price', DataType.DECIMAL)
199
+ .withField('quantity', DataType.DECIMAL)
200
+ .withField('fee', DataType.DECIMAL, true)
201
+ .schema
202
+ );
203
+
204
+ const dividend = new TransactionSchema(SchemaBuilder.withName('DV')
205
+ .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
206
+ .withField('position', DataType.STRING, true)
207
+ .withField('currency', DataType.forEnum(Currency, 'Currency'))
208
+ .withField('date', DataType.DAY)
209
+ .withField('rate', DataType.DECIMAL)
210
+ .withField('open', DataType.DECIMAL, true)
211
+ .withField('effective', DataType.DAY, true)
212
+ .withField('fee', DataType.DECIMAL, true)
213
+ .schema
214
+ );
215
+
216
+ const dividendReinvest = new TransactionSchema(SchemaBuilder.withName('DX')
217
+ .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
218
+ .withField('position', DataType.STRING, true)
219
+ .withField('currency', DataType.forEnum(Currency, 'Currency'))
220
+ .withField('date', DataType.DAY)
221
+ .withField('rate', DataType.DECIMAL)
222
+ .withField('open', DataType.DECIMAL, true)
223
+ .withField('effective', DataType.DAY, true)
224
+ .withField('price', DataType.DECIMAL)
225
+ .withField('fee', DataType.DECIMAL, true)
226
+ .schema
227
+ );
228
+
229
+ const dividendStock = new TransactionSchema(SchemaBuilder.withName('DS')
230
+ .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
231
+ .withField('position', DataType.STRING, true)
232
+ .withField('currency', DataType.forEnum(Currency, 'Currency'))
233
+ .withField('date', DataType.DAY)
234
+ .withField('rate', DataType.DECIMAL)
235
+ .withField('open', DataType.DECIMAL, true)
236
+ .withField('effective', DataType.DAY, true)
237
+ .withField('price', DataType.DECIMAL)
238
+ .withField('fee', DataType.DECIMAL, true)
239
+ .schema
240
+ );
241
+
242
+ const distributionCash = new TransactionSchema(SchemaBuilder.withName('DC')
243
+ .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
244
+ .withField('position', DataType.STRING, true)
245
+ .withField('currency', DataType.forEnum(Currency, 'Currency'))
246
+ .withField('date', DataType.DAY)
247
+ .withField('rate', DataType.DECIMAL)
248
+ .withField('open', DataType.DECIMAL, true)
249
+ .withField('effective', DataType.DAY, true)
250
+ .withField('fee', DataType.DECIMAL, true)
251
+ .schema
252
+ );
253
+
254
+ const distributionFund = new TransactionSchema(SchemaBuilder.withName('DF')
255
+ .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
256
+ .withField('position', DataType.STRING, true)
257
+ .withField('currency', DataType.forEnum(Currency, 'Currency'))
258
+ .withField('date', DataType.DAY)
259
+ .withField('rate', DataType.DECIMAL)
260
+ .withField('open', DataType.DECIMAL, true)
261
+ .withField('effective', DataType.DAY, true)
262
+ .withField('fee', DataType.DECIMAL, true)
263
+ .schema
264
+ );
265
+
266
+ const split = new TransactionSchema(SchemaBuilder.withName('SP')
267
+ .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
268
+ .withField('position', DataType.STRING, true)
269
+ .withField('currency', DataType.forEnum(Currency, 'Currency'))
270
+ .withField('date', DataType.DAY)
271
+ .withField('numerator', DataType.DECIMAL)
272
+ .withField('denominator', DataType.DECIMAL)
273
+ .withField('effective', DataType.DAY, true)
274
+ .withField('fee', DataType.DECIMAL, true)
275
+ .schema
276
+ );
277
+
278
+ const fee = new TransactionSchema(SchemaBuilder.withName('F')
279
+ .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
280
+ .withField('position', DataType.STRING, true)
281
+ .withField('currency', DataType.forEnum(Currency, 'Currency'))
282
+ .withField('date', DataType.DAY)
283
+ .withField('fee', DataType.DECIMAL)
284
+ .schema
285
+ );
286
+
287
+ const feeUnits = new TransactionSchema(SchemaBuilder.withName('FU')
288
+ .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
289
+ .withField('position', DataType.STRING, true)
290
+ .withField('currency', DataType.forEnum(Currency, 'Currency'))
291
+ .withField('date', DataType.DAY)
292
+ .withField('fee', DataType.DECIMAL)
293
+ .withField('price', DataType.DECIMAL)
294
+ .schema
295
+ );
296
+
297
+ const deposit = new TransactionSchema(SchemaBuilder.withName('D')
298
+ .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
299
+ .withField('position', DataType.STRING, true)
300
+ .withField('currency', DataType.forEnum(Currency, 'Currency'))
301
+ .withField('date', DataType.DAY)
302
+ .withField('amount', DataType.DECIMAL)
303
+ .withField('fee', DataType.DECIMAL, true)
304
+ .schema
305
+ );
306
+
307
+ const withdrawal = new TransactionSchema(SchemaBuilder.withName('W')
308
+ .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
309
+ .withField('position', DataType.STRING, true)
310
+ .withField('currency', DataType.forEnum(Currency, 'Currency'))
311
+ .withField('date', DataType.DAY)
312
+ .withField('amount', DataType.DECIMAL)
313
+ .withField('fee', DataType.DECIMAL, true)
314
+ .schema
315
+ );
316
+
317
+ const debit = new TransactionSchema(SchemaBuilder.withName('DR')
318
+ .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
319
+ .withField('position', DataType.STRING, true)
320
+ .withField('currency', DataType.forEnum(Currency, 'Currency'))
321
+ .withField('date', DataType.DAY)
322
+ .withField('amount', DataType.DECIMAL)
323
+ .withField('fee', DataType.DECIMAL, true)
324
+ .schema
325
+ );
326
+
327
+ const credit = new TransactionSchema(SchemaBuilder.withName('CR')
328
+ .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
329
+ .withField('position', DataType.STRING, true)
330
+ .withField('currency', DataType.forEnum(Currency, 'Currency'))
331
+ .withField('date', DataType.DAY)
332
+ .withField('amount', DataType.DECIMAL)
333
+ .withField('fee', DataType.DECIMAL, true)
334
+ .schema
335
+ );
336
+
337
+ const valuation = new TransactionSchema(SchemaBuilder.withName('V')
338
+ .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
339
+ .withField('position', DataType.STRING, true)
340
+ .withField('currency', DataType.forEnum(Currency, 'Currency'))
341
+ .withField('date', DataType.DAY)
342
+ .withField('value', DataType.DECIMAL)
343
+ .withField('fee', DataType.DECIMAL, true)
344
+ .schema
345
+ );
346
+
347
+ const income = new TransactionSchema(SchemaBuilder.withName('I')
348
+ .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
349
+ .withField('position', DataType.STRING, true)
350
+ .withField('currency', DataType.forEnum(Currency, 'Currency'))
351
+ .withField('date', DataType.DAY)
352
+ .withField('income', DataType.DECIMAL)
353
+ .withField('fee', DataType.DECIMAL, true)
354
+ .schema
355
+ );
356
+
357
+
78
358
  return TransactionSchema;
79
359
  })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.0.17",
3
+ "version": "1.0.21",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",
@@ -14,6 +14,7 @@
14
14
  "devDependencies": {
15
15
  "babel-core": "^6.26.0",
16
16
  "babel-preset-es2015": "^6.24.1",
17
+ "babelify": "^8.0.0",
17
18
  "browserify": "^14.5.0",
18
19
  "git-get-status": "^1.0.5",
19
20
  "glob": "^6.0.1",