@barchart/portfolio-api-common 1.0.102 → 1.0.106

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.
@@ -213,12 +213,12 @@ module.exports = (() => {
213
213
  return Object.keys(this._symbols);
214
214
  }
215
215
 
216
- setPositionPrice(symbol, price) {
217
- assert.argumentIsOptional(symbol, 'symbol', String);
218
- assert.argumentIsOptional(price, 'price', Number);
216
+ setPositionQuote(symbol, quote) {
217
+ assert.argumentIsRequired(symbol, 'symbol', String);
218
+ assert.argumentIsRequired(quote, 'quote', Object);
219
219
 
220
- if (this._symbols.hasOwnProperty(symbol) && is.number(price)) {
221
- this._symbols[symbol].forEach(item => item.setPrice(price));
220
+ if (this._symbols.hasOwnProperty(symbol)) {
221
+ this._symbols[symbol].forEach(item => item.setQuote(quote));
222
222
  }
223
223
  }
224
224
 
@@ -234,9 +234,9 @@ module.exports = (() => {
234
234
  }, [ ]);
235
235
  }
236
236
 
237
- setForexPrice(symbol, price) {
238
- assert.argumentIsOptional(symbol, 'symbol', String);
239
- assert.argumentIsOptional(price, 'price', Number);
237
+ setForexQuote(symbol, quote) {
238
+ assert.argumentIsRequired(symbol, 'symbol', String);
239
+ assert.argumentIsRequired(quote, 'quote', Object);
240
240
 
241
241
  return;
242
242
  }
@@ -35,12 +35,23 @@ module.exports = (() => {
35
35
  this._dataFormat.key = this._key;
36
36
  this._dataFormat.description = this._description;
37
37
 
38
+ this._dataFormat.quantity = null;
39
+
38
40
  if (this._single) {
39
41
  this._dataFormat.instrument = items[0].position.instrument;
40
42
  } else {
41
43
  this._dataFormat.instrument = null;
42
44
  }
43
45
 
46
+ this._dataFormat.quoteLast = null;
47
+ this._dataFormat.quoteOpen = null;
48
+ this._dataFormat.quoteHigh = null;
49
+ this._dataFormat.quoteLow = null;
50
+ this._dataFormat.quoteChange = null;
51
+ this._dataFormat.quoteChangePercent = null;
52
+ this._dataFormat.quoteTime = null;
53
+ this._dataFormat.quoteVolume = null;
54
+
44
55
  this._dataActual.currentPrice = null;
45
56
  this._dataActual.previousPrice = null;
46
57
  this._dataActual.basis = null;
@@ -71,10 +82,21 @@ module.exports = (() => {
71
82
  this._dataFormat.summaryTotalPreviousNegative = false;
72
83
 
73
84
  this._items.forEach((item) => {
74
- item.registerPriceChangeHandler((data, sender) => {
85
+ item.registerQuoteChangeHandler((quote, sender) => {
75
86
  if (this._single) {
76
- this._dataActual.currentPrice = data.currentPrice;
77
- this._dataFormat.currentPrice = formatCurrency(data.currentPrice, sender.position.instrument.currency);
87
+ const precision = sender.position.instrument.currency.precision;
88
+
89
+ this._dataActual.currentPrice = sender.data.currentPrice;
90
+ this._dataFormat.currentPrice = formatNumber(this._dataActual.currentPrice, precision);
91
+
92
+ this._dataFormat.quoteLast = formatNumber(quote.previousPrice, precision);
93
+ this._dataFormat.quoteOpen = formatNumber(quote.openPrice, precision);
94
+ this._dataFormat.quoteHigh = formatNumber(quote.highPrice, precision);
95
+ this._dataFormat.quoteLow = formatNumber(quote.lowPrice, precision);
96
+ this._dataFormat.quoteChange = formatNumber(quote.priceChange, precision);
97
+ this._dataFormat.quoteChangePercent = formatPercent(quote.percentChange, 2);
98
+ this._dataFormat.quoteTime = quote.timeDisplay;
99
+ this._dataFormat.quoteVolume = formatNumber(quote.volume, 0);
78
100
  } else {
79
101
  this._dataActual.currentPrice = null;
80
102
  this._dataFormat.currentPrice = null;
@@ -163,9 +185,17 @@ module.exports = (() => {
163
185
  }
164
186
  }
165
187
 
166
- function formatNumber(decimal, precision) {
188
+ function formatNumber(number, precision) {
189
+ if (is.number(number)) {
190
+ return formatter.numberToString(number, precision, ',', false);
191
+ } else {
192
+ return '—';
193
+ }
194
+ }
195
+
196
+ function formatDecimal(decimal, precision) {
167
197
  if (decimal !== null) {
168
- return formatter.numberToString(decimal.toFloat(), precision, ',', false);
198
+ return formatNumber(decimal.toFloat(), precision);
169
199
  } else {
170
200
  return '—';
171
201
  }
@@ -173,14 +203,14 @@ module.exports = (() => {
173
203
 
174
204
  function formatPercent(decimal, precision) {
175
205
  if (decimal !== null) {
176
- return formatNumber(decimal.multiply(100), precision);
206
+ return formatDecimal(decimal.multiply(100), precision);
177
207
  } else {
178
208
  return '—';
179
209
  }
180
210
  }
181
211
 
182
212
  function formatCurrency(decimal, currency) {
183
- return formatNumber(decimal, currency.precision);
213
+ return formatDecimal(decimal, currency.precision);
184
214
  }
185
215
 
186
216
  function calculateStaticData(group) {
@@ -223,6 +253,10 @@ module.exports = (() => {
223
253
  format.summaryTotalCurrent = formatCurrency(updates.summaryTotalCurrent, currency);
224
254
  format.summaryTotalPrevious = formatCurrency(updates.summaryTotalPrevious, currency);
225
255
  format.summaryTotalPreviousNegative = updates.summaryTotalPrevious.getIsNegative();
256
+
257
+ if (group.single) {
258
+ format.quantity = formatDecimal(group.position.snapshot.open, 2);
259
+ }
226
260
  }
227
261
 
228
262
  function calculatePriceData(group, item, forceRefresh) {
@@ -16,7 +16,7 @@ module.exports = (() => {
16
16
  constructor(portfolio, position, currentSummary, previousSummaries) {
17
17
  this._portfolio = portfolio;
18
18
  this._position = position;
19
-
19
+
20
20
  this._currentSummary = currentSummary || null;
21
21
  this._previousSummaries = previousSummaries || [ ];
22
22
 
@@ -24,6 +24,9 @@ module.exports = (() => {
24
24
 
25
25
  this._data.basis = null;
26
26
 
27
+ this._currentQuote = null;
28
+ this._previousQuote = null;
29
+
27
30
  this._data.currentPrice = null;
28
31
  this._data.previousPrice = null;
29
32
 
@@ -46,7 +49,7 @@ module.exports = (() => {
46
49
  calculateStaticData(this);
47
50
  calculatePriceData(this, null);
48
51
 
49
- this._priceChangeEvent = new Event(this);
52
+ this._quoteChangedEvent = new Event(this);
50
53
  this._excludedChangeEvent = new Event(this);
51
54
  }
52
55
 
@@ -70,17 +73,24 @@ module.exports = (() => {
70
73
  return this._data;
71
74
  }
72
75
 
76
+ get quote() {
77
+ return this._currentQuote;
78
+ }
79
+
73
80
  get excluded() {
74
81
  return this._excluded;
75
82
  }
76
83
 
77
- setPrice(price) {
78
- assert.argumentIsRequired(price, 'price', Number);
84
+ setQuote(quote) {
85
+ assert.argumentIsRequired(quote, 'quote', Object);
79
86
 
80
87
  if (this._data.price !== price) {
81
- calculatePriceData(this, this._data.currentPrice = price);
88
+ this._previousQuote = this._currentQuote;
89
+ this._currentQuote = quote;
90
+
91
+ calculatePriceData(this, this._currentQuote.lastPrice);
82
92
 
83
- this._priceChangeEvent.fire(this._data);
93
+ this._quoteChangedEvent.fire(this._data, this._currentQuote);
84
94
  }
85
95
  }
86
96
 
@@ -92,8 +102,8 @@ module.exports = (() => {
92
102
  }
93
103
  }
94
104
 
95
- registerPriceChangeHandler(handler) {
96
- this._priceChangeEvent.register(handler);
105
+ registerQuoteChangeHandler(handler) {
106
+ this._quoteChangedEvent.register(handler);
97
107
  }
98
108
 
99
109
  registerExcludedChangeHandler(handler) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.0.102",
3
+ "version": "1.0.106",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",
@@ -929,12 +929,12 @@ module.exports = (() => {
929
929
  return Object.keys(this._symbols);
930
930
  }
931
931
 
932
- setPositionPrice(symbol, price) {
933
- assert.argumentIsOptional(symbol, 'symbol', String);
934
- assert.argumentIsOptional(price, 'price', Number);
932
+ setPositionQuote(symbol, quote) {
933
+ assert.argumentIsRequired(symbol, 'symbol', String);
934
+ assert.argumentIsRequired(quote, 'quote', Object);
935
935
 
936
- if (this._symbols.hasOwnProperty(symbol) && is.number(price)) {
937
- this._symbols[symbol].forEach(item => item.setPrice(price));
936
+ if (this._symbols.hasOwnProperty(symbol)) {
937
+ this._symbols[symbol].forEach(item => item.setQuote(quote));
938
938
  }
939
939
  }
940
940
 
@@ -950,9 +950,9 @@ module.exports = (() => {
950
950
  }, [ ]);
951
951
  }
952
952
 
953
- setForexPrice(symbol, price) {
954
- assert.argumentIsOptional(symbol, 'symbol', String);
955
- assert.argumentIsOptional(price, 'price', Number);
953
+ setForexQuote(symbol, quote) {
954
+ assert.argumentIsRequired(symbol, 'symbol', String);
955
+ assert.argumentIsRequired(quote, 'quote', Object);
956
956
 
957
957
  return;
958
958
  }
@@ -1038,12 +1038,23 @@ module.exports = (() => {
1038
1038
  this._dataFormat.key = this._key;
1039
1039
  this._dataFormat.description = this._description;
1040
1040
 
1041
+ this._dataFormat.quantity = null;
1042
+
1041
1043
  if (this._single) {
1042
1044
  this._dataFormat.instrument = items[0].position.instrument;
1043
1045
  } else {
1044
1046
  this._dataFormat.instrument = null;
1045
1047
  }
1046
1048
 
1049
+ this._dataFormat.quoteLast = null;
1050
+ this._dataFormat.quoteOpen = null;
1051
+ this._dataFormat.quoteHigh = null;
1052
+ this._dataFormat.quoteLow = null;
1053
+ this._dataFormat.quoteChange = null;
1054
+ this._dataFormat.quoteChangePercent = null;
1055
+ this._dataFormat.quoteTime = null;
1056
+ this._dataFormat.quoteVolume = null;
1057
+
1047
1058
  this._dataActual.currentPrice = null;
1048
1059
  this._dataActual.previousPrice = null;
1049
1060
  this._dataActual.basis = null;
@@ -1074,10 +1085,21 @@ module.exports = (() => {
1074
1085
  this._dataFormat.summaryTotalPreviousNegative = false;
1075
1086
 
1076
1087
  this._items.forEach((item) => {
1077
- item.registerPriceChangeHandler((data, sender) => {
1088
+ item.registerQuoteChangeHandler((quote, sender) => {
1078
1089
  if (this._single) {
1079
- this._dataActual.currentPrice = data.currentPrice;
1080
- this._dataFormat.currentPrice = formatCurrency(data.currentPrice, sender.position.instrument.currency);
1090
+ const precision = sender.position.instrument.currency.precision;
1091
+
1092
+ this._dataActual.currentPrice = sender.data.currentPrice;
1093
+ this._dataFormat.currentPrice = formatNumber(this._dataActual.currentPrice, precision);
1094
+
1095
+ this._dataFormat.quoteLast = formatNumber(quote.previousPrice, precision);
1096
+ this._dataFormat.quoteOpen = formatNumber(quote.openPrice, precision);
1097
+ this._dataFormat.quoteHigh = formatNumber(quote.highPrice, precision);
1098
+ this._dataFormat.quoteLow = formatNumber(quote.lowPrice, precision);
1099
+ this._dataFormat.quoteChange = formatNumber(quote.priceChange, precision);
1100
+ this._dataFormat.quoteChangePercent = formatPercent(quote.percentChange, 2);
1101
+ this._dataFormat.quoteTime = quote.timeDisplay;
1102
+ this._dataFormat.quoteVolume = formatNumber(quote.volume, 0);
1081
1103
  } else {
1082
1104
  this._dataActual.currentPrice = null;
1083
1105
  this._dataFormat.currentPrice = null;
@@ -1166,9 +1188,17 @@ module.exports = (() => {
1166
1188
  }
1167
1189
  }
1168
1190
 
1169
- function formatNumber(decimal, precision) {
1191
+ function formatNumber(number, precision) {
1192
+ if (is.number(number)) {
1193
+ return formatter.numberToString(number, precision, ',', false);
1194
+ } else {
1195
+ return '—';
1196
+ }
1197
+ }
1198
+
1199
+ function formatDecimal(decimal, precision) {
1170
1200
  if (decimal !== null) {
1171
- return formatter.numberToString(decimal.toFloat(), precision, ',', false);
1201
+ return formatNumber(decimal.toFloat(), precision);
1172
1202
  } else {
1173
1203
  return '—';
1174
1204
  }
@@ -1176,14 +1206,14 @@ module.exports = (() => {
1176
1206
 
1177
1207
  function formatPercent(decimal, precision) {
1178
1208
  if (decimal !== null) {
1179
- return formatNumber(decimal.multiply(100), precision);
1209
+ return formatDecimal(decimal.multiply(100), precision);
1180
1210
  } else {
1181
1211
  return '—';
1182
1212
  }
1183
1213
  }
1184
1214
 
1185
1215
  function formatCurrency(decimal, currency) {
1186
- return formatNumber(decimal, currency.precision);
1216
+ return formatDecimal(decimal, currency.precision);
1187
1217
  }
1188
1218
 
1189
1219
  function calculateStaticData(group) {
@@ -1226,6 +1256,10 @@ module.exports = (() => {
1226
1256
  format.summaryTotalCurrent = formatCurrency(updates.summaryTotalCurrent, currency);
1227
1257
  format.summaryTotalPrevious = formatCurrency(updates.summaryTotalPrevious, currency);
1228
1258
  format.summaryTotalPreviousNegative = updates.summaryTotalPrevious.getIsNegative();
1259
+
1260
+ if (group.single) {
1261
+ format.quantity = formatDecimal(group.position.snapshot.open, 2);
1262
+ }
1229
1263
  }
1230
1264
 
1231
1265
  function calculatePriceData(group, item, forceRefresh) {
@@ -1362,7 +1396,7 @@ module.exports = (() => {
1362
1396
  constructor(portfolio, position, currentSummary, previousSummaries) {
1363
1397
  this._portfolio = portfolio;
1364
1398
  this._position = position;
1365
-
1399
+
1366
1400
  this._currentSummary = currentSummary || null;
1367
1401
  this._previousSummaries = previousSummaries || [ ];
1368
1402
 
@@ -1370,6 +1404,9 @@ module.exports = (() => {
1370
1404
 
1371
1405
  this._data.basis = null;
1372
1406
 
1407
+ this._currentQuote = null;
1408
+ this._previousQuote = null;
1409
+
1373
1410
  this._data.currentPrice = null;
1374
1411
  this._data.previousPrice = null;
1375
1412
 
@@ -1392,7 +1429,7 @@ module.exports = (() => {
1392
1429
  calculateStaticData(this);
1393
1430
  calculatePriceData(this, null);
1394
1431
 
1395
- this._priceChangeEvent = new Event(this);
1432
+ this._quoteChangedEvent = new Event(this);
1396
1433
  this._excludedChangeEvent = new Event(this);
1397
1434
  }
1398
1435
 
@@ -1416,17 +1453,24 @@ module.exports = (() => {
1416
1453
  return this._data;
1417
1454
  }
1418
1455
 
1456
+ get quote() {
1457
+ return this._currentQuote;
1458
+ }
1459
+
1419
1460
  get excluded() {
1420
1461
  return this._excluded;
1421
1462
  }
1422
1463
 
1423
- setPrice(price) {
1424
- assert.argumentIsRequired(price, 'price', Number);
1464
+ setQuote(quote) {
1465
+ assert.argumentIsRequired(quote, 'quote', Object);
1425
1466
 
1426
1467
  if (this._data.price !== price) {
1427
- calculatePriceData(this, this._data.currentPrice = price);
1468
+ this._previousQuote = this._currentQuote;
1469
+ this._currentQuote = quote;
1470
+
1471
+ calculatePriceData(this, this._currentQuote.lastPrice);
1428
1472
 
1429
- this._priceChangeEvent.fire(this._data);
1473
+ this._quoteChangedEvent.fire(this._data, this._currentQuote);
1430
1474
  }
1431
1475
  }
1432
1476
 
@@ -1438,8 +1482,8 @@ module.exports = (() => {
1438
1482
  }
1439
1483
  }
1440
1484
 
1441
- registerPriceChangeHandler(handler) {
1442
- this._priceChangeEvent.register(handler);
1485
+ registerQuoteChangeHandler(handler) {
1486
+ this._quoteChangedEvent.register(handler);
1443
1487
  }
1444
1488
 
1445
1489
  registerExcludedChangeHandler(handler) {