pandas-ta
pandas-ta copied to clipboard
ha_open has Nan Values when open is smoothed with ema
Which version are you running? The lastest version is on Github. Pip is for major releases. 0.3.14b0
Do you have TA Lib also installed in your environment? yes
Did you upgrade? Did the upgrade resolve the issue? nope, pandas_ta is newly installed on my new laptop
Describe the bug i am trying to create smoothed heikin ashi. however ha_open has Nan values. i am not sure if this is a bug or if this is a expected result
To Reproduce
import ccxt
import os,sys
import pandas_ta as pta
import pandas as pd
from datetime import datetime
import time
import pprint
import math
exchange = ccxt.binance({
'enableRateLimit': True,
'options': {
'defaultType': 'future', # ←-------------- quotes and 'future'
},
})
pair='BTC/USDT'
ohlc = exchange.fetch_ohlcv(pair, timeframe='5m')
df = pd.DataFrame(ohlc, columns = ['time', 'open', 'high', 'low', 'close', 'volume'])
df['time'] = pd.to_datetime(df['time'], unit='ms')
df['sopen']=pta.ema(df['open'], length=10)
df['shigh']=pta.ema(df['high'], length=10)
df['slow']=pta.ema(df['low'], length=10)
df['sclose']=pta.ema(df['close'], length=10)
df[['ha_open','ha_high','ha_low','ha_close']] = pta.ha(df['open'],df['high'],df['low'],df['close'])
df[['sha_open','sha_high','sha_low','sha_close']] = pta.ha(df['sopen'],df['shigh'],df['slow'],df['sclose'])
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('display.width', None)
pd.set_option('display.max_colwidth', None)
print(df)
Expected behavior sha_open is heiken ashi_open with smoothed open values using ema. it is returning Nan values but this doesnt happen in smoothed High,smoothed low and smoothed close. only on smoothed heikin ashi open (sha_open)
Screenshots

Additional context NA
Thanks for using Pandas TA!
Hello @mhgutier,
Noted. Unfortunately I do not have an expected time frame when I can address this. There is a fix but it does cause other unexpected bugs that will take more time to squash. Contributions are welcome to help address this and patch sooner. 😎
Kind Regards, KJ
Hi @mhgutier Not sure if you solved this but, The ha method will fill the HA_open cell 0 of the dataframe with 'open_.iloc[0] + close.iloc[0]' and than does the calc for the rest in a loop. So its a good idea to not send the ta.ha columns with na in them. I tend to do the smoothed HA as follows as I needed to match Tradingvies Smoothed HA.
- Calculate HA on OHLC
- Apply double smoothing ta.ema
- Dropna
#-------------------------------------------------------------------------------------------------
# Heikin Ashi section
#-------------------------------------------------------------------------------------------------
def get_HA(self, candles:list, ema: int):
""" Retrieve a new dataframe of Smoothed HA data"""
candles = candles[::-1] # important to have candles in oldest to newest
candles_df: pd.DataFrame = pd.DataFrame(candles)
candles_df['date_time'] = pd.to_datetime(candles_df['startAt'], unit='s',utc=False)
candles_df.set_index(pd.DatetimeIndex(candles_df["date_time"]), inplace=True)
df = self.HA(candles_df, 'HA_', '')
df = self.smooth_heikin_ashi(df, ema)
df = df[::-1] # return in reverse
return df
def smooth_heikin_ashi(self, df: pd.DataFrame, window):
""" Smooth the HA with ta.ema """
df = df.sort_index()
df['Open_SMA'] = ta.ema(ta.ema(df['HA_open'], length=window), length=window)
df['Close_SMA'] = ta.ema(ta.ema(df['HA_close'], length=window), length=window)
df['High_SMA'] = ta.ema(ta.ema(df['HA_high'], length=window), length=window)
df['Low_SMA'] = ta.ema(ta.ema(df['HA_low'], length=window), length=window)
df['HA_SMA_Direction'] = ta.increasing(df['Close_SMA'] ,1)
df.dropna(inplace=True)
return df
def HA(self, df: pd.DataFrame, pre: str, it: str):
""" calculate HA and assign columns"""
df = df.sort_index()
dfha = ta.ha(df[f'{it}open'],df[f'{it}high'],df[f'{it}low'],df[f'{it}close'])
dfha = dfha.rename(columns={'HA_open':f'{pre}open',"HA_high":f'{pre}high' ,'HA_low':f'{pre}low',"HA_close":f'{pre}close' })
df = pd.merge(df, dfha, right_index=True,left_index=True)
return df