here's the updated files as requested
=SCAN(0,date,
LAMBDA(c∕d, period,
LET(
rate, XLOOKUP(period, date, Return),
change, XLOOKUP(period, date, InOut),
balance, (change + c∕d) * (1 + rate),
balance
)
)
)
openingBalance
= LET(
c∕d, VSTACK(0, DROP(closingBalance, -1)),
c∕d + InOut)
)
gainLoss
= closingBalance - openingBalance
= LET(
dataϑ, BYROW(periodData, LAMBDA(x, LAMBDA(x))),
return, SCAN( 0, dataϑ,
LAMBDA(c∕d, perioddataϑ,
LET(
rate, INDEX(perioddataϑ(), 1),
change, INDEX(perioddataϑ(), 2),
balance, (change + c∕d) * (1 + rate),
balance
)
)
),
return
)