@barchart/portfolio-api-common 1.29.3 → 1.31.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.
@@ -57,6 +57,14 @@ module.exports = (() => {
57
57
  return Decimal.ONE;
58
58
  }
59
59
 
60
+ function calculateForCrypto(instrument, basis, quantity) {
61
+ if (basis === null || quantity === null || quantity.getIsZero()) {
62
+ return null;
63
+ }
64
+
65
+ return basis.divide(quantity).opposite();
66
+ }
67
+
60
68
  function calculateForEquity(instrument, basis, quantity) {
61
69
  if (basis === null || quantity === null || quantity.getIsZero()) {
62
70
  return null;
@@ -116,6 +124,7 @@ module.exports = (() => {
116
124
  const calculators = new Map();
117
125
 
118
126
  calculators.set(InstrumentType.CASH, calculateForCash);
127
+ calculators.set(InstrumentType.CRYPTO, calculateForCrypto);
119
128
  calculators.set(InstrumentType.EQUITY, calculateForEquity);
120
129
  calculators.set(InstrumentType.EQUITY_OPTION, calculateForEquityOption);
121
130
  calculators.set(InstrumentType.FUND, calculateForFund);
@@ -53,6 +53,10 @@ module.exports = (() => {
53
53
  return new Decimal(quantity);
54
54
  }
55
55
 
56
+ function calculateForCrypto(instrument, price, quantity) {
57
+ return price.multiply(quantity);
58
+ }
59
+
56
60
  function calculateForEquity(instrument, price, quantity) {
57
61
  return price.multiply(quantity);
58
62
  }
@@ -88,6 +92,7 @@ module.exports = (() => {
88
92
  const calculators = new Map();
89
93
 
90
94
  calculators.set(InstrumentType.CASH, calculateForCash);
95
+ calculators.set(InstrumentType.CRYPTO, calculateForCrypto);
91
96
  calculators.set(InstrumentType.EQUITY, calculateForEquity);
92
97
  calculators.set(InstrumentType.EQUITY_OPTION, calculateForEquityOption);
93
98
  calculators.set(InstrumentType.FUND, calculateForFund);
@@ -221,6 +221,17 @@ module.exports = (() => {
221
221
  return cash;
222
222
  }
223
223
 
224
+ /**
225
+ * Crypto Tokens.
226
+ *
227
+ * @public
228
+ * @static
229
+ * @returns {InstrumentType}
230
+ */
231
+ static get CRYPTO() {
232
+ return crypto;
233
+ }
234
+
224
235
  /**
225
236
  * An equity issue.
226
237
  *
@@ -325,12 +336,14 @@ module.exports = (() => {
325
336
  return InstrumentType.EQUITY;
326
337
  } else if (code === 34) {
327
338
  return InstrumentType.EQUITY_OPTION;
328
- } else if (code === 5 || code == 15) {
339
+ } else if (code === 5 || code === 15) {
329
340
  return InstrumentType.FUND;
330
341
  } else if (code === 2) {
331
342
  return InstrumentType.FUTURE;
332
343
  } else if (code === 12) {
333
344
  return InstrumentType.FUTURE_OPTION;
345
+ } else if (code === 999) {
346
+ return InstrumentType.CRYPTO;
334
347
  } else {
335
348
  throw new Error(`Unable to determine InstrumentType for [ ${code} ]`);
336
349
  }
@@ -342,6 +355,7 @@ module.exports = (() => {
342
355
  }
343
356
 
344
357
  const cash = new InstrumentType('CASH', 'cash', 'Cash', true, false, false, true, false, false, true, false, false, false, instrument => `BARCHART-${instrument.type.code}-${instrument.currency.code}`);
358
+ const crypto = new InstrumentType('CRYPTO', 'crypto', 'Crypto', false, false, true, false, true, false, true, false, false, true, instrument => `BARCHART-CRYPTO-${instrument.name.toUpperCase()}`);
345
359
  const equity = new InstrumentType('EQUITY', 'equity', 'Equities', false, true, true, false, true, true, true, true, true, true, instrument => `BARCHART-${instrument.type.code}-${instrument.symbol.barchart}`);
346
360
  const equityOption = new InstrumentType('EQUITY_OPTION', 'equity option', 'Equity Options', false, false, true, false, true, false, false, false, false, true, instrument => `BARCHART-${instrument.type.code}-${instrument.symbol.barchart}`);
347
361
  const fund = new InstrumentType('FUND', 'mutual fund', 'Funds', false, true, false, false, true, true, true,false, true, true, instrument => `BARCHART-${instrument.type.code}-${instrument.symbol.barchart}`);
@@ -235,6 +235,11 @@ module.exports = (() => {
235
235
  validTransactionTypes[instrumentTypeCode].push({ type: transactionType, user: userInitiated, directions: directions || [ PositionDirection.LONG, PositionDirection.SHORT, PositionDirection.EVEN ] });
236
236
  }
237
237
 
238
+ associateTypes(InstrumentType.CRYPTO, TransactionType.BUY, true, [ PositionDirection.LONG, PositionDirection.EVEN ]);
239
+ associateTypes(InstrumentType.CRYPTO, TransactionType.SELL, true, [ PositionDirection.LONG ]);
240
+ associateTypes(InstrumentType.CRYPTO, TransactionType.SELL_SHORT, true, [ PositionDirection.SHORT, PositionDirection.EVEN ]);
241
+ associateTypes(InstrumentType.CRYPTO, TransactionType.BUY_SHORT, true, [ PositionDirection.SHORT ]);
242
+
238
243
  associateTypes(InstrumentType.EQUITY, TransactionType.BUY, true, [ PositionDirection.LONG, PositionDirection.EVEN ]);
239
244
  associateTypes(InstrumentType.EQUITY, TransactionType.SELL, true, [ PositionDirection.LONG ]);
240
245
  associateTypes(InstrumentType.EQUITY, TransactionType.SELL_SHORT, true, [ PositionDirection.SHORT, PositionDirection.EVEN ]);
@@ -301,6 +306,10 @@ module.exports = (() => {
301
306
  validDirections[instrumentTypeCode].push(positionDirection);
302
307
  }
303
308
 
309
+ associateDirections(InstrumentType.CRYPTO, PositionDirection.EVEN);
310
+ associateDirections(InstrumentType.CRYPTO, PositionDirection.LONG);
311
+ associateDirections(InstrumentType.CRYPTO, PositionDirection.SHORT);
312
+
304
313
  associateDirections(InstrumentType.EQUITY, PositionDirection.EVEN);
305
314
  associateDirections(InstrumentType.EQUITY, PositionDirection.LONG);
306
315
  associateDirections(InstrumentType.EQUITY, PositionDirection.SHORT);
@@ -689,6 +689,28 @@ module.exports = (() => {
689
689
  }
690
690
  }
691
691
 
692
+ function formatFractionAndTranslate(value, currency, instrument) {
693
+ let translatedCurrency;
694
+ let translatedValue;
695
+
696
+ if (currency === Currency.GBX) {
697
+ translatedCurrency = Currency.GBP;
698
+
699
+ if (value instanceof Decimal) {
700
+ translatedValue = Rate.convert(value, Currency.GBX, Currency.GBP);
701
+ } else if (is.number(value)) {
702
+ translatedValue = Rate.convert(new Decimal(value), Currency.GBX, Currency.GBP).toFloat();
703
+ } else {
704
+ translatedValue = value;
705
+ }
706
+ } else {
707
+ translatedCurrency = currency;
708
+ translatedValue = value;
709
+ }
710
+
711
+ return formatFraction(translatedValue, translatedCurrency, instrument);
712
+ }
713
+
692
714
  function formatNumber(number, precision) {
693
715
  if (is.number(number)) {
694
716
  return formatter.numberToString(number, precision, ',', false);
@@ -722,7 +744,15 @@ module.exports = (() => {
722
744
  }
723
745
 
724
746
  function formatCurrency(decimal, currency) {
725
- return formatDecimal(decimal, currency.precision);
747
+ let formatted;
748
+
749
+ if (decimal === null || currency !== Currency.GBX) {
750
+ formatted = formatDecimal(decimal, currency.precision);
751
+ } else {
752
+ formatted = formatDecimal(Rate.convert(decimal, Currency.GBX, Currency.GBP), Currency.GBP.precision);
753
+ }
754
+
755
+ return formatted;
726
756
  }
727
757
 
728
758
  function calculateStaticData(group, rates, definition) {
@@ -863,7 +893,7 @@ module.exports = (() => {
863
893
  format.quantityPrevious = formatDecimal(actual.quantityPrevious, 2);
864
894
 
865
895
  actual.basisPrice = item.data.basisPrice;
866
- format.basisPrice = formatFraction(actual.basisPrice, currency, instrument);
896
+ format.basisPrice = formatFractionAndTranslate(actual.basisPrice, currency, instrument);
867
897
 
868
898
  actual.periodPrice = item.data.periodPrice;
869
899
  actual.periodPricePrevious = item.data.periodPricePrevious;
@@ -872,7 +902,7 @@ module.exports = (() => {
872
902
  format.periodPricePrevious = formatCurrency(actual.periodPricePrevious, currency);
873
903
 
874
904
  actual.unrealizedPrice = item.data.unrealizedPrice;
875
- format.unrealizedPrice = formatFraction(actual.unrealizedPrice, currency, instrument);
905
+ format.unrealizedPrice = formatFractionAndTranslate(actual.unrealizedPrice, currency, instrument);
876
906
 
877
907
  format.invalid = definition.type === PositionLevelType.POSITION && item.invalid;
878
908
  format.locked = definition.type === PositionLevelType.POSITION && item.data.locked;
@@ -1032,7 +1062,7 @@ module.exports = (() => {
1032
1062
 
1033
1063
  if (group.single && item) {
1034
1064
  actual.unrealizedPrice = item.data.unrealizedPrice;
1035
- format.unrealizedPrice = formatFraction(actual.unrealizedPrice, currency, item.position.instrument);
1065
+ format.unrealizedPrice = formatFractionAndTranslate(actual.unrealizedPrice, currency, item.position.instrument);
1036
1066
  }
1037
1067
  }
1038
1068
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.29.3",
3
+ "version": "1.31.0",
4
4
  "description": "Common JavaScript code used by Barchart's Portfolio Service",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",