I'm a programmer and need a formula for calculating overal ROI [duplicate]

I'm programming a web app to track my hedge fund performance. I'd like to find my overall ROI for all time.

If I had a beginning balance and my current balance then finding the ROI is easy. For instance ((current balance / beginning balance) * 100) to get the percentage.

Where I am having difficulty is in the fact that each week there are debits/credits to the account.

So how can I accurately figure a lifetime ROI when each week I'm adding/removing funds?

Here is an example weekly report.

``````9/9/2013-9/13/2013 :
beginning balance = 25000 :
Profit/Loss in % = 16.45 :
Gross Profit/Loss = 4,112.72 :
Debit/Credit = -2000 :
New Balance 27,114
``````

Imagine the next 9 weeks look similar with fluctuating profit/loss percentages (weekly roi's)

Can any of you geniuses provide me with a formula for discovering how to ascertain exactly how well the hedge fund manager has been doing up to present day regardless of how many weeks I have been involved? I'm assuming some type of weekly compounding.

EDIT: So I found a formula that I think is useful and plugged in 4 weeks worth of data

``````    25000((1+i)^4) - 4000((1+i)^3) - 300((1+i)^2) - 1500((1+i)) = 32318.63
``````

How might I solve for 'i'?

• You have a hedge fund that accepted just \$25K, and permits weekly deposits/withdrawals? Dec 20 '13 at 21:29
• Lol, no it WILL be a hedge fund in about 2 months, right now it's just my brother who's been trading for about 10 years and sends me checks upon request ;) Dec 20 '13 at 21:34
• I see your edit. Did the link to similar question not help? Dec 21 '13 at 18:14
• I suggest you use money-weighted or continuously compounded (logarithmic) returns - effectively the same thing - as expained here to calculate the periodic returns - in your case, apparently weekly. (That requires a root solver algorithm.) Then accumulate the weekly returns for an annual return in the manner demonstrated here. I would add a fuller answer but the question has been marked as duplicate and answers cannot now be posted. Dec 21 '13 at 22:37
• Nevermind I just took (1.1253^(# of iterations = '4') - 1) * 100 and that equaled 60.34% which as you mentioned is the ROI for the whole 4 weeks. If I'm correct, then I want to thank you so much for helping me get here. I'm sure you've saved me a few more days of hair pulling. Next time you're in Columbus, Ohio, let me buy you a beer! Dec 22 '13 at 15:34

return on investment (%) = (Net profit / Investment) × 100

^ A much clearer formula to base your calculations upon. Now you just have to calculate your net profit for each week, add all those up, do the same for investment, and you have your answer.

• That formula works if i'm not constantly adding/withdrawing funds on a weekly basis. Unless I'm missing something? Dec 20 '13 at 21:38

You have an unequal stream of payments occurring at variable intervals of time. And you know the formula for ROI. What you are trying to determine is what interest rate for the whole period best reflects the fund accumulated value, And you have correctly deduced the formula to calculate each...

``````Time    Amount  Effect
4        25000  25000((1+i)^4) #contribution due to first deposit
3         4000  4000((1+i)^3) #contribution due to second deposit
2         -300  -300((1+i)^2) #contribution due to third (withdrawal)
1         1500  1500((1+i)^1) #contribution due to fourth deposit
``````

You need an iterative solution. You need start with a guess for your interest rate, i, and then iterate over your deposit vector, recalculating the fund with the guessed rate. You will need to adjust your guessed interest rate as you iterate, and the amount by which you adjust your guessed rate will affect how quickly you converge on a solution.

The amount you change your guessed rate will determine how quickly you converge on a solution (indeed, whether you converge on a solution). The general algorithm below will get you started. You need to tinker with the adjustment calculation in the guessrate function. The complication is that the ROI calculation is non-linear, so you will need something that adjusts non-linearly, and on an order similar to the calculation value. My sample guessrate function is linear, so you need to refine the function.

Try an iterative solution. Pick a value for i, and iterate

``````intialize_fund
{
value = 32318.63

deposits[]
deposits[0] = +25000
deposits[1] =  +4000
deposits[2] =   -300
deposits[3] =  +1500
duration = 5  //number of periods
}

calcval( deposits[], rate, duration )
{
accum = 0.0
for period=0; period<duration; ++period)
{
accum = accum * (1+rate)
if( exists deposits[period] ) accum += deposits[period]
}
return accum
}

#you will need to tinker with this until you get a function that
#converges
#converges quickly
guessrate( actual, calculated, rate )
{
adjustby = abs(actual - calculated) / actual
if(calculated > actual) rate = rate * (1.0 - adjustby)
else rate = rate * (1.0 + adjustby)
return rate
}

guessRoi
{
guess = 0.01 #initial guess
tryVal = calcval( guess, changes, duration )
while( abs(actual - tryVal) > .01 )
{
rate = guessrate( actual, tryVal, rate )
tryVal = calcval( deposits, rate, duration )
}
#guess has calculated rate, i
#tryRoi should have converged to actual accumulation
}
``````

What the heck is a deposit vector?

But yeah, you need to start with 2 (TWO) guesses for i - one you know is too high, and one you know is too low. Then try with the mid point to see if it is too high or too low. If too high, your new range is the lower half - else the upper half. Now repeat, with this new smaller range, again and again.

The function is non-linear, but smooth and well-behaved so you don't need to worry about convergence, just keep halving the interval on each iteration until you are as close as you want to be.

• The Excel Coal Seeking function will do this for you... Dec 21 '13 at 5:57