JKQTPlotter trunk/v5.0.0
an extensive Qt5+Qt6 Plotter framework (including a feature-richt plotter widget, a speed-optimized, but limited variant and a LaTeX equation renderer!), written fully in C/C++ and without external dependencies
Loading...
Searching...
No Matches
jkqtpfinancial.h
1/*
2 Copyright (c) 2008-2024 Jan W. Krieger (<jan@jkrieger.de>)
3
4
5
6 This software is free software: you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License (LGPL) as published by
8 the Free Software Foundation, either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License (LGPL) for more details.
15
16 You should have received a copy of the GNU Lesser General Public License (LGPL)
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#ifndef jkqtpfinancial_H
21#define jkqtpfinancial_H
22
23#include <QString>
24#include <QPainter>
25#include <QPair>
26#include "jkqtplotter/jkqtptools.h"
27#include "jkqtplotter/jkqtplotter_imexport.h"
28#include "jkqtplotter/jkqtpgraphsbase.h"
29#include "jkqtplotter/jkqtpgraphsbasestylingmixins.h"
30
31
32/** \brief This class draws an Open-High-Low-Close (OHLC) or candle-stick graph typically representing financial data
33 * \ingroup jkqtplotter_financialgraphs
34 *
35 * This graph draws either <b>candlstick charts</b>:
36 * \image html financialgraphs_basics_candlestick.png
37 * or <b>Open-High-Low-Close (OHLC) charts</b> (depending on what you set in setGraphType() ).
38 * \image html financialgraphs_basics_ohlc.png
39 *
40 * Here is an example graph showcasing both variants:
41 *
42 * \image html JKQTPFinancialGraph.png
43 *
44 * \see For details on the graph types see https://en.wikipedia.org/wiki/Candlestick_chart and https://en.wikipedia.org/wiki/Open-high-low-close_chart
45 * \see JKQTPXGraph
46 *
47 *
48 * \section JKQTPFinancialGraphStyling Styling the Financial Graphs
49 * You can control the type of graph and it's appearance with these functions (example images are in the documentation of the functions):
50 * - setGraphType()
51 * - setCandlestickTwoColor()
52 * - setCandlestickOneColor()
53 * - setOHLCTwoColor()
54 * .
55 *
56 * For fine-tuning, you can access the line- and fill-styles for positiove (close>open) and negative (close<open)
57 * items separately via:
58 * - lineStylePositive() and fillStylePositive()
59 * - lineStyleNegative() and fillStyleNegative()
60 * .
61 *
62 * Note that the width of the single elements for each x-value is automatically from the distance to the
63 * next neighbours. Here set parameter width (setWidth(), getWidth() ) is employed. It sets the relative
64 * amount of space that an element occupies in its part of the graph. Typically the default width is 0.8 (or 80%).
65 *
66 * \image html financialgraphs_width.png
67 *
68 *
69 * \section JKQTPFinancialGraphSideBySide Several Financial Graphs side-by-side
70 * In case you want to display several of these graphs in the same plot, you can make them appear side-by-side, even
71 * if they all cover the same x-values and would thus be draw atop each other. We apply the same principles that
72 * were used for barcharts in JKQTPBarGraphBase and add a shift-parameter (setShift() ) that shifts each element
73 * in the available space:
74 *
75 * \image html financialgraphs_shiftwidth.png
76 *
77 * For example for two financial graph elements per x-value one would set:
78 * \verbatim
79 * width=0.4
80 * shift=-0.5 / +0.5
81 * \endverbatim
82 *
83 * Two functions are provided (JKQTPFinancialGraph::autoscaleBoxWidthAndShift(), JKQTPFinancialGraph::autoscaleBoxWidthAndShiftSeparatedGroups() )
84 * that can be applied to any JKQTPFinancialGraph in the plot and will calculate all JKQTPFinancialGraphs' shift and width parameter
85 * in such a way that the graphs appear tidied up into groups by x-value. With these functions you don't have to calculate
86 * the shift and width values by hand! A call always only affects the JKQTPFinancialGraph in the plot with the same orientation
87 * (vertical or horizontal).
88 *
89 * \image html JKQTPFinancialGraphSidyBySide.png
90 *
91 */
93 Q_OBJECT
94 public:
95 /** \brief types of financial graphs that can be drawn by this class */
97 CandleStick, //!< \brief candlestick graphs \image html JKQTPFinancialGraphCandleStick.png
98 OHLC //!< \brief OHLC (Open-High-Low-Close) graphs \image html JKQTPFinancialGraphOHLC.png
99 };
100 Q_ENUM(FinancialGraphType)
101
102 /** \brief class constructor */
104 /** \brief class constructor */
106
107 /** \copydoc JKQTPXGraph::drawKeyMarker() */
108 virtual void drawKeyMarker(JKQTPEnhancedPainter& painter, const QRectF& rect) override;
109 /** \copydoc JKQTPXGraph::getKeyLabelColor() */
110 virtual QColor getKeyLabelColor() const override;
111 /** \copydoc JKQTPXGraph::draw() */
112 virtual void draw(JKQTPEnhancedPainter& painter) override;
113
114
115
116 /** \copydoc shift */
117 double getShift() const;
118 /** \copydoc width */
119 double getWidth() const;
120
121 /** \copydoc m_fillStyleNegative */
122 JKQTPGraphFillStyleMixin &fillStyleNegative();
123 /** \copydoc m_fillStyleNegative */
124 const JKQTPGraphFillStyleMixin& fillStyleNegative() const;
125 /** \copydoc m_fillStylePositive */
126 JKQTPGraphFillStyleMixin &fillStylePositive();
127 /** \copydoc m_fillStylePositive */
128 const JKQTPGraphFillStyleMixin& fillStylePositive() const;
129
130
131 /** \copydoc m_lineStyleNegative */
132 JKQTPGraphLineStyleMixin &lineStyleNegative();
133 /** \copydoc m_lineStyleNegative */
134 const JKQTPGraphLineStyleMixin& lineStyleNegative() const;
135 /** \copydoc m_lineStylePositive */
136 JKQTPGraphLineStyleMixin &lineStylePositive();
137 /** \copydoc m_lineStylePositive */
138 const JKQTPGraphLineStyleMixin& lineStylePositive() const;
139
140 /** \copydoc JKQTPXGraph::getIndexRange() */
141 virtual bool getIndexRange(int &imin, int &imax) const override;
142 /** \copydoc JKQTPXGraph::getYMinMax() */
143 virtual bool getYMinMax(double& miny, double& maxy, double& smallestGreaterZero) override;
144 /** \copydoc JKQTPXGraph::usesColumn() */
145 virtual bool usesColumn(int column) const override;
146
147
148
149 /** \copydoc openColumn */
150 int getOpenColumn() const;
151
152 /** \copydoc closeColumn */
153 int getCloseColumn() const;
154
155 /** \copydoc highColumn */
156 int getHighColumn() const;
157
158 /** \copydoc lowColumn */
159 int getLowColumn() const;
160 /** \copydoc graphType */
161 FinancialGraphType getGraphType() const;
162
163 Q_PROPERTY(int openColumn READ getOpenColumn WRITE setOpenColumn)
164 Q_PROPERTY(int closeColumn READ getCloseColumn WRITE setCloseColumn)
165 Q_PROPERTY(int highColumn READ getHighColumn WRITE setHighColumn)
166 Q_PROPERTY(int lowColumn READ getLowColumn WRITE setLowColumn)
167 Q_PROPERTY(FinancialGraphType graphType READ getGraphType WRITE setGraphType)
168
169 static void clearColorAssignStore();
170 public Q_SLOTS:
171
172 /** \brief finds all financial charts of the same orientation and determines width and shift, so they stand side by side
173 *
174 * \param maxWidth the maximum (relative) width, that all boxes will span of the (doubled) inter-box distance
175 * \param shrinkFactor factor, by which the boxes are shrinked compared to the available space
176 *
177 * \note This function will scale ALL graphs of the parent plot, which were derived from JKQTPFinancialGraph, that match in orientation (currently all).
178 */
179 virtual void autoscaleBoxWidthAndShift(double maxWidth=0.8, double shrinkFactor=0.8);
180
181 /** \brief equivalent to \c autoscaleBoxWidthAndShift(groupWidth,0.8);
182 */
183 void autoscaleBoxWidthAndShiftSeparatedGroups(double groupWidth=0.65);
184 /** \copydoc shift */
185 void setShift(double __value);
186 /** \copydoc width */
187 void setWidth(double __value);
188 /** \copydoc graphType */
189 void setGraphType(FinancialGraphType __value);
190
191 /** \brief set outline and fill color at the same time, for both positive and negative boxes
192 *
193 * \param cPositive color for positive items (close>open)
194 * \param cNegative color for negative items (close<open)
195 */
196 virtual void setColor(QColor cPositive, QColor cNegative);
197 /** \brief set outline and fill color at the same time, for both positive and negative boxes
198 *
199 * \param linePositive line-color for positive items (close>open)
200 * \param fillPositive fill-color for positive items (close>open)
201 * \param lineNegative line-color for negative items (close<open)
202 * \param fillNegative fill-color for negative items (close<open)
203 */
204 virtual void setColor(QColor linePositive, QColor fillPositive, QColor lineNegative, QColor fillNegative);
205 /** \brief sets graphType = FinancialGraphType::CandleStick and sets the positive and negative color (for fill and line equal!)
206 *
207 * \param cPositive color for positive items (close>open)
208 * \param cNegative color for negative items (close<open)
209 *
210 * Here is an example output for <code>setCandlestickTwoColor(QColor("blue"), QColor("orange"))</code>:
211 *
212 * \image html JKQTPFinancialGraphSetCandlestickTwoColor.png
213 */
214 void setCandlestickTwoColor(QColor cPositive, QColor cNegative);
215 /** \brief sets graphType = FinancialGraphType::CandleStick and sets the positive and negative fill color and a single line-color
216 *
217 * \param cPositive color for fill of positive items (close>open)
218 * \param cNegative color for fill of negative items (close<open)
219 *
220 * Here is an example output for <code>setCandlestickTwoColor(QColor("green"), QColor("red"), QColor("black"))</code>:
221 *
222 * \image html JKQTPFinancialGraphSetCandlestickTwoColor2.png
223 */
224 void setCandlestickTwoColor(QColor cPositive, QColor cNegative, QColor lineCOlor);
225 /** \brief sets graphType = FinancialGraphType::CandleStick and sets one color \a cLine for positive and negative items, where positive items are unfilled and negative items are filled
226 *
227 * \param cLine line-color for all items and also fill-color for positive items (close>open), negative items are unfilled
228 *
229 * Here is an example output for <code>setCandlestickOneColor(QColor("black"))</code>:
230 *
231 * \image html JKQTPFinancialGraphSetCandlestickOneColor.png
232 */
233 void setCandlestickOneColor(QColor cLine);
234 /** \brief sets graphType = FinancialGraphType::OHLC and sets the positive and negative color (for fill and line equal!)
235 *
236 * \param cPositive color for positive items (close>open)
237 * \param cNegative color for negative items (close<open)
238 *
239 * Here is an example output for <code>setOHLCTwoColor(QColor("darkblue"), QColor("darkorange"))</code>:
240 *
241 * \image html JKQTPFinancialGraphSetOHLCTwoColor.png
242 */
243 void setOHLCTwoColor(QColor cPositive, QColor cNegative);
244
245
246 /** \copydoc openColumn */
247 void setOpenColumn(int __value);
248 /** \copydoc openColumn */
249 void setOpenColumn (size_t __value);
250 /** \copydoc closeColumn */
251 void setCloseColumn(int __value);
252 /** \copydoc closeColumn */
253 void setCloseColumn (size_t __value);
254 /** \copydoc highColumn */
255 void setHighColumn(int __value);
256 /** \copydoc highColumn */
257 void setHighColumn (size_t __value);
258 /** \copydoc lowColumn */
259 void setLowColumn(int __value);
260 /** \copydoc lowColumn */
261 void setLowColumn (size_t __value);
262
263 protected:
264 static QSet<JKQTBasePlotter*> parentsAlreadySeen;
265 /** \brief type of the financial graph (OHLC or candle-stick)
266 *
267 * \see setGraphType(), getGraphType(), FinancialGraphType
268 */
269 FinancialGraphType graphType;
270 /** \brief column for the "Open" value of the financial graph
271 *
272 * \see setOpenColumn(), getOpenColumn()
273 */
274 int openColumn;
275 /** \brief column for the "Close" value of the financial graph
276 *
277 * \see setCloseColumn(), getCloseColumn()
278 */
279 int closeColumn;
280 /** \brief column for the "High" value of the financial graph
281 *
282 * \see setHighColumn(), getHighColumn()
283 */
284 int highColumn;
285 /** \brief column for the "Low" value of the financial graph
286 *
287 * \see setLowColumn(), getLowColumn()
288 */
289 int lowColumn;
290
291 /** \brief the width of the boxes, relative to the distance between the current and the next x-value
292 *
293 * See the following graphic to understand this concept (same principle as described in the JKQTPBarGraphBase documentation):
294 * \image html financialgraphs_shiftwidth.png
295 */
296 double width;
297 /** \brief the shift of the boxes, relative to the distance between the current and the next x-value
298 *
299 * See the following graphic to understand this concept (same principle as described in the JKQTPBarGraphBase documentation):
300 * \image html financialgraphs_shiftwidth.png
301 */
302 double shift;
303 /** \brief fill style for positive items (close>open)
304 *
305 * \see fillStylePositive()
306 */
307 JKQTPGraphFillStyleMixin m_fillStylePositive;
308 /** \brief fill style for negative items (close<open)
309 *
310 * \see fillStyleNegative()
311 */
312 JKQTPGraphFillStyleMixin m_fillStyleNegative;
313 /** \brief fill style for positive items (close>open)
314 *
315 * \see lineStylePositive()
316 */
317 JKQTPGraphLineStyleMixin m_lineStylePositive;
318 /** \brief fill style for negative items (close<open)
319 *
320 * \see lineStyleNegative()
321 */
322 JKQTPGraphLineStyleMixin m_lineStyleNegative;
323 /** \brief this function is used by autoscaleBoxWidthAndShift() to determine whether a given graph shall be taken into account when autoscaling.
324 * Typically this returns \c true for all JKQTPFinancialGraph-derved objects with the same orientation (horizontal or vertical, i.e. currently all) */
325 virtual bool considerForAutoscaling( JKQTPFinancialGraph* other) const;
326
327
328
329};
330
331
332
333
334
335
336
337#endif // jkqtpfinancial_H
base class for 2D plotter classes (used by the plotter widget JKQTPlotter)
Definition jkqtpbaseplotter.h:394
this class extends the QPainter
Definition jkqtpenhancedpainter.h:33
This class draws an Open-High-Low-Close (OHLC) or candle-stick graph typically representing financial...
Definition jkqtpfinancial.h:92
FinancialGraphType
types of financial graphs that can be drawn by this class
Definition jkqtpfinancial.h:96
@ CandleStick
candlestick graphs
Definition jkqtpfinancial.h:97
This Mix-In class provides setter/getter methods, storage and other facilities for the graph filling ...
Definition jkqtpgraphsbasestylingmixins.h:529
This Mix-In class provides setter/getter methods, storage and other facilities for the graph line sty...
Definition jkqtpgraphsbasestylingmixins.h:49
This virtual JKQTPGraph descendent may be used as base for all graphs that use at least one column th...
Definition jkqtpgraphsbase.h:1119
plotter widget for scientific plots (uses JKQTBasePlotter to do the actual drawing)
Definition jkqtplotter.h:364
#define JKQTPLOTTER_LIB_EXPORT
Definition jkqtplotter_imexport.h:89