Home > Software engineering >  Heiken Ashi candles plotted on graph Binance
Heiken Ashi candles plotted on graph Binance

Time:02-25

So I'm trying to plot Heiken Ashi candles, and then I want to plot them on graph.

My code so far:

def heikin_ashi():
    historical_data = client.get_historical_klines(symbol=SYMBOL, interval=TIME_PERIOD, start_str="15 days ago UTC", klines_type=HistoricalKlinesType.FUTURES)
    hist_df = pd.DataFrame(historical_data)
    hist_df.columns = ['Open Time', 'Open', 'High', 'Low', 'Close', 'Volume', 'Close Time', 'Quote Asset Volume',
                       'Number of Trades', 'TB Base Volume', 'TB Quote Volume', 'Ignore']

    hist_df['Open Time'] = pd.to_datetime(hist_df['Open Time']/1000, unit='s')
    hist_df['Close Time'] = pd.to_datetime(hist_df['Close Time']/1000, unit='s')

    df_HA = hist_df
    df_HA['Close'] = (hist_df['Open']   hist_df['High']   hist_df['Low']   hist_df['Close']) / 4

    # idx = df_HA.index.name
    # df_HA.reset_index(inplace=True)

    for i in range(0, len(hist_df)):
        if i == 0:
            df_HA['Open'][i] = ((hist_df['Open'][i]   hist_df['Close'][i]) / 2)
        else:
            df_HA['Open'][i] = ((hist_df['Open'][i - 1]   hist_df['Close'][i - 1]) / 2)

    # if idx:
    # df_HA.set_index(idx, inplace=True)

    df_HA['High'] = hist_df[['Open', 'Close', 'High']].max(axis=1)
    df_HA['Low'] = hist_df[['Open', 'Close', 'Low']].min(axis=1)

    print(df_HA)

Error:

result[mask] = op(xrav[mask], y)
TypeError: unsupported operand type(s) for /: 'str' and 'int'

Also I came across this:

import pandas as pd

def heikin_ashi(df):
    heikin_ashi_df = pd.DataFrame(index=df.index.values, columns=['open', 'high', 'low', 'close'])
    
    heikin_ashi_df['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['high'] = heikin_ashi_df.loc[:, ['open', 'close']].join(df['high']).max(axis=1)
    
    heikin_ashi_df['low'] = heikin_ashi_df.loc[:, ['open', 'close']].join(df['low']).min(axis=1)
    
    return heikin_ashi_df

How do I use the above code with my data? I'm a novice, so I'm confused. I'd appreciate it if someone could provide me with a proper way to do this.

Link to the source: enter image description here

  • Related