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. This is how Heiken Ashi [HA] looks like-
Heikin-Ashi Candle Calculations
HA_Close = (Open + High + Low + Close) / 4
HA_Open = (previous HA_Open + previous HA_Close) / 2
HA_Low = minimum of Low, HA_Open, and HA_Close
HA_High = maximum of High, HA_Open, and HA_Close
Heikin-Ashi Calculations on First Run
HA_Close = (Open + High + Low + Close) / 4
HA_Open = (Open + Close) / 2
HA_Low = Low
HA_High = High
There is a lot of stuff available on various websites using for loop and pure python but i think Pandas can also do job well. This is my progress-
def HA(df):
df['HA_Close']=(df['Open']+ df['High']+ df['Low']+ df['Close'])/4
ha_o=df['Open']+df['Close'] #Creating a Variable
#(for 1st row)
HA_O=df['HA_Open'].shift(1)+df['HA_Close'].shift(1) #Another variable
#(for subsequent rows)
df['HA_Open']=[ha_o/2 if df['HA_Open']='nan' else HA_O/2]
#(error Part Where am i going wrong?)
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
Can Anyone Help me with this please?` It doesnt work.... I tried on this-
import pandas_datareader.data as web
import HA
import pandas as pd
start='2016-1-1'
end='2016-10-30'
DAX=web.DataReader('^GDAXI','yahoo',start,end)
This is the New Code i wrote
def HA(df):
df['HA_Close']=(df['Open']+ df['High']+ df['Low']+df['Close'])/4
...: ha_o=df['Open']+df['Close']
...: df['HA_Open']=0.0
...: HA_O=df['HA_Open'].shift(1)+df['HA_Close'].shift(1)
...: df['HA_Open']= np.where( df['HA_Open']==np.nan, ha_o/2, HA_O/2 )
...: 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
But still the HA_Open result was not satisfactory
df['HA_Open']= np.where( df['HA_Open']==np.nan, ha_o/2, HA_O/2 )
, but I think you also failed to definedf['HA_Open']
? – Englebertimport numpy as np
if you didn't already – Englebert