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.
#include <cstdlib>
#include <random>
#include <string>
#include <vector>
auto main(
int argc,
char *argv[]) ->
int {
const int numDays = 60;
std::vector<std::string> dates;
std::vector<double> open, high, low, close, volume;
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++) {
dates.push_back("2024-" + std::to_string((i / 30) + 1) + "-" +
std::to_string((i % 30) + 1));
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;
}
{"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"}}}}}};
{"x", dates},
{"y", volume},
{"name", "Volume"},
{"yaxis", "y2"},
{"opacity", 0.3},
{"marker", {{"color", "blue"}}}};
{"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}};
std::vector<plotly::Object> data = {candlestickTrace, volumeTrace};
if (!args.headless) {
} else {
{"width", 1000},
{"height", 700},
{"filename", "financial_candlestick"}};
}
return 0;
}
auto parseGalleryArgs(int argc, char *argv[]) -> GalleryArgs
Parse command line arguments for gallery examples.
Definition arg_parser.cpp:4
auto main() -> int
Definition gallery_animate_sin_wave.cpp:48
nlohmann::json Object
Definition plotly.hpp:26
Public Plotly C++ API header.