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
I'm not that knowledgeable regarding Python, or Pandas, but after some research, this is what I could figure would be a good solution.
Please, feel free to add any comments. I very much appreciate.
I used namedtuples and itertuples (seem to be the fastest, if looping through a DataFrame).
I hope it helps!
def HA(df):
df['HA_Close']=(df['Open']+ df['High']+ df['Low']+df['Close'])/4
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'] = ha_open
previous_row = nt(ha_open, row.Close)
i += 1
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