0
$\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$
3
$\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$
  • $\begingroup$ thanks, but that still means I have to run one loop for j number of stocks right? $\endgroup$ – Prash Apr 25 '18 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 Apr 25 '18 at 23:49

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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