mirror of https://github.com/ghostfolio/ghostfolio
committed by
GitHub
72 changed files with 505 additions and 1016 deletions
@ -1,252 +0,0 @@ |
|||||
import { Activity } from '@ghostfolio/api/app/order/interfaces/activities.interface'; |
|
||||
import { userDummyData } from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator-test-utils'; |
|
||||
import { |
|
||||
PerformanceCalculationType, |
|
||||
PortfolioCalculatorFactory |
|
||||
} from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator.factory'; |
|
||||
import { CurrentRateService } from '@ghostfolio/api/app/portfolio/current-rate.service'; |
|
||||
import { CurrentRateServiceMock } from '@ghostfolio/api/app/portfolio/current-rate.service.mock'; |
|
||||
import { RedisCacheService } from '@ghostfolio/api/app/redis-cache/redis-cache.service'; |
|
||||
import { RedisCacheServiceMock } from '@ghostfolio/api/app/redis-cache/redis-cache.service.mock'; |
|
||||
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; |
|
||||
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service'; |
|
||||
import { PortfolioSnapshotService } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service'; |
|
||||
import { PortfolioSnapshotServiceMock } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service.mock'; |
|
||||
import { parseDate } from '@ghostfolio/common/helper'; |
|
||||
|
|
||||
import { Big } from 'big.js'; |
|
||||
import { existsSync, readFileSync } from 'fs'; |
|
||||
import { last } from 'lodash'; |
|
||||
import { join } from 'path'; |
|
||||
|
|
||||
jest.mock('@ghostfolio/api/app/portfolio/current-rate.service', () => { |
|
||||
return { |
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
||||
CurrentRateService: jest.fn().mockImplementation(() => { |
|
||||
return CurrentRateServiceMock; |
|
||||
}) |
|
||||
}; |
|
||||
}); |
|
||||
|
|
||||
jest.mock( |
|
||||
'@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service', |
|
||||
() => { |
|
||||
return { |
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
||||
PortfolioSnapshotService: jest.fn().mockImplementation(() => { |
|
||||
return PortfolioSnapshotServiceMock; |
|
||||
}) |
|
||||
}; |
|
||||
} |
|
||||
); |
|
||||
|
|
||||
jest.mock('@ghostfolio/api/app/redis-cache/redis-cache.service', () => { |
|
||||
return { |
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
||||
RedisCacheService: jest.fn().mockImplementation(() => { |
|
||||
return RedisCacheServiceMock; |
|
||||
}) |
|
||||
}; |
|
||||
}); |
|
||||
|
|
||||
describe('PortfolioCalculator', () => { |
|
||||
let configurationService: ConfigurationService; |
|
||||
let currentRateService: CurrentRateService; |
|
||||
let exchangeRateDataService: ExchangeRateDataService; |
|
||||
let portfolioCalculatorFactory: PortfolioCalculatorFactory; |
|
||||
let portfolioSnapshotService: PortfolioSnapshotService; |
|
||||
let redisCacheService: RedisCacheService; |
|
||||
|
|
||||
beforeEach(() => { |
|
||||
configurationService = new ConfigurationService(); |
|
||||
|
|
||||
currentRateService = new CurrentRateService(null, null, null, null); |
|
||||
|
|
||||
exchangeRateDataService = new ExchangeRateDataService( |
|
||||
null, |
|
||||
null, |
|
||||
null, |
|
||||
null |
|
||||
); |
|
||||
|
|
||||
portfolioSnapshotService = new PortfolioSnapshotService(null); |
|
||||
|
|
||||
redisCacheService = new RedisCacheService(null, null); |
|
||||
|
|
||||
portfolioCalculatorFactory = new PortfolioCalculatorFactory( |
|
||||
configurationService, |
|
||||
currentRateService, |
|
||||
exchangeRateDataService, |
|
||||
portfolioSnapshotService, |
|
||||
redisCacheService |
|
||||
); |
|
||||
}); |
|
||||
|
|
||||
//read from activities json
|
|
||||
let activities: any[]; |
|
||||
|
|
||||
beforeAll(() => { |
|
||||
const jsonFilePath = join( |
|
||||
__dirname, |
|
||||
'../../../../../../../test/import/ok-novn-buy-and-sell.json' |
|
||||
); |
|
||||
|
|
||||
if (!existsSync(jsonFilePath)) |
|
||||
throw new Error('JSON file not found at: ' + jsonFilePath); |
|
||||
|
|
||||
const jsonData = readFileSync(jsonFilePath, 'utf8'); |
|
||||
activities = JSON.parse(jsonData).activities; |
|
||||
}); |
|
||||
|
|
||||
describe('get current positions', () => { |
|
||||
it.only('with NOVN.SW buy and sell', async () => { |
|
||||
jest.useFakeTimers().setSystemTime(parseDate('2022-04-11').getTime()); |
|
||||
|
|
||||
//map activity with json
|
|
||||
const mappedactivities: Activity[] = activities.map((activity) => ({ |
|
||||
...activity, |
|
||||
date: new Date(activity.date), |
|
||||
SymbolProfile: { |
|
||||
currency: activity.currency || 'CHF', |
|
||||
dataSource: activity.dataSource || 'YAHOO', |
|
||||
name: activity.name || 'Default Name', // provide a default name if missing
|
|
||||
symbol: activity.symbol || 'UNKNOWN' // provide a default symbol if missing
|
|
||||
} |
|
||||
})); |
|
||||
|
|
||||
const portfolioCalculator = portfolioCalculatorFactory.createCalculator({ |
|
||||
activities: mappedactivities, |
|
||||
calculationType: PerformanceCalculationType.TWR, |
|
||||
currency: 'CHF', |
|
||||
userId: userDummyData.id |
|
||||
}); |
|
||||
|
|
||||
const portfolioSnapshot = await portfolioCalculator.computeSnapshot(); |
|
||||
|
|
||||
const investments = portfolioCalculator.getInvestments(); |
|
||||
|
|
||||
const investmentsByMonth = portfolioCalculator.getInvestmentsByGroup({ |
|
||||
data: portfolioSnapshot.historicalData, |
|
||||
groupBy: 'month' |
|
||||
}); |
|
||||
|
|
||||
expect(portfolioSnapshot.historicalData[0]).toEqual({ |
|
||||
date: '2022-03-06', |
|
||||
investmentValueWithCurrencyEffect: 0, |
|
||||
netPerformance: 0, |
|
||||
netPerformanceInPercentage: 0, |
|
||||
netPerformanceInPercentageWithCurrencyEffect: 0, |
|
||||
netPerformanceWithCurrencyEffect: 0, |
|
||||
netWorth: 0, |
|
||||
totalAccountBalance: 0, |
|
||||
totalInvestment: 0, |
|
||||
totalInvestmentValueWithCurrencyEffect: 0, |
|
||||
value: 0, |
|
||||
valueWithCurrencyEffect: 0 |
|
||||
}); |
|
||||
|
|
||||
expect(portfolioSnapshot.historicalData[1]).toEqual({ |
|
||||
date: '2022-03-07', |
|
||||
investmentValueWithCurrencyEffect: 151.6, |
|
||||
netPerformance: 0, |
|
||||
netPerformanceInPercentage: 0, |
|
||||
netPerformanceInPercentageWithCurrencyEffect: 0, |
|
||||
netPerformanceWithCurrencyEffect: 0, |
|
||||
netWorth: 151.6, |
|
||||
totalAccountBalance: 0, |
|
||||
totalInvestment: 151.6, |
|
||||
totalInvestmentValueWithCurrencyEffect: 151.6, |
|
||||
value: 151.6, |
|
||||
valueWithCurrencyEffect: 151.6 |
|
||||
}); |
|
||||
|
|
||||
expect( |
|
||||
portfolioSnapshot.historicalData[ |
|
||||
portfolioSnapshot.historicalData.length - 1 |
|
||||
] |
|
||||
).toEqual({ |
|
||||
date: '2022-04-11', |
|
||||
investmentValueWithCurrencyEffect: 0, |
|
||||
netPerformance: 19.86, |
|
||||
netPerformanceInPercentage: 0.13100263852242744, |
|
||||
netPerformanceInPercentageWithCurrencyEffect: 0.13100263852242744, |
|
||||
netPerformanceWithCurrencyEffect: 19.86, |
|
||||
netWorth: 0, |
|
||||
totalAccountBalance: 0, |
|
||||
totalInvestment: 0, |
|
||||
totalInvestmentValueWithCurrencyEffect: 0, |
|
||||
value: 0, |
|
||||
valueWithCurrencyEffect: 0 |
|
||||
}); |
|
||||
|
|
||||
expect(portfolioSnapshot).toMatchObject({ |
|
||||
currentValueInBaseCurrency: new Big('0'), |
|
||||
errors: [], |
|
||||
hasErrors: false, |
|
||||
positions: [ |
|
||||
{ |
|
||||
averagePrice: new Big('0'), |
|
||||
currency: 'CHF', |
|
||||
dataSource: 'YAHOO', |
|
||||
dividend: new Big('0'), |
|
||||
dividendInBaseCurrency: new Big('0'), |
|
||||
fee: new Big('0'), |
|
||||
feeInBaseCurrency: new Big('0'), |
|
||||
firstBuyDate: '2022-03-07', |
|
||||
grossPerformance: new Big('19.86'), |
|
||||
grossPerformancePercentage: new Big('0.13100263852242744063'), |
|
||||
grossPerformancePercentageWithCurrencyEffect: new Big( |
|
||||
'0.13100263852242744063' |
|
||||
), |
|
||||
grossPerformanceWithCurrencyEffect: new Big('19.86'), |
|
||||
investment: new Big('0'), |
|
||||
investmentWithCurrencyEffect: new Big('0'), |
|
||||
netPerformance: new Big('19.86'), |
|
||||
netPerformancePercentage: new Big('0.13100263852242744063'), |
|
||||
netPerformancePercentageWithCurrencyEffectMap: { |
|
||||
max: new Big('0.13100263852242744063') |
|
||||
}, |
|
||||
netPerformanceWithCurrencyEffectMap: { |
|
||||
max: new Big('19.86') |
|
||||
}, |
|
||||
marketPrice: 87.8, |
|
||||
marketPriceInBaseCurrency: 87.8, |
|
||||
quantity: new Big('0'), |
|
||||
symbol: 'NOVN.SW', |
|
||||
tags: [], |
|
||||
timeWeightedInvestment: new Big('151.6'), |
|
||||
timeWeightedInvestmentWithCurrencyEffect: new Big('151.6'), |
|
||||
transactionCount: 2, |
|
||||
valueInBaseCurrency: new Big('0') |
|
||||
} |
|
||||
], |
|
||||
totalFeesWithCurrencyEffect: new Big('0'), |
|
||||
totalInterestWithCurrencyEffect: new Big('0'), |
|
||||
totalInvestment: new Big('0'), |
|
||||
totalInvestmentWithCurrencyEffect: new Big('0'), |
|
||||
totalLiabilitiesWithCurrencyEffect: new Big('0'), |
|
||||
totalValuablesWithCurrencyEffect: new Big('0') |
|
||||
}); |
|
||||
|
|
||||
expect(last(portfolioSnapshot.historicalData)).toMatchObject( |
|
||||
expect.objectContaining({ |
|
||||
netPerformance: 19.86, |
|
||||
netPerformanceInPercentage: 0.13100263852242744063, |
|
||||
netPerformanceInPercentageWithCurrencyEffect: 0.13100263852242744063, |
|
||||
netPerformanceWithCurrencyEffect: 19.86, |
|
||||
totalInvestmentValueWithCurrencyEffect: 0 |
|
||||
}) |
|
||||
); |
|
||||
|
|
||||
expect(investments).toEqual([ |
|
||||
{ date: '2022-03-07', investment: new Big('151.6') }, |
|
||||
{ date: '2022-04-08', investment: new Big('0') } |
|
||||
]); |
|
||||
|
|
||||
expect(investmentsByMonth).toEqual([ |
|
||||
{ date: '2022-03-01', investment: 151.6 }, |
|
||||
{ date: '2022-04-01', investment: -151.6 } |
|
||||
]); |
|
||||
}); |
|
||||
}); |
|
||||
}); |
|
@ -1,7 +1,5 @@ |
|||||
import { PortfolioReportRule } from '@ghostfolio/common/interfaces'; |
import { PortfolioReportRule } from '@ghostfolio/common/interfaces'; |
||||
import { XRayRulesSettings } from '@ghostfolio/common/types'; |
|
||||
|
|
||||
export interface IRuleSettingsDialogParams { |
export interface IRuleSettingsDialogParams { |
||||
rule: PortfolioReportRule; |
rule: PortfolioReportRule; |
||||
settings: XRayRulesSettings['AccountClusterRiskCurrentInvestment']; |
|
||||
} |
} |
||||
|
@ -1,85 +1,37 @@ |
|||||
<div mat-dialog-title>{{ data.rule.name }}</div> |
<div mat-dialog-title>{{ data.rule.name }}</div> |
||||
|
|
||||
<div class="py-3" mat-dialog-content> |
<div class="py-3" mat-dialog-content> |
||||
<div |
<mat-form-field |
||||
|
appearance="outline" |
||||
class="w-100" |
class="w-100" |
||||
[ngClass]="{ 'd-none': !data.rule.configuration.thresholdMin }" |
[ngClass]="{ 'd-none': settings.thresholdMin === undefined }" |
||||
> |
> |
||||
<h6 class="mb-0"> |
<mat-label i18n>Threshold Min</mat-label> |
||||
<ng-container i18n>Threshold Min</ng-container>: |
<input |
||||
@if (data.rule.configuration.threshold.unit === '%') { |
matInput |
||||
{{ data.settings.thresholdMin | percent: '1.2-2' }} |
|
||||
} @else { |
|
||||
{{ data.settings.thresholdMin }} |
|
||||
} |
|
||||
</h6> |
|
||||
@if (data.rule.configuration.threshold.unit === '%') { |
|
||||
<label>{{ |
|
||||
data.rule.configuration.threshold.min | percent: '1.2-2' |
|
||||
}}</label> |
|
||||
} @else { |
|
||||
<label>{{ data.rule.configuration.threshold.min }}</label> |
|
||||
} |
|
||||
<mat-slider |
|
||||
name="thresholdMin" |
name="thresholdMin" |
||||
[max]="data.rule.configuration.threshold.max" |
type="number" |
||||
[min]="data.rule.configuration.threshold.min" |
[(ngModel)]="settings.thresholdMin" |
||||
[step]="data.rule.configuration.threshold.step" |
/> |
||||
> |
</mat-form-field> |
||||
<input matSliderThumb [(ngModel)]="data.settings.thresholdMin" /> |
<mat-form-field |
||||
</mat-slider> |
appearance="outline" |
||||
@if (data.rule.configuration.threshold.unit === '%') { |
|
||||
<label>{{ |
|
||||
data.rule.configuration.threshold.max | percent: '1.2-2' |
|
||||
}}</label> |
|
||||
} @else { |
|
||||
<label>{{ data.rule.configuration.threshold.max }}</label> |
|
||||
} |
|
||||
</div> |
|
||||
<div |
|
||||
class="w-100" |
class="w-100" |
||||
[ngClass]="{ 'd-none': !data.rule.configuration.thresholdMax }" |
[ngClass]="{ 'd-none': settings.thresholdMax === undefined }" |
||||
> |
> |
||||
<h6 class="mb-0"> |
<mat-label i18n>Threshold Max</mat-label> |
||||
<ng-container i18n>Threshold Max</ng-container>: |
<input |
||||
@if (data.rule.configuration.threshold.unit === '%') { |
matInput |
||||
{{ data.settings.thresholdMax | percent: '1.2-2' }} |
|
||||
} @else { |
|
||||
{{ data.settings.thresholdMax }} |
|
||||
} |
|
||||
</h6> |
|
||||
@if (data.rule.configuration.threshold.unit === '%') { |
|
||||
<label>{{ |
|
||||
data.rule.configuration.threshold.min | percent: '1.2-2' |
|
||||
}}</label> |
|
||||
} @else { |
|
||||
<label>{{ data.rule.configuration.threshold.min }}</label> |
|
||||
} |
|
||||
<mat-slider |
|
||||
name="thresholdMax" |
name="thresholdMax" |
||||
[max]="data.rule.configuration.threshold.max" |
type="number" |
||||
[min]="data.rule.configuration.threshold.min" |
[(ngModel)]="settings.thresholdMax" |
||||
[step]="data.rule.configuration.threshold.step" |
/> |
||||
> |
</mat-form-field> |
||||
<input matSliderThumb [(ngModel)]="data.settings.thresholdMax" /> |
|
||||
</mat-slider> |
|
||||
@if (data.rule.configuration.threshold.unit === '%') { |
|
||||
<label>{{ |
|
||||
data.rule.configuration.threshold.max | percent: '1.2-2' |
|
||||
}}</label> |
|
||||
} @else { |
|
||||
<label>{{ data.rule.configuration.threshold.max }}</label> |
|
||||
} |
|
||||
</div> |
|
||||
</div> |
</div> |
||||
|
|
||||
<div align="end" mat-dialog-actions> |
<div align="end" mat-dialog-actions> |
||||
<button i18n mat-button (click)="dialogRef.close()">Close</button> |
<button i18n mat-button (click)="dialogRef.close()">Close</button> |
||||
<button |
<button color="primary" mat-flat-button (click)="dialogRef.close(settings)"> |
||||
color="primary" |
|
||||
mat-flat-button |
|
||||
(click)="dialogRef.close(data.settings)" |
|
||||
> |
|
||||
<ng-container i18n>Save</ng-container> |
<ng-container i18n>Save</ng-container> |
||||
</button> |
</button> |
||||
</div> |
</div> |
||||
|
@ -1,17 +1,11 @@ |
|||||
export interface PortfolioReportRule { |
export interface PortfolioReportRule { |
||||
configuration?: { |
|
||||
threshold?: { |
|
||||
max: number; |
|
||||
min: number; |
|
||||
step: number; |
|
||||
unit?: string; |
|
||||
}; |
|
||||
thresholdMax?: boolean; |
|
||||
thresholdMin?: boolean; |
|
||||
}; |
|
||||
evaluation?: string; |
evaluation?: string; |
||||
isActive: boolean; |
isActive: boolean; |
||||
key: string; |
key: string; |
||||
name: string; |
name: string; |
||||
|
settings?: { |
||||
|
thresholdMax?: number; |
||||
|
thresholdMin?: number; |
||||
|
}; |
||||
value?: boolean; |
value?: boolean; |
||||
} |
} |
||||
|
Loading…
Reference in new issue