python-3.x 反转长模式

6yt4nkrj  于 2023-05-02  发布在  Python
关注(0)|答案(1)|浏览(230)
self.minute_olhc_df
                               timestamp    open    high     low   close    volume  percentage_change  dollar_volume
timestamp                                                                                                           
2023-04-12 08:00:00  2023-04-12 08:00:00  0.4451  0.7200  0.4451  0.5800   32664.0          48.760331   1.674193e+04
2023-04-12 08:01:00  2023-04-12 08:01:00  0.5700  0.8800  0.5441  0.7500  245138.0          81.818182   1.785330e+05
2023-04-12 08:02:00  2023-04-12 08:02:00  0.7613  0.8899  0.6800  0.8786  226893.0          83.863636   3.645739e+05
2023-04-12 08:03:00  2023-04-12 08:03:00  0.8786  0.9500  0.8140  0.9350  249540.0          96.280992   5.908568e+05
2023-04-12 08:04:00  2023-04-12 08:04:00  0.9399  0.9870  0.8949  0.9142  264829.0         103.925620   8.363665e+05
...                                  ...     ...     ...     ...     ...       ...                ...            ...
2023-04-12 19:55:00  2023-04-12 19:55:00  0.8300  0.8300  0.8300  0.8300     462.0          71.487603   9.644505e+07
2023-04-12 19:56:00  2023-04-12 19:56:00  0.8390  0.8489  0.8217  0.8217   13363.0          75.392562   9.645615e+07
2023-04-12 19:57:00  2023-04-12 19:57:00  0.8350  0.8350  0.8350  0.8350     583.0          72.520661   9.645663e+07
2023-04-12 19:58:00  2023-04-12 19:58:00  0.8337  0.8337  0.8329  0.8329    1635.0          72.252066   9.645800e+07
2023-04-12 19:59:00  2023-04-12 19:59:00  0.8218  0.8397  0.8205  0.8205    9239.0          73.491736   9.646558e+07

[673 rows x 8 columns]

这是一个python dataframe我有两个变量i即morning_high_pricemorning_high_time。它们是上午9:30至中午之间的最高价格及其发生的时间。我如何才能获得中午到下午4:30之间价格与morning_high_price相同或低2%以内的所有时刻?

ChatGPT解决方案:

# Set the time range
start_time = pd.Timestamp('2023-04-12 12:00:00')
end_time = pd.Timestamp('2023-04-12 16:30:00')

# Get the morning high price and time
morning_high_price = self.minute_olhc_df['high']['2023-04-12 09:30:00':'2023-04-12 12:00:00'].max()
morning_high_time = self.minute_olhc_df['high']['2023-04-12 09:30:00':'2023-04-12 12:00:00'].idxmax()

# Filter the dataframe to the time range and where the price is within 2% lower
filtered_df = self.minute_olhc_df[(self.minute_olhc_df['timestamp'] > start_time) & (self.minute_olhc_df['timestamp'] < end_time) & (self.minute_olhc_df['high'] >= morning_high_price*0.98) & (self.minute_olhc_df['high'] <= morning_high_price)]

# Get the moments where the prices were the same as morning_high_price and within 2% lower
result = filtered_df[filtered_df['high'] == morning_high_price]

这是chat gpt的解决方案,但这真的不是我想要的。它在中午到下午4:30之间给我最高的价格,但这不是我所期望的。粗略地说,我只需要知道上午的最高价格是否在下午重新出现,并且非常接近。

r3i60tvu

r3i60tvu1#

在一天内从yfinance获取数据。在本例中,High列的名称是您的high列(如果您要更正数据)。为了过滤日期时间,我使用日期,因为使用slices来索引'2023-04-12 09:30:00':'2023-04-12 12:00:00',可能会发现,例如,12点钟位置的第一行将是'2023-04-12 12:01:00'。

import pandas as pd
import yfinance as yf

minute_olhc_df = yf.download(tickers='MSFT', period='1d', interval='1m')

morning_high_time = minute_olhc_df.loc[(minute_olhc_df.index >= '2023-04-27 09:30:00')
                                       & (minute_olhc_df.index < '2023-04-27 12:00:00'), 'High'].idxmax()
morning_high_price = minute_olhc_df.loc[morning_high_time, 'High']

以确定是否已超过最大值。这会获取大于最大值的行。第一行是第一个超出的最大值。

more_max = minute_olhc_df.loc[(minute_olhc_df.index >= '2023-04-27 12:00:00') &
                              (minute_olhc_df.index <= '2023-04-27 16:30:00')
                              & (minute_olhc_df['High'] > morning_high_price), 'High']

第一超额的指数可以这样获得:

more_max.index[0]

如果需要最大值减2%,则将条件替换为:

max_minus2percent = minute_olhc_df.loc[(minute_olhc_df.index >= '2023-04-27 12:00:00') &
                                       (minute_olhc_df.index <= '2023-04-27 16:30:00')
                                       & (minute_olhc_df['High'] > morning_high_price * 0.98), 'High']

关于GPT:filtered_df[filtered_df['high'] == morning_high_price]这会找到一个最大值,等于早晨。我承认这个选项,最大会比上午多但不等于它,那么就不会有结果了。

相关问题