@barchart/portfolio-api-common 1.0.213 → 1.0.217

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.
@@ -34,6 +34,18 @@ module.exports = (() => {
34
34
  return transactionCreateFailedOutOfSequence;
35
35
  }
36
36
 
37
+ /**
38
+ * The transaction type is not valid, given the current position size
39
+ * (e.g. sell is invalid for a short position, sell short must be used).
40
+ *
41
+ * @public
42
+ * @static
43
+ * @returns {FailureType}
44
+ */
45
+ static get TRANSACTION_CREATE_FAILED_TYPE_INVALID_FOR_POSITION() {
46
+ return transactionCreateFailedTypeInvalidForPosition;
47
+ }
48
+
37
49
  /**
38
50
  * The transaction would cause the position to change (from long to
39
51
  * short, or vice versa).
@@ -74,7 +86,8 @@ module.exports = (() => {
74
86
  const positionCreateFailedNoPortfolio = new FailureType('POSITION_CREATE_FAILED_NO_PORTFOLIO', 'Unable to create transaction. The referenced portfolio does not exist. Has it been deleted?');
75
87
 
76
88
  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).');
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.');
89
+ const transactionCreateFailedTypeInvalidForPosition = new FailureType('TRANSACTION_CREATE_FAILED_INVALID_FOR_POSITION', 'A {L|type.description} are not allowed. At the time of the transaction, your position is {L|direction} (i.e. {L|sign}).');
90
+ 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), then enter a second transaction.');
78
91
  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).');
79
92
 
80
93
  const transactionDeleteFailedOutOfSequence = new FailureType('TRANSACTION_DELETE_FAILED_OUT_OF_SEQUENCE', 'Deleting any transaction, except for the most recent, will cause transaction history to be re-written. Please confirm your intent to re-write transaction history (which could take some time and alter the historical results for this position).');
@@ -15,19 +15,26 @@ module.exports = (() => {
15
15
  * @param {String} alternateDescription
16
16
  * @param {String} code
17
17
  * @param {Boolean} canReinvest
18
+ * @param {Boolean} canShort
19
+ * @param {Boolean} canSwitchDirection
18
20
  * @param {Boolean} usesSymbols
21
+ * @param {Function} usesSymbols
19
22
  */
20
23
  class InstrumentType extends Enum {
21
- constructor(code, description, alternateDescription, canReinvest, usesSymbols, generator) {
24
+ constructor(code, description, alternateDescription, canReinvest, canShort, canSwitchDirection, usesSymbols, generator) {
22
25
  super(code, description);
23
26
 
24
27
  assert.argumentIsRequired(alternateDescription, 'alternateDescription', String);
25
28
  assert.argumentIsRequired(canReinvest, 'canReinvest', Boolean);
29
+ assert.argumentIsRequired(canShort, 'canShort', Boolean);
30
+ assert.argumentIsRequired(canSwitchDirection, 'canSwitchDirection', Boolean);
26
31
  assert.argumentIsRequired(usesSymbols, 'usesSymbols', Boolean);
27
32
  assert.argumentIsRequired(generator, 'generator', Function);
28
33
 
29
34
  this._alternateDescription = alternateDescription;
30
35
  this._canReinvest = canReinvest;
36
+ this._canShort = canShort;
37
+ this._canSwitchDirection = canSwitchDirection;
31
38
  this._usesSymbols = usesSymbols;
32
39
 
33
40
  this._generator = generator;
@@ -53,6 +60,27 @@ module.exports = (() => {
53
60
  return this._canReinvest;
54
61
  }
55
62
 
63
+ /**
64
+ * Indicates if short-selling is possible for this instrument type.
65
+ *
66
+ * @public
67
+ * @returns {Boolean}
68
+ */
69
+ get canShort() {
70
+ return this._canShort;
71
+ }
72
+
73
+ /**
74
+ * Indicates if one transaction is allowed to switch a position size from
75
+ * positive to negative (or vice versa).
76
+ *
77
+ * @public
78
+ * @returns {Boolean}
79
+ */
80
+ get canSwitchDirection() {
81
+ return this._canSwitchDirection;
82
+ }
83
+
56
84
  /**
57
85
  * Indicates if an instrument of this type can be represented by a symbol.
58
86
  *
@@ -141,10 +169,10 @@ module.exports = (() => {
141
169
  }
142
170
  }
143
171
 
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()}`);
172
+ const cash = new InstrumentType('CASH', 'cash', 'Cash', false, false, true, false, (instrument) => `BARCHART-${instrument.type.code}-${instrument.currency.code}`);
173
+ const equity = new InstrumentType('EQUITY', 'equity', 'Equities', true, true, false, true, (instrument) => `BARCHART-${instrument.type.code}-${instrument.symbol.barchart}`);
174
+ const fund = new InstrumentType('FUND', 'mutual fund', 'Funds', true, false, false, true, (instrument) => `BARCHART-${instrument.type.code}-${instrument.symbol.barchart}`);
175
+ const other = new InstrumentType('OTHER', 'other', 'Other', false, false, false, false, (instrument) => `BARCHART-${instrument.type.code}-${uuid.v4()}`);
148
176
 
149
177
  const map = { };
150
178
 
@@ -0,0 +1,142 @@
1
+ const assert = require('@barchart/common-js/lang/assert'),
2
+ Decimal = require('@barchart/common-js/lang/Decimal'),
3
+ Enum = require('@barchart/common-js/lang/Enum');
4
+
5
+ module.exports = (() => {
6
+ 'use strict';
7
+
8
+ /**
9
+ * Describes a position size -- positive values are long, negative values
10
+ * are short and zero values are even.
11
+ *
12
+ * @public
13
+ * @extends {Enum}
14
+ * @param {String} code
15
+ * @param {String} description
16
+ * @param {sign} sign
17
+ */
18
+ class PositionDirection extends Enum {
19
+ constructor(code, description, sign) {
20
+ super(code, description);
21
+
22
+ assert.argumentIsString(sign, 'sign', String);
23
+
24
+ this._sign = sign;
25
+ }
26
+
27
+ /**
28
+ * A description of the positiveness or negativeness of the size of the
29
+ * position.
30
+ *
31
+ * @public
32
+ * @returns {String}
33
+ */
34
+ get sign() {
35
+ return this._sign;
36
+ }
37
+
38
+ /**
39
+ * Indicates if the position size is positive (i.e. is {@link PositionDirection.LONG}).
40
+ *
41
+ * @public
42
+ * @returns {boolean}
43
+ */
44
+ get positive() {
45
+ return this === long;
46
+ }
47
+
48
+ /**
49
+ * Indicates if the position size is negative (i.e. is {@link PositionDirection.SHORT}).
50
+ *
51
+ * @public
52
+ * @returns {boolean}
53
+ */
54
+ get negative() {
55
+ return this === short;
56
+ }
57
+
58
+ /**
59
+ * Indicates if the position size is zero (i.e. is {@link PositionDirection.EVEN}).
60
+ *
61
+ * @public
62
+ * @returns {boolean}
63
+ */
64
+ get closed() {
65
+ return this === even;
66
+ }
67
+
68
+ /**
69
+ * A positive quantity position.
70
+ *
71
+ * @public
72
+ * @static
73
+ * @returns {PositionDirection}
74
+ */
75
+ static get LONG() {
76
+ return long;
77
+ }
78
+
79
+ /**
80
+ * A positive quantity position.
81
+ *
82
+ * @public
83
+ * @static
84
+ * @returns {PositionDirection}
85
+ */
86
+ static get SHORT() {
87
+ return short;
88
+ }
89
+
90
+ /**
91
+ * A zero quantity position.
92
+ *
93
+ * @public
94
+ * @static
95
+ * @returns {PositionDirection}
96
+ */
97
+ static get EVEN() {
98
+ return even;
99
+ }
100
+
101
+ /**
102
+ * Given an open quantity, returns a {@link PositionDirection} that
103
+ * describes the quantity.
104
+ *
105
+ * @public
106
+ * @static
107
+ * @param {Decimal} open
108
+ * @returns {PositionDirection}
109
+ */
110
+ static for(open) {
111
+ assert.argumentIsRequired(open, 'open', Decimal, 'Decimal');
112
+
113
+ if (open.getIsPositive()) {
114
+ return long;
115
+ } else if (open.getIsNegative()) {
116
+ return short;
117
+ } else {
118
+ return even;
119
+ }
120
+ }
121
+
122
+ /**
123
+ * Returns true of the direction switched (from long to short, or
124
+ * short to long).
125
+ *
126
+ * @public
127
+ * @static
128
+ * @param {PositionDirection} a
129
+ * @param {PositionDirection} b
130
+ * @returns {Boolean}
131
+ */
132
+ static switched(a, b) {
133
+ return !a.closed && !b.closed && a.positive !== b.positive;
134
+ }
135
+ }
136
+
137
+ const long = new PositionDirection('LONG', 'Long', 'positive');
138
+ const short = new PositionDirection('SHORT', 'Short', 'negative');
139
+ const even = new PositionDirection('EVEN', 'Even', 'zero');
140
+
141
+ return PositionDirection;
142
+ })();
@@ -7,6 +7,7 @@ const assert = require('@barchart/common-js/lang/assert'),
7
7
  SchemaBuilder = require('@barchart/common-js/serialization/json/builders/SchemaBuilder');
8
8
 
9
9
  const InstrumentType = require('./../data/InstrumentType'),
10
+ PositionDirection = require('./../data/PositionDirection'),
10
11
  ValuationType = require('./../data/ValuationType');
11
12
 
12
13
  module.exports = (() => {
@@ -79,6 +80,7 @@ module.exports = (() => {
79
80
  .withField('reinvest', DataType.BOOLEAN)
80
81
  .withField('snapshot.date', DataType.DAY)
81
82
  .withField('snapshot.open', DataType.DECIMAL)
83
+ .withField('snapshot.direction', DataType.forEnum(PositionDirection, 'PositionDirection'))
82
84
  .withField('snapshot.buys', DataType.DECIMAL)
83
85
  .withField('snapshot.sells', DataType.DECIMAL)
84
86
  .withField('snapshot.gain', DataType.DECIMAL)
@@ -110,6 +112,7 @@ module.exports = (() => {
110
112
  .withField('reinvest', DataType.BOOLEAN)
111
113
  .withField('snapshot.date', DataType.DAY)
112
114
  .withField('snapshot.open', DataType.DECIMAL)
115
+ .withField('snapshot.direction', DataType.forEnum(PositionDirection, 'PositionDirection'))
113
116
  .withField('snapshot.buys', DataType.DECIMAL)
114
117
  .withField('snapshot.sells', DataType.DECIMAL)
115
118
  .withField('snapshot.gain', DataType.DECIMAL)
@@ -6,7 +6,8 @@ const assert = require('@barchart/common-js/lang/assert'),
6
6
  Schema = require('@barchart/common-js/serialization/json/Schema'),
7
7
  SchemaBuilder = require('@barchart/common-js/serialization/json/builders/SchemaBuilder');
8
8
 
9
- const PositionSummaryFrame = require('./../data/PositionSummaryFrame');
9
+ const PositionDirection = require('./../data/PositionDirection'),
10
+ PositionSummaryFrame = require('./../data/PositionSummaryFrame');
10
11
 
11
12
  module.exports = (() => {
12
13
  'use strict';
@@ -76,11 +77,13 @@ module.exports = (() => {
76
77
  .withField('start.date', DataType.DAY)
77
78
  .withField('start.sequence', DataType.NUMBER)
78
79
  .withField('start.open', DataType.DECIMAL)
80
+ .withField('start.direction', DataType.forEnum(PositionDirection, 'PositionDirection'))
79
81
  .withField('start.basis', DataType.DECIMAL)
80
82
  .withField('start.value', DataType.DECIMAL)
81
83
  .withField('end.date', DataType.DAY)
82
84
  .withField('end.sequence', DataType.NUMBER)
83
85
  .withField('end.open', DataType.DECIMAL)
86
+ .withField('end.direction', DataType.forEnum(PositionDirection, 'PositionDirection'))
84
87
  .withField('end.basis', DataType.DECIMAL)
85
88
  .withField('end.value', DataType.DECIMAL)
86
89
  .withField('period.buys', DataType.DECIMAL)
@@ -108,11 +111,13 @@ module.exports = (() => {
108
111
  .withField('start.date', DataType.DAY)
109
112
  .withField('start.sequence', DataType.NUMBER)
110
113
  .withField('start.open', DataType.DECIMAL)
114
+ .withField('start.direction', DataType.forEnum(PositionDirection, 'PositionDirection'))
111
115
  .withField('start.basis', DataType.DECIMAL)
112
116
  .withField('start.value', DataType.DECIMAL)
113
117
  .withField('end.date', DataType.DAY)
114
118
  .withField('end.sequence', DataType.NUMBER)
115
119
  .withField('end.open', DataType.DECIMAL)
120
+ .withField('end.direction', DataType.forEnum(PositionDirection, 'PositionDirection'))
116
121
  .withField('end.basis', DataType.DECIMAL)
117
122
  .withField('end.value', DataType.DECIMAL)
118
123
  .withField('period.buys', DataType.DECIMAL)
@@ -7,6 +7,7 @@ const assert = require('@barchart/common-js/lang/assert'),
7
7
  SchemaBuilder = require('@barchart/common-js/serialization/json/builders/SchemaBuilder');
8
8
 
9
9
  const InstrumentType = require('./../data/InstrumentType'),
10
+ PositionDirection = require('./../data/PositionDirection'),
10
11
  TransactionType = require('./../data/TransactionType');
11
12
 
12
13
  module.exports = (() => {
@@ -176,6 +177,7 @@ module.exports = (() => {
176
177
  .withField('reference.position', DataType.STRING, true)
177
178
  .withField('reference.sequence', DataType.NUMBER, true)
178
179
  .withField('snapshot.open', DataType.DECIMAL)
180
+ .withField('snapshot.direction', DataType.forEnum(PositionDirection, 'PositionDirection'))
179
181
  .withField('snapshot.buys', DataType.DECIMAL)
180
182
  .withField('snapshot.sells', DataType.DECIMAL)
181
183
  .withField('snapshot.gain', DataType.DECIMAL)
@@ -219,6 +221,7 @@ module.exports = (() => {
219
221
  .withField('reference.position', DataType.STRING, true)
220
222
  .withField('reference.sequence', DataType.NUMBER, true)
221
223
  .withField('snapshot.open', DataType.DECIMAL)
224
+ .withField('snapshot.direction', DataType.forEnum(PositionDirection, 'PositionDirection'))
222
225
  .withField('snapshot.buys', DataType.DECIMAL)
223
226
  .withField('snapshot.sells', DataType.DECIMAL)
224
227
  .withField('snapshot.gain', DataType.DECIMAL)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.0.213",
3
+ "version": "1.0.217",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",
@@ -16,19 +16,26 @@ module.exports = (() => {
16
16
  * @param {String} alternateDescription
17
17
  * @param {String} code
18
18
  * @param {Boolean} canReinvest
19
+ * @param {Boolean} canShort
20
+ * @param {Boolean} canSwitchDirection
19
21
  * @param {Boolean} usesSymbols
22
+ * @param {Function} usesSymbols
20
23
  */
21
24
  class InstrumentType extends Enum {
22
- constructor(code, description, alternateDescription, canReinvest, usesSymbols, generator) {
25
+ constructor(code, description, alternateDescription, canReinvest, canShort, canSwitchDirection, usesSymbols, generator) {
23
26
  super(code, description);
24
27
 
25
28
  assert.argumentIsRequired(alternateDescription, 'alternateDescription', String);
26
29
  assert.argumentIsRequired(canReinvest, 'canReinvest', Boolean);
30
+ assert.argumentIsRequired(canShort, 'canShort', Boolean);
31
+ assert.argumentIsRequired(canSwitchDirection, 'canSwitchDirection', Boolean);
27
32
  assert.argumentIsRequired(usesSymbols, 'usesSymbols', Boolean);
28
33
  assert.argumentIsRequired(generator, 'generator', Function);
29
34
 
30
35
  this._alternateDescription = alternateDescription;
31
36
  this._canReinvest = canReinvest;
37
+ this._canShort = canShort;
38
+ this._canSwitchDirection = canSwitchDirection;
32
39
  this._usesSymbols = usesSymbols;
33
40
 
34
41
  this._generator = generator;
@@ -54,6 +61,27 @@ module.exports = (() => {
54
61
  return this._canReinvest;
55
62
  }
56
63
 
64
+ /**
65
+ * Indicates if short-selling is possible for this instrument type.
66
+ *
67
+ * @public
68
+ * @returns {Boolean}
69
+ */
70
+ get canShort() {
71
+ return this._canShort;
72
+ }
73
+
74
+ /**
75
+ * Indicates if one transaction is allowed to switch a position size from
76
+ * positive to negative (or vice versa).
77
+ *
78
+ * @public
79
+ * @returns {Boolean}
80
+ */
81
+ get canSwitchDirection() {
82
+ return this._canSwitchDirection;
83
+ }
84
+
57
85
  /**
58
86
  * Indicates if an instrument of this type can be represented by a symbol.
59
87
  *
@@ -142,10 +170,10 @@ module.exports = (() => {
142
170
  }
143
171
  }
144
172
 
145
- const cash = new InstrumentType('CASH', 'cash', 'Cash', false, false, (instrument) => `BARCHART-${instrument.type.code}-${instrument.currency.code}`);
146
- const equity = new InstrumentType('EQUITY', 'equity', 'Equities', true, true, (instrument) => `BARCHART-${instrument.type.code}-${instrument.symbol.barchart}`);
147
- const fund = new InstrumentType('FUND', 'mutual fund', 'Funds', true, true, (instrument) => `BARCHART-${instrument.type.code}-${instrument.symbol.barchart}`);
148
- const other = new InstrumentType('OTHER', 'other', 'Other', false, false, (instrument) => `BARCHART-${instrument.type.code}-${uuid.v4()}`);
173
+ const cash = new InstrumentType('CASH', 'cash', 'Cash', false, false, true, false, (instrument) => `BARCHART-${instrument.type.code}-${instrument.currency.code}`);
174
+ const equity = new InstrumentType('EQUITY', 'equity', 'Equities', true, true, false, true, (instrument) => `BARCHART-${instrument.type.code}-${instrument.symbol.barchart}`);
175
+ const fund = new InstrumentType('FUND', 'mutual fund', 'Funds', true, false, false, true, (instrument) => `BARCHART-${instrument.type.code}-${instrument.symbol.barchart}`);
176
+ const other = new InstrumentType('OTHER', 'other', 'Other', false, false, false, false, (instrument) => `BARCHART-${instrument.type.code}-${uuid.v4()}`);
149
177
 
150
178
  const map = { };
151
179