2
$\begingroup$

I am trying to daily calculate the close-to-open return for j stocks for t days. Is there anyway I can calculate without using a for loop? I have one Dataframe for daily close prices and one for daily open. I am using python.

If it is close-to-close return, I am able to use the pct_change, not sure for close-to-open.

RCO(t,j) = SO(t,j)/SC(t-1,j)-1

where RCO is the returns on day t for stock j, SO is the opening price and SC is the previous days closing price.

$\endgroup$

1 Answer 1

4
$\begingroup$

Please refer to this code example, which calculates open-to-close returns over the past five trading days.

import pandas_datareader.data as web
symbol = 'WIKI/AAPL'
df = web.DataReader(symbol, 'quandl', '2018-01-01', '2018-03-31')
df['AdjClose'] / df['AdjOpen'].shift(5) - 1  # change 5 to the desired interval
$\endgroup$
2
  • $\begingroup$ thanks, but that still means I have to run one loop for j number of stocks right? $\endgroup$
    – Prash
    Commented Apr 25, 2018 at 23:44
  • $\begingroup$ @Prash It depends on how you construct the inputs. If you have one DataFrame for closing prices and one DataFrame for opening quotes with matching columns names, you can divide the two (with appropriate shifts) directly. You can also play around with higher dimensional data structures like xarray. $\endgroup$
    – Helin
    Commented Apr 25, 2018 at 23:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.