diff --git a/apps/api/src/app/benchmark/benchmark.controller.ts b/apps/api/src/app/benchmark/benchmark.controller.ts index 34409e274..b5562bf86 100644 --- a/apps/api/src/app/benchmark/benchmark.controller.ts +++ b/apps/api/src/app/benchmark/benchmark.controller.ts @@ -29,13 +29,20 @@ export class BenchmarkController { }; } - @Get(':dataSource/:symbol') + @Get(':dataSource/:symbol/:startDateString') @UseInterceptors(TransformDataSourceInRequestInterceptor) @UseGuards(AuthGuard('jwt')) public async getBenchmarkMarketDataBySymbol( @Param('dataSource') dataSource: DataSource, + @Param('startDateString') startDateString: string, @Param('symbol') symbol: string ): Promise { - return this.benchmarkService.getMarketDataBySymbol({ dataSource, symbol }); + const startDate = new Date(startDateString); + + return this.benchmarkService.getMarketDataBySymbol({ + dataSource, + startDate, + symbol + }); } } diff --git a/apps/api/src/app/benchmark/benchmark.service.ts b/apps/api/src/app/benchmark/benchmark.service.ts index 0ea259994..351ec2974 100644 --- a/apps/api/src/app/benchmark/benchmark.service.ts +++ b/apps/api/src/app/benchmark/benchmark.service.ts @@ -119,23 +119,36 @@ export class BenchmarkService { public async getMarketDataBySymbol({ dataSource, + startDate, symbol - }: UniqueAsset): Promise { + }: { startDate: Date } & UniqueAsset): Promise { const marketDataItems = await this.marketDataService.marketDataItems({ orderBy: { date: 'asc' }, where: { dataSource, - symbol + symbol, + date: { + gte: startDate + } } }); + const marketPriceAtStartDate = new Big( + marketDataItems?.[0]?.marketPrice ?? 0 + ); + return { marketData: marketDataItems.map((marketDataItem) => { return { date: format(marketDataItem.date, DATE_FORMAT), - value: marketDataItem.marketPrice + value: marketPriceAtStartDate.eq(0) + ? 0 + : new Big(marketDataItem.marketPrice) + .div(marketPriceAtStartDate) + .minus(1) + .toNumber() * 100 }; }) }; diff --git a/apps/client/src/app/pages/portfolio/analysis/analysis-page.component.ts b/apps/client/src/app/pages/portfolio/analysis/analysis-page.component.ts index 26b7ce084..124e46a78 100644 --- a/apps/client/src/app/pages/portfolio/analysis/analysis-page.component.ts +++ b/apps/client/src/app/pages/portfolio/analysis/analysis-page.component.ts @@ -28,6 +28,7 @@ export class AnalysisPageComponent implements OnDestroy, OnInit { public bottom3: Position[]; public daysInMarket: number; public deviceType: string; + public firstOrderDate: Date; public hasImpersonationId: boolean; public investments: InvestmentItem[]; public investmentsByMonth: InvestmentItem[]; @@ -67,6 +68,7 @@ export class AnalysisPageComponent implements OnDestroy, OnInit { .fetchChart({ range: 'max', version: 2 }) .pipe(takeUntil(this.unsubscribeSubject)) .subscribe(({ chart }) => { + this.firstOrderDate = new Date(chart?.[0]?.date); this.performanceDataItems = chart; this.changeDetectorRef.markForCheck(); @@ -122,9 +124,13 @@ export class AnalysisPageComponent implements OnDestroy, OnInit { }); } - public onChangeBenchmark(aBenchmark: UniqueAsset) { + public onChangeBenchmark({ dataSource, symbol }: UniqueAsset) { this.dataService - .fetchBenchmarkBySymbol(aBenchmark) + .fetchBenchmarkBySymbol({ + dataSource, + symbol, + startDate: this.firstOrderDate + }) .pipe(takeUntil(this.unsubscribeSubject)) .subscribe(({ marketData }) => { this.benchmarkDataItems = marketData.map(({ date, value }) => { diff --git a/apps/client/src/app/services/data.service.ts b/apps/client/src/app/services/data.service.ts index 46fdfe123..763bf582f 100644 --- a/apps/client/src/app/services/data.service.ts +++ b/apps/client/src/app/services/data.service.ts @@ -14,6 +14,7 @@ import { UserItem } from '@ghostfolio/api/app/user/interfaces/user-item.interfac import { UpdateUserSettingDto } from '@ghostfolio/api/app/user/update-user-setting.dto'; import { UpdateUserSettingsDto } from '@ghostfolio/api/app/user/update-user-settings.dto'; import { PropertyDto } from '@ghostfolio/api/services/property/property.dto'; +import { DATE_FORMAT } from '@ghostfolio/common/helper'; import { Access, Accounts, @@ -32,12 +33,13 @@ import { PortfolioPublicDetails, PortfolioReport, PortfolioSummary, + UniqueAsset, User } from '@ghostfolio/common/interfaces'; import { filterGlobalPermissions } from '@ghostfolio/common/permissions'; import { AccountWithValue, DateRange } from '@ghostfolio/common/types'; import { DataSource, Order as OrderModel } from '@prisma/client'; -import { parseISO } from 'date-fns'; +import { format, parseISO } from 'date-fns'; import { cloneDeep, groupBy } from 'lodash'; import { Observable } from 'rxjs'; import { map } from 'rxjs/operators'; @@ -184,13 +186,16 @@ export class DataService { public fetchBenchmarkBySymbol({ dataSource, + startDate, symbol }: { - dataSource: DataSource; - symbol: string; - }): Observable { + startDate: Date; + } & UniqueAsset): Observable { return this.http.get( - `/api/v1/benchmark/${dataSource}/${symbol}` + `/api/v1/benchmark/${dataSource}/${symbol}/${format( + startDate, + DATE_FORMAT + )}` ); }