Pandas OHLC aggregation on OHLC data

前端 未结 5 1480
刺人心
刺人心 2020-12-22 23:57

I understand that OHLC re-sampling of time series data in Pandas, using one column of data, will work perfectly, for example on the following dataframe:

>         


        
相关标签:
5条回答
  • 2020-12-23 00:22

    Converstion from OHLC to OHLC for me worked like this:

    df.resample('1H').agg({
        'openbid':'first',
        'highbid':'max',
        'lowbid':'min',
        'closebid':'last'
    })
    
    0 讨论(0)
  • 2020-12-23 00:27

    Given a dataframe with price and amount columns

    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()
    
    0 讨论(0)
  • 2020-12-23 00:28

    You need to use an OrderedDict to keep row order in the newer versions of pandas, like so:

    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'),
        ])
    )
    
    0 讨论(0)
  • 2020-12-23 00:35

    This is similar to the answer you linked, but it a little cleaner, and faster, because it uses the optimized aggregations, rather than lambdas.

    Note that the resample(...).agg(...) syntax requires pandas version 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
    
    0 讨论(0)
  • 2020-12-23 00:39

    This one seems to work,

    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)
    
    0 讨论(0)
提交回复
热议问题