I have a DataFrame that contains stock data with the following columns:
time ticker price
0 2020-04-02 09:30:35 EV 33.860
1 2020-04-02 09:00:00 AMG 60.430
2 2020-04-02 09:30:35 AMG 60.750
3 2020-04-02 09:00:00 BLK 455.350
4 2020-04-02 09:30:35 BLK 451.514
... ... ... ...
502596 2020-04-02 13:00:56 TLT 166.450
502597 2020-04-02 13:00:56 VXX 47.150
502598 2020-04-02 13:00:56 TSLA 529.800
502599 2020-04-02 13:00:56 BIDU 103.500
502600 2020-04-02 13:00:56 ON 12.700
Its stock price data for every 20 seconds for several hundred stocks. I have put the time column into datetime format.
From here, I need to separate the data into 5 minute intervals then plot the data, which I've done by using:
out = df.groupby("ticker")\
.apply(lambda x: x.set_index("time")\
.resample("5T")\
.first()\
.reset_index())\
.reset_index(drop=True)
dffinal = out.dropna(axis=0)
def plot_tick(data, ticker):
ts = data[data["ticker"]==ticker].reset_index(drop=True)
ts.plot(x="time", y="price",title=ticker,figsize=(20,20),kind='line')
plot_tick(dffinal, "A")
The chart comes out well, but the problem is I need the high ,low, open, and close prices for each 5 minute interval for each stock. I need this in order to make a candlestick chart, which I can easily do once I have high, low,open, and close columns.
The open and close is the price at the beginning and end of the 5 minute period, respectively. The high and low column is the highest price during the interval, and lowest price during the interval, respectively.
So I'm looking for and end result like this:
time ticker price open close high low
0 2020-04-02 09:00:00 A 72.6700
6 2020-04-02 09:30:00 A 72.1400
7 2020-04-02 09:35:00 A 72.5400
8 2020-04-02 09:40:00 A 72.4000
9 2020-04-02 09:45:00 A 72.3338
... ... ... ...
38895 2020-04-02 12:40:00 ZUMZ 17.6000
38896 2020-04-02 12:45:00 ZUMZ 17.6300
38897 2020-04-02 12:50:00 ZUMZ 17.6000
38898 2020-04-02 12:55:00 ZUMZ 17.7400
38899 2020-04-02 13:00:00 ZUMZ 17.560
with the open, close,high,low filled in, obviously.