@barchart/portfolio-api-common 1.0.83 → 1.0.88
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((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
|
|
|
@@ -92,6 +95,10 @@ module.exports = (() => {
|
|
|
92
95
|
return this._dataFormat;
|
|
93
96
|
}
|
|
94
97
|
|
|
98
|
+
get actual() {
|
|
99
|
+
return this._dataActual;
|
|
100
|
+
}
|
|
101
|
+
|
|
95
102
|
get single() {
|
|
96
103
|
return this._single;
|
|
97
104
|
}
|
|
@@ -131,6 +138,14 @@ module.exports = (() => {
|
|
|
131
138
|
calculatePriceData(this, null, true);
|
|
132
139
|
}
|
|
133
140
|
|
|
141
|
+
refreshMarketPercent() {
|
|
142
|
+
calculateMarketPercent(this, true);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
registerMarketPercentChangeHandler(handler) {
|
|
146
|
+
this._marketPercentChangeEvent.register(handler);
|
|
147
|
+
}
|
|
148
|
+
|
|
134
149
|
toString() {
|
|
135
150
|
return '[PositionGroup]';
|
|
136
151
|
}
|
|
@@ -248,12 +263,11 @@ module.exports = (() => {
|
|
|
248
263
|
}
|
|
249
264
|
|
|
250
265
|
actual.market = updates.market;
|
|
251
|
-
actual.marketPercent = updates.marketPercent;
|
|
252
266
|
actual.unrealizedToday = updates.unrealizedToday;
|
|
253
267
|
actual.total = updates.unrealizedToday.add(actual.realized).add(actual.income);
|
|
268
|
+
|
|
254
269
|
format.market = formatCurrency(actual.market, currency);
|
|
255
|
-
|
|
256
|
-
|
|
270
|
+
|
|
257
271
|
if (updates.marketDirection.up || updates.marketDirection.down) {
|
|
258
272
|
format.marketDirection = unchanged;
|
|
259
273
|
setTimeout(() => format.marketDirection = updates.marketDirection, 0);
|
|
@@ -263,6 +277,41 @@ module.exports = (() => {
|
|
|
263
277
|
format.unrealizedTodayNegative = actual.unrealizedToday.getIsNegative();
|
|
264
278
|
format.total = formatCurrency(actual.total, currency);
|
|
265
279
|
format.totalNegative = actual.total.getIsNegative();
|
|
280
|
+
|
|
281
|
+
calculateMarketPercent(group, false);
|
|
282
|
+
}
|
|
283
|
+
|
|
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
|
+
}
|
|
307
|
+
|
|
308
|
+
actual.marketPercent = marketPercent;
|
|
309
|
+
|
|
310
|
+
format.marketPercent = formatPercent(actual.marketPercent, 2);
|
|
311
|
+
|
|
312
|
+
if (!silent) {
|
|
313
|
+
group._marketPercentChangeEvent.fire(group);
|
|
314
|
+
}
|
|
266
315
|
}
|
|
267
316
|
|
|
268
317
|
const unchanged = { up: false, down: false };
|
package/package.json
CHANGED
package/test/SpecRunner.js
CHANGED
|
@@ -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((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
|
|
|
@@ -1031,6 +1038,10 @@ module.exports = (() => {
|
|
|
1031
1038
|
return this._dataFormat;
|
|
1032
1039
|
}
|
|
1033
1040
|
|
|
1041
|
+
get actual() {
|
|
1042
|
+
return this._dataActual;
|
|
1043
|
+
}
|
|
1044
|
+
|
|
1034
1045
|
get single() {
|
|
1035
1046
|
return this._single;
|
|
1036
1047
|
}
|
|
@@ -1070,6 +1081,14 @@ module.exports = (() => {
|
|
|
1070
1081
|
calculatePriceData(this, null, true);
|
|
1071
1082
|
}
|
|
1072
1083
|
|
|
1084
|
+
refreshMarketPercent() {
|
|
1085
|
+
calculateMarketPercent(this, true);
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1088
|
+
registerMarketPercentChangeHandler(handler) {
|
|
1089
|
+
this._marketPercentChangeEvent.register(handler);
|
|
1090
|
+
}
|
|
1091
|
+
|
|
1073
1092
|
toString() {
|
|
1074
1093
|
return '[PositionGroup]';
|
|
1075
1094
|
}
|
|
@@ -1187,12 +1206,11 @@ module.exports = (() => {
|
|
|
1187
1206
|
}
|
|
1188
1207
|
|
|
1189
1208
|
actual.market = updates.market;
|
|
1190
|
-
actual.marketPercent = updates.marketPercent;
|
|
1191
1209
|
actual.unrealizedToday = updates.unrealizedToday;
|
|
1192
1210
|
actual.total = updates.unrealizedToday.add(actual.realized).add(actual.income);
|
|
1211
|
+
|
|
1193
1212
|
format.market = formatCurrency(actual.market, currency);
|
|
1194
|
-
|
|
1195
|
-
|
|
1213
|
+
|
|
1196
1214
|
if (updates.marketDirection.up || updates.marketDirection.down) {
|
|
1197
1215
|
format.marketDirection = unchanged;
|
|
1198
1216
|
setTimeout(() => format.marketDirection = updates.marketDirection, 0);
|
|
@@ -1202,6 +1220,41 @@ module.exports = (() => {
|
|
|
1202
1220
|
format.unrealizedTodayNegative = actual.unrealizedToday.getIsNegative();
|
|
1203
1221
|
format.total = formatCurrency(actual.total, currency);
|
|
1204
1222
|
format.totalNegative = actual.total.getIsNegative();
|
|
1223
|
+
|
|
1224
|
+
calculateMarketPercent(group, false);
|
|
1225
|
+
}
|
|
1226
|
+
|
|
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;
|
|
1249
|
+
}
|
|
1250
|
+
|
|
1251
|
+
actual.marketPercent = marketPercent;
|
|
1252
|
+
|
|
1253
|
+
format.marketPercent = formatPercent(actual.marketPercent, 2);
|
|
1254
|
+
|
|
1255
|
+
if (!silent) {
|
|
1256
|
+
group._marketPercentChangeEvent.fire(group);
|
|
1257
|
+
}
|
|
1205
1258
|
}
|
|
1206
1259
|
|
|
1207
1260
|
const unchanged = { up: false, down: false };
|
|
@@ -1209,7 +1262,7 @@ module.exports = (() => {
|
|
|
1209
1262
|
return PositionGroup;
|
|
1210
1263
|
})();
|
|
1211
1264
|
|
|
1212
|
-
},{"@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){
|
|
1213
1266
|
const assert = require('@barchart/common-js/lang/assert'),
|
|
1214
1267
|
is = require('@barchart/common-js/lang/is');
|
|
1215
1268
|
|
|
@@ -5863,10 +5916,10 @@ describe('When a position container data is gathered', () => {
|
|
|
5863
5916
|
income: new Decimal(0),
|
|
5864
5917
|
gain: new Decimal(0)
|
|
5865
5918
|
}
|
|
5866
|
-
}
|
|
5919
|
+
};
|
|
5867
5920
|
}
|
|
5868
5921
|
|
|
5869
|
-
describe('for two portfolios, each with the same position, and the second portfolio with an
|
|
5922
|
+
describe('for two portfolios, each with the same position, and the second portfolio with an addition position', () => {
|
|
5870
5923
|
let portfolios;
|
|
5871
5924
|
let positions;
|
|
5872
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
|
|
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;
|