@barchart/portfolio-api-common 1.0.215 → 1.0.219

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.
@@ -118,6 +118,20 @@ 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
+ }
121
135
  }
122
136
 
123
137
  const long = new PositionDirection('LONG', 'Long', 'positive');
@@ -0,0 +1,103 @@
1
+ const assert = require('@barchart/common-js/lang/assert');
2
+
3
+ const InstrumentType = require('./InstrumentType'),
4
+ TransactionType = require('./TransactionType');
5
+
6
+ module.exports = (() => {
7
+ 'use strict';
8
+
9
+ /**
10
+ * Static utilities for validating transactions.
11
+ *
12
+ * @public
13
+ */
14
+ class TypeValidator {
15
+ constructor() {
16
+
17
+ }
18
+
19
+ /**
20
+ * Given an instrument type, returns all valid transaction types.
21
+ *
22
+ * @static
23
+ * @public
24
+ * @param {InstrumentType} instrumentType
25
+ * @param {Boolean=} userInitiated
26
+ * @return {Array.<TransactionType>}
27
+ */
28
+ static getTransactionTypesFor(instrumentType, userInitiated) {
29
+ assert.argumentIsRequired(instrumentType, 'instrumentType', InstrumentType, 'InstrumentType');
30
+ assert.argumentIsOptional(userInitiated, 'userInitiated', Boolean);
31
+
32
+ let valid = validTransactionTypes[instrumentType.code] || [ ];
33
+
34
+ if (userInitiated) {
35
+ valid = valid.filter(d => d.user === userInitiated);
36
+ }
37
+
38
+ return valid.map(d => d.type);
39
+ }
40
+
41
+ /**
42
+ * Checks to see is an transaction type is applicable to an instrument type.
43
+ *
44
+ * @static
45
+ * @public
46
+ * @param {InstrumentType} instrumentType
47
+ * @param {Boolean=} userInitiated
48
+ * @return {Boolean}
49
+ */
50
+ static validateTransactionTypeFor(instrumentType, transactionType, userInitiated) {
51
+ const transactionTypes = TypeValidator.getTransactionTypesFor(instrumentType, userInitiated);
52
+
53
+ return transactionType.some(t => t === transactionType);
54
+ }
55
+
56
+ toString() {
57
+ return '[TypeValidator]';
58
+ }
59
+ }
60
+
61
+ const validTransactionTypes = { };
62
+
63
+ function associateTypes(instrumentType, transactionType, userInitiated) {
64
+ const instrumentTypeCode = instrumentType.code;
65
+
66
+ if (!validTransactionTypes.hasOwnProperty(instrumentTypeCode)) {
67
+ validTransactionTypes[instrumentTypeCode] = [ ];
68
+ }
69
+
70
+ validTransactionTypes[instrumentTypeCode].push({ type: transactionType, user: userInitiated });
71
+ }
72
+
73
+ associateTypes(InstrumentType.EQUITY, TransactionType.BUY, true);
74
+ associateTypes(InstrumentType.EQUITY, TransactionType.SELL, true);
75
+ associateTypes(InstrumentType.EQUITY, TransactionType.SELL_SHORT, true);
76
+ associateTypes(InstrumentType.EQUITY, TransactionType.BUY_SHORT, true);
77
+ associateTypes(InstrumentType.EQUITY, TransactionType.FEE, true);
78
+ associateTypes(InstrumentType.EQUITY, TransactionType.DIVIDEND, false);
79
+ associateTypes(InstrumentType.EQUITY, TransactionType.DIVIDEND_REINVEST, false);
80
+ associateTypes(InstrumentType.EQUITY, TransactionType.DIVIDEND_STOCK, false);
81
+ associateTypes(InstrumentType.EQUITY, TransactionType.SPLIT, false);
82
+
83
+ associateTypes(InstrumentType.FUND, TransactionType.BUY, true);
84
+ associateTypes(InstrumentType.FUND, TransactionType.SELL, true);
85
+ associateTypes(InstrumentType.FUND, TransactionType.FEE, true);
86
+ associateTypes(InstrumentType.FUND, TransactionType.FEE_UNITS, false);
87
+ associateTypes(InstrumentType.FUND, TransactionType.DISTRIBUTION_CASH, false);
88
+ associateTypes(InstrumentType.FUND, TransactionType.DISTRIBUTION_FUND, false);
89
+
90
+ associateTypes(InstrumentType.OTHER, TransactionType.BUY, true);
91
+ associateTypes(InstrumentType.OTHER, TransactionType.SELL, true);
92
+ associateTypes(InstrumentType.OTHER, TransactionType.INCOME, true);
93
+ associateTypes(InstrumentType.OTHER, TransactionType.FEE, true);
94
+ associateTypes(InstrumentType.OTHER, TransactionType.VALUATION, true);
95
+
96
+ associateTypes(InstrumentType.CASH, TransactionType.DEPOSIT, true);
97
+ associateTypes(InstrumentType.CASH, TransactionType.WITHDRAWAL, true);
98
+ associateTypes(InstrumentType.CASH, TransactionType.FEE, true);
99
+ associateTypes(InstrumentType.CASH, TransactionType.DEBIT, false);
100
+ associateTypes(InstrumentType.CASH, TransactionType.CREDIT, false);
101
+
102
+ return TypeValidator;
103
+ })();
@@ -6,7 +6,8 @@ const assert = require('@barchart/common-js/lang/assert'),
6
6
  Schema = require('@barchart/common-js/serialization/json/Schema'),
7
7
  SchemaBuilder = require('@barchart/common-js/serialization/json/builders/SchemaBuilder');
8
8
 
9
- const PositionSummaryFrame = require('./../data/PositionSummaryFrame');
9
+ const PositionDirection = require('./../data/PositionDirection'),
10
+ PositionSummaryFrame = require('./../data/PositionSummaryFrame');
10
11
 
11
12
  module.exports = (() => {
12
13
  'use strict';
@@ -76,11 +77,13 @@ module.exports = (() => {
76
77
  .withField('start.date', DataType.DAY)
77
78
  .withField('start.sequence', DataType.NUMBER)
78
79
  .withField('start.open', DataType.DECIMAL)
80
+ .withField('start.direction', DataType.forEnum(PositionDirection, 'PositionDirection'))
79
81
  .withField('start.basis', DataType.DECIMAL)
80
82
  .withField('start.value', DataType.DECIMAL)
81
83
  .withField('end.date', DataType.DAY)
82
84
  .withField('end.sequence', DataType.NUMBER)
83
85
  .withField('end.open', DataType.DECIMAL)
86
+ .withField('end.direction', DataType.forEnum(PositionDirection, 'PositionDirection'))
84
87
  .withField('end.basis', DataType.DECIMAL)
85
88
  .withField('end.value', DataType.DECIMAL)
86
89
  .withField('period.buys', DataType.DECIMAL)
@@ -108,11 +111,13 @@ module.exports = (() => {
108
111
  .withField('start.date', DataType.DAY)
109
112
  .withField('start.sequence', DataType.NUMBER)
110
113
  .withField('start.open', DataType.DECIMAL)
114
+ .withField('start.direction', DataType.forEnum(PositionDirection, 'PositionDirection'))
111
115
  .withField('start.basis', DataType.DECIMAL)
112
116
  .withField('start.value', DataType.DECIMAL)
113
117
  .withField('end.date', DataType.DAY)
114
118
  .withField('end.sequence', DataType.NUMBER)
115
119
  .withField('end.open', DataType.DECIMAL)
120
+ .withField('end.direction', DataType.forEnum(PositionDirection, 'PositionDirection'))
116
121
  .withField('end.basis', DataType.DECIMAL)
117
122
  .withField('end.value', DataType.DECIMAL)
118
123
  .withField('period.buys', DataType.DECIMAL)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.0.215",
3
+ "version": "1.0.219",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",