-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgallery_financial_candlestick.cpp
More file actions
144 lines (127 loc) 路 4.79 KB
/
Copy pathgallery_financial_candlestick.cpp
File metadata and controls
144 lines (127 loc) 路 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* @file gallery_financial_candlestick.cpp
* @brief Financial Candlestick Chart - OHLC Stock Data Visualization
* @author plotly.cpp contributors
* @date 2025
*
* @example gallery_financial_candlestick.cpp
*
* # Financial Candlestick Chart Example
*
* This example demonstrates how to create professional financial charts using
* candlestick traces with volume indicators. It generates synthetic stock
* market data and visualizes it with the standard OHLC (Open, High, Low, Close)
* format used in financial analysis.
*
* ## What You'll Learn
* - Creating candlestick charts for financial data visualization
* - Generating realistic synthetic stock data with random price movements
* - Implementing dual y-axis layouts for price and volume data
* - Customizing candlestick colors for bullish/bearish patterns
* - Working with time-series data and date formatting
* - Adding volume bars as complementary indicators
*
* ## Sample Output
* The example creates a comprehensive financial chart featuring:
* - 60-day candlestick chart with OHLC price data
* - Green candles for price increases, red candles for decreases
* - Volume bars displayed below the main price chart
* - Interactive hover information showing detailed price data
* - Professional styling suitable for financial analysis
*
* @image html financial_candlestick.png "Financial Candlestick Chart Example"
*
* @see plotly::Object For candlestick trace configuration
* @see parseGalleryArgs() For command line argument handling
*/
#include "plotly/plotly.hpp"
#include "utils/arg_parser.hpp"
#include <cstdlib>
#include <random>
#include <string>
#include <vector>
auto main(int argc, char *argv[]) -> int {
// Parse command line arguments
auto args = parseGalleryArgs(argc, argv);
plotly::Figure fig;
fig.openBrowser(args.headless);
// Generate synthetic stock data
const int numDays = 60;
std::vector<std::string> dates;
std::vector<double> open, high, low, close, volume;
// Random number generation for realistic stock data
std::random_device rd;
std::mt19937 gen(rd());
std::normal_distribution<double> priceChange(0.0, 2.0);
std::uniform_real_distribution<double> volumeDist(100000, 1000000);
double currentPrice = 100.0;
for (int i = 0; i < numDays; i++) {
// Generate date string (simplified)
dates.push_back("2024-" + std::to_string((i / 30) + 1) + "-" +
std::to_string((i % 30) + 1));
// Generate OHLC data
double dayOpen = currentPrice;
double priceRange = std::abs(priceChange(gen));
double dayHigh = dayOpen + priceRange * 0.8;
double dayLow = dayOpen - priceRange * 0.6;
double dayClose =
dayLow + (dayHigh - dayLow) *
std::uniform_real_distribution<double>(0.2, 0.8)(gen);
open.push_back(dayOpen);
high.push_back(dayHigh);
low.push_back(dayLow);
close.push_back(dayClose);
volume.push_back(volumeDist(gen));
currentPrice = dayClose + priceChange(gen) * 0.5;
if (currentPrice < 50)
currentPrice = 50;
if (currentPrice > 200)
currentPrice = 200;
}
// Create candlestick trace
plotly::Object candlestickTrace = {
{"type", "candlestick"},
{"x", dates},
{"open", open},
{"high", high},
{"low", low},
{"close", close},
{"name", "Stock Price"},
{"yaxis", "y"},
{"increasing",
{{"fillcolor", "#00ff00"}, {"line", {{"color", "#00aa00"}}}}},
{"decreasing",
{{"fillcolor", "#ff0000"}, {"line", {{"color", "#aa0000"}}}}}};
// Create volume bar trace
plotly::Object volumeTrace = {{"type", "bar"},
{"x", dates},
{"y", volume},
{"name", "Volume"},
{"yaxis", "y2"},
{"opacity", 0.3},
{"marker", {{"color", "blue"}}}};
// Create layout with dual y-axes
plotly::Object layout = {
{"title", {{"text", "Stock Price Candlestick Chart with Volume"}}},
{"xaxis", {{"title", "Date"}, {"rangeslider", {{"visible", false}}}}},
{"yaxis", {{"title", "Price ($)"}, {"domain", {0.3, 1.0}}}},
{"yaxis2",
{{"title", "Volume"}, {"domain", {0.0, 0.25}}, {"side", "right"}}},
{"width", 1000},
{"height", 700},
{"showlegend", true}};
// Create the plot
std::vector<plotly::Object> data = {candlestickTrace, volumeTrace};
fig.newPlot(data, layout);
if (!args.headless) {
fig.waitClose();
} else {
// Save image instead of opening browser
plotly::Object imageOpts = {{"format", "png"},
{"width", 1000},
{"height", 700},
{"filename", "financial_candlestick"}};
fig.downloadImage(imageOpts);
}
return 0;
}