Files
lightgui/widgets/components/StockVolumeProfile/StockVolumeProfile.h
cnmcdee 099733b27d Stock chart CSV data: loader in StockData.h, load_csv/add_series_csv on 23 widgets, docs + tests
- xframe::load_ohlcv_csv / parse_ohlcv_csv_text in StockData.h:
  date,open,high,low,close,volume, optional header matched by name
  (Massive exports load as-is), YYYY-MM-DD / ISO / epoch s-ms-ns dates,
  volume optional, malformed rows skipped + counted, error string out.
- 15 single-series widgets gain load_csv() + clear_series();
  8 comparative widgets gain add_series_csv(); failure leaves the
  series untouched. Drawing tools intentionally excluded (coordinates,
  not series); Ind* widgets take CSV only via the constructor.
- Fixtures tests/data/ohlcv_sample.csv (+ _b, 30 rows each) and
  widgets/tests/csv_unit_test.cpp + test_stock_csv.py (23/23 PASS:
  CSV shot differs from template shot, 30 items, bad path exits 2).
- docs/stock-market-csv-data.md + CSV sections in the stock group docs.
2026-12-02 10:15:50 +00:00

218 lines
12 KiB
C++

#pragma once
// StockVolumeProfile — XFrame Stock Market section.
// Horizontal volume-by-price histogram: volume accumulated into price bins, with the point of control marked.
// Attribution: XFrame.
#include "core/xframe_core.h"
#include "../StockData.h"
#include <X11/keysym.h>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <string>
#include <vector>
class StockVolumeProfile
{
private:
Display* display_;
int screen_;
Window window_;
GC gc_;
SStyle_Manager* styles_;
int x1_, y1_, x2_, y2_;
std::string name_ = "StockVolumeProfile";
std::string text_ = "StockVolumeProfile";
std::string style_name_;
bool visible_ = true;
bool enabled_ = true;
bool focused_ = false;
double view_first_ = 0.0;
double view_count_ = 0.0;
bool dragging_ = false;
int drag_start_x_ = 0;
double drag_start_first_ = 0.0;
bool cross_valid_ = false;
int cross_x_ = 0, cross_y_ = 0;
static constexpr Srgb kPalette[8] = { {0,100,200},{200,30,30},{0,150,0},{230,126,34},{120,60,180},{0,150,150},{180,120,0},{100,100,100} };
xframe::OHLCVSeries series_;
std::string symbol_ = "SYM";
// Getter/setter pass (Group C): render state exposed as properties.
Srgb bg_color_ = Srgb{255, 255, 255};
Srgb line_color_ = Srgb{0, 100, 200};
Srgb poc_color_ = Srgb{200, 30, 30};
public:
std::function<void(double, double)> on_view_change;
StockVolumeProfile(Display* display, int screen, Window window, GC gc, SStyle_Manager* styles, int x, int y, int width, int height)
: display_(display), screen_(screen), window_(window), gc_(gc), styles_(styles), x1_(x), y1_(y), x2_(x + width), y2_(y + height)
{
set_series(xframe::make_seeded_series(60, 42));
set_symbol("SYM");
}
~StockVolumeProfile() {}
void set_bounds(int x, int y, int width, int height) { x1_ = x; y1_ = y; x2_ = x + width; y2_ = y + height; }
int width() { return x2_ - x1_; }
int height() { return y2_ - y1_; }
bool contains(int x, int y) { return visible_ && x >= x1_ && x <= x2_ && y >= y1_ && y <= y2_; }
void set_visible(bool v) { visible_ = v; }
bool get_visible() { return visible_; }
void set_enabled(bool e) { enabled_ = e; }
bool get_enabled() { return enabled_; }
void set_style_name(std::string s) { style_name_ = s; }
void set_text(std::string t) { text_ = t; }
std::string get_text() { return text_; }
// Render properties (getter/setter pass, Group C): defaults preserve pre-pass rendering.
void set_bg_color(Srgb c) { bg_color_ = c; }
Srgb get_bg_color() { return bg_color_; }
void set_line_color(Srgb c) { line_color_ = c; }
Srgb get_line_color() { return line_color_; }
void set_poc_color(Srgb c) { poc_color_ = c; }
Srgb get_poc_color() { return poc_color_; }
void set_view_first(double v) { view_first_ = v; }
double get_view_first() { return view_first_; }
void set_view_count(double v) { view_count_ = v; }
double get_view_count() { return view_count_; }
std::string get_symbol() { return symbol_; }
SStyle style() { return styles_->resolve(style_name_); }
void set_series(const xframe::OHLCVSeries& s) { series_ = s; view_first_ = 0.0; view_count_ = 0.0; }
// CSV: load an OHLCV file (format: see load_ohlcv_csv in StockData.h)
// and replace the series. Returns false and leaves the series
// unchanged on error; `error` explains why.
bool load_csv(const std::string& path, std::string& error) {
xframe::OHLCVSeries s = xframe::load_ohlcv_csv(path, error);
if (!error.empty()) return false;
set_series(s);
return true;
}
// Clear the chart: same as set_series with an empty series.
void clear_series() { set_series(xframe::OHLCVSeries{}); }
void set_symbol(const std::string& s) { symbol_ = s; }
std::string symbol() { return symbol_; }
int series_count() { return series_.empty() ? 0 : 1; }
int item_count() { return (int)series_.size(); }
int bins_ = 20;
void set_bins(int n) { bins_ = std::max(2, std::min(100, n)); }
int get_bins() { return bins_; }
std::vector<double> profile() {
std::vector<double> out(bins_, 0.0); if (series_.empty()) return out;
double lo = 1e300, hi = -1e300; for (auto& b : series_) { lo = std::min(lo, b.low); hi = std::max(hi, b.high); }
if (hi <= lo) hi = lo + 1;
for (auto& b : series_) { double tp = (b.high + b.low + b.close) / 3.0; int k = (int)((tp - lo) / (hi - lo) * bins_); if (k < 0) k = 0; if (k >= bins_) k = bins_ - 1; out[k] += b.volume; }
return out;
}
int poc_bin() { auto p = profile(); int best = 0; for (int i = 1; i < (int)p.size(); ++i) if (p[i] > p[best]) best = i; return best; }
double poc_price() { if (series_.empty()) return 0.0; double lo = 1e300, hi = -1e300; for (auto& b : series_) { lo = std::min(lo, b.low); hi = std::max(hi, b.high); } if (hi <= lo) hi = lo + 1; return lo + (hi - lo) * (poc_bin() + 0.5) / bins_; }
// ---- geometry / scale helpers ----
int axis_w_ = 56, axis_h_ = 20;
int plot_x() { return x1_ + axis_w_; }
int plot_y() { return y1_ + 6; }
int plot_w() { return width() - axis_w_ - 8; }
int plot_h() { return height() - axis_h_ - 12; }
double effective_count() { double t = (double)item_count(); if (view_count_ <= 0 || view_count_ > t) return t; return view_count_; }
double item_w() { double c = effective_count(); return c > 0 ? plot_w() / c : (double)plot_w(); }
int x_at(double i) { return plot_x() + (int)((i - view_first_) * item_w()); }
int x_center(int i) { return plot_x() + (int)((i - view_first_ + 0.5) * item_w()); }
int y_at(double v, double lo, double hi) { if (hi <= lo) return plot_y() + plot_h() / 2; return plot_y() + (int)((hi - v) / (hi - lo) * plot_h()); }
static double nice_step(double range, int target) { if (range <= 0) return 1.0; double raw = range / target; double mag = std::pow(10.0, std::floor(std::log10(raw))); double norm = raw / mag; double step = (norm < 1.5) ? 1.0 : (norm < 3.5) ? 2.0 : (norm < 7.5) ? 5.0 : 10.0; return step * mag; }
static std::string fmt(double v) { std::ostringstream o; o.precision(2); o << std::fixed << v; std::string s = o.str(); while (!s.empty() && s.back() == '0') s.pop_back(); if (!s.empty() && s.back() == '.') s.pop_back(); return s.empty() ? "0" : s; }
void fire_view_change() { if (on_view_change) on_view_change(view_first_, effective_count()); }
void reset_view() { view_first_ = 0.0; view_count_ = (double)item_count(); fire_view_change(); }
void zoom_at(double f, double frac) { double t = (double)item_count(); if (t <= 0) return; double c = effective_count(); double idx = view_first_ + frac * c; double nc = std::max(5.0, std::min(t, c * f)); view_first_ = std::max(0.0, std::min(t - nc, idx - frac * nc)); view_count_ = nc; fire_view_change(); }
void draw_grid(double lo, double hi) {
SStyle st = style();
unsigned long grid = styles_->get_color_rgb(225, 225, 225);
double step = nice_step(hi - lo, 6); if (step <= 0) step = 1.0;
double start = std::ceil(lo / step) * step;
for (double v = start; v <= hi + 1e-9; v += step) {
int gy = y_at(v, lo, hi);
XSetForeground(display_, gc_, grid);
XDrawLine(display_, window_, gc_, plot_x(), gy, plot_x() + plot_w(), gy);
std::string lab = fmt(v);
int tw = xf::text_width(st, lab);
xf::draw_text_at(display_, window_, gc_, st, plot_x() - tw - 6, gy + 4, lab);
}
}
void draw_legend(const std::vector<std::pair<std::string, Srgb>>& items) {
SStyle st = style();
int lx = plot_x() + 4, ly = plot_y() + 4;
for (auto& it : items) {
unsigned long c = styles_->get_color_Srgb(it.second);
XSetForeground(display_, gc_, c);
XFillRectangle(display_, window_, gc_, lx, ly, 12, 3);
xf::draw_text_at(display_, window_, gc_, st, lx + 16, ly + 11, it.first);
lx += 16 + xf::text_width(st, it.first) + 18;
}
}
void draw_border() { SStyle st = style(); XSetForeground(display_, gc_, st.lineColor_); XDrawRectangle(display_, window_, gc_, x1_, y1_, width(), height()); }
void draw_crosshair(double lo, double hi) {
if (!cross_valid_) return;
SStyle st = style();
unsigned long c = styles_->get_color_rgb(150, 150, 150);
XSetForeground(display_, gc_, c);
XDrawLine(display_, window_, gc_, cross_x_, plot_y(), cross_x_, plot_y() + plot_h());
XDrawLine(display_, window_, gc_, plot_x(), cross_y_, plot_x() + plot_w(), cross_y_);
(void)st; (void)lo; (void)hi;
}
void draw() {
if (!visible_) return;
SStyle st = style();
unsigned long white = styles_->get_color_Srgb(bg_color_);
XSetForeground(display_, gc_, white);
XFillRectangle(display_, window_, gc_, x1_, y1_, width(), height());
auto prof = profile();
double lo = 1e300, hi = -1e300, vmax = 0;
for (auto& b : series_) { lo = std::min(lo, b.low); hi = std::max(hi, b.high); }
for (double v : prof) vmax = std::max(vmax, v);
if (series_.empty()) { lo = 0; hi = 1; } if (hi <= lo) hi = lo + 1; if (vmax <= 0) vmax = 1;
draw_grid(lo, hi);
int n = (int)prof.size();
unsigned long c = styles_->get_color_Srgb(line_color_);
unsigned long poc = styles_->get_color_Srgb(poc_color_);
for (int k = 0; k < n; ++k) {
double p_lo = lo + (hi - lo) * k / n, p_hi = lo + (hi - lo) * (k + 1) / n;
int y0 = y_at(p_hi, lo, hi), y1 = y_at(p_lo, lo, hi);
int bw = (int)(prof[k] / vmax * plot_w());
bool is_poc = (k == poc_bin());
XSetForeground(display_, gc_, is_poc ? poc : c);
XFillRectangle(display_, window_, gc_, plot_x(), y0, std::max(1, bw), std::max(1, y1 - y0 - 1));
}
xf::draw_text_at(display_, window_, gc_, style(), plot_x() + 4, plot_y() + 12, "Volume profile POC=" + fmt(poc_price()));
draw_border();
XFlush(display_);
}
bool on_button_press(int x, int y, unsigned int button) {
if (!contains(x, y) || !enabled_) return false;
focused_ = true;
double frac = plot_w() > 0 ? std::max(0.0, std::min(1.0, (double)(x - plot_x()) / plot_w())) : 0.0;
if (button == 4) { zoom_at(0.8, frac); return true; }
if (button == 5) { zoom_at(1.25, frac); return true; }
if (button == 1) { dragging_ = true; drag_start_x_ = x; drag_start_first_ = view_first_; cross_valid_ = true; cross_x_ = x; cross_y_ = y; return true; }
return false;
}
bool on_button_release(int x, int y, unsigned int button) { (void)x; (void)y; if (button == 1 && dragging_) { dragging_ = false; return true; } return false; }
bool on_motion(int x, int y) {
cross_valid_ = true; cross_x_ = x; cross_y_ = y;
if (dragging_) { double t = (double)item_count(); double c = effective_count(); double d = -(x - drag_start_x_) / item_w(); view_first_ = std::max(0.0, std::min(std::max(0.0, t - c), drag_start_first_ + d)); fire_view_change(); }
return true;
}
bool on_key(KeySym key, const char* str) {
(void)str;
if (!focused_ || !enabled_) return false;
double t = (double)item_count(), c = effective_count();
switch (key) {
case XK_Left: view_first_ = std::max(0.0, view_first_ - c * 0.1); fire_view_change(); return true;
case XK_Right: view_first_ = std::min(std::max(0.0, t - c), view_first_ + c * 0.1); fire_view_change(); return true;
case XK_plus: case XK_equal: zoom_at(0.8, 0.5); return true;
case XK_minus: zoom_at(1.25, 0.5); return true;
case XK_Home: case XK_r: case XK_R: reset_view(); return true;
}
return false;
}
};