@ -196,6 +196,7 @@ export abstract class PortfolioCalculator {
hasErrors : false ,
historicalData : [ ] ,
positions : [ ] ,
totalCashInBaseCurrency : new Big ( 0 ) ,
totalFeesWithCurrencyEffect : new Big ( 0 ) ,
totalInterestWithCurrencyEffect : new Big ( 0 ) ,
totalInvestment : new Big ( 0 ) ,
@ -204,10 +205,12 @@ export abstract class PortfolioCalculator {
} ;
}
const cashSymbols = new Set < string > ( ) ;
const currencies : { [ symbol : string ] : string } = { } ;
const dataGatheringItems : DataGatheringItem [ ] = [ ] ;
let firstIndex = transactionPoints . length ;
let firstTransactionPoint : TransactionPoint = null ;
let totalCashInBaseCurrency = new Big ( 0 ) ;
let totalInterestWithCurrencyEffect = new Big ( 0 ) ;
let totalLiabilitiesWithCurrencyEffect = new Big ( 0 ) ;
@ -316,7 +319,7 @@ export abstract class PortfolioCalculator {
const accumulatedValuesByDate : {
[ date : string ] : {
investmentValueWithCurrencyEffect : Big ;
totalAccountBalanc eWithCurrencyEffect : Big ;
totalCashValu eWithCurrencyEffect : Big ;
totalCurrentValue : Big ;
totalCurrentValueWithCurrencyEffect : Big ;
totalInvestmentValue : Big ;
@ -351,6 +354,8 @@ export abstract class PortfolioCalculator {
] ? ? 1
) ;
const valueInBaseCurrency = marketPriceInBaseCurrency . mul ( item . quantity ) ;
const {
currentValues ,
currentValuesWithCurrencyEffect ,
@ -391,25 +396,19 @@ export abstract class PortfolioCalculator {
hasAnySymbolMetricsErrors = hasAnySymbolMetricsErrors || hasErrors ;
const includeInTotalAssetValue =
item . assetSubClass !== AssetSubClass . CASH ;
if ( includeInTotalAssetValue ) {
valuesBySymbol [ item . symbol ] = {
currentValues ,
currentValuesWithCurrencyEffect ,
investmentValuesAccumulated ,
investmentValuesAccumulatedWithCurrencyEffect ,
investmentValuesWithCurrencyEffect ,
netPerformanceValues ,
netPerformanceValuesWithCurrencyEffect ,
timeWeightedInvestmentValues ,
timeWeightedInvestmentValuesWithCurrencyEffect
} ;
}
valuesBySymbol [ item . symbol ] = {
currentValues ,
currentValuesWithCurrencyEffect ,
investmentValuesAccumulated ,
investmentValuesAccumulatedWithCurrencyEffect ,
investmentValuesWithCurrencyEffect ,
netPerformanceValues ,
netPerformanceValuesWithCurrencyEffect ,
timeWeightedInvestmentValues ,
timeWeightedInvestmentValuesWithCurrencyEffect
} ;
positions . push ( {
includeInTotalAssetValue ,
timeWeightedInvestment ,
timeWeightedInvestmentWithCurrencyEffect ,
activitiesCount : item.activitiesCount ,
@ -450,11 +449,16 @@ export abstract class PortfolioCalculator {
quantity : item.quantity ,
symbol : item . symbol ,
tags : item.tags ,
valueInBaseCurrency : new Big ( marketPriceInBaseCurrency ) . mul (
item . quantity
)
valueInBaseCurrency
} ) ;
if ( item . assetSubClass === AssetSubClass . CASH ) {
cashSymbols . add ( item . symbol ) ;
totalCashInBaseCurrency =
totalCashInBaseCurrency . plus ( valueInBaseCurrency ) ;
}
totalInterestWithCurrencyEffect = totalInterestWithCurrencyEffect . plus (
totalInterestInBaseCurrency
) ;
@ -474,28 +478,7 @@ export abstract class PortfolioCalculator {
}
}
const accountBalanceItemsMap = this . accountBalanceItems . reduce (
( map , { date , value } ) = > {
map [ date ] = new Big ( value ) ;
return map ;
} ,
{ } as { [ date : string ] : Big }
) ;
const accountBalanceMap : { [ date : string ] : Big } = { } ;
let lastKnownBalance = new Big ( 0 ) ;
for ( const dateString of chartDates ) {
if ( accountBalanceItemsMap [ dateString ] !== undefined ) {
// If there's an exact balance for this date, update lastKnownBalance
lastKnownBalance = accountBalanceItemsMap [ dateString ] ;
}
// Add the most recent balance to the accountBalanceMap
accountBalanceMap [ dateString ] = lastKnownBalance ;
for ( const symbol of Object . keys ( valuesBySymbol ) ) {
const symbolValues = valuesBySymbol [ symbol ] ;
@ -538,7 +521,14 @@ export abstract class PortfolioCalculator {
accumulatedValuesByDate [ dateString ]
? . investmentValueWithCurrencyEffect ? ? new Big ( 0 )
) . add ( investmentValueWithCurrencyEffect ) ,
totalAccountBalanceWithCurrencyEffect : accountBalanceMap [ dateString ] ,
totalCashValueWithCurrencyEffect : (
accumulatedValuesByDate [ dateString ]
? . totalCashValueWithCurrencyEffect ? ? new Big ( 0 )
) . add (
cashSymbols . has ( symbol )
? currentValueWithCurrencyEffect
: new Big ( 0 )
) ,
totalCurrentValue : (
accumulatedValuesByDate [ dateString ] ? . totalCurrentValue ? ? new Big ( 0 )
) . add ( currentValue ) ,
@ -579,7 +569,7 @@ export abstract class PortfolioCalculator {
) . map ( ( [ date , values ] ) = > {
const {
investmentValueWithCurrencyEffect ,
totalAccountBalanc eWithCurrencyEffect ,
totalCashValu eWithCurrencyEffect ,
totalCurrentValue ,
totalCurrentValueWithCurrencyEffect ,
totalInvestmentValue ,
@ -612,10 +602,8 @@ export abstract class PortfolioCalculator {
netPerformance : totalNetPerformanceValue.toNumber ( ) ,
netPerformanceWithCurrencyEffect :
totalNetPerformanceValueWithCurrencyEffect . toNumber ( ) ,
netWorth : totalCurrentValueWithCurrencyEffect
. plus ( totalAccountBalanceWithCurrencyEffect )
. toNumber ( ) ,
totalAccountBalance : totalAccountBalanceWithCurrencyEffect.toNumber ( ) ,
netWorth : totalCurrentValueWithCurrencyEffect.toNumber ( ) ,
totalCashInBaseCurrency : totalCashValueWithCurrencyEffect.toNumber ( ) ,
totalInvestment : totalInvestmentValue.toNumber ( ) ,
totalInvestmentValueWithCurrencyEffect :
totalInvestmentValueWithCurrencyEffect . toNumber ( ) ,
@ -639,6 +627,7 @@ export abstract class PortfolioCalculator {
. . . overall ,
errors ,
historicalData ,
totalCashInBaseCurrency ,
totalInterestWithCurrencyEffect ,
totalLiabilitiesWithCurrencyEffect ,
hasErrors : hasAnySymbolMetricsErrors || overall . hasErrors ,
@ -776,11 +765,6 @@ export abstract class PortfolioCalculator {
? 0
: netPerformanceWithCurrencyEffectSinceStartDate /
timeWeightedInvestmentValue
// TODO: Add net worth
// netWorth: totalCurrentValueWithCurrencyEffect
// .plus(totalAccountBalanceWithCurrencyEffect)
// .toNumber()
// netWorth: 0
} ) ;
}
}