Sorry if I misunderstood this, but anyhow:
If the annual interest is p%, you will have 1 + p/100 times the money at the end of the year (1.04 in the case of 4%). Let's call that i := 1+ p/100.
So if you have been investing for, say, 74 days, you have i^(74/365) times the money at the end of the peridod.
Now, suppose you have invested m money for d days and have x money at the end of the period. The internal interest rate of the investment (as I believe it is called) comes from the formula
m*i^(d/365) = x,
hence
i^(d/365) =x/m
i=(d/365)-root(x/m),
Again, sorry if I completely misunderstood the problem. I have come across with this kinds of things when I studied a little bit actuarial maths. You might be interested too. Fundamentals of actuarial mathematics by David Promislow had a special excel approach to it.
EDIT: Especially, there's a way to do this better if you invest in the same fund every month or something like that. I don't remember it, and even if I would reproducing it here would be laborous and confusing.
This is almost it! I'm not sure how you get from "i^(d/365) =x/m" to "i=(d/365)-root(x/m)" though. Isn't it "i = (x/m)^(365/d)" ?
The problem is, this is fine for working out the return on one single month or period, but if I invest the same amount each month for 12 separate months, and I know the total amount invested, the total value of investments at the end of the period, and the amount I put in (and at what dates and prices), how can I calculate the rate of return on the entire 12 month period? Each month will have a different return, because, although the amount I put in each month is the same, (a) the value at the end will be different (due to different buy and sell prices), and (b) the amount of time elapsed "d" will be different each month. So for each of the 12 periods, I'll have a different "i" calculated at the end of it. I can't just take an average obviously, as this doesn't give me the right number. A weighted average, weighting by value or units or time, also doesn't seem to work.
I haven't got an analytical solution for this, and I think think would be preferable. At the moment, what I'm doing is, taking the £ amount invested each month m, along with a trial value for the interest rate i, and calculating "m*i^(d
t/365) = x
t" to get x for each month "t". Then I'm summing x
t for all months t to give me X. Then, I'm comparing this value of X to the actual value I have in my investment account at the end of the period X
actual. If X > X
actual, then "i" goes down a little bit, and vice versa, until X converges with X
actual. When they converge, I have the interest rate "i" that would fully explain how I got from putting in t monthly payments of m to getting out X
actual. I do this with a simple iterative formula in Excel.
So I've got a numerical method for solving this. The problem is (a) I can't find an analytical solution, which would be preferable, and (b) programming this with my limited knowledge of Python is quite clunky. So I suppose I have two problems...
Does this make sense?