| 
						
						
							
								
							
						
						
					 | 
					@ -51,6 +51,7 @@ import { | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					  HistoricalDataItem, | 
					 | 
					 | 
					  HistoricalDataItem, | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					  PortfolioPositionDetail | 
					 | 
					 | 
					  PortfolioPositionDetail | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					} from './interfaces/portfolio-position-detail.interface'; | 
					 | 
					 | 
					} from './interfaces/portfolio-position-detail.interface'; | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					import { OrderType } from '@ghostfolio/api/models/order-type'; | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					
 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					@Injectable() | 
					 | 
					 | 
					@Injectable() | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					export class PortfolioService { | 
					 | 
					 | 
					export class PortfolioService { | 
				
			
			
		
	
	
		
		
			
				
					| 
						
							
								
							
						
						
							
								
							
						
						
					 | 
					@ -91,14 +92,7 @@ export class PortfolioService { | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					      ).createFromData({ orders, portfolioItems, user }); | 
					 | 
					 | 
					      ).createFromData({ orders, portfolioItems, user }); | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    } else { | 
					 | 
					 | 
					    } else { | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					      // Get portfolio from database
 | 
					 | 
					 | 
					      // Get portfolio from database
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					      const orders = await this.orderService.orders({ | 
					 | 
					 | 
					      const orders = await this.getOrders(aUserId); | 
				
			
			
				
				
			
		
	
		
		
			
				
					 | 
					 | 
					        include: { | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					          Account: true, | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					          SymbolProfile: true | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					        }, | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					        orderBy: { date: 'asc' }, | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					        where: { userId: aUserId } | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					      }); | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
	
		
		
			
				
					 | 
					 | 
					
 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					      portfolio = new Portfolio( | 
					 | 
					 | 
					      portfolio = new Portfolio( | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					        this.accountService, | 
					 | 
					 | 
					        this.accountService, | 
				
			
			
		
	
	
		
		
			
				
					| 
						
							
								
							
						
						
							
								
							
						
						
					 | 
					@ -160,50 +154,27 @@ export class PortfolioService { | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    console.timeEnd('impersonation-service'); | 
					 | 
					 | 
					    console.timeEnd('impersonation-service'); | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					
 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    console.time('create-portfolio'); | 
					 | 
					 | 
					    console.time('create-portfolio'); | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					    const portfolio = await this.createPortfolio( | 
					 | 
					 | 
					    const userId = impersonationUserId || this.request.user.id; | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					      impersonationUserId || this.request.user.id | 
					 | 
					 | 
					    const orders = await this.getOrders(userId); | 
				
			
			
				
				
			
		
	
		
		
			
				
					 | 
					 | 
					    ); | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
	
		
		
	
		
		
			
				
					 | 
					 | 
					    console.timeEnd('create-portfolio'); | 
					 | 
					 | 
					    console.timeEnd('create-portfolio'); | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					
 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    const orders = portfolio.getOrders(); | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    if (orders.length <= 0) { | 
					 | 
					 | 
					    if (orders.length <= 0) { | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					      return []; | 
					 | 
					 | 
					      return []; | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    } | 
					 | 
					 | 
					    } | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					
 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    const dateRangeDate = this.convertDateRangeToDate( | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					      aDateRange, | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					      portfolio.getMinDate() | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    ); | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    const portfolioCalculator = new PortfolioCalculator( | 
					 | 
					 | 
					    const portfolioCalculator = new PortfolioCalculator( | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					      this.currentRateService, | 
					 | 
					 | 
					      this.currentRateService, | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					      this.request.user.Settings.currency | 
					 | 
					 | 
					      this.request.user.Settings.currency | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    ); | 
					 | 
					 | 
					    ); | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					
 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					    const portfolioOrders: PortfolioOrder[] = orders | 
					 | 
					 | 
					    const portfolioOrders: PortfolioOrder[] = orders.map((order) => ({ | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					      .filter((portfolioItem) => { | 
					 | 
					 | 
					      date: format(order.date, 'yyyy-MM-dd'), | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					        if (isAfter(parseISO(portfolioItem.getDate()), endOfToday())) { | 
					 | 
					 | 
					      quantity: new Big(order.quantity), | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					          // Filter out future dates
 | 
					 | 
					 | 
					      symbol: order.symbol, | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					          return false; | 
					 | 
					 | 
					      type: <OrderType>order.type, | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					        } | 
					 | 
					 | 
					      unitPrice: new Big(order.unitPrice), | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					
 | 
					 | 
					 | 
					      currency: order.currency | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					        if (dateRangeDate === undefined) { | 
					 | 
					 | 
					    })); | 
				
			
			
				
				
			
		
	
		
		
			
				
					 | 
					 | 
					          return true; | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					        } | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					        return ( | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					          isSameDay(parseISO(portfolioItem.getDate()), dateRangeDate) || | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					          isAfter(parseISO(portfolioItem.getDate()), dateRangeDate) | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					        ); | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					      }) | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					      .map((order) => ({ | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					        date: order.getDate().substr(0, 10), | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					        quantity: new Big(order.getQuantity()), | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					        symbol: order.getSymbol(), | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					        type: order.getType(), | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					        unitPrice: new Big(order.getUnitPrice()), | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					        currency: order.getCurrency() | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					      })); | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					 | 
					 | 
					    portfolioCalculator.computeTransactionPoints(portfolioOrders); | 
					 | 
					 | 
					    portfolioCalculator.computeTransactionPoints(portfolioOrders); | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    const transactionPoints = portfolioCalculator.getTransactionPoints(); | 
					 | 
					 | 
					    const transactionPoints = portfolioCalculator.getTransactionPoints(); | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    if (transactionPoints.length === 0) { | 
					 | 
					 | 
					    if (transactionPoints.length === 0) { | 
				
			
			
		
	
	
		
		
			
				
					| 
						
							
								
							
						
						
							
								
							
						
						
					 | 
					@ -505,4 +476,17 @@ export class PortfolioService { | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					        return undefined; | 
					 | 
					 | 
					        return undefined; | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					    } | 
					 | 
					 | 
					    } | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					  } | 
					 | 
					 | 
					  } | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					  private getOrders(aUserId: string) { | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					    return this.orderService.orders({ | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					      include: { | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					        // eslint-disable-next-line @typescript-eslint/naming-convention
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					        Account: true, | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					        // eslint-disable-next-line @typescript-eslint/naming-convention
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					        SymbolProfile: true | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					      }, | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					      orderBy: { date: 'asc' }, | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					      where: { userId: aUserId } | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					    }); | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					  } | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					} | 
					 | 
					 | 
					} | 
				
			
			
		
	
	
		
		
			
				
					| 
						
						
						
					 | 
					
  |