def initChart(self): self._chart = QChart(title='蜡烛图悬浮提示') self._chart.setAnimationOptions(QChart.SeriesAnimations) series = QCandlestickSeries() series.setIncreasingColor(QColor(Qt.red)) series.setDecreasingColor(QColor(Qt.green)) series.setName(self.stocks['name'].iloc[0]) for _, stock in self.stocks.iterrows(): time_p = datetime.datetime.strptime(stock['trade_date'], '%Y%m%d') time_p = float(time.mktime(time_p.timetuple())) _set = QCandlestickSet(float(stock['open']), float(stock['high']), float(stock['low']), float(stock['close']), time_p, series) _set.hovered.connect(self.handleBarHoverd) # 鼠标悬停 series.append(_set) self._chart.addSeries(series) self._chart.createDefaultAxes() self._chart.setLocalizeNumbers(True) axis_x = self._chart.axisX() axis_y = self._chart.axisY() axis_x.setGridLineVisible(False) axis_y.setGridLineVisible(False) # axis_y.setLabelFormat("%.2f") axis_x.setCategories(self.category) max_p = self.stocks[['high', 'low']].stack().max() + 10 min_p = self.stocks[['high', 'low']].stack().min() - 10 axis_y.setRange(min_p, max_p) # chart的图例 legend = self._chart.legend() # 设置图例由Series来决定样式 legend.setMarkerShape(QLegend.MarkerShapeFromSeries) self.setChart(self._chart)
def addSeries( self, _x2idx: typing.Dict, _idx2x: list, _chart: QChart, _axis_x: QValueAxis, _axis_y: QValueAxis, ): series = QCandlestickSeries() series.setName(self.name) for x, y in zip(self.x_list, self.y_list): series.append(QCandlestickSet(*y, _x2idx[x])) if self.inc_color is not None: series.setIncreasingColor(self.inc_color) else: series.setIncreasingColor(QColor("#c41919")) if self.dec_color is not None: series.setDecreasingColor(self.dec_color) else: series.setDecreasingColor(QColor("#009f9f")) _chart.addSeries(series) _chart.setAxisX(_axis_x, series) _chart.setAxisY(_axis_y, series) if self.show_value: self.createShow()
def plot_candlechart(ohlc_data): app = ParaMakerApplication([]) #app.setStyleSheet("background-color:black;") series = QCandlestickSeries() series.setBodyOutlineVisible(False) series.setDecreasingColor(Qt.red) series.setIncreasingColor(Qt.green) rsi = qc.QLineSeries() # 5-days average data line rsi.append(QPointF(ohlc_data[300].timestamp, ohlc_data[300].closed)) rsi.append(QPointF(ohlc_data[700].timestamp, ohlc_data[700].closed)) #rsi.append(QPointF(ohlc_data[150].timestamp, ohlc_data[100].closed)) tm = [] # stores str type data # in a loop, series and rsi append corresponding data for candle in ohlc_data: series.append( QCandlestickSet(candle.opened, candle.high, candle.low, candle.closed)) #rsi.append(QPointF(num, m)) tm.append(str(candle.timestamp)) #rsi.append(str(candle.timestamp)) #rsi_values = calculate_rsi(14, ohlc_data) chart = QChart() chart.setBackgroundVisible(False) chart.setPlotAreaBackgroundVisible(False) chart.addSeries(series) # candle chart.addSeries(rsi) # rsi line #chart.axisX(rsi).setRange(ohlc_data[0].timestamp, ohlc_data[-1].timestamp) chart.createDefaultAxes() axisXRSI = QValueAxis() axisYRSI = QValueAxis() axisXRSI.setRange(ohlc_data[0].timestamp, ohlc_data[-1].timestamp) axisYRSI.setRange(ohlc_data[0].closed, ohlc_data[-1].closed) axisXRSI.setGridLineVisible(False) axisYRSI.setGridLineVisible(False) chart.setAxisX(axisXRSI, rsi) chart.setAxisY(axisYRSI, rsi) chart.legend().hide() chart.axisX(series).setCategories(tm) #chart.axisX(series).setGridLineVisible(False) #chart.axisY(series).setGridLineVisible(False) ###chart.axisX(rsi).setVisible(False) chartview = QChartView(chart) chartview.setRenderHint(QPainter.Antialiasing) ui = ParaMakerWindow() ui.setCentralWidget(chartview) sys.exit(app.exec_())
class ChartWidget(QWidget): def __init__(self, parent=None, ticker="BTCUSDT"): super().__init__(parent) uic.loadUi("resource/chart.ui", self) self.ticker = ticker self.viewLimit = 10 self.tm = [] self.priceData = QCandlestickSeries() self.priceData.setDecreasingColor(Qt.red) self.priceData.setIncreasingColor(Qt.green) self.priceChart = QChart() self.priceChart.addSeries(self.priceData) self.priceChart.legend().hide() axisX = QDateTimeAxis() axisX.setFormat("hh:mm:ss") axisX.setTickCount(4) dt = QDateTime.currentDateTime() axisX.setRange(dt, dt.addSecs(self.viewLimit)) axisY = QValueAxis() axisY.setVisible(False) self.priceChart.addAxis(axisX, Qt.AlignBottom) self.priceChart.addAxis(axisY, Qt.AlignRight) self.priceData.attachAxis(axisX) self.priceData.attachAxis(axisY) self.priceChart.layout().setContentsMargins(0, 0, 0, 0) self.priceView.setChart(self.priceChart) self.priceView.setRenderHints(QPainter.Antialiasing) self.pw = PriceWorker(ticker) self.pw.dataSent.connect(self.appendData) self.pw.start() def appendData(self, o, h, l, c): if len(self.tm) == self.viewLimit: self.priceData.remove(0) self.tm.remove(0) dt = QDateTime.currentDateTime() self.priceData.append(QCandlestickSet(o, h, l, c)) print(dt.toMSecsSinceEpoch()) print(type(dt.toMSecsSinceEpoch())) self.tm.append(dt.toMSecsSinceEpoch()) self.__updateAxis() def __updateAxis(self): pvs = self.tm dtStart = QDateTime.fromMSecsSinceEpoch(int(pvs[0])) if len(self.priceData) == self.viewLimit: dtLast = QDateTime.fromMSecsSinceEpoch(int(pvs[-1])) else: dtLast = dtStart.addSecs(self.viewLimit) ax = self.priceChart.axisX() ax.setRange(dtStart, dtLast)
def add_data(self, xdata, ydata, color=None): curve = QCandlestickSeries() pen = curve.pen() if color is not None: pen.setColor(color) pen.setWidthF(.1) curve.setPen(pen) curve.setUseOpenGL(True) curve.append(series_to_polyline(xdata, ydata)) self.chart.addSeries(curve) self.chart.createDefaultAxes() self.ncurves += 1
def main(): import sys a = QApplication(sys.argv) acmeSeries = QCandlestickSeries() acmeSeries.setName("Acme Ltd") acmeSeries.setIncreasingColor(QColor(Qt.green)) acmeSeries.setDecreasingColor(QColor(Qt.red)) acmeData = QFile(":acme") if not acmeData.open(QIODevice.ReadOnly | QIODevice.Text): sys.exit(1) categories = [] dataReader = CandlestickDataReader(acmeData) while not dataReader.atEnd(): _set = dataReader.readCandlestickSet() if _set is not None: acmeSeries.append(_set) categories.append( QDateTime.fromMSecsSinceEpoch(int( _set.timestamp())).toString("dd")) chart = QChart() chart.addSeries(acmeSeries) chart.setTitle("Acme Ltd Historical Data (July 2015)") chart.setAnimationOptions(QChart.SeriesAnimations) chart.createDefaultAxes() axisX = chart.axes(Qt.Horizontal)[0] axisX.setCategories(categories) axisY = chart.axes(Qt.Vertical)[0] axisY.setMax(axisY.max() * 1.01) axisY.setMin(axisY.min() * 0.99) chart.legend().setVisible(True) chart.legend().setAlignment(Qt.AlignBottom) chartView = QChartView(chart) chartView.setRenderHint(QPainter.Antialiasing) window = QMainWindow() window.setCentralWidget(chartView) window.resize(800, 600) window.show() sys.exit(a.exec_())
def main (): if len (sys.argv) < 3: print ("файл не задан") sys.exit () with open (sys.argv[1], 'rt') as file: TicksFile = file.read() TicksTuple = tuple(TicksFile.split('\n')[:-1]) Bars = (FormBarFrame(TicksTuple, 60)) with open(sys.argv[1][:-4] + '_' + sys.argv[2] + '.bars', 'wt') as file: for i in reversed(Bars): file.write("{0} {1} {2} {3} {4} {5} {6}\n".format(str(i[0]), i[1], i[2], i[3], i[4], i[5], i[6])) # Вывод графика app = QApplication(sys.argv) series = QCandlestickSeries() series.setDecreasingColor(Qt.red) series.setIncreasingColor(Qt.green) #ma5 = qc.QLineSeries() # 5-days average data line tm = [] # stores str type data # in a loop, series and ma5 append corresponding data #for num, o, h, l, c in Bars: for i in range(len(Bars)): series.append(QCandlestickSet(Bars[i][1], Bars[i][4], Bars[i][3], Bars[i][2])) #ma5.append(QPointF(num, m)) tm.append(str(Bars[i][0])) chart = QChart() chart.addSeries(series) # candle #chart.addSeries(ma5) # ma5 line chart.setAnimationOptions(QChart.SeriesAnimations) chart.createDefaultAxes() chart.legend().hide() chart.axisX(series).setCategories(tm) #chart.axisX(ma5).setVisible(False) chartview = QChartView(chart) ui = QMainWindow() ui.setGeometry(50, 50, 500, 300) ui.setCentralWidget(chartview) ui.show() sys.exit(app.exec_())
class ChartTab(QChartView): def __init__(self): QChartView.__init__(self) self.chart = QChart() self.candleSeries = QCandlestickSeries(self) self.candleSeries.setIncreasingColor(Qt.blue) self.candleSeries.setBodyOutlineVisible(False) self.candleSeries.setDecreasingColor(Qt.red) self.candleSeries.setMaximumColumnWidth(20) self.candleSeries.clicked.connect(lambda set: print(set.high())) self.xAxis = QDateTimeAxis() self.xAxis.setTickCount(10) self.xAxis.setFormat("h:mm") self.yAxis = QValueAxis() for candle in data: self.addCandle( self.get_integer_price(candle[0]), self.get_integer_price(candle[1]), self.get_integer_price(candle[2]), self.get_integer_price(candle[3]), datetime.strptime(candle[4], '%Y%m%d%H%M%S').timestamp() * 1000) self.chart.addSeries(self.candleSeries) self.setChart(self.chart) self.chart.setAxisX(self.xAxis, self.candleSeries) self.chart.setAxisY(self.yAxis, self.candleSeries) self.chart.legend().setVisible(False) def mouseMoveEvent(self, QMouseEvent): chartValues = self.chart.mapToValue(QMouseEvent.pos()) price = chartValues.y() print(price) def addCandle(self, open, close, high, low, timestamp): candlestickSet = QCandlestickSet() candlestickSet.setOpen(open) candlestickSet.setClose(close) candlestickSet.setHigh(high) candlestickSet.setLow(low) candlestickSet.setTimestamp(timestamp) pen = QPen() candlestickSet.setPen(pen) self.candleSeries.append(candlestickSet) def get_integer_price(self, str_price): return int(str_price.strip()[1:])
class Display: def __init__(self): self.data = [] self.app = QApplication(sys.argv) self.series = QCandlestickSeries() self.series.setDecreasingColor(Qt.red) self.series.setIncreasingColor(Qt.green) self.win = QMainWindow() self.tm = [] # stores str type data def load_data(self, _data): self.data = _data # data format [[Open] [High] [Low] [Close] ] for i in range(len(self.data[0])): self.series.append( QCandlestickSet(float(self.data[0][i]), float(self.data[1][i]), float(self.data[2][i]), float(self.data[3][i]))) self.tm.append(str(i)) i = i + 1 def set_window_size(self, width, high, grid_x=20, grid_y=20): self.win.setGeometry(grid_x, grid_y, width, high) def run_app(self): chart = QChart() chart.addSeries(self.series) # candle chart.createDefaultAxes() chart.legend().hide() chart.axisX(self.series).setCategories(self.tm) chart_view = QChartView(chart) self.win.setGeometry(50, 50, 800, 500) self.win.setCentralWidget(chart_view) self.win.show() sys.exit(self.app.exec_())
def addSeries( self, _x2idx: typing.Dict, _idx2x: list, _chart: QChart, _axis_x: QValueAxis, _axis_y: QValueAxis ): series = QCandlestickSeries() series.setName(self.name) for x, y in zip(self.x_list, self.y_list): series.append(QCandlestickSet(*y, _x2idx[x])) if self.inc_color is not None: series.setIncreasingColor(self.inc_color) else: series.setIncreasingColor(QColor('#c41919')) if self.dec_color is not None: series.setDecreasingColor(self.dec_color) else: series.setDecreasingColor(QColor('#009f9f')) _chart.addSeries(series) _chart.setAxisX(_axis_x, series) _chart.setAxisY(_axis_y, series) if self.show_value: self.createShow()
class PriceFigure: def __init__(self, name): self.name = name self.chart_view = QChartView() self.price_time_axis = QDateTimeAxis() self.price_time_axis.setFormat('h:mm') self.price_axis = QValueAxis() self.candle_stick_series = QCandlestickSeries() self.candle_stick_series.setIncreasingColor(Qt.red) self.candle_stick_series.setDecreasingColor(Qt.blue) self.moving_average_series = QLineSeries() self.top_edge_series = QScatterSeries() self.bottom_edge_series = QScatterSeries() self.trend_lines = [] self.short_top_trend_series = QLineSeries() self.short_bottom_trend_series = QLineSeries() self.long_top_trend_series = QLineSeries() self.long_bottom_trend_series = QLineSeries() self.trend_lines.append(self.short_top_trend_series) self.trend_lines.append(self.short_bottom_trend_series) self.trend_lines.append(self.long_top_trend_series) self.trend_lines.append(self.long_bottom_trend_series) self.chart_view.chart().addSeries(self.candle_stick_series) self.chart_view.chart().addSeries(self.moving_average_series) self.chart_view.chart().addSeries(self.top_edge_series) self.chart_view.chart().addSeries(self.bottom_edge_series) self.chart_view.chart().addSeries(self.short_top_trend_series) self.chart_view.chart().addSeries(self.long_top_trend_series) self.chart_view.chart().addSeries(self.short_bottom_trend_series) self.chart_view.chart().addSeries(self.long_bottom_trend_series) self.chart_view.chart().addAxis(self.price_time_axis, Qt.AlignBottom) self.chart_view.chart().addAxis(self.price_axis, Qt.AlignLeft) self.chart_view.chart().legend().hide() self.chart_view.setRenderHint(QPainter.Antialiasing) self.set_marker_color() self.set_trend_line_pen() def set_trend_line_pen(self): brushes = [ QBrush(QColor(255, 0, 0, 90)), QBrush(QColor(0, 0, 255, 90)), QBrush(QColor(205, 56, 47, 255)), QBrush(QColor(0, 153, 213, 255)) ] for i, tl in enumerate(self.trend_lines): tl.setPen(QPen(brushes[i], 4, Qt.DotLine)) def set_marker_color(self): self.top_edge_series.setPen(Qt.black) self.top_edge_series.setBrush(QBrush(QColor(255, 0, 255, 90))) self.bottom_edge_series.setPen(Qt.black) self.bottom_edge_series.setBrush(QBrush(QColor(0, 255, 255, 90))) def set_datetime(self, d): self.chart_datetime = d self.datetime_range = (d.timestamp() * 1000, d.replace(hour=23, minute=59).timestamp() * 1000) start_time = QDateTime() until_time = QDateTime() start_time.setDate(QDate(d.year, d.month, d.day)) until_time.setDate(QDate(d.year, d.month, d.day)) start_time.setTime(QTime(9, 0)) until_time.setTime(QTime(16, 0)) self.price_time_axis.setRange(start_time, until_time) def attach(self): self.price_time_axis.setTickCount(7) self.candle_stick_series.attachAxis(self.price_time_axis) self.candle_stick_series.attachAxis(self.price_axis) self.moving_average_series.attachAxis(self.price_time_axis) self.moving_average_series.attachAxis(self.price_axis) self.top_edge_series.attachAxis(self.price_time_axis) self.top_edge_series.attachAxis(self.price_axis) self.bottom_edge_series.attachAxis(self.price_time_axis) self.bottom_edge_series.attachAxis(self.price_axis) self.short_top_trend_series.attachAxis(self.price_time_axis) self.short_top_trend_series.attachAxis(self.price_axis) self.long_top_trend_series.attachAxis(self.price_time_axis) self.long_top_trend_series.attachAxis(self.price_axis) self.short_bottom_trend_series.attachAxis(self.price_time_axis) self.short_bottom_trend_series.attachAxis(self.price_axis) self.long_bottom_trend_series.attachAxis(self.price_time_axis) self.long_bottom_trend_series.attachAxis(self.price_axis) def in_datetime_range(self, q): return self.datetime_range[0] < q < self.datetime_range[1] def clear_series_data(self): self.candle_stick_series.clear() self.moving_average_series.clear() self.top_edge_series.clear() self.bottom_edge_series.clear() self.short_top_trend_series.clear() self.long_top_trend_series.clear() self.short_bottom_trend_series.clear() self.long_bottom_trend_series.clear() def get_chart_view(self): return self.chart_view def add_moving_average(self, q, price): if self.in_datetime_range(q): self.moving_average_series.append(q, price) def add_candle_stick(self, q, o, h, l, c): if self.in_datetime_range(q): self.candle_stick_series.append(QCandlestickSet(o, h, l, c, q)) def set_price_range(self, price_min, price_max): self.price_axis.setRange(price_min, price_max) tick_count = int( math.ceil((price_max - price_min) / price_min * 100. / 2.0)) self.price_axis.setTickCount(tick_count if tick_count + 1 > 2 else 2) def add_top_edge(self, q, price): if self.in_datetime_range(q): self.top_edge_series.append(q, price) def add_bottom_edge(self, q, price): if self.in_datetime_range(q): self.bottom_edge_series.append(q, price) def add_short_top_trend(self, q, price, draw_horizontal=False): if self.in_datetime_range(q): if draw_horizontal: self.short_top_trend_series.append(q, price) if self.name == 'yesterday': self.short_top_trend_series.append(self.datetime_range[1], price) else: self.short_top_trend_series.append(self.datetime_range[0], price) else: self.short_top_trend_series.append(q, price) def add_long_top_trend(self, q, price, draw_horizontal=False): if self.in_datetime_range(q): if draw_horizontal: self.long_top_trend_series.append(q, price) if self.name == 'yesterday': self.long_top_trend_series.append(self.datetime_range[1], price) else: self.long_top_trend_series.append(self.datetime_range[0], price) else: self.long_top_trend_series.append(q, price) def add_short_bottom_trend(self, q, price, draw_horizontal=False): if self.in_datetime_range(q): if draw_horizontal: self.short_bottom_trend_series.append(q, price) if self.name == 'yesterday': self.short_bottom_trend_series.append( self.datetime_range[1], price) else: self.short_bottom_trend_series.append( self.datetime_range[0], price) else: self.short_bottom_trend_series.append(q, price) def add_long_bottom_trend(self, q, price, draw_horizontal=False): if self.in_datetime_range(q): if draw_horizontal: self.long_bottom_trend_series.append(q, price) if self.name == 'yesterday': self.long_bottom_trend_series.append( self.datetime_range[1], price) else: self.long_bottom_trend_series.append( self.datetime_range[0], price) else: self.long_bottom_trend_series.append(q, price)
class MainClass(QWidget): def __init__(self): super().__init__() self.initUI() def getStockData(self): self.close() print("HI") self.mainLayout.removeWidget(self.newChartView) self.newSeries = QCandlestickSeries() self.newSeries.setName("Test Chart 2") self.saucePage = urllib.request.urlopen( 'https://www.alphavantage.co/query?function=TIME_SERIES_MONTHLY&symbol=' + self.stockRequestBox.text() + '&interval=1min&apikey=NOC1N35PNDQOFF1A') self.output = self.saucePage.read() self.webContent = self.output.decode( 'utf-8') # Convert from bytes to string self.writePage = open( 'stock.json', 'w' ) # Write webContent to a html file because otherwise i can't use readlines() self.writePage.write( self.webContent) # It's very efficient code trust me self.writePage.close() self.sauce = json.load(open('stock.json')) # print(sauce["Monthly Time Series"]["2018-01-14 20:23:00"]["1. open"]) self.openStocks = {} for date in self.sauce["Monthly Time Series"]: self.openStocks[date] = self.sauce["Monthly Time Series"][date][ "1. open"] self.ordered = collections.OrderedDict(sorted(self.openStocks.items())) for k, v in self.ordered.items(): dateString = k dt = datetime.datetime(int(dateString[:4]), int(dateString[5:7]), int(dateString[8:10])) self.newSet = QCandlestickSet((time.mktime(dt.timetuple()) * 1000)) self.newSet.setOpen(float(v)) self.newSet.setHigh( float(self.sauce["Monthly Time Series"][k]["2. high"])) self.newSet.setLow( float(self.sauce["Monthly Time Series"][k]["3. low"])) self.newSet.setClose( float(self.sauce["Monthly Time Series"][k]["4. close"])) self.newSeries.append(self.newSet) self.newChart = QChart() self.newChart.addSeries(self.newSeries) self.newChart.setTitle("Test Actual Chart") self.newChart.createDefaultAxes() self.newChartView = QChartView(self.newChart) self.mainLayout.addWidget(self.newChartView) self.show() def initUI(self): self.mainLayout = QVBoxLayout() self.stockFrame = QFrame() self.mainLayout.addWidget(self.stockFrame) self.mainButton = QPushButton("Push Me!") self.mainButton.clicked.connect(self.getStockData) self.mainLayout.addWidget(self.mainButton) self.stockRequestLayout = QHBoxLayout() self.stockRequestedLabel = QLabel("Stock: ") self.stockRequestLayout.addWidget(self.stockRequestedLabel) self.stockRequestBox = QLineEdit() self.stockRequestLayout.addWidget(self.stockRequestBox) self.mainLayout.addLayout(self.stockRequestLayout) self.setLayout(self.mainLayout) self.newSeries = QCandlestickSeries() self.newSeries.setName("Test Chart") # for i in range(1000): # self.newSet = QCandlestickSet(time.time() * 1000 + (random.randint(-5, 5) * 60000)) # self.newSet.setOpen(random.randint(1, 10)) # self.newSet.setHigh(random.randint(1, 10)) # self.newSet.setLow(random.randint(1, 10)) # self.newSet.setClose(random.randint(1, 10)) # self.newSeries.append(self.newSet) # self.newSet2 = QCandlestickSet() # self.newSet2.setOpen(1.00) # self.newSet2.setHigh(10.00) # self.newSet2.setLow(0.50) # self.newSet2.setClose(5.00) # self.newSeries.append(self.newSet2) self.saucePage = urllib.request.urlopen( 'https://www.alphavantage.co/query?function=TIME_SERIES_MONTHLY&symbol=BHP&apikey=NOC1N35PNDQOFF1A' ) self.output = self.saucePage.read() self.webContent = self.output.decode( 'utf-8') # Convert from bytes to string self.writePage = open( 'stock.json', 'w' ) # Write webContent to a html file because otherwise i can't use readlines() self.writePage.write( self.webContent) # It's very efficient code trust me self.writePage.close() self.sauce = json.load(open('stock.json')) # print(sauce["Monthly Time Series"]["2018-01-14 20:23:00"]["1. open"]) self.openStocks = {} for date in self.sauce["Monthly Time Series"]: self.openStocks[date] = self.sauce["Monthly Time Series"][date][ "1. open"] self.ordered = collections.OrderedDict(sorted(self.openStocks.items())) count = 0 for k, v in self.ordered.items(): dateString = k dt = datetime.datetime(int(dateString[:4]), int(dateString[5:7]), int(dateString[8:10])) self.newSet = QCandlestickSet((time.mktime(dt.timetuple()) * 1000)) self.newSet.setOpen(float(v)) self.newSet.setHigh( float(self.sauce["Monthly Time Series"][k]["2. high"])) self.newSet.setLow( float(self.sauce["Monthly Time Series"][k]["3. low"])) self.newSet.setClose( float(self.sauce["Monthly Time Series"][k]["4. close"])) self.newSeries.append(self.newSet) count = count + 1 # if count > 8: # break self.newChart = QChart() self.newChart.addSeries(self.newSeries) self.newChart.setTitle("Test Actual Chart") self.newChart.createDefaultAxes() self.newChartView = QChartView(self.newChart) self.mainLayout.addWidget(self.newChartView) # self.subjectLabel = QLabel() # self.subjectLabel.setAlignment(Qt.Qt.AlignCenter) # self.mainLayout.addWidget(self.subjectLabel) # self.testLabel = QLabel() # self.testLabel.setAlignment(Qt.Qt.AlignCenter) # self.mainLayout.addWidget(self.testLabel) # self.randomButton = QPushButton("Choose") # self.randomButton.clicked.connect(self.randomDef) # self.mainLayout.addWidget(self.randomButton) # self.setLayout(self.mainLayout) self.setGeometry(300, 300, 300, 300) self.setWindowTitle("Stocks") self.show()
acmeSeries.setIncreasingColor(QColor("light green")) acmeSeries.setDecreasingColor(QColor("red")) # [1] # [2] load the data acmeData = QFile("acme_data.txt") if not acmeData.open(QIODevice.ReadOnly | QIODevice.Text): sys.exit(1) categories = [] dataReader = CandlestickDataReader(acmeData) while not dataReader.atEnd(): line = dataReader.read_candlestick_set() if line is not None: acmeSeries.append(line) categories.append( QDateTime.fromMSecsSinceEpoch(line.timestamp()).toString("dd")) # [2] # [3] create the chart chart = QChart() chart.addSeries(acmeSeries) chart.setTitle("Acme Ltd Historical Data (July 2015)") chart.setAnimationOptions(QChart.SeriesAnimations) # [3] # [4] set the axes properties chart.createDefaultAxes() axisX = chart.axes(Qt.Horizontal)[0]
class CTViewPair(QWidget): def __init__(self, CTMain=None, exchange=None, base_curr=None, curr_curr=None, chart_lookback=None, chart_interval=None, order_book_depth=None): super().__init__() self._CTMain = CTMain self._exchange = exchange self._base_curr = base_curr self._curr_curr = curr_curr self._chart_lookback = chart_lookback self._chart_interval = chart_interval self._order_book_depth = order_book_depth if self._exchange is None: self._exchange = CTMain._settings['Initial Market View Exchange'] if self._base_curr is None: self._base_curr = CTMain._settings[ 'Initial Market View Base Currency'] if self._curr_curr is None: self._curr_curr = CTMain._settings[ 'Initial Market View Quote Currency'] if self._chart_lookback is None: self._chart_lookback = CTMain._settings[ 'Initial Market View Chart Lookback'] if self._chart_interval is None: self._chart_interval = CTMain._settings[ 'Initial Market View Chart Interval'] if self._order_book_depth is None: self._order_book_depth = CTMain._settings[ 'Default Order Book Depth'] if 'Fusion' in QStyleFactory.keys(): self.change_style('Fusion') self._layout = QGridLayout() self._order_book_widget = CTOrderBook(self._CTMain, None, None, None, None, self._order_book_depth) self.CandlestickSeries = QCandlestickSeries() self.CandlestickSeries.setIncreasingColor(Qt.green) self.CandlestickSeries.setDecreasingColor(Qt.red) self.VolumeBarSeries = QCandlestickSeries() self.VolumeBarSeries.setBodyWidth(1.0) self.VolumeBarSeries.setIncreasingColor(Qt.green) self.VolumeBarSeries.setDecreasingColor(Qt.red) # self._chart_view = CTChartView(self) # self._chart_view_volume = QChartView(self) # self._chart_view_volume.chart().legend().setVisible(False) exchanges = self._CTMain._Crypto_Trader.trader.keys() self._dropdown_exchange = Dropdown(exchanges, self._exchange) self._dropdown_exchange.activated[str].connect( self.refresh_dropdown_exchange_change) base_codes = sorted(self._CTMain._Crypto_Trader.trader[ self._exchange]._active_markets.keys()) self._dropdown_base_curr = Dropdown(base_codes, self._base_curr) self._dropdown_base_curr.activated[str].connect( self.refresh_dropdown_base_change) curr_codes = sorted(self._CTMain._Crypto_Trader.trader[ self._exchange]._active_markets[self._base_curr].keys()) self._dropdown_curr_curr = Dropdown(curr_codes, self._curr_curr) self._dropdown_curr_curr.activated[str].connect( self.refresh_dropdown_curr_change) # label_lookback = QLabel("Lookback:") # self._chart_dropdown_lookback = Dropdown( # list(self._CTMain._settings['Chart Lookback Window']), # self._chart_lookback # ) # self._chart_dropdown_lookback.currentTextChanged.connect(self.draw_chart) # label_interval = QLabel("Interval:") # self._chart_dropdown_interval = Dropdown( # list(self._CTMain._settings['Chart Interval']), # self._chart_interval # ) # self._chart_dropdown_interval.currentTextChanged.connect(self.draw_chart) self._market_symbol = self._CTMain._Crypto_Trader.get_market_symbol( self._exchange, self._base_curr, self._curr_curr) self._trade_widget = CTTradeWidget(self._CTMain, self._exchange, self._base_curr, self._curr_curr, self._market_symbol) self._open_orders_widget = CTOpenOrdersWidget(self._CTMain, self._exchange, self._market_symbol) self._recent_trades_widget = CTRecentTradesWidget( self._CTMain, self._exchange, self._base_curr, self._curr_curr, self._market_symbol) self.refresh_dropdown_exchange_change(self._exchange, self._base_curr, self._curr_curr) label_exchange = QLabel("&Exchange:") label_exchange.setBuddy(self._dropdown_exchange) label_base_curr = QLabel("&Base:") label_base_curr.setBuddy(self._dropdown_base_curr) label_curr_curr = QLabel("&Currency:") label_curr_curr.setBuddy(self._dropdown_curr_curr) self._debug_button = QPushButton() self._debug_button.setText("Debug") self._debug_button.clicked.connect(self.debug) top_layout = QHBoxLayout() top_layout.addWidget(label_exchange) top_layout.addWidget(self._dropdown_exchange) top_layout.addWidget(label_base_curr) top_layout.addWidget(self._dropdown_base_curr) top_layout.addWidget(label_curr_curr) top_layout.addWidget(self._dropdown_curr_curr) # top_layout.addWidget(label_lookback) # top_layout.addWidget(self._chart_dropdown_lookback) # top_layout.addWidget(label_interval) # top_layout.addWidget(self._chart_dropdown_interval) top_layout.addWidget(self._debug_button) top_layout.addStretch(1) self._layout.addLayout(top_layout, 0, 0, 1, 10) self._splitter_top = QSplitter(Qt.Horizontal) self._splitter_left = QSplitter(Qt.Vertical) self._splitter_left.addWidget(self._order_book_widget) self._splitter_left.addWidget(self._recent_trades_widget) self._splitter_left.addWidget(self._trade_widget) self._splitter_left.setSizes([500, 100, 100]) self._splitter_right = QSplitter(Qt.Vertical) # self._splitter_right.addWidget(self._chart_view) # self._splitter_right.addWidget(self._chart_view_volume) self._splitter_right.addWidget(self._open_orders_widget) self._splitter_right.setSizes([500, 100, 100]) self._splitter_top.addWidget(self._splitter_left) self._splitter_top.addWidget(self._splitter_right) window_width = self._CTMain.frameGeometry().width() self._splitter_top.setSizes( [round(0.3 * window_width), round(0.7 * window_width)]) self._layout.addWidget(self._splitter_top, 1, 0, 9, 10) self.setLayout(self._layout) self.show() def refresh_dropdown_exchange_change(self, exchange, default_base=None, default_curr=None): self._exchange = exchange if default_base is None: default_base = self._dropdown_base_curr.currentText if default_curr is None: default_curr = self._dropdown_curr_curr.currentText base_codes = sorted( list(self._CTMain._Crypto_Trader.trader[exchange]._active_markets)) self._dropdown_base_curr.clear() self._dropdown_base_curr.addItems(base_codes) if default_base not in base_codes: default_base = base_codes[0] self._dropdown_base_curr.setCurrentText(default_base) self.refresh_dropdown_base_change(default_base, default_curr) def refresh_dropdown_base_change(self, base_curr, default_curr=None): self._base_curr = base_curr if default_curr is None: default_curr = self._dropdown_curr_curr.currentText curr_codes = sorted( list(self._CTMain._Crypto_Trader.trader[ self._exchange]._active_markets[base_curr])) self._dropdown_curr_curr.clear() self._dropdown_curr_curr.addItems(curr_codes) if default_curr not in curr_codes: default_curr = curr_codes[0] self._dropdown_curr_curr.setCurrentText(default_curr) self.refresh_dropdown_curr_change(default_curr) def refresh_dropdown_curr_change(self, curr_curr): self._curr_curr = curr_curr self._market_symbol = self._CTMain._Crypto_Trader.get_market_symbol( self._exchange, self._base_curr, curr_curr) self.initiate_widgets() @staticmethod def change_style(style_name): QApplication.setStyle(QStyleFactory.create(style_name)) @staticmethod def debug(): import ipdb ipdb.set_trace() # def refresh(self): # t = threading.Thread(target = self.refresh_widgets) # t.start() # t.join(1) def initiate_widgets(self): self._order_book_widget.refresh_order_book(self._exchange, self._market_symbol, self._base_curr, self._curr_curr) self._trade_widget.update_currencies(self._exchange, self._base_curr, self._curr_curr, self._market_symbol) self._open_orders_widget.update_market(self._exchange, self._market_symbol) self._recent_trades_widget.update_market(self._exchange, self._base_curr, self._curr_curr, self._market_symbol) # self.draw_chart() def draw_chart(self): exchange = self._exchange market_symbol = self._market_symbol interval_name = self._chart_dropdown_interval.currentText() lookback_name = self._chart_dropdown_lookback.currentText() interval = self._CTMain._settings['Chart Interval'][interval_name] lookback = self._CTMain._settings['Chart Lookback Window'][ lookback_name] load_chart = self._CTMain._Crypto_Trader.trader[ exchange].load_chart_data(market_symbol, interval, lookback) self.CandlestickSeries.clear() self.VolumeBarSeries.clear() ch_min = load_chart[0][3] ch_max = load_chart[0][2] t_min = load_chart[0][0] t_max = load_chart[0][0] v_max = load_chart[0][6] for point in load_chart: candle = CTCandlestickSet(timestamp=point[0] * 1000, c_open=point[1], c_high=point[2], c_low=point[3], c_close=point[4], volume=point[5], base_volume=point[6], base_curr=self._base_curr, curr_curr=self._curr_curr, parent=self) self.CandlestickSeries.append(candle) ch_min = min(ch_min, point[3]) ch_max = max(ch_max, point[2]) t_min = min(t_min, point[0]) t_max = max(t_max, point[0]) v_max = max(v_max, point[6]) min_y = max(0, ch_min - 0.15 * (ch_max - ch_min)) max_y = ch_max + 0.1 * (ch_max - ch_min) max_volume = 0 for point in load_chart: # high = min_y + 0.1 * (max_y - min_y) * point[6] / v_max # low = min_y v_low = 0 v_high = point[6] max_volume = max(max_volume, point[6]) if point[4] >= point[1]: v_open = v_low v_close = v_high else: v_open = v_high v_close = v_low volume_candle = QCandlestickSet(v_open, v_high, v_low, v_close, point[0] * 1000, self) self.VolumeBarSeries.append(volume_candle) if not self._chart_view._chart_loaded: self._chart_view.chart.addSeries(self.CandlestickSeries) self._chart_view_volume.chart().addSeries(self.VolumeBarSeries) self._chart_view._chart_loaded = True axis_x = QDateTimeAxis() axis_x.setFormat("dd-MM-yyyy h:mm") axis_x.setRange(datetime.fromtimestamp(int(t_min) - 30 * interval), datetime.fromtimestamp(int(t_max) + 30 * interval)) self._chart_view.chart.setAxisX(axis_x, self.CandlestickSeries) axis_y = QValueAxis() axis_y.setRange(min_y, max_y) self._chart_view.chart.setAxisY(axis_y, self.CandlestickSeries) axis_x2 = QDateTimeAxis() axis_x2.setFormat("dd-MM-yyyy h:mm") axis_x2.setRange(datetime.fromtimestamp(int(t_min) - 30 * interval), datetime.fromtimestamp(int(t_max) + 30 * interval)) self._chart_view_volume.chart().setAxisX(axis_x2, self.VolumeBarSeries) axis_y2 = QValueAxis() axis_y2.setRange(0, max_volume) self._chart_view_volume.chart().setAxisY(axis_y2, self.VolumeBarSeries)
class KLineChartView(QChartView): # QCandlestickSeries的hovered的信号响应后传递日期出去 candles_hovered = pyqtSignal(bool, str) def __init__(self, data: pd.DataFrame): super(KLineChartView, self).__init__() self.setRenderHint(QPainter.Antialiasing) # 抗锯齿 self._chart = QChart() self._series = QCandlestickSeries() self._stocks = data self._category = list() self._count = None self.init_chart() self._zero_value = (0, self._chart.axisY().min()) self._max_value = (len(self._chart.axisX().categories()), self._chart.axisY().max()) self._zero_point = self._chart.mapToPosition( QPointF(self._zero_value[0], self._zero_value[1])) self._max_point = self._chart.mapToPosition( QPointF(self._max_value[0], self._max_value[1])) # 计算x轴单个cate的宽度,用来处理横线不能画到边界 self._cate_width = (self._max_point.x() - self._zero_point.x()) / len( self._category) self._series.hovered.connect(self.on_series_hovered) def on_series_hovered(self, status, candles_set): trade_date = time.strftime('%Y%m%d', time.localtime(candles_set.timestamp())) self.candles_hovered.emit(status, trade_date) def set_name(self, name): self._series.setName(name) def clear_series_values(self): self._series.clear() self._chart.axisY().setRange(0, 10) self._chart.axisX().setCategories(list()) self._stocks = None def add_series_values(self, data: pd.DataFrame, is_init=False): self._stocks = data self._category = self._stocks['trade_date'] self._count = len(self._category) for _, stock in self._stocks.iterrows(): time_p = datetime.datetime.strptime(stock['trade_date'], '%Y%m%d') time_p = float(time.mktime(time_p.timetuple())) _set = QCandlestickSet(float(stock['open']), float(stock['high']), float(stock['low']), float(stock['close']), time_p, self._series) self._series.append(_set) if not is_init: self._stocks = data self._category = self._stocks['trade_date'] axis_x = self._chart.axisX() axis_y = self._chart.axisY() axis_x.setCategories(self._category) max_p = self._stocks[['high', 'low']].stack().max() min_p = self._stocks[['high', 'low']].stack().min() axis_y.setRange(min_p * 0.99, max_p * 1.01) self._zero_value = (0, self._chart.axisY().min()) self._max_value = (len(self._chart.axisX().categories()), self._chart.axisY().max()) # 计算x轴单个cate的宽度,用来处理横线不能画到边界 self._cate_width = (self._max_point.x() - self._zero_point.x()) / len(self._category) def resizeEvent(self, event): super(KLineChartView, self).resizeEvent(event) self._zero_point = self._chart.mapToPosition( QPointF(self._zero_value[0], self._zero_value[1])) self._max_point = self._chart.mapToPosition( QPointF(self._max_value[0], self._max_value[1])) self._cate_width = (self._max_point.x() - self._zero_point.x()) / len( self._category) def max_point(self): return QPointF(self._max_point.x() + self._cate_width / 2, self._max_point.y()) def min_point(self): return QPointF(self._zero_point.x() - self._cate_width / 2, self._zero_point.y()) def init_chart(self): self._chart.setAnimationOptions(QChart.SeriesAnimations) self._series.setIncreasingColor(QColor(Qt.red)) self._series.setDecreasingColor(QColor(Qt.green)) self._series.setName(self._stocks['name'].iloc[0]) self.add_series_values(self._stocks, True) self._chart.addSeries(self._series) self._chart.createDefaultAxes() self._chart.setLocalizeNumbers(True) axis_x = self._chart.axisX() axis_y = self._chart.axisY() axis_x.setGridLineVisible(False) axis_y.setGridLineVisible(False) axis_x.setCategories(self._category) axis_x.setLabelsVisible(False) axis_x.setVisible(False) max_p = self._stocks[['high', 'low']].stack().max() min_p = self._stocks[['high', 'low']].stack().min() axis_y.setRange(min_p * 0.99, max_p * 1.01) # chart的图例 legend = self._chart.legend() # 设置图例由Series来决定样式 legend.setMarkerShape(QLegend.MarkerShapeFromSeries) self.setChart(self._chart) # 设置外边界全部为0 self._chart.layout().setContentsMargins(0, 0, 0, 0) # 设置内边界的bottom为0 margins = self._chart.margins() self._chart.setMargins(QMargins(margins.left(), 0, margins.right(), 0)) # 设置背景区域无圆角 self._chart.setBackgroundRoundness(0)
class MainWindow(QMainWindow): def __init__(self, data, parent=None): super().__init__(parent) self.data = data self.centralwidget = QWidget(self) self.centralwidget.setObjectName("centralwidget") self.label = QLabel(self.centralwidget) self.label.setGeometry(QRect(0, 0, 261, 31)) font = QtGui.QFont() font.setFamily("Consolas") font.setPointSize(18) self.label.setFont(font) self.label.setTextFormat(Qt.AutoText) self.label.setObjectName("label") self.series = QCandlestickSeries() self.series.setDecreasingColor(Qt.red) self.series.setIncreasingColor(Qt.green) self.ma5 = qc.QLineSeries() self.tm = [] self.chart = QChart() self.chart.addSeries(self.series) # candle self.chart.addSeries(self.ma5) # ma5 line self.chart.createDefaultAxes() self.chart.legend().hide() self.chart.axisX(self.series).setCategories(self.tm) self.chart.axisX(self.ma5).setVisible(False) self.chartview = QChartView(self.chart) self.setGeometry(50, 50, 500, 300) self.setCentralWidget(self.chartview) def append_data_and_plot(self, d): """Append and update the plot""" num, o, h, l, c, m = d ax1 = self.chart.axisX(self.ma5) ay1 = self.chart.axisY(self.ma5) xmin = xmax = num ymin = ymax = m step = 10 offset = 100 for p in self.ma5.pointsVector()[-step:]: xmin = min(p.x(), xmin) xmax = max(p.x(), xmax) ymin = min(p.y(), ymin) - offset ymax = max(p.y(), ymax) + offset xmin = max(0, xmax - step) ax1.setMin(xmin) ax1.setMax(xmax) ay1.setMin(ymin) ay1.setMax(ymax) self.ma5.append(QPointF(num, m)) self.tm.append(str(num)) self.series.append(QCandlestickSet(o, h, l, c)) ax2 = self.chart.axisX(self.series) ax2.setCategories(self.tm) ax2.setMin(str(xmin)) ax2.setMax(str(xmax)) ay2 = self.chart.axisY(self.series) ay2.setMin(ymin) ay2.setMax(ymax)
class VolumeFigure: def __init__(self, name): self.chart_volume_view = QChartView() self.chart_volume_view.chart().setLocalizeNumbers(True) self.volume_series = QCandlestickSeries() self.volume_time_axis = QDateTimeAxis() self.volume_time_axis.setFormat('h:mm') self.volume_axis = QValueAxis() self.volume_average_series = QLineSeries() self.chart_volume_view.chart().addSeries(self.volume_series) self.chart_volume_view.chart().addSeries(self.volume_average_series) self.chart_volume_view.chart().addAxis(self.volume_time_axis, Qt.AlignBottom) self.chart_volume_view.chart().addAxis(self.volume_axis, Qt.AlignLeft) self.chart_volume_view.chart().legend().hide() self.chart_volume_view.setRenderHint(QPainter.Antialiasing) def in_datetime_range(self, q): return self.datetime_range[0] < q < self.datetime_range[1] def get_chart_view(self): return self.chart_volume_view def clear_series_data(self): self.volume_series.clear() self.volume_average_series.clear() def set_volume_average(self, vol): self.volume_average_series.append(self.datetime_range[0], vol) self.volume_average_series.append(self.datetime_range[1], vol) def set_datetime(self, d): self.chart_datetime = d self.datetime_range = (d.timestamp() * 1000, d.replace(hour=23, minute=59).timestamp() * 1000) start_time = QDateTime() until_time = QDateTime() start_time.setDate(QDate(d.year, d.month, d.day)) until_time.setDate(QDate(d.year, d.month, d.day)) start_time.setTime(QTime(9, 0)) until_time.setTime(QTime(16, 0)) self.volume_time_axis.setRange(start_time, until_time) def add_volume_data(self, q, volume): if self.in_datetime_range(q): self.volume_series.append(QCandlestickSet(0, volume, 0, volume, q)) def attach(self): self.volume_time_axis.setTickCount(7) self.volume_average_series.attachAxis(self.volume_axis) self.volume_average_series.attachAxis(self.volume_time_axis) self.volume_series.attachAxis(self.volume_axis) self.volume_series.attachAxis(self.volume_time_axis) def set_max(self, vol): self.volume_axis.setRange(0, vol)
def __drawChart(self): ##绘制图表 self.chart.removeAllSeries() #删除所有序列 self.chart.setTitle("股票日线图--" + self.ui.tabWidget.tabText(0)) ## 1. 创建蜡烛图 seriesCandle = QCandlestickSeries() seriesCandle.setName("蜡烛图") seriesCandle.setIncreasingColor(Qt.red) #暴涨 seriesCandle.setDecreasingColor(Qt.darkGreen) #暴跌 visible = self.ui.chkBox_Outline.isChecked() seriesCandle.setBodyOutlineVisible(visible) seriesCandle.setCapsVisible(self.ui.chkBox_Caps.isChecked()) self.chart.addSeries(seriesCandle) seriesCandle.attachAxis(self.__axisX) seriesCandle.attachAxis(self.__axisY) seriesCandle.clicked.connect(self.do_candleClicked) seriesCandle.hovered.connect(self.do_candleHovered) ## 2. 创建MA曲线 pen = QPen() pen.setWidth(2) seriesMA1 = QLineSeries() #不能使用QSplineSeries seriesMA1.setName("MA5") pen.setColor(Qt.magenta) seriesMA1.setPen(pen) self.chart.addSeries(seriesMA1) seriesMA1.attachAxis(self.__axisX) seriesMA1.attachAxis(self.__axisY) seriesMA2 = QLineSeries() seriesMA2.setName("MA10") pen.setColor(Qt.yellow) seriesMA2.setPen(pen) self.chart.addSeries(seriesMA2) seriesMA2.attachAxis(self.__axisX) seriesMA2.attachAxis(self.__axisY) seriesMA3 = QLineSeries() seriesMA3.setName("MA20") pen.setColor(Qt.cyan) seriesMA3.setPen(pen) self.chart.addSeries(seriesMA3) seriesMA3.attachAxis(self.__axisX) seriesMA3.attachAxis(self.__axisY) seriesMA4 = QLineSeries() seriesMA4.setName("MA60") pen.setColor(Qt.green) #green seriesMA4.setPen(pen) self.chart.addSeries(seriesMA4) seriesMA4.attachAxis(self.__axisX) seriesMA4.attachAxis(self.__axisY) ## 3. 填充数据到序列 dataRowCount = self.itemModel.rowCount() #数据点个数 for i in range(dataRowCount): dateStr = self.itemModel.item(i, 0).text() #日期字符串,如"2017/02/03" dateValue = QDate.fromString(dateStr, "yyyy/MM/dd") #QDate dtValue = QDateTime(dateValue) #日期时间 QDateTime timeStamp = dtValue.toMSecsSinceEpoch() #毫秒数 oneCandle = QCandlestickSet() #QCandlestickSet oneCandle.setOpen(float(self.itemModel.item(i, 1).text())) #开盘 oneCandle.setHigh(float(self.itemModel.item(i, 2).text())) #最高 oneCandle.setLow(float(self.itemModel.item(i, 3).text())) #最低 oneCandle.setClose(float(self.itemModel.item(i, 4).text())) #收盘 oneCandle.setTimestamp(timeStamp) #时间戳 seriesCandle.append(oneCandle) #添加到序列 M1 = float(self.itemModel.item(i, 5).text()) M2 = float(self.itemModel.item(i, 6).text()) M3 = float(self.itemModel.item(i, 7).text()) M4 = float(self.itemModel.item(i, 8).text()) seriesMA1.append(timeStamp, M1) seriesMA2.append(timeStamp, M2) seriesMA3.append(timeStamp, M3) seriesMA4.append(timeStamp, M4) ## 4. 设置坐标轴范围 minDateStr = self.itemModel.item(0, 0).text() #日期字符串,如"2017/02/03" minDate = QDate.fromString(minDateStr, "yyyy/MM/dd") #QDate minDateTime = QDateTime(minDate) #最小日期时间,QDateTime maxDateStr = self.itemModel.item(dataRowCount - 1, 0).text() #日期字符串,如"2017/05/03" maxDate = QDate.fromString(maxDateStr, "yyyy/MM/dd") maxDateTime = QDateTime(maxDate) #最大日期时间 self.__axisX.setRange(minDateTime, maxDateTime) #日期时间范围 dateFormat = self.ui.comboDateFormat.currentText() #格式,如"MM-dd" self.__axisX.setFormat(dateFormat) #标签格式 self.__axisY.applyNiceNumbers() #自动 for marker in self.chart.legend().markers(): #QLegendMarker类型列表 marker.clicked.connect(self.do_LegendMarkerClicked)
(6, 7500, 7590, 7480, 7560, 7512), (7, 7560, 7830, 7540, 7800, 7584), ) app = QApplication(sys.argv) # series = QCandlestickSeries() series.setDecreasingColor(Qt.red) series.setIncreasingColor(Qt.green) ma5 = qc.QLineSeries() # 5-days average data line tm = [] # stores str type data # in a loop, series and ma5 append corresponding data for num, o, h, l, c, m in data: series.append(QCandlestickSet(o, h, l, c)) ma5.append(QPointF(num, m)) tm.append(str(num)) chart = QChart() chart.addSeries(series) # candle chart.addSeries(ma5) # ma5 line chart.setAnimationOptions(QChart.SeriesAnimations) chart.createDefaultAxes() chart.legend().hide() chart.axisX(series).setCategories(tm) chart.axisX(ma5).setVisible(False)