英文:
How to handle discrete times with df.resample?
问题
交易时间为9:00至10:15,10:30至11:15,11:15至11:30,13:30至15:00。
交易软件中的30分钟K线时间如下:
9:00至9:30,9:30至10:00,10:00至10:45,10:45至11:15,11:15至13:15,13:15至14:45,14:45至15:00。
如果我从df.resample('30T').agg({'open':'first','high':'max','low':'min','close':'last','volume':'sum'})
中得到的时间是连续的,我该如何使其显示与交易软件中的时间相同?
输入:
df.resample('30T').agg({'open':'first','high':'max','low':'min','close':'last','volume':'sum'})
输出:
由于市场休息15分钟,所以10:15至10:45的交易时间为30分钟。
我使用1分钟K线合成30分钟K线,所以我尝试:
df2 = df.reset_index()
r = df2.index // 30
df2.groupby(r).agg({'date':'last','open':'first','high':'max','low':'min','close':'last','volume':'sum'})
但是最后一个蜡烛必须是从14:45至15:00。
英文:
Trading hours are from 9:00 to 10:15, from 10:30 to 11:15, from 11:15 to 11:30, from 13:30 to 15:00
The 30-minute K-line time in the trading software is as follows:
9:00 to 9:30, 9:30 to 10:00, 10:00 to 10:45, 10:45 to 11:15, 11:15 to 13:15, 13:15 to 14:45 , 14:45 to 15:00
If the time I get from df.resample is continuous, how can I make it appear the same as in the trading software?
Input:
df.resample('30T').agg({'open':'first','high':'max','low':'min','close':'last','volume':'sum'})
Output:
10:15 to 10:45 because the market is closed for 15 minutes, so the trading time is 30 minutes
I use the 1-minute K-line to synthesize the 30-minute K-line, so I try
df2=df.reset_index()
r=df2.index//30
df2.groupby(r).agg({'date':'last','open':'first','high':'max','low':'min','close':'last','volume':'sum'})
But the last candlestick must be from 14:45 to 15:00
答案1
得分: 0
希望能帮助每个人。
df = df.reset_index()
trading_hour = ['09:30', '10:00', '10:45', '11:15', '13:45', '14:15', '14:45', '15:00', '21:30', '22:00', '22:30', '23:00']
m30 = df['date'].dt.strftime('%H:%M').isin(trading_hour).shift().fillna(False)
res = df.groupby(m30.cumsum()).agg({'date': 'last', 'open': 'first', 'high': 'max', 'low': 'min', 'close': 'last', 'volume': 'sum'})
这是你提供的代码部分的翻译。
英文:
Hope to help everyone.
df=df.reset_index()
trading_hour=['09:30','10:00','10:45','11:15','13:45','14:15','14:45','15:00','21:30','22:00','22:30','23:00']
m30=df['date'].dt.strftime('%H:%M').isin(trading_hour).shift().fillna(False)
res=df.groupby(m30.cumsum()).agg({'date':'last','open':'first','high':'max','low':'min','close':'last','volume':'sum'})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论