Heiken Ashi Using pandas python

前端 未结 7 2067
南方客
南方客 2021-01-31 23:23

I was defining a function Heiken Ashi which is one of the popular chart type in Technical Analysis. I was writing a function on it using Pandas but finding little difficulty. T

7条回答
  •  遥遥无期
    2021-01-31 23:49

    Here is the fastest, accurate and efficient implementation as per my tests:

    def HA(df):
        df['HA_Close']=(df['Open']+ df['High']+ df['Low']+df['Close'])/4
    
        idx = df.index.name
        df.reset_index(inplace=True)
    
        for i in range(0, len(df)):
            if i == 0:
                df.set_value(i, 'HA_Open', ((df.get_value(i, 'Open') + df.get_value(i, 'Close')) / 2))
            else:
                df.set_value(i, 'HA_Open', ((df.get_value(i - 1, 'HA_Open') + df.get_value(i - 1, 'HA_Close')) / 2))
    
        if idx:
            df.set_index(idx, inplace=True)
    
        df['HA_High']=df[['HA_Open','HA_Close','High']].max(axis=1)
        df['HA_Low']=df[['HA_Open','HA_Close','Low']].min(axis=1)
        return df
    

    Here is my test algorithm (essentially I used the algorithm provided in this post to benchmark the speed results):

    import quandl
    import time
    
    df = quandl.get("NSE/NIFTY_50", start_date='1997-01-01')
    
    def test_HA():
        print('HA Test')
        start = time.time()
        HA(df)
        end = time.time()
        print('Time taken by set and get value functions for HA {}'.format(end-start))
    
        start = time.time()
        df['HA_Close_t']=(df['Open']+ df['High']+ df['Low']+df['Close'])/4
    
        from collections import namedtuple
        nt = namedtuple('nt', ['Open','Close'])
        previous_row = nt(df.ix[0,'Open'],df.ix[0,'Close'])
        i = 0
        for row in df.itertuples():
            ha_open = (previous_row.Open + previous_row.Close) / 2
            df.ix[i,'HA_Open_t'] = ha_open
            previous_row = nt(ha_open, row.Close)
            i += 1
    
        df['HA_High_t']=df[['HA_Open_t','HA_Close_t','High']].max(axis=1)
        df['HA_Low_t']=df[['HA_Open_t','HA_Close_t','Low']].min(axis=1)
        end = time.time()
        print('Time taken by ix (iloc, loc) functions for HA {}'.format(end-start))
    

    Here is the output I got on my i7 processor (please note the results may vary depending on your processor speed but I assume that the results will be similar):

    HA Test
    Time taken by set and get value functions for HA 0.05005788803100586
    Time taken by ix (iloc, loc) functions for HA 0.9360761642456055
    

    My experience with Pandas shows that functions like ix, loc, iloc are slower in comparison to set_value and get_value functions. Moreover computing value for a column on itself using shift function gives erroneous results.

提交回复
热议问题