Database

[{"to":"A","from":"0","ts":1},{"to":"A","from":"0","ts":1},{"to":"B","from":"0","ts":1},{"to":"B","from":"0","ts":2},{"to":"C","from":"0","ts":4},{"to":"A","from":"B","ts":4}]

Query

db.collection.aggregate([{$project:{ts:"$ts",data:[{owner:"$to",change:1},{owner:"$from",change:-1}]}},{$unwind:"$data"},{$group:{_id:{ts:"$ts",owner:"$data.owner"},holdings:{$sum:"$data.change"}}},{$setWindowFields:{partitionBy:"$_id.owner",sortBy:{"_id.ts":1},output:{cumulativeHoldings:{$sum:"$holdings",window:{documents:["unbounded","current"]}}}}},{$group:{_id:"$_id.ts",holdings:{$push:{owner:"$_id.owner",holdings:"$cumulativeHoldings"}}}}])

Result