持续5分钟获取开盘价,最高价,最低价,最低价python [英] Getting Open, High, Low, Close for 5 min stock data python

查看:92
本文介绍了持续5分钟获取开盘价,最高价,最低价,最低价python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataFrame,其中包含带有以下列的库存数据:

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

其每20秒内有数百只股票的股价数据.我已将时间"列设置为日期时间格式.

Its stock price data for every 20 seconds for several hundred stocks. I have put the time column into datetime format.

在这里,我需要将数据分成5分钟间隔,然后使用以下方法绘制数据:

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")

图表很好,但是问题是我需要每只股票在每5分钟间隔内有高价,低价,开盘价和收盘价.我需要用它来制作烛台图,一旦我有高,低,打开和关闭列,就可以轻松地完成此操作.

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.

开盘价和收盘价分别是5分钟周期开始和结束时的价格.高和低列分别是该时间间隔内的最高价格和该时间间隔内的最低价格.

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.

推荐答案

IIUC,在 groupby 中,您可以通过"ticker"进行操作,也可以使用 pd.NamedAgg ,其中 first 用于打开, last 用于关闭, max 表示高, min 表示低.

IIUC, in the groupby you can do it by 'ticker' but also using pd.Grouper on 'time' with a frequence of 5 minutes. In the agg method, you can use since pandas>0.25 pd.NamedAgg with first for open, last for close, max for high and min for low.

# dummy variables
np.random.seed(0)
df = pd.DataFrame({'time':pd.date_range('2020-04-01 9:30:00', freq='20s', periods=50).tolist()*2, 
                   'ticker': ['ticker1']*50 + ['ticker2']*50, 
                   'price':np.random.randint(30, 50, 100)})

# groupby and agg, then reset_index
df_f = df.groupby(['ticker', pd.Grouper(key='time', freq='5T')])\
         .agg(open=pd.NamedAgg(column='price', aggfunc='first'), 
              close=pd.NamedAgg(column='price', aggfunc='last'), 
              high=pd.NamedAgg(column='price', aggfunc='max'), 
              low=pd.NamedAgg(column='price', aggfunc='min'))\
         .reset_index()

print (df_f)
    ticker                time  open  close  high  low
0  ticker1 2020-04-01 09:30:00    42     37    49   30
1  ticker1 2020-04-01 09:35:00    44     33    49   30
2  ticker1 2020-04-01 09:40:00    47     32    49   30
3  ticker1 2020-04-01 09:45:00    30     36    36   30
4  ticker2 2020-04-01 09:30:00    38     48    48   31
5  ticker2 2020-04-01 09:35:00    30     44    45   30
6  ticker2 2020-04-01 09:40:00    45     34    48   30
7  ticker2 2020-04-01 09:45:00    32     40    46   32

使用plotly绘制烛台,您可以执行以下操作:

for plotting candlestick with plotly, you can do:

import plotly.figure_factory

def plot_tick(data, ticker):
    ts = data[data["ticker"]==ticker].reset_index(drop=True)
    fig = plotly.figure_factory.create_candlestick(ts.open, ts.high, ts.low, 
                                                   ts.close, dates=ts.time)
    fig.show()

plot_tick(df_f, 'ticker1')

这篇关于持续5分钟获取开盘价,最高价,最低价,最低价python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆