@barchart/portfolio-api-common 6.2.0 → 7.0.0

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
@@ -110,6 +110,7 @@ module.exports = (() => {
110
110
  .withField('legacy.drops', DataType.NUMBER, true)
111
111
  .withField('miscellany', DataType.AD_HOC, true)
112
112
  .withField('email', DataType.AD_HOC, true)
113
+ .withField('linked', DataType.BOOLEAN)
113
114
  .withField('system.calculate.processors', DataType.NUMBER, true)
114
115
  .withField('system.sequence', DataType.NUMBER)
115
116
  .withField('system.version', DataType.STRING)
@@ -135,6 +136,7 @@ module.exports = (() => {
135
136
  .withField('legacy.drops', DataType.NUMBER, true)
136
137
  .withField('miscellany', DataType.AD_HOC, true)
137
138
  .withField('email', DataType.AD_HOC, true)
139
+ .withField('linked', DataType.BOOLEAN)
138
140
  .withField('system.calculate.processors', DataType.NUMBER, true)
139
141
  .schema
140
142
  );
@@ -155,7 +157,8 @@ module.exports = (() => {
155
157
  .withField('defaults.valuation', DataType.forEnum(ValuationType, 'ValuationType'), true)
156
158
  .withField('miscellany', DataType.AD_HOC, true)
157
159
  .withField('email', DataType.AD_HOC, true)
158
- .schema
160
+ .withField('linked', DataType.BOOLEAN)
161
+ .schema
159
162
  );
160
163
 
161
164
  const update = new PortfolioSchema(SchemaBuilder.withName('update')
@@ -167,7 +170,7 @@ module.exports = (() => {
167
170
  .withField('defaults.reinvest', DataType.BOOLEAN, true)
168
171
  .withField('miscellany', DataType.AD_HOC, true)
169
172
  .withField('email', DataType.AD_HOC, true)
170
- .schema
173
+ .schema
171
174
  );
172
175
 
173
176
  return PortfolioSchema;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "6.2.0",
3
+ "version": "7.0.0",
4
4
  "description": "Common JavaScript code used by Barchart's Portfolio Service",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",