@barchart/portfolio-api-common 1.2.54 → 1.2.58
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.
|
@@ -19,10 +19,11 @@ module.exports = (() => {
|
|
|
19
19
|
* @param {Boolean} canSwitchDirection
|
|
20
20
|
* @param {Boolean} usesSymbols
|
|
21
21
|
* @param {Boolean} hasCorporateActions
|
|
22
|
+
* @param {Number} closeFractional
|
|
22
23
|
* @param {Function} generator
|
|
23
24
|
*/
|
|
24
25
|
class InstrumentType extends Enum {
|
|
25
|
-
constructor(code, description, alternateDescription, canReinvest, canShort, canSwitchDirection, usesSymbols, hasCorporateActions, generator) {
|
|
26
|
+
constructor(code, description, alternateDescription, canReinvest, canShort, canSwitchDirection, usesSymbols, hasCorporateActions, closeFractional, generator) {
|
|
26
27
|
super(code, description);
|
|
27
28
|
|
|
28
29
|
assert.argumentIsRequired(alternateDescription, 'alternateDescription', String);
|
|
@@ -31,6 +32,7 @@ module.exports = (() => {
|
|
|
31
32
|
assert.argumentIsRequired(canSwitchDirection, 'canSwitchDirection', Boolean);
|
|
32
33
|
assert.argumentIsRequired(usesSymbols, 'usesSymbols', Boolean);
|
|
33
34
|
assert.argumentIsRequired(hasCorporateActions, 'hasCorporateActions', Boolean);
|
|
35
|
+
assert.argumentIsRequired(closeFractional, 'closeFractional', Boolean);
|
|
34
36
|
assert.argumentIsRequired(generator, 'generator', Function);
|
|
35
37
|
|
|
36
38
|
this._alternateDescription = alternateDescription;
|
|
@@ -39,6 +41,7 @@ module.exports = (() => {
|
|
|
39
41
|
this._canSwitchDirection = canSwitchDirection;
|
|
40
42
|
this._usesSymbols = usesSymbols;
|
|
41
43
|
this._hasCorporateActions = hasCorporateActions;
|
|
44
|
+
this._closeFractional = closeFractional;
|
|
42
45
|
|
|
43
46
|
this._generator = generator;
|
|
44
47
|
}
|
|
@@ -104,6 +107,17 @@ module.exports = (() => {
|
|
|
104
107
|
return this._hasCorporateActions;
|
|
105
108
|
}
|
|
106
109
|
|
|
110
|
+
/**
|
|
111
|
+
* Indicates if fractional shares should be closed when the position
|
|
112
|
+
* size is less than one (or some of the fractional shares are closed).
|
|
113
|
+
*
|
|
114
|
+
* @public
|
|
115
|
+
* @returns {Boolean}
|
|
116
|
+
*/
|
|
117
|
+
get closeFractional() {
|
|
118
|
+
return this._closeFractional;
|
|
119
|
+
}
|
|
120
|
+
|
|
107
121
|
/**
|
|
108
122
|
* Generates an identifier for the instrument.
|
|
109
123
|
*
|
|
@@ -201,10 +215,10 @@ module.exports = (() => {
|
|
|
201
215
|
}
|
|
202
216
|
}
|
|
203
217
|
|
|
204
|
-
const cash = new InstrumentType('CASH', 'cash', 'Cash', false, false, true, false, false, (instrument) => `BARCHART-${instrument.type.code}-${instrument.currency.code}`);
|
|
205
|
-
const equity = new InstrumentType('EQUITY', 'equity', 'Equities', true, true, false, true, true, (instrument) => `BARCHART-${instrument.type.code}-${instrument.symbol.barchart}`);
|
|
206
|
-
const fund = new InstrumentType('FUND', 'mutual fund', 'Funds', true, false, false, true, true, (instrument) => `BARCHART-${instrument.type.code}-${instrument.symbol.barchart}`);
|
|
207
|
-
const other = new InstrumentType('OTHER', 'other', 'Other', false, false, false, false, false, (instrument) => `BARCHART-${instrument.type.code}-${uuid.v4()}`);
|
|
218
|
+
const cash = new InstrumentType('CASH', 'cash', 'Cash', false, false, true, false, false, false, (instrument) => `BARCHART-${instrument.type.code}-${instrument.currency.code}`);
|
|
219
|
+
const equity = new InstrumentType('EQUITY', 'equity', 'Equities', true, true, false, true, true, true, (instrument) => `BARCHART-${instrument.type.code}-${instrument.symbol.barchart}`);
|
|
220
|
+
const fund = new InstrumentType('FUND', 'mutual fund', 'Funds', true, false, false, true, true, false, (instrument) => `BARCHART-${instrument.type.code}-${instrument.symbol.barchart}`);
|
|
221
|
+
const other = new InstrumentType('OTHER', 'other', 'Other', false, false, false, false, false, false, (instrument) => `BARCHART-${instrument.type.code}-${uuid.v4()}`);
|
|
208
222
|
|
|
209
223
|
const map = { };
|
|
210
224
|
|
|
@@ -26,10 +26,11 @@ module.exports = (() => {
|
|
|
26
26
|
* @public
|
|
27
27
|
* @static
|
|
28
28
|
* @param {Array.<Object>} transactions
|
|
29
|
+
* @param {Boolean=} strict
|
|
29
30
|
* @returns {Boolean}
|
|
30
31
|
*/
|
|
31
|
-
static validateOrder(transactions) {
|
|
32
|
-
return TransactionValidator.getInvalidIndex(transactions) < 0;
|
|
32
|
+
static validateOrder(transactions, strict) {
|
|
33
|
+
return TransactionValidator.getInvalidIndex(transactions, strict) < 0;
|
|
33
34
|
}
|
|
34
35
|
|
|
35
36
|
/**
|
|
@@ -77,12 +78,14 @@ module.exports = (() => {
|
|
|
77
78
|
* @public
|
|
78
79
|
* @static
|
|
79
80
|
* @param {Array.<Object>} transactions
|
|
81
|
+
* @param {Boolean=} strict
|
|
80
82
|
* @returns {Number}
|
|
81
83
|
*/
|
|
82
|
-
static getInvalidIndex(transactions) {
|
|
84
|
+
static getInvalidIndex(transactions, strict) {
|
|
83
85
|
assert.argumentIsArray(transactions, 'transactions');
|
|
86
|
+
assert.argumentIsOptional(strict, 'strict', Boolean);
|
|
84
87
|
|
|
85
|
-
return transactions.findIndex((t, i, a) => t.sequence !== (i + 1) || (i !== 0 && t.date.getIsBefore(a[ i - 1 ].date)) || (i !== 0 && t.date.getIsEqual(a[i - 1].date) && t.type.sequence < a[i - 1].type.sequence));
|
|
88
|
+
return transactions.findIndex((t, i, a) => t.sequence !== (i + 1) || (i !== 0 && t.date.getIsBefore(a[ i - 1 ].date)) || (i !== 0 && is.boolean(strict) && strict && t.date.getIsEqual(a[i - 1].date) && t.type.sequence < a[i - 1].type.sequence));
|
|
86
89
|
}
|
|
87
90
|
|
|
88
91
|
/**
|
package/package.json
CHANGED
package/test/SpecRunner.js
CHANGED
|
@@ -20,10 +20,11 @@ module.exports = (() => {
|
|
|
20
20
|
* @param {Boolean} canSwitchDirection
|
|
21
21
|
* @param {Boolean} usesSymbols
|
|
22
22
|
* @param {Boolean} hasCorporateActions
|
|
23
|
+
* @param {Number} closeFractional
|
|
23
24
|
* @param {Function} generator
|
|
24
25
|
*/
|
|
25
26
|
class InstrumentType extends Enum {
|
|
26
|
-
constructor(code, description, alternateDescription, canReinvest, canShort, canSwitchDirection, usesSymbols, hasCorporateActions, generator) {
|
|
27
|
+
constructor(code, description, alternateDescription, canReinvest, canShort, canSwitchDirection, usesSymbols, hasCorporateActions, closeFractional, generator) {
|
|
27
28
|
super(code, description);
|
|
28
29
|
|
|
29
30
|
assert.argumentIsRequired(alternateDescription, 'alternateDescription', String);
|
|
@@ -32,6 +33,7 @@ module.exports = (() => {
|
|
|
32
33
|
assert.argumentIsRequired(canSwitchDirection, 'canSwitchDirection', Boolean);
|
|
33
34
|
assert.argumentIsRequired(usesSymbols, 'usesSymbols', Boolean);
|
|
34
35
|
assert.argumentIsRequired(hasCorporateActions, 'hasCorporateActions', Boolean);
|
|
36
|
+
assert.argumentIsRequired(closeFractional, 'closeFractional', Boolean);
|
|
35
37
|
assert.argumentIsRequired(generator, 'generator', Function);
|
|
36
38
|
|
|
37
39
|
this._alternateDescription = alternateDescription;
|
|
@@ -40,6 +42,7 @@ module.exports = (() => {
|
|
|
40
42
|
this._canSwitchDirection = canSwitchDirection;
|
|
41
43
|
this._usesSymbols = usesSymbols;
|
|
42
44
|
this._hasCorporateActions = hasCorporateActions;
|
|
45
|
+
this._closeFractional = closeFractional;
|
|
43
46
|
|
|
44
47
|
this._generator = generator;
|
|
45
48
|
}
|
|
@@ -105,6 +108,17 @@ module.exports = (() => {
|
|
|
105
108
|
return this._hasCorporateActions;
|
|
106
109
|
}
|
|
107
110
|
|
|
111
|
+
/**
|
|
112
|
+
* Indicates if fractional shares should be closed when the position
|
|
113
|
+
* size is less than one (or some of the fractional shares are closed).
|
|
114
|
+
*
|
|
115
|
+
* @public
|
|
116
|
+
* @returns {Boolean}
|
|
117
|
+
*/
|
|
118
|
+
get closeFractional() {
|
|
119
|
+
return this._closeFractional;
|
|
120
|
+
}
|
|
121
|
+
|
|
108
122
|
/**
|
|
109
123
|
* Generates an identifier for the instrument.
|
|
110
124
|
*
|
|
@@ -202,10 +216,10 @@ module.exports = (() => {
|
|
|
202
216
|
}
|
|
203
217
|
}
|
|
204
218
|
|
|
205
|
-
const cash = new InstrumentType('CASH', 'cash', 'Cash', false, false, true, false, false, (instrument) => `BARCHART-${instrument.type.code}-${instrument.currency.code}`);
|
|
206
|
-
const equity = new InstrumentType('EQUITY', 'equity', 'Equities', true, true, false, true, true, (instrument) => `BARCHART-${instrument.type.code}-${instrument.symbol.barchart}`);
|
|
207
|
-
const fund = new InstrumentType('FUND', 'mutual fund', 'Funds', true, false, false, true, true, (instrument) => `BARCHART-${instrument.type.code}-${instrument.symbol.barchart}`);
|
|
208
|
-
const other = new InstrumentType('OTHER', 'other', 'Other', false, false, false, false, false, (instrument) => `BARCHART-${instrument.type.code}-${uuid.v4()}`);
|
|
219
|
+
const cash = new InstrumentType('CASH', 'cash', 'Cash', false, false, true, false, false, false, (instrument) => `BARCHART-${instrument.type.code}-${instrument.currency.code}`);
|
|
220
|
+
const equity = new InstrumentType('EQUITY', 'equity', 'Equities', true, true, false, true, true, true, (instrument) => `BARCHART-${instrument.type.code}-${instrument.symbol.barchart}`);
|
|
221
|
+
const fund = new InstrumentType('FUND', 'mutual fund', 'Funds', true, false, false, true, true, false, (instrument) => `BARCHART-${instrument.type.code}-${instrument.symbol.barchart}`);
|
|
222
|
+
const other = new InstrumentType('OTHER', 'other', 'Other', false, false, false, false, false, false, (instrument) => `BARCHART-${instrument.type.code}-${uuid.v4()}`);
|
|
209
223
|
|
|
210
224
|
const map = { };
|
|
211
225
|
|
|
@@ -1069,10 +1083,11 @@ module.exports = (() => {
|
|
|
1069
1083
|
* @public
|
|
1070
1084
|
* @static
|
|
1071
1085
|
* @param {Array.<Object>} transactions
|
|
1086
|
+
* @param {Boolean=} strict
|
|
1072
1087
|
* @returns {Boolean}
|
|
1073
1088
|
*/
|
|
1074
|
-
static validateOrder(transactions) {
|
|
1075
|
-
return TransactionValidator.getInvalidIndex(transactions) < 0;
|
|
1089
|
+
static validateOrder(transactions, strict) {
|
|
1090
|
+
return TransactionValidator.getInvalidIndex(transactions, strict) < 0;
|
|
1076
1091
|
}
|
|
1077
1092
|
|
|
1078
1093
|
/**
|
|
@@ -1120,12 +1135,14 @@ module.exports = (() => {
|
|
|
1120
1135
|
* @public
|
|
1121
1136
|
* @static
|
|
1122
1137
|
* @param {Array.<Object>} transactions
|
|
1138
|
+
* @param {Boolean=} strict
|
|
1123
1139
|
* @returns {Number}
|
|
1124
1140
|
*/
|
|
1125
|
-
static getInvalidIndex(transactions) {
|
|
1141
|
+
static getInvalidIndex(transactions, strict) {
|
|
1126
1142
|
assert.argumentIsArray(transactions, 'transactions');
|
|
1143
|
+
assert.argumentIsOptional(strict, 'strict', Boolean);
|
|
1127
1144
|
|
|
1128
|
-
return transactions.findIndex((t, i, a) => t.sequence !== (i + 1) || (i !== 0 && t.date.getIsBefore(a[ i - 1 ].date)) || (i !== 0 && t.date.getIsEqual(a[i - 1].date) && t.type.sequence < a[i - 1].type.sequence));
|
|
1145
|
+
return transactions.findIndex((t, i, a) => t.sequence !== (i + 1) || (i !== 0 && t.date.getIsBefore(a[ i - 1 ].date)) || (i !== 0 && is.boolean(strict) && strict && t.date.getIsEqual(a[i - 1].date) && t.type.sequence < a[i - 1].type.sequence));
|
|
1129
1146
|
}
|
|
1130
1147
|
|
|
1131
1148
|
/**
|
|
@@ -17234,8 +17251,12 @@ describe('When validating transaction order', () => {
|
|
|
17234
17251
|
expect(TransactionValidator.validateOrder([ build(1, '2018-04-30'), build(2, '2018-04-30', TransactionType.DIVIDEND) ])).toEqual(true);
|
|
17235
17252
|
});
|
|
17236
17253
|
|
|
17237
|
-
it('An array of transactions with ordered sequences, on the same day should be valid, where a dividend occurs first, should not be valid', () => {
|
|
17238
|
-
expect(TransactionValidator.validateOrder([ build(1, '2018-04-30', TransactionType.DIVIDEND), build(2, '2018-04-30') ])).toEqual(false);
|
|
17254
|
+
it('An array of transactions with ordered sequences, on the same day should be valid, where a dividend occurs first, in strict mode, should not be valid', () => {
|
|
17255
|
+
expect(TransactionValidator.validateOrder([ build(1, '2018-04-30', TransactionType.DIVIDEND), build(2, '2018-04-30') ], true)).toEqual(false);
|
|
17256
|
+
});
|
|
17257
|
+
|
|
17258
|
+
it('An array of transactions with ordered sequences, on the same day should be valid, where a dividend occurs first, in non-strict mode, should be valid', () => {
|
|
17259
|
+
expect(TransactionValidator.validateOrder([ build(1, '2018-04-30', TransactionType.DIVIDEND), build(2, '2018-04-30') ], false)).toEqual(true);
|
|
17239
17260
|
});
|
|
17240
17261
|
|
|
17241
17262
|
it('An array of transactions with ordered sequences, on the sequential days should be valid', () => {
|
|
@@ -22,8 +22,12 @@ describe('When validating transaction order', () => {
|
|
|
22
22
|
expect(TransactionValidator.validateOrder([ build(1, '2018-04-30'), build(2, '2018-04-30', TransactionType.DIVIDEND) ])).toEqual(true);
|
|
23
23
|
});
|
|
24
24
|
|
|
25
|
-
it('An array of transactions with ordered sequences, on the same day should be valid, where a dividend occurs first, should not be valid', () => {
|
|
26
|
-
expect(TransactionValidator.validateOrder([ build(1, '2018-04-30', TransactionType.DIVIDEND), build(2, '2018-04-30') ])).toEqual(false);
|
|
25
|
+
it('An array of transactions with ordered sequences, on the same day should be valid, where a dividend occurs first, in strict mode, should not be valid', () => {
|
|
26
|
+
expect(TransactionValidator.validateOrder([ build(1, '2018-04-30', TransactionType.DIVIDEND), build(2, '2018-04-30') ], true)).toEqual(false);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('An array of transactions with ordered sequences, on the same day should be valid, where a dividend occurs first, in non-strict mode, should be valid', () => {
|
|
30
|
+
expect(TransactionValidator.validateOrder([ build(1, '2018-04-30', TransactionType.DIVIDEND), build(2, '2018-04-30') ], false)).toEqual(true);
|
|
27
31
|
});
|
|
28
32
|
|
|
29
33
|
it('An array of transactions with ordered sequences, on the sequential days should be valid', () => {
|