Heiken Ashi 蜡烛图绘制在 Binance 图表上

Heiken Ashi candles plotted on graph Binance

所以我正在尝试绘制 Heiken Ashi 蜡烛图,然后我想将它们绘制在图表上。

到目前为止我的代码:

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)

错误:

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

我也遇到了这个:

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

如何将上述代码用于我的数据?我是新手,所以我很困惑。如果有人能为我提供正确的方法,我将不胜感激。

Link 到来源:https://github.com/emreturan/heikin-ashi/blob/master/heikin_ashi.py

我也需要在图表上绘制它。谢谢

我将使用 'heikin_ashi' 代码来回答使用 mplfinance(一种流行的财务库)绘制图形的示例。还有许多其他库可用于可视化投资,因此我们将使用它作为数据采集和可视化的基本形式。可以找到 mplfinance 的示例 here 以供参考。

import yfinance as yf
import pandas as pd
import mplfinance as mpf

data = yf.download("AAPL", start="2021-07-01", end="2022-01-01", progress=False)
data.columns = ['open', 'high', 'low', 'close', 'adj close', 'volume']

def heikin_ashi(df):
    heikin_ashi_df = df.copy()
    #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

df_ha = heikin_ashi(data)
# mpf plotting
mpf.plot(df_ha, type='candle', figratio=(8,4), title='APPL', style='yahoo')