@barchart/portfolio-api-common 1.0.221 → 1.0.226

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.
@@ -87,10 +87,10 @@ module.exports = (() => {
87
87
 
88
88
  const positionCreateFailedNoPortfolio = new FailureType('POSITION_CREATE_FAILED_NO_PORTFOLIO', 'Unable to create transaction. The referenced portfolio does not exist. Has it been deleted?');
89
89
 
90
- 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).');
91
- const transactionCreateFailedTypeInvalidForInstrument = new FailureType('TRANSACTION_CREATE_FAILED_TYPE_INVALID_FOR_INSTRUMENT', '{L|transactionType.description} are not allowed for {L|instrumentType.description}');
92
- const transactionCreateFailedTypeInvalidForDirection = new FailureType('TRANSACTION_CREATE_FAILED_TYPE_INVALID_FOR_DIRECTION', 'A {L|type.description} are not allowed. At the time of the transaction, your position is {L|direction} (i.e. {L|sign}).');
93
- const transactionCreateFailedInvalidDirectionSwitch = new FailureType('TRANSACTION_CREATE_FAILED_INVALID_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.');
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|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.');
94
94
 
95
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).');
96
96
  const transactionDeleteUnsupported = new FailureType('TRANSACTION_DELETE_UNSUPPORTED', 'Unable to delete transaction. This operation is not currently supported (but will be implemented soon).');
@@ -34,7 +34,11 @@ module.exports = (() => {
34
34
  let valid = validTransactionTypes[instrumentType.code] || [ ];
35
35
 
36
36
  if (userInitiated) {
37
- 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));
38
42
  }
39
43
 
40
44
  return valid.map(d => d.type);
@@ -55,7 +59,7 @@ module.exports = (() => {
55
59
 
56
60
  const transactionTypes = TransactionValidator.getTransactionTypesFor(instrumentType, userInitiated);
57
61
 
58
- return transactionType.some(t => t === transactionType);
62
+ return transactionTypes.some(t => t === transactionType);
59
63
  }
60
64
 
61
65
  /**
@@ -68,11 +72,11 @@ module.exports = (() => {
68
72
  * @param {PositionDirection} direction
69
73
  * @return {Boolean}
70
74
  */
71
- static validateDirection(transactionType, direction) {
72
- assert.argumentIsRequired(transactionType, 'transactionType', TransactionType, 'TransactionType');
75
+ static validateDirection(instrumentType, direction) {
76
+ assert.argumentIsRequired(instrumentType, 'instrumentType', InstrumentType, 'InstrumentType');
73
77
  assert.argumentIsRequired(direction, 'direction', PositionDirection, 'PositionDirection');
74
78
 
75
- return validDirections[transactionType.code].some(d => d === direction);
79
+ return validDirections[instrumentType.code].some(d => d === direction);
76
80
  }
77
81
 
78
82
  /**
@@ -104,30 +108,30 @@ module.exports = (() => {
104
108
  validTransactionTypes[instrumentTypeCode] = [ ];
105
109
  }
106
110
 
107
- validTransactionTypes[instrumentTypeCode].push({ type: transactionType, user: userInitiated });
111
+ validTransactionTypes[instrumentTypeCode].push({ type: transactionType, user: userInitiated, directions: directions || [ PositionDirection.LONG, PositionDirection.SHORT, PositionDirection.EVEN ] });
108
112
  }
109
113
 
110
- associateTypes(InstrumentType.EQUITY, TransactionType.BUY, true);
111
- associateTypes(InstrumentType.EQUITY, TransactionType.SELL, true);
112
- associateTypes(InstrumentType.EQUITY, TransactionType.SELL_SHORT, true);
113
- associateTypes(InstrumentType.EQUITY, TransactionType.BUY_SHORT, true);
114
- 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 ]);
115
119
  associateTypes(InstrumentType.EQUITY, TransactionType.DIVIDEND, false);
116
120
  associateTypes(InstrumentType.EQUITY, TransactionType.DIVIDEND_REINVEST, false);
117
121
  associateTypes(InstrumentType.EQUITY, TransactionType.DIVIDEND_STOCK, false);
118
122
  associateTypes(InstrumentType.EQUITY, TransactionType.SPLIT, false);
119
123
 
120
- associateTypes(InstrumentType.FUND, TransactionType.BUY, true);
121
- associateTypes(InstrumentType.FUND, TransactionType.SELL, true);
122
- 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 ]);
123
127
  associateTypes(InstrumentType.FUND, TransactionType.FEE_UNITS, false);
124
128
  associateTypes(InstrumentType.FUND, TransactionType.DISTRIBUTION_CASH, false);
125
129
  associateTypes(InstrumentType.FUND, TransactionType.DISTRIBUTION_FUND, false);
126
130
 
127
- associateTypes(InstrumentType.OTHER, TransactionType.BUY, true);
128
- associateTypes(InstrumentType.OTHER, TransactionType.SELL, true);
129
- associateTypes(InstrumentType.OTHER, TransactionType.INCOME, true);
130
- 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 ]);
131
135
  associateTypes(InstrumentType.OTHER, TransactionType.VALUATION, true);
132
136
 
133
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.221",
3
+ "version": "1.0.226",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",