@barchart/portfolio-api-common 1.0.82 → 1.0.86

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.
@@ -153,6 +153,10 @@ module.exports = (() => {
153
153
  compositeGroups.forEach((group) => {
154
154
  const child = tree.addChild(group);
155
155
 
156
+ group.registerMarketPercentChangeHandler(() => {
157
+ this._tree.walk((childNode, childGroup) => childGroup.refreshMarketPercent());
158
+ });
159
+
156
160
  createGroups(child, group.items, additionalDefinitions);
157
161
  });
158
162
  };
@@ -1,6 +1,7 @@
1
1
  const assert = require('@barchart/common-js/lang/assert'),
2
2
  Currency = require('@barchart/common-js/lang/Currency'),
3
3
  Decimal = require('@barchart/common-js/lang/Decimal'),
4
+ Event = require('@barchart/common-js/messaging/Event'),
4
5
  formatter = require('@barchart/common-js/lang/formatter'),
5
6
  is = require('@barchart/common-js/lang/is');
6
7
 
@@ -25,6 +26,8 @@ module.exports = (() => {
25
26
  this._excluded = false;
26
27
  this._suspended = false;
27
28
 
29
+ this._marketPercentChangeEvent = new Event(this);
30
+
28
31
  this._dataFormat = { };
29
32
  this._dataActual = { };
30
33
 
@@ -52,7 +55,6 @@ module.exports = (() => {
52
55
  this._dataFormat.marketDirection = null;
53
56
  this._dataFormat.unrealizedToday = null;
54
57
  this._dataFormat.unrealizedTodayNegative = false;
55
- this._dataFormat.unrealizedTodayDirection = null;
56
58
  this._dataFormat.total = null;
57
59
  this._dataFormat.totalNegative = false;
58
60
  this._dataFormat.summaryOneTotal = null;
@@ -93,6 +95,10 @@ module.exports = (() => {
93
95
  return this._dataFormat;
94
96
  }
95
97
 
98
+ get actual() {
99
+ return this._dataActual;
100
+ }
101
+
96
102
  get single() {
97
103
  return this._single;
98
104
  }
@@ -132,6 +138,14 @@ module.exports = (() => {
132
138
  calculatePriceData(this, null, true);
133
139
  }
134
140
 
141
+ refreshMarketPercent() {
142
+ calculateMarketPercent(this, true);
143
+ }
144
+
145
+ registerMarketPercentChangeHandler(handler) {
146
+ this._marketPercentChangeEvent.register(handler);
147
+ }
148
+
135
149
  toString() {
136
150
  return '[PositionGroup]';
137
151
  }
@@ -225,16 +239,14 @@ module.exports = (() => {
225
239
  }, {
226
240
  market: Decimal.ZERO,
227
241
  marketDirection: unchanged,
228
- unrealizedToday: Decimal.ZERO,
229
- unrealizedTodayDirection: unchanged
242
+ unrealizedToday: Decimal.ZERO
230
243
 
231
244
  });
232
245
  } else {
233
246
  updates = {
234
247
  market: actual.market.add(item.data.marketChange),
235
248
  marketDirection: { up: item.data.marketChange.getIsPositive(), down: item.data.marketChange.getIsNegative() },
236
- unrealizedToday: actual.unrealizedToday.add(item.data.unrealizedTodayChange),
237
- unrealizedTodayDirection: { up: item.data.unrealizedTodayChange.getIsPositive(), down: item.data.unrealizedTodayChange.getIsNegative() }
249
+ unrealizedToday: actual.unrealizedToday.add(item.data.unrealizedTodayChange)
238
250
  };
239
251
  }
240
252
 
@@ -251,24 +263,54 @@ module.exports = (() => {
251
263
  }
252
264
 
253
265
  actual.market = updates.market;
254
- actual.marketPercent = updates.marketPercent;
255
266
  actual.unrealizedToday = updates.unrealizedToday;
256
267
  actual.total = updates.unrealizedToday.add(actual.realized).add(actual.income);
268
+
257
269
  format.market = formatCurrency(actual.market, currency);
258
- format.marketPercent = formatPercent(actual.marketPercent, 2);
270
+
271
+ if (updates.marketDirection.up || updates.marketDirection.down) {
272
+ format.marketDirection = unchanged;
273
+ setTimeout(() => format.marketDirection = updates.marketDirection, 0);
274
+ }
275
+
259
276
  format.unrealizedToday = formatCurrency(actual.unrealizedToday, currency);
260
277
  format.unrealizedTodayNegative = actual.unrealizedToday.getIsNegative();
261
278
  format.total = formatCurrency(actual.total, currency);
262
279
  format.totalNegative = actual.total.getIsNegative();
280
+
281
+ calculateMarketPercent(group, false);
282
+ }
263
283
 
264
- if (updates.marketDirection.up || updates.marketDirection.down) {
265
- format.marketDirection = unchanged;
266
- setTimeout(() => format.marketDirection = updates.marketDirection, 0);
284
+ function calculateMarketPercent(group, silent) {
285
+ if (group.suspended) {
286
+ return;
287
+ }
288
+
289
+ const parent = group._parent;
290
+
291
+ const actual = group._dataActual;
292
+ const format = group._dataFormat;
293
+
294
+ let marketPercent;
295
+
296
+ if (parent !== null) {
297
+ const parentData = parent._dataActual;
298
+
299
+ if (parentData.market !== null && !parentData.market.getIsZero()) {
300
+ marketPercent = actual.market.divide(parentData.market);
301
+ } else {
302
+ marketPercent = null;
303
+ }
304
+ } else {
305
+ marketPercent = null;
267
306
  }
268
307
 
269
- if (updates.unrealizedTodayDirection.up || updates.unrealizedTodayDirection.down) {
270
- format.unrealizedTodayDirection = unchanged;
271
- setTimeout(() => format.unrealizedTodayDirection = updates.unrealizedTodayDirection, 0);
308
+ actual.marketPercent = marketPercent;
309
+
310
+ format.marketPercent = formatPercent(actual.marketPercent, 2);
311
+
312
+ if (!silent) {
313
+ group._marketPercentChangeEvent.fire(group);
272
314
  }
273
315
  }
274
316
 
@@ -159,7 +159,7 @@ module.exports = (() => {
159
159
  }
160
160
 
161
161
  toString() {
162
- return '[TransactionSchema]';
162
+ return `[TransactionSchema (code=${this.code})]`;
163
163
  }
164
164
  }
165
165
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.0.82",
3
+ "version": "1.0.86",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",
@@ -857,6 +857,10 @@ module.exports = (() => {
857
857
  compositeGroups.forEach((group) => {
858
858
  const child = tree.addChild(group);
859
859
 
860
+ group.registerMarketPercentChangeHandler(() => {
861
+ this._tree.walk((childNode, childGroup) => childGroup.refreshMarketPercent());
862
+ });
863
+
860
864
  createGroups(child, group.items, additionalDefinitions);
861
865
  });
862
866
  };
@@ -940,6 +944,7 @@ module.exports = (() => {
940
944
  const assert = require('@barchart/common-js/lang/assert'),
941
945
  Currency = require('@barchart/common-js/lang/Currency'),
942
946
  Decimal = require('@barchart/common-js/lang/Decimal'),
947
+ Event = require('@barchart/common-js/messaging/Event'),
943
948
  formatter = require('@barchart/common-js/lang/formatter'),
944
949
  is = require('@barchart/common-js/lang/is');
945
950
 
@@ -964,6 +969,8 @@ module.exports = (() => {
964
969
  this._excluded = false;
965
970
  this._suspended = false;
966
971
 
972
+ this._marketPercentChangeEvent = new Event(this);
973
+
967
974
  this._dataFormat = { };
968
975
  this._dataActual = { };
969
976
 
@@ -991,7 +998,6 @@ module.exports = (() => {
991
998
  this._dataFormat.marketDirection = null;
992
999
  this._dataFormat.unrealizedToday = null;
993
1000
  this._dataFormat.unrealizedTodayNegative = false;
994
- this._dataFormat.unrealizedTodayDirection = null;
995
1001
  this._dataFormat.total = null;
996
1002
  this._dataFormat.totalNegative = false;
997
1003
  this._dataFormat.summaryOneTotal = null;
@@ -1032,6 +1038,10 @@ module.exports = (() => {
1032
1038
  return this._dataFormat;
1033
1039
  }
1034
1040
 
1041
+ get actual() {
1042
+ return this._dataActual;
1043
+ }
1044
+
1035
1045
  get single() {
1036
1046
  return this._single;
1037
1047
  }
@@ -1071,6 +1081,14 @@ module.exports = (() => {
1071
1081
  calculatePriceData(this, null, true);
1072
1082
  }
1073
1083
 
1084
+ refreshMarketPercent() {
1085
+ calculateMarketPercent(this, true);
1086
+ }
1087
+
1088
+ registerMarketPercentChangeHandler(handler) {
1089
+ this._marketPercentChangeEvent.register(handler);
1090
+ }
1091
+
1074
1092
  toString() {
1075
1093
  return '[PositionGroup]';
1076
1094
  }
@@ -1164,16 +1182,14 @@ module.exports = (() => {
1164
1182
  }, {
1165
1183
  market: Decimal.ZERO,
1166
1184
  marketDirection: unchanged,
1167
- unrealizedToday: Decimal.ZERO,
1168
- unrealizedTodayDirection: unchanged
1185
+ unrealizedToday: Decimal.ZERO
1169
1186
 
1170
1187
  });
1171
1188
  } else {
1172
1189
  updates = {
1173
1190
  market: actual.market.add(item.data.marketChange),
1174
1191
  marketDirection: { up: item.data.marketChange.getIsPositive(), down: item.data.marketChange.getIsNegative() },
1175
- unrealizedToday: actual.unrealizedToday.add(item.data.unrealizedTodayChange),
1176
- unrealizedTodayDirection: { up: item.data.unrealizedTodayChange.getIsPositive(), down: item.data.unrealizedTodayChange.getIsNegative() }
1192
+ unrealizedToday: actual.unrealizedToday.add(item.data.unrealizedTodayChange)
1177
1193
  };
1178
1194
  }
1179
1195
 
@@ -1190,24 +1206,54 @@ module.exports = (() => {
1190
1206
  }
1191
1207
 
1192
1208
  actual.market = updates.market;
1193
- actual.marketPercent = updates.marketPercent;
1194
1209
  actual.unrealizedToday = updates.unrealizedToday;
1195
1210
  actual.total = updates.unrealizedToday.add(actual.realized).add(actual.income);
1211
+
1196
1212
  format.market = formatCurrency(actual.market, currency);
1197
- format.marketPercent = formatPercent(actual.marketPercent, 2);
1213
+
1214
+ if (updates.marketDirection.up || updates.marketDirection.down) {
1215
+ format.marketDirection = unchanged;
1216
+ setTimeout(() => format.marketDirection = updates.marketDirection, 0);
1217
+ }
1218
+
1198
1219
  format.unrealizedToday = formatCurrency(actual.unrealizedToday, currency);
1199
1220
  format.unrealizedTodayNegative = actual.unrealizedToday.getIsNegative();
1200
1221
  format.total = formatCurrency(actual.total, currency);
1201
1222
  format.totalNegative = actual.total.getIsNegative();
1223
+
1224
+ calculateMarketPercent(group, false);
1225
+ }
1202
1226
 
1203
- if (updates.marketDirection.up || updates.marketDirection.down) {
1204
- format.marketDirection = unchanged;
1205
- setTimeout(() => format.marketDirection = updates.marketDirection, 0);
1227
+ function calculateMarketPercent(group, silent) {
1228
+ if (group.suspended) {
1229
+ return;
1230
+ }
1231
+
1232
+ const parent = group._parent;
1233
+
1234
+ const actual = group._dataActual;
1235
+ const format = group._dataFormat;
1236
+
1237
+ let marketPercent;
1238
+
1239
+ if (parent !== null) {
1240
+ const parentData = parent._dataActual;
1241
+
1242
+ if (parentData.market !== null && !parentData.market.getIsZero()) {
1243
+ marketPercent = actual.market.divide(parentData.market);
1244
+ } else {
1245
+ marketPercent = null;
1246
+ }
1247
+ } else {
1248
+ marketPercent = null;
1206
1249
  }
1207
1250
 
1208
- if (updates.unrealizedTodayDirection.up || updates.unrealizedTodayDirection.down) {
1209
- format.unrealizedTodayDirection = unchanged;
1210
- setTimeout(() => format.unrealizedTodayDirection = updates.unrealizedTodayDirection, 0);
1251
+ actual.marketPercent = marketPercent;
1252
+
1253
+ format.marketPercent = formatPercent(actual.marketPercent, 2);
1254
+
1255
+ if (!silent) {
1256
+ group._marketPercentChangeEvent.fire(group);
1211
1257
  }
1212
1258
  }
1213
1259
 
@@ -1216,7 +1262,7 @@ module.exports = (() => {
1216
1262
  return PositionGroup;
1217
1263
  })();
1218
1264
 
1219
- },{"@barchart/common-js/lang/Currency":11,"@barchart/common-js/lang/Decimal":13,"@barchart/common-js/lang/assert":17,"@barchart/common-js/lang/formatter":18,"@barchart/common-js/lang/is":19}],6:[function(require,module,exports){
1265
+ },{"@barchart/common-js/lang/Currency":11,"@barchart/common-js/lang/Decimal":13,"@barchart/common-js/lang/assert":17,"@barchart/common-js/lang/formatter":18,"@barchart/common-js/lang/is":19,"@barchart/common-js/messaging/Event":20}],6:[function(require,module,exports){
1220
1266
  const assert = require('@barchart/common-js/lang/assert'),
1221
1267
  is = require('@barchart/common-js/lang/is');
1222
1268
 
@@ -5870,10 +5916,10 @@ describe('When a position container data is gathered', () => {
5870
5916
  income: new Decimal(0),
5871
5917
  gain: new Decimal(0)
5872
5918
  }
5873
- }
5919
+ };
5874
5920
  }
5875
5921
 
5876
- describe('for two portfolios, each with the same position, and the second portfolio with an additonal position', () => {
5922
+ describe('for two portfolios, each with the same position, and the second portfolio with an addition position', () => {
5877
5923
  let portfolios;
5878
5924
  let positions;
5879
5925
  let summaries;
@@ -29,10 +29,10 @@ describe('When a position container data is gathered', () => {
29
29
  income: new Decimal(0),
30
30
  gain: new Decimal(0)
31
31
  }
32
- }
32
+ };
33
33
  }
34
34
 
35
- describe('for two portfolios, each with the same position, and the second portfolio with an additonal position', () => {
35
+ describe('for two portfolios, each with the same position, and the second portfolio with an addition position', () => {
36
36
  let portfolios;
37
37
  let positions;
38
38
  let summaries;