@@ -151,13 +151,13 @@ export class CustodyService {
151151 const custodyUserIds = account . users . filter ( ( u ) => u . role === UserRole . CUSTODY ) . map ( ( u ) => u . id ) ;
152152 const custodyOrders = await this . custodyOrderRepo . find ( {
153153 where : { user : { id : In ( custodyUserIds ) } , status : CustodyOrderStatus . COMPLETED } ,
154- order : { id : 'ASC' } ,
154+ order : { updated : 'ASC' } ,
155155 } ) ;
156156
157157 if ( ! custodyOrders . length ) return { totalValue : [ ] } ;
158158
159159 const orderMap = custodyOrders . reduce ( ( map , order ) => {
160- const key = Util . isoDate ( order . created ) ;
160+ const key = Util . isoDate ( order . updated ) ;
161161 const dayMap = map . get ( key ) ?? new Map < number , CustodyOrderSingle [ ] > ( ) ;
162162
163163 [
@@ -178,33 +178,38 @@ export class CustodyService {
178178 const assets = Array . from ( new Map ( allAssets . map ( ( a ) => [ a . id , a ] ) ) . values ( ) ) ;
179179
180180 // get all prices (by date)
181- const startDate = new Date ( custodyOrders [ 0 ] . created ) ;
181+ const startDate = new Date ( custodyOrders [ 0 ] . updated ) ;
182182 startDate . setHours ( 0 , 0 , 0 , 0 ) ;
183183 const prices = await this . assetPricesService . getAssetPrices ( assets , startDate ) ;
184184
185185 const priceMap = Util . groupByAccessor ( prices , ( p ) => Util . isoDate ( p . created ) ) ;
186186
187- // process by day
187+ // process by day: apply order volumes before calculating value
188188 const assetBalancesMap = new Map < number , number > ( ) ;
189189 const totalValue : CustodyHistoryEntryDto [ ] = [ ] ;
190+ const sortedOrderDays = [ ...orderMap . keys ( ) ] . sort ( ) ;
191+ let orderDayIndex = 0 ;
192+
193+ for ( const [ day , dayPrices ] of priceMap . entries ( ) ) {
194+ // apply all order balance changes up to and including this day
195+ while ( orderDayIndex < sortedOrderDays . length && sortedOrderDays [ orderDayIndex ] <= day ) {
196+ const dayOrders = orderMap . get ( sortedOrderDays [ orderDayIndex ] ) ;
197+ if ( dayOrders ) {
198+ for ( const [ assetId , orders ] of dayOrders . entries ( ) ) {
199+ const currentBalance = assetBalancesMap . get ( assetId ) ?? 0 ;
200+ assetBalancesMap . set ( assetId , currentBalance + Util . sum ( orders . map ( ( o ) => o . amount ) ) ) ;
201+ }
202+ }
203+ orderDayIndex ++ ;
204+ }
190205
191- for ( const [ day , prices ] of priceMap . entries ( ) ) {
192- const dailyValue = prices . reduce (
206+ // calculate daily portfolio value from current balances and available prices
207+ const dailyValue = dayPrices . reduce (
193208 ( value , price ) => {
194- // update asset balance
195- const currentBalance = assetBalancesMap . get ( price . asset . id ) ?? 0 ;
196-
197- const ordersToday = orderMap . get ( day ) ?. get ( price . asset . id ) ?? [ ] ;
198- const dayVolume = Util . sum ( ordersToday . map ( ( o ) => o . amount ) ) ;
199-
200- const newBalance = currentBalance + dayVolume ;
201- assetBalancesMap . set ( price . asset . id , newBalance ) ;
202-
203- // update value
204- value . chf += newBalance * price . priceChf ;
205- value . eur += newBalance * price . priceEur ;
206- value . usd += newBalance * price . priceUsd ;
207-
209+ const balance = assetBalancesMap . get ( price . asset . id ) ?? 0 ;
210+ value . chf += balance * price . priceChf ;
211+ value . eur += balance * price . priceEur ;
212+ value . usd += balance * price . priceUsd ;
208213 return value ;
209214 } ,
210215 { chf : 0 , eur : 0 , usd : 0 } ,
0 commit comments