@barchart/portfolio-api-common 1.2.15 → 1.2.19
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/api/failures/PortfolioFailureType.js +12 -0
- package/lib/data/TransactionType.js +36 -23
- package/lib/data/TransactionValidator.js +13 -3
- package/lib/processing/PositionItem.js +45 -18
- package/package.json +1 -1
- package/test/SpecRunner.js +100 -44
- package/test/specs/data/TransactionValidatorSpec.js +6 -0
|
@@ -108,6 +108,17 @@ module.exports = (() => {
|
|
|
108
108
|
return transactionCreateFailedInvalidDirectionSwitch;
|
|
109
109
|
}
|
|
110
110
|
|
|
111
|
+
/**
|
|
112
|
+
* The initial transaction type (first in sequence) would be invalid.
|
|
113
|
+
*
|
|
114
|
+
* @public
|
|
115
|
+
* @static
|
|
116
|
+
* @returns {FailureType}
|
|
117
|
+
*/
|
|
118
|
+
static get TRANSACTION_CREATE_FAILED_INVALID_INITIAL_TYPE() {
|
|
119
|
+
return transactionCreateFailedInvalidInitialType;
|
|
120
|
+
}
|
|
121
|
+
|
|
111
122
|
/**
|
|
112
123
|
* Deleting any transaction except for the most recent requires
|
|
113
124
|
* re-writing transaction history.
|
|
@@ -158,6 +169,7 @@ module.exports = (() => {
|
|
|
158
169
|
const transactionCreateFailedTypeInvalidForInstrument = new FailureType('TRANSACTION_CREATE_FAILED_TYPE_INVALID_FOR_INSTRUMENT', 'Unable to process transaction, {L|transactionType.description} transactions cannot be used with {L|instrumentType.description} positions.');
|
|
159
170
|
const transactionCreateFailedTypeInvalidForDirection = new FailureType('TRANSACTION_CREATE_FAILED_TYPE_INVALID_FOR_DIRECTION', 'Unable to process transaction, a {L|positionDirection.description} position would be created (i.e. you would have {L|positionDirection.sign} shares/units). {u|instrumentType.description} positions cannot have {L|positionDirection.description} positions.');
|
|
160
171
|
const transactionCreateFailedInvalidDirectionSwitch = new FailureType('TRANSACTION_CREATE_FAILED_INVALID_DIRECTION_SWITCH', 'Unable to process transaction, the transaction would switch the position from {L|currentDirection.description} to {L|proposedDirection.description} (i.e. {L|currentDirection.sign} to {L|proposedDirection.sign} shares/units). This is not allowed. Please close the current position (i.e. zero it out) and then enter a second transaction.');
|
|
172
|
+
const transactionCreateFailedInvalidInitialType = new FailureType('TRANSACTION_CREATE_FAILED_INITIAL_TYPE', 'Unable to process operatoin, the type of the first transaction cannot be a {U|transactionType.description}.');
|
|
161
173
|
|
|
162
174
|
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).');
|
|
163
175
|
const transactionDeleteFailedNoTransaction = new FailureType('TRANSACTION_DELETE_FAILED_NO_TRANSACTION', 'Unable to delete transaction. The referenced transaction does not exist.');
|
|
@@ -19,9 +19,10 @@ module.exports = (() => {
|
|
|
19
19
|
* @param {Boolean} closing
|
|
20
20
|
* @param {Boolean} fee
|
|
21
21
|
* @param {Boolean} corporateAction
|
|
22
|
+
* @param {Boolean} corporateAction
|
|
22
23
|
*/
|
|
23
24
|
class TransactionType extends Enum {
|
|
24
|
-
constructor(code, description, display, purchase, sale, income, opening, closing, fee, corporateAction) {
|
|
25
|
+
constructor(code, description, display, purchase, sale, income, opening, closing, fee, corporateAction, initial) {
|
|
25
26
|
super(code, description);
|
|
26
27
|
|
|
27
28
|
assert.argumentIsRequired(display, 'display', String);
|
|
@@ -32,6 +33,7 @@ module.exports = (() => {
|
|
|
32
33
|
assert.argumentIsRequired(closing, 'closing', Boolean);
|
|
33
34
|
assert.argumentIsRequired(fee, 'fee', Boolean);
|
|
34
35
|
assert.argumentIsRequired(corporateAction, 'corporateAction', Boolean);
|
|
36
|
+
assert.argumentIsRequired(initial, 'initial', Boolean);
|
|
35
37
|
|
|
36
38
|
this._display = display;
|
|
37
39
|
this._purchase = purchase;
|
|
@@ -41,6 +43,7 @@ module.exports = (() => {
|
|
|
41
43
|
this._closing = closing;
|
|
42
44
|
this._fee = fee;
|
|
43
45
|
this._corporateAction = corporateAction;
|
|
46
|
+
this._initial = initial;
|
|
44
47
|
}
|
|
45
48
|
|
|
46
49
|
/**
|
|
@@ -136,6 +139,16 @@ module.exports = (() => {
|
|
|
136
139
|
return this._corporateAction;
|
|
137
140
|
}
|
|
138
141
|
|
|
142
|
+
/**
|
|
143
|
+
* Indicates if the transaction can be the first transaction for a position.
|
|
144
|
+
*
|
|
145
|
+
* @public
|
|
146
|
+
* @returns {Boolean}
|
|
147
|
+
*/
|
|
148
|
+
get initial() {
|
|
149
|
+
return this._initial;
|
|
150
|
+
}
|
|
151
|
+
|
|
139
152
|
/**
|
|
140
153
|
* A purchase.
|
|
141
154
|
*
|
|
@@ -350,28 +363,28 @@ module.exports = (() => {
|
|
|
350
363
|
}
|
|
351
364
|
}
|
|
352
365
|
|
|
353
|
-
const buy = new TransactionType('B', 'Buy', 'Buy', true, false, false, true,
|
|
354
|
-
const sell = new TransactionType('S', 'Sell', 'Sell', false, true, false, false, true, false, false);
|
|
355
|
-
const buyShort = new TransactionType('BS', 'Buy To Cover', 'Buy To Cover', true, false, false, false, true, false, false);
|
|
356
|
-
const sellShort = new TransactionType('SS', 'Sell Short', 'Sell Short', false, true, false, true, false, false, false);
|
|
357
|
-
const dividend = new TransactionType('DV', 'Dividend', 'Dividend', false, false, true, false, false, false, true);
|
|
358
|
-
const dividendReinvest = new TransactionType('DX', 'Dividend (Reinvested)', 'Dividend Reinvest', false, false, false, true, false, false, true);
|
|
359
|
-
const dividendStock = new TransactionType('DS', 'Dividend (Stock)', 'Dividend Stock', false, false, false, true, false, false, true);
|
|
360
|
-
const split = new TransactionType('SP', 'Split', 'Split', false, false, false, true, false, false, true);
|
|
361
|
-
const fee = new TransactionType('F', 'Fee', 'Fee', false, false, false, false, false, true, false);
|
|
362
|
-
const feeUnits = new TransactionType('FU', 'Fee Units', 'Fee', false, false, false, false, true, false, false);
|
|
363
|
-
|
|
364
|
-
const distributionCash = new TransactionType('DC', 'Distribution (Cash)', 'Cash Distribution', false, false, true, false, false, false, true);
|
|
365
|
-
const distributionReinvest = new TransactionType('DY', 'Distribution (Reinvested)', 'Distribution Reinvest', false, false, false, true, false, false, true);
|
|
366
|
-
const distributionFund = new TransactionType('DF', 'Distribution (Units)', 'Unit Distribution', false, false, false, true, false, false, true);
|
|
367
|
-
|
|
368
|
-
const deposit = new TransactionType('D', 'Deposit', 'Deposit', false, false, false, false, false, false, false);
|
|
369
|
-
const withdrawal = new TransactionType('W', 'Withdrawal', 'Withdrawal', false, false, false, false, false, false, false);
|
|
370
|
-
const debit = new TransactionType('DR', 'Debit', 'Debit', false, false, false, false, false, false, false);
|
|
371
|
-
const credit = new TransactionType('CR', 'Credit', 'Credit', false, false, false, false, false, false, false);
|
|
372
|
-
|
|
373
|
-
const valuation = new TransactionType('V', 'Valuation', 'Valuation', false, false, false, false, false, false, false);
|
|
374
|
-
const income = new TransactionType('I', 'Income', 'Income', false, false, true, false, false, false, false);
|
|
366
|
+
const buy = new TransactionType('B', 'Buy', 'Buy', true, false, false, true, false, false, false, true);
|
|
367
|
+
const sell = new TransactionType('S', 'Sell', 'Sell', false, true, false, false, true, false, false, false);
|
|
368
|
+
const buyShort = new TransactionType('BS', 'Buy To Cover', 'Buy To Cover', true, false, false, false, true, false, false, false);
|
|
369
|
+
const sellShort = new TransactionType('SS', 'Sell Short', 'Sell Short', false, true, false, true, false, false, false, true);
|
|
370
|
+
const dividend = new TransactionType('DV', 'Dividend', 'Dividend', false, false, true, false, false, false, true, false);
|
|
371
|
+
const dividendReinvest = new TransactionType('DX', 'Dividend (Reinvested)', 'Dividend Reinvest', false, false, false, true, false, false, true, false);
|
|
372
|
+
const dividendStock = new TransactionType('DS', 'Dividend (Stock)', 'Dividend Stock', false, false, false, true, false, false, true, false);
|
|
373
|
+
const split = new TransactionType('SP', 'Split', 'Split', false, false, false, true, false, false, true, false);
|
|
374
|
+
const fee = new TransactionType('F', 'Fee', 'Fee', false, false, false, false, false, true, false, false);
|
|
375
|
+
const feeUnits = new TransactionType('FU', 'Fee Units', 'Fee', false, false, false, false, true, false, false, false);
|
|
376
|
+
|
|
377
|
+
const distributionCash = new TransactionType('DC', 'Distribution (Cash)', 'Cash Distribution', false, false, true, false, false, false, true, false);
|
|
378
|
+
const distributionReinvest = new TransactionType('DY', 'Distribution (Reinvested)', 'Distribution Reinvest', false, false, false, true, false, false, true, false);
|
|
379
|
+
const distributionFund = new TransactionType('DF', 'Distribution (Units)', 'Unit Distribution', false, false, false, true, false, false, true, false);
|
|
380
|
+
|
|
381
|
+
const deposit = new TransactionType('D', 'Deposit', 'Deposit', false, false, false, false, false, false, false, true);
|
|
382
|
+
const withdrawal = new TransactionType('W', 'Withdrawal', 'Withdrawal', false, false, false, false, false, false, false, true);
|
|
383
|
+
const debit = new TransactionType('DR', 'Debit', 'Debit', false, false, false, false, false, false, false, true);
|
|
384
|
+
const credit = new TransactionType('CR', 'Credit', 'Credit', false, false, false, false, false, false, false, true);
|
|
385
|
+
|
|
386
|
+
const valuation = new TransactionType('V', 'Valuation', 'Valuation', false, false, false, false, false, false, false, false);
|
|
387
|
+
const income = new TransactionType('I', 'Income', 'Income', false, false, true, false, false, false, false, false);
|
|
375
388
|
|
|
376
389
|
return TransactionType;
|
|
377
390
|
})();
|
|
@@ -96,7 +96,7 @@ module.exports = (() => {
|
|
|
96
96
|
}
|
|
97
97
|
|
|
98
98
|
/**
|
|
99
|
-
*
|
|
99
|
+
* Determines if a transaction type is applicable to an instrument type.
|
|
100
100
|
*
|
|
101
101
|
* @static
|
|
102
102
|
* @public
|
|
@@ -114,7 +114,17 @@ module.exports = (() => {
|
|
|
114
114
|
}
|
|
115
115
|
|
|
116
116
|
/**
|
|
117
|
-
*
|
|
117
|
+
* Determines if a transaction type is valid as the first transaction of
|
|
118
|
+
* a position.
|
|
119
|
+
*
|
|
120
|
+
* @param {TransactionType} transactionType
|
|
121
|
+
*/
|
|
122
|
+
static validateInitialTransactionType(transactionType) {
|
|
123
|
+
return transactionType.initial;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Determines if a position for a given instrument type can exist in
|
|
118
128
|
* the given direction.
|
|
119
129
|
*
|
|
120
130
|
* @static
|
|
@@ -131,7 +141,7 @@ module.exports = (() => {
|
|
|
131
141
|
}
|
|
132
142
|
|
|
133
143
|
/**
|
|
134
|
-
*
|
|
144
|
+
* Determines if the position switches direction and if the direction switch
|
|
135
145
|
* is valid.
|
|
136
146
|
*
|
|
137
147
|
* @static
|
|
@@ -406,32 +406,59 @@ module.exports = (() => {
|
|
|
406
406
|
|
|
407
407
|
const summary = item.currentSummary;
|
|
408
408
|
|
|
409
|
-
|
|
410
|
-
const period = summary.period;
|
|
409
|
+
let priceToUse;
|
|
411
410
|
|
|
412
|
-
|
|
413
|
-
|
|
411
|
+
if (price) {
|
|
412
|
+
priceToUse = price;
|
|
413
|
+
} else {
|
|
414
414
|
|
|
415
|
-
|
|
416
|
-
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
if (summary) {
|
|
418
|
+
let priceToUse;
|
|
419
|
+
|
|
420
|
+
if (price) {
|
|
421
|
+
priceToUse = price;
|
|
422
|
+
} else if (data.previousPrice) {
|
|
423
|
+
priceToUse = new Decimal(data.previousPrice);
|
|
424
|
+
} else if (!summary.end.open.getIsZero()) {
|
|
425
|
+
priceToUse = summary.end.value.divide(summary.end.open);
|
|
417
426
|
} else {
|
|
418
|
-
|
|
427
|
+
priceToUse = null;
|
|
419
428
|
}
|
|
420
429
|
|
|
421
|
-
|
|
422
|
-
|
|
430
|
+
if (priceToUse !== null) {
|
|
431
|
+
const period = summary.period;
|
|
423
432
|
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
433
|
+
let unrealized = summary.end.open.multiply(priceToUse).add(summary.end.basis);
|
|
434
|
+
let unrealizedChange;
|
|
435
|
+
|
|
436
|
+
if (data.unrealizedToday !== null) {
|
|
437
|
+
unrealizedChange = unrealized.subtract(data.unrealized);
|
|
438
|
+
} else {
|
|
439
|
+
unrealizedChange = Decimal.ZERO;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
let summaryTotalCurrent = period.realized.add(period.income).add(unrealized);
|
|
443
|
+
let summaryTotalCurrentChange;
|
|
444
|
+
|
|
445
|
+
if (data.summaryTotalCurrent !== null) {
|
|
446
|
+
summaryTotalCurrentChange = summaryTotalCurrent.subtract(data.summaryTotalCurrent);
|
|
447
|
+
} else {
|
|
448
|
+
summaryTotalCurrentChange = Decimal.ZERO;
|
|
449
|
+
}
|
|
429
450
|
|
|
430
|
-
|
|
431
|
-
|
|
451
|
+
data.summaryTotalCurrent = summaryTotalCurrent;
|
|
452
|
+
data.summaryTotalCurrentChange = summaryTotalCurrentChange;
|
|
432
453
|
|
|
433
|
-
|
|
434
|
-
|
|
454
|
+
data.unrealized = unrealized;
|
|
455
|
+
data.unrealizedChange = unrealizedChange;
|
|
456
|
+
} else {
|
|
457
|
+
data.summaryTotalCurrentChange = Decimal.ZERO;
|
|
458
|
+
|
|
459
|
+
data.unrealized = Decimal.ZERO;
|
|
460
|
+
data.unrealizedChange = Decimal.ZERO;
|
|
461
|
+
}
|
|
435
462
|
} else {
|
|
436
463
|
data.summaryTotalCurrentChange = Decimal.ZERO;
|
|
437
464
|
|
package/package.json
CHANGED
package/test/SpecRunner.js
CHANGED
|
@@ -629,9 +629,10 @@ module.exports = (() => {
|
|
|
629
629
|
* @param {Boolean} closing
|
|
630
630
|
* @param {Boolean} fee
|
|
631
631
|
* @param {Boolean} corporateAction
|
|
632
|
+
* @param {Boolean} corporateAction
|
|
632
633
|
*/
|
|
633
634
|
class TransactionType extends Enum {
|
|
634
|
-
constructor(code, description, display, purchase, sale, income, opening, closing, fee, corporateAction) {
|
|
635
|
+
constructor(code, description, display, purchase, sale, income, opening, closing, fee, corporateAction, initial) {
|
|
635
636
|
super(code, description);
|
|
636
637
|
|
|
637
638
|
assert.argumentIsRequired(display, 'display', String);
|
|
@@ -642,6 +643,7 @@ module.exports = (() => {
|
|
|
642
643
|
assert.argumentIsRequired(closing, 'closing', Boolean);
|
|
643
644
|
assert.argumentIsRequired(fee, 'fee', Boolean);
|
|
644
645
|
assert.argumentIsRequired(corporateAction, 'corporateAction', Boolean);
|
|
646
|
+
assert.argumentIsRequired(initial, 'initial', Boolean);
|
|
645
647
|
|
|
646
648
|
this._display = display;
|
|
647
649
|
this._purchase = purchase;
|
|
@@ -651,6 +653,7 @@ module.exports = (() => {
|
|
|
651
653
|
this._closing = closing;
|
|
652
654
|
this._fee = fee;
|
|
653
655
|
this._corporateAction = corporateAction;
|
|
656
|
+
this._initial = initial;
|
|
654
657
|
}
|
|
655
658
|
|
|
656
659
|
/**
|
|
@@ -746,6 +749,16 @@ module.exports = (() => {
|
|
|
746
749
|
return this._corporateAction;
|
|
747
750
|
}
|
|
748
751
|
|
|
752
|
+
/**
|
|
753
|
+
* Indicates if the transaction can be the first transaction for a position.
|
|
754
|
+
*
|
|
755
|
+
* @public
|
|
756
|
+
* @returns {Boolean}
|
|
757
|
+
*/
|
|
758
|
+
get initial() {
|
|
759
|
+
return this._initial;
|
|
760
|
+
}
|
|
761
|
+
|
|
749
762
|
/**
|
|
750
763
|
* A purchase.
|
|
751
764
|
*
|
|
@@ -960,28 +973,28 @@ module.exports = (() => {
|
|
|
960
973
|
}
|
|
961
974
|
}
|
|
962
975
|
|
|
963
|
-
const buy = new TransactionType('B', 'Buy', 'Buy', true, false, false, true,
|
|
964
|
-
const sell = new TransactionType('S', 'Sell', 'Sell', false, true, false, false, true, false, false);
|
|
965
|
-
const buyShort = new TransactionType('BS', 'Buy To Cover', 'Buy To Cover', true, false, false, false, true, false, false);
|
|
966
|
-
const sellShort = new TransactionType('SS', 'Sell Short', 'Sell Short', false, true, false, true, false, false, false);
|
|
967
|
-
const dividend = new TransactionType('DV', 'Dividend', 'Dividend', false, false, true, false, false, false, true);
|
|
968
|
-
const dividendReinvest = new TransactionType('DX', 'Dividend (Reinvested)', 'Dividend Reinvest', false, false, false, true, false, false, true);
|
|
969
|
-
const dividendStock = new TransactionType('DS', 'Dividend (Stock)', 'Dividend Stock', false, false, false, true, false, false, true);
|
|
970
|
-
const split = new TransactionType('SP', 'Split', 'Split', false, false, false, true, false, false, true);
|
|
971
|
-
const fee = new TransactionType('F', 'Fee', 'Fee', false, false, false, false, false, true, false);
|
|
972
|
-
const feeUnits = new TransactionType('FU', 'Fee Units', 'Fee', false, false, false, false, true, false, false);
|
|
973
|
-
|
|
974
|
-
const distributionCash = new TransactionType('DC', 'Distribution (Cash)', 'Cash Distribution', false, false, true, false, false, false, true);
|
|
975
|
-
const distributionReinvest = new TransactionType('DY', 'Distribution (Reinvested)', 'Distribution Reinvest', false, false, false, true, false, false, true);
|
|
976
|
-
const distributionFund = new TransactionType('DF', 'Distribution (Units)', 'Unit Distribution', false, false, false, true, false, false, true);
|
|
977
|
-
|
|
978
|
-
const deposit = new TransactionType('D', 'Deposit', 'Deposit', false, false, false, false, false, false, false);
|
|
979
|
-
const withdrawal = new TransactionType('W', 'Withdrawal', 'Withdrawal', false, false, false, false, false, false, false);
|
|
980
|
-
const debit = new TransactionType('DR', 'Debit', 'Debit', false, false, false, false, false, false, false);
|
|
981
|
-
const credit = new TransactionType('CR', 'Credit', 'Credit', false, false, false, false, false, false, false);
|
|
982
|
-
|
|
983
|
-
const valuation = new TransactionType('V', 'Valuation', 'Valuation', false, false, false, false, false, false, false);
|
|
984
|
-
const income = new TransactionType('I', 'Income', 'Income', false, false, true, false, false, false, false);
|
|
976
|
+
const buy = new TransactionType('B', 'Buy', 'Buy', true, false, false, true, false, false, false, true);
|
|
977
|
+
const sell = new TransactionType('S', 'Sell', 'Sell', false, true, false, false, true, false, false, false);
|
|
978
|
+
const buyShort = new TransactionType('BS', 'Buy To Cover', 'Buy To Cover', true, false, false, false, true, false, false, false);
|
|
979
|
+
const sellShort = new TransactionType('SS', 'Sell Short', 'Sell Short', false, true, false, true, false, false, false, true);
|
|
980
|
+
const dividend = new TransactionType('DV', 'Dividend', 'Dividend', false, false, true, false, false, false, true, false);
|
|
981
|
+
const dividendReinvest = new TransactionType('DX', 'Dividend (Reinvested)', 'Dividend Reinvest', false, false, false, true, false, false, true, false);
|
|
982
|
+
const dividendStock = new TransactionType('DS', 'Dividend (Stock)', 'Dividend Stock', false, false, false, true, false, false, true, false);
|
|
983
|
+
const split = new TransactionType('SP', 'Split', 'Split', false, false, false, true, false, false, true, false);
|
|
984
|
+
const fee = new TransactionType('F', 'Fee', 'Fee', false, false, false, false, false, true, false, false);
|
|
985
|
+
const feeUnits = new TransactionType('FU', 'Fee Units', 'Fee', false, false, false, false, true, false, false, false);
|
|
986
|
+
|
|
987
|
+
const distributionCash = new TransactionType('DC', 'Distribution (Cash)', 'Cash Distribution', false, false, true, false, false, false, true, false);
|
|
988
|
+
const distributionReinvest = new TransactionType('DY', 'Distribution (Reinvested)', 'Distribution Reinvest', false, false, false, true, false, false, true, false);
|
|
989
|
+
const distributionFund = new TransactionType('DF', 'Distribution (Units)', 'Unit Distribution', false, false, false, true, false, false, true, false);
|
|
990
|
+
|
|
991
|
+
const deposit = new TransactionType('D', 'Deposit', 'Deposit', false, false, false, false, false, false, false, true);
|
|
992
|
+
const withdrawal = new TransactionType('W', 'Withdrawal', 'Withdrawal', false, false, false, false, false, false, false, true);
|
|
993
|
+
const debit = new TransactionType('DR', 'Debit', 'Debit', false, false, false, false, false, false, false, true);
|
|
994
|
+
const credit = new TransactionType('CR', 'Credit', 'Credit', false, false, false, false, false, false, false, true);
|
|
995
|
+
|
|
996
|
+
const valuation = new TransactionType('V', 'Valuation', 'Valuation', false, false, false, false, false, false, false, false);
|
|
997
|
+
const income = new TransactionType('I', 'Income', 'Income', false, false, true, false, false, false, false, false);
|
|
985
998
|
|
|
986
999
|
return TransactionType;
|
|
987
1000
|
})();
|
|
@@ -1085,7 +1098,7 @@ module.exports = (() => {
|
|
|
1085
1098
|
}
|
|
1086
1099
|
|
|
1087
1100
|
/**
|
|
1088
|
-
*
|
|
1101
|
+
* Determines if a transaction type is applicable to an instrument type.
|
|
1089
1102
|
*
|
|
1090
1103
|
* @static
|
|
1091
1104
|
* @public
|
|
@@ -1103,7 +1116,17 @@ module.exports = (() => {
|
|
|
1103
1116
|
}
|
|
1104
1117
|
|
|
1105
1118
|
/**
|
|
1106
|
-
*
|
|
1119
|
+
* Determines if a transaction type is valid as the first transaction of
|
|
1120
|
+
* a position.
|
|
1121
|
+
*
|
|
1122
|
+
* @param {TransactionType} transactionType
|
|
1123
|
+
*/
|
|
1124
|
+
static validateInitialTransactionType(transactionType) {
|
|
1125
|
+
return transactionType.initial;
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
/**
|
|
1129
|
+
* Determines if a position for a given instrument type can exist in
|
|
1107
1130
|
* the given direction.
|
|
1108
1131
|
*
|
|
1109
1132
|
* @static
|
|
@@ -1120,7 +1143,7 @@ module.exports = (() => {
|
|
|
1120
1143
|
}
|
|
1121
1144
|
|
|
1122
1145
|
/**
|
|
1123
|
-
*
|
|
1146
|
+
* Determines if the position switches direction and if the direction switch
|
|
1124
1147
|
* is valid.
|
|
1125
1148
|
*
|
|
1126
1149
|
* @static
|
|
@@ -3424,32 +3447,59 @@ module.exports = (() => {
|
|
|
3424
3447
|
|
|
3425
3448
|
const summary = item.currentSummary;
|
|
3426
3449
|
|
|
3427
|
-
|
|
3428
|
-
const period = summary.period;
|
|
3450
|
+
let priceToUse;
|
|
3429
3451
|
|
|
3430
|
-
|
|
3431
|
-
|
|
3452
|
+
if (price) {
|
|
3453
|
+
priceToUse = price;
|
|
3454
|
+
} else {
|
|
3432
3455
|
|
|
3433
|
-
|
|
3434
|
-
|
|
3456
|
+
}
|
|
3457
|
+
|
|
3458
|
+
if (summary) {
|
|
3459
|
+
let priceToUse;
|
|
3460
|
+
|
|
3461
|
+
if (price) {
|
|
3462
|
+
priceToUse = price;
|
|
3463
|
+
} else if (data.previousPrice) {
|
|
3464
|
+
priceToUse = new Decimal(data.previousPrice);
|
|
3465
|
+
} else if (!summary.end.open.getIsZero()) {
|
|
3466
|
+
priceToUse = summary.end.value.divide(summary.end.open);
|
|
3435
3467
|
} else {
|
|
3436
|
-
|
|
3468
|
+
priceToUse = null;
|
|
3437
3469
|
}
|
|
3438
3470
|
|
|
3439
|
-
|
|
3440
|
-
|
|
3471
|
+
if (priceToUse !== null) {
|
|
3472
|
+
const period = summary.period;
|
|
3441
3473
|
|
|
3442
|
-
|
|
3443
|
-
|
|
3444
|
-
|
|
3445
|
-
|
|
3446
|
-
|
|
3474
|
+
let unrealized = summary.end.open.multiply(priceToUse).add(summary.end.basis);
|
|
3475
|
+
let unrealizedChange;
|
|
3476
|
+
|
|
3477
|
+
if (data.unrealizedToday !== null) {
|
|
3478
|
+
unrealizedChange = unrealized.subtract(data.unrealized);
|
|
3479
|
+
} else {
|
|
3480
|
+
unrealizedChange = Decimal.ZERO;
|
|
3481
|
+
}
|
|
3482
|
+
|
|
3483
|
+
let summaryTotalCurrent = period.realized.add(period.income).add(unrealized);
|
|
3484
|
+
let summaryTotalCurrentChange;
|
|
3447
3485
|
|
|
3448
|
-
|
|
3449
|
-
|
|
3486
|
+
if (data.summaryTotalCurrent !== null) {
|
|
3487
|
+
summaryTotalCurrentChange = summaryTotalCurrent.subtract(data.summaryTotalCurrent);
|
|
3488
|
+
} else {
|
|
3489
|
+
summaryTotalCurrentChange = Decimal.ZERO;
|
|
3490
|
+
}
|
|
3450
3491
|
|
|
3451
|
-
|
|
3452
|
-
|
|
3492
|
+
data.summaryTotalCurrent = summaryTotalCurrent;
|
|
3493
|
+
data.summaryTotalCurrentChange = summaryTotalCurrentChange;
|
|
3494
|
+
|
|
3495
|
+
data.unrealized = unrealized;
|
|
3496
|
+
data.unrealizedChange = unrealizedChange;
|
|
3497
|
+
} else {
|
|
3498
|
+
data.summaryTotalCurrentChange = Decimal.ZERO;
|
|
3499
|
+
|
|
3500
|
+
data.unrealized = Decimal.ZERO;
|
|
3501
|
+
data.unrealizedChange = Decimal.ZERO;
|
|
3502
|
+
}
|
|
3453
3503
|
} else {
|
|
3454
3504
|
data.summaryTotalCurrentChange = Decimal.ZERO;
|
|
3455
3505
|
|
|
@@ -9262,6 +9312,12 @@ describe('When requesting all the user-initiated transaction types', () => {
|
|
|
9262
9312
|
});
|
|
9263
9313
|
});
|
|
9264
9314
|
|
|
9315
|
+
describe('When validating direction', () => {
|
|
9316
|
+
'use strict';
|
|
9317
|
+
|
|
9318
|
+
|
|
9319
|
+
});
|
|
9320
|
+
|
|
9265
9321
|
},{"./../../../lib/data/TransactionValidator":5,"@barchart/common-js/lang/Day":18}],37:[function(require,module,exports){
|
|
9266
9322
|
const Currency = require('@barchart/common-js/lang/Currency'),
|
|
9267
9323
|
Decimal = require('@barchart/common-js/lang/Decimal');
|