thnx @tomdx that’s a great resource.
But from what I understand it’s primary meant to calculate the prjected future rewards.
What I try is to simple calculate ROI based on historic Data from previous Epochs.
I think there is one mistake in your script. Pledge is also awarded with rewards. So you dont need to substract it from the active stake. Besides of that i think it looks good.
If you want a historical ROI to date rather than a projection you could simply query Cardano DB sync for all the blocks (UTXO) that produced rewards from your pool and sum them?
Something like this maybe:
select
sum(reward.amount) as total_lovelace
from reward
inner join pool_hash on reward.pool_id = <your pool hash id here>
where reward.epoch_no > 239 -- 240 was first epoch of 2021