FinancialChart2D Example

import sys, os
sys.path.append(os.path.dirname(os.path.realpath(__file__)) + "/../shared")

from DevMachines import __pyside2__, __pyside6__
from DevMachines.QtitanBase import Qtitan
from DevMachines.QtitanChart import (Chart, ChartOpenHighLowCloseSeries, ChartCandleStickSeries)

if __pyside2__:
    from PySide2 import QtCore
    from PySide2.QtCore import Qt
    from PySide2.QtGui import QBrush
    from PySide2.QtWidgets import (QApplication, QComboBox, QCheckBox, QSlider, QSpinBox, QLabel)

if __pyside6__:
    from PySide6 import QtCore
    from PySide6.QtCore import Qt
    from PySide6.QtGui import QBrush
    from PySide6.QtWidgets import (QApplication, QComboBox, QCheckBox, QSlider, QSpinBox, QLabel)

from DemoChartWindow import DemoChartWindow

def appendSumbolsPoint(series, arg, low, high, open, close):
    series.addXYY1Y2Y3(arg, low, high, open, close)

class MainWindow(DemoChartWindow):
    OpenHighLowCloseSeries = 0
    CandleStickSeries = 1
    def __init__(self):
        DemoChartWindow.__init__(self, "Financial Charts")
        self.setChart(None)
        self.createSeriesParametrs()
        self.createMarkersGroup()
        self.createLabelsGroup()
        self.seriesChanged(self.seriesSwitcher.currentIndex())

    def createSeriesParametrs(self):
        seriesTypeGroup = self.createGroupParameters(self.tr("Series"))
        localLayout = seriesTypeGroup.layout()

        self.seriesSwitcher = QComboBox(seriesTypeGroup)
        self.seriesSwitcher.addItem("OpenHighLowClose", MainWindow.OpenHighLowCloseSeries)
        self.seriesSwitcher.addItem("CandleStick", MainWindow.CandleStickSeries)
        self.connect(self.seriesSwitcher, QtCore.SIGNAL("currentIndexChanged(int)"), self, QtCore.SLOT("seriesChanged(int)"))

        self.thicknessLabelBox = QLabel(self.tr("Thickness"), seriesTypeGroup)
        self.thicknessBox = QSpinBox(seriesTypeGroup)
        self.thicknessBox.setValue(5)
        self.thicknessBox.setSingleStep(1)
        self.thicknessBox.setRange(1, 5)
        self.connect(self.thicknessBox, QtCore.SIGNAL("valueChanged(int)"), self, QtCore.SLOT("valueThicknessChanged(int)"))

        localLayout.addRow(self.seriesSwitcher)
        localLayout.addRow(self.thicknessLabelBox, self.thicknessBox)

    def createOpenHighLowCloseSeries(self):
        self.createTitle(self.tr("Stock Prices (OHLC)"))
        self.chart.legend().setVisible(True)

        series = ChartOpenHighLowCloseSeries()
        series.setName(self.tr("Microsoft"))

        appendSumbolsPoint(series, "2018-Jan-24", 305.19, 315.80, 311.53, 306.40)
        appendSumbolsPoint(series, "2018-Jan-25", 307.23, 313.5,  307.83, 312.88)
        appendSumbolsPoint(series, "2018-Jan-26", 310.8,  315.60, 312.36, 311.40)
        appendSumbolsPoint(series, "2018-Jan-27", 311.48, 316.50, 313.10, 315.25)
        appendSumbolsPoint(series, "2018-Jun-04", 314.59, 328.00, 315.02, 324.15)
        self.chart.appendSeries(series)

        view = series.view()
        view.axisY().title().setText(self.tr("Price"))
        view.axisY().title().setVisible(True)
        view.axisY().setAutoRange(False)
        view.axisY().setFixedRange(300, 335)
        view.axisY().setShowZeroLevel(False)
        view.axisY().setMinorGridLinesVisible(False)
        view.axisX().title().setText(self.tr("Day"))
        view.axisX().title().setVisible(True)
        view.axisX().label().setVisible(True)

    def createCandleStickSeries(self):
        self.createTitle(self.tr("Stock Prices (Candlestick)"))
        self.chart.legend().setVisible(True)

        series = ChartCandleStickSeries()
        series.setName(self.tr("ACME Corp."))
        #      Time      low,   high,  open,   close
        appendSumbolsPoint(series, "28", 40.89, 41.18, 41.00, 41.17)
        appendSumbolsPoint(series, "29", 41.23, 41.50, 41.35, 41.39)
        appendSumbolsPoint(series, "30", 40.8,  41.29, 41.15, 40.96)
        appendSumbolsPoint(series, "31", 40.48, 40.99, 40.98, 40.48)
        appendSumbolsPoint(series, "04", 40.59, 41.10, 40.62, 40.95)
        appendSumbolsPoint(series, "05", 40.64, 41.10, 40.85, 40.96)
        appendSumbolsPoint(series, "06", 40.52, 41.08, 40.88, 40.77)
        appendSumbolsPoint(series, "07", 40.19, 40.70, 40.63, 40.45)
        appendSumbolsPoint(series, "08", 40.24, 40.88, 40.28, 40.66)
        appendSumbolsPoint(series, "11", 40.12, 40.76, 40.71, 40.27)
        appendSumbolsPoint(series, "12", 39.91, 40.40, 40.15, 40.07)
        appendSumbolsPoint(series, "13", 40.01, 40.52, 40.26, 40.35)
        appendSumbolsPoint(series, "14", 40.26, 41.10, 40.31, 40.96)
        appendSumbolsPoint(series, "15", 40.71, 41.24, 41.08, 40.86)
        appendSumbolsPoint(series, "19", 40.68, 41.24, 40.75, 41.10)
        appendSumbolsPoint(series, "20", 40.31, 40.94, 40.81, 40.59)
        appendSumbolsPoint(series, "21", 40.00, 40.72, 40.61, 40.01)
        appendSumbolsPoint(series, "22", 38.84, 40.20, 40.00, 38.96)
        appendSumbolsPoint(series, "25", 39.10, 39.66, 39.24, 39.32)
        appendSumbolsPoint(series, "26", 39.09, 39.85, 39.20, 39.50)
        appendSumbolsPoint(series, "27", 39.02, 39.82, 39.35, 39.67)
        appendSumbolsPoint(series, "28", 38.89, 39.87, 39.84, 39.16)
        appendSumbolsPoint(series, "29", 37.66, 39.92, 39.90, 38.18)
        appendSumbolsPoint(series, "01", 37.92, 38.48, 38.39, 38.41)
        appendSumbolsPoint(series, "02", 38.14, 38.50, 38.37, 38.46)
        appendSumbolsPoint(series, "03", 38.12, 38.79, 38.26, 38.63)
        self.chart.appendSeries(series)
        view = series.view()
        view.axisY().title().setText(self.tr("Price"))
        view.axisY().title().setVisible(True)
        view.axisY().setAutoRange(False)
        view.axisY().setFixedRange(37, 43)
        view.axisY().setShowZeroLevel(False)
        view.axisY().setMinorGridLinesVisible(False)
        view.axisX().title().setText(self.tr("Day"))
        view.axisX().title().setVisible(True)
        view.axisX().label().setVisible(True)
        view.axisX().setMinorCount(0)

    def updateValueParameters(self):
        DemoChartWindow.updateValueParameters(self)
        self.valueThicknessChanged(self.thicknessBox.value())

    def seriesChanged(self, index):
        self.chart.clearSeries()
        self.chart.clearTitles()
        self.chart.setBackgroundBrush(QBrush())

        v = self.seriesSwitcher.itemData(index)
        if int(v) == MainWindow.OpenHighLowCloseSeries:
            self.dataLabelsGroup.show()
            self.markerShapeSizeTitleSwitcher.show()
            self.dataMarkersGroup.hide()
            self.dataLabelsGroup.hide()
            self.createOpenHighLowCloseSeries()
        elif int(v) == MainWindow.CandleStickSeries:
            self.dataLabelsGroup.show()
            self.markerShapeSizeTitleSwitcher.show()
            self.dataMarkersGroup.hide()
            self.dataLabelsGroup.hide()
            self.createCandleStickSeries()
            self.thicknessBox.setValue(2)
            # valueThicknessChanged(2)

        self.updateValueParameters()

    def valueThicknessChanged(self, value):
        listSeries = self.chart.series()
        for series in listSeries:
            if series.inherits("Qtitan::ChartOpenHighLowCloseSeries"):
                series.setLineThickness(value)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())