@barchart/portfolio-api-common 1.0.220 → 1.0.225

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.
@@ -35,15 +35,21 @@ module.exports = (() => {
35
35
  }
36
36
 
37
37
  /**
38
- * The transaction type is not valid, given the current position size
39
- * (e.g. sell is invalid for a short position, sell short must be used).
40
- *
41
38
  * @public
42
39
  * @static
43
40
  * @returns {FailureType}
44
41
  */
45
- static get TRANSACTION_CREATE_FAILED_TYPE_INVALID_FOR_POSITION() {
46
- return transactionCreateFailedTypeInvalidForPosition;
42
+ static get TRANSACTION_CREATE_FAILED_TYPE_INVALID_FOR_INSTRUMENT() {
43
+ return transactionCreateFailedTypeInvalidForInstrument;
44
+ }
45
+
46
+ /**
47
+ * @public
48
+ * @static
49
+ * @returns {FailureType}
50
+ */
51
+ static get TRANSACTION_CREATE_FAILED_TYPE_INVALID_FOR_DIRECTION() {
52
+ return transactionCreateFailedTypeInvalidForDirection;
47
53
  }
48
54
 
49
55
  /**
@@ -54,12 +60,8 @@ module.exports = (() => {
54
60
  * @static
55
61
  * @returns {FailureType}
56
62
  */
57
- static get TRANSACTION_CREATE_FAILED_DIRECTION_SWITCH() {
58
- return transactionCreateFailedDirectionSwitch;
59
- }
60
-
61
- static get TRANSACTION_CREATE_REWRITE_UNSUPPORTED() {
62
- return transactionCreateRewriteUnsupported;
63
+ static get TRANSACTION_CREATE_FAILED_INVALID_DIRECTION_SWITCH() {
64
+ return transactionCreateFailedInvalidDirectionSwitch;
63
65
  }
64
66
 
65
67
  /**
@@ -85,10 +87,10 @@ module.exports = (() => {
85
87
 
86
88
  const positionCreateFailedNoPortfolio = new FailureType('POSITION_CREATE_FAILED_NO_PORTFOLIO', 'Unable to create transaction. The referenced portfolio does not exist. Has it been deleted?');
87
89
 
88
- const transactionCreateFailedOutOfSequence = new FailureType('TRANSACTION_CREATE_FAILED_OUT_OF_SEQUENCE', 'Unable to create transaction, because the transaction date is out-of-sequence. In other words, it would occur before an existing transaction. Please confirm your intent to re-write transaction history (which could take some time and alter the historical results for this position).');
89
- const transactionCreateFailedTypeInvalidForPosition = new FailureType('TRANSACTION_CREATE_FAILED_INVALID_FOR_POSITION', 'A {L|type.description} are not allowed. At the time of the transaction, your position is {L|direction} (i.e. {L|sign}).');
90
- const transactionCreateFailedDirectionSwitch = new FailureType('TRANSACTION_CREATE_FAILED_DIRECTION_SWITCH', 'Unable to create transaction, because the position direction would be switched (from long to short or vice versa). Please close the position (to a zero balance), then enter a second transaction.');
91
- const transactionCreateRewriteUnsupported = new FailureType('TRANSACTION_CREATE_REWRITE_UNSUPPORTED', 'Unable to re-write transaction history. This operation is not currently supported (but will be implemented soon).');
90
+ const transactionCreateFailedOutOfSequence = new FailureType('TRANSACTION_CREATE_FAILED_OUT_OF_SEQUENCE', 'Unable to process transaction, because the transaction date is out-of-sequence. In other words, it would occur before an existing transaction. Please confirm your intent to re-write transaction history (which could take some time and alter the historical results for this position).');
91
+ 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.');
92
+ 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.');
93
+ const transactionCreateFailedInvalidDirectionSwitch = new FailureType('TRANSACTION_CREATE_FAILED_INVALID_DIRECTION_SWITCH', 'Unable to process transaction, the transaction would switch the position from {L|previousDirection.description} to {L|proposedDirection.description} (i.e. {L|previousDirection.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.');
92
94
 
93
95
  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).');
94
96
  const transactionDeleteUnsupported = new FailureType('TRANSACTION_DELETE_UNSUPPORTED', 'Unable to delete transaction. This operation is not currently supported (but will be implemented soon).');
@@ -19,7 +19,7 @@ module.exports = (() => {
19
19
  constructor(code, description, sign) {
20
20
  super(code, description);
21
21
 
22
- assert.argumentIsString(sign, 'sign', String);
22
+ assert.argumentIsRequired(sign, 'sign', String);
23
23
 
24
24
  this._sign = sign;
25
25
  }
@@ -24,16 +24,21 @@ module.exports = (() => {
24
24
  * @public
25
25
  * @param {InstrumentType} instrumentType
26
26
  * @param {Boolean=} userInitiated
27
+ * @pararm {PositionDirection=} currentDirection
27
28
  * @return {Array.<TransactionType>}
28
29
  */
29
- static getTransactionTypesFor(instrumentType, userInitiated) {
30
+ static getTransactionTypesFor(instrumentType, userInitiated, currentDirection) {
30
31
  assert.argumentIsRequired(instrumentType, 'instrumentType', InstrumentType, 'InstrumentType');
31
32
  assert.argumentIsOptional(userInitiated, 'userInitiated', Boolean);
32
33
 
33
34
  let valid = validTransactionTypes[instrumentType.code] || [ ];
34
35
 
35
36
  if (userInitiated) {
36
- valid = valid.filter(d => d.user === userInitiated);
37
+ valid = valid.filter(data => data.user === userInitiated);
38
+ }
39
+
40
+ if (currentDirection) {
41
+ valid = valid.filter(data => data.directions.some(d => d === currentDirection));
37
42
  }
38
43
 
39
44
  return valid.map(d => d.type);
@@ -54,7 +59,7 @@ module.exports = (() => {
54
59
 
55
60
  const transactionTypes = TransactionValidator.getTransactionTypesFor(instrumentType, userInitiated);
56
61
 
57
- return transactionType.some(t => t === transactionType);
62
+ return transactionTypes.some(t => t === transactionType);
58
63
  }
59
64
 
60
65
  /**
@@ -67,11 +72,11 @@ module.exports = (() => {
67
72
  * @param {PositionDirection} direction
68
73
  * @return {Boolean}
69
74
  */
70
- static validateDirection(transactionType, direction) {
71
- assert.argumentIsRequired(transactionType, 'transactionType', TransactionType, 'TransactionType');
75
+ static validateDirection(instrumentType, direction) {
76
+ assert.argumentIsRequired(instrumentType, 'instrumentType', InstrumentType, 'InstrumentType');
72
77
  assert.argumentIsRequired(direction, 'direction', PositionDirection, 'PositionDirection');
73
78
 
74
- return validDirections[transactionType.code].some(d => d === direction);
79
+ return validDirections[instrumentType.code].some(d => d === direction);
75
80
  }
76
81
 
77
82
  /**
@@ -96,37 +101,37 @@ module.exports = (() => {
96
101
 
97
102
  const validTransactionTypes = { };
98
103
 
99
- function associateTypes(instrumentType, transactionType, userInitiated) {
104
+ function associateTypes(instrumentType, transactionType, userInitiated, directions) {
100
105
  const instrumentTypeCode = instrumentType.code;
101
106
 
102
107
  if (!validTransactionTypes.hasOwnProperty(instrumentTypeCode)) {
103
108
  validTransactionTypes[instrumentTypeCode] = [ ];
104
109
  }
105
110
 
106
- validTransactionTypes[instrumentTypeCode].push({ type: transactionType, user: userInitiated });
111
+ validTransactionTypes[instrumentTypeCode].push({ type: transactionType, user: userInitiated, directions: directions || [ PositionDirection.LONG, PositionDirection.SHORT, PositionDirection.EVEN ] });
107
112
  }
108
113
 
109
- associateTypes(InstrumentType.EQUITY, TransactionType.BUY, true);
110
- associateTypes(InstrumentType.EQUITY, TransactionType.SELL, true);
111
- associateTypes(InstrumentType.EQUITY, TransactionType.SELL_SHORT, true);
112
- associateTypes(InstrumentType.EQUITY, TransactionType.BUY_SHORT, true);
113
- associateTypes(InstrumentType.EQUITY, TransactionType.FEE, true);
114
+ associateTypes(InstrumentType.EQUITY, TransactionType.BUY, true, [ PositionDirection.LONG, PositionDirection.EVEN ]);
115
+ associateTypes(InstrumentType.EQUITY, TransactionType.SELL, true, [ PositionDirection.LONG ]);
116
+ associateTypes(InstrumentType.EQUITY, TransactionType.SELL_SHORT, true, [ PositionDirection.SHORT, PositionDirection.EVEN ]);
117
+ associateTypes(InstrumentType.EQUITY, TransactionType.BUY_SHORT, true, [ PositionDirection.SHORT ]);
118
+ associateTypes(InstrumentType.EQUITY, TransactionType.FEE, true, [ PositionDirection.LONG, PositionDirection.SHORT ]);
114
119
  associateTypes(InstrumentType.EQUITY, TransactionType.DIVIDEND, false);
115
120
  associateTypes(InstrumentType.EQUITY, TransactionType.DIVIDEND_REINVEST, false);
116
121
  associateTypes(InstrumentType.EQUITY, TransactionType.DIVIDEND_STOCK, false);
117
122
  associateTypes(InstrumentType.EQUITY, TransactionType.SPLIT, false);
118
123
 
119
- associateTypes(InstrumentType.FUND, TransactionType.BUY, true);
120
- associateTypes(InstrumentType.FUND, TransactionType.SELL, true);
121
- associateTypes(InstrumentType.FUND, TransactionType.FEE, true);
124
+ associateTypes(InstrumentType.FUND, TransactionType.BUY, true, [ PositionDirection.LONG, PositionDirection.EVEN ]);
125
+ associateTypes(InstrumentType.FUND, TransactionType.SELL, true, [ PositionDirection.LONG ]);
126
+ associateTypes(InstrumentType.FUND, TransactionType.FEE, true, [ PositionDirection.LONG ]);
122
127
  associateTypes(InstrumentType.FUND, TransactionType.FEE_UNITS, false);
123
128
  associateTypes(InstrumentType.FUND, TransactionType.DISTRIBUTION_CASH, false);
124
129
  associateTypes(InstrumentType.FUND, TransactionType.DISTRIBUTION_FUND, false);
125
130
 
126
- associateTypes(InstrumentType.OTHER, TransactionType.BUY, true);
127
- associateTypes(InstrumentType.OTHER, TransactionType.SELL, true);
128
- associateTypes(InstrumentType.OTHER, TransactionType.INCOME, true);
129
- associateTypes(InstrumentType.OTHER, TransactionType.FEE, true);
131
+ associateTypes(InstrumentType.OTHER, TransactionType.BUY, true, [ PositionDirection.LONG, PositionDirection.EVEN ]);
132
+ associateTypes(InstrumentType.OTHER, TransactionType.SELL, true, [ PositionDirection.LONG ]);
133
+ associateTypes(InstrumentType.OTHER, TransactionType.INCOME, true, [ PositionDirection.LONG ]);
134
+ associateTypes(InstrumentType.OTHER, TransactionType.FEE, true, [ PositionDirection.LONG ]);
130
135
  associateTypes(InstrumentType.OTHER, TransactionType.VALUATION, true);
131
136
 
132
137
  associateTypes(InstrumentType.CASH, TransactionType.DEPOSIT, true);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.0.220",
3
+ "version": "1.0.225",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",