@barchart/portfolio-api-common 1.0.85 → 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
 
@@ -135,6 +138,14 @@ module.exports = (() => {
135
138
  calculatePriceData(this, null, true);
136
139
  }
137
140
 
141
+ refreshMarketPercent() {
142
+ calculateMarketPercent(this, true);
143
+ }
144
+
145
+ registerMarketPercentChangeHandler(handler) {
146
+ this._marketPercentChangeEvent.register(handler);
147
+ }
148
+
138
149
  toString() {
139
150
  return '[PositionGroup]';
140
151
  }
@@ -252,12 +263,11 @@ module.exports = (() => {
252
263
  }
253
264
 
254
265
  actual.market = updates.market;
255
- actual.marketPercent = updates.marketPercent;
256
266
  actual.unrealizedToday = updates.unrealizedToday;
257
267
  actual.total = updates.unrealizedToday.add(actual.realized).add(actual.income);
268
+
258
269
  format.market = formatCurrency(actual.market, currency);
259
- format.marketPercent = formatPercent(actual.marketPercent, 2);
260
-
270
+
261
271
  if (updates.marketDirection.up || updates.marketDirection.down) {
262
272
  format.marketDirection = unchanged;
263
273
  setTimeout(() => format.marketDirection = updates.marketDirection, 0);
@@ -267,10 +277,41 @@ module.exports = (() => {
267
277
  format.unrealizedTodayNegative = actual.unrealizedToday.getIsNegative();
268
278
  format.total = formatCurrency(actual.total, currency);
269
279
  format.totalNegative = actual.total.getIsNegative();
280
+
281
+ calculateMarketPercent(group, false);
270
282
  }
271
283
 
272
- function calculatePercent() {
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;
306
+ }
273
307
 
308
+ actual.marketPercent = marketPercent;
309
+
310
+ format.marketPercent = formatPercent(actual.marketPercent, 2);
311
+
312
+ if (!silent) {
313
+ group._marketPercentChangeEvent.fire(group);
314
+ }
274
315
  }
275
316
 
276
317
  const unchanged = { up: false, down: false };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.0.85",
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
 
@@ -1074,6 +1081,14 @@ module.exports = (() => {
1074
1081
  calculatePriceData(this, null, true);
1075
1082
  }
1076
1083
 
1084
+ refreshMarketPercent() {
1085
+ calculateMarketPercent(this, true);
1086
+ }
1087
+
1088
+ registerMarketPercentChangeHandler(handler) {
1089
+ this._marketPercentChangeEvent.register(handler);
1090
+ }
1091
+
1077
1092
  toString() {
1078
1093
  return '[PositionGroup]';
1079
1094
  }
@@ -1191,12 +1206,11 @@ module.exports = (() => {
1191
1206
  }
1192
1207
 
1193
1208
  actual.market = updates.market;
1194
- actual.marketPercent = updates.marketPercent;
1195
1209
  actual.unrealizedToday = updates.unrealizedToday;
1196
1210
  actual.total = updates.unrealizedToday.add(actual.realized).add(actual.income);
1211
+
1197
1212
  format.market = formatCurrency(actual.market, currency);
1198
- format.marketPercent = formatPercent(actual.marketPercent, 2);
1199
-
1213
+
1200
1214
  if (updates.marketDirection.up || updates.marketDirection.down) {
1201
1215
  format.marketDirection = unchanged;
1202
1216
  setTimeout(() => format.marketDirection = updates.marketDirection, 0);
@@ -1206,10 +1220,41 @@ module.exports = (() => {
1206
1220
  format.unrealizedTodayNegative = actual.unrealizedToday.getIsNegative();
1207
1221
  format.total = formatCurrency(actual.total, currency);
1208
1222
  format.totalNegative = actual.total.getIsNegative();
1223
+
1224
+ calculateMarketPercent(group, false);
1209
1225
  }
1210
1226
 
1211
- function calculatePercent() {
1227
+ function calculateMarketPercent(group, silent) {
1228
+ if (group.suspended) {
1229
+ return;
1230
+ }
1231
+
1232
+ const parent = group._parent;
1212
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;
1249
+ }
1250
+
1251
+ actual.marketPercent = marketPercent;
1252
+
1253
+ format.marketPercent = formatPercent(actual.marketPercent, 2);
1254
+
1255
+ if (!silent) {
1256
+ group._marketPercentChangeEvent.fire(group);
1257
+ }
1213
1258
  }
1214
1259
 
1215
1260
  const unchanged = { up: false, down: false };
@@ -1217,7 +1262,7 @@ module.exports = (() => {
1217
1262
  return PositionGroup;
1218
1263
  })();
1219
1264
 
1220
- },{"@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){
1221
1266
  const assert = require('@barchart/common-js/lang/assert'),
1222
1267
  is = require('@barchart/common-js/lang/is');
1223
1268
 
@@ -5871,10 +5916,10 @@ describe('When a position container data is gathered', () => {
5871
5916
  income: new Decimal(0),
5872
5917
  gain: new Decimal(0)
5873
5918
  }
5874
- }
5919
+ };
5875
5920
  }
5876
5921
 
5877
- 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', () => {
5878
5923
  let portfolios;
5879
5924
  let positions;
5880
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;