Creating Candlestick Charts

Note: This is part of the Charts with Widgets Gallery example.

To display a candlestick chart, we start by creating QCandlestickSeries to handle daily data. We are also specifying custom increasing and decreasing body colors.

auto acmeSeries = new QCandlestickSeries;
acmeSeries->setName("Acme Ltd");

QFile is used for accessing a text file where the non-continuous data is kept. The CandlestickDataReader is an auxiliary class for reading the text file and finding the open, high, low, close, and timestamp values from the data. The CandlestickDataReader is explained in more detail later. The method readCandlestickSet() reads the values and sets them to the QCandlestickSet item which the method returns for the caller. The returned QCandlestickSet item is added to the series. We are also saving custom categories list for later use.

QFile acmeData(":candlestick");
if (! | QIODevice::Text)) {
    m_loadError = QStringLiteral("Failed to load '%1' file.").arg(acmeData.fileName());
    return false;

QStringList categories;

CandlestickDataReader dataReader(&acmeData);
while (!dataReader.atEnd()) {
    QCandlestickSet *set = dataReader.readCandlestickSet();
    if (set) {
        categories << QDateTime::fromMSecsSinceEpoch(set->timestamp()).toString("dd");

Below, a new QChart instance is created and the previously created series object is added to it. We also define a title, and set an animation as QChart::SeriesAnimation.

auto chart = new QChart;
chart->setTitle("Acme Ltd. Historical Data (July 2015)");

Here, we ask the chart to create default axes for our presentation. Then, we set custom categories for the horizontal axis by querying the pointer for the axis from the chart, and then setting the categories from previously saved custom categories list. We also set the range for the vertical axis by querying the pointer for the axis from the chart, and then setting the min and max values for that axis.


auto axisX = qobject_cast<QBarCategoryAxis *>(chart->axes(Qt::Horizontal).at(0));

auto axisY = qobject_cast<QValueAxis *>(chart->axes(Qt::Vertical).at(0));
axisY->setMax(axisY->max() * 1.01);
axisY->setMin(axisY->min() * 0.99);

Below, we set the legend to be visible and place it at the bottom of the chart.


Finally, we add the chart onto a view.


The chart is ready to be shown.

Here, the method readCandlestickSet() is explained in detail. First, a line is read from the file, rejecting any lines starting with # as they are considered comment lines.

QString line = m_textStream.readLine();
if (line.startsWith("#") || line.isEmpty())
    return nullptr;

In the file, the data is arranged as a space-delimited sequence of numbers. On this snippet the line is split into single number strings which are stored in a QStringList.

QStringList strList = line.split(QLatin1Char(' '), Qt::SkipEmptyParts);
if (strList.count() != 5)
    return nullptr;

To select values from the continuous data, following code is used. The values in a strList are stored in the following order: timestamp, open, high, low, close.

const qreal timestamp =;
const qreal open =;
const qreal high =;
const qreal low =;
const qreal close =;

Finally, the following snippet shows how to create a new QCandlestickSet and provide it with all the necessary values.

auto candlestickSet = new QCandlestickSet(timestamp);

© 2024 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.