Back to Community
How to calculate daily return?

Hi,

I am very new to the community as well as to coding. I have a very simple question but it has been confusing me for the past several days.

I am trying to use the UsEquity data set and compute the daily return for all stocks: Below is how I define it:

inputs = [USEquityPricing.close]
window_length = 2

def compute(self, today, assets, out, close):
out[:] = close[0]/close[-1]*100

Basically, I am using the current period's closing price and divide it by previous period's closing price. Then I magnify it by 100 just for the ease of reading the result.

After running through several back tests, I found that the return calculated using this formula seems to be wrong (stocks that have losses on a particular day have return calculated to be above 100 in my case). Can anyone here shed a light on where I might be wrong?

Thanks in advance.

5 responses

Must be:

out[:] = (close[-1]/close[0] - 1) * 100

Hi Thanasis,

Thank you for the response! I tried and it worked out perfectly. May I ask you why I need to use the prior day divide by current day? It seems to be up-side down?

Hi mencgche chen,

close[-1] is the current day (close[-2] is the prvious one and so on...)

close[0] is the start day (in your case with window_length = 2 it is the previous one)

Hi Thanasis,

Thank you very much for the clarification. Makes sense.

your welcome