1

在此处输入图像描述

从上面的折线图可以看出,它只是 matplotlib.show 的 x 轴从最旧到最新,但 mplfinance ohlc 从最新到最旧。我相信这是因为索引从最新到最旧。

那么如何反转 mplfinance 的索引呢?

我正在为 ohlc 图表使用 pandas 数据框。

这是代码:

def runCandles(user_symbol):
    openarr = []
    closearr = []
    higharr = []
    lowarr = []
    datesarr = []

    ha_close_arr = []
    ha_open_arr = []
    ha_high_arr = []
    ha_low_arr = []
    
    symbol = user_symbol
    response = requests.get("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol="+symbol+"&apikey=")
    responsedata = response.json()
    
    #pass response json as data
    def get_ohlc(data):
        for value in data['Time Series (Daily)']:
            date = datetime.strptime(value,'%Y-%m-%d')
            open = float(responsedata['Time Series (Daily)'][value]['1. open'])
            close = float(responsedata['Time Series (Daily)'][value]['4. close'])
            high = float(responsedata['Time Series (Daily)'][value]['2. high'])
            low = float(responsedata['Time Series (Daily)'][value]['3. low'])
            openarr.append(open)
            closearr.append(close)
            lowarr.append(low)
            higharr.append(high)
            datesarr.append(date)

    def get_ha_ohlc():
        if (len(openarr) == len(closearr) == len(lowarr) == len(higharr) == len(datesarr)):
            length = len(openarr)
            count = 0
        while count < length:
            ha_close = 0.25*(openarr[count]+closearr[count]+lowarr[count]+higharr[count])
            ha_close_arr.append(ha_close)
            
            ha_open = 0.5*(openarr[count-1]+closearr[count-1])
            ha_open_arr.append(ha_open)
            
            ha_high = max(openarr[count],closearr[count],higharr[count])
            ha_high_arr.append(ha_high)
            
            ha_low = min(openarr[count],closearr[count],lowarr[count])
            ha_low_arr.append(ha_low)
            
            count += 1
    
    get_ohlc(responsedata)
    get_ha_ohlc()
            
    reformatted_data = dict()
    reformatted_data["Date"] = []
    reformatted_data["Open"] = []
    reformatted_data["High"] = []
    reformatted_data["Close"] = []
    reformatted_data["Low"] = []

    reformatted_data["Date"] = datesarr
    reformatted_data["Open"] = ha_open_arr
    reformatted_data["High"] = ha_high_arr
    reformatted_data["Close"] = ha_close_arr
    reformatted_data["Low"] = ha_low_arr

    df = pd.DataFrame.from_dict(reformatted_data)
    print(df)
    df.set_index('Date', inplace=True)
#     df.reindex(index=df.index[::-1])
    print(df)
#     df.head(3)
    plt.plot(datesarr, ha_close_arr)
    plt.show()
    
    mpf.plot(df)
    
#     print(datesarr)
#     print(list(reversed(datesarr)))

这是数据:

Date,Open,High,Close,Low
2020-09-23,19.755000000000003,22.219,21.089775000000003,20.1901
2020-09-22,20.975,22.06,21.6096,21.1884
2020-09-21,21.595,22.09,21.53,21.11
2020-09-18,21.46,23.04,22.549999999999997,22.18
2020-09-17,22.490000000000002,23.54,22.457499999999996,21.72
2020-09-16,22.285,23.54,23.0175,22.66
2020-09-15,22.935,24.6,23.839999999999996,23.25
2020-09-14,23.755,24.15,23.428325,22.9233
2020-09-11,23.32,23.49,22.98,22.37
2020-09-10,23.03,24.49,23.609999999999996,23.07
2020-09-09,23.439999999999998,24.97,23.87,23.25
2020-09-08,23.630000000000003,24.77,23.27,22.55
2020-09-04,22.88,27.0,25.235,22.56
2020-09-03,25.689999999999998,29.15,27.3675,25.8
2020-09-02,27.259999999999998,34.4234,32.57085,30.61
2020-09-01,32.625,31.0,29.4275,27.8
2020-08-31,29.455,29.639,28.42975,26.75
2020-08-28,28.665,28.98,28.0192,27.1768
2020-08-27,27.96,28.97,27.52,26.7
2020-08-26,27.205,28.48,27.497500000000002,26.51
2020-08-25,27.5,29.42,28.61,27.11
2020-08-24,28.955,29.96,28.93,27.85
2020-08-21,28.955,27.2,25.560000000000002,24.42
2020-08-20,25.310000000000002,24.45,23.235,22.2
2020-08-19,23.145,21.66,20.785,20.0
2020-08-18,20.74,20.8,19.915000000000003,19.1
2020-08-17,19.880000000000003,19.43,19.167499999999997,18.81
2020-08-14,19.215,20.49,19.6075,18.68
2020-08-13,19.630000000000003,19.6,18.9625,18.31
2020-08-12,18.97,21.19,20.2725,19.25
4

1 回答 1

1

只需在将 Pandas DataFrame 传递给mpf.plot(df)...之前反转它

df = df.iloc[::-1]
mpf.plot(df)

那应该这样做。Mplfinance 期望数据帧数据和日期时间索引从最旧(行号0)到最新(行号len(df)-1)排序

于 2020-09-24T15:29:30.550 回答