当 OHLC 转换发生 HIGH 和 LOW 时,是否有任何标准功能可以显示给我?

Is there any standard function that shows me when the HIGH and LOW of a OHLC conversion happened?

我可以转换报价数据,也可以使用 pandas 的 resample 函数对其重新采样。请参阅下面的代码。

是否还有任何标准的 numpy / pandas / .... 功能可以让我及时返回 highlow 值出现的时刻?

我想将这些日期时间作为结果数据框中的两个额外列。

import numpy as np
import pandas as pd
np.random.seed(1)
data = np.random.rand(500)
myRange = pd.date_range('2018-04-09', periods=500, freq='50s')

df = pd.DataFrame(data,myRange)
df.columns = ['price']

dfOHLC = df.price.resample('1h').ohlc()

dfOHLC_resampled = df.resample('2h').agg({'open': 'first',
                                        'high': 'max',
                                        'low': 'min',
                                        'close': 'last',
                                        })
print(dfOHLC)
#                          open      high       low     close
# 2018-04-09 00:00:00  0.417022  0.988861  0.000114  0.137475
# 2018-04-09 01:00:00  0.139276  0.997323  0.002870  0.121343
# 2018-04-09 02:00:00  0.044552  0.988616  0.012556  0.505662
# 2018-04-09 03:00:00  0.021525  0.976759  0.000402  0.802161
# 2018-04-09 04:00:00  0.572489  0.990472  0.022330  0.990472
# 2018-04-09 05:00:00  0.300248  0.993913  0.018333  0.450087
# 2018-04-09 06:00:00  0.478073  0.989955  0.003018  0.227900

print(dfOHLC_resampled)
#                          open      high       low     close
#                         price     price     price     price
# 2018-04-09 00:00:00  0.417022  0.997323  0.000114  0.121343
# 2018-04-09 02:00:00  0.044552  0.988616  0.000402  0.802161
# 2018-04-09 04:00:00  0.572489  0.993913  0.018333  0.450087
# 2018-04-09 06:00:00  0.478073  0.989955  0.003018  0.227900

在版本 pandas 0.24+ 中可以使用 Series.idxmaxSeries.idxmin:

dfOHLC_resampled = dfOHLC.resample('2h').agg({'open': 'first',
                                        'high': ['max', 'idxmax'],
                                        'low': ['min', 'idxmin'],
                                        'close': 'last',
                                        })
print(dfOHLC_resampled)
                         open      high                           low  \
                        first       max              idxmax       min   
2018-04-09 00:00:00  0.417022  0.997323 2018-04-09 01:00:00  0.000114   
2018-04-09 02:00:00  0.044552  0.988616 2018-04-09 02:00:00  0.000402   
2018-04-09 04:00:00  0.572489  0.993913 2018-04-09 05:00:00  0.018333   
2018-04-09 06:00:00  0.478073  0.989955 2018-04-09 06:00:00  0.003018   

                                            close  
                                 idxmin      last  
2018-04-09 00:00:00 2018-04-09 00:00:00  0.121343  
2018-04-09 02:00:00 2018-04-09 03:00:00  0.802161  
2018-04-09 04:00:00 2018-04-09 05:00:00  0.450087  
2018-04-09 06:00:00 2018-04-09 06:00:00  0.227900