@barchart/portfolio-api-common 6.2.0 → 6.3.1

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.
@@ -108,50 +108,90 @@ module.exports = (() => {
108
108
  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));
109
109
  }
110
110
 
111
- static getSwitchIndex(transactions, position) {
112
- assert.argumentIsArray(transactions, 'transactions');
113
- assert.argumentIsOptional(position, 'position');
114
-
115
- let open;
116
-
117
- if (position) {
118
- open = position.snapshot.open;
119
- } else {
120
- open = Decimal.ZERO;
121
- }
122
-
123
- let initial;
124
-
125
- if (open.getIsZero()) {
126
- initial = null;
127
- } else {
128
- initial = PositionDirection.for(open);
129
- }
130
-
131
- return transactions.findIndex((t) => {
132
- let quantity = t.quantity.absolute();
133
-
134
- if (t.type.sale) {
135
- quantity = quantity.opposite();
136
- }
137
-
138
- open = open.add(quantity);
139
-
140
- const current = PositionDirection.for(open);
141
-
142
- if (initial !== null && initial !== current && current !== PositionDirection.EVEN) {
143
- return true;
144
- }
145
-
146
- if (initial === null && !open.getIsZero()) {
147
- initial = current;
148
- }
149
-
150
- return false;
151
- });
152
- }
153
-
154
- /**
111
+ /**
112
+ * Given an array of transactions, returns the index of the first transaction that would cause an invalid direction switch.
113
+ *
114
+ * @public
115
+ * @static
116
+ * @param {Object[]} transactions
117
+ * @param {InstrumentType} instrumentType
118
+ * @param {Object=} position
119
+ * @return {Number}
120
+ */
121
+ static getSwitchIndex(transactions, instrumentType, position) {
122
+ assert.argumentIsArray(transactions, 'transactions');
123
+ assert.argumentIsRequired(instrumentType, 'instrumentType', InstrumentType, 'InstrumentType');
124
+ assert.argumentIsOptional(position, 'position');
125
+
126
+ let open = position ? position.snapshot.open : Decimal.ZERO;
127
+ let currentDirection = open.getIsZero() ? null : PositionDirection.for(open);
128
+
129
+ return transactions.findIndex((t) => {
130
+ let quantity = t.quantity.absolute();
131
+
132
+ if (t.type.sale) {
133
+ quantity = quantity.opposite();
134
+ }
135
+
136
+ const nextOpen = open.add(quantity);
137
+ const nextDirection = nextOpen.getIsZero() ? PositionDirection.EVEN : PositionDirection.for(nextOpen);
138
+
139
+ const isValidSwitch = TransactionValidator.validateDirectionSwitch(instrumentType, currentDirection, nextDirection);
140
+
141
+ if (!isValidSwitch) {
142
+ return true;
143
+ }
144
+
145
+ open = nextOpen;
146
+ currentDirection = nextDirection;
147
+
148
+ return false;
149
+ });
150
+ }
151
+
152
+ /**
153
+ * Given an array of transactions, returns the index of the first transaction that would violate position rules.
154
+ *
155
+ * @public
156
+ * @static
157
+ * @param {Object[]} transactions
158
+ * @param {InstrumentType} instrumentType
159
+ * @param {Object=} position
160
+ * @return {Number}
161
+ */
162
+ static getPositionViolationIndex(transactions, instrumentType, position) {
163
+ assert.argumentIsArray(transactions, 'transactions');
164
+ assert.argumentIsRequired(instrumentType, 'instrumentType', InstrumentType, 'InstrumentType');
165
+ assert.argumentIsOptional(position, 'position');
166
+
167
+ let open = position ? position.snapshot.open : Decimal.ZERO;
168
+ let currentDirection = open.getIsZero() ? PositionDirection.EVEN : PositionDirection.for(open);
169
+
170
+ return transactions.findIndex((t) => {
171
+ const quantity = t.quantity.absolute();
172
+ const type = t.type;
173
+
174
+ const validTypes = TransactionValidator.getTransactionTypesFor(instrumentType, true, currentDirection);
175
+
176
+ const isValidType = validTypes.includes(type);
177
+
178
+ if (!isValidType) {
179
+ return true;
180
+ }
181
+
182
+ const delta = type.sale ? quantity.opposite() : quantity;
183
+
184
+ const nextOpen = open.add(delta);
185
+ const nextDirection = nextOpen.getIsZero() ? PositionDirection.EVEN : PositionDirection.for(nextOpen);
186
+
187
+ open = nextOpen;
188
+ currentDirection = nextDirection;
189
+
190
+ return false;
191
+ });
192
+ }
193
+
194
+ /**
155
195
  * Given an instrument type, returns all valid transaction types.
156
196
  *
157
197
  * @static
@@ -311,6 +351,7 @@ module.exports = (() => {
311
351
  associateTypes(InstrumentType.FUND, TransactionType.DISTRIBUTION_CASH, false);
312
352
  associateTypes(InstrumentType.FUND, TransactionType.DISTRIBUTION_REINVEST, false);
313
353
  associateTypes(InstrumentType.FUND, TransactionType.DISTRIBUTION_FUND, false);
354
+ associateTypes(InstrumentType.FUND, TransactionType.SPLIT, false);
314
355
  associateTypes(InstrumentType.FUND, TransactionType.DELIST, false);
315
356
  associateTypes(InstrumentType.FUND, TransactionType.MERGER_OPEN, false);
316
357
  associateTypes(InstrumentType.FUND, TransactionType.MERGER_CLOSE, false);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "6.2.0",
3
+ "version": "6.3.1",
4
4
  "description": "Common JavaScript code used by Barchart's Portfolio Service",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",