@barchart/portfolio-api-common 1.29.2 → 1.30.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.
@@ -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) {
@@ -775,7 +805,7 @@ module.exports = (() => {
775
805
  updates.cashTotal = updates.cashTotal.add(translate(item, item.data.market));
776
806
  }
777
807
 
778
- updates.totalDivisor = updates.periodDivisorCurrent.add(translate(item, item.data.totalDivisor));
808
+ updates.totalDivisor = updates.totalDivisor.add(translate(item, item.data.totalDivisor));
779
809
  updates.periodDivisorCurrent = updates.periodDivisorCurrent.add(translate(item, item.data.periodDivisor));
780
810
  updates.periodDivisorPrevious = updates.periodDivisorPrevious.add(translate(item, item.data.periodDivisorPrevious));
781
811
  updates.periodDivisorPrevious2 = updates.periodDivisorPrevious2.add(translate(item, item.data.periodDivisorPrevious2));
@@ -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
 
@@ -447,7 +447,7 @@ module.exports = (() => {
447
447
 
448
448
  const data = item._data;
449
449
 
450
- data.initiate = guessInitiateDirection(item.previousSummaries, item.currentSummary);
450
+ data.initiate = guessInitialDirection(position, item.previousSummaries, item.currentSummary);
451
451
 
452
452
  data.quantity = snapshot.open;
453
453
  data.previousPrice = position.previous || null;
@@ -498,7 +498,7 @@ module.exports = (() => {
498
498
  data.periodPricePrevious = null;
499
499
  }
500
500
 
501
- data.totalDivisor = calculateTotalDivisor(position.instrument.type, data.initiate, currentSummary, position);
501
+ data.totalDivisor = calculateTotalDivisor(position.instrument.type, data.initiate, position);
502
502
  }
503
503
 
504
504
  function calculatePriceData(item, price) {
@@ -650,7 +650,11 @@ module.exports = (() => {
650
650
  }
651
651
  }
652
652
 
653
- function guessInitiateDirection(previousSummaries, currentSummary) {
653
+ function guessInitialDirection(position, previousSummaries, currentSummary) {
654
+ if (position.snapshot.initial) {
655
+ return position.snapshot.initial;
656
+ }
657
+
654
658
  const summaries = previousSummaries.concat(currentSummary);
655
659
 
656
660
  const direction = summaries.reduce((accumulator, summary) => {
@@ -740,24 +744,22 @@ module.exports = (() => {
740
744
  return returnRef;
741
745
  }
742
746
 
743
- function calculateTotalDivisor(type, direction, finalSummary, position) {
744
- let returnRef;
747
+ function calculateTotalDivisor(type, direction, position) {
748
+ if (type === InstrumentType.CASH) {
749
+ return Decimal.ZERO;
750
+ }
745
751
 
746
- // 2019-06-05, BRI. We should be reading from the summary -- in case we are
747
- // running for a previous period. However, the summary does not have buy and
748
- // sell totals for the entire history. Could be added.
752
+ let divisor;
749
753
 
750
- if (finalSummary && type !== InstrumentType.CASH) {
751
- if (direction === PositionDirection.SHORT) {
752
- returnRef = position.snapshot.sells;
753
- } else {
754
- returnRef = position.snapshot.buys.opposite();
755
- }
754
+ if (direction === PositionDirection.SHORT) {
755
+ divisor = position.snapshot.sells;
756
+ } else if (direction === PositionDirection.LONG) {
757
+ divisor = position.snapshot.buys.opposite();
756
758
  } else {
757
- returnRef = Decimal.ZERO;
759
+ divisor = Decimal.ZERO;
758
760
  }
759
761
 
760
- return returnRef;
762
+ return divisor;
761
763
  }
762
764
 
763
765
  function getPreviousSummary(previousSummaries, count) {
@@ -1,7 +1,6 @@
1
1
  const Currency = require('@barchart/common-js/lang/Currency'),
2
2
  DataType = require('@barchart/common-js/serialization/json/DataType'),
3
3
  Enum = require('@barchart/common-js/lang/Enum'),
4
- Schema = require('@barchart/common-js/serialization/json/Schema'),
5
4
  SchemaBuilder = require('@barchart/common-js/serialization/json/builders/SchemaBuilder'),
6
5
  Timezones = require('@barchart/common-js/lang/Timezones');
7
6
 
@@ -1,7 +1,6 @@
1
1
  const Currency = require('@barchart/common-js/lang/Currency'),
2
2
  DataType = require('@barchart/common-js/serialization/json/DataType'),
3
3
  Enum = require('@barchart/common-js/lang/Enum'),
4
- Schema = require('@barchart/common-js/serialization/json/Schema'),
5
4
  SchemaBuilder = require('@barchart/common-js/serialization/json/builders/SchemaBuilder');
6
5
 
7
6
  const UnitCode = require('@barchart/marketdata-api-js/lib/utilities/data/UnitCode');
@@ -121,6 +120,7 @@ module.exports = (() => {
121
120
  .withField('snapshot.basis', DataType.DECIMAL)
122
121
  .withField('snapshot.income', DataType.DECIMAL)
123
122
  .withField('snapshot.value', DataType.DECIMAL)
123
+ .withField('snapshot.initial', DataType.forEnum(PositionDirection, 'PositionDirection'), true)
124
124
  .withField('legacy.system', DataType.STRING, true)
125
125
  .withField('legacy.user', DataType.STRING, true)
126
126
  .withField('legacy.portfolio', DataType.STRING, true)
@@ -167,6 +167,7 @@ module.exports = (() => {
167
167
  .withField('snapshot.basis', DataType.DECIMAL)
168
168
  .withField('snapshot.income', DataType.DECIMAL)
169
169
  .withField('snapshot.value', DataType.DECIMAL)
170
+ .withField('snapshot.initial', DataType.forEnum(PositionDirection, 'PositionDirection'), true)
170
171
  .withField('system.calculate.processors', DataType.NUMBER, true)
171
172
  .withField('system.locked', DataType.BOOLEAN, true)
172
173
  .withField('previous', DataType.NUMBER, true)
@@ -1,6 +1,5 @@
1
1
  const DataType = require('@barchart/common-js/serialization/json/DataType'),
2
2
  Enum = require('@barchart/common-js/lang/Enum'),
3
- Schema = require('@barchart/common-js/serialization/json/Schema'),
4
3
  SchemaBuilder = require('@barchart/common-js/serialization/json/builders/SchemaBuilder');
5
4
 
6
5
  const PositionDirection = require('./../data/PositionDirection'),
@@ -2,7 +2,6 @@ const is = require('@barchart/common-js/lang/is'),
2
2
  Currency = require('@barchart/common-js/lang/Currency'),
3
3
  DataType = require('@barchart/common-js/serialization/json/DataType'),
4
4
  Enum = require('@barchart/common-js/lang/Enum'),
5
- Schema = require('@barchart/common-js/serialization/json/Schema'),
6
5
  SchemaBuilder = require('@barchart/common-js/serialization/json/builders/SchemaBuilder');
7
6
 
8
7
  const InstrumentType = require('./../data/InstrumentType'),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.29.2",
3
+ "version": "1.30.0",
4
4
  "description": "Common JavaScript code used by Barchart's Portfolio Service",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",