0

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 :

traceback

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.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
  • 1) Could you link to a description of the Heikin Ashi method which describes how to calculate it? The resources I found were a little vague. 2) I think you can avoid the loop in your solution by [shifting one of your columns by one](https://stackoverflow.com/questions/20095673/shift-column-in-pandas-dataframe-up-by-one). – Nick ODell Jan 16 '21 at 01:24
  • The `TraceBack` should be posted as text. **[No Screenshots](https://meta.stackoverflow.com/questions/303812/)** of code or data. Always provide a [mre] with code, **data, errors, current output, and expected output**, as **[formatted text](https://stackoverflow.com/help/formatting)**. If relevant, plot images are okay. Please see [How to ask a good question](https://stackoverflow.com/help/how-to-ask). – Trenton McKinney Jan 16 '21 at 05:41

1 Answers1

0

I tried your code and my first issue is that I am trying to add columns with df.append(), but I think it is because the columns are not being added. If I try to run your code after adding the opening price, high price and bottom price with empty, the result will be displayed. The column structure of the dataframe changes some, so I have modified it accordingly. Also, I think this answer about "heikin-ashi" on SO will be helpful.

import pandas as pd
import numpy as np
import io

data = '''
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
'''

df = pd.read_csv(io.StringIO(data), sep=',')

o = pd.Series([], index=[], name='HE_Open', dtype=float)
h = pd.Series([], index=[], name='HE_High', dtype=float)
l = pd.Series([], index=[], name='HE_Low', dtype=float)
df = pd.concat([df, o, h, l],axis=1)
df['HE_Close'] = (df['Open'] + df['High'] + df['Low'] + df['Close']) / 4

for i in range(len(df)):
    if i == 0:
        df.iat[0, 7] = df['Open'].iloc[0]
    else:
        df.iat[i, 7] = (df.iat[i - 1, 1] + df.iat[i - 1, 4]) / 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)

df
    Date    Open    High    Low     Close   Volume  OpenInterest    HE_Open     HE_High     HE_Low  HE_Close
0   2020-08-25 06:00    15.12   15.25   14.8200     15.05   3824776     0.0     15.120  15.25   14.8200     15.060000
1   2020-08-26 06:00    15.09   15.44   15.0200     15.07   5933882     0.0     15.085  15.44   15.0200     15.155000
2   2020-08-27 06:00    15.22   15.33   14.6720     14.84   5728962     0.0     15.080  15.33   14.6720     15.015500
3   2020-08-28 06:00    15.01   15.18   14.8605     15.12   5992532     0.0     15.030  15.18   14.8605     15.042625
4   2020-08-31 06:00    15.23   15.23   14.6200     14.62   7000994     0.0     15.065  15.23   14.6200     14.925000
5   2020-09-01 06:00    14.64   15.18   14.2400     15.08   7347598     0.0     14.925  15.18   14.2400     14.785000
r-beginners
  • 31,170
  • 3
  • 14
  • 32