A
A
Alexander2020-04-05 10:45:23
Python
Alexander, 2020-04-05 10:45:23

PyQtChart how to make automatic height scaling?

How to make automatic scaling in height if a new candlestick is above/below the current range?
And also, how to make it so that there are a maximum of 20 candles on the screen, and they simply move to the left when a new one appears, and do not shrink

import sys
from time import sleep
from PyQt5.QtChart import QCandlestickSeries, QChart, QChartView, QCandlestickSet
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import Qt, QPointF
from PyQt5 import QtChart as qc
from PyQt5 import QtCore, QtWidgets
import random

class MyThread(QtCore.QThread):
  mysignal = QtCore.pyqtSignal(str)
  def __init__(self, parent=None):
    QtCore.QThread.__init__(self, parent)

  def run(self):
    for i in range(1, 100):
      sleep(0.05)
      #Передача данных из потока через сигнал
      self.mysignal.emit(str(i))

def addBar(s):
  barOpen = random.randint(100, 200)
  barLow = barOpen - random.randint(50, 100)
  barHigh = barOpen + random.randint(50, 100)
  barClose = barOpen - random.randint(-50, 50)
  barMA = barOpen - random.randint(-50, 50)

  series.append(QCandlestickSet(barOpen, barHigh, barLow, barClose))
  ma5.append(QPointF(int(s), barMA))
  tm.append(str(s))
  chart.axisX(series).setCategories(tm)




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

barOpen = random.randint(100, 200)
barLow = barOpen - random.randint(50, 100)
barHigh = barOpen + random.randint(50, 100)
barClose = barOpen - random.randint(-50, 50)
barMA = barOpen - random.randint(-50, 50)

series.append(QCandlestickSet(barOpen, barHigh, barLow, barClose))
ma5.append(QPointF(int(0), barMA))
tm.append(str(0))

chart = QChart()

chart.addSeries(series)  # candle
chart.addSeries(ma5)  # ma5 line


chart.createDefaultAxes()
chart.legend().hide()

chart.axisX(series).setCategories(tm)
chart.axisX(ma5).setVisible(True)
chart.axisX().setLabelsAngle(-90)
chartview = QChartView(chart)
ui = QMainWindow()
mythread = MyThread()
mythread.start()
mythread.mysignal.connect(addBar, QtCore.Qt.QueuedConnection)

ui.setGeometry(50, 50, 500, 300)
ui.setCentralWidget(chartview)
ui.show()
sys.exit(app.exec_())

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question