open high low close volume date time
0 0.9738 0.9742 0.9738 0.9740 48 2009-09-27 1900-01-01 18:00:00
1 0.9738 0.9739 0.9737 0.9737 11 2009-09-27 1900-01-01 18:01:00
2 0.9733 0.9734 0.9733 0.9734 6 2009-09-27 1900-01-01 18:02:00
3 0.9734 0.9734 0.9734 0.9734 1 2009-09-27 1900-01-01 18:03:00
4 0.9735 0.9735 0.9735 0.9735 1 2009-09-27 1900-01-01 18:04:00
I have a large dataframe that looks like above (1-minute intraday bars of a stock price).
question: how do i get the next bar of prices if there are no prices for the 1-minute bar at 8:00:00 am? I want to be able to get the open price for each day where the time = 8:00:00 am, or the next closest price after.
The function below gets the open, cumulative high, cumulative low, and the close reconstructed from some continuous section of bars (continually from a set open time to a set close time).
def getOpenHighLowClose(x=None, openTime=None, closeTime=None):
x.loc[(x['time']==openTime), 'openPriceOfDay'] = x['open']
x.loc[(x['time']==closeTime), 'closePriceOfDay'] = x['close']
x['openPriceOfDay']=x['openPriceOfDay'].fillna(0)
x['closePriceOfDay']=x['closePriceOfDay'].fillna(0)
x['OpenCashMkt']=x['openPriceOfDay'].max()
x['CloseCashMkt']=x['closePriceOfDay'].max()
x.loc[(x['time']>=openTime) & (x['time']<=closeTime), 'cumHigh'] = x['high'].cummax()
x.loc[(x['time']>=openTime) & (x['time']<=closeTime), 'cumLow'] = x['low'].cummin()
I wrote the code this way so that I could construct my own [open high low close] for any timeframe and use .shift(x) to create a return series using groupby 'date'.
I am a novice so please tell me if I can clarify any further.
Thanks!