| 
						
						
						
					 | 
					@ -1,7 +1,7 @@ | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					import { getYesterday } from '@ghostfolio/helper'; | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					import { Injectable } from '@nestjs/common'; | 
					 | 
					 | 
					import { Injectable } from '@nestjs/common'; | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					import { Currency } from '@prisma/client'; | 
					 | 
					 | 
					import { Currency } from '@prisma/client'; | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					import { format } from 'date-fns'; | 
					 | 
					 | 
					import { format } from 'date-fns'; | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					import { getYesterday } from 'libs/helper/src'; | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					
 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					import { DataProviderService } from './data-provider.service'; | 
					 | 
					 | 
					import { DataProviderService } from './data-provider.service'; | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					
 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
	
		
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
					@ -15,6 +15,8 @@ export class ExchangeRateDataService { | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					  } | 
					 | 
					 | 
					  } | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					
 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					  public async initialize() { | 
					 | 
					 | 
					  public async initialize() { | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					    this.pairs = []; | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    this.addPairs(Currency.CHF, Currency.EUR); | 
					 | 
					 | 
					    this.addPairs(Currency.CHF, Currency.EUR); | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    this.addPairs(Currency.CHF, Currency.GBP); | 
					 | 
					 | 
					    this.addPairs(Currency.CHF, Currency.GBP); | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    this.addPairs(Currency.CHF, Currency.USD); | 
					 | 
					 | 
					    this.addPairs(Currency.CHF, Currency.USD); | 
				
			
			
		
	
	
		
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
					@ -25,11 +27,6 @@ export class ExchangeRateDataService { | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    await this.loadCurrencies(); | 
					 | 
					 | 
					    await this.loadCurrencies(); | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					  } | 
					 | 
					 | 
					  } | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					
 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					  private addPairs(aCurrency1: Currency, aCurrency2: Currency) { | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    this.pairs.push(`${aCurrency1}${aCurrency2}`); | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    this.pairs.push(`${aCurrency2}${aCurrency1}`); | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					  } | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					  public async loadCurrencies() { | 
					 | 
					 | 
					  public async loadCurrencies() { | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    const result = await this.dataProviderService.getHistorical( | 
					 | 
					 | 
					    const result = await this.dataProviderService.getHistorical( | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					      this.pairs, | 
					 | 
					 | 
					      this.pairs, | 
				
			
			
		
	
	
		
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
					@ -38,19 +35,34 @@ export class ExchangeRateDataService { | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					      getYesterday() | 
					 | 
					 | 
					      getYesterday() | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    ); | 
					 | 
					 | 
					    ); | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					
 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					    this.pairs.forEach((pair) => { | 
					 | 
					 | 
					    const resultExtended = result; | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					      this.currencies[pair] = | 
					 | 
					 | 
					
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					        result[pair]?.[format(getYesterday(), 'yyyy-MM-dd')]?.marketPrice || 1; | 
					 | 
					 | 
					    Object.keys(result).forEach((pair) => { | 
				
			
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					      const [currency1, currency2] = pair.match(/.{1,3}/g); | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					      const [date] = Object.keys(result[pair]); | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					      // Calculate the opposite direction
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					      resultExtended[`${currency2}${currency1}`] = { | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					        [date]: { | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					          marketPrice: 1 / result[pair][date].marketPrice | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					        } | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					      }; | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					    }); | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					
 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					      if (this.currencies[pair] === 1) { | 
					 | 
					 | 
					    this.pairs.forEach((pair) => { | 
				
			
			
				
				
			
		
	
		
		
			
				
					 | 
					 | 
					        // Calculate the other direction
 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
	
		
		
			
				
					 | 
					 | 
					      const [currency1, currency2] = pair.match(/.{1,3}/g); | 
					 | 
					 | 
					      const [currency1, currency2] = pair.match(/.{1,3}/g); | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					      const date = format(getYesterday(), 'yyyy-MM-dd'); | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					      this.currencies[pair] = resultExtended[pair]?.[date]?.marketPrice; | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					
 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					      if (!this.currencies[pair]) { | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					        // Not found, calculate indirectly via USD
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					        this.currencies[pair] = | 
					 | 
					 | 
					        this.currencies[pair] = | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					          1 / | 
					 | 
					 | 
					          resultExtended[`${currency1}${Currency.USD}`][date].marketPrice * | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					          result[`${currency2}${currency1}`]?.[ | 
					 | 
					 | 
					          resultExtended[`${Currency.USD}${currency2}`][date].marketPrice; | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					            format(getYesterday(), 'yyyy-MM-dd') | 
					 | 
					 | 
					
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					          ]?.marketPrice; | 
					 | 
					 | 
					        // Calculate the opposite direction
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					        this.currencies[`${currency2}${currency1}`] = 1 / this.currencies[pair]; | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					      } | 
					 | 
					 | 
					      } | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    }); | 
					 | 
					 | 
					    }); | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					  } | 
					 | 
					 | 
					  } | 
				
			
			
		
	
	
		
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
					@ -60,6 +72,11 @@ export class ExchangeRateDataService { | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    aFromCurrency: Currency, | 
					 | 
					 | 
					    aFromCurrency: Currency, | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    aToCurrency: Currency | 
					 | 
					 | 
					    aToCurrency: Currency | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					  ) { | 
					 | 
					 | 
					  ) { | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					    if (isNaN(this.currencies[`${Currency.USD}${Currency.CHF}`])) { | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					      // Reinitialize if data is not loaded correctly
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					      this.initialize(); | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					    } | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    let factor = 1; | 
					 | 
					 | 
					    let factor = 1; | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					
 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    if (aFromCurrency !== aToCurrency) { | 
					 | 
					 | 
					    if (aFromCurrency !== aToCurrency) { | 
				
			
			
		
	
	
		
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
					@ -68,4 +85,9 @@ export class ExchangeRateDataService { | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					
 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    return factor * aValue; | 
					 | 
					 | 
					    return factor * aValue; | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					  } | 
					 | 
					 | 
					  } | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					  private addPairs(aCurrency1: Currency, aCurrency2: Currency) { | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					    this.pairs.push(`${aCurrency1}${aCurrency2}`); | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					    this.pairs.push(`${aCurrency2}${aCurrency1}`); | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					  } | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					} | 
					 | 
					 | 
					} | 
				
			
			
		
	
	
		
		
			
				
					| 
						
						
						
					 | 
					
  |