@barchart/portfolio-api-common 6.2.0 → 6.3.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.
- package/lib/data/TransactionValidator.js +84 -44
- package/package.json +1 -1
|
@@ -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
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
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
|