@barchart/portfolio-api-common 1.0.36 → 1.0.37
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.
|
@@ -15,14 +15,16 @@ module.exports = (() => {
|
|
|
15
15
|
* @param {String} code
|
|
16
16
|
* @param {String} description
|
|
17
17
|
* @param {Function} rangeCalculator
|
|
18
|
+
* @param {Function} startDateCalculator
|
|
18
19
|
*/
|
|
19
20
|
class PositionSummaryFrame extends Enum {
|
|
20
|
-
constructor(code, description, rangeCalculator) {
|
|
21
|
+
constructor(code, description, rangeCalculator, startDateCalculator) {
|
|
21
22
|
super(code, description);
|
|
22
23
|
|
|
23
24
|
assert.argumentIsRequired(rangeCalculator, 'rangeCalculator', Function);
|
|
24
25
|
|
|
25
26
|
this._rangeCalculator = rangeCalculator;
|
|
27
|
+
this._startDateCalculator = startDateCalculator;
|
|
26
28
|
}
|
|
27
29
|
|
|
28
30
|
getRanges(transactions) {
|
|
@@ -31,6 +33,12 @@ module.exports = (() => {
|
|
|
31
33
|
return this._rangeCalculator(getFilteredTransactions(transactions));
|
|
32
34
|
}
|
|
33
35
|
|
|
36
|
+
getStartDate(periods) {
|
|
37
|
+
assert.argumentIsRequired(periods, 'periods', Number);
|
|
38
|
+
|
|
39
|
+
return this._startDateCalculator(periods);
|
|
40
|
+
}
|
|
41
|
+
|
|
34
42
|
/**
|
|
35
43
|
* A summary for a calendar year.
|
|
36
44
|
*
|
|
@@ -76,10 +84,10 @@ module.exports = (() => {
|
|
|
76
84
|
}
|
|
77
85
|
}
|
|
78
86
|
|
|
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);
|
|
87
|
+
const yearly = new PositionSummaryFrame('YEARLY', 'year', getYearlyRanges, getYearlyStartDate);
|
|
88
|
+
const quarterly = new PositionSummaryFrame('QUARTER', 'quarter', getQuarterlyRanges, getQuarterlyStartDate);
|
|
89
|
+
const monthly = new PositionSummaryFrame('MONTH', 'month', getMonthlyRanges, getMonthlyStartDate);
|
|
90
|
+
const ytd = new PositionSummaryFrame('YTD', 'year-to-date', getYearToDateRanges, getYearToDateStartDate);
|
|
83
91
|
|
|
84
92
|
function getRange(start, end) {
|
|
85
93
|
return {
|
|
@@ -142,6 +150,27 @@ module.exports = (() => {
|
|
|
142
150
|
return ranges;
|
|
143
151
|
}
|
|
144
152
|
|
|
153
|
+
function getYearlyStartDate(periods) {
|
|
154
|
+
const today = Day.getToday();
|
|
155
|
+
|
|
156
|
+
return Day.getToday()
|
|
157
|
+
.subtractMonths(today.month - 1)
|
|
158
|
+
.subtractDays(today.day)
|
|
159
|
+
.subtractYears(periods);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function getQuarterlyStartDate(periods) {
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function getMonthlyStartDate(periods) {
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function getYearToDateStartDate(periods) {
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
|
|
145
174
|
function getFilteredTransactions(transactions) {
|
|
146
175
|
return transactions.reduce((filtered, transaction) => {
|
|
147
176
|
if (!transaction.snapshot.open.getIsZero() || transaction.type.closing) {
|
|
@@ -56,15 +56,6 @@ module.exports = (() => {
|
|
|
56
56
|
return client;
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
/**
|
|
60
|
-
* @static
|
|
61
|
-
* @public
|
|
62
|
-
* @returns {PositionSummarySchema}
|
|
63
|
-
*/
|
|
64
|
-
static get DISPLAY() {
|
|
65
|
-
return display;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
59
|
toString() {
|
|
69
60
|
return '[PositionSummarySchema]';
|
|
70
61
|
}
|
|
@@ -132,37 +123,5 @@ module.exports = (() => {
|
|
|
132
123
|
.schema
|
|
133
124
|
);
|
|
134
125
|
|
|
135
|
-
const display = new PositionSummarySchema(SchemaBuilder.withName('display')
|
|
136
|
-
.withField('user', DataType.STRING)
|
|
137
|
-
.withField('portfolio', DataType.STRING)
|
|
138
|
-
.withField('position', DataType.STRING)
|
|
139
|
-
.withField('instrument.id', DataType.STRING)
|
|
140
|
-
.withField('instrument.name', DataType.STRING)
|
|
141
|
-
.withField('instrument.type', DataType.STRING)
|
|
142
|
-
.withField('instrument.currency', DataType.forEnum(Currency, 'Currency'))
|
|
143
|
-
.withField('instrument.delist', DataType.DAY, true)
|
|
144
|
-
.withField('instrument.symbol.barchart', DataType.STRING, true)
|
|
145
|
-
.withField('instrument.symbol.display', DataType.STRING, true)
|
|
146
|
-
.withField('frame', DataType.forEnum(PositionSummaryFrame, 'PositionSummaryFrame'))
|
|
147
|
-
.withField('start.date', DataType.DAY)
|
|
148
|
-
.withField('start.sequence', DataType.NUMBER)
|
|
149
|
-
.withField('start.open', DataType.DECIMAL)
|
|
150
|
-
.withField('start.basis', DataType.DECIMAL)
|
|
151
|
-
.withField('start.value', DataType.DECIMAL)
|
|
152
|
-
.withField('end.date', DataType.DAY)
|
|
153
|
-
.withField('end.sequence', DataType.NUMBER)
|
|
154
|
-
.withField('end.open', DataType.DECIMAL)
|
|
155
|
-
.withField('end.basis', DataType.DECIMAL)
|
|
156
|
-
.withField('end.value', DataType.DECIMAL)
|
|
157
|
-
.withField('period.buys', DataType.DECIMAL)
|
|
158
|
-
.withField('period.sells', DataType.DECIMAL)
|
|
159
|
-
.withField('period.income', DataType.DECIMAL)
|
|
160
|
-
.withField('period.realized', DataType.DECIMAL)
|
|
161
|
-
.withField('period.unrealized', DataType.DECIMAL)
|
|
162
|
-
.withField('additional.name', DataType.STRING)
|
|
163
|
-
.withField('additional.close', DataType.NUMBER)
|
|
164
|
-
.schema
|
|
165
|
-
);
|
|
166
|
-
|
|
167
126
|
return PositionSummarySchema;
|
|
168
127
|
})();
|
package/package.json
CHANGED
package/test/SpecRunner.js
CHANGED
|
@@ -16,14 +16,16 @@ module.exports = (() => {
|
|
|
16
16
|
* @param {String} code
|
|
17
17
|
* @param {String} description
|
|
18
18
|
* @param {Function} rangeCalculator
|
|
19
|
+
* @param {Function} startDateCalculator
|
|
19
20
|
*/
|
|
20
21
|
class PositionSummaryFrame extends Enum {
|
|
21
|
-
constructor(code, description, rangeCalculator) {
|
|
22
|
+
constructor(code, description, rangeCalculator, startDateCalculator) {
|
|
22
23
|
super(code, description);
|
|
23
24
|
|
|
24
25
|
assert.argumentIsRequired(rangeCalculator, 'rangeCalculator', Function);
|
|
25
26
|
|
|
26
27
|
this._rangeCalculator = rangeCalculator;
|
|
28
|
+
this._startDateCalculator = startDateCalculator;
|
|
27
29
|
}
|
|
28
30
|
|
|
29
31
|
getRanges(transactions) {
|
|
@@ -32,6 +34,12 @@ module.exports = (() => {
|
|
|
32
34
|
return this._rangeCalculator(getFilteredTransactions(transactions));
|
|
33
35
|
}
|
|
34
36
|
|
|
37
|
+
getStartDate(periods) {
|
|
38
|
+
assert.argumentIsRequired(periods, 'periods', Number);
|
|
39
|
+
|
|
40
|
+
return this._startDateCalculator(periods);
|
|
41
|
+
}
|
|
42
|
+
|
|
35
43
|
/**
|
|
36
44
|
* A summary for a calendar year.
|
|
37
45
|
*
|
|
@@ -77,10 +85,10 @@ module.exports = (() => {
|
|
|
77
85
|
}
|
|
78
86
|
}
|
|
79
87
|
|
|
80
|
-
const yearly = new PositionSummaryFrame('YEARLY', 'year', getYearlyRanges);
|
|
81
|
-
const quarterly = new PositionSummaryFrame('QUARTER', 'quarter', getQuarterlyRanges);
|
|
82
|
-
const monthly = new PositionSummaryFrame('MONTH', 'month', getMonthlyRanges);
|
|
83
|
-
const ytd = new PositionSummaryFrame('YTD', 'year-to-date', getYearToDateRanges);
|
|
88
|
+
const yearly = new PositionSummaryFrame('YEARLY', 'year', getYearlyRanges, getYearlyStartDate);
|
|
89
|
+
const quarterly = new PositionSummaryFrame('QUARTER', 'quarter', getQuarterlyRanges, getQuarterlyStartDate);
|
|
90
|
+
const monthly = new PositionSummaryFrame('MONTH', 'month', getMonthlyRanges, getMonthlyStartDate);
|
|
91
|
+
const ytd = new PositionSummaryFrame('YTD', 'year-to-date', getYearToDateRanges, getYearToDateStartDate);
|
|
84
92
|
|
|
85
93
|
function getRange(start, end) {
|
|
86
94
|
return {
|
|
@@ -143,6 +151,27 @@ module.exports = (() => {
|
|
|
143
151
|
return ranges;
|
|
144
152
|
}
|
|
145
153
|
|
|
154
|
+
function getYearlyStartDate(periods) {
|
|
155
|
+
const today = Day.getToday();
|
|
156
|
+
|
|
157
|
+
return Day.getToday()
|
|
158
|
+
.subtractMonths(today.month - 1)
|
|
159
|
+
.subtractDays(today.day)
|
|
160
|
+
.subtractYears(periods);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function getQuarterlyStartDate(periods) {
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function getMonthlyStartDate(periods) {
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function getYearToDateStartDate(periods) {
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
|
|
146
175
|
function getFilteredTransactions(transactions) {
|
|
147
176
|
return transactions.reduce((filtered, transaction) => {
|
|
148
177
|
if (!transaction.snapshot.open.getIsZero() || transaction.type.closing) {
|
|
@@ -3963,6 +3992,48 @@ describe('After the PositionSummaryFrame enumeration is initialized', () => {
|
|
|
3963
3992
|
expect(ranges[0].end.format()).toEqual('2018-12-31');
|
|
3964
3993
|
});
|
|
3965
3994
|
});
|
|
3995
|
+
|
|
3996
|
+
describe('and getting the start date for yearly frames', () => {
|
|
3997
|
+
describe('for one year ago', function() {
|
|
3998
|
+
let start;
|
|
3999
|
+
|
|
4000
|
+
beforeEach(() => {
|
|
4001
|
+
start = PositionSummaryFrame.YEARLY.getStartDate(1);
|
|
4002
|
+
});
|
|
4003
|
+
|
|
4004
|
+
it('should be in December', () => {
|
|
4005
|
+
expect(start.month).toEqual(12);
|
|
4006
|
+
});
|
|
4007
|
+
|
|
4008
|
+
it('should be on the 31st', () => {
|
|
4009
|
+
expect(start.day).toEqual(31);
|
|
4010
|
+
});
|
|
4011
|
+
|
|
4012
|
+
it('should be two years ago', () => {
|
|
4013
|
+
expect(start.year).toEqual(Day.getToday().year - 2);
|
|
4014
|
+
});
|
|
4015
|
+
});
|
|
4016
|
+
|
|
4017
|
+
describe('for two years ago', function() {
|
|
4018
|
+
let start;
|
|
4019
|
+
|
|
4020
|
+
beforeEach(() => {
|
|
4021
|
+
start = PositionSummaryFrame.YEARLY.getStartDate(2);
|
|
4022
|
+
});
|
|
4023
|
+
|
|
4024
|
+
it('should be in December', () => {
|
|
4025
|
+
expect(start.month).toEqual(12);
|
|
4026
|
+
});
|
|
4027
|
+
|
|
4028
|
+
it('should be on the 31st', () => {
|
|
4029
|
+
expect(start.day).toEqual(31);
|
|
4030
|
+
});
|
|
4031
|
+
|
|
4032
|
+
it('should be two years ago', () => {
|
|
4033
|
+
expect(start.year).toEqual(Day.getToday().year - 3);
|
|
4034
|
+
});
|
|
4035
|
+
});
|
|
4036
|
+
});
|
|
3966
4037
|
});
|
|
3967
4038
|
|
|
3968
4039
|
},{"./../../../lib/data/PositionSummaryFrame":1,"./../../../lib/data/TransactionType":2,"@barchart/common-js/lang/Day":5,"@barchart/common-js/lang/Decimal":6}]},{},[12]);
|
|
@@ -310,4 +310,46 @@ describe('After the PositionSummaryFrame enumeration is initialized', () => {
|
|
|
310
310
|
expect(ranges[0].end.format()).toEqual('2018-12-31');
|
|
311
311
|
});
|
|
312
312
|
});
|
|
313
|
+
|
|
314
|
+
describe('and getting the start date for yearly frames', () => {
|
|
315
|
+
describe('for one year ago', function() {
|
|
316
|
+
let start;
|
|
317
|
+
|
|
318
|
+
beforeEach(() => {
|
|
319
|
+
start = PositionSummaryFrame.YEARLY.getStartDate(1);
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
it('should be in December', () => {
|
|
323
|
+
expect(start.month).toEqual(12);
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
it('should be on the 31st', () => {
|
|
327
|
+
expect(start.day).toEqual(31);
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
it('should be two years ago', () => {
|
|
331
|
+
expect(start.year).toEqual(Day.getToday().year - 2);
|
|
332
|
+
});
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
describe('for two years ago', function() {
|
|
336
|
+
let start;
|
|
337
|
+
|
|
338
|
+
beforeEach(() => {
|
|
339
|
+
start = PositionSummaryFrame.YEARLY.getStartDate(2);
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
it('should be in December', () => {
|
|
343
|
+
expect(start.month).toEqual(12);
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
it('should be on the 31st', () => {
|
|
347
|
+
expect(start.day).toEqual(31);
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
it('should be two years ago', () => {
|
|
351
|
+
expect(start.year).toEqual(Day.getToday().year - 3);
|
|
352
|
+
});
|
|
353
|
+
});
|
|
354
|
+
});
|
|
313
355
|
});
|