mirror of https://github.com/ghostfolio/ghostfolio
committed by
GitHub
194 changed files with 5238 additions and 3391 deletions
@ -0,0 +1,72 @@ |
|||||
|
--- |
||||
|
name: karpathy-guidelines |
||||
|
description: Behavioral guidelines to reduce common LLM coding mistakes. Use when writing, reviewing, or refactoring code to avoid overcomplication, make surgical changes, surface assumptions, and define verifiable success criteria. |
||||
|
license: MIT |
||||
|
--- |
||||
|
|
||||
|
# Karpathy Guidelines |
||||
|
|
||||
|
Behavioral guidelines to reduce common LLM coding mistakes, derived from [Andrej Karpathy's observations](https://x.com/karpathy/status/2015883857489522876) on LLM coding pitfalls. |
||||
|
|
||||
|
**Tradeoff:** These guidelines bias toward caution over speed. For trivial tasks, use judgment. |
||||
|
|
||||
|
## 1. Think Before Coding |
||||
|
|
||||
|
**Don't assume. Don't hide confusion. Surface tradeoffs.** |
||||
|
|
||||
|
Before implementing: |
||||
|
|
||||
|
- State your assumptions explicitly. If uncertain, ask. |
||||
|
- If multiple interpretations exist, present them - don't pick silently. |
||||
|
- If a simpler approach exists, say so. Push back when warranted. |
||||
|
- If something is unclear, stop. Name what's confusing. Ask. |
||||
|
|
||||
|
## 2. Simplicity First |
||||
|
|
||||
|
**Minimum code that solves the problem. Nothing speculative.** |
||||
|
|
||||
|
- No features beyond what was asked. |
||||
|
- No abstractions for single-use code. |
||||
|
- No "flexibility" or "configurability" that wasn't requested. |
||||
|
- No error handling for impossible scenarios. |
||||
|
- If you write 200 lines and it could be 50, rewrite it. |
||||
|
|
||||
|
Ask yourself: "Would a senior engineer say this is overcomplicated?" If yes, simplify. |
||||
|
|
||||
|
## 3. Surgical Changes |
||||
|
|
||||
|
**Touch only what you must. Clean up only your own mess.** |
||||
|
|
||||
|
When editing existing code: |
||||
|
|
||||
|
- Don't "improve" adjacent code, comments, or formatting. |
||||
|
- Don't refactor things that aren't broken. |
||||
|
- Match existing style, even if you'd do it differently. |
||||
|
- If you notice unrelated dead code, mention it - don't delete it. |
||||
|
|
||||
|
When your changes create orphans: |
||||
|
|
||||
|
- Remove imports/variables/functions that YOUR changes made unused. |
||||
|
- Don't remove pre-existing dead code unless asked. |
||||
|
|
||||
|
The test: Every changed line should trace directly to the user's request. |
||||
|
|
||||
|
## 4. Goal-Driven Execution |
||||
|
|
||||
|
**Define success criteria. Loop until verified.** |
||||
|
|
||||
|
Transform tasks into verifiable goals: |
||||
|
|
||||
|
- "Add validation" → "Write tests for invalid inputs, then make them pass" |
||||
|
- "Fix the bug" → "Write a test that reproduces it, then make it pass" |
||||
|
- "Refactor X" → "Ensure tests pass before and after" |
||||
|
|
||||
|
For multi-step tasks, state a brief plan: |
||||
|
|
||||
|
``` |
||||
|
1. [Step] → verify: [check] |
||||
|
2. [Step] → verify: [check] |
||||
|
3. [Step] → verify: [check] |
||||
|
``` |
||||
|
|
||||
|
Strong success criteria let you loop independently. Weak criteria ("make it work") require constant clarification. |
||||
@ -0,0 +1 @@ |
|||||
|
../../.agents/skills/karpathy-guidelines |
||||
@ -0,0 +1,29 @@ |
|||||
|
import * as config from '@ghostfolio/common/config'; |
||||
|
import type { PropertyKey } from '@ghostfolio/common/types'; |
||||
|
|
||||
|
import { BadRequestException, Injectable, PipeTransform } from '@nestjs/common'; |
||||
|
|
||||
|
@Injectable() |
||||
|
export class PropertyKeyPipe implements PipeTransform<string, PropertyKey> { |
||||
|
private readonly allowedKeys: Set<string>; |
||||
|
|
||||
|
public constructor() { |
||||
|
this.allowedKeys = new Set<string>( |
||||
|
Object.entries(config) |
||||
|
.filter(([key]) => { |
||||
|
return key.startsWith('PROPERTY_'); |
||||
|
}) |
||||
|
.map(([, value]) => { |
||||
|
return value as string; |
||||
|
}) |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
public transform(value: string): PropertyKey { |
||||
|
if (!this.allowedKeys.has(value)) { |
||||
|
throw new BadRequestException(`Invalid property key: ${value}`); |
||||
|
} |
||||
|
|
||||
|
return value as PropertyKey; |
||||
|
} |
||||
|
} |
||||
@ -1,231 +1,231 @@ |
|||||
import { |
import { |
||||
activityDummyData, |
activityDummyData, |
||||
symbolProfileDummyData, |
assetProfileDummyData, |
||||
userDummyData |
userDummyData |
||||
} from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator-test-utils'; |
} from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator-test-utils'; |
||||
import { PortfolioCalculatorFactory } from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator.factory'; |
import { PortfolioCalculatorFactory } from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator.factory'; |
||||
import { CurrentRateService } from '@ghostfolio/api/app/portfolio/current-rate.service'; |
import { CurrentRateService } from '@ghostfolio/api/app/portfolio/current-rate.service'; |
||||
import { CurrentRateServiceMock } from '@ghostfolio/api/app/portfolio/current-rate.service.mock'; |
import { CurrentRateServiceMock } from '@ghostfolio/api/app/portfolio/current-rate.service.mock'; |
||||
import { RedisCacheService } from '@ghostfolio/api/app/redis-cache/redis-cache.service'; |
import { RedisCacheService } from '@ghostfolio/api/app/redis-cache/redis-cache.service'; |
||||
import { RedisCacheServiceMock } from '@ghostfolio/api/app/redis-cache/redis-cache.service.mock'; |
import { RedisCacheServiceMock } from '@ghostfolio/api/app/redis-cache/redis-cache.service.mock'; |
||||
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; |
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; |
||||
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.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 { 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 { PortfolioSnapshotServiceMock } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service.mock'; |
||||
import { parseDate } from '@ghostfolio/common/helper'; |
import { parseDate } from '@ghostfolio/common/helper'; |
||||
import { Activity } from '@ghostfolio/common/interfaces'; |
import { Activity } from '@ghostfolio/common/interfaces'; |
||||
import { PerformanceCalculationType } from '@ghostfolio/common/types/performance-calculation-type.type'; |
import { PerformanceCalculationType } from '@ghostfolio/common/types/performance-calculation-type.type'; |
||||
|
|
||||
import { Big } from 'big.js'; |
import { Big } from 'big.js'; |
||||
|
|
||||
jest.mock('@ghostfolio/api/app/portfolio/current-rate.service', () => { |
jest.mock('@ghostfolio/api/app/portfolio/current-rate.service', () => { |
||||
return { |
return { |
||||
CurrentRateService: jest.fn().mockImplementation(() => { |
CurrentRateService: jest.fn().mockImplementation(() => { |
||||
return CurrentRateServiceMock; |
return CurrentRateServiceMock; |
||||
}) |
}) |
||||
}; |
}; |
||||
}); |
}); |
||||
|
|
||||
jest.mock( |
jest.mock( |
||||
'@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service', |
'@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service', |
||||
() => { |
() => { |
||||
return { |
return { |
||||
PortfolioSnapshotService: jest.fn().mockImplementation(() => { |
PortfolioSnapshotService: jest.fn().mockImplementation(() => { |
||||
return PortfolioSnapshotServiceMock; |
return PortfolioSnapshotServiceMock; |
||||
}) |
}) |
||||
}; |
}; |
||||
} |
} |
||||
); |
); |
||||
|
|
||||
jest.mock('@ghostfolio/api/app/redis-cache/redis-cache.service', () => { |
jest.mock('@ghostfolio/api/app/redis-cache/redis-cache.service', () => { |
||||
return { |
return { |
||||
RedisCacheService: jest.fn().mockImplementation(() => { |
RedisCacheService: jest.fn().mockImplementation(() => { |
||||
return RedisCacheServiceMock; |
return RedisCacheServiceMock; |
||||
}) |
}) |
||||
}; |
}; |
||||
}); |
}); |
||||
|
|
||||
describe('PortfolioCalculator', () => { |
describe('PortfolioCalculator', () => { |
||||
let configurationService: ConfigurationService; |
let configurationService: ConfigurationService; |
||||
let currentRateService: CurrentRateService; |
let currentRateService: CurrentRateService; |
||||
let exchangeRateDataService: ExchangeRateDataService; |
let exchangeRateDataService: ExchangeRateDataService; |
||||
let portfolioCalculatorFactory: PortfolioCalculatorFactory; |
let portfolioCalculatorFactory: PortfolioCalculatorFactory; |
||||
let portfolioSnapshotService: PortfolioSnapshotService; |
let portfolioSnapshotService: PortfolioSnapshotService; |
||||
let redisCacheService: RedisCacheService; |
let redisCacheService: RedisCacheService; |
||||
|
|
||||
beforeEach(() => { |
beforeEach(() => { |
||||
configurationService = new ConfigurationService(); |
configurationService = new ConfigurationService(); |
||||
|
|
||||
currentRateService = new CurrentRateService(null, null, null, null); |
currentRateService = new CurrentRateService(null, null, null, null); |
||||
|
|
||||
exchangeRateDataService = new ExchangeRateDataService( |
exchangeRateDataService = new ExchangeRateDataService( |
||||
null, |
null, |
||||
null, |
null, |
||||
null, |
null, |
||||
null |
null |
||||
); |
); |
||||
|
|
||||
portfolioSnapshotService = new PortfolioSnapshotService(null, null); |
portfolioSnapshotService = new PortfolioSnapshotService(null, null); |
||||
|
|
||||
redisCacheService = new RedisCacheService(null, null); |
redisCacheService = new RedisCacheService(null, null); |
||||
|
|
||||
portfolioCalculatorFactory = new PortfolioCalculatorFactory( |
portfolioCalculatorFactory = new PortfolioCalculatorFactory( |
||||
configurationService, |
configurationService, |
||||
currentRateService, |
currentRateService, |
||||
exchangeRateDataService, |
exchangeRateDataService, |
||||
portfolioSnapshotService, |
portfolioSnapshotService, |
||||
redisCacheService |
redisCacheService |
||||
); |
); |
||||
}); |
}); |
||||
|
|
||||
describe('get current positions', () => { |
describe('get current positions', () => { |
||||
it.only('with BALN.SW buy and sell in two activities', async () => { |
it.only('with BALN.SW buy and sell in two activities', async () => { |
||||
jest.useFakeTimers().setSystemTime(parseDate('2021-12-18').getTime()); |
jest.useFakeTimers().setSystemTime(parseDate('2021-12-18').getTime()); |
||||
|
|
||||
const activities: Activity[] = [ |
const activities: Activity[] = [ |
||||
{ |
{ |
||||
...activityDummyData, |
...activityDummyData, |
||||
date: new Date('2021-11-22'), |
assetProfile: { |
||||
feeInAssetProfileCurrency: 1.55, |
...assetProfileDummyData, |
||||
feeInBaseCurrency: 1.55, |
currency: 'CHF', |
||||
quantity: 2, |
dataSource: 'YAHOO', |
||||
SymbolProfile: { |
name: 'Bâloise Holding AG', |
||||
...symbolProfileDummyData, |
symbol: 'BALN.SW' |
||||
currency: 'CHF', |
}, |
||||
dataSource: 'YAHOO', |
date: new Date('2021-11-22'), |
||||
name: 'Bâloise Holding AG', |
feeInAssetProfileCurrency: 1.55, |
||||
symbol: 'BALN.SW' |
feeInBaseCurrency: 1.55, |
||||
}, |
quantity: 2, |
||||
type: 'BUY', |
type: 'BUY', |
||||
unitPriceInAssetProfileCurrency: 142.9 |
unitPriceInAssetProfileCurrency: 142.9 |
||||
}, |
}, |
||||
{ |
{ |
||||
...activityDummyData, |
...activityDummyData, |
||||
date: new Date('2021-11-30'), |
assetProfile: { |
||||
feeInAssetProfileCurrency: 1.65, |
...assetProfileDummyData, |
||||
feeInBaseCurrency: 1.65, |
currency: 'CHF', |
||||
quantity: 1, |
dataSource: 'YAHOO', |
||||
SymbolProfile: { |
name: 'Bâloise Holding AG', |
||||
...symbolProfileDummyData, |
symbol: 'BALN.SW' |
||||
currency: 'CHF', |
}, |
||||
dataSource: 'YAHOO', |
date: new Date('2021-11-30'), |
||||
name: 'Bâloise Holding AG', |
feeInAssetProfileCurrency: 1.65, |
||||
symbol: 'BALN.SW' |
feeInBaseCurrency: 1.65, |
||||
}, |
quantity: 1, |
||||
type: 'SELL', |
type: 'SELL', |
||||
unitPriceInAssetProfileCurrency: 136.6 |
unitPriceInAssetProfileCurrency: 136.6 |
||||
}, |
}, |
||||
{ |
{ |
||||
...activityDummyData, |
...activityDummyData, |
||||
date: new Date('2021-11-30'), |
assetProfile: { |
||||
feeInAssetProfileCurrency: 0, |
...assetProfileDummyData, |
||||
feeInBaseCurrency: 0, |
currency: 'CHF', |
||||
quantity: 1, |
dataSource: 'YAHOO', |
||||
SymbolProfile: { |
name: 'Bâloise Holding AG', |
||||
...symbolProfileDummyData, |
symbol: 'BALN.SW' |
||||
currency: 'CHF', |
}, |
||||
dataSource: 'YAHOO', |
date: new Date('2021-11-30'), |
||||
name: 'Bâloise Holding AG', |
feeInAssetProfileCurrency: 0, |
||||
symbol: 'BALN.SW' |
feeInBaseCurrency: 0, |
||||
}, |
quantity: 1, |
||||
type: 'SELL', |
type: 'SELL', |
||||
unitPriceInAssetProfileCurrency: 136.6 |
unitPriceInAssetProfileCurrency: 136.6 |
||||
} |
} |
||||
]; |
]; |
||||
|
|
||||
const portfolioCalculator = portfolioCalculatorFactory.createCalculator({ |
const portfolioCalculator = portfolioCalculatorFactory.createCalculator({ |
||||
activities, |
activities, |
||||
calculationType: PerformanceCalculationType.ROAI, |
calculationType: PerformanceCalculationType.ROAI, |
||||
currency: 'CHF', |
currency: 'CHF', |
||||
userId: userDummyData.id |
userId: userDummyData.id |
||||
}); |
}); |
||||
|
|
||||
const portfolioSnapshot = await portfolioCalculator.computeSnapshot(); |
const portfolioSnapshot = await portfolioCalculator.computeSnapshot(); |
||||
|
|
||||
const investments = portfolioCalculator.getInvestments(); |
const investments = portfolioCalculator.getInvestments(); |
||||
|
|
||||
const investmentsByMonth = portfolioCalculator.getInvestmentsByGroup({ |
const investmentsByMonth = portfolioCalculator.getInvestmentsByGroup({ |
||||
data: portfolioSnapshot.historicalData, |
data: portfolioSnapshot.historicalData, |
||||
groupBy: 'month' |
groupBy: 'month' |
||||
}); |
}); |
||||
|
|
||||
const investmentsByYear = portfolioCalculator.getInvestmentsByGroup({ |
const investmentsByYear = portfolioCalculator.getInvestmentsByGroup({ |
||||
data: portfolioSnapshot.historicalData, |
data: portfolioSnapshot.historicalData, |
||||
groupBy: 'year' |
groupBy: 'year' |
||||
}); |
}); |
||||
|
|
||||
expect(portfolioSnapshot).toMatchObject({ |
expect(portfolioSnapshot).toMatchObject({ |
||||
currentValueInBaseCurrency: new Big('0'), |
currentValueInBaseCurrency: new Big('0'), |
||||
errors: [], |
errors: [], |
||||
hasErrors: false, |
hasErrors: false, |
||||
positions: [ |
positions: [ |
||||
{ |
{ |
||||
activitiesCount: 3, |
activitiesCount: 3, |
||||
averagePrice: new Big('0'), |
averagePrice: new Big('0'), |
||||
currency: 'CHF', |
currency: 'CHF', |
||||
dataSource: 'YAHOO', |
dataSource: 'YAHOO', |
||||
dateOfFirstActivity: '2021-11-22', |
dateOfFirstActivity: '2021-11-22', |
||||
dividend: new Big('0'), |
dividend: new Big('0'), |
||||
dividendInBaseCurrency: new Big('0'), |
dividendInBaseCurrency: new Big('0'), |
||||
fee: new Big('3.2'), |
fee: new Big('3.2'), |
||||
feeInBaseCurrency: new Big('3.2'), |
feeInBaseCurrency: new Big('3.2'), |
||||
grossPerformance: new Big('-12.6'), |
grossPerformance: new Big('-12.6'), |
||||
grossPerformancePercentage: new Big('-0.04408677396780965649'), |
grossPerformancePercentage: new Big('-0.04408677396780965649'), |
||||
grossPerformancePercentageWithCurrencyEffect: new Big( |
grossPerformancePercentageWithCurrencyEffect: new Big( |
||||
'-0.04408677396780965649' |
'-0.04408677396780965649' |
||||
), |
), |
||||
grossPerformanceWithCurrencyEffect: new Big('-12.6'), |
grossPerformanceWithCurrencyEffect: new Big('-12.6'), |
||||
investment: new Big('0'), |
investment: new Big('0'), |
||||
investmentWithCurrencyEffect: new Big('0'), |
investmentWithCurrencyEffect: new Big('0'), |
||||
netPerformancePercentageWithCurrencyEffectMap: { |
netPerformancePercentageWithCurrencyEffectMap: { |
||||
max: new Big('-0.0552834149755073478') |
max: new Big('-0.0552834149755073478') |
||||
}, |
}, |
||||
netPerformanceWithCurrencyEffectMap: { |
netPerformanceWithCurrencyEffectMap: { |
||||
max: new Big('-15.8') |
max: new Big('-15.8') |
||||
}, |
}, |
||||
marketPrice: 148.9, |
marketPrice: 148.9, |
||||
marketPriceInBaseCurrency: 148.9, |
marketPriceInBaseCurrency: 148.9, |
||||
quantity: new Big('0'), |
quantity: new Big('0'), |
||||
symbol: 'BALN.SW', |
symbol: 'BALN.SW', |
||||
tags: [], |
tags: [], |
||||
timeWeightedInvestment: new Big('285.80000000000000396627'), |
timeWeightedInvestment: new Big('285.80000000000000396627'), |
||||
timeWeightedInvestmentWithCurrencyEffect: new Big( |
timeWeightedInvestmentWithCurrencyEffect: new Big( |
||||
'285.80000000000000396627' |
'285.80000000000000396627' |
||||
), |
), |
||||
valueInBaseCurrency: new Big('0') |
valueInBaseCurrency: new Big('0') |
||||
} |
} |
||||
], |
], |
||||
totalFeesWithCurrencyEffect: new Big('3.2'), |
totalFeesWithCurrencyEffect: new Big('3.2'), |
||||
totalInterestWithCurrencyEffect: new Big('0'), |
totalInterestWithCurrencyEffect: new Big('0'), |
||||
totalInvestment: new Big('0'), |
totalInvestment: new Big('0'), |
||||
totalInvestmentWithCurrencyEffect: new Big('0'), |
totalInvestmentWithCurrencyEffect: new Big('0'), |
||||
totalLiabilitiesWithCurrencyEffect: new Big('0') |
totalLiabilitiesWithCurrencyEffect: new Big('0') |
||||
}); |
}); |
||||
|
|
||||
expect(portfolioSnapshot.historicalData.at(-1)).toMatchObject( |
expect(portfolioSnapshot.historicalData.at(-1)).toMatchObject( |
||||
expect.objectContaining({ |
expect.objectContaining({ |
||||
netPerformance: -15.8, |
netPerformance: -15.8, |
||||
netPerformanceInPercentage: -0.05528341497550734703, |
netPerformanceInPercentage: -0.05528341497550734703, |
||||
netPerformanceInPercentageWithCurrencyEffect: -0.05528341497550734703, |
netPerformanceInPercentageWithCurrencyEffect: -0.05528341497550734703, |
||||
netPerformanceWithCurrencyEffect: -15.8, |
netPerformanceWithCurrencyEffect: -15.8, |
||||
totalInvestment: 0, |
totalInvestment: 0, |
||||
totalInvestmentValueWithCurrencyEffect: 0 |
totalInvestmentValueWithCurrencyEffect: 0 |
||||
}) |
}) |
||||
); |
); |
||||
|
|
||||
expect(investments).toEqual([ |
expect(investments).toEqual([ |
||||
{ date: '2021-11-22', investment: new Big('285.8') }, |
{ date: '2021-11-22', investment: new Big('285.8') }, |
||||
{ date: '2021-11-30', investment: new Big('0') } |
{ date: '2021-11-30', investment: new Big('0') } |
||||
]); |
]); |
||||
|
|
||||
expect(investmentsByMonth).toEqual([ |
expect(investmentsByMonth).toEqual([ |
||||
{ date: '2021-11-01', investment: 0 }, |
{ date: '2021-11-01', investment: 0 }, |
||||
{ date: '2021-12-01', investment: 0 } |
{ date: '2021-12-01', investment: 0 } |
||||
]); |
]); |
||||
|
|
||||
expect(investmentsByYear).toEqual([ |
expect(investmentsByYear).toEqual([ |
||||
{ date: '2021-01-01', investment: 0 } |
{ date: '2021-01-01', investment: 0 } |
||||
]); |
]); |
||||
}); |
}); |
||||
}); |
}); |
||||
}); |
}); |
||||
|
|||||
@ -1,190 +1,190 @@ |
|||||
import { |
import { |
||||
activityDummyData, |
activityDummyData, |
||||
loadExportFile, |
assetProfileDummyData, |
||||
symbolProfileDummyData, |
loadExportFile, |
||||
userDummyData |
userDummyData |
||||
} from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator-test-utils'; |
} from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator-test-utils'; |
||||
import { PortfolioCalculatorFactory } from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator.factory'; |
import { PortfolioCalculatorFactory } from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator.factory'; |
||||
import { CurrentRateService } from '@ghostfolio/api/app/portfolio/current-rate.service'; |
import { CurrentRateService } from '@ghostfolio/api/app/portfolio/current-rate.service'; |
||||
import { CurrentRateServiceMock } from '@ghostfolio/api/app/portfolio/current-rate.service.mock'; |
import { CurrentRateServiceMock } from '@ghostfolio/api/app/portfolio/current-rate.service.mock'; |
||||
import { RedisCacheService } from '@ghostfolio/api/app/redis-cache/redis-cache.service'; |
import { RedisCacheService } from '@ghostfolio/api/app/redis-cache/redis-cache.service'; |
||||
import { RedisCacheServiceMock } from '@ghostfolio/api/app/redis-cache/redis-cache.service.mock'; |
import { RedisCacheServiceMock } from '@ghostfolio/api/app/redis-cache/redis-cache.service.mock'; |
||||
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; |
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; |
||||
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.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 { 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 { PortfolioSnapshotServiceMock } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service.mock'; |
||||
import { parseDate } from '@ghostfolio/common/helper'; |
import { parseDate } from '@ghostfolio/common/helper'; |
||||
import { Activity, ExportResponse } from '@ghostfolio/common/interfaces'; |
import { Activity, ExportResponse } from '@ghostfolio/common/interfaces'; |
||||
import { PerformanceCalculationType } from '@ghostfolio/common/types/performance-calculation-type.type'; |
import { PerformanceCalculationType } from '@ghostfolio/common/types/performance-calculation-type.type'; |
||||
|
|
||||
import { Big } from 'big.js'; |
import { Big } from 'big.js'; |
||||
import { join } from 'node:path'; |
import { join } from 'node:path'; |
||||
|
|
||||
jest.mock('@ghostfolio/api/app/portfolio/current-rate.service', () => { |
jest.mock('@ghostfolio/api/app/portfolio/current-rate.service', () => { |
||||
return { |
return { |
||||
CurrentRateService: jest.fn().mockImplementation(() => { |
CurrentRateService: jest.fn().mockImplementation(() => { |
||||
return CurrentRateServiceMock; |
return CurrentRateServiceMock; |
||||
}) |
}) |
||||
}; |
}; |
||||
}); |
}); |
||||
|
|
||||
jest.mock( |
jest.mock( |
||||
'@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service', |
'@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service', |
||||
() => { |
() => { |
||||
return { |
return { |
||||
PortfolioSnapshotService: jest.fn().mockImplementation(() => { |
PortfolioSnapshotService: jest.fn().mockImplementation(() => { |
||||
return PortfolioSnapshotServiceMock; |
return PortfolioSnapshotServiceMock; |
||||
}) |
}) |
||||
}; |
}; |
||||
} |
} |
||||
); |
); |
||||
|
|
||||
jest.mock('@ghostfolio/api/app/redis-cache/redis-cache.service', () => { |
jest.mock('@ghostfolio/api/app/redis-cache/redis-cache.service', () => { |
||||
return { |
return { |
||||
RedisCacheService: jest.fn().mockImplementation(() => { |
RedisCacheService: jest.fn().mockImplementation(() => { |
||||
return RedisCacheServiceMock; |
return RedisCacheServiceMock; |
||||
}) |
}) |
||||
}; |
}; |
||||
}); |
}); |
||||
|
|
||||
describe('PortfolioCalculator', () => { |
describe('PortfolioCalculator', () => { |
||||
let exportResponse: ExportResponse; |
let exportResponse: ExportResponse; |
||||
|
|
||||
let configurationService: ConfigurationService; |
let configurationService: ConfigurationService; |
||||
let currentRateService: CurrentRateService; |
let currentRateService: CurrentRateService; |
||||
let exchangeRateDataService: ExchangeRateDataService; |
let exchangeRateDataService: ExchangeRateDataService; |
||||
let portfolioCalculatorFactory: PortfolioCalculatorFactory; |
let portfolioCalculatorFactory: PortfolioCalculatorFactory; |
||||
let portfolioSnapshotService: PortfolioSnapshotService; |
let portfolioSnapshotService: PortfolioSnapshotService; |
||||
let redisCacheService: RedisCacheService; |
let redisCacheService: RedisCacheService; |
||||
|
|
||||
beforeAll(() => { |
beforeAll(() => { |
||||
exportResponse = loadExportFile( |
exportResponse = loadExportFile( |
||||
join( |
join( |
||||
__dirname, |
__dirname, |
||||
'../../../../../../../test/import/ok/jnug-buy-and-sell-and-buy-and-sell.json' |
'../../../../../../../test/import/ok/jnug-buy-and-sell-and-buy-and-sell.json' |
||||
) |
) |
||||
); |
); |
||||
}); |
}); |
||||
|
|
||||
beforeEach(() => { |
beforeEach(() => { |
||||
configurationService = new ConfigurationService(); |
configurationService = new ConfigurationService(); |
||||
|
|
||||
currentRateService = new CurrentRateService(null, null, null, null); |
currentRateService = new CurrentRateService(null, null, null, null); |
||||
|
|
||||
exchangeRateDataService = new ExchangeRateDataService( |
exchangeRateDataService = new ExchangeRateDataService( |
||||
null, |
null, |
||||
null, |
null, |
||||
null, |
null, |
||||
null |
null |
||||
); |
); |
||||
|
|
||||
portfolioSnapshotService = new PortfolioSnapshotService(null, null); |
portfolioSnapshotService = new PortfolioSnapshotService(null, null); |
||||
|
|
||||
redisCacheService = new RedisCacheService(null, null); |
redisCacheService = new RedisCacheService(null, null); |
||||
|
|
||||
portfolioCalculatorFactory = new PortfolioCalculatorFactory( |
portfolioCalculatorFactory = new PortfolioCalculatorFactory( |
||||
configurationService, |
configurationService, |
||||
currentRateService, |
currentRateService, |
||||
exchangeRateDataService, |
exchangeRateDataService, |
||||
portfolioSnapshotService, |
portfolioSnapshotService, |
||||
redisCacheService |
redisCacheService |
||||
); |
); |
||||
}); |
}); |
||||
|
|
||||
describe('get current positions', () => { |
describe('get current positions', () => { |
||||
it.only('with JNUG buy and sell', async () => { |
it.only('with JNUG buy and sell', async () => { |
||||
jest.useFakeTimers().setSystemTime(parseDate('2025-12-28').getTime()); |
jest.useFakeTimers().setSystemTime(parseDate('2025-12-28').getTime()); |
||||
|
|
||||
const activities: Activity[] = exportResponse.activities.map( |
const activities: Activity[] = exportResponse.activities.map( |
||||
(activity) => ({ |
(activity) => ({ |
||||
...activityDummyData, |
...activityDummyData, |
||||
...activity, |
...activity, |
||||
date: parseDate(activity.date), |
assetProfile: { |
||||
feeInAssetProfileCurrency: activity.fee, |
...assetProfileDummyData, |
||||
feeInBaseCurrency: activity.fee, |
currency: activity.currency, |
||||
SymbolProfile: { |
dataSource: activity.dataSource, |
||||
...symbolProfileDummyData, |
name: 'Direxion Daily Junior Gold Miners Index Bull 2X Shares', |
||||
currency: activity.currency, |
symbol: activity.symbol |
||||
dataSource: activity.dataSource, |
}, |
||||
name: 'Direxion Daily Junior Gold Miners Index Bull 2X Shares', |
date: parseDate(activity.date), |
||||
symbol: activity.symbol |
feeInAssetProfileCurrency: activity.fee, |
||||
}, |
feeInBaseCurrency: activity.fee, |
||||
unitPriceInAssetProfileCurrency: activity.unitPrice |
unitPriceInAssetProfileCurrency: activity.unitPrice |
||||
}) |
}) |
||||
); |
); |
||||
|
|
||||
const portfolioCalculator = portfolioCalculatorFactory.createCalculator({ |
const portfolioCalculator = portfolioCalculatorFactory.createCalculator({ |
||||
activities, |
activities, |
||||
calculationType: PerformanceCalculationType.ROAI, |
calculationType: PerformanceCalculationType.ROAI, |
||||
currency: exportResponse.user.settings.currency, |
currency: exportResponse.user.settings.currency, |
||||
userId: userDummyData.id |
userId: userDummyData.id |
||||
}); |
}); |
||||
|
|
||||
const portfolioSnapshot = await portfolioCalculator.computeSnapshot(); |
const portfolioSnapshot = await portfolioCalculator.computeSnapshot(); |
||||
|
|
||||
const investments = portfolioCalculator.getInvestments(); |
const investments = portfolioCalculator.getInvestments(); |
||||
|
|
||||
const investmentsByMonth = portfolioCalculator.getInvestmentsByGroup({ |
const investmentsByMonth = portfolioCalculator.getInvestmentsByGroup({ |
||||
data: portfolioSnapshot.historicalData, |
data: portfolioSnapshot.historicalData, |
||||
groupBy: 'month' |
groupBy: 'month' |
||||
}); |
}); |
||||
|
|
||||
const investmentsByYear = portfolioCalculator.getInvestmentsByGroup({ |
const investmentsByYear = portfolioCalculator.getInvestmentsByGroup({ |
||||
data: portfolioSnapshot.historicalData, |
data: portfolioSnapshot.historicalData, |
||||
groupBy: 'year' |
groupBy: 'year' |
||||
}); |
}); |
||||
|
|
||||
expect(portfolioSnapshot).toMatchObject({ |
expect(portfolioSnapshot).toMatchObject({ |
||||
currentValueInBaseCurrency: new Big('0'), |
currentValueInBaseCurrency: new Big('0'), |
||||
errors: [], |
errors: [], |
||||
hasErrors: false, |
hasErrors: false, |
||||
positions: [ |
positions: [ |
||||
{ |
{ |
||||
activitiesCount: 4, |
activitiesCount: 4, |
||||
averagePrice: new Big('0'), |
averagePrice: new Big('0'), |
||||
currency: 'USD', |
currency: 'USD', |
||||
dataSource: 'YAHOO', |
dataSource: 'YAHOO', |
||||
dateOfFirstActivity: '2025-12-11', |
dateOfFirstActivity: '2025-12-11', |
||||
dividend: new Big('0'), |
dividend: new Big('0'), |
||||
dividendInBaseCurrency: new Big('0'), |
dividendInBaseCurrency: new Big('0'), |
||||
fee: new Big('4'), |
fee: new Big('4'), |
||||
feeInBaseCurrency: new Big('4'), |
feeInBaseCurrency: new Big('4'), |
||||
grossPerformance: new Big('43.95'), // (1890.00 - 1885.05) + (2080.10 - 2041.10)
|
grossPerformance: new Big('43.95'), // (1890.00 - 1885.05) + (2080.10 - 2041.10)
|
||||
grossPerformanceWithCurrencyEffect: new Big('43.95'), // (1890.00 - 1885.05) + (2080.10 - 2041.10)
|
grossPerformanceWithCurrencyEffect: new Big('43.95'), // (1890.00 - 1885.05) + (2080.10 - 2041.10)
|
||||
investment: new Big('0'), |
investment: new Big('0'), |
||||
investmentWithCurrencyEffect: new Big('0'), |
investmentWithCurrencyEffect: new Big('0'), |
||||
netPerformance: new Big('39.95'), // (1890.00 - 1885.05) + (2080.10 - 2041.10) - 4
|
netPerformance: new Big('39.95'), // (1890.00 - 1885.05) + (2080.10 - 2041.10) - 4
|
||||
netPerformanceWithCurrencyEffectMap: { |
netPerformanceWithCurrencyEffectMap: { |
||||
max: new Big('39.95') // (1890.00 - 1885.05) + (2080.10 - 2041.10) - 4
|
max: new Big('39.95') // (1890.00 - 1885.05) + (2080.10 - 2041.10) - 4
|
||||
}, |
}, |
||||
marketPrice: 237.8000030517578, |
marketPrice: 237.8000030517578, |
||||
marketPriceInBaseCurrency: 237.8000030517578, |
marketPriceInBaseCurrency: 237.8000030517578, |
||||
quantity: new Big('0'), |
quantity: new Big('0'), |
||||
symbol: 'JNUG', |
symbol: 'JNUG', |
||||
tags: [], |
tags: [], |
||||
valueInBaseCurrency: new Big('0') |
valueInBaseCurrency: new Big('0') |
||||
} |
} |
||||
], |
], |
||||
totalFeesWithCurrencyEffect: new Big('4'), |
totalFeesWithCurrencyEffect: new Big('4'), |
||||
totalInterestWithCurrencyEffect: new Big('0'), |
totalInterestWithCurrencyEffect: new Big('0'), |
||||
totalInvestment: new Big('0'), |
totalInvestment: new Big('0'), |
||||
totalInvestmentWithCurrencyEffect: new Big('0'), |
totalInvestmentWithCurrencyEffect: new Big('0'), |
||||
totalLiabilitiesWithCurrencyEffect: new Big('0') |
totalLiabilitiesWithCurrencyEffect: new Big('0') |
||||
}); |
}); |
||||
|
|
||||
expect(investments).toEqual([ |
expect(investments).toEqual([ |
||||
{ date: '2025-12-11', investment: new Big('1885.05') }, |
{ date: '2025-12-11', investment: new Big('1885.05') }, |
||||
{ date: '2025-12-18', investment: new Big('2041.1') }, |
{ date: '2025-12-18', investment: new Big('2041.1') }, |
||||
{ date: '2025-12-28', investment: new Big('0') } |
{ date: '2025-12-28', investment: new Big('0') } |
||||
]); |
]); |
||||
|
|
||||
expect(investmentsByMonth).toEqual([ |
expect(investmentsByMonth).toEqual([ |
||||
{ date: '2025-12-01', investment: 0 } |
{ date: '2025-12-01', investment: 0 } |
||||
]); |
]); |
||||
|
|
||||
expect(investmentsByYear).toEqual([ |
expect(investmentsByYear).toEqual([ |
||||
{ date: '2025-01-01', investment: 0 } |
{ date: '2025-01-01', investment: 0 } |
||||
]); |
]); |
||||
}); |
}); |
||||
}); |
}); |
||||
}); |
}); |
||||
|
|||||
@ -1,264 +1,264 @@ |
|||||
import { |
import { |
||||
activityDummyData, |
activityDummyData, |
||||
loadExportFile, |
assetProfileDummyData, |
||||
symbolProfileDummyData, |
loadExportFile, |
||||
userDummyData |
userDummyData |
||||
} from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator-test-utils'; |
} from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator-test-utils'; |
||||
import { PortfolioCalculatorFactory } from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator.factory'; |
import { PortfolioCalculatorFactory } from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator.factory'; |
||||
import { CurrentRateService } from '@ghostfolio/api/app/portfolio/current-rate.service'; |
import { CurrentRateService } from '@ghostfolio/api/app/portfolio/current-rate.service'; |
||||
import { CurrentRateServiceMock } from '@ghostfolio/api/app/portfolio/current-rate.service.mock'; |
import { CurrentRateServiceMock } from '@ghostfolio/api/app/portfolio/current-rate.service.mock'; |
||||
import { RedisCacheService } from '@ghostfolio/api/app/redis-cache/redis-cache.service'; |
import { RedisCacheService } from '@ghostfolio/api/app/redis-cache/redis-cache.service'; |
||||
import { RedisCacheServiceMock } from '@ghostfolio/api/app/redis-cache/redis-cache.service.mock'; |
import { RedisCacheServiceMock } from '@ghostfolio/api/app/redis-cache/redis-cache.service.mock'; |
||||
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; |
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; |
||||
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.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 { 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 { PortfolioSnapshotServiceMock } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service.mock'; |
||||
import { parseDate } from '@ghostfolio/common/helper'; |
import { parseDate } from '@ghostfolio/common/helper'; |
||||
import { Activity, ExportResponse } from '@ghostfolio/common/interfaces'; |
import { Activity, ExportResponse } from '@ghostfolio/common/interfaces'; |
||||
import { PerformanceCalculationType } from '@ghostfolio/common/types/performance-calculation-type.type'; |
import { PerformanceCalculationType } from '@ghostfolio/common/types/performance-calculation-type.type'; |
||||
|
|
||||
import { Big } from 'big.js'; |
import { Big } from 'big.js'; |
||||
import { join } from 'node:path'; |
import { join } from 'node:path'; |
||||
|
|
||||
jest.mock('@ghostfolio/api/app/portfolio/current-rate.service', () => { |
jest.mock('@ghostfolio/api/app/portfolio/current-rate.service', () => { |
||||
return { |
return { |
||||
CurrentRateService: jest.fn().mockImplementation(() => { |
CurrentRateService: jest.fn().mockImplementation(() => { |
||||
return CurrentRateServiceMock; |
return CurrentRateServiceMock; |
||||
}) |
}) |
||||
}; |
}; |
||||
}); |
}); |
||||
|
|
||||
jest.mock( |
jest.mock( |
||||
'@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service', |
'@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service', |
||||
() => { |
() => { |
||||
return { |
return { |
||||
PortfolioSnapshotService: jest.fn().mockImplementation(() => { |
PortfolioSnapshotService: jest.fn().mockImplementation(() => { |
||||
return PortfolioSnapshotServiceMock; |
return PortfolioSnapshotServiceMock; |
||||
}) |
}) |
||||
}; |
}; |
||||
} |
} |
||||
); |
); |
||||
|
|
||||
jest.mock('@ghostfolio/api/app/redis-cache/redis-cache.service', () => { |
jest.mock('@ghostfolio/api/app/redis-cache/redis-cache.service', () => { |
||||
return { |
return { |
||||
RedisCacheService: jest.fn().mockImplementation(() => { |
RedisCacheService: jest.fn().mockImplementation(() => { |
||||
return RedisCacheServiceMock; |
return RedisCacheServiceMock; |
||||
}) |
}) |
||||
}; |
}; |
||||
}); |
}); |
||||
|
|
||||
describe('PortfolioCalculator', () => { |
describe('PortfolioCalculator', () => { |
||||
let exportResponse: ExportResponse; |
let exportResponse: ExportResponse; |
||||
|
|
||||
let configurationService: ConfigurationService; |
let configurationService: ConfigurationService; |
||||
let currentRateService: CurrentRateService; |
let currentRateService: CurrentRateService; |
||||
let exchangeRateDataService: ExchangeRateDataService; |
let exchangeRateDataService: ExchangeRateDataService; |
||||
let portfolioCalculatorFactory: PortfolioCalculatorFactory; |
let portfolioCalculatorFactory: PortfolioCalculatorFactory; |
||||
let portfolioSnapshotService: PortfolioSnapshotService; |
let portfolioSnapshotService: PortfolioSnapshotService; |
||||
let redisCacheService: RedisCacheService; |
let redisCacheService: RedisCacheService; |
||||
|
|
||||
beforeAll(() => { |
beforeAll(() => { |
||||
exportResponse = loadExportFile( |
exportResponse = loadExportFile( |
||||
join( |
join( |
||||
__dirname, |
__dirname, |
||||
'../../../../../../../test/import/ok/novn-buy-and-sell.json' |
'../../../../../../../test/import/ok/novn-buy-and-sell.json' |
||||
) |
) |
||||
); |
); |
||||
}); |
}); |
||||
|
|
||||
beforeEach(() => { |
beforeEach(() => { |
||||
configurationService = new ConfigurationService(); |
configurationService = new ConfigurationService(); |
||||
|
|
||||
currentRateService = new CurrentRateService(null, null, null, null); |
currentRateService = new CurrentRateService(null, null, null, null); |
||||
|
|
||||
exchangeRateDataService = new ExchangeRateDataService( |
exchangeRateDataService = new ExchangeRateDataService( |
||||
null, |
null, |
||||
null, |
null, |
||||
null, |
null, |
||||
null |
null |
||||
); |
); |
||||
|
|
||||
portfolioSnapshotService = new PortfolioSnapshotService(null, null); |
portfolioSnapshotService = new PortfolioSnapshotService(null, null); |
||||
|
|
||||
redisCacheService = new RedisCacheService(null, null); |
redisCacheService = new RedisCacheService(null, null); |
||||
|
|
||||
portfolioCalculatorFactory = new PortfolioCalculatorFactory( |
portfolioCalculatorFactory = new PortfolioCalculatorFactory( |
||||
configurationService, |
configurationService, |
||||
currentRateService, |
currentRateService, |
||||
exchangeRateDataService, |
exchangeRateDataService, |
||||
portfolioSnapshotService, |
portfolioSnapshotService, |
||||
redisCacheService |
redisCacheService |
||||
); |
); |
||||
}); |
}); |
||||
|
|
||||
describe('get current positions', () => { |
describe('get current positions', () => { |
||||
it.only('with NOVN.SW buy and sell', async () => { |
it.only('with NOVN.SW buy and sell', async () => { |
||||
jest.useFakeTimers().setSystemTime(parseDate('2022-04-11').getTime()); |
jest.useFakeTimers().setSystemTime(parseDate('2022-04-11').getTime()); |
||||
|
|
||||
const activities: Activity[] = exportResponse.activities.map( |
const activities: Activity[] = exportResponse.activities.map( |
||||
(activity) => ({ |
(activity) => ({ |
||||
...activityDummyData, |
...activityDummyData, |
||||
...activity, |
...activity, |
||||
date: parseDate(activity.date), |
assetProfile: { |
||||
feeInAssetProfileCurrency: activity.fee, |
...assetProfileDummyData, |
||||
feeInBaseCurrency: activity.fee, |
currency: activity.currency, |
||||
SymbolProfile: { |
dataSource: activity.dataSource, |
||||
...symbolProfileDummyData, |
name: 'Novartis AG', |
||||
currency: activity.currency, |
symbol: activity.symbol |
||||
dataSource: activity.dataSource, |
}, |
||||
name: 'Novartis AG', |
date: parseDate(activity.date), |
||||
symbol: activity.symbol |
feeInAssetProfileCurrency: activity.fee, |
||||
}, |
feeInBaseCurrency: activity.fee, |
||||
unitPriceInAssetProfileCurrency: activity.unitPrice |
unitPriceInAssetProfileCurrency: activity.unitPrice |
||||
}) |
}) |
||||
); |
); |
||||
|
|
||||
const portfolioCalculator = portfolioCalculatorFactory.createCalculator({ |
const portfolioCalculator = portfolioCalculatorFactory.createCalculator({ |
||||
activities, |
activities, |
||||
calculationType: PerformanceCalculationType.ROAI, |
calculationType: PerformanceCalculationType.ROAI, |
||||
currency: exportResponse.user.settings.currency, |
currency: exportResponse.user.settings.currency, |
||||
userId: userDummyData.id |
userId: userDummyData.id |
||||
}); |
}); |
||||
|
|
||||
const portfolioSnapshot = await portfolioCalculator.computeSnapshot(); |
const portfolioSnapshot = await portfolioCalculator.computeSnapshot(); |
||||
|
|
||||
const investments = portfolioCalculator.getInvestments(); |
const investments = portfolioCalculator.getInvestments(); |
||||
|
|
||||
const investmentsByMonth = portfolioCalculator.getInvestmentsByGroup({ |
const investmentsByMonth = portfolioCalculator.getInvestmentsByGroup({ |
||||
data: portfolioSnapshot.historicalData, |
data: portfolioSnapshot.historicalData, |
||||
groupBy: 'month' |
groupBy: 'month' |
||||
}); |
}); |
||||
|
|
||||
const investmentsByYear = portfolioCalculator.getInvestmentsByGroup({ |
const investmentsByYear = portfolioCalculator.getInvestmentsByGroup({ |
||||
data: portfolioSnapshot.historicalData, |
data: portfolioSnapshot.historicalData, |
||||
groupBy: 'year' |
groupBy: 'year' |
||||
}); |
}); |
||||
|
|
||||
expect(portfolioSnapshot.historicalData[0]).toEqual({ |
expect(portfolioSnapshot.historicalData[0]).toEqual({ |
||||
date: '2022-03-06', |
date: '2022-03-06', |
||||
investmentValueWithCurrencyEffect: 0, |
investmentValueWithCurrencyEffect: 0, |
||||
netPerformance: 0, |
netPerformance: 0, |
||||
netPerformanceInPercentage: 0, |
netPerformanceInPercentage: 0, |
||||
netPerformanceInPercentageWithCurrencyEffect: 0, |
netPerformanceInPercentageWithCurrencyEffect: 0, |
||||
netPerformanceWithCurrencyEffect: 0, |
netPerformanceWithCurrencyEffect: 0, |
||||
netWorth: 0, |
netWorth: 0, |
||||
totalAccountBalance: 0, |
totalAccountBalance: 0, |
||||
totalInvestment: 0, |
totalInvestment: 0, |
||||
totalInvestmentValueWithCurrencyEffect: 0, |
totalInvestmentValueWithCurrencyEffect: 0, |
||||
value: 0, |
value: 0, |
||||
valueWithCurrencyEffect: 0 |
valueWithCurrencyEffect: 0 |
||||
}); |
}); |
||||
|
|
||||
/** |
/** |
||||
* Closing price on 2022-03-07 is unknown, |
* Closing price on 2022-03-07 is unknown, |
||||
* hence it uses the last unit price (2022-04-11): 87.8 |
* hence it uses the last unit price (2022-04-11): 87.8 |
||||
*/ |
*/ |
||||
expect(portfolioSnapshot.historicalData[1]).toEqual({ |
expect(portfolioSnapshot.historicalData[1]).toEqual({ |
||||
date: '2022-03-07', |
date: '2022-03-07', |
||||
investmentValueWithCurrencyEffect: 151.6, |
investmentValueWithCurrencyEffect: 151.6, |
||||
netPerformance: 24, // 2 * (87.8 - 75.8) = 24
|
netPerformance: 24, // 2 * (87.8 - 75.8) = 24
|
||||
netPerformanceInPercentage: 0.158311345646438, // 24 ÷ 151.6 = 0.158311345646438
|
netPerformanceInPercentage: 0.158311345646438, // 24 ÷ 151.6 = 0.158311345646438
|
||||
netPerformanceInPercentageWithCurrencyEffect: 0.158311345646438, // 24 ÷ 151.6 = 0.158311345646438
|
netPerformanceInPercentageWithCurrencyEffect: 0.158311345646438, // 24 ÷ 151.6 = 0.158311345646438
|
||||
netPerformanceWithCurrencyEffect: 24, |
netPerformanceWithCurrencyEffect: 24, |
||||
netWorth: 175.6, // 2 * 87.8 = 175.6
|
netWorth: 175.6, // 2 * 87.8 = 175.6
|
||||
totalAccountBalance: 0, |
totalAccountBalance: 0, |
||||
totalInvestment: 151.6, |
totalInvestment: 151.6, |
||||
totalInvestmentValueWithCurrencyEffect: 151.6, |
totalInvestmentValueWithCurrencyEffect: 151.6, |
||||
value: 175.6, // 2 * 87.8 = 175.6
|
value: 175.6, // 2 * 87.8 = 175.6
|
||||
valueWithCurrencyEffect: 175.6 |
valueWithCurrencyEffect: 175.6 |
||||
}); |
}); |
||||
|
|
||||
expect( |
expect( |
||||
portfolioSnapshot.historicalData[ |
portfolioSnapshot.historicalData[ |
||||
portfolioSnapshot.historicalData.length - 1 |
portfolioSnapshot.historicalData.length - 1 |
||||
] |
] |
||||
).toEqual({ |
).toEqual({ |
||||
date: '2022-04-11', |
date: '2022-04-11', |
||||
investmentValueWithCurrencyEffect: 0, |
investmentValueWithCurrencyEffect: 0, |
||||
netPerformance: 19.86, |
netPerformance: 19.86, |
||||
netPerformanceInPercentage: 0.13100263852242744, |
netPerformanceInPercentage: 0.13100263852242744, |
||||
netPerformanceInPercentageWithCurrencyEffect: 0.13100263852242744, |
netPerformanceInPercentageWithCurrencyEffect: 0.13100263852242744, |
||||
netPerformanceWithCurrencyEffect: 19.86, |
netPerformanceWithCurrencyEffect: 19.86, |
||||
netWorth: 0, |
netWorth: 0, |
||||
totalAccountBalance: 0, |
totalAccountBalance: 0, |
||||
totalInvestment: 0, |
totalInvestment: 0, |
||||
totalInvestmentValueWithCurrencyEffect: 0, |
totalInvestmentValueWithCurrencyEffect: 0, |
||||
value: 0, |
value: 0, |
||||
valueWithCurrencyEffect: 0 |
valueWithCurrencyEffect: 0 |
||||
}); |
}); |
||||
|
|
||||
expect(portfolioSnapshot).toMatchObject({ |
expect(portfolioSnapshot).toMatchObject({ |
||||
currentValueInBaseCurrency: new Big('0'), |
currentValueInBaseCurrency: new Big('0'), |
||||
errors: [], |
errors: [], |
||||
hasErrors: false, |
hasErrors: false, |
||||
positions: [ |
positions: [ |
||||
{ |
{ |
||||
activitiesCount: 2, |
activitiesCount: 2, |
||||
averagePrice: new Big('0'), |
averagePrice: new Big('0'), |
||||
currency: 'CHF', |
currency: 'CHF', |
||||
dataSource: 'YAHOO', |
dataSource: 'YAHOO', |
||||
dateOfFirstActivity: '2022-03-07', |
dateOfFirstActivity: '2022-03-07', |
||||
dividend: new Big('0'), |
dividend: new Big('0'), |
||||
dividendInBaseCurrency: new Big('0'), |
dividendInBaseCurrency: new Big('0'), |
||||
fee: new Big('0'), |
fee: new Big('0'), |
||||
feeInBaseCurrency: new Big('0'), |
feeInBaseCurrency: new Big('0'), |
||||
grossPerformance: new Big('19.86'), |
grossPerformance: new Big('19.86'), |
||||
grossPerformancePercentage: new Big('0.13100263852242744063'), |
grossPerformancePercentage: new Big('0.13100263852242744063'), |
||||
grossPerformancePercentageWithCurrencyEffect: new Big( |
grossPerformancePercentageWithCurrencyEffect: new Big( |
||||
'0.13100263852242744063' |
'0.13100263852242744063' |
||||
), |
), |
||||
grossPerformanceWithCurrencyEffect: new Big('19.86'), |
grossPerformanceWithCurrencyEffect: new Big('19.86'), |
||||
investment: new Big('0'), |
investment: new Big('0'), |
||||
investmentWithCurrencyEffect: new Big('0'), |
investmentWithCurrencyEffect: new Big('0'), |
||||
netPerformance: new Big('19.86'), |
netPerformance: new Big('19.86'), |
||||
netPerformancePercentage: new Big('0.13100263852242744063'), |
netPerformancePercentage: new Big('0.13100263852242744063'), |
||||
netPerformancePercentageWithCurrencyEffectMap: { |
netPerformancePercentageWithCurrencyEffectMap: { |
||||
max: new Big('0.13100263852242744063') |
max: new Big('0.13100263852242744063') |
||||
}, |
}, |
||||
netPerformanceWithCurrencyEffectMap: { |
netPerformanceWithCurrencyEffectMap: { |
||||
max: new Big('19.86') |
max: new Big('19.86') |
||||
}, |
}, |
||||
marketPrice: 87.8, |
marketPrice: 87.8, |
||||
marketPriceInBaseCurrency: 87.8, |
marketPriceInBaseCurrency: 87.8, |
||||
quantity: new Big('0'), |
quantity: new Big('0'), |
||||
symbol: 'NOVN.SW', |
symbol: 'NOVN.SW', |
||||
tags: [], |
tags: [], |
||||
timeWeightedInvestment: new Big('151.6'), |
timeWeightedInvestment: new Big('151.6'), |
||||
timeWeightedInvestmentWithCurrencyEffect: new Big('151.6'), |
timeWeightedInvestmentWithCurrencyEffect: new Big('151.6'), |
||||
valueInBaseCurrency: new Big('0') |
valueInBaseCurrency: new Big('0') |
||||
} |
} |
||||
], |
], |
||||
totalFeesWithCurrencyEffect: new Big('0'), |
totalFeesWithCurrencyEffect: new Big('0'), |
||||
totalInterestWithCurrencyEffect: new Big('0'), |
totalInterestWithCurrencyEffect: new Big('0'), |
||||
totalInvestment: new Big('0'), |
totalInvestment: new Big('0'), |
||||
totalInvestmentWithCurrencyEffect: new Big('0'), |
totalInvestmentWithCurrencyEffect: new Big('0'), |
||||
totalLiabilitiesWithCurrencyEffect: new Big('0') |
totalLiabilitiesWithCurrencyEffect: new Big('0') |
||||
}); |
}); |
||||
|
|
||||
expect(portfolioSnapshot.historicalData.at(-1)).toMatchObject( |
expect(portfolioSnapshot.historicalData.at(-1)).toMatchObject( |
||||
expect.objectContaining({ |
expect.objectContaining({ |
||||
netPerformance: 19.86, |
netPerformance: 19.86, |
||||
netPerformanceInPercentage: 0.13100263852242744063, |
netPerformanceInPercentage: 0.13100263852242744063, |
||||
netPerformanceInPercentageWithCurrencyEffect: 0.13100263852242744063, |
netPerformanceInPercentageWithCurrencyEffect: 0.13100263852242744063, |
||||
netPerformanceWithCurrencyEffect: 19.86, |
netPerformanceWithCurrencyEffect: 19.86, |
||||
totalInvestment: 0, |
totalInvestment: 0, |
||||
totalInvestmentValueWithCurrencyEffect: 0 |
totalInvestmentValueWithCurrencyEffect: 0 |
||||
}) |
}) |
||||
); |
); |
||||
|
|
||||
expect(investments).toEqual([ |
expect(investments).toEqual([ |
||||
{ date: '2022-03-07', investment: new Big('151.6') }, |
{ date: '2022-03-07', investment: new Big('151.6') }, |
||||
{ date: '2022-04-08', investment: new Big('0') } |
{ date: '2022-04-08', investment: new Big('0') } |
||||
]); |
]); |
||||
|
|
||||
expect(investmentsByMonth).toEqual([ |
expect(investmentsByMonth).toEqual([ |
||||
{ date: '2022-03-01', investment: 151.6 }, |
{ date: '2022-03-01', investment: 151.6 }, |
||||
{ date: '2022-04-01', investment: -151.6 } |
{ date: '2022-04-01', investment: -151.6 } |
||||
]); |
]); |
||||
|
|
||||
expect(investmentsByYear).toEqual([ |
expect(investmentsByYear).toEqual([ |
||||
{ date: '2022-01-01', investment: 0 } |
{ date: '2022-01-01', investment: 0 } |
||||
]); |
]); |
||||
}); |
}); |
||||
}); |
}); |
||||
}); |
}); |
||||
|
|||||
@ -1,13 +1,13 @@ |
|||||
import { Activity } from '@ghostfolio/common/interfaces'; |
import { Activity } from '@ghostfolio/common/interfaces'; |
||||
|
|
||||
export interface PortfolioOrder extends Pick<Activity, 'tags' | 'type'> { |
export interface PortfolioOrder extends Pick<Activity, 'tags' | 'type'> { |
||||
|
assetProfile: Pick< |
||||
|
Activity['assetProfile'], |
||||
|
'assetSubClass' | 'currency' | 'dataSource' | 'name' | 'symbol' | 'userId' |
||||
|
>; |
||||
date: string; |
date: string; |
||||
fee: Big; |
fee: Big; |
||||
feeInBaseCurrency: Big; |
feeInBaseCurrency: Big; |
||||
quantity: Big; |
quantity: Big; |
||||
SymbolProfile: Pick< |
|
||||
Activity['SymbolProfile'], |
|
||||
'assetSubClass' | 'currency' | 'dataSource' | 'name' | 'symbol' | 'userId' |
|
||||
>; |
|
||||
unitPrice: Big; |
unitPrice: Big; |
||||
} |
} |
||||
|
|||||
@ -0,0 +1,21 @@ |
|||||
|
import { ExecutionContext, Injectable, Logger } from '@nestjs/common'; |
||||
|
import { ThrottlerException, ThrottlerGuard } from '@nestjs/throttler'; |
||||
|
|
||||
|
@Injectable() |
||||
|
export class CustomThrottlerGuard extends ThrottlerGuard { |
||||
|
private readonly logger = new Logger(CustomThrottlerGuard.name); |
||||
|
|
||||
|
public async canActivate(context: ExecutionContext): Promise<boolean> { |
||||
|
try { |
||||
|
return await super.canActivate(context); |
||||
|
} catch (error) { |
||||
|
if (error instanceof ThrottlerException) { |
||||
|
throw error; |
||||
|
} |
||||
|
|
||||
|
this.logger.error(error); |
||||
|
|
||||
|
return true; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,22 @@ |
|||||
|
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; |
||||
|
|
||||
|
export function getRedisConnectionOptions( |
||||
|
configurationService: ConfigurationService |
||||
|
) { |
||||
|
return { |
||||
|
db: configurationService.get('REDIS_DB'), |
||||
|
host: configurationService.get('REDIS_HOST'), |
||||
|
password: configurationService.get('REDIS_PASSWORD'), |
||||
|
port: configurationService.get('REDIS_PORT') |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
export function getRedisConnectionUrl( |
||||
|
configurationService: ConfigurationService |
||||
|
): string { |
||||
|
const { db, host, password, port } = |
||||
|
getRedisConnectionOptions(configurationService); |
||||
|
const encodedPassword = encodeURIComponent(password); |
||||
|
|
||||
|
return `redis://${encodedPassword ? `:${encodedPassword}` : ''}@${host}:${port}/${db}`; |
||||
|
} |
||||
@ -1,13 +1,19 @@ |
|||||
import { SymbolModule } from '@ghostfolio/api/app/symbol/symbol.module'; |
import { SymbolModule } from '@ghostfolio/api/app/symbol/symbol.module'; |
||||
import { BenchmarkModule } from '@ghostfolio/api/services/benchmark/benchmark.module'; |
import { BenchmarkModule } from '@ghostfolio/api/services/benchmark/benchmark.module'; |
||||
import { ConfigurationModule } from '@ghostfolio/api/services/configuration/configuration.module'; |
import { ConfigurationModule } from '@ghostfolio/api/services/configuration/configuration.module'; |
||||
|
import { DataProviderModule } from '@ghostfolio/api/services/data-provider/data-provider.module'; |
||||
import { TwitterBotService } from '@ghostfolio/api/services/twitter-bot/twitter-bot.service'; |
import { TwitterBotService } from '@ghostfolio/api/services/twitter-bot/twitter-bot.service'; |
||||
|
|
||||
import { Module } from '@nestjs/common'; |
import { Module } from '@nestjs/common'; |
||||
|
|
||||
@Module({ |
@Module({ |
||||
exports: [TwitterBotService], |
exports: [TwitterBotService], |
||||
imports: [BenchmarkModule, ConfigurationModule, SymbolModule], |
imports: [ |
||||
|
BenchmarkModule, |
||||
|
ConfigurationModule, |
||||
|
DataProviderModule, |
||||
|
SymbolModule |
||||
|
], |
||||
providers: [TwitterBotService] |
providers: [TwitterBotService] |
||||
}) |
}) |
||||
export class TwitterBotModule {} |
export class TwitterBotModule {} |
||||
|
|||||
@ -1,6 +1,49 @@ |
|||||
const { composePlugins, withNx } = require('@nx/webpack'); |
const { NxAppWebpackPlugin } = require('@nx/webpack/app-plugin'); |
||||
|
const path = require('path'); |
||||
|
|
||||
module.exports = composePlugins(withNx(), (config, { options, context }) => { |
// These options were migrated by @nx/webpack:convert-to-inferred from
|
||||
// Customize webpack config here
|
// the project.json file and merged with the options in this file
|
||||
return config; |
const configValues = { |
||||
|
build: { |
||||
|
default: { |
||||
|
compiler: 'tsc', |
||||
|
deleteOutputPath: false, |
||||
|
main: './src/main.ts', |
||||
|
outputPath: 'dist/apps/api', |
||||
|
outputHashing: 'none', |
||||
|
sourceMap: true, |
||||
|
target: 'node', |
||||
|
tsConfig: './tsconfig.app.json' |
||||
|
}, |
||||
|
production: { |
||||
|
extractLicenses: true, |
||||
|
fileReplacements: [ |
||||
|
{ |
||||
|
replace: path.resolve(__dirname, './src/environments/environment.ts'), |
||||
|
with: path.resolve( |
||||
|
__dirname, |
||||
|
'./src/environments/environment.prod.ts' |
||||
|
) |
||||
|
} |
||||
|
], |
||||
|
generatePackageJson: true, |
||||
|
inspect: false, |
||||
|
optimization: true |
||||
|
} |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
// Determine the correct configValue to use based on the configuration
|
||||
|
const configuration = process.env.NX_TASK_TARGET_CONFIGURATION || 'default'; |
||||
|
|
||||
|
const buildOptions = { |
||||
|
...configValues.build.default, |
||||
|
...configValues.build[configuration] |
||||
|
}; |
||||
|
|
||||
|
/** |
||||
|
* @type {import('webpack').WebpackOptionsNormalized} |
||||
|
*/ |
||||
|
module.exports = async () => ({ |
||||
|
plugins: [new NxAppWebpackPlugin(buildOptions)] |
||||
}); |
}); |
||||
|
|||||
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue