@barchart/portfolio-api-common 1.0.218 → 1.0.222

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
  /**
@@ -86,9 +88,9 @@ module.exports = (() => {
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
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).');
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).');
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.');
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
  }
@@ -118,20 +118,6 @@ module.exports = (() => {
118
118
  return even;
119
119
  }
120
120
  }
121
-
122
- /**
123
- * Returns true of the direction switched (from long to short, or
124
- * short to long).
125
- *
126
- * @public
127
- * @static
128
- * @param {PositionDirection} a
129
- * @param {PositionDirection} b
130
- * @returns {Boolean}
131
- */
132
- static switched(a, b) {
133
- return !a.closed && !b.closed && a.positive !== b.positive;
134
- }
135
121
  }
136
122
 
137
123
  const long = new PositionDirection('LONG', 'Long', 'positive');
@@ -1,6 +1,7 @@
1
1
  const assert = require('@barchart/common-js/lang/assert');
2
2
 
3
3
  const InstrumentType = require('./InstrumentType'),
4
+ PositionDirection = require('./PositionDirection'),
4
5
  TransactionType = require('./TransactionType');
5
6
 
6
7
  module.exports = (() => {
@@ -11,7 +12,7 @@ module.exports = (() => {
11
12
  *
12
13
  * @public
13
14
  */
14
- class Validator {
15
+ class TransactionValidator {
15
16
  constructor() {
16
17
 
17
18
  }
@@ -23,9 +24,10 @@ module.exports = (() => {
23
24
  * @public
24
25
  * @param {InstrumentType} instrumentType
25
26
  * @param {Boolean=} userInitiated
27
+ * @pararm {PositionDirection=} currentDirection
26
28
  * @return {Array.<TransactionType>}
27
29
  */
28
- static getValidTransactionTypesFor(instrumentType, userInitiated) {
30
+ static getTransactionTypesFor(instrumentType, userInitiated, currentDirection) {
29
31
  assert.argumentIsRequired(instrumentType, 'instrumentType', InstrumentType, 'InstrumentType');
30
32
  assert.argumentIsOptional(userInitiated, 'userInitiated', Boolean);
31
33
 
@@ -39,28 +41,63 @@ module.exports = (() => {
39
41
  }
40
42
 
41
43
  /**
42
- * Checks to see is an transaction type is applicable to an instrument type.
44
+ * Checks to see if an transaction type is applicable to an instrument type.
43
45
  *
44
46
  * @static
45
47
  * @public
46
48
  * @param {InstrumentType} instrumentType
49
+ * @param {TransactionType} transactionType
47
50
  * @param {Boolean=} userInitiated
48
51
  * @return {Boolean}
49
52
  */
50
- static validateTransactionTypeFor(instrumentType, transactionType, userInitiated) {
51
- const transactionTypes = Validator.getValidTransactionTypesFor(instrumentType, userInitiated);
53
+ static validateTransactionType(instrumentType, transactionType, userInitiated) {
54
+ assert.argumentIsRequired(transactionType, 'transactionType', TransactionType, 'TransactionType');
52
55
 
53
- return transactionType.some(t => t === transactionType);
56
+ const transactionTypes = TransactionValidator.getTransactionTypesFor(instrumentType, userInitiated);
57
+
58
+ return transactionTypes.some(t => t === transactionType);
59
+ }
60
+
61
+ /**
62
+ * Checks to see if a position for a given instrument type can exist in
63
+ * the given direction.
64
+ *
65
+ * @static
66
+ * @public
67
+ * @param {InstrumentType} instrumentType
68
+ * @param {PositionDirection} direction
69
+ * @return {Boolean}
70
+ */
71
+ static validateDirection(transactionType, direction) {
72
+ assert.argumentIsRequired(transactionType, 'transactionType', TransactionType, 'TransactionType');
73
+ assert.argumentIsRequired(direction, 'direction', PositionDirection, 'PositionDirection');
74
+
75
+ return validDirections[transactionType.code].some(d => d === direction);
76
+ }
77
+
78
+ /**
79
+ * Checks to see if the position switches direction and if the direction switch
80
+ * is valid.
81
+ *
82
+ * @static
83
+ * @public
84
+ * @param {InstrumentType} instrumentType
85
+ * @param {PositionDirection|null|undefined} currentDirection
86
+ * @param {PositionDirection} proposedDirection
87
+ * @return {Boolean}
88
+ */
89
+ static validateDirectionSwitch(instrumentType, currentDirection, proposedDirection) {
90
+ return currentDirection === null || instrumentType.canSwitchDirection || (!currentDirection.closed && !proposedDirection.closed && currentDirection.positive !== proposedDirection.positive);
54
91
  }
55
92
 
56
93
  toString() {
57
- return '[Validator]';
94
+ return '[TransactionValidator]';
58
95
  }
59
96
  }
60
97
 
61
98
  const validTransactionTypes = { };
62
99
 
63
- function associateTypes(instrumentType, transactionType, userInitiated) {
100
+ function associateTypes(instrumentType, transactionType, userInitiated, directions) {
64
101
  const instrumentTypeCode = instrumentType.code;
65
102
 
66
103
  if (!validTransactionTypes.hasOwnProperty(instrumentTypeCode)) {
@@ -99,5 +136,31 @@ module.exports = (() => {
99
136
  associateTypes(InstrumentType.CASH, TransactionType.DEBIT, false);
100
137
  associateTypes(InstrumentType.CASH, TransactionType.CREDIT, false);
101
138
 
102
- return Validator;
139
+ const validDirections = { };
140
+
141
+ function associateDirections(instrumentType, positionDirection) {
142
+ const instrumentTypeCode = instrumentType.code;
143
+
144
+ if (!validDirections.hasOwnProperty(instrumentTypeCode)) {
145
+ validDirections[instrumentTypeCode] = [ ];
146
+ }
147
+
148
+ validDirections[instrumentTypeCode].push(positionDirection);
149
+ }
150
+
151
+ associateTypes(InstrumentType.EQUITY, PositionDirection.EVEN);
152
+ associateTypes(InstrumentType.EQUITY, PositionDirection.LONG);
153
+ associateTypes(InstrumentType.EQUITY, PositionDirection.SHORT);
154
+
155
+ associateTypes(InstrumentType.FUND, PositionDirection.EVEN);
156
+ associateTypes(InstrumentType.FUND, PositionDirection.LONG);
157
+
158
+ associateTypes(InstrumentType.OTHER, PositionDirection.EVEN);
159
+ associateTypes(InstrumentType.OTHER, PositionDirection.LONG);
160
+
161
+ associateTypes(InstrumentType.CASH, PositionDirection.EVEN);
162
+ associateTypes(InstrumentType.CASH, PositionDirection.LONG);
163
+ associateTypes(InstrumentType.CASH, PositionDirection.SHORT);
164
+
165
+ return TransactionValidator;
103
166
  })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.0.218",
3
+ "version": "1.0.222",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",