@barchart/portfolio-api-common 1.23.0 → 1.24.0

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.
@@ -24,7 +24,7 @@ module.exports = (() => {
24
24
  * @param {Object} instrument
25
25
  * @param {Decimal|Number} basis
26
26
  * @param {Decimal|Number} quantity
27
- * @returns {null|Decimal}
27
+ * @returns {Decimal|null}
28
28
  */
29
29
  static calculate(instrument, basis, quantity) {
30
30
  let basisToUse = null;
@@ -6,6 +6,8 @@ const assert = require('@barchart/common-js/lang/assert'),
6
6
  is = require('@barchart/common-js/lang/is'),
7
7
  formatter = require('@barchart/common-js/lang/formatter');
8
8
 
9
+ formatter.numberToFraction = require('@barchart/marketdata-api-js/lib/utilities/format/fraction');
10
+
9
11
  const InstrumentType = require('./../data/InstrumentType'),
10
12
  TransactionType = require('./../data/TransactionType');
11
13
 
@@ -32,12 +34,14 @@ module.exports = (() => {
32
34
  * @param {Object[]} transactions
33
35
  * @param {Object[]} positions
34
36
  * @param {Boolean=} descending
37
+ * @param {Boolean=} fractions
35
38
  * @returns {Array}
36
39
  */
37
- static format(transactions, positions, descending) {
40
+ static format(transactions, positions, descending, fractions) {
38
41
  assert.argumentIsArray(transactions, 'transactions');
39
42
  assert.argumentIsArray(positions, 'positions');
40
43
  assert.argumentIsOptional(descending, 'descending', Boolean);
44
+ assert.argumentIsOptional(fractions, 'fractions', Boolean);
41
45
 
42
46
  const instruments = positions.reduce((map, p) => {
43
47
  const instrument = Object.assign({ }, p.instrument || { });
@@ -52,7 +56,7 @@ module.exports = (() => {
52
56
 
53
57
  if (instruments.hasOwnProperty(position)) {
54
58
  let instrument = instruments[position];
55
- let formatted = { instrument, raw: {} };
59
+ let formatted = { instrument, raw: { } };
56
60
 
57
61
  const formatterFunctions = formatters.get(transaction.type);
58
62
 
@@ -60,13 +64,21 @@ module.exports = (() => {
60
64
  formatterFunction(transaction, formatted);
61
65
  });
62
66
 
67
+ const code = instrument.code;
68
+
63
69
  Object.keys(formatted).forEach((key) => {
64
70
  const value = formatted[key];
65
71
 
66
72
  if (value instanceof Decimal) {
67
- const precision = instrument.currency.precision;
73
+ if (fractions && code && code.supportsFractions && (instrument.type === InstrumentType.FUTURE || instrument.type === InstrumentType.FUTURE_OPTION) && keys.fractions.some(k => k === key)) {
74
+ const rounded = code.roundToNearestTick(value.toFloat(), instrument.future ? instrument.future.tick : instrument.option.tick, true);
75
+
76
+ formatted[key] = formatter.numberToFraction(rounded, code.fractionFactor, code.fractionDigits, '-', true);
77
+ } else {
78
+ const precision = instrument.currency.precision;
68
79
 
69
- formatted[key] = formatter.numberToString(value.toFloat(), precision, ',');
80
+ formatted[key] = formatter.numberToString(value.toFloat(), precision, ',');
81
+ }
70
82
  }
71
83
  });
72
84
 
@@ -122,6 +134,10 @@ module.exports = (() => {
122
134
  }
123
135
  }
124
136
 
137
+ const keys = { };
138
+
139
+ keys.fractions = [ 'average', 'price' ];
140
+
125
141
  const basicFormatter = (t, f) => {
126
142
  f.date = t.date;
127
143
  f.type = t.type.display;
@@ -147,6 +163,7 @@ module.exports = (() => {
147
163
  }
148
164
 
149
165
  f.average = average;
166
+ f.raw.average = getRawForDecimal(average);
150
167
  };
151
168
 
152
169
  const buySellFormatter = (t, f) => {
@@ -9,6 +9,8 @@ const array = require('@barchart/common-js/lang/array'),
9
9
  is = require('@barchart/common-js/lang/is'),
10
10
  Rate = require('@barchart/common-js/lang/Rate');
11
11
 
12
+ const fractionFormatter = require('@barchart/marketdata-api-js/lib/utilities/format/fraction');
13
+
12
14
  const InstrumentType = require('./../data/InstrumentType');
13
15
 
14
16
  const PositionLevelDefinition = require('./definitions/PositionLevelDefinition'),
@@ -218,10 +220,10 @@ module.exports = (() => {
218
220
  }
219
221
 
220
222
  /**
221
- * The {@link LevelDefinition} which was used to generate this group.
223
+ * The {@link PositionLevelDefinition} which was used to generate this group.
222
224
  *
223
225
  * @public
224
- * @returns {LevelDefinition}
226
+ * @returns {PositionLevelDefinition}
225
227
  */
226
228
  get definition() {
227
229
  return this._definition;
@@ -516,10 +518,12 @@ module.exports = (() => {
516
518
  function bindItem(item) {
517
519
  const quoteBinding = item.registerQuoteChangeHandler((quote, sender) => {
518
520
  if (this._single) {
519
- const precision = sender.position.instrument.currency.precision;
521
+ const instrument = sender.position.instrument;
522
+ const currency = instrument.currency;
523
+ const precision = currency.precision;
520
524
 
521
525
  this._dataActual.currentPrice = quote.lastPrice;
522
- this._dataFormat.currentPrice = formatNumber(this._dataActual.currentPrice, precision);
526
+ this._dataFormat.currentPrice = formatFraction(this._dataActual.currentPrice, currency, instrument);
523
527
 
524
528
  this._dataActual.quoteLast = quote.previousPrice;
525
529
  this._dataActual.quoteOpen = quote.openPrice;
@@ -534,7 +538,8 @@ module.exports = (() => {
534
538
  this._dataFormat.quoteOpen = formatNumber(this._dataActual.quoteOpen, precision);
535
539
  this._dataFormat.quoteHigh = formatNumber(this._dataActual.quoteHigh, precision);
536
540
  this._dataFormat.quoteLow = formatNumber(this._dataActual.quoteLow, precision);
537
- this._dataFormat.quoteChange = formatNumber(this._dataActual.quoteChange, precision);
541
+ this._dataFormat.quoteChange = formatFraction(this._dataActual.quoteChange, currency, instrument);
542
+
538
543
  this._dataFormat.quoteChangePercent = formatPercent(new Decimal(this._dataActual.quoteChangePercent || 0), 2);
539
544
  this._dataFormat.quoteTime = this._dataActual.quoteTime;
540
545
  this._dataFormat.quoteVolume = formatNumber(this._dataActual.quoteVolume, 0);
@@ -660,6 +665,27 @@ module.exports = (() => {
660
665
  }));
661
666
  }
662
667
 
668
+ function formatFraction(value, currency, instrument) {
669
+ let decimal = value instanceof Decimal;
670
+
671
+ if (instrument) {
672
+ const type = instrument.type;
673
+ const code = instrument.code;
674
+
675
+ if (code && code.supportsFractions && (type === InstrumentType.FUTURE || type === InstrumentType.FUTURE_OPTION)) {
676
+ const rounded = code.roundToNearestTick(decimal ? value.toFloat() : value, instrument.future ? instrument.future.tick : instrument.option.tick, true);
677
+
678
+ return fractionFormatter(rounded, code.fractionFactor, code.fractionDigits, '-', true);
679
+ }
680
+ }
681
+
682
+ if (decimal) {
683
+ return formatDecimal(value, currency.precision);
684
+ } else {
685
+ return formatNumber(value, currency.precision);
686
+ }
687
+ }
688
+
663
689
  function formatNumber(number, precision) {
664
690
  if (is.number(number)) {
665
691
  return formatter.numberToString(number, precision, ',', false);
@@ -833,8 +859,7 @@ module.exports = (() => {
833
859
  format.quantityPrevious = formatDecimal(actual.quantityPrevious, 2);
834
860
 
835
861
  actual.basisPrice = item.data.basisPrice;
836
-
837
- format.basisPrice = formatCurrency(actual.basisPrice, currency);
862
+ format.basisPrice = formatFraction(actual.basisPrice, currency, item.position.instrument);
838
863
 
839
864
  actual.periodPrice = item.data.periodPrice;
840
865
  actual.periodPricePrevious = item.data.periodPricePrevious;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.23.0",
3
+ "version": "1.24.0",
4
4
  "description": "Common JavaScript code used by Barchart's Portfolio Service",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",
@@ -16,7 +16,7 @@
16
16
  },
17
17
  "dependencies": {
18
18
  "@barchart/common-js": "^4.27.0",
19
- "@barchart/marketdata-api-js": "^6.2.0",
19
+ "@barchart/marketdata-api-js": "^6.2.1",
20
20
  "uuid": "^8.3.2"
21
21
  },
22
22
  "devDependencies": {