@barchart/portfolio-api-common 1.0.202 → 1.0.206
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.
- package/lib/api/failures/PortfolioFailureType.js +13 -0
- package/lib/data/InstrumentType.js +46 -5
- package/lib/formatters/TransactionFormatter.js +2 -1
- package/lib/serialization/TransactionSchema.js +0 -1
- package/package-lock.json +5484 -0
- package/package.json +3 -2
- package/test/SpecRunner.js +273 -10
|
@@ -12,6 +12,17 @@ module.exports = (() => {
|
|
|
12
12
|
constructor() {
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
+
/**
|
|
16
|
+
* The portfolio referenced does not exist.
|
|
17
|
+
*
|
|
18
|
+
* @public
|
|
19
|
+
* @static
|
|
20
|
+
* @returns {FailureType}
|
|
21
|
+
*/
|
|
22
|
+
static get POSITION_CREATE_FAILED_NO_PORTFOLIO() {
|
|
23
|
+
return positionCreateFailedNoPortfolio;
|
|
24
|
+
}
|
|
25
|
+
|
|
15
26
|
/**
|
|
16
27
|
* The transaction would occur before an existing transaction.
|
|
17
28
|
*
|
|
@@ -60,6 +71,8 @@ module.exports = (() => {
|
|
|
60
71
|
}
|
|
61
72
|
}
|
|
62
73
|
|
|
74
|
+
const positionCreateFailedNoPortfolio = new FailureType('POSITION_CREATE_FAILED_OUT_OF_SEQUENCE', 'Unable to create transaction. The referenced portfolio does not exist. Has it been deleted?');
|
|
75
|
+
|
|
63
76
|
const transactionCreateFailedOutOfSequence = new FailureType('TRANSACTION_CREATE_FAILED_OUT_OF_SEQUENCE', 'Unable to create transaction, because the transaction date is out-of-sequence. In other words, it would occur before an existing transaction. Please confirm your intent to re-write transaction history (which could take some time and alter the historical results for this position).');
|
|
64
77
|
const transactionCreateFailedDirectionSwitch = new FailureType('TRANSACTION_CREATE_FAILED_DIRECTION_SWITCH', 'Unable to create transaction, because the position direction would be switched (from long to short or vice versa). Please close the position (to a zero balance) first, then enter a second transaction.');
|
|
65
78
|
const transactionCreateRewriteUnsupported = new FailureType('TRANSACTION_CREATE_REWRITE_UNSUPPORTED', 'Unable to re-write transaction history. This operation is not currently supported (but will be implemented soon).');
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
const uuid = require('uuid');
|
|
2
|
+
|
|
1
3
|
const assert = require('@barchart/common-js/lang/assert'),
|
|
2
4
|
Enum = require('@barchart/common-js/lang/Enum');
|
|
3
5
|
|
|
@@ -16,16 +18,19 @@ module.exports = (() => {
|
|
|
16
18
|
* @param {Boolean} usesSymbols
|
|
17
19
|
*/
|
|
18
20
|
class InstrumentType extends Enum {
|
|
19
|
-
constructor(code, description, alternateDescription, canReinvest, usesSymbols) {
|
|
21
|
+
constructor(code, description, alternateDescription, canReinvest, usesSymbols, generator) {
|
|
20
22
|
super(code, description);
|
|
21
23
|
|
|
22
24
|
assert.argumentIsRequired(alternateDescription, 'alternateDescription', String);
|
|
23
25
|
assert.argumentIsRequired(canReinvest, 'canReinvest', Boolean);
|
|
24
26
|
assert.argumentIsRequired(usesSymbols, 'usesSymbols', Boolean);
|
|
27
|
+
assert.argumentIsRequired(generator, 'generator', Function);
|
|
25
28
|
|
|
26
29
|
this._alternateDescription = alternateDescription;
|
|
27
30
|
this._canReinvest = canReinvest;
|
|
28
31
|
this._usesSymbols = usesSymbols;
|
|
32
|
+
|
|
33
|
+
this._generator = generator;
|
|
29
34
|
}
|
|
30
35
|
|
|
31
36
|
/**
|
|
@@ -58,6 +63,23 @@ module.exports = (() => {
|
|
|
58
63
|
return this._usesSymbols;
|
|
59
64
|
}
|
|
60
65
|
|
|
66
|
+
/**
|
|
67
|
+
* Generates an identifier for the instrument.
|
|
68
|
+
*
|
|
69
|
+
* @public
|
|
70
|
+
* @param instrument
|
|
71
|
+
* @returns {String}
|
|
72
|
+
*/
|
|
73
|
+
generateIdentifier(instrument) {
|
|
74
|
+
assert.argumentIsRequired(instrument, 'instrument');
|
|
75
|
+
|
|
76
|
+
if (instrument.type !== this) {
|
|
77
|
+
throw new Error('Unable to generate instrument identifier for incompatible type.');
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return this._generator(instrument);
|
|
81
|
+
}
|
|
82
|
+
|
|
61
83
|
/**
|
|
62
84
|
* Cash.
|
|
63
85
|
*
|
|
@@ -102,15 +124,34 @@ module.exports = (() => {
|
|
|
102
124
|
return other;
|
|
103
125
|
}
|
|
104
126
|
|
|
127
|
+
/**
|
|
128
|
+
* Generates an identifier for the instrument.
|
|
129
|
+
*
|
|
130
|
+
* @static
|
|
131
|
+
* @public
|
|
132
|
+
* @param instrument
|
|
133
|
+
* @returns {String}
|
|
134
|
+
*/
|
|
135
|
+
static generateIdentifier(instrument) {
|
|
136
|
+
return map[instrument.type.code].generateIdentifier(instrument);
|
|
137
|
+
}
|
|
138
|
+
|
|
105
139
|
toString() {
|
|
106
140
|
return '[InstrumentType]';
|
|
107
141
|
}
|
|
108
142
|
}
|
|
109
143
|
|
|
110
|
-
const cash = new InstrumentType('CASH', 'cash', 'Cash', false, false);
|
|
111
|
-
const equity = new InstrumentType('EQUITY', 'equity', 'Equities', true, true);
|
|
112
|
-
const fund = new InstrumentType('FUND', 'mutual fund', 'Funds', true, true);
|
|
113
|
-
const other = new InstrumentType('OTHER', 'other', 'Other', false, false);
|
|
144
|
+
const cash = new InstrumentType('CASH', 'cash', 'Cash', false, false, (instrument) => `BARCHART-${instrument.type.code}-${instrument.currency.code}`);
|
|
145
|
+
const equity = new InstrumentType('EQUITY', 'equity', 'Equities', true, true, (instrument) => `BARCHART-${instrument.type.code}-${instrument.symbol.barchart}`);
|
|
146
|
+
const fund = new InstrumentType('FUND', 'mutual fund', 'Funds', true, true, (instrument) => `BARCHART-${instrument.type.code}-${instrument.symbol.barchart}`);
|
|
147
|
+
const other = new InstrumentType('OTHER', 'other', 'Other', false, false, (instrument) => `BARCHART-${instrument.type.code}-${uuid.v4()}`);
|
|
148
|
+
|
|
149
|
+
const map = { };
|
|
150
|
+
|
|
151
|
+
map[cash.code] = cash;
|
|
152
|
+
map[equity.code] = equity;
|
|
153
|
+
map[fund.code] = fund;
|
|
154
|
+
map[other.code] = other;
|
|
114
155
|
|
|
115
156
|
return InstrumentType;
|
|
116
157
|
})();
|
|
@@ -448,7 +448,6 @@ module.exports = (() => {
|
|
|
448
448
|
.withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
|
|
449
449
|
.withField('date', DataType.DAY)
|
|
450
450
|
.withField('value', DataType.DECIMAL)
|
|
451
|
-
.withField('fee', DataType.DECIMAL, true)
|
|
452
451
|
.withField('force', DataType.BOOLEAN, true)
|
|
453
452
|
.schema
|
|
454
453
|
);
|