I was trying to adapt some code I found online for heikin ashi candles
the code is :
def heikin_ashi(self, df):
heikin_ashi_df = pd.DataFrame(index=df.index.values, columns=['HE_Open', 'HE_High', 'HE_Low', 'HE_Close'])
heikin_ashi_df['HE_Close'] = (df['Open'] + df['High'] + df['Low'] + df['Close']) / 4
for i in range(len(df)):
if i == 0:
heikin_ashi_df.iat[0, 0] = df['Open'].iloc[0]
else:
heikin_ashi_df.iat[i, 0] = (heikin_ashi_df.iat[i - 1, 0] + heikin_ashi_df.iat[i - 1, 3]) / 2
heikin_ashi_df['HE_High'] = heikin_ashi_df.loc[:, ['HE_Open', 'HE_Close']].join(df['High']).max(axis=1)
heikin_ashi_df['HE_Low'] = heikin_ashi_df.loc[:, ['HE_Open', 'HE_Close']].join(df['Low']).min(axis=1)
return heikin_ashi_df
but this method returns a new pandas table and I wanted to just add the new rows to my existing table
this is my attempt:
def heikin_ashi_full(self, df):
df.append(pd.Series(name='HE_Open', dtype=float))
df.append(pd.Series(name='HE_High', dtype=float))
df.append(pd.Series(name='HE_Low', dtype=float))
df.append(pd.Series(name='HE_Close', dtype=float))
df['HE_Close'] = (df['Open'] + df['High'] + df['Low'] + df['Close']) / 4
for i in range(len(df)):
if i == 0:
df.iat[0, 0] = df['Open'].iloc[0]
else:
df.iat[i, 0] = (df.iat[i - 1, 0] + df.iat[i - 1, 3]) / 2
df['HE_High'] = df.loc[:, ['HE_Open', 'HE_Close', 'High']].max(axis=1)
df['HE_Low'] = df.loc[:, ['HE_Open', 'HE_Close', 'Low']].min(axis=1)
return df
unfortunately the method crashes at line 15 of the method
df['HE_High'] = df.loc[:, ['HE_Open', 'HE_Close', 'High']].max(axis=1)
traceback is shown here :
the main tables is crated from files with data like this:
Date,Open,High,Low,Close,Volume,OpenInterest
2020-08-25 06:00,15.12,15.25,14.82,15.05,3824776,0.0
2020-08-26 06:00,15.09,15.44,15.02,15.07,5933882,0.0
2020-08-27 06:00,15.22,15.33,14.672,14.84,5728962,0.0
2020-08-28 06:00,15.01,15.18,14.8605,15.12,5992532,0.0
2020-08-31 06:00,15.23,15.23,14.62,14.62,7000994,0.0
2020-09-01 06:00,14.64,15.18,14.24,15.08,7347598,0.0
Interestingly enough the first method I got from the internet seems to work just fine and is accurate. I'm not to familiar with pandas put have no idea how I did it wrong nor could I find a fix. Your help is very much appreciated and thank you in advance for your time.