@ -3,8 +3,13 @@ import { ToggleOption } from '@ghostfolio/client/components/toggle/interfaces/to
import { DataService } from '@ghostfolio/client/services/data.service' ;
import { ImpersonationStorageService } from '@ghostfolio/client/services/impersonation-storage.service' ;
import { UserService } from '@ghostfolio/client/services/user/user.service' ;
import { UNKNOWN_KEY } from '@ghostfolio/common/config' ;
import { PortfolioPosition , User } from '@ghostfolio/common/interfaces' ;
import { ghostfolioCashSymbol , UNKNOWN_KEY } from '@ghostfolio/common/config' ;
import {
PortfolioDetails ,
PortfolioPosition ,
User
} from '@ghostfolio/common/interfaces' ;
import { AssetClass } from '@prisma/client' ;
import { DeviceDetectorService } from 'ngx-device-detector' ;
import { Subject } from 'rxjs' ;
import { takeUntil } from 'rxjs/operators' ;
@ -31,7 +36,7 @@ export class AllocationsPageComponent implements OnDestroy, OnInit {
{ label : 'Initial' , value : 'original' } ,
{ label : 'Current' , value : 'current' }
] ;
public portfolioPositions : { [ symbol : string ] : PortfolioPosition } ;
public portfolioDetails : PortfolioDetails ;
public positions : { [ symbol : string ] : any } ;
public positionsArray : PortfolioPosition [ ] ;
public sectors : {
@ -66,11 +71,12 @@ export class AllocationsPageComponent implements OnDestroy, OnInit {
} ) ;
this . dataService
. fetchPortfolioPosition s ( { } )
. fetchPortfolioDetail s ( { } )
. pipe ( takeUntil ( this . unsubscribeSubject ) )
. subscribe ( ( response = { } ) = > {
this . portfolioPositions = response ;
this . initializeAnalysisData ( this . portfolioPositions , this . period ) ;
. subscribe ( ( portfolioDetails ) = > {
this . portfolioDetails = portfolioDetails ;
this . initializeAnalysisData ( this . period ) ;
this . changeDetectorRef . markForCheck ( ) ;
} ) ;
@ -86,12 +92,7 @@ export class AllocationsPageComponent implements OnDestroy, OnInit {
} ) ;
}
public initializeAnalysisData (
aPortfolioPositions : {
[ symbol : string ] : PortfolioPosition ;
} ,
aPeriod : string
) {
public initializeAnalysisData ( aPeriod : string ) {
this . accounts = { } ;
this . continents = {
[ UNKNOWN_KEY ] : {
@ -114,7 +115,18 @@ export class AllocationsPageComponent implements OnDestroy, OnInit {
}
} ;
for ( const [ symbol , position ] of Object . entries ( aPortfolioPositions ) ) {
for ( const [ name , { current , original } ] of Object . entries (
this . portfolioDetails . accounts
) ) {
this . accounts [ name ] = {
name ,
value : aPeriod === 'original' ? original : current
} ;
}
for ( const [ symbol , position ] of Object . entries (
this . portfolioDetails . holdings
) ) {
this . positions [ symbol ] = {
assetClass : position.assetClass ,
currency : position.currency ,
@ -126,84 +138,74 @@ export class AllocationsPageComponent implements OnDestroy, OnInit {
} ;
this . positionsArray . push ( position ) ;
for ( const [ account , { current , original } ] of Object . entries (
position . accounts
) ) {
if ( this . accounts [ account ] ? . value ) {
this . accounts [ account ] . value +=
aPeriod === 'original' ? original : current ;
} else {
this . accounts [ account ] = {
name : account ,
value : aPeriod === 'original' ? original : current
} ;
}
}
if ( position . countries . length > 0 ) {
for ( const country of position . countries ) {
const { code , continent , name , weight } = country ;
if ( this . continents [ continent ] ? . value ) {
this . continents [ continent ] . value += weight * position . value ;
} else {
this . continents [ continent ] = {
name : continent ,
value :
weight *
( aPeriod === 'original'
? this . portfolioPositions [ symbol ] . investment
: this . portfolioPositions [ symbol ] . value )
} ;
}
if ( this . countries [ code ] ? . value ) {
this . countries [ code ] . value += weight * position . value ;
} else {
this . countries [ code ] = {
name ,
value :
weight *
( aPeriod === 'original'
? this . portfolioPositions [ symbol ] . investment
: this . portfolioPositions [ symbol ] . value )
} ;
if ( position . assetClass !== AssetClass . CASH ) {
// Prepare analysis data by continents, countries and sectors except for cash
if ( position . countries . length > 0 ) {
for ( const country of position . countries ) {
const { code , continent , name , weight } = country ;
if ( this . continents [ continent ] ? . value ) {
this . continents [ continent ] . value += weight * position . value ;
} else {
this . continents [ continent ] = {
name : continent ,
value :
weight *
( aPeriod === 'original'
? this . portfolioDetails . holdings [ symbol ] . investment
: this . portfolioDetails . holdings [ symbol ] . value )
} ;
}
if ( this . countries [ code ] ? . value ) {
this . countries [ code ] . value += weight * position . value ;
} else {
this . countries [ code ] = {
name ,
value :
weight *
( aPeriod === 'original'
? this . portfolioDetails . holdings [ symbol ] . investment
: this . portfolioDetails . holdings [ symbol ] . value )
} ;
}
}
} else {
this . continents [ UNKNOWN_KEY ] . value +=
aPeriod === 'original'
? this . portfolioDetails . holdings [ symbol ] . investment
: this . portfolioDetails . holdings [ symbol ] . value ;
this . countries [ UNKNOWN_KEY ] . value +=
aPeriod === 'original'
? this . portfolioDetails . holdings [ symbol ] . investment
: this . portfolioDetails . holdings [ symbol ] . value ;
}
} else {
this . continents [ UNKNOWN_KEY ] . value +=
aPeriod === 'original'
? this . portfolioPositions [ symbol ] . investment
: this . portfolioPositions [ symbol ] . value ;
this . countries [ UNKNOWN_KEY ] . value +=
aPeriod === 'original'
? this . portfolioPositions [ symbol ] . investment
: this . portfolioPositions [ symbol ] . value ;
}
if ( position . sectors . length > 0 ) {
for ( const sector of position . sectors ) {
const { name , weight } = sector ;
if ( this . sectors [ name ] ? . value ) {
this . sectors [ name ] . value += weight * position . value ;
} else {
this . sectors [ name ] = {
name ,
value :
weight *
( aPeriod === 'original'
? this . portfolioPositions [ symbol ] . investment
: this . portfolioPositions [ symbol ] . value )
} ;
if ( position . sectors . length > 0 ) {
for ( const sector of position . sectors ) {
const { name , weight } = sector ;
if ( this . sectors [ name ] ? . value ) {
this . sectors [ name ] . value += weight * position . value ;
} else {
this . sectors [ name ] = {
name ,
value :
weight *
( aPeriod === 'original'
? this . portfolioDetails . holdings [ symbol ] . investment
: this . portfolioDetails . holdings [ symbol ] . value )
} ;
}
}
} else {
this . sectors [ UNKNOWN_KEY ] . value +=
aPeriod === 'original'
? this . portfolioDetails . holdings [ symbol ] . investment
: this . portfolioDetails . holdings [ symbol ] . value ;
}
} else {
this . sectors [ UNKNOWN_KEY ] . value +=
aPeriod === 'original'
? this . portfolioPositions [ symbol ] . investment
: this . portfolioPositions [ symbol ] . value ;
}
}
}
@ -211,7 +213,7 @@ export class AllocationsPageComponent implements OnDestroy, OnInit {
public onChangePeriod ( aValue : string ) {
this . period = aValue ;
this . initializeAnalysisData ( this . portfolioPositions , this . p eriod ) ;
this . initializeAnalysisData ( this . period ) ;
}
public ngOnDestroy() {