【问题标题】:Is there any standard function that shows me when the HIGH and LOW of a OHLC conversion happened?当 OHLC 转换的 HIGH 和 LOW 发生时,是否有任何标准函数可以显示?
【发布时间】:2019-05-25 15:40:13
【问题描述】:

我能够转换刻度数据,也可以使用 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

【问题讨论】:

    标签: python pandas numpy resampling


    【解决方案1】:

    在 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  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-02
      • 2010-12-02
      • 2013-08-13
      • 1970-01-01
      • 2020-08-03
      • 2011-08-13
      • 1970-01-01
      • 2013-03-14
      相关资源
      最近更新 更多