如何在基于时间序列的数据框中使用不同时间范围的数据?

发布于 2025-01-13 06:34:08 字数 2468 浏览 0 评论 0原文

在包含纳斯达克指数价格的数据框中,我想获取不同时间单位的价格。

对于非交易者来说,最常见的价格表示称为“日本蜡烛”,并考虑每条线的开盘价、收盘价、最高价和最低价。 就我而言,数据帧的每一行代表一个 1 分钟的蜡烛。

在交易中,考虑不同时间单位的数据非常重要(了解短期、中期和长期趋势......) 所以我想添加一个列,该列将根据另一个时间单位显示数据:开盘价、15 分钟内(除了 1 分钟)当天的最高价和最低价。

因此,我使用 pandas 的重新采样方法在 15 分钟内获取包含信息的数据框(注意重命名列:

df = pd.DataFrame({
'Time' : ['2022-01-11 09:30:00', '2022-01-11 09:31:00', '2022-01-11 09:32:00', '2022-01-11 09:33:00', '2022-01-11 09:34:00', '2022-01-11 09:35:00', '2022-01-11 09:36:00' ,
      '2022-01-11 09:37:00' , '2022-01-11 09:38:00' , '2022-01-11 09:39:00', '2022-01-11 09:40:00', '2022-01-11 09:41:00', '2022-01-11 09:42:00','2022-01-11 09:43:00', 
      '2022-01-11 09:44:00', '2022-01-11 09:45:00',
      '2022-01-11 09:46:00', '2022-01-11 09:47:00', '2022-01-11 09:48:00', '2022-01-11 09:49:00', '2022-01-11 09:50:00', '2022-01-11 09:51:00', '2022-01-11 09:52:00' ,
      '2022-01-11 09:53:00' , '2022-01-11 09:54:00' , '2022-01-11 09:55:00', '2022-01-11 09:56:00', '2022-01-11 09:57:00', '2022-01-11 09:58:00','2022-01-11 09:59:00', 
      '2022-01-11 10:00:00'],
'Open' : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31], 
'High' : [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30],
'Low' : [4,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29],
'Close' : [2,0,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28]})

df['Time'] = pd.to_datetime(df['Time'])
df.set_index(['Time'], inplace =True)

df_15 = df_rand.resample('15min').first()
df_15

然后我使用 concat 方法将 1 分钟数据和 15 分钟数据分组在同一数据框中:

df_15.rename(columns={'Open' : 'Open_15',
                  'High' : 'High15',
                  'Low' : 'Low15',
                  'Close' : 'Close15'})

我遇到的两个问题遇到: 首先,不考虑列名称的更改,我有两次“关闭”列...并且我无法为每个单独的列创建指标:

df_all = pd.concat([df, df_15], axis=1)
df_all['Close']
  

其次,在这个新数据框中,在每刻钟之间, 15 分钟内开盘 - 收盘 - 高 - 低列的行是空的:

df_all = pd.concat([df, df_15], axis=1)
df_all

我想要的是这些行填充每分钟发生的新数据。 在提供的示例中,1 分钟内的低列(左侧)从 9:30 的 4 开始,并且该数字只会在 9:36 超过 5。 因此,15 分钟内“低”列的第一行(右侧)应以 4, 4, 4 , 4, 4 , 4, 5, 6 ... 开头,当达到最大值时,它将保持显示在专栏中直到下一刻钟。此时,倒计时重新开始。 该过程与“低”列的最小值类似。 对于“开盘”列,每行应为每季度第一分钟的开盘价。 (“收盘”列每分钟显示的价格与 1 分钟内收盘列中的价格相同)

由于有许多交易网站,因此必须有一个解决方案或库可以显示此数据。

有人可以帮我解决这些问题:

  1. 如何明确重命名为 15 分钟数据创建的列?
  2. 如何用新数据填充行?

In a dataframe containing the price of the Nasdaq index, I want to get the price over different time units.

For the non-traders, the most common price representation is called 'Japanese candle', and takes into account the opening, closing, high and low price for each line.
In my case, each line of the dataframe represents a 1min candle.

In trading it is important to take into account the data on different time units (to know the short term, medium and long term trends...)
So I want to add a column that will display data based on another time unit: the opening price, the highest and lowest of the day in 15 minutes (in addition to the 1min).

So I used the resample method of pandas to get a dataframe with the information in 15minutes (taking care to rename the columns :

df = pd.DataFrame({
'Time' : ['2022-01-11 09:30:00', '2022-01-11 09:31:00', '2022-01-11 09:32:00', '2022-01-11 09:33:00', '2022-01-11 09:34:00', '2022-01-11 09:35:00', '2022-01-11 09:36:00' ,
      '2022-01-11 09:37:00' , '2022-01-11 09:38:00' , '2022-01-11 09:39:00', '2022-01-11 09:40:00', '2022-01-11 09:41:00', '2022-01-11 09:42:00','2022-01-11 09:43:00', 
      '2022-01-11 09:44:00', '2022-01-11 09:45:00',
      '2022-01-11 09:46:00', '2022-01-11 09:47:00', '2022-01-11 09:48:00', '2022-01-11 09:49:00', '2022-01-11 09:50:00', '2022-01-11 09:51:00', '2022-01-11 09:52:00' ,
      '2022-01-11 09:53:00' , '2022-01-11 09:54:00' , '2022-01-11 09:55:00', '2022-01-11 09:56:00', '2022-01-11 09:57:00', '2022-01-11 09:58:00','2022-01-11 09:59:00', 
      '2022-01-11 10:00:00'],
'Open' : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31], 
'High' : [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30],
'Low' : [4,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29],
'Close' : [2,0,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28]})

df['Time'] = pd.to_datetime(df['Time'])
df.set_index(['Time'], inplace =True)

df_15 = df_rand.resample('15min').first()
df_15

I then used the concat method to group the 1min data and the 15min data within the same dataframe :

df_15.rename(columns={'Open' : 'Open_15',
                  'High' : 'High15',
                  'Low' : 'Low15',
                  'Close' : 'Close15'})

The 2 problems I am encountering:
First, the change of column name is not taken into account, I have 2 times the column 'Close'... and I cannot create indicators for each individual column:

df_all = pd.concat([df, df_15], axis=1)
df_all['Close']
  

Second, in this new dataframe, between each quarter of an hour, the rows of the columns Open - Close - High - Low in 15min are empty:

df_all = pd.concat([df, df_15], axis=1)
df_all

What I want is that these rows are filled with the new data that occurs every minute.
In the example provided, the Low column in 1min (the one on the left) starts at 4 at 9:30, and this number will only be exceeded at 9:36 by 5.
The first lines of the 'Low' column in 15min (the one on the right), should therefore start with 4, 4, 4 , 4, 4 , 4, 5, 6 ... and when a maximum is reached it remains displayed in the column until the next quarter of an hour. At this point the countdown resumes.
The process is similar maid with the minima for the 'low' column.
For the 'open' column, each line should be the opening price of the 1st minute of each quarter.
(and the 'close' column will show the same price for each minute as the one in the close column in 1min)

As there are many websites on trading there must be a solution or library that can display this data.

Can someone help me solve these problems:

  1. How to definitely rename the columns created for the 15min data?
  2. How to fill the rows with the new data?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文