|  | @ -68,7 +68,7 @@ import { | 
			
		
	
		
		
			
				
					|  |  |   subDays, |  |  |   subDays, | 
			
		
	
		
		
			
				
					|  |  |   subYears |  |  |   subYears | 
			
		
	
		
		
			
				
					|  |  | } from 'date-fns'; |  |  | } from 'date-fns'; | 
			
		
	
		
		
			
				
					
					|  |  | import { isEmpty, sortBy, uniqBy } from 'lodash'; |  |  | import { isEmpty, sortBy, uniq, uniqBy } from 'lodash'; | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					|  |  | 
 |  |  | 
 | 
			
		
	
		
		
			
				
					|  |  | import { |  |  | import { | 
			
		
	
		
		
			
				
					|  |  |   HistoricalDataContainer, |  |  |   HistoricalDataContainer, | 
			
		
	
	
		
		
			
				
					|  | @ -344,10 +344,11 @@ export class PortfolioService { | 
			
		
	
		
		
			
				
					|  |  |       startDate |  |  |       startDate | 
			
		
	
		
		
			
				
					|  |  |     ); |  |  |     ); | 
			
		
	
		
		
			
				
					|  |  | 
 |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |     const cashDetails = await this.accountService.getCashDetails( |  |  |     const cashDetails = await this.accountService.getCashDetails({ | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					|  |  |       userId, |  |  |       userId, | 
			
		
	
		
		
			
				
					
					|  |  |       userCurrency |  |  |       currency: userCurrency, | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |     ); |  |  |       filters: aFilters | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
			
				
					|  |  |  |  |  |     }); | 
			
		
	
		
		
			
				
					|  |  | 
 |  |  | 
 | 
			
		
	
		
		
			
				
					|  |  |     const holdings: PortfolioDetails['holdings'] = {}; |  |  |     const holdings: PortfolioDetails['holdings'] = {}; | 
			
		
	
		
		
			
				
					|  |  |     const totalInvestment = currentPositions.totalInvestment.plus( |  |  |     const totalInvestment = currentPositions.totalInvestment.plus( | 
			
		
	
	
		
		
			
				
					|  | @ -440,6 +441,7 @@ export class PortfolioService { | 
			
		
	
		
		
			
				
					|  |  |       }; |  |  |       }; | 
			
		
	
		
		
			
				
					|  |  |     } |  |  |     } | 
			
		
	
		
		
			
				
					|  |  | 
 |  |  | 
 | 
			
		
	
		
		
			
				
					|  |  |  |  |  |     if (aFilters?.length === 0) { | 
			
		
	
		
		
			
				
					|  |  |       const cashPositions = await this.getCashPositions({ |  |  |       const cashPositions = await this.getCashPositions({ | 
			
		
	
		
		
			
				
					|  |  |         cashDetails, |  |  |         cashDetails, | 
			
		
	
		
		
			
				
					|  |  |         emergencyFund, |  |  |         emergencyFund, | 
			
		
	
	
		
		
			
				
					|  | @ -448,18 +450,17 @@ export class PortfolioService { | 
			
		
	
		
		
			
				
					|  |  |         value: totalValue |  |  |         value: totalValue | 
			
		
	
		
		
			
				
					|  |  |       }); |  |  |       }); | 
			
		
	
		
		
			
				
					|  |  | 
 |  |  | 
 | 
			
		
	
		
		
			
				
					|  |  |     if (aFilters?.length === 0) { |  |  |  | 
			
		
	
		
		
			
				
					|  |  |       for (const symbol of Object.keys(cashPositions)) { |  |  |       for (const symbol of Object.keys(cashPositions)) { | 
			
		
	
		
		
			
				
					|  |  |         holdings[symbol] = cashPositions[symbol]; |  |  |         holdings[symbol] = cashPositions[symbol]; | 
			
		
	
		
		
			
				
					|  |  |       } |  |  |       } | 
			
		
	
		
		
			
				
					|  |  |     } |  |  |     } | 
			
		
	
		
		
			
				
					|  |  | 
 |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |     const accounts = await this.getValueOfAccounts( |  |  |     const accounts = await this.getValueOfAccounts({ | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					|  |  |       orders, |  |  |       orders, | 
			
		
	
		
		
			
				
					|  |  |  |  |  |       userId, | 
			
		
	
		
		
			
				
					|  |  |       portfolioItemsNow, |  |  |       portfolioItemsNow, | 
			
		
	
		
		
			
				
					
					|  |  |       userCurrency, |  |  |       filters: aFilters | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |       userId |  |  |     }); | 
			
				
				
			
		
	
		
		
			
				
					|  |  |     ); |  |  |  | 
			
		
	
		
		
	
		
		
	
		
		
			
				
					|  |  | 
 |  |  | 
 | 
			
		
	
		
		
			
				
					|  |  |     return { accounts, holdings, hasErrors: currentPositions.hasErrors }; |  |  |     return { accounts, holdings, hasErrors: currentPositions.hasErrors }; | 
			
		
	
		
		
			
				
					|  |  |   } |  |  |   } | 
			
		
	
	
		
		
			
				
					|  | @ -890,12 +891,11 @@ export class PortfolioService { | 
			
		
	
		
		
			
				
					|  |  |     for (const position of currentPositions.positions) { |  |  |     for (const position of currentPositions.positions) { | 
			
		
	
		
		
			
				
					|  |  |       portfolioItemsNow[position.symbol] = position; |  |  |       portfolioItemsNow[position.symbol] = position; | 
			
		
	
		
		
			
				
					|  |  |     } |  |  |     } | 
			
		
	
		
		
			
				
					
					|  |  |     const accounts = await this.getValueOfAccounts( |  |  |     const accounts = await this.getValueOfAccounts({ | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					|  |  |       orders, |  |  |       orders, | 
			
		
	
		
		
			
				
					|  |  |       portfolioItemsNow, |  |  |       portfolioItemsNow, | 
			
		
	
		
		
			
				
					|  |  |       currency, |  |  |  | 
			
		
	
		
		
			
				
					|  |  |       userId |  |  |       userId | 
			
		
	
		
		
			
				
					
					|  |  |     ); |  |  |     }); | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					|  |  |     return { |  |  |     return { | 
			
		
	
		
		
			
				
					|  |  |       rules: { |  |  |       rules: { | 
			
		
	
		
		
			
				
					|  |  |         accountClusterRisk: await this.rulesService.evaluate( |  |  |         accountClusterRisk: await this.rulesService.evaluate( | 
			
		
	
	
		
		
			
				
					|  | @ -957,10 +957,10 @@ export class PortfolioService { | 
			
		
	
		
		
			
				
					|  |  | 
 |  |  | 
 | 
			
		
	
		
		
			
				
					|  |  |     const performanceInformation = await this.getPerformance(aImpersonationId); |  |  |     const performanceInformation = await this.getPerformance(aImpersonationId); | 
			
		
	
		
		
			
				
					|  |  | 
 |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |     const { balanceInBaseCurrency } = await this.accountService.getCashDetails( |  |  |     const { balanceInBaseCurrency } = await this.accountService.getCashDetails({ | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					|  |  |       userId, |  |  |       userId, | 
			
		
	
		
		
			
				
					
					|  |  |       userCurrency |  |  |       currency: userCurrency | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |     ); |  |  |     }); | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
			
				
					|  |  |     const orders = await this.orderService.getOrders({ |  |  |     const orders = await this.orderService.getOrders({ | 
			
		
	
		
		
			
				
					|  |  |       userCurrency, |  |  |       userCurrency, | 
			
		
	
		
		
			
				
					|  |  |       userId |  |  |       userId | 
			
		
	
	
		
		
			
				
					|  | @ -1253,21 +1253,40 @@ export class PortfolioService { | 
			
		
	
		
		
			
				
					|  |  |     portfolioCalculator.computeTransactionPoints(); |  |  |     portfolioCalculator.computeTransactionPoints(); | 
			
		
	
		
		
			
				
					|  |  | 
 |  |  | 
 | 
			
		
	
		
		
			
				
					|  |  |     return { |  |  |     return { | 
			
		
	
		
		
			
				
					|  |  |       transactionPoints: portfolioCalculator.getTransactionPoints(), |  |  |  | 
			
		
	
		
		
			
				
					|  |  |       orders, |  |  |       orders, | 
			
		
	
		
		
			
				
					
					|  |  |       portfolioOrders |  |  |       portfolioOrders, | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					|  |  |  |  |  |       transactionPoints: portfolioCalculator.getTransactionPoints() | 
			
		
	
		
		
			
				
					|  |  |     }; |  |  |     }; | 
			
		
	
		
		
			
				
					|  |  |   } |  |  |   } | 
			
		
	
		
		
			
				
					|  |  | 
 |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |   private async getValueOfAccounts( |  |  |   private async getValueOfAccounts({ | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |     orders: OrderWithAccount[], |  |  |     filters = [], | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |     portfolioItemsNow: { [p: string]: TimelinePosition }, |  |  |     orders, | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |     userCurrency: string, |  |  |     portfolioItemsNow, | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |     userId: string |  |  |     userId | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |   ) { |  |  |   }: { | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					|  |  |  |  |  |     filters?: Filter[]; | 
			
		
	
		
		
			
				
					|  |  |  |  |  |     orders: OrderWithAccount[]; | 
			
		
	
		
		
			
				
					|  |  |  |  |  |     portfolioItemsNow: { [p: string]: TimelinePosition }; | 
			
		
	
		
		
			
				
					|  |  |  |  |  |     userId: string; | 
			
		
	
		
		
			
				
					|  |  |  |  |  |   }) { | 
			
		
	
		
		
			
				
					|  |  |     const accounts: PortfolioDetails['accounts'] = {}; |  |  |     const accounts: PortfolioDetails['accounts'] = {}; | 
			
		
	
		
		
			
				
					|  |  | 
 |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |     const currentAccounts = await this.accountService.getAccounts(userId); |  |  |     let currentAccounts = []; | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					|  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					|  |  |  |  |  |     if (filters.length === 0) { | 
			
		
	
		
		
			
				
					|  |  |  |  |  |       currentAccounts = await this.accountService.getAccounts(userId); | 
			
		
	
		
		
			
				
					|  |  |  |  |  |     } else { | 
			
		
	
		
		
			
				
					|  |  |  |  |  |       const accountIds = uniq( | 
			
		
	
		
		
			
				
					|  |  |  |  |  |         orders.map(({ accountId }) => { | 
			
		
	
		
		
			
				
					|  |  |  |  |  |           return accountId; | 
			
		
	
		
		
			
				
					|  |  |  |  |  |         }) | 
			
		
	
		
		
			
				
					|  |  |  |  |  |       ); | 
			
		
	
		
		
			
				
					|  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					|  |  |  |  |  |       currentAccounts = await this.accountService.accounts({ | 
			
		
	
		
		
			
				
					|  |  |  |  |  |         where: { id: { in: accountIds } } | 
			
		
	
		
		
			
				
					|  |  |  |  |  |       }); | 
			
		
	
		
		
			
				
					|  |  |  |  |  |     } | 
			
		
	
		
		
			
				
					|  |  | 
 |  |  | 
 | 
			
		
	
		
		
			
				
					|  |  |     for (const account of currentAccounts) { |  |  |     for (const account of currentAccounts) { | 
			
		
	
		
		
			
				
					|  |  |       const ordersByAccount = orders.filter(({ accountId }) => { |  |  |       const ordersByAccount = orders.filter(({ accountId }) => { | 
			
		
	
	
		
		
			
				
					|  | 
 |