gpt4 book ai didi

python - 使用 matplotlib 在 python 中绘制 OHLC 图表

转载 作者:行者123 更新时间:2023-11-30 22:16:21 35 4
gpt4 key购买 nike

我有一个烛台对象列表,每个烛台对象都有 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)

另请注意,matplotlib.finance 在 2.0 中已弃用。

关于python - 使用 matplotlib 在 python 中绘制 OHLC 图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49971911/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com