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
Heikin Ashi candles are calculated this way: Open: (Open (previous candle) + Close (previous candle))/2. Close: (Open + Low + Close + High)/4. High: the same of the actual candle.
Heikin Ashi charts can be used on any timeframe. The calculation is applied to the chosen time frame. Swing traders typically look at hourly, four-hour, or daily charts.
Heikin-Ashi has a smoother look because it is essentially taking an average of the movement. There is a tendency with Heikin-Ashi for the candles to stay red during a downtrend and green during an uptrend, whereas normal candlesticks alternate color even if the price is moving dominantly in one direction.
Reliability: Heikin-Ashi is a very reliable indicator, providing accurate results. It uses historical data, which is also quite dependable. Filtering of market noise: The indicator filters out market noise and reduces small corrections making the signals more transparent.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With