@barchart/portfolio-api-common 6.1.0 → 6.2.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.
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const assert = require('@barchart/common-js/lang/assert'),
|
|
2
2
|
array = require('@barchart/common-js/lang/array'),
|
|
3
|
+
Decimal = require('@barchart/common-js/lang/Decimal'),
|
|
3
4
|
is = require('@barchart/common-js/lang/is');
|
|
4
5
|
|
|
5
6
|
const InstrumentType = require('./InstrumentType'),
|
|
@@ -107,6 +108,49 @@ module.exports = (() => {
|
|
|
107
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));
|
|
108
109
|
}
|
|
109
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
|
+
|
|
110
154
|
/**
|
|
111
155
|
* Given an instrument type, returns all valid transaction types.
|
|
112
156
|
*
|