@barchart/portfolio-api-common 1.2.16 → 1.2.20

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,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,11 @@ module.exports = (() => {
19
19
  * @param {Boolean} closing
20
20
  * @param {Boolean} fee
21
21
  * @param {Boolean} corporateAction
22
+ * @param {Boolean} initial
23
+ * @param {Boolean} significant
22
24
  */
23
25
  class TransactionType extends Enum {
24
- constructor(code, description, display, purchase, sale, income, opening, closing, fee, corporateAction) {
26
+ constructor(code, description, display, purchase, sale, income, opening, closing, fee, corporateAction, initial, significant) {
25
27
  super(code, description);
26
28
 
27
29
  assert.argumentIsRequired(display, 'display', String);
@@ -32,6 +34,8 @@ module.exports = (() => {
32
34
  assert.argumentIsRequired(closing, 'closing', Boolean);
33
35
  assert.argumentIsRequired(fee, 'fee', Boolean);
34
36
  assert.argumentIsRequired(corporateAction, 'corporateAction', Boolean);
37
+ assert.argumentIsRequired(initial, 'initial', Boolean);
38
+ assert.argumentIsRequired(significant, 'significant', Boolean);
35
39
 
36
40
  this._display = display;
37
41
  this._purchase = purchase;
@@ -41,6 +45,8 @@ module.exports = (() => {
41
45
  this._closing = closing;
42
46
  this._fee = fee;
43
47
  this._corporateAction = corporateAction;
48
+ this._initial = initial;
49
+ this._significant = significant;
44
50
  }
45
51
 
46
52
  /**
@@ -136,6 +142,26 @@ module.exports = (() => {
136
142
  return this._corporateAction;
137
143
  }
138
144
 
145
+ /**
146
+ * Indicates if the transaction can be the first transaction for a position.
147
+ *
148
+ * @public
149
+ * @returns {Boolean}
150
+ */
151
+ get initial() {
152
+ return this._initial;
153
+ }
154
+
155
+ /**
156
+ * Significant transactions cannot be discarded during transaction re-write.
157
+ *
158
+ * @public
159
+ * @returns {Boolean}
160
+ */
161
+ get significant() {
162
+ return this._significant;
163
+ }
164
+
139
165
  /**
140
166
  * A purchase.
141
167
  *
@@ -350,28 +376,28 @@ module.exports = (() => {
350
376
  }
351
377
  }
352
378
 
353
- const buy = new TransactionType('B', 'Buy', 'Buy', true, false, false, true, false, false, false);
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);
379
+ const buy = new TransactionType('B', 'Buy', 'Buy', true, false, false, true, false, false, false, true, true);
380
+ const sell = new TransactionType('S', 'Sell', 'Sell', false, true, false, false, true, false, false, false, true);
381
+ const buyShort = new TransactionType('BS', 'Buy To Cover', 'Buy To Cover', true, false, false, false, true, false, false, false, true);
382
+ const sellShort = new TransactionType('SS', 'Sell Short', 'Sell Short', false, true, false, true, false, false, false, true, true);
383
+ const dividend = new TransactionType('DV', 'Dividend', 'Dividend', false, false, true, false, false, false, true, false, false);
384
+ const dividendReinvest = new TransactionType('DX', 'Dividend (Reinvested)', 'Dividend Reinvest', false, false, false, true, false, false, true, false, false);
385
+ const dividendStock = new TransactionType('DS', 'Dividend (Stock)', 'Dividend Stock', false, false, false, true, false, false, true, false, false);
386
+ const split = new TransactionType('SP', 'Split', 'Split', false, false, false, true, false, false, true, false, false);
387
+ const fee = new TransactionType('F', 'Fee', 'Fee', false, false, false, false, false, true, false, false, false);
388
+ const feeUnits = new TransactionType('FU', 'Fee Units', 'Fee', false, false, false, false, true, false, false, false, false);
389
+
390
+ const distributionCash = new TransactionType('DC', 'Distribution (Cash)', 'Cash Distribution', false, false, true, false, false, false, true, false, false);
391
+ const distributionReinvest = new TransactionType('DY', 'Distribution (Reinvested)', 'Distribution Reinvest', false, false, false, true, false, false, true, false, false);
392
+ const distributionFund = new TransactionType('DF', 'Distribution (Units)', 'Unit Distribution', false, false, false, true, false, false, true, false, false);
393
+
394
+ const deposit = new TransactionType('D', 'Deposit', 'Deposit', false, false, false, false, false, false, false, true, true);
395
+ const withdrawal = new TransactionType('W', 'Withdrawal', 'Withdrawal', false, false, false, false, false, false, false, true, true);
396
+ const debit = new TransactionType('DR', 'Debit', 'Debit', false, false, false, false, false, false, false, true, true);
397
+ const credit = new TransactionType('CR', 'Credit', 'Credit', false, false, false, false, false, false, false, true, true);
398
+
399
+ const valuation = new TransactionType('V', 'Valuation', 'Valuation', false, false, false, false, false, false, false, false, false);
400
+ const income = new TransactionType('I', 'Income', 'Income', false, false, true, false, false, false, false, false, false);
375
401
 
376
402
  return TransactionType;
377
403
  })();
@@ -96,7 +96,7 @@ module.exports = (() => {
96
96
  }
97
97
 
98
98
  /**
99
- * Checks to see if an transaction type is applicable to an instrument type.
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
- * Checks to see if a position for a given instrument type can exist in
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
- * Checks to see if the position switches direction and if the direction switch
144
+ * Determines if the position switches direction and if the direction switch
135
145
  * is valid.
136
146
  *
137
147
  * @static
@@ -204,7 +204,7 @@ module.exports = (() => {
204
204
  } else if (t.snapshot.open.getIsZero()) {
205
205
  rate = null;
206
206
  } else {
207
- rate = t.valuation.value.divide(t.snapshot.open)
207
+ rate = t.valuation.value.divide(t.snapshot.open);
208
208
  }
209
209
 
210
210
  return {
@@ -419,6 +419,8 @@ module.exports = (() => {
419
419
 
420
420
  if (price) {
421
421
  priceToUse = price;
422
+ } else if (data.previousPrice) {
423
+ priceToUse = new Decimal(data.previousPrice);
422
424
  } else if (!summary.end.open.getIsZero()) {
423
425
  priceToUse = summary.end.value.divide(summary.end.open);
424
426
  } else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.2.16",
3
+ "version": "1.2.20",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",
@@ -629,9 +629,11 @@ module.exports = (() => {
629
629
  * @param {Boolean} closing
630
630
  * @param {Boolean} fee
631
631
  * @param {Boolean} corporateAction
632
+ * @param {Boolean} initial
633
+ * @param {Boolean} significant
632
634
  */
633
635
  class TransactionType extends Enum {
634
- constructor(code, description, display, purchase, sale, income, opening, closing, fee, corporateAction) {
636
+ constructor(code, description, display, purchase, sale, income, opening, closing, fee, corporateAction, initial, significant) {
635
637
  super(code, description);
636
638
 
637
639
  assert.argumentIsRequired(display, 'display', String);
@@ -642,6 +644,8 @@ module.exports = (() => {
642
644
  assert.argumentIsRequired(closing, 'closing', Boolean);
643
645
  assert.argumentIsRequired(fee, 'fee', Boolean);
644
646
  assert.argumentIsRequired(corporateAction, 'corporateAction', Boolean);
647
+ assert.argumentIsRequired(initial, 'initial', Boolean);
648
+ assert.argumentIsRequired(significant, 'significant', Boolean);
645
649
 
646
650
  this._display = display;
647
651
  this._purchase = purchase;
@@ -651,6 +655,8 @@ module.exports = (() => {
651
655
  this._closing = closing;
652
656
  this._fee = fee;
653
657
  this._corporateAction = corporateAction;
658
+ this._initial = initial;
659
+ this._significant = significant;
654
660
  }
655
661
 
656
662
  /**
@@ -746,6 +752,26 @@ module.exports = (() => {
746
752
  return this._corporateAction;
747
753
  }
748
754
 
755
+ /**
756
+ * Indicates if the transaction can be the first transaction for a position.
757
+ *
758
+ * @public
759
+ * @returns {Boolean}
760
+ */
761
+ get initial() {
762
+ return this._initial;
763
+ }
764
+
765
+ /**
766
+ * Significant transactions cannot be discarded during transaction re-write.
767
+ *
768
+ * @public
769
+ * @returns {Boolean}
770
+ */
771
+ get significant() {
772
+ return this._significant;
773
+ }
774
+
749
775
  /**
750
776
  * A purchase.
751
777
  *
@@ -960,28 +986,28 @@ module.exports = (() => {
960
986
  }
961
987
  }
962
988
 
963
- const buy = new TransactionType('B', 'Buy', 'Buy', true, false, false, true, false, false, false);
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);
989
+ const buy = new TransactionType('B', 'Buy', 'Buy', true, false, false, true, false, false, false, true, true);
990
+ const sell = new TransactionType('S', 'Sell', 'Sell', false, true, false, false, true, false, false, false, true);
991
+ const buyShort = new TransactionType('BS', 'Buy To Cover', 'Buy To Cover', true, false, false, false, true, false, false, false, true);
992
+ const sellShort = new TransactionType('SS', 'Sell Short', 'Sell Short', false, true, false, true, false, false, false, true, true);
993
+ const dividend = new TransactionType('DV', 'Dividend', 'Dividend', false, false, true, false, false, false, true, false, false);
994
+ const dividendReinvest = new TransactionType('DX', 'Dividend (Reinvested)', 'Dividend Reinvest', false, false, false, true, false, false, true, false, false);
995
+ const dividendStock = new TransactionType('DS', 'Dividend (Stock)', 'Dividend Stock', false, false, false, true, false, false, true, false, false);
996
+ const split = new TransactionType('SP', 'Split', 'Split', false, false, false, true, false, false, true, false, false);
997
+ const fee = new TransactionType('F', 'Fee', 'Fee', false, false, false, false, false, true, false, false, false);
998
+ const feeUnits = new TransactionType('FU', 'Fee Units', 'Fee', false, false, false, false, true, false, false, false, false);
999
+
1000
+ const distributionCash = new TransactionType('DC', 'Distribution (Cash)', 'Cash Distribution', false, false, true, false, false, false, true, false, false);
1001
+ const distributionReinvest = new TransactionType('DY', 'Distribution (Reinvested)', 'Distribution Reinvest', false, false, false, true, false, false, true, false, false);
1002
+ const distributionFund = new TransactionType('DF', 'Distribution (Units)', 'Unit Distribution', false, false, false, true, false, false, true, false, false);
1003
+
1004
+ const deposit = new TransactionType('D', 'Deposit', 'Deposit', false, false, false, false, false, false, false, true, true);
1005
+ const withdrawal = new TransactionType('W', 'Withdrawal', 'Withdrawal', false, false, false, false, false, false, false, true, true);
1006
+ const debit = new TransactionType('DR', 'Debit', 'Debit', false, false, false, false, false, false, false, true, true);
1007
+ const credit = new TransactionType('CR', 'Credit', 'Credit', false, false, false, false, false, false, false, true, true);
1008
+
1009
+ const valuation = new TransactionType('V', 'Valuation', 'Valuation', false, false, false, false, false, false, false, false, false);
1010
+ const income = new TransactionType('I', 'Income', 'Income', false, false, true, false, false, false, false, false, false);
985
1011
 
986
1012
  return TransactionType;
987
1013
  })();
@@ -1085,7 +1111,7 @@ module.exports = (() => {
1085
1111
  }
1086
1112
 
1087
1113
  /**
1088
- * Checks to see if an transaction type is applicable to an instrument type.
1114
+ * Determines if a transaction type is applicable to an instrument type.
1089
1115
  *
1090
1116
  * @static
1091
1117
  * @public
@@ -1103,7 +1129,17 @@ module.exports = (() => {
1103
1129
  }
1104
1130
 
1105
1131
  /**
1106
- * Checks to see if a position for a given instrument type can exist in
1132
+ * Determines if a transaction type is valid as the first transaction of
1133
+ * a position.
1134
+ *
1135
+ * @param {TransactionType} transactionType
1136
+ */
1137
+ static validateInitialTransactionType(transactionType) {
1138
+ return transactionType.initial;
1139
+ }
1140
+
1141
+ /**
1142
+ * Determines if a position for a given instrument type can exist in
1107
1143
  * the given direction.
1108
1144
  *
1109
1145
  * @static
@@ -1120,7 +1156,7 @@ module.exports = (() => {
1120
1156
  }
1121
1157
 
1122
1158
  /**
1123
- * Checks to see if the position switches direction and if the direction switch
1159
+ * Determines if the position switches direction and if the direction switch
1124
1160
  * is valid.
1125
1161
  *
1126
1162
  * @static
@@ -3437,6 +3473,8 @@ module.exports = (() => {
3437
3473
 
3438
3474
  if (price) {
3439
3475
  priceToUse = price;
3476
+ } else if (data.previousPrice) {
3477
+ priceToUse = new Decimal(data.previousPrice);
3440
3478
  } else if (!summary.end.open.getIsZero()) {
3441
3479
  priceToUse = summary.end.value.divide(summary.end.open);
3442
3480
  } else {
@@ -9287,6 +9325,12 @@ describe('When requesting all the user-initiated transaction types', () => {
9287
9325
  });
9288
9326
  });
9289
9327
 
9328
+ describe('When validating direction', () => {
9329
+ 'use strict';
9330
+
9331
+
9332
+ });
9333
+
9290
9334
  },{"./../../../lib/data/TransactionValidator":5,"@barchart/common-js/lang/Day":18}],37:[function(require,module,exports){
9291
9335
  const Currency = require('@barchart/common-js/lang/Currency'),
9292
9336
  Decimal = require('@barchart/common-js/lang/Decimal');
@@ -55,3 +55,9 @@ describe('When requesting all the user-initiated transaction types', () => {
55
55
  expect(userInitiated.length).toEqual(9);
56
56
  });
57
57
  });
58
+
59
+ describe('When validating direction', () => {
60
+ 'use strict';
61
+
62
+
63
+ });