【问题标题】:Pandas OHLC aggregation on OHLC dataPandas OHLC 对 OHLC 数据的聚合
【发布时间】:2016-07-13 09:15:41
【问题描述】:

我了解,使用一列数据对 Pandas 中的时间序列数据进行 OHLC 重新采样将完美运行,例如在以下数据帧上:

>>df
ctime       openbid
1443654000  1.11700
1443654060  1.11700
...

df['ctime']  = pd.to_datetime(df['ctime'], unit='s')
df           = df.set_index('ctime')
df.resample('1H',  how='ohlc', axis=0, fill_method='bfill')


>>>
                     open     high     low       close
ctime                                                   
2015-09-30 23:00:00  1.11700  1.11700  1.11687   1.11697
2015-09-30 24:00:00  1.11700  1.11712  1.11697   1.11697
...

但是如果数据已经是 OHLC 格式,我该怎么办?据我所知,API 的 OHLC 方法会为每一列计算一个 OHLC 切片,因此如果我的数据采用以下格式:

             ctime  openbid  highbid   lowbid  closebid
0       1443654000  1.11700  1.11700  1.11687   1.11697
1       1443654060  1.11700  1.11712  1.11697   1.11697
2       1443654120  1.11701  1.11708  1.11699   1.11708

当我尝试重新采样时,每列都会得到一个 OHLC,如下所示:

                     openbid                             highbid           \
                        open     high      low    close     open     high   
ctime                                                                       
2015-09-30 23:00:00  1.11700  1.11700  1.11700  1.11700  1.11700  1.11712   
2015-09-30 23:01:00  1.11701  1.11701  1.11701  1.11701  1.11708  1.11708 
...
                                        lowbid                             \
                         low    close     open     high      low    close   
ctime                                                                       
2015-09-30 23:00:00  1.11700  1.11712  1.11687  1.11697  1.11687  1.11697   
2015-09-30 23:01:00  1.11708  1.11708  1.11699  1.11699  1.11699  1.11699  
...

                    closebid                             
                        open     high      low    close  
ctime                                                    
2015-09-30 23:00:00  1.11697  1.11697  1.11697  1.11697  
2015-09-30 23:01:00  1.11708  1.11708  1.11708  1.11708  

有没有人愿意分享的快速(ish)解决方法,而无需我深入了解熊猫手册?

谢谢。

ps,有这个答案 - Converting OHLC stock data into a different timeframe with python and pandas - 但那是 4 年前的事了,所以我希望有一些进展。

【问题讨论】:

    标签: python python-2.7 pandas dataframe resampling


    【解决方案1】:

    这与您链接的答案相似,但更简洁、更快,因为它使用优化的聚合,而不是 lambda。

    注意resample(...).agg(...) 语法需要pandas 版本0.18.0

    In [101]: df.resample('1H').agg({'openbid': 'first', 
                                     'highbid': 'max', 
                                     'lowbid': 'min', 
                                     'closebid': 'last'})
    Out[101]: 
                          lowbid  highbid  closebid  openbid
    ctime                                                   
    2015-09-30 23:00:00  1.11687  1.11712   1.11708    1.117
    

    【讨论】:

    • 是的,我会选择那个;这意味着更新熊猫,但我的功能的方式意味着这是更可取的选择。谢谢。
    • 知道是否可以将 fill_method='bfill' 方法添加到该解决方案中以处理 NAN 问题?
    • 忘记最后一个问题,方法已更改为 .bfill()
    • 如果您在上面的 ctime 索引中遇到错误,这里有一个替代方法:df = df.set_index('datetime')
    • 我不得不使用这些参数来匹配我的图表平台df.resample('1H', closed = 'right',label = 'right').agg({'open': 'first', 'high': 'max', 'low': 'min', 'close': 'last'})
    【解决方案2】:

    您需要使用 OrderedDict 在新版本的 pandas 中保持行顺序,如下所示:

    import pandas as pd
    from collections import OrderedDict
    
    df['ctime'] = pd.to_datetime(df['ctime'], unit='s')
    df = df.set_index('ctime')
    df = df.resample('5Min').agg(
        OrderedDict([
            ('open', 'first'),
            ('high', 'max'),
            ('low', 'min'),
            ('close', 'last'),
            ('volume', 'sum'),
        ])
    )
    

    【讨论】:

      【解决方案3】:

      给定一个包含价格和金额列的数据框

      def agg_ohlcv(x):
          arr = x['price'].values
          names = {
              'low': min(arr) if len(arr) > 0 else np.nan,
              'high': max(arr) if len(arr) > 0 else np.nan,
              'open': arr[0] if len(arr) > 0 else np.nan,
              'close': arr[-1] if len(arr) > 0 else np.nan,
              'volume': sum(x['amount'].values) if len(x['amount'].values) > 0 else 0,
          }
          return pd.Series(names)
      
      df = df.resample('1min').apply(agg_ohlcv)
      df = df.ffill()
      

      【讨论】:

        【解决方案4】:

        对我来说,从 OHLC 到 OHLC 的转换是这样的:

        df.resample('1H').agg({
            'openbid':'first',
            'highbid':'max',
            'lowbid':'min',
            'closebid':'last'
        })
        

        【讨论】:

          【解决方案5】:

          这个好像有用,

          def ohlcVolume(x):
              if len(x):
                  ohlc={ "open":x["open"][0],"high":max(x["high"]),"low":min(x["low"]),"close":x["close"][-1],"volume":sum(x["volume"])}
                  return pd.Series(ohlc)
          
          daily=df.resample('1D').apply(ohlcVolume)
          

          【讨论】:

          • 请解释一下。 Volume len() 怎么了?
          猜你喜欢
          • 2022-01-05
          • 2012-11-08
          • 1970-01-01
          • 2018-06-14
          • 1970-01-01
          • 2016-06-17
          • 2018-05-18
          • 2021-11-18
          • 1970-01-01
          相关资源
          最近更新 更多