@barchart/portfolio-api-common 1.0.216 → 1.0.220

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.
@@ -0,0 +1,165 @@
1
+ const assert = require('@barchart/common-js/lang/assert');
2
+
3
+ const InstrumentType = require('./InstrumentType'),
4
+ PositionDirection = require('./PositionDirection'),
5
+ TransactionType = require('./TransactionType');
6
+
7
+ module.exports = (() => {
8
+ 'use strict';
9
+
10
+ /**
11
+ * Static utilities for validating transactions.
12
+ *
13
+ * @public
14
+ */
15
+ class TransactionValidator {
16
+ constructor() {
17
+
18
+ }
19
+
20
+ /**
21
+ * Given an instrument type, returns all valid transaction types.
22
+ *
23
+ * @static
24
+ * @public
25
+ * @param {InstrumentType} instrumentType
26
+ * @param {Boolean=} userInitiated
27
+ * @return {Array.<TransactionType>}
28
+ */
29
+ static getTransactionTypesFor(instrumentType, userInitiated) {
30
+ assert.argumentIsRequired(instrumentType, 'instrumentType', InstrumentType, 'InstrumentType');
31
+ assert.argumentIsOptional(userInitiated, 'userInitiated', Boolean);
32
+
33
+ let valid = validTransactionTypes[instrumentType.code] || [ ];
34
+
35
+ if (userInitiated) {
36
+ valid = valid.filter(d => d.user === userInitiated);
37
+ }
38
+
39
+ return valid.map(d => d.type);
40
+ }
41
+
42
+ /**
43
+ * Checks to see if an transaction type is applicable to an instrument type.
44
+ *
45
+ * @static
46
+ * @public
47
+ * @param {InstrumentType} instrumentType
48
+ * @param {TransactionType} transactionType
49
+ * @param {Boolean=} userInitiated
50
+ * @return {Boolean}
51
+ */
52
+ static validateTransactionType(instrumentType, transactionType, userInitiated) {
53
+ assert.argumentIsRequired(transactionType, 'transactionType', TransactionType, 'TransactionType');
54
+
55
+ const transactionTypes = TransactionValidator.getTransactionTypesFor(instrumentType, userInitiated);
56
+
57
+ return transactionType.some(t => t === transactionType);
58
+ }
59
+
60
+ /**
61
+ * Checks to see if a position for a given instrument type can exist in
62
+ * the given direction.
63
+ *
64
+ * @static
65
+ * @public
66
+ * @param {InstrumentType} instrumentType
67
+ * @param {PositionDirection} direction
68
+ * @return {Boolean}
69
+ */
70
+ static validateDirection(transactionType, direction) {
71
+ assert.argumentIsRequired(transactionType, 'transactionType', TransactionType, 'TransactionType');
72
+ assert.argumentIsRequired(direction, 'direction', PositionDirection, 'PositionDirection');
73
+
74
+ return validDirections[transactionType.code].some(d => d === direction);
75
+ }
76
+
77
+ /**
78
+ * Checks to see if the position switches direction and if the direction switch
79
+ * is valid.
80
+ *
81
+ * @static
82
+ * @public
83
+ * @param {InstrumentType} instrumentType
84
+ * @param {PositionDirection|null|undefined} currentDirection
85
+ * @param {PositionDirection} proposedDirection
86
+ * @return {Boolean}
87
+ */
88
+ static validateDirectionSwitch(instrumentType, currentDirection, proposedDirection) {
89
+ return currentDirection === null || instrumentType.canSwitchDirection || (!currentDirection.closed && !proposedDirection.closed && currentDirection.positive !== proposedDirection.positive);
90
+ }
91
+
92
+ toString() {
93
+ return '[TransactionValidator]';
94
+ }
95
+ }
96
+
97
+ const validTransactionTypes = { };
98
+
99
+ function associateTypes(instrumentType, transactionType, userInitiated) {
100
+ const instrumentTypeCode = instrumentType.code;
101
+
102
+ if (!validTransactionTypes.hasOwnProperty(instrumentTypeCode)) {
103
+ validTransactionTypes[instrumentTypeCode] = [ ];
104
+ }
105
+
106
+ validTransactionTypes[instrumentTypeCode].push({ type: transactionType, user: userInitiated });
107
+ }
108
+
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.DIVIDEND, false);
115
+ associateTypes(InstrumentType.EQUITY, TransactionType.DIVIDEND_REINVEST, false);
116
+ associateTypes(InstrumentType.EQUITY, TransactionType.DIVIDEND_STOCK, false);
117
+ associateTypes(InstrumentType.EQUITY, TransactionType.SPLIT, false);
118
+
119
+ associateTypes(InstrumentType.FUND, TransactionType.BUY, true);
120
+ associateTypes(InstrumentType.FUND, TransactionType.SELL, true);
121
+ associateTypes(InstrumentType.FUND, TransactionType.FEE, true);
122
+ associateTypes(InstrumentType.FUND, TransactionType.FEE_UNITS, false);
123
+ associateTypes(InstrumentType.FUND, TransactionType.DISTRIBUTION_CASH, false);
124
+ associateTypes(InstrumentType.FUND, TransactionType.DISTRIBUTION_FUND, false);
125
+
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);
130
+ associateTypes(InstrumentType.OTHER, TransactionType.VALUATION, true);
131
+
132
+ associateTypes(InstrumentType.CASH, TransactionType.DEPOSIT, true);
133
+ associateTypes(InstrumentType.CASH, TransactionType.WITHDRAWAL, true);
134
+ associateTypes(InstrumentType.CASH, TransactionType.FEE, true);
135
+ associateTypes(InstrumentType.CASH, TransactionType.DEBIT, false);
136
+ associateTypes(InstrumentType.CASH, TransactionType.CREDIT, false);
137
+
138
+ const validDirections = { };
139
+
140
+ function associateDirections(instrumentType, positionDirection) {
141
+ const instrumentTypeCode = instrumentType.code;
142
+
143
+ if (!validDirections.hasOwnProperty(instrumentTypeCode)) {
144
+ validDirections[instrumentTypeCode] = [ ];
145
+ }
146
+
147
+ validDirections[instrumentTypeCode].push(positionDirection);
148
+ }
149
+
150
+ associateTypes(InstrumentType.EQUITY, PositionDirection.EVEN);
151
+ associateTypes(InstrumentType.EQUITY, PositionDirection.LONG);
152
+ associateTypes(InstrumentType.EQUITY, PositionDirection.SHORT);
153
+
154
+ associateTypes(InstrumentType.FUND, PositionDirection.EVEN);
155
+ associateTypes(InstrumentType.FUND, PositionDirection.LONG);
156
+
157
+ associateTypes(InstrumentType.OTHER, PositionDirection.EVEN);
158
+ associateTypes(InstrumentType.OTHER, PositionDirection.LONG);
159
+
160
+ associateTypes(InstrumentType.CASH, PositionDirection.EVEN);
161
+ associateTypes(InstrumentType.CASH, PositionDirection.LONG);
162
+ associateTypes(InstrumentType.CASH, PositionDirection.SHORT);
163
+
164
+ return TransactionValidator;
165
+ })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.0.216",
3
+ "version": "1.0.220",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",