我有一个烛台对象列表,每个对象都有6个值(打开,高,低,关闭,音量,时间戳)。我想使用matplotlib.finance.candlestick2_ohlc(ax,opens,highs,lows,closes,width = 4,colorup ='k',colordown ='r',alpha = 0.75)函数来绘制此数据。问题是,如何将列表细分为开盘价,最高价,最低价和最低价,以便将其加载到此功能?

这是我的烛台课程:

class Candle:
#Candlestick chart object
def __init__(self, open, high, low, close, volume, timeStamp):
    self.open = open
    self.high = high
    self.low = low
    self.close = close
    self.volume = volume
    self.timestamp = timeStamp

def __str__(self):
    return """
    Open: %s
    High: %s
    Low: %s
    Close: %s
    Volume : %s
    Timestamp: %s""" %(self.open, self.high, self.low, self.close, self.volume, self.timestamp)


这是我的列表构造方法:

def getTradeHistory(self, timeFrame, symbol, count):
    #Get the trade history from the API

    return self.client.Trade.Trade_getBucketed(binSize=timeFrame, partial=True, symbol=symbol, reverse=False, count=count).result()

def constructCandles(self, th):
    #Iterate through list of trade history items and store them as candles in a list

    for candle in th :
        self.candles.append(Candle(candle['open'], candle['high'], candle['low'], candle['close'], candle['volume'], candle['timestamp']))

最佳答案

假设您的烛台对象列表称为my_candles,然后:

opens = [candle.open for candle in my_candles]
highs = [candle.high for candle in my_candles]
lows = [candle.low for candle in my_candles]
closes = [candle.close for candle in my_candles]


现在,您有了打开,关闭,最高和最低的列表,可以调用matplotlib.finance.candlestick2_ohlc

import matplotlib.pyplot as plt
import matplotlib.finance as mpf

fig, ax = plt.subplots(figsize=(8,5))
mpf.candlestick2_ochl(ax, opens, closes, highs, lows, width=4, colorup='k', colordown='r', alpha=0.75)


另请注意,在2.0中不推荐使用matplotlib.finance。

09-25 20:58