diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..afa667e --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +fdesigner +*.o +tests/gen_matrix diff --git a/DESIGN.md b/DESIGN.md new file mode 100644 index 0000000..ec426db --- /dev/null +++ b/DESIGN.md @@ -0,0 +1,228 @@ +# FDesigner — C++/GTK conversion design + +## What we're building + +A C++ rebuild of Chris's fdesigner (Tkinter Rapid Designer). The original is a +~7,000-line Python/Tkinter app (`original/tkinter_designer/fdesigner7.py`): +place widgets on a canvas at absolute positions, edit their properties, then +generate runnable source code for the layout. + +Per Chris's decision (2026-11-25) the C++ version **generates C++/GTK code**, +not Tkinter Python. The designer's own UI is also GTK, defined in Glade .ui +files. No Qt anywhere. + +## Stack + +- **GTK 3.24** — Glade's target toolkit. (GTK 4 has no Glade; its successor is + Cambalache. Glade means GTK 3.) +- **C++ with the GTK C API**, windows loaded via **GtkBuilder** from .ui files + authored in Glade. No gtkmm: GtkBuilder is the native path, and this keeps + the generated code and the designer itself on the same API. +- Runs on **X11** (GTK 3's X11 backend), same as the original Tk app. +- Build: g++ + pkg-config (`gtk+-3.0`), plain Makefile, like Chris's other + tools. +- Toolchain verified on Muse's VM 2026-11-25: GTK 3.24.41 headers + Glade + installed; a C++ test program including compiles, links, and + runs under Xvfb. (VM quirk: apt's libzstd-dev version pin conflicts with the + VM's patched libzstd1, so the -dev packages were installed from downloaded + .debs with `dpkg --force-depends` — headers only, no runtime change. On + Chris's Parrot box a plain `apt install libgtk-3-dev glade` should work.) + +## Generated output + +For each designed window the generator emits two files, mirroring the spirit +of the Python tool's generated class + stub handlers: + +1. **`.ui`** — a Glade/GtkBuilder file holding the layout: a GtkWindow + containing a GtkFixed, with each widget placed at its designed x/y and + sized with width/height requests (the GTK equivalent of Tk's `.place()`). +2. **`.h` / `.cpp`** — a C++ class that loads the .ui with + GtkBuilder, fetches each widget by id, and declares one stub signal + handler per connected signal (`// Code block here`, like the original's + stubs), ready for the user to fill in. + +The generated project compiles standalone with: +`g++ -o app *.cpp $(pkg-config --cflags --libs gtk+-3.0)` + +## Widget mapping (Tk -> GTK) + +| Tk (original palette) | GTK 3 | +|---|---| +| Root / Toplevel | GtkWindow | +| Frame / Labelframe | GtkFrame | +| Button | GtkButton | +| Label | GtkLabel | +| Entry | GtkEntry | +| Checkbutton | GtkCheckButton | +| Radiobutton | GtkRadioButton | +| Combobox (ttk) | GtkComboBoxText | +| Listbox | GtkListBox (inside GtkScrolledWindow) | +| Menu | GtkMenu / GtkMenuBar | +| Menubutton | GtkMenuButton | +| Canvas | GtkDrawingArea | +| Scale | GtkScale | +| Scrollbar | GtkScrollbar | +| Text | GtkTextView (inside GtkScrolledWindow) | +| Spreadsheet (custom) | GtkGrid of GtkEntry (later phase) | + +## Internal model + +```cpp +struct Widget { + std::string name; // e.g. "Button_001" + std::string type; // palette type key, e.g. "Button" + int x, y, width, height; // canvas geometry + std::map props; // GTK properties, as strings + std::vector signals; // signals to stub out in generated code +}; +struct Project { + std::string title, class_name; + int win_width, win_height; + std::string background; // window background colour + std::vector widgets; // Root is implicit (the window itself) +}; +``` + +## Project file format + +**JSON** (`.fdj`), replacing Python pickle, which C++ cannot read. If Chris +has old .pkl designs he wants to keep, a one-off Python converter script +(pkl -> fdj) can be written later; pickle stays on the Python side only. + +## What does not port directly + +- **pickle** save/load -> JSON (above). +- **pyperclip** -> GtkClipboard (built into GTK). +- **openpyxl** spreadsheet modeller -> later phase; the on-canvas spreadsheet + widget is a GtkGrid; xlsx import, if wanted, needs a separate library. +- The huge generated colour files in `library/` (super_color.py ~31k lines, + colormenu.py ~15k lines) are data, not logic: they become colour-name data + tables / GTK colour chooser integration, not hand-ported code. + +## Phases + +1. **Skeleton + canvas**: main window from Glade .ui; palette adds widgets to + a GtkFixed canvas; click to select, drag to move, delete. *(skeleton done)* +2. **Property editor**: per-type property tables (ported from the original's + Entry_001..019 mapping), plus the original's bulk operations — set + parameter on all / selected / filtered widgets, clear parameter. +3. **Code generator**: emit .ui + .h/.cpp per the format above; generated + project compiles and runs. +4. **Save/load**: JSON project files; optional .pkl -> .fdj converter. +5. **Extras**: colour palette, menu designer, spreadsheet widget, canvas + widget (drawing area) support. + +**Status 2026-11-25: all five phases built and verified.** The self-test +(`FDESIGNER_SELFTEST=1 ./fdesigner`) exercises placement, properties, bulk +ops, generation, save/load round-trips, and the Phase 5 extras; generated +projects compile with their own Makefile and run. Known v1 limits: +designing *inside* a Toplevel or a generated menu's submenus is not +supported (Toplevel content starts as an empty GtkFixed; menus are a +one-level text spec), and there is no .pkl converter unless old designs +need importing. + +**Generator revision 2026-11-25 (Chris's direction):** generated projects +are ALL CODE (no .ui — so a stepping debugger can walk construction) and +every widget is a STANDALONE CLASS in its own .h/.cpp pair, mirroring the +Python generator's boilerplate: per-widget `// #region`, creation + +`.place()`-equivalent + config in the constructor, a `util_error_print` +helper, and the same nine mouse handlers per widget (buttonPress1-3, +dblbuttonPress1-3, buttonRelease1-3) routed by two dispatchers; the +window class keeps the Root_001 handler set and just instantiates the +widget classes. Verified by `tests/run_matrix.sh`: 13 generated designs +(every type, every property, every enum value, menus, hostile strings, +class-name edge cases, 40 widgets, spreadsheet extremes) each compile +with -Wall -Wextra, zero warnings, and run clean. The matrix caught a +real bug: property ranges (e.g. Label xalign 0..1) were not enforced, so +an out-of-range value could reach GTK — canonicalize() now enforces the +property tables' lo/hi for Int and Double. + +**StockChart widget added 2026-11-25:** a candle chart for Massive +aggregates CSVs (header `ticker,volume,open,close,high,low,window_start, +transactions`, matched by column name). Properties: `data-file`, +`up-color`, `down-color`, `max-candles`. The designer plots a built-in +preview series on the canvas and swaps in real data when data-file is +set; the generated StockChart class is self-contained — it embeds the +CSV loader (`load_csv`, plus `set_candles`/`candle_count`) and cairo +plotting (wicks + bodies, min/max scaled). Fake data for trying it: +`tests/data/SPY_fake.csv` (120 synthetic SPY minute bars). The matrix +grew to 14 cases plus a render test that draws the generated chart +offscreen and counts up/down-coloured pixels, proving it plots. + +**Indicator submodules added 2026-11-25:** `src/indicators.h` is ONE +self-contained header holding **250 named technical indicators** — +moving averages and Ehlers/Hilbert filters, bands/channels, five pivot +families, momentum oscillators, volume indicators, volatility and +statistics, and all 61 TA-Lib candlestick patterns plus 5 extras. Each +is a registry module (namespace `fdi`) that consumes the chart's OHLCV +bars and draws itself: price-pane overlays share the price scale, +oscillators get their own labelled pane strip, patterns draw markers. +They have no palette button; a StockChart enables them via its +`indicators` property (`"SMA(20), RSI, MACD(12,26,9), Engulfing"`) in +the designer, or `add_indicator("SMA(20)")` in generated code. The +generator embeds the header text (via `src/indicators_embedded.h`, +regenerated by `tools/embed_indicators.sh` from the Makefile rule) and +copies it into any generated project that contains a StockChart. +Candle structs carry `volume` (OBV/VWAP/MFI need it). Testing: +`tests/indicator_test.cpp` computes and renders every indicator over +`tests/data/SPY_fake_long.csv` (1,170 bars, 3 days), counts palette-ink +pixels per render, hand-checks 18 patterns on crafted candle sequences +and bounded oscillators against their legal ranges, and writes a +labelled contact sheet of all 250 plots to +`tests/out/indicators_contact_sheet.png`. Result: 244 render-verified +on data; 6 ultra-rare gap patterns verified by crafted sequences. +Matrix case 15 covers a generated chart with indicators attached. + +**StockChart Pro added 2026-11-25:** the chart gained a full +annotation system, comparative plotting, a percent scale, a time axis +and wheel zoom, in both the designer preview and generated code. + +- `src/drawings.h` is ONE self-contained header (namespace `fdw`) + holding the drawing module: 11 kinds — trend, ray, horizontal and + vertical lines, rectangle, ellipse, arrow, text labels, Fibonacci + retracement, freehand, and a measure tool (Δ price / % / bars). + Anchors are DATA coordinates (bar index, price), never pixels, so + annotations survive widget resizes, zooms and pans; every render + re-maps through `fdw::Map`. Drawings come from text specs + (`"trend 10 655 200 672.5 red; hline 650 blue dash"`) via + `fdw::parse_specs`, or interactively in generated apps: keys + T H V R L A X F D M E S select trend/hline/vline/rect/ellipse/arrow/ + text/fib/freehand/measure/erase/select-move, Esc returns to pan, + Delete removes the selected drawing, and a text tool click asks for + the label in a dialog. The generator embeds the header + (`src/drawings_embedded.h` via `tools/embed_drawings.sh`) and copies + `drawings.h` into StockChart projects, like indicators.h. +- New StockChart properties: `scale-mode` (Price|Percent — percent + relabels the right axis as change from the first visible close), + `compare-files` (comma-separated CSVs of other tickers, aligned by + timestamp and rebased onto the first visible close, drawn as + coloured lines with a legend), and `drawings` (semicolon-separated + specs). The bottom axis plots UTC time from `window_start` (date + shown at day changes); gridlines use nice 1/2/5 steps. +- View: the mouse wheel zooms around the cursor (`zoom_at(factor, + frac)` in code), drag pans with no tool active, `reset_view()` + restores the max-candles window. The designer preview zooms with + the wheel too. +- Testing: `tests/drawings_test.cpp` renders every kind and checks + palette ink, verifies a trend line's endpoints remap at 1x and 2x + plot sizes, and exercises hit testing. Matrix case 16 + (`16_stockchart_pro`, a 1024x768 window) designs a percent-mode + chart with a QQQ comparison (`tests/data/QQQ_fake_long.csv`, same + timestamp grid as SPY, made by `tests/make_compare_data.py`), SMA + + RSI and 7 annotations; `tests/chart_pro_render_test.cpp` asserts + candle/comparison/arrow/fib ink, percent and time axis labels, + zoom in/out/clamp/reset, and that arrow + fib still draw after the + widget is resized smaller. `tests/chart_screenshot.cpp` captures + the generated app at exactly 1024x768 to + `tests/out/chart_pro_1024x768.png`. Full regression after the + change: designer self-test PASS, matrix 21/21. +- Gotcha found by the matrix: the generated chart's event handlers + must not be named `on_press`/`on_release` — those are the widget + stub dispatchers. They are `on_chart_press` etc. + +## Layout of this workspace + +- `original/` — the Python source, read-only reference (do not run it here). +- `ui/designer.ui` — the designer's own main window (Glade). +- `src/main.cpp` — entry point; loads the .ui via GtkBuilder. +- `Makefile` — builds `./fdesigner`. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b07f395 --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +CXX ?= g++ +CXXFLAGS ?= -O2 -Wall -Wextra -std=c++17 +GTKFLAGS := $(shell pkg-config --cflags gtk+-3.0) +GTKLIBS := $(shell pkg-config --libs gtk+-3.0) + +fdesigner: src/main.cpp src/generator.cpp src/project_io.cpp src/model.h src/props.h src/generator.h src/project_io.h src/indicators.h src/indicators_embedded.h src/drawings.h src/drawings_embedded.h ui/designer.ui + $(CXX) $(CXXFLAGS) -o $@ src/main.cpp src/generator.cpp src/project_io.cpp $(GTKFLAGS) $(GTKLIBS) + +src/indicators_embedded.h: src/indicators.h tools/embed_indicators.sh + sh tools/embed_indicators.sh src/indicators.h $@ + +src/drawings_embedded.h: src/drawings.h tools/embed_drawings.sh + sh tools/embed_drawings.sh src/drawings.h $@ + +clean: + rm -f fdesigner + +.PHONY: clean diff --git a/chart_pro_1024x768.png b/chart_pro_1024x768.png new file mode 100644 index 0000000..87fbff7 Binary files /dev/null and b/chart_pro_1024x768.png differ diff --git a/indicators_contact_sheet.png b/indicators_contact_sheet.png new file mode 100644 index 0000000..674f48a Binary files /dev/null and b/indicators_contact_sheet.png differ diff --git a/src/drawings.h b/src/drawings.h new file mode 100644 index 0000000..b061c0d --- /dev/null +++ b/src/drawings.h @@ -0,0 +1,597 @@ +// drawings.h — chart annotation module for the StockChart widget. +// +// One self-contained header (namespace fdw), in the same spirit as +// indicators.h. A Drawing is stored in DATA coordinates — an anchor is +// (bar index, price) — never in pixels, so annotations stay glued to +// the candles they were drawn on through widget resizes, zooms and +// pans: every render re-maps data -> pixels through the current Map. +// +// Kinds: trend line, ray, horizontal line, vertical line, rectangle, +// ellipse, arrow, text label, Fibonacci retracement, freehand stroke, +// and a measure tool (delta price / percent / bars between two points). +// +// Drawings can be created three ways: interactively (the generated +// StockChart's mouse tools build them), from a text spec via +// parse_spec()/parse_specs(), or directly in code. Spec grammar +// (semicolon-separated in parse_specs): +// +// trend x1 y1 x2 y2 [colour] [dash] +// ray x1 y1 x2 y2 [colour] [dash] +// arrow x1 y1 x2 y2 [colour] [dash] +// rect x1 y1 x2 y2 [colour] [dash] +// ellipse x1 y1 x2 y2 [colour] [dash] +// fib x1 y1 x2 y2 [colour] +// measure x1 y1 x2 y2 [colour] +// hline price [colour] [dash] +// vline bar [colour] [dash] +// text x y [colour] label words... +// free x1 y1 x2 y2 ... [colour] +// +// x is a bar index (fractional allowed), y a price. Colours by name: +// blue red green orange purple teal magenta brown black slate. +#pragma once + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace fdw { + +// One anchor point in data coordinates. +struct Anchor { + double bar = 0; // bar index (fractional = between bars) + double price = 0; // price +}; + +enum class Kind { + Trend, + Ray, + HLine, + VLine, + Rect, + Ellipse, + Arrow, + Text, + Fib, + Freehand, + Measure +}; + +struct Drawing { + Kind kind = Kind::Trend; + std::vector pts; // 1 for hline/vline/text, 2 for most, + // N for freehand + int color = 0; // palette index + double width = 1.5; // line width, px + bool dash = false; + std::string text; // Text kind only +}; + +// Drawing palette (strong, saturated colours that read on white). +inline const double kPalette[][3] = { + {0.10, 0.25, 0.80}, // 0 blue + {0.85, 0.10, 0.20}, // 1 red + {0.00, 0.55, 0.15}, // 2 green + {0.95, 0.50, 0.05}, // 3 orange + {0.55, 0.20, 0.75}, // 4 purple + {0.00, 0.60, 0.60}, // 5 teal + {0.85, 0.20, 0.65}, // 6 magenta + {0.55, 0.35, 0.15}, // 7 brown + {0.10, 0.10, 0.10}, // 8 black + {0.35, 0.40, 0.50}, // 9 slate +}; +inline const int kPaletteSize = 10; + +inline int color_index_of(const std::string& name) { + static const char* kNames[] = {"blue", "red", "green", "orange", + "purple", "teal", "magenta", "brown", + "black", "slate"}; + std::string low; + for (char c : name) low += (char)std::tolower((unsigned char)c); + for (int i = 0; i < kPaletteSize; ++i) + if (low == kNames[i]) return i; + return -1; +} + +inline const char* kind_name(Kind k) { + switch (k) { + case Kind::Trend: return "trend"; + case Kind::Ray: return "ray"; + case Kind::HLine: return "hline"; + case Kind::VLine: return "vline"; + case Kind::Rect: return "rect"; + case Kind::Ellipse: return "ellipse"; + case Kind::Arrow: return "arrow"; + case Kind::Text: return "text"; + case Kind::Fib: return "fib"; + case Kind::Freehand: return "freehand"; + case Kind::Measure: return "measure"; + } + return "?"; +} + +inline bool kind_from_name(const std::string& s, Kind& out) { + std::string low; + for (char c : s) low += (char)std::tolower((unsigned char)c); + if (low == "trend") out = Kind::Trend; + else if (low == "ray") out = Kind::Ray; + else if (low == "hline" || low == "horizontal") out = Kind::HLine; + else if (low == "vline" || low == "vertical") out = Kind::VLine; + else if (low == "rect" || low == "rectangle") out = Kind::Rect; + else if (low == "ellipse" || low == "oval") out = Kind::Ellipse; + else if (low == "arrow") out = Kind::Arrow; + else if (low == "text" || low == "label") out = Kind::Text; + else if (low == "fib" || low == "fibonacci") out = Kind::Fib; + else if (low == "free" || low == "freehand") out = Kind::Freehand; + else if (low == "measure") out = Kind::Measure; + else return false; + return true; +} + +// The data -> pixel mapping for one render. x/y/w/h is the price-pane +// plot rectangle in pixels; begin/count is the visible window in bar +// units; lo/hi is the visible price range. Bar centres sit at +// (bar - begin + 0.5) slots, matching the candle layout. +struct Map { + double x = 0, y = 0, w = 0, h = 0; + double begin = 0, count = 1; + double lo = 0, hi = 1; + + double px(double bar) const { + return x + (bar - begin + 0.5) * (w / count); + } + double py(double price) const { + return y + (hi - price) / (hi - lo) * h; + } + double bar_at(double px_) const { + return begin + (px_ - x) / (w / count) - 0.5; + } + double price_at(double py_) const { + return hi - (py_ - y) / h * (hi - lo); + } +}; + +namespace detail { + +inline void set_style(cairo_t* cr, const Drawing& d, bool selected) { + int ci = ((d.color % kPaletteSize) + kPaletteSize) % kPaletteSize; + const double* c = kPalette[ci]; + cairo_set_source_rgba(cr, c[0], c[1], c[2], 0.92); + cairo_set_line_width(cr, d.width + (selected ? 1.0 : 0.0)); + if (d.dash) { + static const double kDash[] = {6, 4}; + cairo_set_dash(cr, kDash, 2, 0); + } else { + cairo_set_dash(cr, nullptr, 0, 0); + } +} + +inline double seg_dist(double px, double py, double x1, double y1, + double x2, double y2) { + double dx = x2 - x1, dy = y2 - y1; + double len2 = dx * dx + dy * dy; + double t = len2 > 0 ? ((px - x1) * dx + (py - y1) * dy) / len2 : 0; + t = std::max(0.0, std::min(1.0, t)); + double ex = x1 + t * dx - px, ey = y1 + t * dy - py; + return std::sqrt(ex * ex + ey * ey); +} + +// A small filled label with a light backing box (text + measure). +inline void label_box(cairo_t* cr, double x, double y, + const std::string& s, const double* col) { + cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL, + CAIRO_FONT_WEIGHT_NORMAL); + cairo_set_font_size(cr, 11); + cairo_text_extents_t te; + cairo_text_extents(cr, s.c_str(), &te); + cairo_set_source_rgba(cr, 1, 1, 1, 0.85); + cairo_rectangle(cr, x - 3, y - te.height - 4, te.width + 6, + te.height + 5); + cairo_fill(cr); + cairo_set_source_rgba(cr, col[0], col[1], col[2], 1.0); + cairo_move_to(cr, x, y - 3); + cairo_show_text(cr, s.c_str()); +} + +} // namespace detail + +// Render one drawing. Clips to the plot rectangle (rays and lines may +// run off the edge). `selected` draws it slightly bolder. +inline void draw(cairo_t* cr, const Map& m, const Drawing& d, + bool selected = false) { + if (d.pts.empty() || m.w < 2 || m.h < 2 || m.hi - m.lo < 1e-12) + return; + int ci = ((d.color % kPaletteSize) + kPaletteSize) % kPaletteSize; + const double* col = kPalette[ci]; + cairo_save(cr); + cairo_rectangle(cr, m.x - 40, m.y - 40, m.w + 80, m.h + 80); + cairo_clip(cr); + detail::set_style(cr, d, selected); + auto X = [&](const Anchor& a) { return m.px(a.bar); }; + auto Y = [&](const Anchor& a) { return m.py(a.price); }; + switch (d.kind) { + case Kind::Trend: + case Kind::Measure: { + if (d.pts.size() < 2) break; + cairo_move_to(cr, X(d.pts[0]), Y(d.pts[0])); + cairo_line_to(cr, X(d.pts[1]), Y(d.pts[1])); + cairo_stroke(cr); + if (d.kind == Kind::Measure) { + double dp = d.pts[1].price - d.pts[0].price; + double pct = d.pts[0].price != 0 + ? dp / std::fabs(d.pts[0].price) * 100 + : 0; + long bars = std::lround(d.pts[1].bar - d.pts[0].bar); + char buf[96]; + std::snprintf(buf, sizeof buf, "%+.2f (%+.2f%%) / %ld bars", + dp, pct, bars); + detail::label_box(cr, + (X(d.pts[0]) + X(d.pts[1])) / 2, + (Y(d.pts[0]) + Y(d.pts[1])) / 2 - 4, + buf, col); + } + break; + } + case Kind::Ray: { + if (d.pts.size() < 2) break; + double x1 = X(d.pts[0]), y1 = Y(d.pts[0]); + double x2 = x1 + (X(d.pts[1]) - x1) * 1000; + double y2 = y1 + (Y(d.pts[1]) - y1) * 1000; + cairo_move_to(cr, x1, y1); + cairo_line_to(cr, x2, y2); + cairo_stroke(cr); + break; + } + case Kind::Arrow: { + if (d.pts.size() < 2) break; + double x1 = X(d.pts[0]), y1 = Y(d.pts[0]); + double x2 = X(d.pts[1]), y2 = Y(d.pts[1]); + cairo_move_to(cr, x1, y1); + cairo_line_to(cr, x2, y2); + cairo_stroke(cr); + double ang = std::atan2(y2 - y1, x2 - x1); + double alen = 11; + cairo_move_to(cr, x2, y2); + cairo_line_to(cr, x2 - alen * std::cos(ang - 0.42), + y2 - alen * std::sin(ang - 0.42)); + cairo_move_to(cr, x2, y2); + cairo_line_to(cr, x2 - alen * std::cos(ang + 0.42), + y2 - alen * std::sin(ang + 0.42)); + cairo_stroke(cr); + break; + } + case Kind::HLine: { + double y = Y(d.pts[0]); + cairo_move_to(cr, m.x, y); + cairo_line_to(cr, m.x + m.w, y); + cairo_stroke(cr); + char buf[32]; + std::snprintf(buf, sizeof buf, "%.2f", d.pts[0].price); + detail::label_box(cr, m.x + 4, y - 2, buf, col); + break; + } + case Kind::VLine: { + double x = X(d.pts[0]); + cairo_move_to(cr, x, m.y); + cairo_line_to(cr, x, m.y + m.h); + cairo_stroke(cr); + break; + } + case Kind::Rect: + case Kind::Ellipse: { + if (d.pts.size() < 2) break; + double x1 = X(d.pts[0]), y1 = Y(d.pts[0]); + double x2 = X(d.pts[1]), y2 = Y(d.pts[1]); + double rx = std::min(x1, x2), ry = std::min(y1, y2); + double rw = std::fabs(x2 - x1), rh = std::fabs(y2 - y1); + cairo_set_source_rgba(cr, col[0], col[1], col[2], 0.08); + if (d.kind == Kind::Rect) { + cairo_rectangle(cr, rx, ry, rw, rh); + cairo_fill(cr); + detail::set_style(cr, d, selected); + cairo_rectangle(cr, rx, ry, rw, rh); + cairo_stroke(cr); + } else { + cairo_save(cr); + cairo_translate(cr, rx + rw / 2, ry + rh / 2); + cairo_scale(cr, std::max(rw, 0.1), std::max(rh, 0.1)); + cairo_arc(cr, 0, 0, 0.5, 0, 2 * M_PI); + cairo_restore(cr); + cairo_fill(cr); + detail::set_style(cr, d, selected); + cairo_save(cr); + cairo_translate(cr, rx + rw / 2, ry + rh / 2); + cairo_scale(cr, std::max(rw, 0.1), std::max(rh, 0.1)); + cairo_arc(cr, 0, 0, 0.5, 0, 2 * M_PI); + cairo_restore(cr); + cairo_stroke(cr); + } + break; + } + case Kind::Text: { + if (d.text.empty()) break; + detail::label_box(cr, X(d.pts[0]) + 2, Y(d.pts[0]) - 2, + d.text, col); + // Anchor dot. + cairo_set_source_rgba(cr, col[0], col[1], col[2], 1.0); + cairo_arc(cr, X(d.pts[0]), Y(d.pts[0]), 2.5, 0, 2 * M_PI); + cairo_fill(cr); + break; + } + case Kind::Fib: { + if (d.pts.size() < 2) break; + static const double kLevels[] = {0, 0.236, 0.382, 0.5, + 0.618, 0.786, 1.0}; + double x1 = X(d.pts[0]); + cairo_set_line_width(cr, 1.0); + for (double lv : kLevels) { + double price = + d.pts[0].price + (d.pts[1].price - d.pts[0].price) * lv; + double y = m.py(price); + static const double kDash[] = {4, 3}; + cairo_set_dash(cr, kDash, 2, 0); + cairo_set_source_rgba(cr, col[0], col[1], col[2], 0.75); + cairo_move_to(cr, x1, y); + cairo_line_to(cr, m.x + m.w, y); + cairo_stroke(cr); + cairo_set_dash(cr, nullptr, 0, 0); + char buf[48]; + std::snprintf(buf, sizeof buf, "%.1f%% %.2f", lv * 100, + price); + detail::label_box(cr, x1 + 4, y - 2, buf, col); + } + // Connector between the anchors. + detail::set_style(cr, d, selected); + cairo_move_to(cr, X(d.pts[0]), Y(d.pts[0])); + cairo_line_to(cr, X(d.pts[1]), Y(d.pts[1])); + cairo_stroke(cr); + break; + } + case Kind::Freehand: { + if (d.pts.size() < 2) break; + cairo_move_to(cr, X(d.pts[0]), Y(d.pts[0])); + for (size_t i = 1; i < d.pts.size(); ++i) + cairo_line_to(cr, X(d.pts[i]), Y(d.pts[i])); + cairo_stroke(cr); + break; + } + } + if (selected) { + // Corner dots on the anchors. + cairo_set_source_rgba(cr, col[0], col[1], col[2], 1.0); + for (const Anchor& a : d.pts) { + cairo_arc(cr, X(a), Y(a), 3, 0, 2 * M_PI); + cairo_fill(cr); + } + } + cairo_restore(cr); +} + +// Is pixel (px, py) within `tol` px of the drawing? Used by the +// select/move and erase tools. +inline bool hit_test(const Map& m, const Drawing& d, double px, + double py, double tol = 6) { + if (d.pts.empty()) return false; + auto X = [&](const Anchor& a) { return m.px(a.bar); }; + auto Y = [&](const Anchor& a) { return m.py(a.price); }; + switch (d.kind) { + case Kind::Trend: + case Kind::Ray: + case Kind::Arrow: + case Kind::Measure: + if (d.pts.size() < 2) return false; + return detail::seg_dist(px, py, X(d.pts[0]), Y(d.pts[0]), + X(d.pts[1]), Y(d.pts[1])) <= tol; + case Kind::HLine: + return std::fabs(py - Y(d.pts[0])) <= tol && px >= m.x && + px <= m.x + m.w; + case Kind::VLine: + return std::fabs(px - X(d.pts[0])) <= tol && py >= m.y && + py <= m.y + m.h; + case Kind::Rect: + case Kind::Ellipse: { + if (d.pts.size() < 2) return false; + double x1 = X(d.pts[0]), y1 = Y(d.pts[0]); + double x2 = X(d.pts[1]), y2 = Y(d.pts[1]); + bool inside = px >= std::min(x1, x2) && px <= std::max(x1, x2) && + py >= std::min(y1, y2) && py <= std::max(y1, y2); + bool near_edge = + detail::seg_dist(px, py, x1, y1, x2, y1) <= tol || + detail::seg_dist(px, py, x2, y1, x2, y2) <= tol || + detail::seg_dist(px, py, x2, y2, x1, y2) <= tol || + detail::seg_dist(px, py, x1, y2, x1, y1) <= tol; + return inside || near_edge; + } + case Kind::Text: { + double x = X(d.pts[0]), y = Y(d.pts[0]); + double w = 10 + (double)d.text.size() * 6.5; + return px >= x - 4 && px <= x + w && py >= y - 16 && + py <= y + 4; + } + case Kind::Fib: { + if (d.pts.size() < 2) return false; + static const double kLevels[] = {0, 0.236, 0.382, 0.5, + 0.618, 0.786, 1.0}; + for (double lv : kLevels) { + double price = + d.pts[0].price + (d.pts[1].price - d.pts[0].price) * lv; + if (std::fabs(py - m.py(price)) <= tol && px >= X(d.pts[0])) + return true; + } + return false; + } + case Kind::Freehand: + for (size_t i = 1; i < d.pts.size(); ++i) + if (detail::seg_dist(px, py, X(d.pts[i - 1]), + Y(d.pts[i - 1]), X(d.pts[i]), + Y(d.pts[i])) <= tol) + return true; + return d.pts.size() == 1 && + std::hypot(px - X(d.pts[0]), py - Y(d.pts[0])) <= tol; + } + return false; +} + +// Shift every anchor by a data-space delta (select/move drags). +inline void move(Drawing& d, double dbar, double dprice) { + for (Anchor& a : d.pts) { + a.bar += dbar; + a.price += dprice; + } + if (d.kind == Kind::HLine) + for (Anchor& a : d.pts) a.bar -= dbar; // bar is meaningless + if (d.kind == Kind::VLine) + for (Anchor& a : d.pts) a.price -= dprice; +} + +// Parse one spec (grammar in the header comment). +inline std::optional parse_spec(const std::string& spec) { + // Tokenize on whitespace, remembering where the tail starts (text). + std::vector tok; + std::vector tok_pos; + size_t i = 0; + while (i < spec.size()) { + while (i < spec.size() && + (spec[i] == ' ' || spec[i] == '\t')) + ++i; + if (i >= spec.size()) break; + size_t s = i; + while (i < spec.size() && spec[i] != ' ' && spec[i] != '\t') + ++i; + tok.push_back(spec.substr(s, i - s)); + tok_pos.push_back(s); + } + if (tok.empty()) return std::nullopt; + Drawing d; + if (!kind_from_name(tok[0], d.kind)) return std::nullopt; + size_t t = 1; + auto number = [&](double& v) { + if (t >= tok.size()) return false; + char* end = nullptr; + v = std::strtod(tok[t].c_str(), &end); + if (end == tok[t].c_str()) return false; + ++t; + return true; + }; + auto anchor = [&](Anchor& a) { + return number(a.bar) && number(a.price); + }; + auto tail_options = [&]() { + while (t < tok.size()) { + int ci = color_index_of(tok[t]); + if (ci >= 0) { + d.color = ci; + ++t; + } else if (tok[t] == "dash") { + d.dash = true; + ++t; + } else { + break; + } + } + }; + switch (d.kind) { + case Kind::HLine: { + double p; + if (!number(p)) return std::nullopt; + d.pts.push_back({0, p}); + tail_options(); + return d; + } + case Kind::VLine: { + double b; + if (!number(b)) return std::nullopt; + d.pts.push_back({b, 0}); + tail_options(); + return d; + } + case Kind::Text: { + Anchor a; + if (!anchor(a)) return std::nullopt; + d.pts.push_back(a); + tail_options(); + if (t < tok.size()) + d.text = spec.substr(tok_pos[t]); + if (d.text.empty()) return std::nullopt; + return d; + } + case Kind::Freehand: { + while (t < tok.size()) { + char* end = nullptr; + double v = std::strtod(tok[t].c_str(), &end); + if (end == tok[t].c_str()) break; + Anchor a; + a.bar = v; + ++t; + if (!number(a.price)) return std::nullopt; + d.pts.push_back(a); + } + tail_options(); + if (d.pts.size() < 2) return std::nullopt; + return d; + } + default: { // two-anchor kinds + Anchor a, b; + if (!anchor(a) || !anchor(b)) return std::nullopt; + d.pts.push_back(a); + d.pts.push_back(b); + tail_options(); + return d; + } + } +} + +// Parse a semicolon-separated list of specs; bad entries are skipped. +inline std::vector parse_specs(const std::string& specs) { + std::vector out; + size_t start = 0; + for (size_t i = 0; i <= specs.size(); ++i) { + if (i == specs.size() || specs[i] == ';') { + auto d = parse_spec(specs.substr(start, i - start)); + if (d) out.push_back(*d); + start = i + 1; + } + } + return out; +} + +// Canonical spec for a drawing (parse_spec(to_spec(d)) round-trips). +inline std::string to_spec(const Drawing& d) { + static const char* kNames[] = {"blue", "red", "green", "orange", + "purple", "teal", "magenta", "brown", + "black", "slate"}; + int ci = ((d.color % kPaletteSize) + kPaletteSize) % kPaletteSize; + char buf[64]; + std::string s = kind_name(d.kind); + auto num = [&](double v) { + std::snprintf(buf, sizeof buf, " %g", v); + s += buf; + }; + if (d.kind == Kind::HLine) { + num(d.pts.empty() ? 0 : d.pts[0].price); + } else if (d.kind == Kind::VLine) { + num(d.pts.empty() ? 0 : d.pts[0].bar); + } else { + for (const Anchor& a : d.pts) { + num(a.bar); + num(a.price); + } + } + s += " "; + s += kNames[ci]; + if (d.dash) s += " dash"; + if (d.kind == Kind::Text) { + s += " "; + s += d.text; + } + return s; +} + +} // namespace fdw diff --git a/src/drawings_embedded.h b/src/drawings_embedded.h new file mode 100644 index 0000000..9fc7e24 --- /dev/null +++ b/src/drawings_embedded.h @@ -0,0 +1,602 @@ +// Generated from src/drawings.h by tools/embed_drawings.sh. +// Do not edit; run 'make' to regenerate. +#pragma once +static const char kDrawingsHeader[] = R"FDDRAWINGS9b2c( +// drawings.h — chart annotation module for the StockChart widget. +// +// One self-contained header (namespace fdw), in the same spirit as +// indicators.h. A Drawing is stored in DATA coordinates — an anchor is +// (bar index, price) — never in pixels, so annotations stay glued to +// the candles they were drawn on through widget resizes, zooms and +// pans: every render re-maps data -> pixels through the current Map. +// +// Kinds: trend line, ray, horizontal line, vertical line, rectangle, +// ellipse, arrow, text label, Fibonacci retracement, freehand stroke, +// and a measure tool (delta price / percent / bars between two points). +// +// Drawings can be created three ways: interactively (the generated +// StockChart's mouse tools build them), from a text spec via +// parse_spec()/parse_specs(), or directly in code. Spec grammar +// (semicolon-separated in parse_specs): +// +// trend x1 y1 x2 y2 [colour] [dash] +// ray x1 y1 x2 y2 [colour] [dash] +// arrow x1 y1 x2 y2 [colour] [dash] +// rect x1 y1 x2 y2 [colour] [dash] +// ellipse x1 y1 x2 y2 [colour] [dash] +// fib x1 y1 x2 y2 [colour] +// measure x1 y1 x2 y2 [colour] +// hline price [colour] [dash] +// vline bar [colour] [dash] +// text x y [colour] label words... +// free x1 y1 x2 y2 ... [colour] +// +// x is a bar index (fractional allowed), y a price. Colours by name: +// blue red green orange purple teal magenta brown black slate. +#pragma once + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace fdw { + +// One anchor point in data coordinates. +struct Anchor { + double bar = 0; // bar index (fractional = between bars) + double price = 0; // price +}; + +enum class Kind { + Trend, + Ray, + HLine, + VLine, + Rect, + Ellipse, + Arrow, + Text, + Fib, + Freehand, + Measure +}; + +struct Drawing { + Kind kind = Kind::Trend; + std::vector pts; // 1 for hline/vline/text, 2 for most, + // N for freehand + int color = 0; // palette index + double width = 1.5; // line width, px + bool dash = false; + std::string text; // Text kind only +}; + +// Drawing palette (strong, saturated colours that read on white). +inline const double kPalette[][3] = { + {0.10, 0.25, 0.80}, // 0 blue + {0.85, 0.10, 0.20}, // 1 red + {0.00, 0.55, 0.15}, // 2 green + {0.95, 0.50, 0.05}, // 3 orange + {0.55, 0.20, 0.75}, // 4 purple + {0.00, 0.60, 0.60}, // 5 teal + {0.85, 0.20, 0.65}, // 6 magenta + {0.55, 0.35, 0.15}, // 7 brown + {0.10, 0.10, 0.10}, // 8 black + {0.35, 0.40, 0.50}, // 9 slate +}; +inline const int kPaletteSize = 10; + +inline int color_index_of(const std::string& name) { + static const char* kNames[] = {"blue", "red", "green", "orange", + "purple", "teal", "magenta", "brown", + "black", "slate"}; + std::string low; + for (char c : name) low += (char)std::tolower((unsigned char)c); + for (int i = 0; i < kPaletteSize; ++i) + if (low == kNames[i]) return i; + return -1; +} + +inline const char* kind_name(Kind k) { + switch (k) { + case Kind::Trend: return "trend"; + case Kind::Ray: return "ray"; + case Kind::HLine: return "hline"; + case Kind::VLine: return "vline"; + case Kind::Rect: return "rect"; + case Kind::Ellipse: return "ellipse"; + case Kind::Arrow: return "arrow"; + case Kind::Text: return "text"; + case Kind::Fib: return "fib"; + case Kind::Freehand: return "freehand"; + case Kind::Measure: return "measure"; + } + return "?"; +} + +inline bool kind_from_name(const std::string& s, Kind& out) { + std::string low; + for (char c : s) low += (char)std::tolower((unsigned char)c); + if (low == "trend") out = Kind::Trend; + else if (low == "ray") out = Kind::Ray; + else if (low == "hline" || low == "horizontal") out = Kind::HLine; + else if (low == "vline" || low == "vertical") out = Kind::VLine; + else if (low == "rect" || low == "rectangle") out = Kind::Rect; + else if (low == "ellipse" || low == "oval") out = Kind::Ellipse; + else if (low == "arrow") out = Kind::Arrow; + else if (low == "text" || low == "label") out = Kind::Text; + else if (low == "fib" || low == "fibonacci") out = Kind::Fib; + else if (low == "free" || low == "freehand") out = Kind::Freehand; + else if (low == "measure") out = Kind::Measure; + else return false; + return true; +} + +// The data -> pixel mapping for one render. x/y/w/h is the price-pane +// plot rectangle in pixels; begin/count is the visible window in bar +// units; lo/hi is the visible price range. Bar centres sit at +// (bar - begin + 0.5) slots, matching the candle layout. +struct Map { + double x = 0, y = 0, w = 0, h = 0; + double begin = 0, count = 1; + double lo = 0, hi = 1; + + double px(double bar) const { + return x + (bar - begin + 0.5) * (w / count); + } + double py(double price) const { + return y + (hi - price) / (hi - lo) * h; + } + double bar_at(double px_) const { + return begin + (px_ - x) / (w / count) - 0.5; + } + double price_at(double py_) const { + return hi - (py_ - y) / h * (hi - lo); + } +}; + +namespace detail { + +inline void set_style(cairo_t* cr, const Drawing& d, bool selected) { + int ci = ((d.color % kPaletteSize) + kPaletteSize) % kPaletteSize; + const double* c = kPalette[ci]; + cairo_set_source_rgba(cr, c[0], c[1], c[2], 0.92); + cairo_set_line_width(cr, d.width + (selected ? 1.0 : 0.0)); + if (d.dash) { + static const double kDash[] = {6, 4}; + cairo_set_dash(cr, kDash, 2, 0); + } else { + cairo_set_dash(cr, nullptr, 0, 0); + } +} + +inline double seg_dist(double px, double py, double x1, double y1, + double x2, double y2) { + double dx = x2 - x1, dy = y2 - y1; + double len2 = dx * dx + dy * dy; + double t = len2 > 0 ? ((px - x1) * dx + (py - y1) * dy) / len2 : 0; + t = std::max(0.0, std::min(1.0, t)); + double ex = x1 + t * dx - px, ey = y1 + t * dy - py; + return std::sqrt(ex * ex + ey * ey); +} + +// A small filled label with a light backing box (text + measure). +inline void label_box(cairo_t* cr, double x, double y, + const std::string& s, const double* col) { + cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL, + CAIRO_FONT_WEIGHT_NORMAL); + cairo_set_font_size(cr, 11); + cairo_text_extents_t te; + cairo_text_extents(cr, s.c_str(), &te); + cairo_set_source_rgba(cr, 1, 1, 1, 0.85); + cairo_rectangle(cr, x - 3, y - te.height - 4, te.width + 6, + te.height + 5); + cairo_fill(cr); + cairo_set_source_rgba(cr, col[0], col[1], col[2], 1.0); + cairo_move_to(cr, x, y - 3); + cairo_show_text(cr, s.c_str()); +} + +} // namespace detail + +// Render one drawing. Clips to the plot rectangle (rays and lines may +// run off the edge). `selected` draws it slightly bolder. +inline void draw(cairo_t* cr, const Map& m, const Drawing& d, + bool selected = false) { + if (d.pts.empty() || m.w < 2 || m.h < 2 || m.hi - m.lo < 1e-12) + return; + int ci = ((d.color % kPaletteSize) + kPaletteSize) % kPaletteSize; + const double* col = kPalette[ci]; + cairo_save(cr); + cairo_rectangle(cr, m.x - 40, m.y - 40, m.w + 80, m.h + 80); + cairo_clip(cr); + detail::set_style(cr, d, selected); + auto X = [&](const Anchor& a) { return m.px(a.bar); }; + auto Y = [&](const Anchor& a) { return m.py(a.price); }; + switch (d.kind) { + case Kind::Trend: + case Kind::Measure: { + if (d.pts.size() < 2) break; + cairo_move_to(cr, X(d.pts[0]), Y(d.pts[0])); + cairo_line_to(cr, X(d.pts[1]), Y(d.pts[1])); + cairo_stroke(cr); + if (d.kind == Kind::Measure) { + double dp = d.pts[1].price - d.pts[0].price; + double pct = d.pts[0].price != 0 + ? dp / std::fabs(d.pts[0].price) * 100 + : 0; + long bars = std::lround(d.pts[1].bar - d.pts[0].bar); + char buf[96]; + std::snprintf(buf, sizeof buf, "%+.2f (%+.2f%%) / %ld bars", + dp, pct, bars); + detail::label_box(cr, + (X(d.pts[0]) + X(d.pts[1])) / 2, + (Y(d.pts[0]) + Y(d.pts[1])) / 2 - 4, + buf, col); + } + break; + } + case Kind::Ray: { + if (d.pts.size() < 2) break; + double x1 = X(d.pts[0]), y1 = Y(d.pts[0]); + double x2 = x1 + (X(d.pts[1]) - x1) * 1000; + double y2 = y1 + (Y(d.pts[1]) - y1) * 1000; + cairo_move_to(cr, x1, y1); + cairo_line_to(cr, x2, y2); + cairo_stroke(cr); + break; + } + case Kind::Arrow: { + if (d.pts.size() < 2) break; + double x1 = X(d.pts[0]), y1 = Y(d.pts[0]); + double x2 = X(d.pts[1]), y2 = Y(d.pts[1]); + cairo_move_to(cr, x1, y1); + cairo_line_to(cr, x2, y2); + cairo_stroke(cr); + double ang = std::atan2(y2 - y1, x2 - x1); + double alen = 11; + cairo_move_to(cr, x2, y2); + cairo_line_to(cr, x2 - alen * std::cos(ang - 0.42), + y2 - alen * std::sin(ang - 0.42)); + cairo_move_to(cr, x2, y2); + cairo_line_to(cr, x2 - alen * std::cos(ang + 0.42), + y2 - alen * std::sin(ang + 0.42)); + cairo_stroke(cr); + break; + } + case Kind::HLine: { + double y = Y(d.pts[0]); + cairo_move_to(cr, m.x, y); + cairo_line_to(cr, m.x + m.w, y); + cairo_stroke(cr); + char buf[32]; + std::snprintf(buf, sizeof buf, "%.2f", d.pts[0].price); + detail::label_box(cr, m.x + 4, y - 2, buf, col); + break; + } + case Kind::VLine: { + double x = X(d.pts[0]); + cairo_move_to(cr, x, m.y); + cairo_line_to(cr, x, m.y + m.h); + cairo_stroke(cr); + break; + } + case Kind::Rect: + case Kind::Ellipse: { + if (d.pts.size() < 2) break; + double x1 = X(d.pts[0]), y1 = Y(d.pts[0]); + double x2 = X(d.pts[1]), y2 = Y(d.pts[1]); + double rx = std::min(x1, x2), ry = std::min(y1, y2); + double rw = std::fabs(x2 - x1), rh = std::fabs(y2 - y1); + cairo_set_source_rgba(cr, col[0], col[1], col[2], 0.08); + if (d.kind == Kind::Rect) { + cairo_rectangle(cr, rx, ry, rw, rh); + cairo_fill(cr); + detail::set_style(cr, d, selected); + cairo_rectangle(cr, rx, ry, rw, rh); + cairo_stroke(cr); + } else { + cairo_save(cr); + cairo_translate(cr, rx + rw / 2, ry + rh / 2); + cairo_scale(cr, std::max(rw, 0.1), std::max(rh, 0.1)); + cairo_arc(cr, 0, 0, 0.5, 0, 2 * M_PI); + cairo_restore(cr); + cairo_fill(cr); + detail::set_style(cr, d, selected); + cairo_save(cr); + cairo_translate(cr, rx + rw / 2, ry + rh / 2); + cairo_scale(cr, std::max(rw, 0.1), std::max(rh, 0.1)); + cairo_arc(cr, 0, 0, 0.5, 0, 2 * M_PI); + cairo_restore(cr); + cairo_stroke(cr); + } + break; + } + case Kind::Text: { + if (d.text.empty()) break; + detail::label_box(cr, X(d.pts[0]) + 2, Y(d.pts[0]) - 2, + d.text, col); + // Anchor dot. + cairo_set_source_rgba(cr, col[0], col[1], col[2], 1.0); + cairo_arc(cr, X(d.pts[0]), Y(d.pts[0]), 2.5, 0, 2 * M_PI); + cairo_fill(cr); + break; + } + case Kind::Fib: { + if (d.pts.size() < 2) break; + static const double kLevels[] = {0, 0.236, 0.382, 0.5, + 0.618, 0.786, 1.0}; + double x1 = X(d.pts[0]); + cairo_set_line_width(cr, 1.0); + for (double lv : kLevels) { + double price = + d.pts[0].price + (d.pts[1].price - d.pts[0].price) * lv; + double y = m.py(price); + static const double kDash[] = {4, 3}; + cairo_set_dash(cr, kDash, 2, 0); + cairo_set_source_rgba(cr, col[0], col[1], col[2], 0.75); + cairo_move_to(cr, x1, y); + cairo_line_to(cr, m.x + m.w, y); + cairo_stroke(cr); + cairo_set_dash(cr, nullptr, 0, 0); + char buf[48]; + std::snprintf(buf, sizeof buf, "%.1f%% %.2f", lv * 100, + price); + detail::label_box(cr, x1 + 4, y - 2, buf, col); + } + // Connector between the anchors. + detail::set_style(cr, d, selected); + cairo_move_to(cr, X(d.pts[0]), Y(d.pts[0])); + cairo_line_to(cr, X(d.pts[1]), Y(d.pts[1])); + cairo_stroke(cr); + break; + } + case Kind::Freehand: { + if (d.pts.size() < 2) break; + cairo_move_to(cr, X(d.pts[0]), Y(d.pts[0])); + for (size_t i = 1; i < d.pts.size(); ++i) + cairo_line_to(cr, X(d.pts[i]), Y(d.pts[i])); + cairo_stroke(cr); + break; + } + } + if (selected) { + // Corner dots on the anchors. + cairo_set_source_rgba(cr, col[0], col[1], col[2], 1.0); + for (const Anchor& a : d.pts) { + cairo_arc(cr, X(a), Y(a), 3, 0, 2 * M_PI); + cairo_fill(cr); + } + } + cairo_restore(cr); +} + +// Is pixel (px, py) within `tol` px of the drawing? Used by the +// select/move and erase tools. +inline bool hit_test(const Map& m, const Drawing& d, double px, + double py, double tol = 6) { + if (d.pts.empty()) return false; + auto X = [&](const Anchor& a) { return m.px(a.bar); }; + auto Y = [&](const Anchor& a) { return m.py(a.price); }; + switch (d.kind) { + case Kind::Trend: + case Kind::Ray: + case Kind::Arrow: + case Kind::Measure: + if (d.pts.size() < 2) return false; + return detail::seg_dist(px, py, X(d.pts[0]), Y(d.pts[0]), + X(d.pts[1]), Y(d.pts[1])) <= tol; + case Kind::HLine: + return std::fabs(py - Y(d.pts[0])) <= tol && px >= m.x && + px <= m.x + m.w; + case Kind::VLine: + return std::fabs(px - X(d.pts[0])) <= tol && py >= m.y && + py <= m.y + m.h; + case Kind::Rect: + case Kind::Ellipse: { + if (d.pts.size() < 2) return false; + double x1 = X(d.pts[0]), y1 = Y(d.pts[0]); + double x2 = X(d.pts[1]), y2 = Y(d.pts[1]); + bool inside = px >= std::min(x1, x2) && px <= std::max(x1, x2) && + py >= std::min(y1, y2) && py <= std::max(y1, y2); + bool near_edge = + detail::seg_dist(px, py, x1, y1, x2, y1) <= tol || + detail::seg_dist(px, py, x2, y1, x2, y2) <= tol || + detail::seg_dist(px, py, x2, y2, x1, y2) <= tol || + detail::seg_dist(px, py, x1, y2, x1, y1) <= tol; + return inside || near_edge; + } + case Kind::Text: { + double x = X(d.pts[0]), y = Y(d.pts[0]); + double w = 10 + (double)d.text.size() * 6.5; + return px >= x - 4 && px <= x + w && py >= y - 16 && + py <= y + 4; + } + case Kind::Fib: { + if (d.pts.size() < 2) return false; + static const double kLevels[] = {0, 0.236, 0.382, 0.5, + 0.618, 0.786, 1.0}; + for (double lv : kLevels) { + double price = + d.pts[0].price + (d.pts[1].price - d.pts[0].price) * lv; + if (std::fabs(py - m.py(price)) <= tol && px >= X(d.pts[0])) + return true; + } + return false; + } + case Kind::Freehand: + for (size_t i = 1; i < d.pts.size(); ++i) + if (detail::seg_dist(px, py, X(d.pts[i - 1]), + Y(d.pts[i - 1]), X(d.pts[i]), + Y(d.pts[i])) <= tol) + return true; + return d.pts.size() == 1 && + std::hypot(px - X(d.pts[0]), py - Y(d.pts[0])) <= tol; + } + return false; +} + +// Shift every anchor by a data-space delta (select/move drags). +inline void move(Drawing& d, double dbar, double dprice) { + for (Anchor& a : d.pts) { + a.bar += dbar; + a.price += dprice; + } + if (d.kind == Kind::HLine) + for (Anchor& a : d.pts) a.bar -= dbar; // bar is meaningless + if (d.kind == Kind::VLine) + for (Anchor& a : d.pts) a.price -= dprice; +} + +// Parse one spec (grammar in the header comment). +inline std::optional parse_spec(const std::string& spec) { + // Tokenize on whitespace, remembering where the tail starts (text). + std::vector tok; + std::vector tok_pos; + size_t i = 0; + while (i < spec.size()) { + while (i < spec.size() && + (spec[i] == ' ' || spec[i] == '\t')) + ++i; + if (i >= spec.size()) break; + size_t s = i; + while (i < spec.size() && spec[i] != ' ' && spec[i] != '\t') + ++i; + tok.push_back(spec.substr(s, i - s)); + tok_pos.push_back(s); + } + if (tok.empty()) return std::nullopt; + Drawing d; + if (!kind_from_name(tok[0], d.kind)) return std::nullopt; + size_t t = 1; + auto number = [&](double& v) { + if (t >= tok.size()) return false; + char* end = nullptr; + v = std::strtod(tok[t].c_str(), &end); + if (end == tok[t].c_str()) return false; + ++t; + return true; + }; + auto anchor = [&](Anchor& a) { + return number(a.bar) && number(a.price); + }; + auto tail_options = [&]() { + while (t < tok.size()) { + int ci = color_index_of(tok[t]); + if (ci >= 0) { + d.color = ci; + ++t; + } else if (tok[t] == "dash") { + d.dash = true; + ++t; + } else { + break; + } + } + }; + switch (d.kind) { + case Kind::HLine: { + double p; + if (!number(p)) return std::nullopt; + d.pts.push_back({0, p}); + tail_options(); + return d; + } + case Kind::VLine: { + double b; + if (!number(b)) return std::nullopt; + d.pts.push_back({b, 0}); + tail_options(); + return d; + } + case Kind::Text: { + Anchor a; + if (!anchor(a)) return std::nullopt; + d.pts.push_back(a); + tail_options(); + if (t < tok.size()) + d.text = spec.substr(tok_pos[t]); + if (d.text.empty()) return std::nullopt; + return d; + } + case Kind::Freehand: { + while (t < tok.size()) { + char* end = nullptr; + double v = std::strtod(tok[t].c_str(), &end); + if (end == tok[t].c_str()) break; + Anchor a; + a.bar = v; + ++t; + if (!number(a.price)) return std::nullopt; + d.pts.push_back(a); + } + tail_options(); + if (d.pts.size() < 2) return std::nullopt; + return d; + } + default: { // two-anchor kinds + Anchor a, b; + if (!anchor(a) || !anchor(b)) return std::nullopt; + d.pts.push_back(a); + d.pts.push_back(b); + tail_options(); + return d; + } + } +} + +// Parse a semicolon-separated list of specs; bad entries are skipped. +inline std::vector parse_specs(const std::string& specs) { + std::vector out; + size_t start = 0; + for (size_t i = 0; i <= specs.size(); ++i) { + if (i == specs.size() || specs[i] == ';') { + auto d = parse_spec(specs.substr(start, i - start)); + if (d) out.push_back(*d); + start = i + 1; + } + } + return out; +} + +// Canonical spec for a drawing (parse_spec(to_spec(d)) round-trips). +inline std::string to_spec(const Drawing& d) { + static const char* kNames[] = {"blue", "red", "green", "orange", + "purple", "teal", "magenta", "brown", + "black", "slate"}; + int ci = ((d.color % kPaletteSize) + kPaletteSize) % kPaletteSize; + char buf[64]; + std::string s = kind_name(d.kind); + auto num = [&](double v) { + std::snprintf(buf, sizeof buf, " %g", v); + s += buf; + }; + if (d.kind == Kind::HLine) { + num(d.pts.empty() ? 0 : d.pts[0].price); + } else if (d.kind == Kind::VLine) { + num(d.pts.empty() ? 0 : d.pts[0].bar); + } else { + for (const Anchor& a : d.pts) { + num(a.bar); + num(a.price); + } + } + s += " "; + s += kNames[ci]; + if (d.dash) s += " dash"; + if (d.kind == Kind::Text) { + s += " "; + s += d.text; + } + return s; +} + +} // namespace fdw +)FDDRAWINGS9b2c"; diff --git a/src/generator.cpp b/src/generator.cpp new file mode 100644 index 0000000..aa8ce5c --- /dev/null +++ b/src/generator.cpp @@ -0,0 +1,1740 @@ +// FDesigner code generator — see generator.h. All-code output with one +// standalone class per widget, mirroring the Python generator's shape. + +#include "generator.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "drawings_embedded.h" +#include "indicators_embedded.h" +#include "props.h" + +namespace fs = std::filesystem; + +// ---------------------------------------------------------------- helpers + +// Escape a string for a C++ string literal. +static std::string esc_c(const std::string& s) { + std::string out; + for (unsigned char c : s) { + switch (c) { + case '"': out += "\\\""; break; + case '\\': out += "\\\\"; break; + case '\n': out += "\\n"; break; + case '\r': out += "\\r"; break; + case '\t': out += "\\t"; break; + default: + if (c < 0x20) { + char buf[8]; + std::snprintf(buf, sizeof buf, "\\%03o", c); + out += buf; + } else { + out += (char)c; + } + } + } + return out; +} + +// GTK constant name for an enum property value, or "" if unmapped. +static std::string enum_const(const std::string& prop, int v) { + static const std::map> kTable = { + {"relief", + {"GTK_RELIEF_NORMAL", "GTK_RELIEF_HALF", "GTK_RELIEF_NONE"}}, + {"shadow-type", + {"GTK_SHADOW_NONE", "GTK_SHADOW_IN", "GTK_SHADOW_OUT", + "GTK_SHADOW_ETCHED_IN", "GTK_SHADOW_ETCHED_OUT"}}, + {"selection-mode", + {"GTK_SELECTION_NONE", "GTK_SELECTION_SINGLE", "GTK_SELECTION_BROWSE", + "GTK_SELECTION_MULTIPLE"}}, + {"direction", + {"GTK_ARROW_UP", "GTK_ARROW_DOWN", "GTK_ARROW_LEFT", + "GTK_ARROW_RIGHT", "GTK_ARROW_NONE"}}, + {"wrap-mode", + {"GTK_WRAP_NONE", "GTK_WRAP_CHAR", "GTK_WRAP_WORD", + "GTK_WRAP_WORD_CHAR"}}, + }; + auto it = kTable.find(prop); + if (it == kTable.end() || v < 0 || v >= (int)it->second.size()) return ""; + return it->second[v]; +} + +// Format a stored double for a C++ literal (varargs need a real double: +// "0" would pass an int, so make sure a '.' or exponent is present). +static std::string fmt_double(const std::string& v) { + char buf[64]; + std::snprintf(buf, sizeof buf, "%g", std::atof(v.c_str())); + std::string s = buf; + if (s.find_first_of(".eEnN") == std::string::npos) s += ".0"; + return s; +} + +static std::string prop_or(const Widget& w, const std::string& name, + const std::string& dflt) { + auto it = w.props.find(name); + return it == w.props.end() ? dflt : it->second; +} + +static std::string trim(const std::string& s) { + size_t a = s.find_first_not_of(" \t\r\n"); + if (a == std::string::npos) return ""; + size_t b = s.find_last_not_of(" \t\r\n"); + return s.substr(a, b - a + 1); +} + +// Split an indicators spec ("SMA(20), MACD(12,26,9), RSI") into its +// individual specs — commas inside parentheses don't split. +static std::vector split_indicator_specs( + const std::string& spec) { + std::vector out; + int depth = 0; + size_t start = 0; + for (size_t i = 0; i <= spec.size(); ++i) { + if (i == spec.size() || (spec[i] == ',' && depth == 0)) { + std::string one = trim(spec.substr(start, i - start)); + if (!one.empty()) out.push_back(one); + start = i + 1; + } else if (spec[i] == '(') { + ++depth; + } else if (spec[i] == ')') { + --depth; + } + } + return out; +} + +// Split a compare-files spec ("a.csv, b.csv") on commas. +static std::vector split_compare_files( + const std::string& spec) { + std::vector out; + size_t start = 0; + for (size_t i = 0; i <= spec.size(); ++i) { + if (i == spec.size() || spec[i] == ',') { + std::string one = trim(spec.substr(start, i - start)); + if (!one.empty()) out.push_back(one); + start = i + 1; + } + } + return out; +} + +// Split a drawings spec ("trend ...; hline ...") on semicolons. +static std::vector split_drawings_specs( + const std::string& spec) { + std::vector out; + size_t start = 0; + for (size_t i = 0; i <= spec.size(); ++i) { + if (i == spec.size() || spec[i] == ';') { + std::string one = trim(spec.substr(start, i - start)); + if (!one.empty()) out.push_back(one); + start = i + 1; + } + } + return out; +} + +static std::string sanitize_class(const std::string& raw) { + std::string out; + for (char c : raw) + if (std::isalnum((unsigned char)c) || c == '_') out += c; + if (out.empty()) out = "MyWindow"; + if (std::isdigit((unsigned char)out[0])) out = "W" + out; + return out; +} + +// ---------------------------------------------------------------- menus + +struct MenuDef { + std::string label; + std::vector items; +}; + +// Parse the designer's menu spec: one "Label: Item | Item | -" per line. +static std::vector parse_menus(const std::string& spec) { + std::vector out; + std::istringstream in(spec); + std::string line; + while (std::getline(in, line)) { + size_t colon = line.find(':'); + if (colon == std::string::npos) continue; + MenuDef m; + m.label = trim(line.substr(0, colon)); + if (m.label.empty()) continue; + std::istringstream items(line.substr(colon + 1)); + std::string item; + while (std::getline(items, item, '|')) { + item = trim(item); + if (!item.empty()) m.items.push_back(item); + } + out.push_back(m); + } + return out; +} + +// Identifier fragment from a menu label ("Save As" -> "save_as"). +static std::string menu_id_part(const std::string& label) { + std::string out; + for (unsigned char c : label) { + if (std::isalnum(c)) + out += (char)std::tolower(c); + else if (!out.empty() && out.back() != '_') + out += '_'; + } + while (!out.empty() && out.back() == '_') out.pop_back(); + return out.empty() ? "item" : out; +} + +static std::string menu_item_id(const MenuDef& m, const std::string& item) { + return "menu_" + menu_id_part(m.label) + "_" + menu_id_part(item); +} + +// ------------------------------------------------------- mouse stub sets +// The Python generator's nine handlers per widget, in its order. In a +// widget class the handlers are unprefixed (Button_001::buttonPress1); +// in the window class the root set keeps the Root_001_ prefix. + +static const char* kPress[3] = {"buttonPress1", "buttonPress2", + "buttonPress3"}; +static const char* kDbl[3] = {"dblbuttonPress1", "dblbuttonPress2", + "dblbuttonPress3"}; +static const char* kRel[3] = {"buttonRelease1", "buttonRelease2", + "buttonRelease3"}; + +struct StubNames { + std::string prefix; // "Root_001_" or "" + std::string press; // dispatcher names + std::string release; +}; + +static void stub_decls(std::ostringstream& o, const StubNames& n) { + for (int i = 0; i < 3; ++i) + o << " void " << n.prefix << kPress[i] + << "(GdkEventButton* event);\n"; + for (int i = 0; i < 3; ++i) + o << " void " << n.prefix << kDbl[i] + << "(GdkEventButton* event);\n"; + for (int i = 0; i < 3; ++i) + o << " void " << n.prefix << kRel[i] + << "(GdkEventButton* event);\n"; + o << " static gboolean " << n.press + << "(GtkWidget*, GdkEventButton*, gpointer self);\n"; + o << " static gboolean " << n.release + << "(GtkWidget*, GdkEventButton*, gpointer self);\n"; +} + +static void stub_defs(std::ostringstream& o, const std::string& cls, + const StubNames& n) { + auto one = [&](const char* handler) { + o << "\nvoid " << cls << "::" << n.prefix << handler + << "(GdkEventButton*) {\n" + << " // Code block here\n" + << "}\n"; + }; + for (int i = 0; i < 3; ++i) one(kPress[i]); + for (int i = 0; i < 3; ++i) one(kDbl[i]); + for (int i = 0; i < 3; ++i) one(kRel[i]); + o << "\ngboolean " << cls << "::" << n.press + << "(GtkWidget*, GdkEventButton* e, gpointer self) {\n" + << " auto* me = static_cast<" << cls << "*>(self);\n" + << " if (e->type == GDK_2BUTTON_PRESS) {\n" + << " if (e->button == 1) me->" << n.prefix << kDbl[0] + << "(e);\n" + << " else if (e->button == 2) me->" << n.prefix << kDbl[1] + << "(e);\n" + << " else if (e->button == 3) me->" << n.prefix << kDbl[2] + << "(e);\n" + << " } else {\n" + << " if (e->button == 1) me->" << n.prefix << kPress[0] + << "(e);\n" + << " else if (e->button == 2) me->" << n.prefix << kPress[1] + << "(e);\n" + << " else if (e->button == 3) me->" << n.prefix << kPress[2] + << "(e);\n" + << " }\n" + << " return FALSE;\n" + << "}\n"; + o << "\ngboolean " << cls << "::" << n.release + << "(GtkWidget*, GdkEventButton* e, gpointer self) {\n" + << " auto* me = static_cast<" << cls << "*>(self);\n" + << " if (e->button == 1) me->" << n.prefix << kRel[0] << "(e);\n" + << " else if (e->button == 2) me->" << n.prefix << kRel[1] + << "(e);\n" + << " else if (e->button == 3) me->" << n.prefix << kRel[2] + << "(e);\n" + << " return FALSE;\n" + << "}\n"; +} + +// Connect a widget's press/release dispatchers (the .bind() equivalent). +static std::string connect_mouse(const std::string& cls, + const std::string& target, + const StubNames& n, int indent) { + std::string p(indent, ' '); + std::ostringstream o; + o << p << "gtk_widget_add_events(" << target + << ", GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);\n" + << p << "g_signal_connect(" << target << ", \"button-press-event\",\n" + << p << " G_CALLBACK(&" << cls << "::" << n.press + << "), this);\n" + << p << "g_signal_connect(" << target + << ", \"button-release-event\",\n" + << p << " G_CALLBACK(&" << cls << "::" << n.release + << "), this);\n"; + return o.str(); +} + +// g_object_set lines for a widget's stored properties, one per property +// (one steppable line each). Skips colours (CSS) and per-type properties +// consumed at creation time. +static std::string prop_lines(const Widget& w, const std::string& target, + const std::set& skip) { + std::ostringstream o; + for (const auto& [name, value] : w.props) { + if (name == "background" || name == "foreground") continue; + if (skip.count(name)) continue; + const PropDef* def = find_prop(w.type, name); + if (!def) continue; + if (w.type == "Scale" && name == "value") { + o << " gtk_range_set_value(GTK_RANGE(" << target << "), " + << fmt_double(value) << ");\n"; + continue; + } + std::string v; + switch (def->kind) { + case PropKind::Str: v = "\"" + esc_c(value) + "\""; break; + case PropKind::Bool: v = value == "true" ? "TRUE" : "FALSE"; break; + case PropKind::Int: v = value; break; + case PropKind::Double: v = fmt_double(value); break; + case PropKind::Enum: { + std::string c = enum_const(name, std::atoi(value.c_str())); + v = c.empty() ? value : c; + break; + } + case PropKind::Color: continue; + } + o << " g_object_set(" << target << ", \"" << name << "\", " + << v << ", nullptr);\n"; + } + return o.str(); +} + +// A widget's own colours as a small CSS block on its style context. +static std::string colour_lines(const Widget& w, const std::string& target) { + auto bg = w.props.find("background"); + auto fg = w.props.find("foreground"); + if (bg == w.props.end() && fg == w.props.end()) return ""; + std::string body; + if (bg != w.props.end()) body += "background: " + bg->second + "; "; + if (fg != w.props.end()) body += "color: " + fg->second + "; "; + std::ostringstream o; + o << " GtkCssProvider* css = gtk_css_provider_new();\n" + << " gtk_css_provider_load_from_data(css, \"* { " << body + << "}\", -1, nullptr);\n" + << " gtk_style_context_add_provider(gtk_widget_get_style_" + "context(" + << target << "),\n" + << " GTK_STYLE_PROVIDER(css), " + "GTK_STYLE_PROVIDER_PRIORITY_USER);\n"; + return o.str(); +} + +// ------------------------------------------------------- widget classes + +static const StubNames kWidgetStubs{"", "on_press", "on_release"}; +static const StubNames kRootStubs{"Root_001_", "on_Root_001_press", + "on_Root_001_release"}; + +static std::string emit_widget_header(const Widget& w) { + std::ostringstream o; + o << "// Generated by FDesigner (C++/GTK) — standalone widget class.\n" + << "#pragma once\n\n" + << "#include \n"; + if (w.type == "StockChart") + o << "#include \n#include \n" + << "#include \n\n" + << "#include \"drawings.h\"\n" + << "#include \"indicators.h\"\n"; + o << "\n" + << "class " << w.name << " {\n" + << "public:\n"; + if (w.type == "Toplevel") + o << " " << w.name << "();\n"; + else + o << " explicit " << w.name << "(GtkWidget* fixed);\n"; + o << " ~" << w.name << "();\n" + << " GtkWidget* widget() const { return widget_; }\n"; + if (w.type == "StockChart") { + o << "\n" + << " // One OHLC bar (Massive aggregates format).\n" + << " struct Candle {\n" + << " double open = 0, high = 0, low = 0, close = 0, volume " + "= 0;\n" + << " long long time = 0; // window_start, ns\n" + << " };\n" + << " bool load_csv(const std::string& path);\n" + << " void set_candles(const std::vector& rows);\n" + << " int candle_count() const { return (int)candles_.size(); " + "}\n" + << " // Indicator submodules (indicators.h): attach by name,\n" + << " // e.g. add_indicator(\"SMA(20)\"). They draw on this\n" + << " // chart's canvas; overlays share the price scale, pane\n" + << " // indicators get their own strip below the candles.\n" + << " bool add_indicator(const std::string& spec);\n" + << " void clear_indicators();\n" + << " // Value scale: 0 = price, 1 = percent change from " + "the\n" + << " // first visible bar's close (the axis relabels).\n" + << " void set_scale_mode(int mode);\n" + << " int scale_mode() const { return scale_mode_; }\n" + << " // Comparative series: another ticker's CSV, aligned " + "by\n" + << " // timestamp and rebased onto this chart's first " + "visible\n" + << " // close, drawn as a coloured line (see the legend).\n" + << " bool add_compare(const std::string& path);\n" + << " int compare_count() const { return " + "(int)compares_.size(); }\n" + << " // Annotations (drawings.h): add by spec, e.g.\n" + << " // add_drawing(\"trend 10 655.0 200 672.5 red\"). " + "Anchors\n" + << " // are data coordinates, so drawings survive " + "resizes.\n" + << " bool add_drawing(const std::string& spec);\n" + << " void clear_drawings();\n" + << " int drawing_count() const { return " + "(int)drawings_.size(); }\n" + << " // Mouse drawing tools: 0 none (drag pans), 1 trend, " + "2 hline,\n" + << " // 3 vline, 4 rect, 5 ellipse, 6 arrow, 7 text, 8 " + "fib,\n" + << " // 9 freehand, 10 measure, 11 erase, 12 select/move.\n" + << " // Keys T H V R L A X F D M E S pick a tool, Esc " + "returns\n" + << " // to pan, Delete removes the selected drawing. The " + "mouse\n" + << " // wheel zooms around the cursor in every mode.\n" + << " void set_draw_tool(int tool);\n" + << " int draw_tool() const { return tool_; }\n" + << " void zoom_at(double factor, double frac);\n" + << " void reset_view();\n" + << " size_t visible_begin() const;\n" + << " size_t visible_count() const;\n"; + } + o << "\nprivate:\n" + << " GtkWidget* widget_ = nullptr;\n"; + if (w.type == "StockChart") { + o << " std::vector candles_;\n" + << " std::vector> " + "indicators_;\n" + << " GdkRGBA up_{0.0, 0.78, 0.0, 1.0};\n" + << " GdkRGBA down_{0.86, 0.0, 0.0, 1.0};\n" + << " int max_candles_ = 120;\n" + << " struct Compare {\n" + << " std::string name;\n" + << " std::vector rows;\n" + << " GdkRGBA color{0, 0, 0, 1};\n" + << " };\n" + << " std::vector compares_;\n" + << " std::vector drawings_;\n" + << " std::string ticker_;\n" + << " int scale_mode_ = 0;\n" + << " int tool_ = 0;\n" + << " int selected_ = -1;\n" + << " double view_begin_ = -1, view_count_ = 0;\n" + << " bool panning_ = false;\n" + << " double pan_x_ = 0, pan_begin_ = 0;\n" + << " bool moving_ = false;\n" + << " double last_bar_ = 0, last_price_ = 0;\n" + << " bool sketching_ = false;\n" + << " fdw::Drawing sketch_;\n" + << " struct Layout {\n" + << " double plot_x = 0, plot_y = 0, plot_w = 0;\n" + << " double price_h = 0, pane_h = 0;\n" + << " double axis_w = 56, axis_h = 18, pad = 4;\n" + << " int panes = 0;\n" + << " size_t begin = 0, count = 0;\n" + << " double lo = 0, hi = 1, base = 0;\n" + << " };\n" + << " Layout layout(int width, int height) const;\n" + << " fdw::Map current_map(const Layout& L) const;\n" + << " double compare_close_at(const Compare& c, long long " + "t) const;\n" + << " double compare_base(const Compare& c, size_t begin,\n" + << " size_t count) const;\n" + << " static gboolean on_draw(GtkWidget*, cairo_t*, gpointer " + "self);\n" + << " static gboolean on_chart_scroll(GtkWidget*, " + "GdkEventScroll*, gpointer self);\n" + << " static gboolean on_chart_press(GtkWidget*, " + "GdkEventButton*, gpointer self);\n" + << " static gboolean on_chart_motion(GtkWidget*, " + "GdkEventMotion*, gpointer self);\n" + << " static gboolean on_chart_release(GtkWidget*, " + "GdkEventButton*, gpointer self);\n" + << " static gboolean on_chart_key(GtkWidget*, GdkEventKey*, " + "gpointer self);\n"; + } + o << " void util_error_print(const char* what);\n\n" + << " // Mouse handlers\n"; + stub_decls(o, kWidgetStubs); + o << "};\n"; + return o.str(); +} + +// The StockChart implementation, emitted verbatim into every +// generated chart class (%CLASS% -> the widget class name). +static std::string chart_methods(const std::string& cls) { + std::string s = R"FDCHART7( +// ------------------------------------------------------------------ +// StockChart implementation: candles + indicators (indicators.h), +// annotations (drawings.h), comparative series, price/percent scale, +// time axis, wheel zoom and drag pan. Emitted verbatim by the +// generator; %CLASS% is the widget's class name. +// ------------------------------------------------------------------ + +// Parse a Massive-format aggregates CSV (columns matched by header +// name); also captures the ticker from the first data row. +static bool parse_massive_csv(const std::string& path, + std::vector<%CLASS%::Candle>& rows, + std::string& ticker) { + std::ifstream in(path); + if (!in) return false; + auto split = [](const std::string& line) { + std::vector fields; + std::stringstream ss(line); + std::string field; + while (std::getline(ss, field, ',')) fields.push_back(field); + return fields; + }; + std::string header; + if (!std::getline(in, header)) return false; + auto cols = split(header); + auto index_of = [&](const char* name) { + for (size_t i = 0; i < cols.size(); ++i) + if (cols[i] == name) return (int)i; + return -1; + }; + int io = index_of("open"), ih = index_of("high"), + il = index_of("low"), ic = index_of("close"), + it = index_of("window_start"), iv = index_of("volume"), + ik = index_of("ticker"); + if (io < 0 || ih < 0 || il < 0 || ic < 0) return false; + std::string line; + while (std::getline(in, line)) { + if (line.empty()) continue; + auto fields = split(line); + int need = std::max(std::max(io, ih), std::max(il, ic)); + if ((int)fields.size() <= need) continue; + %CLASS%::Candle c; + c.open = std::atof(fields[io].c_str()); + c.high = std::atof(fields[ih].c_str()); + c.low = std::atof(fields[il].c_str()); + c.close = std::atof(fields[ic].c_str()); + if (iv >= 0 && (int)fields.size() > iv) + c.volume = std::atof(fields[iv].c_str()); + if (it >= 0 && (int)fields.size() > it) + c.time = std::atoll(fields[it].c_str()); + if (c.high == 0 && c.low == 0 && c.close == 0) continue; + if (ticker.empty() && ik >= 0 && (int)fields.size() > ik) + ticker = fields[ik]; + rows.push_back(c); + } + return !rows.empty(); +} + +// Bottom-axis time labels are UTC (Massive window_start is epoch ns). +static void fmt_axis_time(long long ns, bool with_date, char* buf, + size_t n) { + std::time_t t = (std::time_t)(ns / 1000000000LL); + std::tm tmv; + gmtime_r(&t, &tmv); + std::strftime(buf, n, with_date ? "%m-%d %H:%M" : "%H:%M", &tmv); +} + +// Mouse-tool ids (see set_draw_tool) that sketch a drawing: which +// fdw::Kind they create. Tools 2/3 (hline/vline) commit on click, +// 7 (text) asks for words, 11 erases, 12 selects/moves, 0 pans. +static bool tool_sketch_kind(int tool, fdw::Kind& k) { + switch (tool) { + case 1: k = fdw::Kind::Trend; return true; + case 4: k = fdw::Kind::Rect; return true; + case 5: k = fdw::Kind::Ellipse; return true; + case 6: k = fdw::Kind::Arrow; return true; + case 8: k = fdw::Kind::Fib; return true; + case 9: k = fdw::Kind::Freehand; return true; + case 10: k = fdw::Kind::Measure; return true; + } + return false; +} + +bool %CLASS%::load_csv(const std::string& path) { + std::vector rows; + std::string tick; + if (!parse_massive_csv(path, rows, tick)) return false; + candles_ = rows; + if (!tick.empty()) ticker_ = tick; + view_begin_ = -1; + view_count_ = 0; + if (widget_) gtk_widget_queue_draw(widget_); + return true; +} + +void %CLASS%::set_candles(const std::vector& rows) { + candles_ = rows; + view_begin_ = -1; + view_count_ = 0; + if (widget_) gtk_widget_queue_draw(widget_); +} + +bool %CLASS%::add_indicator(const std::string& spec) { + auto ind = fdi::make_indicator(spec); + if (!ind) return false; + indicators_.push_back(std::move(ind)); + if (widget_) gtk_widget_queue_draw(widget_); + return true; +} + +void %CLASS%::clear_indicators() { + indicators_.clear(); + if (widget_) gtk_widget_queue_draw(widget_); +} + +void %CLASS%::set_scale_mode(int mode) { + scale_mode_ = mode ? 1 : 0; + if (widget_) gtk_widget_queue_draw(widget_); +} + +// Close of a comparison series at-or-before time t (NaN if the series +// has not started yet) — series are aligned by timestamp, not index. +double %CLASS%::compare_close_at(const Compare& c, long long t) const { + const double kNaN = std::numeric_limits::quiet_NaN(); + if (c.rows.empty() || t < c.rows.front().time) return kNaN; + size_t lo = 0, hi = c.rows.size(); + while (lo + 1 < hi) { + size_t mid = (lo + hi) / 2; + if (c.rows[mid].time <= t) + lo = mid; + else + hi = mid; + } + return c.rows[lo].close; +} + +// The comparison close that anchors the rebase for a window: the first +// close available inside it. +double %CLASS%::compare_base(const Compare& c, size_t begin, + size_t count) const { + for (size_t i = begin; i < begin + count && i < candles_.size(); ++i) { + double v = compare_close_at(c, candles_[i].time); + if (v == v && v != 0) return v; + } + return std::numeric_limits::quiet_NaN(); +} + +bool %CLASS%::add_compare(const std::string& path) { + std::vector rows; + std::string tick; + if (!parse_massive_csv(path, rows, tick)) return false; + Compare c; + c.name = tick.empty() ? path : tick; + c.rows = rows; + static const double kCols[][3] = {{0.90, 0.45, 0.05}, + {0.55, 0.20, 0.75}, + {0.00, 0.55, 0.60}, + {0.85, 0.20, 0.65}, + {0.35, 0.35, 0.35}}; + int i = (int)compares_.size() % 5; + c.color = GdkRGBA{kCols[i][0], kCols[i][1], kCols[i][2], 1.0}; + compares_.push_back(c); + if (widget_) gtk_widget_queue_draw(widget_); + return true; +} + +bool %CLASS%::add_drawing(const std::string& spec) { + auto d = fdw::parse_spec(spec); + if (!d) return false; + drawings_.push_back(*d); + if (widget_) gtk_widget_queue_draw(widget_); + return true; +} + +void %CLASS%::clear_drawings() { + drawings_.clear(); + selected_ = -1; + if (widget_) gtk_widget_queue_draw(widget_); +} + +void %CLASS%::set_draw_tool(int tool) { + tool_ = tool; + if (widget_) gtk_widget_queue_draw(widget_); +} + +size_t %CLASS%::visible_count() const { + if (candles_.empty()) return 0; + if (view_count_ > 0) + return (size_t)std::min(view_count_, (double)candles_.size()); + return std::min((size_t)std::max(1, max_candles_), candles_.size()); +} + +size_t %CLASS%::visible_begin() const { + if (candles_.empty()) return 0; + if (view_count_ > 0) { + double vb = view_begin_; + if (vb < 0) vb = 0; + if (vb + view_count_ > (double)candles_.size()) + vb = (double)candles_.size() - view_count_; + return (size_t)vb; + } + return candles_.size() - visible_count(); +} + +// Zoom around a fractional position (0..1) across the plot: the bar +// under the cursor stays under the cursor. +void %CLASS%::zoom_at(double factor, double frac) { + if (candles_.empty() || factor <= 0) return; + if (view_count_ <= 0) { + view_count_ = (double)visible_count(); + view_begin_ = (double)visible_begin(); + } + frac = std::max(0.0, std::min(1.0, frac)); + double anchor = view_begin_ + frac * view_count_; + double nc = std::round(std::min(std::max(view_count_ * factor, 5.0), + (double)candles_.size())); + double nb = std::round(anchor - frac * nc); + if (nb < 0) nb = 0; + if (nb + nc > (double)candles_.size()) + nb = (double)candles_.size() - nc; + view_begin_ = nb; + view_count_ = nc; + if (widget_) gtk_widget_queue_draw(widget_); +} + +void %CLASS%::reset_view() { + view_begin_ = -1; + view_count_ = 0; + if (widget_) gtk_widget_queue_draw(widget_); +} + +%CLASS%::Layout %CLASS%::layout(int width, int height) const { + Layout L; + for (auto& ind : indicators_) + if (ind->kind() == fdi::PlotKind::Pane) ++L.panes; + L.plot_w = std::max(10.0, width - L.axis_w); + double total_h = std::max(10.0, height - L.axis_h); + L.price_h = L.panes ? total_h * 0.66 : total_h; + L.pane_h = L.panes ? (total_h - L.price_h) / L.panes : 0; + if (candles_.empty()) return L; + L.begin = visible_begin(); + L.count = visible_count(); + L.base = candles_[L.begin].close; + if (L.base == 0) L.base = candles_[L.begin].open; + L.lo = candles_[L.begin].low; + L.hi = candles_[L.begin].high; + for (size_t i = L.begin; i < L.begin + L.count; ++i) { + L.lo = std::min(L.lo, candles_[i].low); + L.hi = std::max(L.hi, candles_[i].high); + } + // Comparison series extend the range with their rebased values. + for (const Compare& c : compares_) { + double c0 = compare_base(c, L.begin, L.count); + if (c0 != c0 || c0 == 0 || L.base == 0) continue; + for (size_t i = L.begin; i < L.begin + L.count; ++i) { + double v = compare_close_at(c, candles_[i].time); + if (v == v) { + double eq = L.base * v / c0; + L.lo = std::min(L.lo, eq); + L.hi = std::max(L.hi, eq); + } + } + } + if (L.hi - L.lo < 1e-9) L.hi = L.lo + 1; + return L; +} + +fdw::Map %CLASS%::current_map(const Layout& L) const { + fdw::Map m; + m.x = L.plot_x; + m.y = L.plot_y + L.pad; + m.w = L.plot_w; + m.h = L.price_h - 2 * L.pad; + m.begin = (double)L.begin; + m.count = (double)L.count; + m.lo = L.lo; + m.hi = L.hi; + return m; +} + +gboolean %CLASS%::on_draw(GtkWidget* drawing, cairo_t* cr, + gpointer self) { + auto* me = static_cast<%CLASS%*>(self); + int width = gtk_widget_get_allocated_width(drawing); + int height = gtk_widget_get_allocated_height(drawing); + cairo_set_source_rgb(cr, 1, 1, 1); + cairo_paint(cr); + const std::vector& all = me->candles_; + if (all.empty()) { + cairo_set_source_rgb(cr, 0.4, 0.4, 0.4); + cairo_move_to(cr, 8, 20); + cairo_show_text(cr, "StockChart - no data"); + return FALSE; + } + Layout L = me->layout(width, height); + const size_t begin = L.begin, n = L.count; + const double lo = L.lo, hi = L.hi; + auto y = [&](double v) { + return L.plot_y + L.pad + + (hi - v) / (hi - lo) * (L.price_h - 2 * L.pad); + }; + double slot = L.plot_w / (double)n; + + // Horizontal gridlines + the value axis: prices, or percent + // change from the first visible close when scale_mode_ == 1. + { + double raw = (hi - lo) / 5; + double p10 = std::pow(10, std::floor(std::log10(raw))); + double m = raw / p10; + double step = (m < 1.5 ? 1 : m < 3.5 ? 2 : m < 7.5 ? 5 : 10) * p10; + cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL, + CAIRO_FONT_WEIGHT_NORMAL); + cairo_set_font_size(cr, 9.5); + for (double v = std::ceil(lo / step) * step; v <= hi; v += step) { + double gy = y(v); + cairo_set_source_rgba(cr, 0, 0, 0, 0.08); + cairo_set_line_width(cr, 1); + cairo_move_to(cr, L.plot_x, gy); + cairo_line_to(cr, L.plot_x + L.plot_w, gy); + cairo_stroke(cr); + char buf[32]; + if (me->scale_mode_ == 1 && L.base > 0) + std::snprintf(buf, sizeof buf, "%+.1f%%", + (v / L.base - 1) * 100); + else + std::snprintf(buf, sizeof buf, "%.2f", v); + cairo_set_source_rgb(cr, 0.35, 0.35, 0.35); + cairo_move_to(cr, L.plot_x + L.plot_w + 4, gy + 3); + cairo_show_text(cr, buf); + } + } + + // Vertical gridlines + the bottom time axis (UTC; the date is + // shown on the first tick and whenever the day changes). + if (all[begin].time > 0) { + static const long kSteps[] = {1, 2, 5, 10, 15, 30, + 60, 120, 240, 480, 960, 1920, + 3840, 7680}; + long want = std::max(1L, (long)n / 6); + long step_b = kSteps[sizeof(kSteps) / sizeof(kSteps[0]) - 1]; + for (long s : kSteps) + if (s >= want) { + step_b = s; + break; + } + cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL, + CAIRO_FONT_WEIGHT_NORMAL); + cairo_set_font_size(cr, 9.5); + long long prev_day = -1; + size_t first = (begin + (size_t)step_b - 1) / (size_t)step_b * + (size_t)step_b; + for (size_t i = first; i < begin + n; i += (size_t)step_b) { + double gx = L.plot_x + slot * (double)(i - begin) + slot / 2; + cairo_set_source_rgba(cr, 0, 0, 0, 0.06); + cairo_set_line_width(cr, 1); + cairo_move_to(cr, gx, L.plot_y); + cairo_line_to(cr, gx, L.plot_y + L.price_h); + cairo_stroke(cr); + long long day = all[i].time / 86400000000000LL; + char buf[24]; + fmt_axis_time(all[i].time, prev_day < 0 || day != prev_day, + buf, sizeof buf); + prev_day = day; + cairo_text_extents_t te; + cairo_text_extents(cr, buf, &te); + double tx = std::max( + 0.0, std::min(gx - te.width / 2, (double)width - te.width)); + cairo_set_source_rgb(cr, 0.35, 0.35, 0.35); + cairo_move_to(cr, tx, height - 5); + cairo_show_text(cr, buf); + } + cairo_set_source_rgba(cr, 0, 0, 0, 0.25); + cairo_set_line_width(cr, 1); + cairo_move_to(cr, L.plot_x, height - L.axis_h + 0.5); + cairo_line_to(cr, L.plot_x + L.plot_w, height - L.axis_h + 0.5); + cairo_stroke(cr); + } + + // Candles. + double body_w = std::max(1.0, slot * 0.7); + for (size_t k = 0; k < n; ++k) { + const Candle& c = all[begin + k]; + const GdkRGBA& col = c.close >= c.open ? me->up_ : me->down_; + double cx = L.plot_x + slot * (double)k + slot / 2; + cairo_set_source_rgba(cr, col.red, col.green, col.blue, 1.0); + cairo_set_line_width(cr, 1.0); + cairo_move_to(cr, cx, y(c.high)); + cairo_line_to(cr, cx, y(c.low)); + cairo_stroke(cr); + double y1 = y(std::max(c.open, c.close)); + double y2 = y(std::min(c.open, c.close)); + if (y2 - y1 < 1) y2 = y1 + 1; + cairo_rectangle(cr, cx - body_w / 2, y1, body_w, y2 - y1); + cairo_fill(cr); + } + + // Comparative series: aligned by timestamp, rebased so they start + // at this chart's first visible close, drawn as lines. + for (const Compare& c : me->compares_) { + double c0 = me->compare_base(c, begin, n); + if (c0 != c0 || c0 == 0 || L.base == 0) continue; + cairo_set_source_rgba(cr, c.color.red, c.color.green, c.color.blue, + 1.0); + cairo_set_line_width(cr, 1.5); + bool started = false; + for (size_t k = 0; k < n; ++k) { + double v = me->compare_close_at(c, all[begin + k].time); + if (v != v) { + started = false; + continue; + } + double eq = L.base * v / c0; + double px = L.plot_x + slot * (double)k + slot / 2; + if (!started) { + cairo_move_to(cr, px, y(eq)); + started = true; + } else { + cairo_line_to(cr, px, y(eq)); + } + } + cairo_stroke(cr); + } + + // Indicators (overlays share the price scale — in percent mode the + // scale is affine in price, so the same lo/hi serves both). + std::vector bars(all.size()); + for (size_t i = 0; i < all.size(); ++i) { + bars[i].open = all[i].open; + bars[i].high = all[i].high; + bars[i].low = all[i].low; + bars[i].close = all[i].close; + bars[i].volume = all[i].volume; + bars[i].time = all[i].time; + } + int pane_idx = 0; + for (auto& ind : me->indicators_) { + fdi::PlotArea area; + area.cr = cr; + area.begin = begin; + area.count = n; + if (ind->kind() == fdi::PlotKind::Pane) { + area.x = L.plot_x; + area.y = L.price_h + pane_idx * L.pane_h + 10; + area.w = L.plot_w; + area.h = L.pane_h - 12; + cairo_set_source_rgb(cr, 0.75, 0.75, 0.75); + cairo_set_line_width(cr, 1); + cairo_move_to(cr, L.plot_x, L.price_h + pane_idx * L.pane_h); + cairo_line_to(cr, L.plot_x + L.plot_w, + L.price_h + pane_idx * L.pane_h); + cairo_stroke(cr); + cairo_set_source_rgb(cr, 0.35, 0.35, 0.35); + cairo_move_to(cr, L.plot_x + 2, + L.price_h + pane_idx * L.pane_h + 9); + cairo_show_text(cr, ind->name()); + ind->draw(area, bars); + ++pane_idx; + } else { + area.x = L.plot_x; + area.y = L.plot_y + L.pad; + area.w = L.plot_w; + area.h = L.price_h - 2 * L.pad; + area.lo = lo; + area.hi = hi; + ind->draw(area, bars); + } + } + + // Annotations (data coordinates — resize/zoom safe). + { + fdw::Map map = me->current_map(L); + for (size_t i = 0; i < me->drawings_.size(); ++i) + fdw::draw(cr, map, me->drawings_[i], + (int)i == me->selected_); + if (me->sketching_) fdw::draw(cr, map, me->sketch_); + } + + // Legend: main ticker + last value, then comparison names. + { + cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL, + CAIRO_FONT_WEIGHT_NORMAL); + cairo_set_font_size(cr, 11); + std::vector> entries; + std::string main_name = + me->ticker_.empty() ? std::string("MAIN") : me->ticker_; + char lastbuf[48]; + double last = all[begin + n - 1].close; + if (me->scale_mode_ == 1 && L.base > 0) + std::snprintf(lastbuf, sizeof lastbuf, " %+.2f%%", + (last / L.base - 1) * 100); + else + std::snprintf(lastbuf, sizeof lastbuf, " %.2f", last); + entries.push_back( + {main_name + lastbuf, GdkRGBA{0.15, 0.15, 0.15, 1.0}}); + for (const Compare& c : me->compares_) + entries.push_back({c.name, c.color}); + double tw = 0; + for (auto& e : entries) { + cairo_text_extents_t te; + cairo_text_extents(cr, e.first.c_str(), &te); + tw = std::max(tw, te.width); + } + cairo_set_source_rgba(cr, 1, 1, 1, 0.78); + cairo_rectangle(cr, L.plot_x + 4, L.plot_y + 4, tw + 10, + (double)entries.size() * 14 + 6); + cairo_fill(cr); + double ly = L.plot_y + 15; + for (auto& e : entries) { + cairo_set_source_rgba(cr, e.second.red, e.second.green, + e.second.blue, 1.0); + cairo_move_to(cr, L.plot_x + 8, ly); + cairo_show_text(cr, e.first.c_str()); + ly += 14; + } + } + + // Active-tool hint, right end of the time-axis strip. + if (me->tool_ != 0) { + static const char* kToolNames[] = { + "", "trend", "hline", "vline", "rect", "ellipse", "arrow", + "text", "fib", "freehand", "measure", "erase", "select"}; + char buf[64]; + std::snprintf(buf, sizeof buf, "tool: %s (Esc = pan)", + (me->tool_ >= 1 && me->tool_ <= 12) + ? kToolNames[me->tool_] + : "?"); + cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL, + CAIRO_FONT_WEIGHT_NORMAL); + cairo_set_font_size(cr, 9.5); + cairo_text_extents_t te; + cairo_text_extents(cr, buf, &te); + cairo_set_source_rgb(cr, 0.45, 0.45, 0.45); + cairo_move_to(cr, std::max(0.0, (double)width - te.width - 4), + height - 5); + cairo_show_text(cr, buf); + } + return FALSE; +} + +gboolean %CLASS%::on_chart_scroll(GtkWidget* w, GdkEventScroll* ev, + gpointer self) { + auto* me = static_cast<%CLASS%*>(self); + if (me->candles_.empty()) return FALSE; + Layout L = me->layout(gtk_widget_get_allocated_width(w), + gtk_widget_get_allocated_height(w)); + double frac = L.plot_w > 0 ? (ev->x - L.plot_x) / L.plot_w : 0.5; + double factor; + if (ev->direction == GDK_SCROLL_UP) + factor = 0.8; + else if (ev->direction == GDK_SCROLL_DOWN) + factor = 1.25; + else + factor = ev->delta_y < 0 ? 0.8 : 1.25; // smooth scrolling + me->zoom_at(factor, frac); + return TRUE; +} + +gboolean %CLASS%::on_chart_press(GtkWidget* w, GdkEventButton* ev, + gpointer self) { + auto* me = static_cast<%CLASS%*>(self); + if (ev->button != 1 || me->candles_.empty()) return FALSE; + gtk_widget_grab_focus(w); + Layout L = me->layout(gtk_widget_get_allocated_width(w), + gtk_widget_get_allocated_height(w)); + fdw::Map map = me->current_map(L); + double bar = map.bar_at(ev->x); + double price = map.price_at(ev->y); + fdw::Kind sk; + if (me->tool_ == 0) { + // No tool: drag pans the view. + me->panning_ = true; + me->pan_x_ = ev->x; + if (me->view_count_ <= 0) { + me->view_count_ = (double)me->visible_count(); + me->view_begin_ = (double)me->visible_begin(); + } + me->pan_begin_ = me->view_begin_; + } else if (me->tool_ == 2 || me->tool_ == 3) { + fdw::Drawing d; + d.kind = me->tool_ == 2 ? fdw::Kind::HLine : fdw::Kind::VLine; + d.pts = {{bar, price}}; + me->drawings_.push_back(d); + me->selected_ = (int)me->drawings_.size() - 1; + } else if (me->tool_ == 7) { + // Text label: ask for the words, then anchor them here. + GtkWidget* top = gtk_widget_get_toplevel(w); + GtkWidget* dlg = gtk_dialog_new_with_buttons( + "Annotation text", + GTK_IS_WINDOW(top) ? GTK_WINDOW(top) : nullptr, + GTK_DIALOG_MODAL, "_OK", GTK_RESPONSE_OK, "_Cancel", + GTK_RESPONSE_CANCEL, nullptr); + GtkWidget* entry = gtk_entry_new(); + gtk_container_add( + GTK_CONTAINER(gtk_dialog_get_content_area(GTK_DIALOG(dlg))), + entry); + gtk_widget_show_all(dlg); + if (gtk_dialog_run(GTK_DIALOG(dlg)) == GTK_RESPONSE_OK) { + const char* txt = gtk_entry_get_text(GTK_ENTRY(entry)); + if (txt && *txt) { + fdw::Drawing d; + d.kind = fdw::Kind::Text; + d.pts = {{bar, price}}; + d.text = txt; + me->drawings_.push_back(d); + me->selected_ = (int)me->drawings_.size() - 1; + } + } + gtk_widget_destroy(dlg); + } else if (me->tool_ == 11) { + for (int i = (int)me->drawings_.size() - 1; i >= 0; --i) + if (fdw::hit_test(map, me->drawings_[i], ev->x, ev->y)) { + me->drawings_.erase(me->drawings_.begin() + i); + me->selected_ = -1; + break; + } + } else if (me->tool_ == 12) { + me->selected_ = -1; + for (int i = (int)me->drawings_.size() - 1; i >= 0; --i) + if (fdw::hit_test(map, me->drawings_[i], ev->x, ev->y)) { + me->selected_ = i; + break; + } + if (me->selected_ >= 0) { + me->moving_ = true; + me->last_bar_ = bar; + me->last_price_ = price; + } + } else if (tool_sketch_kind(me->tool_, sk)) { + me->sketch_ = fdw::Drawing(); + me->sketch_.kind = sk; + me->sketch_.pts = {{bar, price}}; + me->sketching_ = true; + } + gtk_widget_queue_draw(w); + return TRUE; +} + +gboolean %CLASS%::on_chart_motion(GtkWidget* w, GdkEventMotion* ev, + gpointer self) { + auto* me = static_cast<%CLASS%*>(self); + if (me->candles_.empty()) return FALSE; + Layout L = me->layout(gtk_widget_get_allocated_width(w), + gtk_widget_get_allocated_height(w)); + fdw::Map map = me->current_map(L); + if (me->panning_) { + double slot = L.plot_w / std::max(1.0, me->view_count_); + double nb = std::round(me->pan_begin_ - (ev->x - me->pan_x_) / slot); + double maxb = (double)me->candles_.size() - me->view_count_; + if (nb < 0) nb = 0; + if (nb > maxb) nb = maxb; + me->view_begin_ = nb; + gtk_widget_queue_draw(w); + return TRUE; + } + if (me->moving_ && me->selected_ >= 0 && + me->selected_ < (int)me->drawings_.size()) { + double bar = map.bar_at(ev->x), price = map.price_at(ev->y); + fdw::move(me->drawings_[me->selected_], bar - me->last_bar_, + price - me->last_price_); + me->last_bar_ = bar; + me->last_price_ = price; + gtk_widget_queue_draw(w); + return TRUE; + } + if (me->sketching_) { + fdw::Anchor a{map.bar_at(ev->x), map.price_at(ev->y)}; + if (me->sketch_.kind == fdw::Kind::Freehand) { + const fdw::Anchor& last = me->sketch_.pts.back(); + double dx = map.px(a.bar) - map.px(last.bar); + double dy = map.py(a.price) - map.py(last.price); + if (dx * dx + dy * dy >= 9) me->sketch_.pts.push_back(a); + } else if (me->sketch_.pts.size() == 1) { + me->sketch_.pts.push_back(a); + } else { + me->sketch_.pts[1] = a; + } + gtk_widget_queue_draw(w); + return TRUE; + } + return FALSE; +} + +gboolean %CLASS%::on_chart_release(GtkWidget* w, GdkEventButton* ev, + gpointer self) { + auto* me = static_cast<%CLASS%*>(self); + if (ev->button != 1) return FALSE; + if (me->panning_) { + me->panning_ = false; + return TRUE; + } + if (me->moving_) { + me->moving_ = false; + return TRUE; + } + if (me->sketching_) { + me->sketching_ = false; + bool ok = me->sketch_.pts.size() >= 2 && + (me->sketch_.kind == fdw::Kind::Freehand || + me->sketch_.pts[0].bar != me->sketch_.pts[1].bar || + me->sketch_.pts[0].price != me->sketch_.pts[1].price); + if (ok) { + me->drawings_.push_back(me->sketch_); + me->selected_ = (int)me->drawings_.size() - 1; + } + gtk_widget_queue_draw(w); + return TRUE; + } + return FALSE; +} + +gboolean %CLASS%::on_chart_key(GtkWidget*, GdkEventKey* ev, gpointer self) { + auto* me = static_cast<%CLASS%*>(self); + int tool = -1; + switch (ev->keyval) { + case GDK_KEY_Escape: + me->tool_ = 0; + me->selected_ = -1; + break; + case GDK_KEY_t: case GDK_KEY_T: tool = 1; break; + case GDK_KEY_h: case GDK_KEY_H: tool = 2; break; + case GDK_KEY_v: case GDK_KEY_V: tool = 3; break; + case GDK_KEY_r: case GDK_KEY_R: tool = 4; break; + case GDK_KEY_l: case GDK_KEY_L: tool = 5; break; + case GDK_KEY_a: case GDK_KEY_A: tool = 6; break; + case GDK_KEY_x: case GDK_KEY_X: tool = 7; break; + case GDK_KEY_f: case GDK_KEY_F: tool = 8; break; + case GDK_KEY_d: case GDK_KEY_D: tool = 9; break; + case GDK_KEY_m: case GDK_KEY_M: tool = 10; break; + case GDK_KEY_e: case GDK_KEY_E: tool = 11; break; + case GDK_KEY_s: case GDK_KEY_S: tool = 12; break; + case GDK_KEY_Delete: + case GDK_KEY_KP_Delete: + case GDK_KEY_BackSpace: + if (me->selected_ >= 0 && + me->selected_ < (int)me->drawings_.size()) { + me->drawings_.erase(me->drawings_.begin() + me->selected_); + me->selected_ = -1; + } + break; + default: + return FALSE; + } + if (tool >= 0) me->tool_ = tool; + if (me->widget_) gtk_widget_queue_draw(me->widget_); + return TRUE; +} +)FDCHART7"; + std::string out; + size_t pos = 0; + while (true) { + size_t hit = s.find("%CLASS%", pos); + if (hit == std::string::npos) { + out += s.substr(pos); + break; + } + out += s.substr(pos, hit - pos); + out += cls; + pos = hit + 7; + } + return out; +} + +static std::string emit_widget_cpp(const Widget& w) { + const std::string& t = w.type; + const std::string M = "widget_"; + std::ostringstream o; + o << "// Generated by FDesigner (C++/GTK) — standalone widget class.\n" + << "#include \"" << w.name << ".h\"\n\n" + << "#include \n"; + if (t == "StockChart") + o << "#include \n#include \n#include " + "\n#include \n#include \n" + << "#include \n#include \n#include " + "\n#include \n"; + o << "\n"; + + if (t == "Toplevel") { + std::string title = prop_or(w, "title", ""); + if (title.empty()) title = w.name; + std::string resizable = + prop_or(w, "resizable", "true") == "true" ? "TRUE" : "FALSE"; + o << w.name << "::" << w.name << "() {\n" + << " // #region " << w.name << "\n" + << " widget_ = gtk_window_new(GTK_WINDOW_TOPLEVEL);\n" + << " if (!widget_) {\n" + << " util_error_print(\"" << w.name + << " creation error\");\n" + << " return;\n" + << " }\n" + << " gtk_widget_set_name(widget_, \"" << w.name << "\");\n" + << " gtk_window_set_title(GTK_WINDOW(widget_), \"" + << esc_c(title) << "\");\n" + << " gtk_window_set_default_size(GTK_WINDOW(widget_), " + << w.width << ", " << w.height << ");\n" + << " gtk_window_set_resizable(GTK_WINDOW(widget_), " + << resizable << ");\n" + << " GtkWidget* inner_fixed = gtk_fixed_new();\n" + << " gtk_container_add(GTK_CONTAINER(widget_), " + "inner_fixed);\n" + << colour_lines(w, M) + << connect_mouse(w.name, M, kWidgetStubs, 4) + << " // #endregion\n" + << "}\n"; + } else { + std::string place_target = M; + std::set skip; + std::string label = prop_or(w, "label", w.name); + std::string creation; + if (t == "Button") { + creation = " " + M + " = gtk_button_new_with_label(\"" + + esc_c(label) + "\");\n"; + skip = {"label"}; + } else if (t == "Label") { + creation = " " + M + " = gtk_label_new(\"" + esc_c(label) + "\");\n"; + skip = {"label"}; + } else if (t == "Entry") { + creation = " " + M + " = gtk_entry_new();\n"; + } else if (t == "Checkbutton") { + creation = " " + M + " = gtk_check_button_new_with_label(\"" + + esc_c(label) + "\");\n"; + skip = {"label"}; + } else if (t == "Radiobutton") { + creation = " " + M + " = gtk_radio_button_new_with_label(nullptr, \"" + + esc_c(label) + "\");\n"; + skip = {"label"}; + } else if (t == "Frame") { + creation = " " + M + " = gtk_frame_new(\"" + esc_c(label) + "\");\n"; + skip = {"label"}; + } else if (t == "Combobox") { + creation = " " + M + " = gtk_combo_box_text_new();\n"; + skip = {"active"}; + } else if (t == "Menubutton") { + creation = " " + M + " = gtk_menu_button_new();\n"; + skip = {"label"}; + } else if (t == "Scale") { + creation = M + + " = gtk_scale_new_with_range(" + "GTK_ORIENTATION_HORIZONTAL, 0, 100, 1);\n"; + } else if (t == "Scrollbar") { + creation = M + + " = gtk_scrollbar_new(GTK_ORIENTATION_HORIZONTAL,\n" + " gtk_adjustment_new(" + + fmt_double(prop_or(w, "value", "0")) + + ", 0, 100, 1, 10, 10));\n"; + skip = {"value"}; + } else if (t == "Canvas" || t == "StockChart" || t == "Listbox" || + t == "Text" || t == "Spreadsheet") { + // Wrapper types: an outer container is placed on the canvas, + // the member widget lives inside it. + std::string outer_decl; + if (t == "Canvas" || t == "StockChart") { + outer_decl = + " GtkWidget* frame = gtk_frame_new(nullptr);\n" + " gtk_frame_set_shadow_type(GTK_FRAME(frame), " + "GTK_SHADOW_IN);\n"; + place_target = "frame"; + } else { + outer_decl = + " GtkWidget* sw = gtk_scrolled_window_new(nullptr, " + "nullptr);\n" + " gtk_scrolled_window_set_shadow_type(" + "GTK_SCROLLED_WINDOW(sw), GTK_SHADOW_IN);\n"; + place_target = "sw"; + } + std::string inner; + if (t == "Canvas") inner = "gtk_drawing_area_new()"; + else if (t == "StockChart") { + inner = "gtk_drawing_area_new()"; + skip = {"data-file", "up-color", "down-color", + "max-candles", "indicators", "scale-mode", + "compare-files", "drawings"}; + } + else if (t == "Listbox") inner = "gtk_list_box_new()"; + else if (t == "Text") inner = "gtk_text_view_new()"; + else { // Spreadsheet + inner = "gtk_grid_new()"; + skip = {"rows", "columns"}; + } + creation = outer_decl + " " + M + " = " + inner + ";\n" + + " gtk_container_add(GTK_CONTAINER(" + + place_target + "), " + M + ");\n"; + } else { + creation = " " + M + " = gtk_label_new(\"" + esc_c(w.name) + "\");\n"; + } + + o << w.name << "::" << w.name << "(GtkWidget* fixed) {\n" + << " // #region " << w.name << "\n" + << creation + << " if (!" << M << ") {\n" + << " util_error_print(\"" << w.name + << " creation error\");\n" + << " return;\n" + << " }\n" + << " gtk_widget_set_name(" << M << ", \"" << w.name << "\");\n"; + + // Creation-time extras that need the live widget. + if (t == "Combobox") { + for (int i = 1; i <= 3; ++i) + o << " gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(" + << M << "), \"Item " << i << "\");\n"; + o << " gtk_combo_box_set_active(GTK_COMBO_BOX(" << M << "), " + << prop_or(w, "active", "0") << ");\n"; + } else if (t == "Menubutton") { + o << " gtk_button_set_label(GTK_BUTTON(" << M << "), \"" + << esc_c(label) << "\");\n"; + } else if (t == "Listbox") { + for (int i = 1; i <= 3; ++i) + o << " gtk_list_box_insert(GTK_LIST_BOX(" << M + << "), gtk_label_new(\"Item " << i << "\"), -1);\n"; + } else if (t == "Text") { + o << " gtk_text_buffer_set_text(gtk_text_view_get_buffer(" + "GTK_TEXT_VIEW(" + << M << ")), \"" << esc_c(w.name) << "\", -1);\n"; + } else if (t == "Spreadsheet") { + int rows = std::atoi(prop_or(w, "rows", "5").c_str()); + int cols = std::atoi(prop_or(w, "columns", "4").c_str()); + if (rows < 1) rows = 1; + if (cols < 1) cols = 1; + o << " gtk_grid_set_column_homogeneous(GTK_GRID(" << M + << "), TRUE);\n" + << " for (int r = 0; r < " << rows << "; ++r)\n" + << " for (int c = 0; c < " << cols << "; ++c) {\n" + << " GtkWidget* cell = gtk_entry_new();\n" + << " gtk_entry_set_width_chars(GTK_ENTRY(cell), " + "8);\n" + << " gtk_grid_attach(GTK_GRID(" << M + << "), cell, c, r, 1, 1);\n" + << " }\n"; + } else if (t == "StockChart") { + o << " gdk_rgba_parse(&up_, \"" + << esc_c(prop_or(w, "up-color", "rgb(0,200,0)")) << "\");\n" + << " gdk_rgba_parse(&down_, \"" + << esc_c(prop_or(w, "down-color", "rgb(220,0,0)")) << "\");\n" + << " max_candles_ = " + << prop_or(w, "max-candles", "120") << ";\n" + << " g_signal_connect(" << M + << ", \"draw\", G_CALLBACK(&" << w.name << "::on_draw), " + "this);\n"; + std::string file = prop_or(w, "data-file", ""); + if (!file.empty()) + o << " load_csv(\"" << esc_c(file) << "\");\n"; + for (const std::string& spec : + split_indicator_specs(prop_or(w, "indicators", ""))) + o << " add_indicator(\"" << esc_c(spec) << "\");\n"; + for (const std::string& cf : + split_compare_files(prop_or(w, "compare-files", ""))) + o << " add_compare(\"" << esc_c(cf) << "\");\n"; + for (const std::string& ds : + split_drawings_specs(prop_or(w, "drawings", ""))) + o << " add_drawing(\"" << esc_c(ds) << "\");\n"; + int scale_mode = + std::atoi(prop_or(w, "scale-mode", "0").c_str()); + if (scale_mode) + o << " set_scale_mode(" << scale_mode << ");\n"; + o << " gtk_widget_add_events(" << M + << ", GDK_SCROLL_MASK | GDK_BUTTON_PRESS_MASK |\n" + << " GDK_BUTTON_RELEASE_MASK | " + "GDK_POINTER_MOTION_MASK |\n" + << " GDK_KEY_PRESS_MASK);\n" + << " gtk_widget_set_can_focus(" << M << ", TRUE);\n" + << " g_signal_connect(" << M + << ", \"scroll-event\", G_CALLBACK(&" << w.name + << "::on_chart_scroll), this);\n" + << " g_signal_connect(" << M + << ", \"button-press-event\", G_CALLBACK(&" << w.name + << "::on_chart_press), this);\n" + << " g_signal_connect(" << M + << ", \"motion-notify-event\", G_CALLBACK(&" << w.name + << "::on_chart_motion), this);\n" + << " g_signal_connect(" << M + << ", \"button-release-event\", G_CALLBACK(&" << w.name + << "::on_chart_release), this);\n" + << " g_signal_connect(" << M + << ", \"key-press-event\", G_CALLBACK(&" << w.name + << "::on_chart_key), this);\n"; + } + + o << " gtk_fixed_put(GTK_FIXED(fixed), " << place_target << ", " + << w.x << ", " << w.y << ");\n" + << " gtk_widget_set_size_request(" << place_target << ", " + << w.width << ", " << w.height << ");\n" + << prop_lines(w, M, skip) + << colour_lines(w, M) + << connect_mouse(w.name, M, kWidgetStubs, 4) + << " // #endregion\n" + << "}\n"; + } + + if (w.type == "StockChart") { + o << chart_methods(w.name); + } + + o << "\n" + << w.name << "::~" << w.name << "() {}\n\n" + << "void " << w.name << "::util_error_print(const char* what) {\n" + << " std::fprintf(stderr, \"%s\\n\", what);\n" + << "}\n"; + stub_defs(o, w.name, kWidgetStubs); + return o.str(); +} + +// ------------------------------------------------------- window class + +static std::string emit_window_header(const std::string& cls, + const std::vector& widgets, + const std::vector& menus) { + std::ostringstream o; + o << "// Generated by FDesigner (C++/GTK) — all code, no .ui file.\n" + << "// Every widget is a standalone class; this class instantiates\n" + << "// them. Construction mirrors the Python fdesigner generator.\n" + << "#pragma once\n\n" + << "#include \n" + << "#include \n\n"; + for (const Widget& w : widgets) + o << "#include \"" << w.name << ".h\"\n"; + if (!widgets.empty()) o << "\n"; + o << "class " << cls << " {\n" + << "public:\n" + << " " << cls << "();\n" + << " ~" << cls << "();\n" + << " void show();\n" + << " GtkWidget* window() const { return window_; }\n\n" + << "private:\n" + << " GtkWidget* window_ = nullptr;\n" + << " GtkWidget* fixed_ = nullptr;\n"; + if (!menus.empty()) o << " GtkWidget* menubar_ = nullptr;\n"; + for (const Widget& w : widgets) + o << " " << w.name << "* " << w.name << "_ = nullptr;\n"; + for (const MenuDef& m : menus) + for (const std::string& item : m.items) + if (item != "-") + o << " GtkWidget* " << menu_item_id(m, item) + << "_ = nullptr;\n"; + o << "\n void util_error_print(const char* what);\n"; + o << "\n // Mouse handlers — Root_001 (the main window)\n"; + stub_decls(o, kRootStubs); + if (!menus.empty()) o << "\n // Menu handlers\n"; + for (const MenuDef& m : menus) + for (const std::string& item : m.items) + if (item != "-") + o << " static void on_" << menu_item_id(m, item) + << "_activate(GtkMenuItem*, gpointer self);\n"; + o << "};\n"; + return o.str(); +} + +static std::string emit_menubar_code(const std::string& cls, + const std::vector& menus) { + std::ostringstream o; + o << "\n // #region menu bar\n" + << " menubar_ = gtk_menu_bar_new();\n"; + for (const MenuDef& m : menus) { + std::string top = "menu_" + menu_id_part(m.label); + o << " {\n" + << " GtkWidget* " << top + << " = gtk_menu_item_new_with_label(\"" << esc_c(m.label) + << "\");\n" + << " GtkWidget* " << top << "_submenu = gtk_menu_new();\n"; + for (const std::string& item : m.items) { + if (item == "-") { + o << " gtk_menu_shell_append(GTK_MENU_SHELL(" << top + << "_submenu), gtk_separator_menu_item_new());\n"; + } else { + std::string id = menu_item_id(m, item); + o << " " << id + << "_ = gtk_menu_item_new_with_label(\"" << esc_c(item) + << "\");\n" + << " g_signal_connect(" << id + << "_, \"activate\", G_CALLBACK(&" << cls << "::on_" << id + << "_activate), this);\n" + << " gtk_menu_shell_append(GTK_MENU_SHELL(" << top + << "_submenu), " << id << "_);\n"; + } + } + o << " gtk_menu_item_set_submenu(GTK_MENU_ITEM(" << top + << "), " << top << "_submenu);\n" + << " gtk_menu_shell_append(GTK_MENU_SHELL(menubar_), " + << top << ");\n" + << " }\n"; + } + o << " gtk_box_pack_start(GTK_BOX(vbox), menubar_, FALSE, FALSE, 0);\n" + << " gtk_box_reorder_child(GTK_BOX(vbox), menubar_, 0);\n" + << " // #endregion\n"; + return o.str(); +} + +static std::string emit_window_cpp(const std::string& cls, const Project& p, + const std::vector& widgets, + const std::vector& menus) { + std::ostringstream o; + o << "// Generated by FDesigner (C++/GTK) — all code, no .ui file.\n" + << "#include \"" << cls << ".h\"\n\n" + << "#include \n\n" + << cls << "::" << cls << "() {\n" + << " // #region root setup\n" + << " window_ = gtk_window_new(GTK_WINDOW_TOPLEVEL);\n" + << " gtk_widget_set_name(window_, \"main_window\");\n" + << " gtk_window_set_title(GTK_WINDOW(window_), \"" + << esc_c(p.title) << "\");\n" + << " gtk_window_set_default_size(GTK_WINDOW(window_), " + << p.win_width << ", " << p.win_height << ");\n"; + if (!menus.empty()) + o << " GtkWidget* vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, " + "0);\n" + << " gtk_container_add(GTK_CONTAINER(window_), vbox);\n"; + o << " fixed_ = gtk_fixed_new();\n"; + if (!menus.empty()) + o << " gtk_box_pack_start(GTK_BOX(vbox), fixed_, TRUE, TRUE, 0);\n"; + else + o << " gtk_container_add(GTK_CONTAINER(window_), fixed_);\n"; + o << connect_mouse(cls, "window_", kRootStubs, 4) + << " g_signal_connect(window_, \"destroy\", " + "G_CALLBACK(gtk_main_quit), nullptr);\n" + << " // #endregion\n"; + if (!menus.empty()) o << emit_menubar_code(cls, menus); + for (const Widget& w : widgets) { + o << "\n // #region " << w.name << "\n" + << " " << w.name << "_ = new " << w.name << "("; + if (w.type != "Toplevel") o << "fixed_"; + o << ");\n" + << " // #endregion\n"; + } + if (!p.background.empty()) { + o << "\n // #region colours\n" + << " GtkCssProvider* css = gtk_css_provider_new();\n" + << " gtk_css_provider_load_from_data(css,\n" + << " \"#main_window { background: " << p.background + << "; }\\n\"\n" + << " , -1, nullptr);\n" + << " gtk_style_context_add_provider_for_screen(\n" + << " gdk_screen_get_default(), GTK_STYLE_PROVIDER(css),\n" + << " GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);\n" + << " // #endregion\n"; + } + o << "}\n\n" + << cls << "::~" << cls << "() {\n"; + for (auto it = widgets.rbegin(); it != widgets.rend(); ++it) + o << " delete " << it->name << "_;\n"; + o << "}\n\n" + << "void " << cls << "::show() {\n" + << " if (window_) gtk_widget_show_all(window_);\n" + << "}\n\n" + << "void " << cls << "::util_error_print(const char* what) {\n" + << " std::fprintf(stderr, \"%s\\n\", what);\n" + << "}\n"; + stub_defs(o, cls, kRootStubs); + for (const MenuDef& m : menus) + for (const std::string& item : m.items) + if (item != "-") + o << "\nvoid " << cls << "::on_" << menu_item_id(m, item) + << "_activate(GtkMenuItem*, gpointer self) {\n" + << " auto* me = static_cast<" << cls << "*>(self);\n" + << " (void)me;\n" + << " // Code block here\n" + << "}\n"; + return o.str(); +} + +static std::string emit_main(const std::string& cls) { + std::ostringstream o; + o << "// Generated by FDesigner (C++/GTK).\n" + << "#include \n\n" + << "#include \"" << cls << ".h\"\n\n" + << "int main(int argc, char** argv) {\n" + << " gtk_init(&argc, &argv);\n" + << " " << cls << " win; // a = my_class()\n" + << " win.show();\n" + << " gtk_main(); // root.mainloop()\n" + << " return 0;\n" + << "}\n"; + return o.str(); +} + +static std::string emit_makefile(const std::string& cls) { + std::ostringstream o; + o << "# Generated by FDesigner (C++/GTK). One class per widget:\n" + << "# SRCS picks up every .cpp, so hand-added classes build too.\n" + << "CXX ?= g++\n" + << "CXXFLAGS ?= -O2 -Wall -std=c++17\n" + << "SRCS := $(wildcard *.cpp)\n\n" + << "app: $(SRCS) " << cls << ".h\n" + << "\t$(CXX) $(CXXFLAGS) -o $@ $(SRCS) $$(pkg-config --cflags " + "--libs gtk+-3.0)\n\n" + << "clean:\n" + << "\trm -f app\n\n" + << ".PHONY: clean\n"; + return o.str(); +} + +// ---------------------------------------------------------------- driver + +static bool write_file(const fs::path& path, const std::string& content, + GenResult& r) { + std::ofstream f(path); + if (!f) { + r.error = "cannot write " + path.string(); + return false; + } + f << content; + if (!f) { + r.error = "write failed for " + path.string(); + return false; + } + r.files.push_back(path.string()); + return true; +} + +GenResult generate_project(const Project& project, + const std::vector& widgets, + const std::string& out_dir) { + GenResult r; + std::error_code ec; + fs::create_directories(out_dir, ec); + if (ec) { + r.error = "cannot create " + out_dir + ": " + ec.message(); + return r; + } + std::string cls = sanitize_class(project.class_name); + std::vector menus = parse_menus(project.menubar); + + fs::path dir(out_dir); + if (!write_file(dir / (cls + ".h"), + emit_window_header(cls, widgets, menus), r)) + return r; + if (!write_file(dir / (cls + ".cpp"), + emit_window_cpp(cls, project, widgets, menus), r)) + return r; + for (const Widget& w : widgets) { + if (!write_file(dir / (w.name + ".h"), emit_widget_header(w), r)) + return r; + if (!write_file(dir / (w.name + ".cpp"), emit_widget_cpp(w), r)) + return r; + } + bool has_chart = false; + for (const Widget& w : widgets) + if (w.type == "StockChart") has_chart = true; + if (has_chart && + !write_file(dir / "indicators.h", kIndicatorsHeader, r)) + return r; + if (has_chart && + !write_file(dir / "drawings.h", kDrawingsHeader, r)) + return r; + if (!write_file(dir / "main.cpp", emit_main(cls), r)) return r; + if (!write_file(dir / "Makefile", emit_makefile(cls), r)) return r; + r.ok = true; + return r; +} diff --git a/src/generator.h b/src/generator.h new file mode 100644 index 0000000..ac7148a --- /dev/null +++ b/src/generator.h @@ -0,0 +1,29 @@ +// FDesigner code generator. +// Turns the designed window (Project + widgets) into a compilable C++/GTK +// project. The output is ALL CODE — no .ui file — and every widget is a +// STANDALONE CLASS in its own .h/.cpp pair, mirroring the Python +// generator's boilerplate: the class creates and configures its widget in +// its constructor and carries the same nine mouse-event stub handlers +// (buttonPress1-3, dblbuttonPress1-3, buttonRelease1-3) plus a +// util_error_print helper. The generated window class just instantiates +// one widget class per designed widget. Because construction is ordinary +// sequential code in per-widget classes, a stepping debugger can walk it +// line by line, and adding a widget by hand means copying one class. + +#pragma once + +#include +#include + +#include "model.h" + +struct GenResult { + bool ok = false; + std::string error; + std::vector files; // paths written, in order +}; + +// Writes the project into out_dir (created if needed). +GenResult generate_project(const Project& project, + const std::vector& widgets, + const std::string& out_dir); diff --git a/src/indicators.h b/src/indicators.h new file mode 100644 index 0000000..6e8700f --- /dev/null +++ b/src/indicators.h @@ -0,0 +1,3885 @@ +// indicators.h — 250 technical indicator submodules for the FDesigner +// StockChart widget, in ONE self-contained header. +// +// Every indicator is a named module in the registry below: it consumes +// Massive-format OHLCV bars (the same data StockChart loads from CSV), +// computes one or more series, and plots itself onto the chart's canvas — +// as a price-pane overlay, in its own oscillator pane, or as markers. +// Indicators have no palette button of their own; a StockChart enables +// them by name, e.g. add_indicator("SMA(20)") or the designer's +// "indicators" property: "SMA(20), EMA(50), RSI, Bollinger Bands". +// +// Shared math primitives (rolling SMA/EMA/stdev/ATR...) live at the top; +// each indicator's own formula is its own function further down. + +#pragma once + +#include + +#include +#include +#include +#include +#include +#include + +namespace fdi { + +// One OHLCV bar (Massive aggregates: open/high/low/close/volume, +// window_start in ns). +struct Bar { + double open = 0, high = 0, low = 0, close = 0, volume = 0; + long long time = 0; +}; + +using Series = std::vector; +using Lines = std::vector; + +inline double nanv() { return std::numeric_limits::quiet_NaN(); } +inline bool fin(double v) { return std::isfinite(v); } + +// ------------------------------------------------------------- fields + +inline Series field(const std::vector& b, double Bar::* member) { + Series s(b.size()); + for (size_t i = 0; i < b.size(); ++i) s[i] = b[i].*member; + return s; +} +inline Series closes(const std::vector& b) { + Series s(b.size()); + for (size_t i = 0; i < b.size(); ++i) s[i] = b[i].close; + return s; +} +inline Series highs(const std::vector& b) { + Series s(b.size()); + for (size_t i = 0; i < b.size(); ++i) s[i] = b[i].high; + return s; +} +inline Series lows(const std::vector& b) { + Series s(b.size()); + for (size_t i = 0; i < b.size(); ++i) s[i] = b[i].low; + return s; +} +inline Series opens(const std::vector& b) { + Series s(b.size()); + for (size_t i = 0; i < b.size(); ++i) s[i] = b[i].open; + return s; +} +inline Series vols(const std::vector& b) { + Series s(b.size()); + for (size_t i = 0; i < b.size(); ++i) s[i] = b[i].volume; + return s; +} +inline Series hl2(const std::vector& b) { + Series s(b.size()); + for (size_t i = 0; i < b.size(); ++i) s[i] = (b[i].high + b[i].low) / 2; + return s; +} +inline Series hlc3(const std::vector& b) { + Series s(b.size()); + for (size_t i = 0; i < b.size(); ++i) + s[i] = (b[i].high + b[i].low + b[i].close) / 3; + return s; +} +inline Series ohlc4(const std::vector& b) { + Series s(b.size()); + for (size_t i = 0; i < b.size(); ++i) + s[i] = (b[i].open + b[i].high + b[i].low + b[i].close) / 4; + return s; +} +inline Series true_range(const std::vector& b) { + Series s(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) { + double tr = b[i].high - b[i].low; + if (i > 0) { + tr = std::max(tr, std::fabs(b[i].high - b[i - 1].close)); + tr = std::max(tr, std::fabs(b[i].low - b[i - 1].close)); + } + s[i] = tr; + } + return s; +} + +// ------------------------------------------------------------- rolling + +inline Series sma_s(const Series& in, int n) { + Series out(in.size(), nanv()); + if (n < 1) return out; + double sum = 0; + int cnt = 0; + for (size_t i = 0; i < in.size(); ++i) { + if (fin(in[i])) { + sum += in[i]; + ++cnt; + } + if (i >= (size_t)n && fin(in[i - n])) { + sum -= in[i - n]; + --cnt; + } + if (cnt == n) out[i] = sum / n; + } + return out; +} + +inline Series ema_s(const Series& in, int n) { + Series out(in.size(), nanv()); + if (n < 1) return out; + double k = 2.0 / (n + 1), e = 0; + bool started = false; + for (size_t i = 0; i < in.size(); ++i) { + if (!fin(in[i])) continue; + if (!started) { + e = in[i]; + started = true; + } else { + e = in[i] * k + e * (1 - k); + } + if (i >= (size_t)(n - 1)) out[i] = e; + } + return out; +} + +// Wilder's smoothing (SMMA / RMA). +inline Series smma_s(const Series& in, int n) { + Series out(in.size(), nanv()); + if (n < 1) return out; + double e = 0; + bool started = false; + for (size_t i = 0; i < in.size(); ++i) { + if (!fin(in[i])) continue; + if (!started) { + e = in[i]; + started = true; + } else { + e = (e * (n - 1) + in[i]) / n; + } + if (i >= (size_t)(n - 1)) out[i] = e; + } + return out; +} + +inline Series wma_s(const Series& in, int n) { + Series out(in.size(), nanv()); + if (n < 1) return out; + double denom = n * (n + 1) / 2.0; + for (size_t i = (size_t)(n - 1); i < in.size(); ++i) { + double sum = 0; + bool ok = true; + for (int j = 0; j < n; ++j) { + double v = in[i - j]; + if (!fin(v)) { + ok = false; + break; + } + sum += v * (n - j); + } + if (ok) out[i] = sum / denom; + } + return out; +} + +inline Series rolling_max(const Series& in, int n) { + Series out(in.size(), nanv()); + for (size_t i = 0; i < in.size(); ++i) { + if (i + 1 < (size_t)n) continue; + double m = -1e300; + bool ok = true; + for (int j = 0; j < n; ++j) { + if (!fin(in[i - j])) ok = false; + m = std::max(m, in[i - j]); + } + if (ok) out[i] = m; + } + return out; +} + +inline Series rolling_min(const Series& in, int n) { + Series out(in.size(), nanv()); + for (size_t i = 0; i < in.size(); ++i) { + if (i + 1 < (size_t)n) continue; + double m = 1e300; + bool ok = true; + for (int j = 0; j < n; ++j) { + if (!fin(in[i - j])) ok = false; + m = std::min(m, in[i - j]); + } + if (ok) out[i] = m; + } + return out; +} + +inline Series rolling_sum(const Series& in, int n) { + Series out(in.size(), nanv()); + double sum = 0; + int cnt = 0; + for (size_t i = 0; i < in.size(); ++i) { + if (fin(in[i])) { + sum += in[i]; + ++cnt; + } + if (i >= (size_t)n && fin(in[i - n])) { + sum -= in[i - n]; + --cnt; + } + if (cnt == n) out[i] = sum; + } + return out; +} + +inline Series stdev_s(const Series& in, int n, bool sample = false) { + Series out(in.size(), nanv()); + Series mean = sma_s(in, n); + for (size_t i = 0; i < in.size(); ++i) { + if (!fin(mean[i])) continue; + double ss = 0; + for (int j = 0; j < n; ++j) { + double d = in[i - j] - mean[i]; + ss += d * d; + } + out[i] = std::sqrt(ss / (sample ? n - 1 : n)); + } + return out; +} + +inline Series atr_s(const std::vector& b, int n) { + return smma_s(true_range(b), n); +} + +// RSI on a price series (Wilder). +inline Series rsi_s(const Series& in, int n) { + Series out(in.size(), nanv()); + if (n < 1 || in.size() < 2) return out; + double gain = 0, loss = 0; + for (size_t i = 1; i < in.size(); ++i) { + double d = in[i] - in[i - 1]; + double g = d > 0 ? d : 0, l = d < 0 ? -d : 0; + if (i == 1) { + gain = g; + loss = l; + } else { + gain = (gain * (n - 1) + g) / n; + loss = (loss * (n - 1) + l) / n; + } + if (i >= (size_t)n) { + if (loss == 0) + out[i] = 100; + else { + double rs = gain / loss; + out[i] = 100 - 100 / (1 + rs); + } + } + } + return out; +} + +// Element-wise helpers. +inline Series combine(const Series& a, const Series& b, char op) { + Series out(a.size(), nanv()); + for (size_t i = 0; i < a.size() && i < b.size(); ++i) { + if (!fin(a[i]) || !fin(b[i])) continue; + switch (op) { + case '+': out[i] = a[i] + b[i]; break; + case '-': out[i] = a[i] - b[i]; break; + case '*': out[i] = a[i] * b[i]; break; + case '/': out[i] = b[i] != 0 ? a[i] / b[i] : nanv(); break; + } + } + return out; +} +inline Series scale_s(const Series& a, double k) { + Series out(a.size(), nanv()); + for (size_t i = 0; i < a.size(); ++i) + if (fin(a[i])) out[i] = a[i] * k; + return out; +} +inline Series shift_s(const Series& a, int n) { // delay by n bars + Series out(a.size(), nanv()); + for (size_t i = 0; i < a.size(); ++i) { + long src = (long)i - n; + if (src >= 0 && src < (long)a.size() && fin(a[src])) + out[i] = a[src]; + } + return out; +} +inline Series diff_s(const Series& a, int n = 1) { + Series out(a.size(), nanv()); + for (size_t i = 0; i < a.size(); ++i) + if ((int)i - n >= 0 && fin(a[i]) && fin(a[i - n])) + out[i] = a[i] - a[i - n]; + return out; +} + +// ------------------------------------------------------------- framework + +enum class PlotKind { Overlay, Pane, Markers }; +enum class Style { Lines, Bars }; + +using ComputeFn = + std::function&, const std::vector&)>; + +struct Def { + const char* name; + PlotKind kind; + Style style; + int color; // palette index (line j uses color + j) + const char* defaults; // comma-separated default parameters + ComputeFn fn; +}; + +// Area an indicator draws into. For Overlay/Markers, lo/hi are the price +// scale; for Pane the indicator scales its own lines into the area. +struct PlotArea { + cairo_t* cr = nullptr; + double x = 0, y = 0, w = 0, h = 0; + size_t begin = 0, count = 0; + double lo = 0, hi = 1; +}; + +inline const double kPalette[][3] = { + {0.13, 0.35, 0.85}, // blue + {0.95, 0.55, 0.10}, // orange + {0.55, 0.25, 0.80}, // purple + {0.00, 0.60, 0.60}, // teal + {0.85, 0.20, 0.55}, // magenta + {0.55, 0.35, 0.15}, // brown + {0.20, 0.65, 0.25}, // green + {0.80, 0.15, 0.15}, // red + {0.10, 0.55, 0.80}, // steel blue + {0.65, 0.65, 0.10}, // olive + {0.35, 0.35, 0.35}, // grey + {0.90, 0.70, 0.10}, // gold + {0.25, 0.25, 0.70}, // indigo + {0.60, 0.20, 0.20}, // brick +}; +inline constexpr int kPaletteSize = 14; + +class Indicator { +public: + Indicator(const Def* def, std::vector params) + : def_(def), params_(std::move(params)) {} + + const char* name() const { return def_->name; } + PlotKind kind() const { return def_->kind; } + Style style() const { return def_->style; } + int color() const { return def_->color; } + const std::vector& params() const { return params_; } + + Lines compute(const std::vector& bars) const { + return def_->fn(bars, params_); + } + + // Draw this indicator into `area` (which covers bars + // [begin, begin+count) of `bars`). + void draw(const PlotArea& area, const std::vector& bars) const { + if (!area.cr || area.count == 0 || area.w < 4 || area.h < 4) return; + Lines lines = compute(bars); + if (lines.empty()) return; + cairo_t* cr = area.cr; + size_t end = std::min(bars.size(), area.begin + area.count); + auto X = [&](size_t i) { + return area.x + (i - area.begin + 0.5) * (area.w / area.count); + }; + if (def_->kind == PlotKind::Markers) { + const Series& m = lines[0]; + const double* col = kPalette[def_->color % kPaletteSize]; + double slot = area.w / area.count; + double hw = std::max(3.0, slot * 0.35); // visible at any zoom + for (size_t i = area.begin; i < end; ++i) { + if (i >= m.size() || !fin(m[i]) || m[i] == 0) continue; + double cx = X(i); + bool bull = m[i] > 0; + double py = bull ? area.y + area.h - + (bars[i].low - area.lo) / + (area.hi - area.lo) * area.h + + 8 + : area.y + + (area.hi - bars[i].high) / + (area.hi - area.lo) * area.h - + 8; + py = std::min(std::max(py, area.y + 8), + area.y + area.h - 8); + cairo_set_source_rgba(cr, col[0], col[1], col[2], 0.9); + cairo_move_to(cr, cx, py); + cairo_line_to(cr, cx - hw, py + (bull ? 7 : -7)); + cairo_line_to(cr, cx + hw, py + (bull ? 7 : -7)); + cairo_close_path(cr); + cairo_fill(cr); + } + return; + } + // Scale: overlays use the price scale; panes scale their own. + double lo = area.lo, hi = area.hi; + if (def_->kind == PlotKind::Pane) { + lo = 1e300; + hi = -1e300; + for (const Series& s : lines) + for (size_t i = area.begin; i < end && i < s.size(); ++i) + if (fin(s[i])) { + lo = std::min(lo, s[i]); + hi = std::max(hi, s[i]); + } + if (lo > hi) return; // nothing finite to draw + if (def_->style == Style::Bars) { + lo = std::min(lo, 0.0); + hi = std::max(hi, 0.0); + } + if (hi - lo < 1e-12) hi = lo + 1; + } + auto Y = [&](double v) { + return area.y + (hi - v) / (hi - lo) * area.h; + }; + for (size_t j = 0; j < lines.size(); ++j) { + const Series& s = lines[j]; + const double* col = + kPalette[(def_->color + (int)j) % kPaletteSize]; + cairo_set_source_rgba(cr, col[0], col[1], col[2], 1.0); + if (def_->style == Style::Bars) { + double bw = std::max(1.0, area.w / area.count * 0.7); + double y0 = Y(0); + for (size_t i = area.begin; i < end && i < s.size(); ++i) { + if (!fin(s[i])) continue; + double y1 = Y(s[i]); + cairo_rectangle(cr, X(i) - bw / 2, std::min(y0, y1), + bw, std::max(1.0, std::fabs(y1 - y0))); + cairo_fill(cr); + } + } else { + cairo_set_line_width(cr, 1.2); + bool pen = false; + for (size_t i = area.begin; i < end && i < s.size(); ++i) { + if (!fin(s[i])) { + pen = false; + continue; + } + if (!pen) { + cairo_move_to(cr, X(i), Y(s[i])); + pen = true; + } else { + cairo_line_to(cr, X(i), Y(s[i])); + } + } + cairo_stroke(cr); + } + } + } + +private: + const Def* def_; + std::vector params_; +}; + +// Registry (defined by the family tables at the bottom of this file). +inline const std::vector& all_defs(); +inline const Def* find_def(const std::string& name); +inline std::vector indicator_names(); +inline std::unique_ptr make_indicator( + const std::string& spec, std::string* err = nullptr); + +// ============================================================ averages +// Moving averages and price filters (price-pane overlays). + +static int pi(const std::vector& p, size_t i, int dflt) { + return i < p.size() && p[i] > 0 ? (int)p[i] : dflt; +} +static double pd(const std::vector& p, size_t i, double dflt) { + return i < p.size() ? p[i] : dflt; +} + +static Lines f_sma(const std::vector& b, const std::vector& p) { + return {sma_s(closes(b), pi(p, 0, 20))}; +} +static Lines f_ema(const std::vector& b, const std::vector& p) { + return {ema_s(closes(b), pi(p, 0, 20))}; +} +static Lines f_wma(const std::vector& b, const std::vector& p) { + return {wma_s(closes(b), pi(p, 0, 20))}; +} +static Lines f_smma(const std::vector& b, const std::vector& p) { + return {smma_s(closes(b), pi(p, 0, 20))}; +} +static Lines f_dema(const std::vector& b, const std::vector& p) { + Series e1 = ema_s(closes(b), pi(p, 0, 20)); + Series e2 = ema_s(e1, pi(p, 0, 20)); + return {combine(scale_s(e1, 2), e2, '-')}; +} +static Lines f_tema(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 20); + Series e1 = ema_s(closes(b), n), e2 = ema_s(e1, n), e3 = ema_s(e2, n); + return {combine(combine(scale_s(e1, 3), scale_s(e2, 3), '-'), e3, '+')}; +} +static Lines f_trima(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 20); + return {sma_s(sma_s(closes(b), (n + 1) / 2), n / 2 + 1)}; +} +static Lines f_kama(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 10); + Series c = closes(b), out(c.size(), nanv()); + Series ad = diff_s(c); + for (size_t i = 0; i < ad.size(); ++i) + if (fin(ad[i])) ad[i] = std::fabs(ad[i]); + Series vol = rolling_sum(ad, n); + double fast = 2.0 / 3, slow = 2.0 / 31; + for (size_t i = (size_t)n; i < c.size(); ++i) { + if (!fin(vol[i]) || vol[i] == 0) continue; + double er = std::fabs(c[i] - c[i - n]) / vol[i]; + double sc = er * (fast - slow) + slow; + sc *= sc; + out[i] = fin(out[i - 1]) ? out[i - 1] + sc * (c[i] - out[i - 1]) + : c[i]; + } + return {out}; +} +static Lines f_t3(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 10); + double a = pd(p, 1, 0.7); + Series e = closes(b); + for (int i = 0; i < 6; ++i) e = ema_s(e, n); + Series e3 = closes(b); + for (int i = 0; i < 3; ++i) e3 = ema_s(e3, n); + Series e4 = ema_s(e3, n), e5 = ema_s(e4, n), e6 = ema_s(e5, n); + double c1 = -a * a * a, c2 = 3 * a * a + 3 * a * a * a, + c3 = -6 * a * a - 3 * a - 3 * a * a * a, + c4 = 1 + 3 * a + a * a * a + 3 * a * a; + (void)e; + return {combine( + combine(scale_s(e6, c1), scale_s(e5, c2), '+'), + combine(scale_s(e4, c3), scale_s(e3, c4), '+'), '+')}; +} +static Lines f_hma(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 20); + Series diff = combine(scale_s(wma_s(closes(b), n / 2), 2), + wma_s(closes(b), n), '-'); + return {wma_s(diff, (int)std::sqrt(n))}; +} +static Lines f_zlema(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 20), lag = (n - 1) / 2; + Series adj = combine(scale_s(closes(b), 2), shift_s(closes(b), lag), '-'); + return {ema_s(adj, n)}; +} +static Lines f_vwma(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 20); + Series cv(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) cv[i] = b[i].close * b[i].volume; + return {combine(rolling_sum(cv, n), rolling_sum(vols(b), n), '/')}; +} +static Lines f_lsma(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 25); + Series c = closes(b), out(c.size(), nanv()); + for (size_t i = (size_t)(n - 1); i < c.size(); ++i) { + double sx = 0, sy = 0, sxx = 0, sxy = 0; + bool ok = true; + for (int j = 0; j < n; ++j) { + double x = j, y = c[i - n + 1 + j]; + if (!fin(y)) ok = false; + sx += x; + sy += y; + sxx += x * x; + sxy += x * y; + } + if (!ok) continue; + double den = n * sxx - sx * sx; + if (den == 0) continue; + double slope = (n * sxy - sx * sy) / den; + double inter = (sy - slope * sx) / n; + out[i] = inter + slope * (n - 1); + } + return {out}; +} +static Lines f_alma(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 9); + double off = pd(p, 1, 0.85), sig = pd(p, 2, 6.0); + Series c = closes(b), out(c.size(), nanv()); + double m = std::floor(off * (n - 1)), s = n / sig, wsum = 0; + std::vector w(n); + for (int j = 0; j < n; ++j) { + w[j] = std::exp(-((j - m) * (j - m)) / (2 * s * s)); + wsum += w[j]; + } + for (size_t i = (size_t)(n - 1); i < c.size(); ++i) { + double sum = 0; + for (int j = 0; j < n; ++j) sum += w[j] * c[i - n + 1 + j]; + out[i] = sum / wsum; + } + return {out}; +} +static Lines f_mcginley(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 14); + Series c = closes(b), out(c.size(), nanv()); + for (size_t i = 0; i < c.size(); ++i) { + if (i == 0 || !fin(out[i - 1])) { + out[i] = c[i]; + continue; + } + double ratio = c[i] / out[i - 1]; + double denom = n * std::pow(ratio, 4); + out[i] = out[i - 1] + (denom > 0 ? (c[i] - out[i - 1]) / denom : 0); + } + return {out}; +} +static Lines f_gma(const std::vector& b, const std::vector& p) { + Series lg = closes(b); + for (double& v : lg) v = v > 0 ? std::log(v) : nanv(); + Series m = sma_s(lg, pi(p, 0, 20)); + for (double& v : m) + if (fin(v)) v = std::exp(v); + return {m}; +} +static Lines f_median_price(const std::vector& b, + const std::vector&) { + return {hl2(b)}; +} +static Lines f_typical_price(const std::vector& b, + const std::vector&) { + return {hlc3(b)}; +} +static Lines f_weighted_close(const std::vector& b, + const std::vector&) { + Series s(b.size()); + for (size_t i = 0; i < b.size(); ++i) + s[i] = (b[i].high + b[i].low + 2 * b[i].close) / 4; + return {s}; +} +static Lines f_midprice(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 14); + return {scale_s(combine(rolling_max(highs(b), n), + rolling_min(lows(b), n), '+'), + 0.5)}; +} +static Lines f_midpoint(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 14); + return {scale_s(combine(rolling_max(closes(b), n), + rolling_min(closes(b), n), '+'), + 0.5)}; +} +static Lines f_vwap(const std::vector& b, const std::vector&) { + Series tp = hlc3(b), out(b.size(), nanv()); + double pv = 0, vv = 0; + for (size_t i = 0; i < b.size(); ++i) { + pv += tp[i] * b[i].volume; + vv += b[i].volume; + if (vv > 0) out[i] = pv / vv; + } + return {out}; +} +static Lines f_heikin_ashi(const std::vector& b, + const std::vector&) { + Series out(b.size(), nanv()); + double hao = 0; + for (size_t i = 0; i < b.size(); ++i) { + double hac = (b[i].open + b[i].high + b[i].low + b[i].close) / 4; + hao = i == 0 ? (b[i].open + b[i].close) / 2 + : (hao + out[i - 1]) / 2; + (void)hao; + out[i] = hac; + } + return {out}; +} +// Ehlers two-pole super smoother (price filter). +static Series super_smoother_s(const Series& in, int n) { + Series out(in.size(), nanv()); + double a = std::exp(-1.414 * M_PI / n); + double b = 2 * a * std::cos(1.414 * M_PI / n); + double c2 = b, c3 = -a * a, c1 = 1 - c2 - c3; + for (size_t i = 0; i < in.size(); ++i) { + if (!fin(in[i])) continue; + double x1 = i > 0 && fin(in[i - 1]) ? in[i - 1] : in[i]; + double y1 = i > 0 && fin(out[i - 1]) ? out[i - 1] : in[i]; + double y2 = i > 1 && fin(out[i - 2]) ? out[i - 2] : y1; + out[i] = c1 * (in[i] + x1) / 2 + c2 * y1 + c3 * y2; + } + return out; +} +static Lines f_super_smoother(const std::vector& b, + const std::vector& p) { + return {super_smoother_s(closes(b), pi(p, 0, 14))}; +} +static Lines f_decycler(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 30); + Series c = closes(b), hp(c.size(), nanv()), out(c.size(), nanv()); + double w = 0.707 * 2 * M_PI / n; + double alpha = (std::cos(w) + std::sin(w) - 1) / std::cos(w); + for (size_t i = 0; i < c.size(); ++i) { + double x1 = i > 0 ? c[i - 1] : c[i], x2 = i > 1 ? c[i - 2] : c[i]; + double h1 = i > 0 && fin(hp[i - 1]) ? hp[i - 1] : 0; + double h2 = i > 1 && fin(hp[i - 2]) ? hp[i - 2] : 0; + double k = 1 - alpha / 2; + hp[i] = k * k * (c[i] - 2 * x1 + x2) + 2 * (1 - alpha) * h1 - + (1 - alpha) * (1 - alpha) * h2; + out[i] = c[i] - hp[i]; + } + return {out}; +} +static Lines f_gaussian(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 14); + Series s = closes(b); + double alpha = 2.0 / (n + 1); + for (int pole = 0; pole < 4; ++pole) { // 4-pole cascade approximates + Series o(s.size(), nanv()); // a gaussian filter + for (size_t i = 0; i < s.size(); ++i) { + if (!fin(s[i])) continue; + double prev = i > 0 && fin(o[i - 1]) ? o[i - 1] : s[i]; + o[i] = prev + alpha * (s[i] - prev); + } + s = o; + } + return {s}; +} +static Lines f_butterworth(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 14); + Series c = closes(b), out(c.size(), nanv()); + double a = std::exp(-1.414 * M_PI / n); + double bb = 2 * a * std::cos(1.414 * M_PI / n), cc = a * a; + for (size_t i = 0; i < c.size(); ++i) { + double x1 = i > 0 ? c[i - 1] : c[i], x2 = i > 1 ? c[i - 2] : c[i]; + double y1 = i > 0 && fin(out[i - 1]) ? out[i - 1] : c[i]; + double y2 = i > 1 && fin(out[i - 2]) ? out[i - 2] : y1; + out[i] = (1 - bb + cc) / 4 * (c[i] + 2 * x1 + x2) + bb * y1 - + cc * y2; + } + return {out}; +} +static Lines f_laguerre(const std::vector& b, + const std::vector& p) { + double g = pd(p, 0, 0.8); + Series c = closes(b), out(c.size(), nanv()); + double L[4] = {0, 0, 0, 0}; + for (size_t i = 0; i < c.size(); ++i) { + double o0 = L[0], o1 = L[1], o2 = L[2], o3 = L[3]; + L[0] = (1 - g) * c[i] + g * o0; + L[1] = -g * L[0] + o0 + g * o1; + L[2] = -g * L[1] + o1 + g * o2; + L[3] = -g * L[2] + o2 + g * o3; + out[i] = (L[0] + 2 * L[1] + 2 * L[2] + L[3]) / 6; + } + return {out}; +} +static Lines f_vidya(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 14); + Series c = closes(b), out(c.size(), nanv()); + Series sd = stdev_s(c, n); + Series sd_lag = shift_s(sd, n); + for (size_t i = 0; i < c.size(); ++i) { + if (!fin(sd[i]) || !fin(sd_lag[i]) || sd_lag[i] == 0) continue; + double vr = sd[i] / sd_lag[i]; + double alpha = vr * 2.0 / (n + 1); + out[i] = fin(out[i - 1]) ? out[i - 1] + alpha * (c[i] - out[i - 1]) + : c[i]; + } + return {out}; +} +static Lines f_frama(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 16); + if (n % 2) ++n; + Series c = closes(b), out(c.size(), nanv()); + Series hh = highs(b), ll = lows(b); + for (size_t i = (size_t)(2 * n - 1); i < c.size(); ++i) { + auto range = [&](size_t from, size_t to) { + double mx = -1e300, mn = 1e300; + for (size_t k = from; k <= to; ++k) { + mx = std::max(mx, hh[k]); + mn = std::min(mn, ll[k]); + } + return mx - mn; + }; + double n1 = range(i - 2 * n + 1, i - n) / n; + double n2 = range(i - n + 1, i) / n; + double n3 = range(i - 2 * n + 1, i) / (2.0 * n); + if (n1 <= 0 || n2 <= 0 || n3 <= 0) continue; + double dim = (std::log(n1 + n2) - std::log(n3)) / std::log(2); + double alpha = std::exp(-4.6 * (dim - 1)); + alpha = std::min(1.0, std::max(0.01, alpha)); + out[i] = fin(out[i - 1]) ? alpha * c[i] + (1 - alpha) * out[i - 1] + : c[i]; + } + return {out}; +} +static Lines f_vma(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 14); + Series c = closes(b), out(c.size(), nanv()); + Series sd = stdev_s(c, n); + for (size_t i = 0; i < c.size(); ++i) { + double sdl = i >= (size_t)n && fin(sd[i - n]) ? sd[i - n] : nanv(); + if (!fin(sd[i]) || !fin(sdl) || sdl == 0) continue; + double vr = sd[i] / sdl; + double alpha = (2.0 / (n + 1)) * vr; + alpha = std::min(1.0, alpha); + out[i] = fin(out[i - 1]) ? out[i - 1] + alpha * (c[i] - out[i - 1]) + : c[i]; + } + return {out}; +} +static Lines f_jma(const std::vector& b, const std::vector& p) { + // Jurik MA (published approximation of the proprietary filter). + int n = pi(p, 0, 14); + double phase = pd(p, 1, 0); + double pr = phase < -100 ? 0.5 : phase > 100 ? 2.5 : phase / 100 + 1.5; + Series c = closes(b), out(c.size(), nanv()); + double beta = 0.45 * (n - 1) / (0.45 * (n - 1) + 2); + double alpha = std::pow(beta, pr); + double e0 = 0, e1 = 0, e2 = 0, jma = 0; + bool started = false; + for (size_t i = 0; i < c.size(); ++i) { + if (!started) { + e0 = jma = c[i]; + started = true; + } + e0 = (1 - alpha) * c[i] + alpha * e0; + e1 = (c[i] - e0) * (1 - beta) + beta * e1; + e2 = (e0 + pr * e1 - jma) * (1 - alpha) * (1 - alpha) + + alpha * alpha * e2; + jma += e2; + out[i] = jma; + } + return {out}; +} + +static void defs_averages(std::vector& v) { + auto O = PlotKind::Overlay; + auto L = Style::Lines; + v.push_back({"SMA", O, L, 0, "20", f_sma}); + v.push_back({"EMA", O, L, 1, "20", f_ema}); + v.push_back({"WMA", O, L, 2, "20", f_wma}); + v.push_back({"SMMA", O, L, 3, "20", f_smma}); + v.push_back({"DEMA", O, L, 4, "20", f_dema}); + v.push_back({"TEMA", O, L, 5, "20", f_tema}); + v.push_back({"TRIMA", O, L, 6, "20", f_trima}); + v.push_back({"KAMA", O, L, 7, "10", f_kama}); + v.push_back({"T3", O, L, 8, "10,0.7", f_t3}); + v.push_back({"HMA", O, L, 9, "20", f_hma}); + v.push_back({"ZLEMA", O, L, 10, "20", f_zlema}); + v.push_back({"VWMA", O, L, 11, "20", f_vwma}); + v.push_back({"LSMA", O, L, 12, "25", f_lsma}); + v.push_back({"ALMA", O, L, 13, "9,0.85,6", f_alma}); + v.push_back({"McGinley Dynamic", O, L, 0, "14", f_mcginley}); + v.push_back({"Geometric MA", O, L, 1, "20", f_gma}); + v.push_back({"Median Price", O, L, 2, "", f_median_price}); + v.push_back({"Typical Price", O, L, 3, "", f_typical_price}); + v.push_back({"Weighted Close", O, L, 4, "", f_weighted_close}); + v.push_back({"Midprice", O, L, 5, "14", f_midprice}); + v.push_back({"Midpoint", O, L, 6, "14", f_midpoint}); + v.push_back({"VWAP", O, L, 7, "", f_vwap}); + v.push_back({"Heikin Ashi Close", O, L, 8, "", f_heikin_ashi}); + v.push_back({"Super Smoother", O, L, 9, "14", f_super_smoother}); + v.push_back({"Decycler", O, L, 10, "30", f_decycler}); + v.push_back({"Gaussian Filter", O, L, 11, "14", f_gaussian}); + v.push_back({"Butterworth Filter", O, L, 12, "14", f_butterworth}); + v.push_back({"Laguerre Filter", O, L, 13, "0.8", f_laguerre}); + v.push_back({"VIDYA", O, L, 0, "14", f_vidya}); + v.push_back({"FRAMA", O, L, 1, "16", f_frama}); + v.push_back({"Variable MA", O, L, 2, "14", f_vma}); + v.push_back({"Jurik MA", O, L, 3, "14,0", f_jma}); +} + +// ============================================================ bands & channels + +static Lines f_bollinger(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 20); + double k = pd(p, 1, 2.0); + Series mid = sma_s(closes(b), n), sd = stdev_s(closes(b), n); + return {combine(mid, scale_s(sd, k), '+'), mid, + combine(mid, scale_s(sd, k), '-')}; +} +static Lines f_bollinger_pb(const std::vector& b, + const std::vector& p) { + Lines bb = f_bollinger(b, p); + Series width = combine(bb[0], bb[2], '-'); + return {combine(combine(closes(b), bb[2], '-'), width, '/')}; +} +static Lines f_bollinger_bw(const std::vector& b, + const std::vector& p) { + Lines bb = f_bollinger(b, p); + return {combine(combine(bb[0], bb[2], '-'), bb[1], '/')}; +} +static Lines f_keltner(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 20); + double k = pd(p, 1, 2.0); + Series mid = ema_s(closes(b), n), atr = atr_s(b, pi(p, 2, 10)); + return {combine(mid, scale_s(atr, k), '+'), mid, + combine(mid, scale_s(atr, k), '-')}; +} +static Lines f_donchian(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 20); + Series up = rolling_max(highs(b), n), dn = rolling_min(lows(b), n); + return {up, scale_s(combine(up, dn, '+'), 0.5), dn}; +} +static Lines f_starc(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 15); + double k = pd(p, 1, 2.0); + Series mid = sma_s(closes(b), n), atr = atr_s(b, pi(p, 2, 15)); + return {combine(mid, scale_s(atr, k), '+'), mid, + combine(mid, scale_s(atr, k), '-')}; +} +static Lines f_envelope(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 20); + double pct = pd(p, 1, 2.5) / 100; + Series mid = sma_s(closes(b), n); + Series up(mid.size(), nanv()), dn(mid.size(), nanv()); + for (size_t i = 0; i < mid.size(); ++i) + if (fin(mid[i])) { + up[i] = mid[i] * (1 + pct); + dn[i] = mid[i] * (1 - pct); + } + return {up, mid, dn}; +} +static Lines f_ichimoku(const std::vector& b, + const std::vector& p) { + int t = pi(p, 0, 9), k = pi(p, 1, 26), s = pi(p, 2, 52); + Series ten = f_midprice(b, {(double)t})[0]; + Series kij = f_midprice(b, {(double)k})[0]; + Series sen_b_raw = f_midprice(b, {(double)s})[0]; + Series sen_a_raw = scale_s(combine(ten, kij, '+'), 0.5); + Series sen_a(b.size(), nanv()), sen_b(b.size(), nanv()), + chikou(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) { + if (i >= (size_t)k) { + if (fin(sen_a_raw[i - k])) sen_a[i] = sen_a_raw[i - k]; + if (fin(sen_b_raw[i - k])) sen_b[i] = sen_b_raw[i - k]; + } + if (i + k < b.size()) chikou[i] = b[i + k].close; + } + return {ten, kij, sen_a, sen_b, chikou}; +} +static Lines f_supertrend(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 10); + double mult = pd(p, 1, 3.0); + Series atr = atr_s(b, n), out(b.size(), nanv()); + Series fup(b.size(), nanv()), flo(b.size(), nanv()); + int dir = 1; + for (size_t i = 0; i < b.size(); ++i) { + if (!fin(atr[i])) continue; + double mid = (b[i].high + b[i].low) / 2; + double bu = mid + mult * atr[i], bl = mid - mult * atr[i]; + if (i == 0 || !fin(fup[i - 1])) { + fup[i] = bu; + flo[i] = bl; + } else { + fup[i] = (bu < fup[i - 1] || b[i - 1].close > fup[i - 1]) + ? bu + : fup[i - 1]; + flo[i] = (bl > flo[i - 1] || b[i - 1].close < flo[i - 1]) + ? bl + : flo[i - 1]; + } + if (fin(out[i - 1 < b.size() ? i - 1 : 0]) && i > 0) { + if (out[i - 1] == fup[i - 1] && b[i].close > fup[i]) + dir = 1; + else if (out[i - 1] == flo[i - 1] && b[i].close < flo[i]) + dir = -1; + } + out[i] = dir > 0 ? flo[i] : fup[i]; + } + return {out}; +} +static Lines f_sar(const std::vector& b, const std::vector& p) { + double step = pd(p, 0, 0.02), mx = pd(p, 1, 0.2); + Series out(b.size(), nanv()); + if (b.size() < 2) return {out}; + bool up = b[1].close >= b[0].close; + double sar = up ? b[0].low : b[0].high; + double ep = up ? b[0].high : b[0].low, af = step; + for (size_t i = 1; i < b.size(); ++i) { + sar = sar + af * (ep - sar); + if (up) { + sar = std::min(sar, std::min(b[i - 1].low, + i > 1 ? b[i - 2].low : b[i - 1].low)); + if (b[i].low < sar) { + up = false; + sar = ep; + ep = b[i].low; + af = step; + } else { + if (b[i].high > ep) { + ep = b[i].high; + af = std::min(mx, af + step); + } + } + } else { + sar = std::max(sar, std::max(b[i - 1].high, + i > 1 ? b[i - 2].high : b[i - 1].high)); + if (b[i].high > sar) { + up = true; + sar = ep; + ep = b[i].high; + af = step; + } else { + if (b[i].low < ep) { + ep = b[i].low; + af = std::min(mx, af + step); + } + } + } + out[i] = sar; + } + return {out}; +} +// Pivot points from the PREVIOUS day (by window_start UTC day). +static Lines f_pivots(const std::vector& b, + const std::vector& p) { + int mode = pi(p, 0, 0); // 0 standard 1 woodie 2 camarilla 3 fib 4 demark + const int NL = 8; + Lines out(NL, Series(b.size(), nanv())); + double H = 0, L = 0, C = 0, O = 0; + bool have_prev = false, in_day = false; + long long day = -1; + double dH = 0, dL = 0, dC = 0, dO = 0; + for (size_t i = 0; i < b.size(); ++i) { + long long d = b[i].time / 86400000000000LL; + if (!in_day || d != day) { + if (in_day) { + H = dH; L = dL; C = dC; O = dO; + have_prev = true; + } + day = d; + in_day = true; + dH = b[i].high; dL = b[i].low; dO = b[i].open; + } + dH = std::max(dH, b[i].high); + dL = std::min(dL, b[i].low); + dC = b[i].close; + if (!have_prev) continue; + double P = 0, R1 = 0, S1 = 0, R2 = 0, S2 = 0, R3 = 0, S3 = 0, + R4 = 0, S4 = 0; + double range = H - L; + switch (mode) { + case 1: // Woodie + P = (H + L + 2 * C) / 4; + R1 = 2 * P - L; S1 = 2 * P - H; + R2 = P + range; S2 = P - range; + R3 = H + 2 * (P - L); S3 = L - 2 * (H - P); + break; + case 2: // Camarilla + P = (H + L + C) / 3; + R1 = C + range * 1.1 / 12; S1 = C - range * 1.1 / 12; + R2 = C + range * 1.1 / 6; S2 = C - range * 1.1 / 6; + R3 = C + range * 1.1 / 4; S3 = C - range * 1.1 / 4; + R4 = C + range * 1.1 / 2; S4 = C - range * 1.1 / 2; + break; + case 3: // Fibonacci + P = (H + L + C) / 3; + R1 = P + 0.382 * range; S1 = P - 0.382 * range; + R2 = P + 0.618 * range; S2 = P - 0.618 * range; + R3 = P + range; S3 = P - range; + break; + case 4: { // DeMark + double X = C < O ? H + 2 * L + C + : C > O ? 2 * H + L + C : H + L + 2 * C; + P = X / 4; + R1 = X / 2 - L; S1 = X / 2 - H; + break; + } + default: // Standard (floor) + P = (H + L + C) / 3; + R1 = 2 * P - L; S1 = 2 * P - H; + R2 = P + range; S2 = P - range; + R3 = H + 2 * (P - L); S3 = L - 2 * (H - P); + break; + } + double vals[8] = {P, R1, S1, R2, S2, R3, S3, mode == 2 ? R4 : nanv()}; + (void)S4; + for (int j = 0; j < NL; ++j) out[j][i] = vals[j]; + if (mode == 2) out[7][i] = R4, out[6][i] = S3; // keep R4 visible + } + return out; +} +static Lines f_lr_channel(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 20); + double k = pd(p, 1, 2.0); + Lines mid = f_lsma(b, {(double)n}); + Series sd = stdev_s(closes(b), n); + return {combine(mid[0], scale_s(sd, k), '+'), mid[0], + combine(mid[0], scale_s(sd, k), '-')}; +} +static Lines f_projection_bands(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 14); + Series c = closes(b), up(c.size(), nanv()), dn(c.size(), nanv()); + for (size_t i = (size_t)(n - 1); i < c.size(); ++i) { + double sx = 0, sy = 0, sxx = 0, sxy = 0; + for (int j = 0; j < n; ++j) { + double x = j, y = c[i - n + 1 + j]; + sx += x; sy += y; sxx += x * x; sxy += x * y; + } + double den = n * sxx - sx * sx; + if (den == 0) continue; + double slope = (n * sxy - sx * sy) / den; + double inter = (sy - slope * sx) / n; + double mx = -1e300, mn = 1e300; + for (int j = 0; j < n; ++j) { + double proj = b[i - n + 1 + j].high + slope * (n - 1 - j); + double projl = b[i - n + 1 + j].low + slope * (n - 1 - j); + mx = std::max(mx, proj); + mn = std::min(mn, projl); + } + (void)inter; + up[i] = mx; + dn[i] = mn; + } + return {up, dn}; +} +static Lines f_atr_bands(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 14); + double k = pd(p, 1, 2.0); + Series atr = atr_s(b, n), c = closes(b); + return {combine(c, scale_s(atr, k), '+'), + combine(c, scale_s(atr, k), '-')}; +} +static Lines f_hilo_bands(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 21); + Series hi = shift_s(sma_s(highs(b), n), 1), lo = shift_s(sma_s(lows(b), n), 1); + return {hi, lo}; +} +static Lines f_fractal_bands(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 21); + return {rolling_max(highs(b), n), rolling_min(lows(b), n)}; +} +static Lines f_chandelier(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 22); + double k = pd(p, 1, 3.0); + Series atr = atr_s(b, n); + Series lng = combine(rolling_max(highs(b), n), scale_s(atr, -k), '+'); + Series sht = combine(rolling_min(lows(b), n), scale_s(atr, k), '+'); + return {lng, sht}; +} +static Lines f_alligator(const std::vector& b, + const std::vector& p) { + (void)p; + Series med = hl2(b); + Series jaw = shift_s(smma_s(med, 13), 8); + Series teeth = shift_s(smma_s(med, 8), 5); + Series lips = shift_s(smma_s(med, 5), 3); + return {jaw, teeth, lips}; +} +static Lines f_guppy(const std::vector& b, + const std::vector& p) { + (void)p; + Series c = closes(b); + return {ema_s(c, 3), ema_s(c, 8), ema_s(c, 15), + ema_s(c, 30), ema_s(c, 45), ema_s(c, 60)}; +} +static Lines f_rainbow(const std::vector& b, + const std::vector& p) { + (void)p; + Series s = closes(b); + Lines out; + for (int i = 0; i < 6; ++i) { + s = sma_s(s, 2); + out.push_back(s); + } + return out; +} +static Lines f_vwap_bands(const std::vector& b, + const std::vector& p) { + double k = pd(p, 0, 1.0); + Series tp = hlc3(b), vw(b.size(), nanv()), up(b.size(), nanv()), + dn(b.size(), nanv()); + double pv = 0, vv = 0, mean = 0, m2 = 0; + for (size_t i = 0; i < b.size(); ++i) { + pv += tp[i] * b[i].volume; + vv += b[i].volume; + if (vv <= 0) continue; + vw[i] = pv / vv; + double d = tp[i] - mean; + mean += d / (i + 1); + m2 += d * (tp[i] - mean); + double sd = std::sqrt(m2 / (i + 1)); + up[i] = vw[i] + k * sd; + dn[i] = vw[i] - k * sd; + } + return {up, vw, dn}; +} +static Lines f_zigzag(const std::vector& b, + const std::vector& p) { + double pct = pd(p, 0, 5.0) / 100; + Series c = closes(b), out(c.size(), nanv()); + if (c.size() < 2) return {out}; + std::vector> piv; + int dir = 0; + double hi = c[0], lo = c[0]; + size_t hii = 0, loi = 0; + for (size_t i = 1; i < c.size(); ++i) { + if (dir >= 0 && c[i] > hi) { hi = c[i]; hii = i; } + if (dir <= 0 && c[i] < lo) { lo = c[i]; loi = i; } + if (dir >= 0 && c[i] < hi * (1 - pct)) { + piv.push_back({hii, hi}); + dir = -1; lo = c[i]; loi = i; + } else if (dir <= 0 && c[i] > lo * (1 + pct)) { + piv.push_back({loi, lo}); + dir = 1; hi = c[i]; hii = i; + } + } + piv.push_back({c.size() - 1, c.back()}); + for (size_t k = 1; k < piv.size(); ++k) { + size_t i0 = piv[k - 1].first, i1 = piv[k].first; + if (i1 == i0) continue; + for (size_t i = i0; i <= i1; ++i) + out[i] = piv[k - 1].second + + (piv[k].second - piv[k - 1].second) * (i - i0) / + (double)(i1 - i0); + } + return {out}; +} +static Lines f_fractals(const std::vector& b, + const std::vector&) { + Series out(b.size(), 0.0); + for (size_t i = 2; i + 2 < b.size(); ++i) { + if (b[i].high > b[i - 1].high && b[i].high > b[i - 2].high && + b[i].high > b[i + 1].high && b[i].high > b[i + 2].high) + out[i] = -1; // up-fractal (resistance) + if (b[i].low < b[i - 1].low && b[i].low < b[i - 2].low && + b[i].low < b[i + 1].low && b[i].low < b[i + 2].low) + out[i] = 1; // down-fractal (support) + } + return {out}; +} +static Lines f_impulse(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 13); + Series e = ema_s(closes(b), n); + Series de = diff_s(e); + Series out(b.size(), 0.0); + // MACD histogram slope as the momentum leg. + Series macd = combine(ema_s(closes(b), 12), ema_s(closes(b), 26), '-'); + Series dh = diff_s(macd); + for (size_t i = 0; i < b.size(); ++i) { + if (!fin(de[i]) || !fin(dh[i])) continue; + if (de[i] > 0 && dh[i] > 0) out[i] = 1; + else if (de[i] < 0 && dh[i] < 0) out[i] = -1; + } + return {out}; +} + +static void defs_bands(std::vector& v) { + auto O = PlotKind::Overlay, P = PlotKind::Pane, M = PlotKind::Markers; + auto L = Style::Lines, B = Style::Bars; + (void)B; + v.push_back({"Bollinger Bands", O, L, 0, "20,2", f_bollinger}); + v.push_back({"Bollinger %B", P, L, 1, "20,2", f_bollinger_pb}); + v.push_back({"Bollinger Bandwidth", P, L, 2, "20,2", f_bollinger_bw}); + v.push_back({"Keltner Channels", O, L, 3, "20,2,10", f_keltner}); + v.push_back({"Donchian Channels", O, L, 4, "20", f_donchian}); + v.push_back({"STARC Bands", O, L, 6, "15,2,15", f_starc}); + v.push_back({"MA Envelope", O, L, 7, "20,2.5", f_envelope}); + v.push_back({"Ichimoku", O, L, 8, "9,26,52", f_ichimoku}); + v.push_back({"Supertrend", O, L, 9, "10,3", f_supertrend}); + v.push_back({"Parabolic SAR", O, L, 10, "0.02,0.2", f_sar}); + v.push_back({"Pivot Points Standard", O, L, 11, "0", f_pivots}); + v.push_back({"Pivot Points Woodie", O, L, 12, "1", f_pivots}); + v.push_back({"Pivot Points Camarilla", O, L, 13, "2", f_pivots}); + v.push_back({"Pivot Points Fibonacci", O, L, 0, "3", f_pivots}); + v.push_back({"Pivot Points DeMark", O, L, 1, "4", f_pivots}); + v.push_back({"Linear Regression Channel", O, L, 2, "20,2", f_lr_channel}); + v.push_back({"Projection Bands", O, L, 3, "14", f_projection_bands}); + v.push_back({"ATR Bands", O, L, 4, "14,2", f_atr_bands}); + v.push_back({"High-Low Bands", O, L, 5, "21", f_hilo_bands}); + v.push_back({"Fractal Chaos Bands", O, L, 6, "21", f_fractal_bands}); + v.push_back({"Chandelier Exit", O, L, 7, "22,3", f_chandelier}); + v.push_back({"Alligator", O, L, 8, "", f_alligator}); + v.push_back({"Guppy MMA", O, L, 9, "", f_guppy}); + v.push_back({"Rainbow MA", O, L, 10, "", f_rainbow}); + v.push_back({"VWAP Bands", O, L, 11, "1", f_vwap_bands}); + v.push_back({"ZigZag", O, L, 12, "5", f_zigzag}); + v.push_back({"Williams Fractals", M, L, 13, "", f_fractals}); + v.push_back({"Elder Impulse", M, L, 0, "13", f_impulse}); +} + +// ============================================================ momentum + +static Series stoch_k(const std::vector& b, int n) { + Series hh = rolling_max(highs(b), n), ll = rolling_min(lows(b), n); + Series c = closes(b), out(c.size(), nanv()); + for (size_t i = 0; i < c.size(); ++i) + if (fin(hh[i]) && fin(ll[i]) && hh[i] > ll[i]) + out[i] = (c[i] - ll[i]) / (hh[i] - ll[i]) * 100; + return out; +} +static Lines f_rsi(const std::vector& b, const std::vector& p) { + return {rsi_s(closes(b), pi(p, 0, 14))}; +} +static Lines f_laguerre_rsi(const std::vector& b, + const std::vector& p) { + double g = pd(p, 0, 0.5); + Series c = closes(b), out(c.size(), nanv()); + double L[4] = {0, 0, 0, 0}; + bool started = false; + for (size_t i = 0; i < c.size(); ++i) { + if (!started) { + for (double& v : L) v = c[i]; + started = true; + } + double o0 = L[0], o1 = L[1], o2 = L[2], o3 = L[3]; + L[0] = (1 - g) * c[i] + g * o0; + L[1] = -g * L[0] + o0 + g * o1; + L[2] = -g * L[1] + o1 + g * o2; + L[3] = -g * L[2] + o2 + g * o3; + double cu = 0, cd = 0; + double lv[4] = {L[0], L[1], L[2], L[3]}; + for (int j = 0; j < 3; ++j) { + if (lv[j] > lv[j + 1]) cu += lv[j] - lv[j + 1]; + else cd += lv[j + 1] - lv[j]; + } + out[i] = (cu + cd) > 0 ? cu / (cu + cd) * 100 : 50; + } + return {out}; +} +static Lines f_connors_rsi(const std::vector& b, + const std::vector& p) { + (void)p; + Series c = closes(b); + Series r1 = rsi_s(c, 3); + // Up/down streak length series. + Series streak(c.size(), 0.0); + for (size_t i = 1; i < c.size(); ++i) { + if (c[i] > c[i - 1]) streak[i] = streak[i - 1] > 0 ? streak[i - 1] + 1 : 1; + else if (c[i] < c[i - 1]) streak[i] = streak[i - 1] < 0 ? streak[i - 1] - 1 : -1; + else streak[i] = 0; + } + Series r2 = rsi_s(streak, 2); + Series roc = diff_s(c); + Series rank(c.size(), nanv()); + for (size_t i = 100; i < c.size(); ++i) { + if (!fin(roc[i])) continue; + int below = 0, tot = 0; + for (int j = 0; j < 100; ++j) + if (fin(roc[i - j])) { + ++tot; + if (roc[i - j] < roc[i]) ++below; + } + if (tot) rank[i] = (double)below / tot * 100; + } + Series out(c.size(), nanv()); + for (size_t i = 0; i < c.size(); ++i) + if (fin(r1[i]) && fin(r2[i]) && fin(rank[i])) + out[i] = (r1[i] + r2[i] + rank[i]) / 3; + return {out}; +} +static Lines f_inv_fisher_rsi(const std::vector& b, + const std::vector& p) { + Series r = rsi_s(closes(b), pi(p, 0, 14)); + Series out(r.size(), nanv()); + for (size_t i = 0; i < r.size(); ++i) + if (fin(r[i])) { + double v = 0.1 * (r[i] - 50); + double e = std::exp(2 * v); + out[i] = ((e - 1) / (e + 1) + 1) * 50; + } + return {out}; +} +static Lines f_stoch_fast(const std::vector& b, + const std::vector& p) { + Series k = stoch_k(b, pi(p, 0, 14)); + return {k, sma_s(k, 3)}; +} +static Lines f_stoch_slow(const std::vector& b, + const std::vector& p) { + Series k = sma_s(stoch_k(b, pi(p, 0, 14)), 3); + return {k, sma_s(k, 3)}; +} +static Lines f_stoch_full(const std::vector& b, + const std::vector& p) { + Series k = sma_s(stoch_k(b, pi(p, 0, 14)), pi(p, 1, 3)); + return {k, sma_s(k, pi(p, 2, 3))}; +} +static Lines f_stoch_rsi(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 14); + Series r = rsi_s(closes(b), n); + Series mx = rolling_max(r, n), mn = rolling_min(r, n); + Series k(r.size(), nanv()); + for (size_t i = 0; i < r.size(); ++i) + if (fin(mx[i]) && fin(mn[i]) && mx[i] > mn[i]) + k[i] = (r[i] - mn[i]) / (mx[i] - mn[i]) * 100; + return {k, sma_s(k, 3)}; +} +static Lines f_smi(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 14); + Series c = closes(b); + Series hh = rolling_max(highs(b), n), ll = rolling_min(lows(b), n); + Series mid(c.size(), nanv()), rel(c.size(), nanv()), rng(c.size(), nanv()); + for (size_t i = 0; i < c.size(); ++i) + if (fin(hh[i]) && fin(ll[i])) { + mid[i] = (hh[i] + ll[i]) / 2; + rel[i] = c[i] - mid[i]; + rng[i] = hh[i] - ll[i]; + } + Series num = ema_s(ema_s(rel, 5), 5); + Series den = ema_s(ema_s(rng, 5), 5); + return {combine(scale_s(num, 100), den, '/')}; +} +static Lines f_williams_r(const std::vector& b, + const std::vector& p) { + Series k = stoch_k(b, pi(p, 0, 14)); + for (double& v : k) + if (fin(v)) v -= 100; + return {k}; +} +static Lines f_cci(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 20); + Series tp = hlc3(b), m = sma_s(tp, n), out(tp.size(), nanv()); + for (size_t i = 0; i < tp.size(); ++i) { + if (!fin(m[i])) continue; + double mad = 0; + for (int j = 0; j < n; ++j) mad += std::fabs(tp[i - j] - m[i]); + mad /= n; + if (mad > 0) out[i] = (tp[i] - m[i]) / (0.015 * mad); + } + return {out}; +} +static Lines f_cmo(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 14); + Series d = diff_s(closes(b)); + Series g(d.size(), nanv()), l(d.size(), nanv()); + for (size_t i = 0; i < d.size(); ++i) + if (fin(d[i])) { + g[i] = d[i] > 0 ? d[i] : 0; + l[i] = d[i] < 0 ? -d[i] : 0; + } + Series gs = rolling_sum(g, n), ls = rolling_sum(l, n); + return {combine(scale_s(combine(gs, ls, '-'), 100), + combine(gs, ls, '+'), '/')}; +} +static Lines f_roc(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 12); + Series c = closes(b), out(c.size(), nanv()); + for (size_t i = (size_t)n; i < c.size(); ++i) + if (c[i - n] != 0) out[i] = (c[i] - c[i - n]) / c[i - n] * 100; + return {out}; +} +static Lines f_momentum(const std::vector& b, + const std::vector& p) { + return {diff_s(closes(b), pi(p, 0, 10))}; +} +static Series macd_line(const std::vector& b, int fast, int slow) { + return combine(ema_s(closes(b), fast), ema_s(closes(b), slow), '-'); +} +static Lines f_macd(const std::vector& b, const std::vector& p) { + Series m = macd_line(b, pi(p, 0, 12), pi(p, 1, 26)); + Series sig = ema_s(m, pi(p, 2, 9)); + return {m, sig}; +} +static Lines f_macd_hist(const std::vector& b, + const std::vector& p) { + Lines m = f_macd(b, p); + return {combine(m[0], m[1], '-')}; +} +static Lines f_ppo(const std::vector& b, const std::vector& p) { + Series slow = ema_s(closes(b), pi(p, 1, 26)); + Series m = combine(macd_line(b, pi(p, 0, 12), pi(p, 1, 26)), slow, '/'); + m = scale_s(m, 100); + return {m, ema_s(m, pi(p, 2, 9))}; +} +static Lines f_apo(const std::vector& b, const std::vector& p) { + return {macd_line(b, pi(p, 0, 12), pi(p, 1, 26))}; +} +static Lines f_trix(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 15); + Series e = ema_s(ema_s(ema_s(closes(b), n), n), n); + Series out(e.size(), nanv()); + for (size_t i = 1; i < e.size(); ++i) + if (fin(e[i]) && fin(e[i - 1]) && e[i - 1] != 0) + out[i] = (e[i] - e[i - 1]) / e[i - 1] * 100; + return {out}; +} +static Lines f_tsi(const std::vector& b, const std::vector& p) { + int r = pi(p, 0, 25), s = pi(p, 1, 13); + Series d = diff_s(closes(b)); + Series ad(d.size(), nanv()); + for (size_t i = 0; i < d.size(); ++i) + if (fin(d[i])) ad[i] = std::fabs(d[i]); + Series num = ema_s(ema_s(d, r), s); + Series den = ema_s(ema_s(ad, r), s); + return {combine(scale_s(num, 100), den, '/')}; +} +static Lines f_rmi(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 14), m = pi(p, 1, 5); + Series d = diff_s(closes(b), m); + Series out(d.size(), nanv()); + double gain = 0, loss = 0; + for (size_t i = 0; i < d.size(); ++i) { + if (!fin(d[i])) continue; + double g = d[i] > 0 ? d[i] : 0, l = d[i] < 0 ? -d[i] : 0; + gain = gain == 0 && loss == 0 ? g : (gain * (n - 1) + g) / n; + loss = gain == g && loss == 0 ? l : (loss * (n - 1) + l) / n; + if (i >= (size_t)(n + m)) + out[i] = loss == 0 ? 100 : 100 - 100 / (1 + gain / loss); + } + return {out}; +} +static Lines f_ultimate(const std::vector& b, + const std::vector& p) { + (void)p; + Series bp(b.size(), nanv()), tr(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) { + double pc = i > 0 ? b[i - 1].close : b[i].open; + bp[i] = b[i].close - std::min(b[i].low, pc); + tr[i] = std::max(b[i].high, pc) - std::min(b[i].low, pc); + } + Series a1 = combine(rolling_sum(bp, 7), rolling_sum(tr, 7), '/'); + Series a2 = combine(rolling_sum(bp, 14), rolling_sum(tr, 14), '/'); + Series a3 = combine(rolling_sum(bp, 28), rolling_sum(tr, 28), '/'); + Series out(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (fin(a1[i]) && fin(a2[i]) && fin(a3[i])) + out[i] = (4 * a1[i] + 2 * a2[i] + a3[i]) / 7 * 100; + return {out}; +} +static Lines f_awesome(const std::vector& b, + const std::vector&) { + return {combine(sma_s(hl2(b), 5), sma_s(hl2(b), 34), '-')}; +} +static Lines f_accelerator(const std::vector& b, + const std::vector& p) { + Lines ao = f_awesome(b, p); + return {combine(ao[0], sma_s(ao[0], 5), '-')}; +} +static Lines f_fisher(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 10); + Series hh = rolling_max(highs(b), n), ll = rolling_min(lows(b), n); + Series c = closes(b), fish(c.size(), nanv()); + double x = 0, prev = 0; + for (size_t i = 0; i < c.size(); ++i) { + if (!fin(hh[i]) || !fin(ll[i]) || hh[i] <= ll[i]) continue; + double v = (c[i] - ll[i]) / (hh[i] - ll[i]); + x = 0.33 * 2 * (v - 0.5) + 0.67 * x; + x = std::min(0.999, std::max(-0.999, x)); + double f = 0.5 * std::log((1 + x) / (1 - x)) + 0.5 * prev; + fish[i] = f; + prev = f; + } + return {fish, shift_s(fish, 1)}; +} +static Lines f_stc(const std::vector& b, const std::vector& p) { + (void)p; + Series m = macd_line(b, 23, 50); + auto stoch_of = [&](const Series& s, int n) { + Series mx = rolling_max(s, n), mn = rolling_min(s, n); + Series out(s.size(), nanv()); + for (size_t i = 0; i < s.size(); ++i) + if (fin(mx[i]) && fin(mn[i]) && mx[i] > mn[i]) + out[i] = (s[i] - mn[i]) / (mx[i] - mn[i]); + return out; + }; + Series s1 = stoch_of(m, 10); + Series sm1 = ema_s(s1, 3); + Series s2 = stoch_of(sm1, 10); + return {scale_s(ema_s(s2, 3), 100)}; +} +static Lines f_coppock(const std::vector& b, + const std::vector& p) { + (void)p; + Series r = combine(f_roc(b, {14})[0], f_roc(b, {11})[0], '+'); + return {wma_s(r, 10)}; +} +static Lines f_kst(const std::vector& b, const std::vector& p) { + (void)p; + Series k = scale_s(sma_s(f_roc(b, {10})[0], 10), 1); + k = combine(k, scale_s(sma_s(f_roc(b, {15})[0], 10), 2), '+'); + k = combine(k, scale_s(sma_s(f_roc(b, {20})[0], 10), 3), '+'); + k = combine(k, scale_s(sma_s(f_roc(b, {30})[0], 15), 4), '+'); + return {k, sma_s(k, 9)}; +} +static Series sym_smooth(const Series& in) { // (x + 2x1 + 2x2 + x3)/6 + Series out(in.size(), nanv()); + for (size_t i = 3; i < in.size(); ++i) + out[i] = (in[i] + 2 * in[i - 1] + 2 * in[i - 2] + in[i - 3]) / 6; + return out; +} +static Lines f_rvi_vigor(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 10); + Series co(b.size()), hl(b.size()); + for (size_t i = 0; i < b.size(); ++i) { + co[i] = b[i].close - b[i].open; + hl[i] = b[i].high - b[i].low; + } + Series rvi = combine(sma_s(sym_smooth(co), n), + sma_s(sym_smooth(hl), n), '/'); + return {rvi, sym_smooth(rvi)}; +} +static Lines f_demarker(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 14); + Series mx(b.size(), 0.0), mn(b.size(), 0.0); + for (size_t i = 1; i < b.size(); ++i) { + mx[i] = std::max(0.0, b[i].high - b[i - 1].high); + mn[i] = std::max(0.0, b[i - 1].low - b[i].low); + } + Series a = sma_s(mx, n), d = sma_s(mn, n); + return {combine(a, combine(a, d, '+'), '/')}; +} +static Lines f_bop(const std::vector& b, const std::vector& p) { + Series out(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (b[i].high > b[i].low) + out[i] = (b[i].close - b[i].open) / (b[i].high - b[i].low); + return {sma_s(out, pi(p, 0, 14))}; +} +static Lines f_bull_bear(const std::vector& b, + const std::vector& p, bool bull) { + Series e = ema_s(closes(b), pi(p, 0, 13)); + Series out(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (fin(e[i])) out[i] = (bull ? b[i].high : b[i].low) - e[i]; + return {out}; +} +static Lines f_bull_power(const std::vector& b, + const std::vector& p) { + return f_bull_bear(b, p, true); +} +static Lines f_bear_power(const std::vector& b, + const std::vector& p) { + return f_bull_bear(b, p, false); +} +static Lines f_force(const std::vector& b, + const std::vector& p) { + Series d = diff_s(closes(b)); + Series f(d.size(), nanv()); + for (size_t i = 0; i < d.size(); ++i) + if (fin(d[i])) f[i] = d[i] * b[i].volume; + return {ema_s(f, pi(p, 0, 13))}; +} +static Lines f_eom(const std::vector& b, const std::vector& p) { + Series m = hl2(b), dm = diff_s(m); + Series out(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) { + if (!fin(dm[i]) || b[i].volume <= 0 || b[i].high <= b[i].low) + continue; + double box = b[i].volume / (b[i].high - b[i].low); + out[i] = dm[i] / box * 10000; + } + return {sma_s(out, pi(p, 0, 14))}; +} +static Lines f_mass_index(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 25); + Series range = combine(highs(b), lows(b), '-'); + Series e1 = ema_s(range, 9); + Series ratio = combine(e1, ema_s(e1, 9), '/'); + return {rolling_sum(ratio, n)}; +} +static Lines f_vortex(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 14); + Series vp(b.size(), nanv()), vm(b.size(), nanv()); + for (size_t i = 1; i < b.size(); ++i) { + vp[i] = std::fabs(b[i].high - b[i - 1].low); + vm[i] = std::fabs(b[i].low - b[i - 1].high); + } + Series trs = rolling_sum(true_range(b), n); + return {combine(rolling_sum(vp, n), trs, '/'), + combine(rolling_sum(vm, n), trs, '/')}; +} +static Lines f_aroon(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 25); + Series up(b.size(), nanv()), dn(b.size(), nanv()); + for (size_t i = (size_t)n; i < b.size(); ++i) { + int since_hi = 0, since_lo = 0; + double mx = -1e300, mn = 1e300; + for (int j = 0; j <= n; ++j) { + if (b[i - j].high >= mx) { mx = b[i - j].high; since_hi = j; } + if (b[i - j].low <= mn) { mn = b[i - j].low; since_lo = j; } + } + up[i] = (double)(n - since_hi) / n * 100; + dn[i] = (double)(n - since_lo) / n * 100; + } + return {up, dn}; +} +static Lines f_aroon_osc(const std::vector& b, + const std::vector& p) { + Lines a = f_aroon(b, p); + return {combine(a[0], a[1], '-')}; +} +struct DiAdx { + Series plus_di, minus_di, dx, adx; +}; +static DiAdx di_adx(const std::vector& b, int n) { + DiAdx r; + size_t N = b.size(); + r.plus_di.assign(N, nanv()); + r.minus_di.assign(N, nanv()); + r.dx.assign(N, nanv()); + r.adx.assign(N, nanv()); + Series pdm(N, 0.0), mdm(N, 0.0), tr = true_range(b); + for (size_t i = 1; i < N; ++i) { + double up = b[i].high - b[i - 1].high; + double dn = b[i - 1].low - b[i].low; + pdm[i] = up > dn && up > 0 ? up : 0; + mdm[i] = dn > up && dn > 0 ? dn : 0; + } + Series sp = smma_s(pdm, n), sm = smma_s(mdm, n), st = smma_s(tr, n); + for (size_t i = 0; i < N; ++i) + if (fin(st[i]) && st[i] > 0 && fin(sp[i]) && fin(sm[i])) { + r.plus_di[i] = 100 * sp[i] / st[i]; + r.minus_di[i] = 100 * sm[i] / st[i]; + double den = r.plus_di[i] + r.minus_di[i]; + if (den > 0) + r.dx[i] = 100 * std::fabs(r.plus_di[i] - r.minus_di[i]) / den; + } + r.adx = smma_s(r.dx, n); + return r; +} +static Lines f_adx(const std::vector& b, const std::vector& p) { + return {di_adx(b, pi(p, 0, 14)).adx}; +} +static Lines f_plus_di(const std::vector& b, + const std::vector& p) { + return {di_adx(b, pi(p, 0, 14)).plus_di}; +} +static Lines f_minus_di(const std::vector& b, + const std::vector& p) { + return {di_adx(b, pi(p, 0, 14)).minus_di}; +} +static Lines f_dx(const std::vector& b, const std::vector& p) { + return {di_adx(b, pi(p, 0, 14)).dx}; +} +static Lines f_adxr(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 14); + Series a = di_adx(b, n).adx; + return {scale_s(combine(a, shift_s(a, n), '+'), 0.5)}; +} +static Lines f_dpo(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 20); + Series m = sma_s(closes(b), n); + return {combine(shift_s(closes(b), n / 2 + 1), m, '-')}; +} +static Lines f_qstick(const std::vector& b, + const std::vector& p) { + Series co(b.size()); + for (size_t i = 0; i < b.size(); ++i) co[i] = b[i].close - b[i].open; + return {sma_s(co, pi(p, 0, 14))}; +} +static Lines f_imi(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 14); + Series g(b.size(), 0.0), l(b.size(), 0.0); + for (size_t i = 0; i < b.size(); ++i) { + double d = b[i].close - b[i].open; + if (d > 0) g[i] = d; + else l[i] = -d; + } + Series gs = rolling_sum(g, n), ls = rolling_sum(l, n); + return {combine(scale_s(gs, 100), combine(gs, ls, '+'), '/')}; +} +static Lines f_rwi(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 14); + Series atr = atr_s(b, n); + Series hi_s(b.size(), nanv()), lo_s(b.size(), nanv()); + double sq = std::sqrt(n); + for (size_t i = (size_t)n; i < b.size(); ++i) + if (fin(atr[i]) && atr[i] > 0) { + hi_s[i] = (b[i].high - b[i - n].low) / (atr[i] * sq); + lo_s[i] = (b[i - n].high - b[i].low) / (atr[i] * sq); + } + return {hi_s, lo_s}; +} +static Series swing_index_s(const std::vector& b) { + Series out(b.size(), nanv()); + for (size_t i = 1; i < b.size(); ++i) { + const Bar& c = b[i]; + const Bar& pc = b[i - 1]; + double r1 = std::fabs(c.high - pc.close); + double r2 = std::fabs(c.low - pc.close); + double r3 = std::fabs(c.high - c.low); + double R; + if (r1 > r2 && r1 > r3) + R = r1 - r2 / 2 + std::fabs(pc.close - pc.open) / 4; + else if (r2 > r1 && r2 > r3) + R = r2 - r1 / 2 + std::fabs(pc.close - pc.open) / 4; + else + R = r3 + std::fabs(pc.close - pc.open) / 4; + if (R == 0) { + out[i] = 0; + continue; + } + double K = std::max(std::fabs(c.high - pc.close), + std::fabs(c.low - pc.close)); + out[i] = 50 * ((c.close - pc.close) + 0.5 * (c.close - c.open) + + 0.25 * (pc.close - pc.open)) / + R * K / 10; + } + return out; +} +static Lines f_swing(const std::vector& b, + const std::vector&) { + return {swing_index_s(b)}; +} +static Lines f_aswing(const std::vector& b, + const std::vector&) { + Series si = swing_index_s(b), out(b.size(), nanv()); + double acc = 0; + for (size_t i = 0; i < b.size(); ++i) + if (fin(si[i])) { + acc += si[i]; + out[i] = acc; + } + return {out}; +} +static Lines f_gator(const std::vector& b, + const std::vector& p) { + Lines a = f_alligator(b, p); + Series up(b.size(), nanv()), dn(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) { + if (fin(a[0][i]) && fin(a[1][i])) + up[i] = std::fabs(a[0][i] - a[1][i]); + if (fin(a[1][i]) && fin(a[2][i])) + dn[i] = -std::fabs(a[1][i] - a[2][i]); + } + return {up, dn}; +} +static Lines f_projection_osc(const std::vector& b, + const std::vector& p) { + Lines bands = f_projection_bands(b, p); + Series c = closes(b), out(c.size(), nanv()); + for (size_t i = 0; i < c.size(); ++i) + if (fin(bands[0][i]) && fin(bands[1][i]) && + bands[0][i] > bands[1][i]) + out[i] = (c[i] - bands[1][i]) / (bands[0][i] - bands[1][i]) * 100; + return {out}; +} + +static void defs_momentum(std::vector& v) { + auto P = PlotKind::Pane; + auto L = Style::Lines, B = Style::Bars; + v.push_back({"RSI", P, L, 0, "14", f_rsi}); + v.push_back({"Laguerre RSI", P, L, 1, "0.5", f_laguerre_rsi}); + v.push_back({"Connors RSI", P, L, 2, "", f_connors_rsi}); + v.push_back({"Inverse Fisher RSI", P, L, 3, "14", f_inv_fisher_rsi}); + v.push_back({"Stochastic Fast", P, L, 4, "14", f_stoch_fast}); + v.push_back({"Stochastic Slow", P, L, 5, "14", f_stoch_slow}); + v.push_back({"Stochastic Full", P, L, 6, "14,3,3", f_stoch_full}); + v.push_back({"Stochastic RSI", P, L, 7, "14", f_stoch_rsi}); + v.push_back({"SMI", P, L, 8, "14", f_smi}); + v.push_back({"Williams %R", P, L, 9, "14", f_williams_r}); + v.push_back({"CCI", P, L, 10, "20", f_cci}); + v.push_back({"CMO", P, L, 11, "14", f_cmo}); + v.push_back({"ROC", P, L, 12, "12", f_roc}); + v.push_back({"Momentum", P, L, 13, "10", f_momentum}); + v.push_back({"MACD", P, L, 0, "12,26,9", f_macd}); + v.push_back({"MACD Histogram", P, B, 1, "12,26,9", f_macd_hist}); + v.push_back({"PPO", P, L, 2, "12,26,9", f_ppo}); + v.push_back({"APO", P, L, 3, "12,26", f_apo}); + v.push_back({"TRIX", P, L, 4, "15", f_trix}); + v.push_back({"TSI", P, L, 5, "25,13", f_tsi}); + v.push_back({"RMI", P, L, 6, "14,5", f_rmi}); + v.push_back({"Ultimate Oscillator", P, L, 7, "", f_ultimate}); + v.push_back({"Awesome Oscillator", P, B, 8, "", f_awesome}); + v.push_back({"Accelerator Oscillator", P, B, 9, "", f_accelerator}); + v.push_back({"Fisher Transform", P, L, 10, "10", f_fisher}); + v.push_back({"Schaff Trend Cycle", P, L, 11, "", f_stc}); + v.push_back({"Coppock Curve", P, L, 12, "", f_coppock}); + v.push_back({"KST", P, L, 13, "", f_kst}); + v.push_back({"Relative Vigor Index", P, L, 0, "10", f_rvi_vigor}); + v.push_back({"DeMarker", P, L, 1, "14", f_demarker}); + v.push_back({"Balance of Power", P, L, 2, "14", f_bop}); + v.push_back({"Bull Power", P, L, 3, "13", f_bull_power}); + v.push_back({"Bear Power", P, L, 4, "13", f_bear_power}); + v.push_back({"Force Index", P, L, 5, "13", f_force}); + v.push_back({"Ease of Movement", P, L, 6, "14", f_eom}); + v.push_back({"Mass Index", P, L, 7, "25", f_mass_index}); + v.push_back({"Vortex Indicator", P, L, 8, "14", f_vortex}); + v.push_back({"Aroon", P, L, 9, "25", f_aroon}); + v.push_back({"Aroon Oscillator", P, L, 10, "25", f_aroon_osc}); + v.push_back({"ADX", P, L, 11, "14", f_adx}); + v.push_back({"Plus DI", P, L, 12, "14", f_plus_di}); + v.push_back({"Minus DI", P, L, 13, "14", f_minus_di}); + v.push_back({"DX", P, L, 0, "14", f_dx}); + v.push_back({"ADXR", P, L, 1, "14", f_adxr}); + v.push_back({"DPO", P, L, 2, "20", f_dpo}); + v.push_back({"Qstick", P, L, 3, "14", f_qstick}); + v.push_back({"IMI", P, L, 4, "14", f_imi}); + v.push_back({"Random Walk Index", P, L, 5, "14", f_rwi}); + v.push_back({"Swing Index", P, L, 6, "", f_swing}); + v.push_back({"Accumulative Swing Index", P, L, 7, "", f_aswing}); + v.push_back({"Gator Oscillator", P, B, 8, "", f_gator}); + v.push_back({"Projection Oscillator", P, L, 9, "14", f_projection_osc}); +} + +// ============================================================ volume + +static Lines f_volume(const std::vector& b, + const std::vector&) { + return {vols(b)}; +} +static Lines f_volume_ma(const std::vector& b, + const std::vector& p) { + return {sma_s(vols(b), pi(p, 0, 20))}; +} +static Lines f_obv(const std::vector& b, const std::vector&) { + Series out(b.size(), nanv()); + double acc = 0; + for (size_t i = 0; i < b.size(); ++i) { + if (i > 0) { + if (b[i].close > b[i - 1].close) acc += b[i].volume; + else if (b[i].close < b[i - 1].close) acc -= b[i].volume; + } + out[i] = acc; + } + return {out}; +} +static Lines f_pvt(const std::vector& b, const std::vector&) { + Series out(b.size(), nanv()); + double acc = 0; + for (size_t i = 0; i < b.size(); ++i) { + if (i > 0 && b[i - 1].close != 0) + acc += b[i].volume * (b[i].close - b[i - 1].close) / + b[i - 1].close; + out[i] = acc; + } + return {out}; +} +static Series ad_line_s(const std::vector& b) { + Series out(b.size(), nanv()); + double acc = 0; + for (size_t i = 0; i < b.size(); ++i) { + if (b[i].high > b[i].low) { + double mfm = ((b[i].close - b[i].low) - (b[i].high - b[i].close)) / + (b[i].high - b[i].low); + acc += mfm * b[i].volume; + } + out[i] = acc; + } + return out; +} +static Lines f_ad_line(const std::vector& b, + const std::vector&) { + return {ad_line_s(b)}; +} +static Lines f_adosc(const std::vector& b, + const std::vector& p) { + Series ad = ad_line_s(b); + return {combine(ema_s(ad, pi(p, 0, 3)), ema_s(ad, pi(p, 1, 10)), '-')}; +} +static Lines f_cmf(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 20); + Series mfv(b.size(), 0.0); + for (size_t i = 0; i < b.size(); ++i) + if (b[i].high > b[i].low) + mfv[i] = ((b[i].close - b[i].low) - (b[i].high - b[i].close)) / + (b[i].high - b[i].low) * b[i].volume; + return {combine(rolling_sum(mfv, n), rolling_sum(vols(b), n), '/')}; +} +static Lines f_mfi(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 14); + Series tp = hlc3(b); + Series pos(b.size(), 0.0), neg(b.size(), 0.0); + for (size_t i = 1; i < b.size(); ++i) { + double flow = tp[i] * b[i].volume; + if (tp[i] > tp[i - 1]) pos[i] = flow; + else if (tp[i] < tp[i - 1]) neg[i] = flow; + } + Series ps = rolling_sum(pos, n), ns = rolling_sum(neg, n); + Series out(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (fin(ps[i]) && fin(ns[i])) + out[i] = ns[i] == 0 ? 100 : 100 - 100 / (1 + ps[i] / ns[i]); + return {out}; +} +static Lines f_vol_osc(const std::vector& b, + const std::vector& p) { + Series fast = sma_s(vols(b), pi(p, 0, 5)), slow = sma_s(vols(b), pi(p, 1, 34)); + Series out(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (fin(slow[i]) && slow[i] != 0) + out[i] = (fast[i] - slow[i]) / slow[i] * 100; + return {out}; +} +static Lines f_pvo(const std::vector& b, const std::vector& p) { + Series fast = ema_s(vols(b), pi(p, 0, 12)), slow = ema_s(vols(b), pi(p, 1, 26)); + Series out(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (fin(slow[i]) && slow[i] != 0) + out[i] = (fast[i] - slow[i]) / slow[i] * 100; + return {out}; +} +static Lines f_vol_roc(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 12); + Series v = vols(b), out(v.size(), nanv()); + for (size_t i = (size_t)n; i < v.size(); ++i) + if (v[i - n] != 0) out[i] = (v[i] - v[i - n]) / v[i - n] * 100; + return {out}; +} +static Lines f_nvi_pvi(const std::vector& b, + const std::vector&, bool nvi) { + Series out(b.size(), nanv()); + double acc = 1000; + for (size_t i = 0; i < b.size(); ++i) { + if (i > 0 && b[i - 1].close != 0) { + bool down_day = b[i].volume < b[i - 1].volume; + if (down_day == nvi) + acc *= 1 + (b[i].close - b[i - 1].close) / b[i - 1].close; + } + out[i] = acc; + } + return {out}; +} +static Lines f_nvi(const std::vector& b, const std::vector& p) { + return f_nvi_pvi(b, p, true); +} +static Lines f_pvi(const std::vector& b, const std::vector& p) { + return f_nvi_pvi(b, p, false); +} +static Lines f_klinger(const std::vector& b, + const std::vector& p) { + (void)p; + Series vf(b.size(), 0.0); + for (size_t i = 1; i < b.size(); ++i) { + double trend = (b[i].high + b[i].low + b[i].close) - + (b[i - 1].high + b[i - 1].low + b[i - 1].close); + vf[i] = trend >= 0 ? b[i].volume : -b[i].volume; + } + Series ko = combine(ema_s(vf, 34), ema_s(vf, 55), '-'); + return {ko, ema_s(ko, 13)}; +} +static Lines f_twiggs(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 21); + Series trl(b.size(), nanv()), adv(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) { + double pc = i > 0 ? b[i - 1].close : b[i].low; + trl[i] = std::max(b[i].high, pc) - std::min(b[i].low, pc); + adv[i] = trl[i] > 0 ? (b[i].close - std::min(b[i].low, pc)) / trl[i] * + b[i].volume + : 0; + } + Series num = ema_s(adv, n), den = ema_s(vols(b), n); + return {combine(num, den, '/')}; +} +static Lines f_williams_ad(const std::vector& b, + const std::vector&) { + Series out(b.size(), nanv()); + double acc = 0; + for (size_t i = 0; i < b.size(); ++i) { + if (i > 0) { + if (b[i].close > b[i - 1].close) + acc += b[i].close - std::min(b[i].low, b[i - 1].close); + else if (b[i].close < b[i - 1].close) + acc -= std::max(b[i].high, b[i - 1].close) - b[i].close; + } + out[i] = acc; + } + return {out}; +} +static Lines f_intensity(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 21); + Series ii(b.size(), 0.0); + for (size_t i = 0; i < b.size(); ++i) + if (b[i].high > b[i].low) + ii[i] = (2 * b[i].close - b[i].high - b[i].low) / + (b[i].high - b[i].low) * b[i].volume; + return {rolling_sum(ii, n)}; +} +static Lines f_ad_percent(const std::vector& b, + const std::vector&) { + Series ad = ad_line_s(b), cumv(b.size(), nanv()); + double acc = 0; + for (size_t i = 0; i < b.size(); ++i) { + acc += b[i].volume; + cumv[i] = acc; + } + return {combine(scale_s(ad, 100), cumv, '/')}; +} +static Lines f_demand(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 20); + Series bp(b.size(), 0.0), sp(b.size(), 0.0); + for (size_t i = 1; i < b.size(); ++i) { + double w = std::pow(1.0 + std::fabs(b[i].close - b[i - 1].close) / + b[i - 1].close, + 10); + if (b[i].close > b[i - 1].close) bp[i] = b[i].volume * w; + else sp[i] = b[i].volume * w; + } + Series bs = sma_s(bp, n), ss = sma_s(sp, n); + Series out(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (fin(bs[i]) && fin(ss[i])) + out[i] = bs[i] > ss[i] ? 1 - ss[i] / bs[i] + : -(1 - bs[i] / ss[i]); + return {scale_s(out, 100)}; +} +static Lines f_tvi(const std::vector& b, const std::vector&) { + Series out(b.size(), nanv()); + double acc = 0; + for (size_t i = 0; i < b.size(); ++i) { + if (i > 0) { + if (b[i].close > b[i - 1].close) acc += b[i].volume; + else if (b[i].close < b[i - 1].close) acc -= b[i].volume; + } + out[i] = acc; + } + return {out}; +} + +static void defs_volume(std::vector& v) { + auto P = PlotKind::Pane; + auto L = Style::Lines, B = Style::Bars; + v.push_back({"Volume", P, B, 10, "", f_volume}); + v.push_back({"Volume MA", P, L, 11, "20", f_volume_ma}); + v.push_back({"OBV", P, L, 0, "", f_obv}); + v.push_back({"Price Volume Trend", P, L, 1, "", f_pvt}); + v.push_back({"A/D Line", P, L, 2, "", f_ad_line}); + v.push_back({"Chaikin Oscillator", P, L, 3, "3,10", f_adosc}); + v.push_back({"Chaikin Money Flow", P, L, 4, "20", f_cmf}); + v.push_back({"MFI", P, L, 5, "14", f_mfi}); + v.push_back({"Volume Oscillator", P, L, 6, "5,34", f_vol_osc}); + v.push_back({"PVO", P, L, 7, "12,26", f_pvo}); + v.push_back({"Volume ROC", P, L, 8, "12", f_vol_roc}); + v.push_back({"NVI", P, L, 9, "", f_nvi}); + v.push_back({"PVI", P, L, 10, "", f_pvi}); + v.push_back({"Klinger Oscillator", P, L, 11, "", f_klinger}); + v.push_back({"Twiggs Money Flow", P, L, 12, "21", f_twiggs}); + v.push_back({"Williams A/D", P, L, 13, "", f_williams_ad}); + v.push_back({"Intraday Intensity", P, L, 0, "21", f_intensity}); + v.push_back({"Accumulation Percent", P, L, 1, "", f_ad_percent}); + v.push_back({"Demand Index", P, L, 2, "20", f_demand}); + v.push_back({"Trade Volume Index", P, L, 3, "", f_tvi}); +} + +// ============================================================ volatility & statistics + +static Lines f_atr(const std::vector& b, const std::vector& p) { + return {atr_s(b, pi(p, 0, 14))}; +} +static Lines f_natr(const std::vector& b, const std::vector& p) { + Series a = atr_s(b, pi(p, 0, 14)), c = closes(b); + return {combine(scale_s(a, 100), c, '/')}; +} +static Lines f_tr(const std::vector& b, const std::vector&) { + return {true_range(b)}; +} +static Lines f_stddev(const std::vector& b, + const std::vector& p) { + return {stdev_s(closes(b), pi(p, 0, 20))}; +} +static Lines f_variance(const std::vector& b, + const std::vector& p) { + Series sd = stdev_s(closes(b), pi(p, 0, 20)); + for (double& v : sd) + if (fin(v)) v *= v; + return {sd}; +} +static Series log_returns(const std::vector& b) { + Series c = closes(b), out(c.size(), nanv()); + for (size_t i = 1; i < c.size(); ++i) + if (c[i] > 0 && c[i - 1] > 0) out[i] = std::log(c[i] / c[i - 1]); + return out; +} +static Lines f_hist_vol(const std::vector& b, + const std::vector& p) { + return {scale_s(stdev_s(log_returns(b), pi(p, 0, 20)), 100)}; +} +static Lines f_chaikin_vol(const std::vector& b, + const std::vector& p) { + Series e = ema_s(combine(highs(b), lows(b), '-'), pi(p, 0, 10)); + Series out(e.size(), nanv()); + int n = pi(p, 1, 10); + for (size_t i = (size_t)n; i < e.size(); ++i) + if (fin(e[i]) && fin(e[i - n]) && e[i - n] != 0) + out[i] = (e[i] - e[i - n]) / e[i - n] * 100; + return {out}; +} +static Lines f_ulcer(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 14); + Series c = closes(b), mx = rolling_max(c, n), dd(c.size(), nanv()); + for (size_t i = 0; i < c.size(); ++i) + if (fin(mx[i]) && mx[i] > 0) { + double d = (c[i] - mx[i]) / mx[i] * 100; + dd[i] = d * d; + } + Series ms = sma_s(dd, n), out(c.size(), nanv()); + for (size_t i = 0; i < c.size(); ++i) + if (fin(ms[i])) out[i] = std::sqrt(ms[i]); + return {out}; +} +static Lines f_choppiness(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 14); + Series atr_sum = rolling_sum(true_range(b), n); + Series hh = rolling_max(highs(b), n), ll = rolling_min(lows(b), n); + Series out(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (fin(atr_sum[i]) && fin(hh[i]) && hh[i] > ll[i] && atr_sum[i] > 0) + out[i] = 100 * std::log10(atr_sum[i] / (hh[i] - ll[i])) / + std::log10(n); + return {out}; +} +static Lines f_rvol_idx(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 10); + Series d = diff_s(closes(b)); + Series up(b.size(), 0.0), dn(b.size(), 0.0); + for (size_t i = 0; i < d.size(); ++i) + if (fin(d[i])) { + if (d[i] > 0) up[i] = d[i]; + else dn[i] = -d[i]; + } + Series su = sma_s(stdev_s(up, n), n), sd = sma_s(stdev_s(dn, n), n); + Series out(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (fin(su[i]) && fin(sd[i]) && su[i] + sd[i] > 0) + out[i] = su[i] / (su[i] + sd[i]) * 100; + return {out}; +} +static Lines f_parkinson(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 20); + Series v(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (b[i].low > 0) { + double x = std::log(b[i].high / b[i].low); + v[i] = x * x; + } + Series m = sma_s(v, n), out(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (fin(m[i])) out[i] = std::sqrt(m[i] / (4 * std::log(2))) * 100; + return {out}; +} +static Lines f_garman_klass(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 20); + Series v(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (b[i].low > 0 && b[i].open > 0) { + double a = std::log(b[i].high / b[i].low); + double c = std::log(b[i].close / b[i].open); + v[i] = 0.5 * a * a - (2 * std::log(2) - 1) * c * c; + } + Series m = sma_s(v, n), out(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (fin(m[i]) && m[i] > 0) out[i] = std::sqrt(m[i]) * 100; + return {out}; +} +static Lines f_rogers_satchell(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 20); + Series v(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (b[i].open > 0 && b[i].close > 0) { + v[i] = std::log(b[i].high / b[i].close) * + std::log(b[i].high / b[i].open) + + std::log(b[i].low / b[i].close) * + std::log(b[i].low / b[i].open); + } + Series m = sma_s(v, n), out(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (fin(m[i]) && m[i] > 0) out[i] = std::sqrt(m[i]) * 100; + return {out}; +} +static Lines f_yang_zhang(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 20); + Series on(b.size(), 0.0), oc(b.size(), 0.0), rs(b.size(), 0.0); + for (size_t i = 0; i < b.size(); ++i) { + if (b[i].open <= 0 || b[i].close <= 0) continue; + if (i > 0 && b[i - 1].close > 0) { + double x = std::log(b[i].open / b[i - 1].close); + on[i] = x * x; + } + double y = std::log(b[i].close / b[i].open); + oc[i] = y * y; + if (b[i].high > 0 && b[i].low > 0) + rs[i] = std::log(b[i].high / b[i].close) * + std::log(b[i].high / b[i].open) + + std::log(b[i].low / b[i].close) * + std::log(b[i].low / b[i].open); + } + Series mo = sma_s(on, n), mc = sma_s(oc, n), mr = sma_s(rs, n); + Series out(b.size(), nanv()); + double k = 0.34; + for (size_t i = 0; i < b.size(); ++i) + if (fin(mo[i]) && fin(mc[i]) && fin(mr[i])) + out[i] = std::sqrt(std::max(0.0, mo[i] + k * mc[i] + + (1 - k) * mr[i])) * + 100; + return {out}; +} +static Lines f_thermometer(const std::vector& b, + const std::vector& p) { + Series m(b.size(), nanv()); + for (size_t i = 1; i < b.size(); ++i) + m[i] = std::max(std::fabs(b[i].high - b[i - 1].high), + std::fabs(b[i].low - b[i - 1].low)); + return {ema_s(m, pi(p, 0, 22))}; +} +static Lines f_std_error(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 20); + return {scale_s(stdev_s(closes(b), n), 1 / std::sqrt((double)n))}; +} +static Lines f_mean_dev(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 20); + Series c = closes(b), m = sma_s(c, n), out(c.size(), nanv()); + for (size_t i = 0; i < c.size(); ++i) { + if (!fin(m[i])) continue; + double s = 0; + for (int j = 0; j < n; ++j) s += std::fabs(c[i - j] - m[i]); + out[i] = s / n; + } + return {out}; +} +static Lines f_moment(const std::vector& b, + const std::vector& p, int power, bool norm) { + int n = pi(p, 0, 20); + Series c = closes(b), m = sma_s(c, n), sd = stdev_s(c, n); + Series out(c.size(), nanv()); + for (size_t i = 0; i < c.size(); ++i) { + if (!fin(m[i])) continue; + double s = 0; + for (int j = 0; j < n; ++j) s += std::pow(c[i - j] - m[i], power); + s /= n; + out[i] = norm && fin(sd[i]) && sd[i] > 0 + ? s / std::pow(sd[i], power) + : s; + } + return {out}; +} +static Lines f_skewness(const std::vector& b, + const std::vector& p) { + return f_moment(b, p, 3, true); +} +static Lines f_kurtosis(const std::vector& b, + const std::vector& p) { + Lines k = f_moment(b, p, 4, true); + for (double& v : k[0]) + if (fin(v)) v -= 3; + return k; +} +static Lines f_zscore(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 20); + Series c = closes(b); + return {combine(combine(c, sma_s(c, n), '-'), stdev_s(c, n), '/')}; +} +struct LinReg { + Series slope, intercept, value; +}; +static LinReg linreg(const Series& in, int n) { + LinReg r; + r.slope.assign(in.size(), nanv()); + r.intercept.assign(in.size(), nanv()); + r.value.assign(in.size(), nanv()); + for (size_t i = (size_t)(n - 1); i < in.size(); ++i) { + double sx = 0, sy = 0, sxx = 0, sxy = 0; + bool ok = true; + for (int j = 0; j < n; ++j) { + double x = j, y = in[i - n + 1 + j]; + if (!fin(y)) ok = false; + sx += x; sy += y; sxx += x * x; sxy += x * y; + } + if (!ok) continue; + double den = n * sxx - sx * sx; + if (den == 0) continue; + r.slope[i] = (n * sxy - sx * sy) / den; + r.intercept[i] = (sy - r.slope[i] * sx) / n; + r.value[i] = r.intercept[i] + r.slope[i] * (n - 1); + } + return r; +} +static Lines f_lr_slope(const std::vector& b, + const std::vector& p) { + return {linreg(closes(b), pi(p, 0, 20)).slope}; +} +static Lines f_lr_intercept(const std::vector& b, + const std::vector& p) { + return {linreg(closes(b), pi(p, 0, 20)).intercept}; +} +static Lines f_lr_angle(const std::vector& b, + const std::vector& p) { + Series s = linreg(closes(b), pi(p, 0, 20)).slope; + for (double& v : s) + if (fin(v)) v = std::atan(v) * 180 / M_PI; + return {s}; +} +static Lines f_r_squared(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 20); + Series c = closes(b), idx(c.size()); + for (size_t i = 0; i < c.size(); ++i) idx[i] = (double)i; + // Rolling correlation of close vs index, squared. + Series out(c.size(), nanv()); + for (size_t i = (size_t)(n - 1); i < c.size(); ++i) { + double mx = 0, my = 0; + for (int j = 0; j < n; ++j) { + mx += idx[i - j]; + my += c[i - j]; + } + mx /= n; my /= n; + double sxy = 0, sxx = 0, syy = 0; + for (int j = 0; j < n; ++j) { + double dx = idx[i - j] - mx, dy = c[i - j] - my; + sxy += dx * dy; sxx += dx * dx; syy += dy * dy; + } + if (sxx > 0 && syy > 0) out[i] = (sxy * sxy) / (sxx * syy); + } + return {out}; +} +static Lines f_tsf(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 20); + LinReg r = linreg(closes(b), n); + Series out(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (fin(r.intercept[i])) out[i] = r.intercept[i] + r.slope[i] * n; + return {out}; +} +static Lines f_forecast_osc(const std::vector& b, + const std::vector& p) { + Series tsf = f_tsf(b, p)[0]; + Series c = closes(b), out(c.size(), nanv()); + for (size_t i = 1; i < c.size(); ++i) + if (fin(tsf[i - 1]) && c[i] != 0) + out[i] = (c[i] - tsf[i - 1]) / c[i] * 100; + return {out}; +} + +// ============================================================ Ehlers / Hilbert + +struct HilbertData { + Series period, phase, inphase, quadrature; +}; +// Homodyne discriminator (Ehlers): dominant cycle period + phase. +static HilbertData hilbert(const Series& price) { + size_t N = price.size(); + HilbertData r; + r.period.assign(N, nanv()); + r.phase.assign(N, nanv()); + r.inphase.assign(N, nanv()); + r.quadrature.assign(N, nanv()); + std::vector smooth(N, 0), detr(N, 0), q1(N, 0), i1(N, 0), + ji(N, 0), jq(N, 0), re(N, 0), im(N, 0); + auto at = [&](const std::vector& v, long i) { + if (i < 0) return v.empty() ? 0.0 : v[0]; + if (i >= (long)v.size()) return v.back(); + return v[(size_t)i]; + }; + double period = 20, phase = 0; + for (size_t i = 0; i < N; ++i) { + long ii = (long)i; + smooth[i] = (4 * price[i] + 3 * at(price, ii - 1) + + 2 * at(price, ii - 2) + at(price, ii - 3)) / + 10; + double adj = 0.075 * period + 0.54; + detr[i] = (0.0962 * smooth[i] + 0.5769 * at(smooth, ii - 2) - + 0.5769 * at(smooth, ii - 4) - 0.0962 * at(smooth, ii - 6)) * + adj; + q1[i] = (0.0962 * detr[i] + 0.5769 * at(detr, ii - 2) - + 0.5769 * at(detr, ii - 4) - 0.0962 * at(detr, ii - 6)) * + adj; + i1[i] = at(detr, ii - 3); + ji[i] = (0.0962 * i1[i] + 0.5769 * at(i1, ii - 2) - + 0.5769 * at(i1, ii - 4) - 0.0962 * at(i1, ii - 6)) * + adj; + jq[i] = (0.0962 * q1[i] + 0.5769 * at(q1, ii - 2) - + 0.5769 * at(q1, ii - 4) - 0.0962 * at(q1, ii - 6)) * + adj; + double re_raw = i1[i] * ji[i] + q1[i] * jq[i]; + double im_raw = i1[i] * jq[i] - q1[i] * ji[i]; + re[i] = 0.2 * re_raw + 0.8 * at(re, ii - 1); + im[i] = 0.2 * im_raw + 0.8 * at(im, ii - 1); + if (im[i] != 0 && re[i] != 0) { + double p = 2 * M_PI / std::atan(im[i] / re[i]); + p = std::min(p, 1.5 * period); + p = std::max(p, 0.67 * period); + p = std::min(50.0, std::max(6.0, p)); + period = 0.2 * p + 0.8 * period; + } + if (i > 6) { + r.period[i] = period; + r.inphase[i] = i1[i]; + r.quadrature[i] = q1[i]; + double dp = i1[i] != 0 ? std::atan(q1[i] / i1[i]) * 180 / M_PI : 0; + phase += dp; + while (phase >= 360) phase -= 360; + while (phase < 0) phase += 360; + r.phase[i] = phase; + } + } + return r; +} +static Lines f_ht_period(const std::vector& b, + const std::vector&) { + return {hilbert(closes(b)).period}; +} +static Lines f_ht_phase(const std::vector& b, + const std::vector&) { + return {hilbert(closes(b)).phase}; +} +static Lines f_ht_phasor(const std::vector& b, + const std::vector&) { + HilbertData h = hilbert(closes(b)); + return {h.inphase, h.quadrature}; +} +static Lines f_ht_sine(const std::vector& b, + const std::vector&) { + HilbertData h = hilbert(closes(b)); + Series s1(b.size(), nanv()), s2(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (fin(h.phase[i])) { + double rad = h.phase[i] * M_PI / 180; + s1[i] = std::sin(rad); + s2[i] = std::sin(rad + M_PI / 4); + } + return {s1, s2}; +} +static Series instantaneous_trendline(const Series& price) { + Series out(price.size(), nanv()); + double alpha = 0.07; + for (size_t i = 0; i < price.size(); ++i) { + double p1 = i > 0 ? price[i - 1] : price[i]; + double p2 = i > 1 ? price[i - 2] : price[i]; + double t1 = i > 0 && fin(out[i - 1]) ? out[i - 1] : price[i]; + double t2 = i > 1 && fin(out[i - 2]) ? out[i - 2] : price[i]; + out[i] = (alpha - alpha * alpha / 4) * price[i] + + 0.5 * alpha * alpha * p1 - + (alpha - 0.75 * alpha * alpha) * p2 + + 2 * (1 - alpha) * t1 - (1 - alpha) * (1 - alpha) * t2; + } + return out; +} +static Lines f_ht_trendline(const std::vector& b, + const std::vector&) { + return {instantaneous_trendline(closes(b))}; +} +static Lines f_ht_trendmode(const std::vector& b, + const std::vector&) { + HilbertData h = hilbert(closes(b)); + Series it = instantaneous_trendline(closes(b)); + Series c = closes(b), out(c.size(), nanv()); + int age = 0; + for (size_t i = 1; i < c.size(); ++i) { + if (!fin(it[i])) continue; + if ((c[i] - it[i]) * (c[i - 1] - it[i - 1]) < 0) age = 0; + else ++age; + double per = fin(h.period[i]) ? h.period[i] : 20; + out[i] = age > per ? 1 : 0; // 1 = trending, 0 = cycling + } + return {out}; +} +static Lines f_mama(const std::vector& b, const std::vector& p, + bool fama) { + double fast_lim = pd(p, 0, 0.5), slow_lim = pd(p, 1, 0.05); + HilbertData h = hilbert(closes(b)); + Series c = closes(b), out(c.size(), nanv()); + double m = 0, f = 0; + bool started = false; + for (size_t i = 1; i < c.size(); ++i) { + if (!fin(h.phase[i]) || !fin(h.phase[i - 1])) continue; + double dp = h.phase[i - 1] - h.phase[i]; + if (dp < 0) dp += 360; + dp = std::max(1.0, dp); + double alpha = std::min(fast_lim, std::max(slow_lim, fast_lim / dp)); + if (!started) { + m = c[i]; + f = c[i]; + started = true; + } + m = alpha * c[i] + (1 - alpha) * m; + f = 0.5 * alpha * m + (1 - 0.5 * alpha) * f; + out[i] = fama ? f : m; + } + return {out}; +} +static Lines f_mama_line(const std::vector& b, + const std::vector& p) { + return f_mama(b, p, false); +} +static Lines f_fama_line(const std::vector& b, + const std::vector& p) { + return f_mama(b, p, true); +} +static Lines f_cyber_cycle(const std::vector& b, + const std::vector& p) { + double a = pd(p, 0, 0.07); + Series c = closes(b), cyc(c.size(), nanv()); + for (size_t i = 0; i < c.size(); ++i) { + auto sm = [&](long k) { + long i0 = (long)i - k; + double v0 = c[std::max(0L, i0)], v1 = c[std::max(0L, i0 - 1)], + v2 = c[std::max(0L, i0 - 2)], v3 = c[std::max(0L, i0 - 3)]; + return (v0 + 2 * v1 + 2 * v2 + v3) / 6; + }; + double s0 = sm(0), s1 = sm(1), s2 = sm(2); + double c1 = i > 0 && fin(cyc[i - 1]) ? cyc[i - 1] : 0; + double c2 = i > 1 && fin(cyc[i - 2]) ? cyc[i - 2] : 0; + double k = 1 - 0.5 * a; + cyc[i] = k * k * (s0 - 2 * s1 + s2) + 2 * (1 - a) * c1 - + (1 - a) * (1 - a) * c2; + } + return {cyc, shift_s(cyc, 1)}; +} +static Lines f_cg(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 10); + Series c = closes(b), out(c.size(), nanv()); + for (size_t i = (size_t)(n - 1); i < c.size(); ++i) { + double num = 0, den = 0; + for (int j = 0; j < n; ++j) { + num += (j + 1) * c[i - j]; + den += c[i - j]; + } + if (den != 0) out[i] = -num / den + (n + 1) / 2.0; + } + return {out, shift_s(out, 1)}; +} +static Lines f_ehlers_stoch(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 14); + Series sm = super_smoother_s(closes(b), 10); + Series mx = rolling_max(sm, n), mn = rolling_min(sm, n); + Series st(sm.size(), nanv()); + for (size_t i = 0; i < sm.size(); ++i) + if (fin(mx[i]) && fin(mn[i]) && mx[i] > mn[i]) + st[i] = (sm[i] - mn[i]) / (mx[i] - mn[i]); + Series s1 = super_smoother_s(st, 5); + return {s1, super_smoother_s(s1, 5)}; +} +static Series highpass_s(const Series& in, int n) { + Series out(in.size(), nanv()); + double w = 0.707 * 2 * M_PI / n; + double a = (std::cos(w) + std::sin(w) - 1) / std::cos(w); + for (size_t i = 0; i < in.size(); ++i) { + double x1 = i > 0 ? in[i - 1] : in[i], x2 = i > 1 ? in[i - 2] : in[i]; + double h1 = i > 0 && fin(out[i - 1]) ? out[i - 1] : 0; + double h2 = i > 1 && fin(out[i - 2]) ? out[i - 2] : 0; + double k = 1 - a / 2; + out[i] = k * k * (in[i] - 2 * x1 + x2) + 2 * (1 - a) * h1 - + (1 - a) * (1 - a) * h2; + } + return out; +} +static Lines f_roofing(const std::vector& b, + const std::vector& p) { + (void)p; + return {super_smoother_s(highpass_s(closes(b), 48), 10)}; +} +static Lines f_bandpass(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 20); + double bw = pd(p, 1, 0.3); + Series c = closes(b), out(c.size(), nanv()); + double beta = std::cos(2 * M_PI / n); + double gamma = 1 / std::cos(2 * M_PI * bw / n); + double alpha = gamma - std::sqrt(gamma * gamma - 1); + for (size_t i = 0; i < c.size(); ++i) { + double x2 = i > 1 ? c[i - 2] : c[i]; + double b1 = i > 0 && fin(out[i - 1]) ? out[i - 1] : 0; + double b2 = i > 1 && fin(out[i - 2]) ? out[i - 2] : 0; + out[i] = 0.5 * (1 - alpha) * (c[i] - x2) + + beta * (1 + alpha) * b1 - alpha * b2; + } + return {out}; +} +static Lines f_highpass(const std::vector& b, + const std::vector& p) { + return {highpass_s(closes(b), pi(p, 0, 48))}; +} +static Lines f_squeeze(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 20); + Series c = closes(b); + Series mid = scale_s(combine(rolling_max(highs(b), n), + rolling_min(lows(b), n), '+'), + 0.5); + Series d = combine(c, scale_s(combine(mid, sma_s(c, n), '+'), 0.5), '-'); + LinReg r = linreg(d, n); + return {r.value}; +} +static Lines f_wavetrend(const std::vector& b, + const std::vector& p) { + (void)p; + Series ap = hlc3(b); + Series esa = ema_s(ap, 10); + Series d = ema_s(combine(ap, esa, '-'), 10); + Series absd(b.size(), nanv()); + Series diff = combine(ap, esa, '-'); + for (size_t i = 0; i < b.size(); ++i) + if (fin(diff[i])) absd[i] = std::fabs(diff[i]); + Series dd = ema_s(absd, 10); + Series ci(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (fin(dd[i]) && dd[i] > 0 && fin(d[i])) + ci[i] = d[i] / (0.015 * dd[i]); + Series wt1 = ema_s(ci, 21); + return {wt1, sma_s(wt1, 4)}; +} +static Lines f_pmo(const std::vector& b, const std::vector& p) { + (void)p; + Series roc1(b.size(), nanv()); + Series c = closes(b); + for (size_t i = 1; i < c.size(); ++i) + if (c[i - 1] != 0) roc1[i] = (c[i] / c[i - 1] - 1) * 100; + Series s1 = ema_s(roc1, 35); + Series s2 = ema_s(scale_s(s1, 10), 20); + return {s2, ema_s(s2, 10)}; +} + +static void defs_volatility(std::vector& v) { + auto O = PlotKind::Overlay, P = PlotKind::Pane; + auto L = Style::Lines, B = Style::Bars; + v.push_back({"ATR", P, L, 0, "14", f_atr}); + v.push_back({"NATR", P, L, 1, "14", f_natr}); + v.push_back({"True Range", P, L, 2, "", f_tr}); + v.push_back({"Standard Deviation", P, L, 3, "20", f_stddev}); + v.push_back({"Variance", P, L, 4, "20", f_variance}); + v.push_back({"Historical Volatility", P, L, 5, "20", f_hist_vol}); + v.push_back({"Chaikin Volatility", P, L, 6, "10,10", f_chaikin_vol}); + v.push_back({"Ulcer Index", P, L, 7, "14", f_ulcer}); + v.push_back({"Choppiness Index", P, L, 8, "14", f_choppiness}); + v.push_back({"Relative Volatility Index", P, L, 9, "10", f_rvol_idx}); + v.push_back({"Parkinson Volatility", P, L, 10, "20", f_parkinson}); + v.push_back({"Garman-Klass Volatility", P, L, 11, "20", f_garman_klass}); + v.push_back({"Rogers-Satchell Volatility", P, L, 12, "20", f_rogers_satchell}); + v.push_back({"Yang-Zhang Volatility", P, L, 13, "20", f_yang_zhang}); + v.push_back({"Market Thermometer", P, L, 0, "22", f_thermometer}); + v.push_back({"Standard Error", P, L, 1, "20", f_std_error}); + v.push_back({"Mean Deviation", P, L, 2, "20", f_mean_dev}); + v.push_back({"Skewness", P, L, 3, "20", f_skewness}); + v.push_back({"Kurtosis", P, L, 4, "20", f_kurtosis}); + v.push_back({"Z-Score", P, L, 5, "20", f_zscore}); + v.push_back({"Linear Regression Slope", P, L, 6, "20", f_lr_slope}); + v.push_back({"Linear Regression Intercept", P, L, 7, "20", f_lr_intercept}); + v.push_back({"Linear Regression Angle", P, L, 8, "20", f_lr_angle}); + v.push_back({"R-Squared", P, L, 9, "20", f_r_squared}); + v.push_back({"TSF", O, L, 10, "20", f_tsf}); + v.push_back({"Forecast Oscillator", P, L, 11, "20", f_forecast_osc}); + v.push_back({"HT DC Period", P, L, 12, "", f_ht_period}); + v.push_back({"HT DC Phase", P, L, 13, "", f_ht_phase}); + v.push_back({"HT Phasor", P, L, 0, "", f_ht_phasor}); + v.push_back({"HT Sine", P, L, 1, "", f_ht_sine}); + v.push_back({"HT Trend Mode", P, L, 2, "", f_ht_trendmode}); + v.push_back({"HT Trendline", O, L, 3, "", f_ht_trendline}); + v.push_back({"MAMA", O, L, 4, "0.5,0.05", f_mama_line}); + v.push_back({"FAMA", O, L, 5, "0.5,0.05", f_fama_line}); + v.push_back({"Cyber Cycle", P, L, 6, "0.07", f_cyber_cycle}); + v.push_back({"CG Oscillator", P, L, 7, "10", f_cg}); + v.push_back({"Ehlers Stochastic", P, L, 8, "14", f_ehlers_stoch}); + v.push_back({"Roofing Filter", P, L, 9, "", f_roofing}); + v.push_back({"Band-Pass Filter", P, L, 10, "20,0.3", f_bandpass}); + v.push_back({"High-Pass Filter", P, L, 11, "48", f_highpass}); + v.push_back({"Squeeze Momentum", P, B, 12, "20", f_squeeze}); + v.push_back({"WaveTrend", P, L, 13, "", f_wavetrend}); + v.push_back({"PMO", P, L, 0, "", f_pmo}); +} + +// ============================================================ candlestick patterns +// TA-Lib's 61 patterns plus five extras. Each returns markers: +// +1 bullish (triangle under the bar), -1 bearish (triangle above). + +struct PC { + double o, h, l, c, body, range, upper, lower; + bool bull; +}; +static std::vector pcs_of(const std::vector& b) { + std::vector v(b.size()); + for (size_t i = 0; i < b.size(); ++i) { + PC& p = v[i]; + p.o = b[i].open; p.h = b[i].high; p.l = b[i].low; p.c = b[i].close; + p.body = std::fabs(p.c - p.o); + p.range = p.h - p.l; + p.upper = p.h - std::max(p.o, p.c); + p.lower = std::min(p.o, p.c) - p.l; + p.bull = p.c >= p.o; + } + return v; +} +static double avg_before(const std::vector& v, size_t i, int n, + bool body) { + double s = 0; + int cnt = 0; + for (size_t k = i >= (size_t)n ? i - n : 0; k < i; ++k) { + s += body ? v[k].body : v[k].range; + ++cnt; + } + return cnt ? s / cnt : 0; +} +static Series marks(const std::vector& b) { + return Series(b.size(), 0.0); +} +static bool is_doji(const PC& p) { return p.range > 0 && p.body <= 0.1 * p.range; } +static bool long_body(const std::vector& v, size_t i) { + return v[i].body > avg_before(v, i, 10, true) && v[i].body > 0; +} +static bool small_body(const std::vector& v, size_t i) { + return v[i].body < avg_before(v, i, 10, true); +} + +static Lines pat_doji(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 0; i < v.size(); ++i) + if (is_doji(v[i])) m[i] = 1; + return {m}; +} +static Lines pat_dragonfly(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 0; i < v.size(); ++i) + if (is_doji(v[i]) && v[i].lower > 3 * v[i].body && + v[i].upper < v[i].lower * 0.25) + m[i] = 1; + return {m}; +} +static Lines pat_gravestone(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 0; i < v.size(); ++i) + if (is_doji(v[i]) && v[i].upper > 3 * v[i].body && + v[i].lower < v[i].upper * 0.25) + m[i] = -1; + return {m}; +} +static Lines pat_longlegged(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 0; i < v.size(); ++i) + if (is_doji(v[i]) && v[i].upper > v[i].range * 0.3 && + v[i].lower > v[i].range * 0.3) + m[i] = 1; + return {m}; +} +static Lines pat_rickshaw(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 0; i < v.size(); ++i) + if (is_doji(v[i]) && v[i].upper > v[i].range * 0.35 && + v[i].lower > v[i].range * 0.35) + m[i] = 1; + return {m}; +} +static Lines pat_highwave(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) + if (small_body(v, i) && v[i].upper > v[i].body * 2 && + v[i].lower > v[i].body * 2) + m[i] = 1; + return {m}; +} +static Lines pat_spinningtop(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) + if (small_body(v, i) && v[i].upper > v[i].body && + v[i].lower > v[i].body && !is_doji(v[i])) + m[i] = 1; + return {m}; +} +static Lines pat_hammer(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) + if (small_body(v, i) && v[i].lower > 2 * v[i].body && + v[i].upper < v[i].body && v[i - 1].c < v[i - 1].o + v[i - 1].body) + m[i] = 1; + return {m}; +} +static Lines pat_takuri(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 0; i < v.size(); ++i) + if (small_body(v, i) && v[i].body > 0 && + v[i].lower > 3 * avg_before(v, i, 10, false) * 0.3 && + v[i].upper < v[i].body) + m[i] = 1; + return {m}; +} +static Lines pat_inv_hammer(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 0; i < v.size(); ++i) + if (small_body(v, i) && v[i].upper > 2 * v[i].body && + v[i].lower < v[i].body) + m[i] = 1; + return {m}; +} +static Lines pat_shooting_star(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) + if (small_body(v, i) && v[i].upper > 2 * v[i].body && + v[i].lower < v[i].body && v[i].l > v[i - 1].h) + m[i] = -1; + return {m}; +} +static Lines pat_hanging_man(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) + if (small_body(v, i) && v[i].lower > 2 * v[i].body && + v[i].upper < v[i].body && v[i - 1].bull) + m[i] = -1; + return {m}; +} +static Lines pat_engulfing(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) { + const PC& a = v[i - 1]; + const PC& c = v[i]; + if (!a.bull && c.bull && c.c >= a.o && c.o <= a.c) m[i] = 1; + if (a.bull && !c.bull && c.o >= a.c && c.c <= a.o) m[i] = -1; + } + return {m}; +} +static Lines pat_harami(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) { + const PC& a = v[i - 1]; + const PC& c = v[i]; + if (!long_body(v, i - 1)) continue; + bool inside = std::max(c.o, c.c) <= std::max(a.o, a.c) && + std::min(c.o, c.c) >= std::min(a.o, a.c); + if (inside) m[i] = a.bull ? -1 : 1; + } + return {m}; +} +static Lines pat_harami_cross(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) { + const PC& a = v[i - 1]; + const PC& c = v[i]; + if (!long_body(v, i - 1) || !is_doji(c)) continue; + bool inside = std::max(c.o, c.c) <= std::max(a.o, a.c) && + std::min(c.o, c.c) >= std::min(a.o, a.c); + if (inside) m[i] = a.bull ? -1 : 1; + } + return {m}; +} +static Lines pat_homing_pigeon(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) { + const PC& a = v[i - 1]; + const PC& c = v[i]; + if (!a.bull && !c.bull && c.o < a.o && c.c > a.c) m[i] = 1; + } + return {m}; +} +static Lines pat_piercing(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) { + const PC& a = v[i - 1]; + const PC& c = v[i]; + if (!a.bull && long_body(v, i - 1) && c.bull && c.o < a.l && + c.c > (a.o + a.c) / 2 && c.c < a.o) + m[i] = 1; + } + return {m}; +} +static Lines pat_dark_cloud(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) { + const PC& a = v[i - 1]; + const PC& c = v[i]; + if (a.bull && long_body(v, i - 1) && !c.bull && c.o > a.h && + c.c < (a.o + a.c) / 2 && c.c > a.o) + m[i] = -1; + } + return {m}; +} +static Lines pat_thrusting(const std::vector& b, const std::vector&) { + // Long white candle, then a black candle opening above its high and + // closing into its body but NOT past the midpoint (that would be + // Dark Cloud Cover). + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) { + const PC& a = v[i - 1]; + const PC& c = v[i]; + if (a.bull && long_body(v, i - 1) && !c.bull && c.o > a.h && + c.c < a.c && c.c > (a.o + a.c) / 2) + m[i] = -1; + } + return {m}; +} +static Lines pat_in_neck(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) { + const PC& a = v[i - 1]; + const PC& c = v[i]; + if (!a.bull && long_body(v, i - 1) && c.bull && small_body(v, i) && + std::fabs(c.c - a.l) <= a.range * 0.05) + m[i] = -1; + } + return {m}; +} +static Lines pat_on_neck(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) { + const PC& a = v[i - 1]; + const PC& c = v[i]; + if (!a.bull && long_body(v, i - 1) && c.bull && small_body(v, i) && + c.c > a.l && c.c <= a.l + a.range * 0.1) + m[i] = -1; + } + return {m}; +} +static Lines pat_counterattack(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) { + const PC& a = v[i - 1]; + const PC& c = v[i]; + if (long_body(v, i - 1) && long_body(v, i) && a.bull != c.bull && + std::fabs(c.c - a.c) <= a.range * 0.05) + m[i] = c.bull ? 1 : -1; + } + return {m}; +} +static Lines pat_separating(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) { + const PC& a = v[i - 1]; + const PC& c = v[i]; + if (a.bull != c.bull && std::fabs(c.o - a.o) <= a.range * 0.05 && + long_body(v, i)) + m[i] = c.bull ? 1 : -1; + } + return {m}; +} +static Lines pat_matching_low(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) { + const PC& a = v[i - 1]; + const PC& c = v[i]; + if (!a.bull && !c.bull && + std::fabs(c.c - a.c) <= a.range * 0.02) + m[i] = 1; + } + return {m}; +} +static Lines pat_belt_hold(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 0; i < v.size(); ++i) { + if (!long_body(v, i)) continue; + if (v[i].bull && v[i].lower < v[i].body * 0.1) m[i] = 1; + if (!v[i].bull && v[i].upper < v[i].body * 0.1) m[i] = -1; + } + return {m}; +} +static Lines pat_marubozu(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) + if (long_body(v, i) && v[i].upper < v[i].body * 0.1 && + v[i].lower < v[i].body * 0.1) + m[i] = v[i].bull ? 1 : -1; + return {m}; +} +static Lines pat_closing_marubozu(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) { + if (!long_body(v, i)) continue; + if (v[i].bull && v[i].upper < v[i].body * 0.1) m[i] = 1; + if (!v[i].bull && v[i].lower < v[i].body * 0.1) m[i] = -1; + } + return {m}; +} +static Lines pat_long_line(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) + if (long_body(v, i) && !is_doji(v[i])) m[i] = v[i].bull ? 1 : -1; + return {m}; +} +static Lines pat_short_line(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) + if (small_body(v, i) && v[i].range < avg_before(v, i, 10, false) && + !is_doji(v[i])) + m[i] = v[i].bull ? 1 : -1; + return {m}; +} +static Lines pat_doji_star(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) { + const PC& a = v[i - 1]; + const PC& c = v[i]; + if (!long_body(v, i - 1) || !is_doji(c)) continue; + if (a.bull && std::min(c.o, c.c) > a.c) m[i] = -1; + if (!a.bull && std::max(c.o, c.c) < a.c) m[i] = 1; + } + return {m}; +} +static Lines pat_morning_star(const std::vector& b, const std::vector&, bool doji) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& a = v[i - 2]; + const PC& s = v[i - 1]; + const PC& c = v[i]; + bool star_ok = doji ? is_doji(s) : small_body(v, i - 1); + if (!a.bull && long_body(v, i - 2) && star_ok && c.bull && + long_body(v, i) && std::max(s.o, s.c) < a.c && + c.c > (a.o + a.c) / 2) + m[i] = 1; + } + return {m}; +} +static Lines pat_evening_star(const std::vector& b, const std::vector&, bool doji) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& a = v[i - 2]; + const PC& s = v[i - 1]; + const PC& c = v[i]; + bool star_ok = doji ? is_doji(s) : small_body(v, i - 1); + if (a.bull && long_body(v, i - 2) && star_ok && !c.bull && + long_body(v, i) && std::min(s.o, s.c) > a.c && + c.c < (a.o + a.c) / 2) + m[i] = -1; + } + return {m}; +} +static Lines pat_morning_star_w(const std::vector& b, const std::vector& p) { + return pat_morning_star(b, p, false); +} +static Lines pat_morning_doji_star_w(const std::vector& b, const std::vector& p) { + return pat_morning_star(b, p, true); +} +static Lines pat_evening_star_w(const std::vector& b, const std::vector& p) { + return pat_evening_star(b, p, false); +} +static Lines pat_evening_doji_star_w(const std::vector& b, const std::vector& p) { + return pat_evening_star(b, p, true); +} +static Lines pat_abandoned_baby(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& a = v[i - 2]; + const PC& s = v[i - 1]; + const PC& c = v[i]; + if (!is_doji(s)) continue; + if (!a.bull && c.bull && s.h < a.l && s.h < c.l) m[i] = 1; + if (a.bull && !c.bull && s.l > a.h && s.l > c.h) m[i] = -1; + } + return {m}; +} +static Lines pat_soldiers(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& a = v[i - 2]; + const PC& d = v[i - 1]; + const PC& c = v[i]; + if (a.bull && d.bull && c.bull && d.o > a.o && d.o < a.c && + c.o > d.o && c.o < d.c && c.c > d.c && d.c > a.c) + m[i] = 1; + } + return {m}; +} +static Lines pat_crows(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& a = v[i - 2]; + const PC& d = v[i - 1]; + const PC& c = v[i]; + if (!a.bull && !d.bull && !c.bull && d.o < a.o && d.o > a.c && + c.o < d.o && c.o > d.c && c.c < d.c && d.c < a.c) + m[i] = -1; + } + return {m}; +} +static Lines pat_identical_crows(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& a = v[i - 2]; + const PC& d = v[i - 1]; + const PC& c = v[i]; + if (!a.bull && !d.bull && !c.bull && + std::fabs(d.o - a.c) <= a.range * 0.02 && + std::fabs(c.o - d.c) <= d.range * 0.02) + m[i] = -1; + } + return {m}; +} +static Lines pat_two_crows(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& a = v[i - 2]; + const PC& d = v[i - 1]; + const PC& c = v[i]; + if (a.bull && long_body(v, i - 2) && !d.bull && !c.bull && + d.o > a.c && c.o > d.c && c.c < d.c && c.c > a.o && c.c < a.c) + m[i] = -1; + } + return {m}; +} +static Lines pat_upside_gap_crows(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& a = v[i - 2]; + const PC& d = v[i - 1]; + const PC& c = v[i]; + if (a.bull && !d.bull && !c.bull && d.l > a.h && c.o > d.c && + c.c < d.c && c.c > a.h) + m[i] = -1; + } + return {m}; +} +static Lines pat_advance_block(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& a = v[i - 2]; + const PC& d = v[i - 1]; + const PC& c = v[i]; + if (a.bull && d.bull && c.bull && long_body(v, i - 2) && + d.body < a.body && c.body < d.body && d.upper > d.body && + c.upper > c.body) + m[i] = -1; + } + return {m}; +} +static Lines pat_stalled(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& a = v[i - 2]; + const PC& d = v[i - 1]; + const PC& c = v[i]; + if (a.bull && d.bull && long_body(v, i - 2) && + (is_doji(c) || small_body(v, i)) && c.o > d.o && c.o < d.c) + m[i] = -1; + } + return {m}; +} +static Lines pat_three_inside(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& a = v[i - 2]; + const PC& s = v[i - 1]; + const PC& c = v[i]; + bool inside = std::max(s.o, s.c) <= std::max(a.o, a.c) && + std::min(s.o, s.c) >= std::min(a.o, a.c); + if (!inside || !long_body(v, i - 2)) continue; + if (!a.bull && c.bull && c.c > a.o) m[i] = 1; + if (a.bull && !c.bull && c.c < a.o) m[i] = -1; + } + return {m}; +} +static Lines pat_three_outside(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& a = v[i - 2]; + const PC& e = v[i - 1]; + const PC& c = v[i]; + bool engulf = (!a.bull && e.bull && e.c >= a.o && e.o <= a.c) || + (a.bull && !e.bull && e.o >= a.c && e.c <= a.o); + if (!engulf) continue; + if (e.bull && c.bull && c.c > e.c) m[i] = 1; + if (!e.bull && !c.bull && c.c < e.c) m[i] = -1; + } + return {m}; +} +static Lines pat_line_strike(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 3; i < v.size(); ++i) { + const PC& a = v[i - 3]; + const PC& d = v[i - 2]; + const PC& e = v[i - 1]; + const PC& c = v[i]; + if (a.bull && d.bull && e.bull && !c.bull && c.o > e.c && + c.c < a.o) + m[i] = -1; + if (!a.bull && !d.bull && !e.bull && c.bull && c.o < e.c && + c.c > a.o) + m[i] = 1; + } + return {m}; +} +static Lines pat_stars_south(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& a = v[i - 2]; + const PC& d = v[i - 1]; + const PC& c = v[i]; + if (!a.bull && !d.bull && !c.bull && long_body(v, i - 2) && + d.body < a.body && c.body < d.body && d.lower > d.body && + c.lower > c.body && c.l > d.l) + m[i] = 1; + } + return {m}; +} +static Lines pat_concealing_swallow(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 3; i < v.size(); ++i) { + const PC& a = v[i - 3]; + const PC& d = v[i - 2]; + const PC& e = v[i - 1]; + const PC& c = v[i]; + if (!a.bull && !d.bull && !e.bull && !c.bull && + a.upper < a.body * 0.1 && a.lower < a.body * 0.1 && + e.h > d.c && c.o > e.h && c.c < e.l) + m[i] = 1; + } + return {m}; +} +static Lines pat_ladder_bottom(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 4; i < v.size(); ++i) { + const PC& a = v[i - 4]; + const PC& d = v[i - 3]; + const PC& e = v[i - 2]; + const PC& f = v[i - 1]; + const PC& c = v[i]; + if (!a.bull && !d.bull && !e.bull && !f.bull && f.upper > f.body && + c.bull && c.o > f.o && c.c > f.h) + m[i] = 1; + } + return {m}; +} +static Lines pat_breakaway(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 4; i < v.size(); ++i) { + const PC& a = v[i - 4]; + const PC& c = v[i]; + bool mid_same = v[i - 3].bull == a.bull && v[i - 2].bull == a.bull && + v[i - 1].bull == a.bull; + if (!mid_same) continue; + if (!a.bull && c.bull && v[i - 3].h < a.l && c.c < a.c && + c.c > v[i - 3].l) + m[i] = 1; + if (a.bull && !c.bull && v[i - 3].l > a.h && c.c > a.c && + c.c < v[i - 3].h) + m[i] = -1; + } + return {m}; +} +static Lines pat_mat_hold(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 4; i < v.size(); ++i) { + const PC& a = v[i - 4]; + const PC& c = v[i]; + bool smalls = small_body(v, i - 3) && small_body(v, i - 2) && + small_body(v, i - 1); + if (!smalls || !long_body(v, i - 4) || !long_body(v, i)) continue; + if (a.bull && c.bull && v[i - 3].o > a.c && c.c > a.c) m[i] = 1; + if (!a.bull && !c.bull && v[i - 3].o < a.c && c.c < a.c) m[i] = -1; + } + return {m}; +} +static Lines pat_risefall_methods(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 4; i < v.size(); ++i) { + const PC& a = v[i - 4]; + const PC& c = v[i]; + bool smalls = small_body(v, i - 3) && small_body(v, i - 2) && + small_body(v, i - 1); + bool within = std::max(v[i - 2].h, v[i - 1].h) <= a.h && + std::min(v[i - 2].l, v[i - 1].l) >= a.l; + if (!smalls || !within || !long_body(v, i - 4) || + !long_body(v, i)) + continue; + if (a.bull && c.bull && !v[i - 2].bull && c.c > a.c) m[i] = 1; + if (!a.bull && !c.bull && v[i - 2].bull && c.c < a.c) m[i] = -1; + } + return {m}; +} +static Lines pat_kicking(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) { + const PC& a = v[i - 1]; + const PC& c = v[i]; + bool maru_a = a.upper < a.body * 0.1 && a.lower < a.body * 0.1; + bool maru_c = c.upper < c.body * 0.1 && c.lower < c.body * 0.1; + if (!maru_a || !maru_c || a.bull == c.bull) continue; + bool gap = c.bull ? c.l > a.h : c.h < a.l; + if (gap) m[i] = c.bull ? 1 : -1; + } + return {m}; +} +static Lines pat_kicking_length(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) { + const PC& a = v[i - 1]; + const PC& c = v[i]; + bool maru_a = a.upper < a.body * 0.1 && a.lower < a.body * 0.1; + bool maru_c = c.upper < c.body * 0.1 && c.lower < c.body * 0.1; + if (!maru_a || !maru_c || a.bull == c.bull) continue; + bool gap = c.bull ? c.l > a.h : c.h < a.l; + if (gap) m[i] = c.body >= a.body ? (c.bull ? 1 : -1) + : (a.bull ? 1 : -1); + } + return {m}; +} +static Lines pat_tasuki_gap(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& a = v[i - 2]; + const PC& d = v[i - 1]; + const PC& c = v[i]; + if (a.bull && d.bull && !c.bull && d.l > a.h && c.o < d.c && + c.o > d.o && c.c > a.h && c.c < d.l) + m[i] = 1; + if (!a.bull && !d.bull && c.bull && d.h < a.l && c.o > d.c && + c.o < d.o && c.c < a.l && c.c > d.h) + m[i] = -1; + } + return {m}; +} +static Lines pat_gap_side_white(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& a = v[i - 2]; + const PC& d = v[i - 1]; + const PC& c = v[i]; + if (d.bull && c.bull && d.l > a.h && + std::fabs(c.o - d.o) <= d.range * 0.1) + m[i] = 1; + if (!d.bull && !c.bull && d.h < a.l && + std::fabs(c.o - d.o) <= d.range * 0.1) + m[i] = -1; + } + return {m}; +} +static Lines pat_xside_gap_methods(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& d = v[i - 1]; + const PC& c = v[i]; + const PC& a = v[i - 2]; + if (d.bull && !c.bull && d.l > a.h && c.o > d.c && c.c < d.o) + m[i] = -1; + if (!d.bull && c.bull && d.h < a.l && c.o < d.c && c.c > d.o) + m[i] = 1; + } + return {m}; +} +static Lines pat_hikkake(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& a = v[i - 2]; + const PC& in = v[i - 1]; + const PC& c = v[i]; + bool inside = in.h < a.h && in.l > a.l; + if (!inside) continue; + if (c.c < in.l) m[i] = 1; // failed breakdown -> bullish hikkake + if (c.c > in.h) m[i] = -1; // failed breakout -> bearish + } + return {m}; +} +static Lines pat_hikkake_mod(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 3; i < v.size(); ++i) { + const PC& a = v[i - 3]; + const PC& in = v[i - 2]; + const PC& f = v[i - 1]; + const PC& c = v[i]; + bool inside = in.h < a.h && in.l > a.l; + if (!inside) continue; + if (f.c < in.l && c.c > in.l && c.c < in.h) m[i] = 1; + if (f.c > in.h && c.c < in.h && c.c > in.l) m[i] = -1; + } + return {m}; +} +static Lines pat_tristar(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + if (is_doji(v[i - 2]) && is_doji(v[i - 1]) && is_doji(v[i])) { + if (v[i - 1].l > v[i - 2].h && v[i - 1].l > v[i].h) m[i] = -1; + else if (v[i - 1].h < v[i - 2].l && v[i - 1].h < v[i].l) + m[i] = 1; + else m[i] = 1; + } + } + return {m}; +} +static Lines pat_unique_river(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& a = v[i - 2]; + const PC& d = v[i - 1]; + const PC& c = v[i]; + if (!a.bull && long_body(v, i - 2) && small_body(v, i - 1) && + d.lower > d.body * 2 && c.bull && c.c > d.h) + m[i] = 1; + } + return {m}; +} +static Lines pat_stick_sandwich(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& a = v[i - 2]; + const PC& d = v[i - 1]; + const PC& c = v[i]; + if (!a.bull && d.bull && !c.bull && + std::fabs(c.c - a.c) <= a.range * 0.02) + m[i] = 1; + } + return {m}; +} +// Extra patterns (beyond TA-Lib's 61). +static Lines pat_inside_bar(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) + if (v[i].h < v[i - 1].h && v[i].l > v[i - 1].l) + m[i] = v[i].bull ? 1 : -1; + return {m}; +} +static Lines pat_outside_bar(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) + if (v[i].h > v[i - 1].h && v[i].l < v[i - 1].l) + m[i] = v[i].bull ? 1 : -1; + return {m}; +} +static Lines pat_pin_bar(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 0; i < v.size(); ++i) { + if (v[i].range <= 0) continue; + if (v[i].lower > v[i].range * 0.6) m[i] = 1; + if (v[i].upper > v[i].range * 0.6) m[i] = -1; + } + return {m}; +} +static Lines pat_nr7(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 6; i < v.size(); ++i) { + bool narrowest = true; + for (int j = 1; j < 7; ++j) + if (v[i - j].range < v[i].range) narrowest = false; + if (narrowest) m[i] = 1; + } + return {m}; +} +static Lines pat_gap(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) { + if (v[i].l > v[i - 1].h) m[i] = 1; + if (v[i].h < v[i - 1].l) m[i] = -1; + } + return {m}; +} + +static void defs_patterns(std::vector& v) { + auto M = PlotKind::Markers; + auto L = Style::Lines; + v.push_back({"Doji", M, L, 0, "", pat_doji}); + v.push_back({"Dragonfly Doji", M, L, 1, "", pat_dragonfly}); + v.push_back({"Gravestone Doji", M, L, 2, "", pat_gravestone}); + v.push_back({"Long Legged Doji", M, L, 3, "", pat_longlegged}); + v.push_back({"Rickshaw Man", M, L, 4, "", pat_rickshaw}); + v.push_back({"High Wave", M, L, 5, "", pat_highwave}); + v.push_back({"Spinning Top", M, L, 6, "", pat_spinningtop}); + v.push_back({"Hammer", M, L, 7, "", pat_hammer}); + v.push_back({"Takuri", M, L, 8, "", pat_takuri}); + v.push_back({"Inverted Hammer", M, L, 9, "", pat_inv_hammer}); + v.push_back({"Shooting Star", M, L, 10, "", pat_shooting_star}); + v.push_back({"Hanging Man", M, L, 11, "", pat_hanging_man}); + v.push_back({"Engulfing", M, L, 12, "", pat_engulfing}); + v.push_back({"Harami", M, L, 13, "", pat_harami}); + v.push_back({"Harami Cross", M, L, 0, "", pat_harami_cross}); + v.push_back({"Homing Pigeon", M, L, 1, "", pat_homing_pigeon}); + v.push_back({"Piercing Line", M, L, 2, "", pat_piercing}); + v.push_back({"Dark Cloud Cover", M, L, 3, "", pat_dark_cloud}); + v.push_back({"Thrusting", M, L, 4, "", pat_thrusting}); + v.push_back({"In-Neck", M, L, 5, "", pat_in_neck}); + v.push_back({"On-Neck", M, L, 6, "", pat_on_neck}); + v.push_back({"Counterattack", M, L, 7, "", pat_counterattack}); + v.push_back({"Separating Lines", M, L, 8, "", pat_separating}); + v.push_back({"Matching Low", M, L, 9, "", pat_matching_low}); + v.push_back({"Belt Hold", M, L, 10, "", pat_belt_hold}); + v.push_back({"Marubozu", M, L, 11, "", pat_marubozu}); + v.push_back({"Closing Marubozu", M, L, 12, "", pat_closing_marubozu}); + v.push_back({"Long Line", M, L, 13, "", pat_long_line}); + v.push_back({"Short Line", M, L, 0, "", pat_short_line}); + v.push_back({"Doji Star", M, L, 1, "", pat_doji_star}); + v.push_back({"Morning Star", M, L, 2, "", pat_morning_star_w}); + v.push_back({"Morning Doji Star", M, L, 3, "", pat_morning_doji_star_w}); + v.push_back({"Evening Star", M, L, 4, "", pat_evening_star_w}); + v.push_back({"Evening Doji Star", M, L, 5, "", pat_evening_doji_star_w}); + v.push_back({"Abandoned Baby", M, L, 6, "", pat_abandoned_baby}); + v.push_back({"Three White Soldiers", M, L, 7, "", pat_soldiers}); + v.push_back({"Three Black Crows", M, L, 8, "", pat_crows}); + v.push_back({"Identical Three Crows", M, L, 9, "", pat_identical_crows}); + v.push_back({"Two Crows", M, L, 10, "", pat_two_crows}); + v.push_back({"Upside Gap Two Crows", M, L, 11, "", pat_upside_gap_crows}); + v.push_back({"Advance Block", M, L, 12, "", pat_advance_block}); + v.push_back({"Stalled Pattern", M, L, 13, "", pat_stalled}); + v.push_back({"Three Inside", M, L, 0, "", pat_three_inside}); + v.push_back({"Three Outside", M, L, 1, "", pat_three_outside}); + v.push_back({"Three Line Strike", M, L, 2, "", pat_line_strike}); + v.push_back({"Three Stars In The South", M, L, 3, "", pat_stars_south}); + v.push_back({"Concealing Baby Swallow", M, L, 4, "", pat_concealing_swallow}); + v.push_back({"Ladder Bottom", M, L, 5, "", pat_ladder_bottom}); + v.push_back({"Breakaway", M, L, 6, "", pat_breakaway}); + v.push_back({"Mat Hold", M, L, 7, "", pat_mat_hold}); + v.push_back({"Rising/Falling Three Methods", M, L, 8, "", pat_risefall_methods}); + v.push_back({"Kicking", M, L, 9, "", pat_kicking}); + v.push_back({"Kicking By Length", M, L, 10, "", pat_kicking_length}); + v.push_back({"Tasuki Gap", M, L, 11, "", pat_tasuki_gap}); + v.push_back({"Gap Side Side White", M, L, 12, "", pat_gap_side_white}); + v.push_back({"X Side Gap Three Methods", M, L, 13, "", pat_xside_gap_methods}); + v.push_back({"Hikkake", M, L, 0, "", pat_hikkake}); + v.push_back({"Hikkake Modified", M, L, 1, "", pat_hikkake_mod}); + v.push_back({"Tristar", M, L, 2, "", pat_tristar}); + v.push_back({"Unique Three River", M, L, 3, "", pat_unique_river}); + v.push_back({"Stick Sandwich", M, L, 4, "", pat_stick_sandwich}); + v.push_back({"Inside Bar", M, L, 5, "", pat_inside_bar}); + v.push_back({"Outside Bar", M, L, 6, "", pat_outside_bar}); + v.push_back({"Pin Bar", M, L, 7, "", pat_pin_bar}); + v.push_back({"NR7", M, L, 8, "", pat_nr7}); + v.push_back({"Gap", M, L, 9, "", pat_gap}); +} + +// ============================================================ final entries + registry + +static Lines f_vhf(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 28); + Series hh = rolling_max(closes(b), n), ll = rolling_min(closes(b), n); + Series d = diff_s(closes(b)); + for (double& v : d) + if (fin(v)) v = std::fabs(v); + Series sum = rolling_sum(d, n); + Series out(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (fin(hh[i]) && fin(sum[i]) && sum[i] > 0) + out[i] = (hh[i] - ll[i]) / sum[i]; + return {out}; +} +static Lines f_disparity(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 14); + Series c = closes(b), m = sma_s(c, n); + Series out(c.size(), nanv()); + for (size_t i = 0; i < c.size(); ++i) + if (fin(m[i]) && m[i] != 0) out[i] = (c[i] - m[i]) / m[i] * 100; + return {out}; +} +static Lines f_atr_trail(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 14); + double k = pd(p, 1, 3.0); + Series atr = atr_s(b, n), c = closes(b), out(c.size(), nanv()); + for (size_t i = 0; i < c.size(); ++i) { + if (!fin(atr[i])) continue; + double stop = c[i] - k * atr[i]; + out[i] = fin(out[i - 1 < c.size() ? i - 1 : 0]) && i > 0 + ? std::max(stop, out[i - 1]) + : stop; + if (c[i] < out[i]) out[i] = stop; // reset on close-through + } + return {out}; +} +static Lines f_pfe(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 14); + Series c = closes(b), out(c.size(), nanv()); + for (size_t i = (size_t)n; i < c.size(); ++i) { + double dist = 0; + for (int j = 0; j < n; ++j) { + double d = c[i - j] - c[i - j - 1]; + dist += std::sqrt(1 + d * d); + } + double direct = std::sqrt(std::pow(c[i] - c[i - n], 2) + n * n); + if (dist > 0) + out[i] = direct / dist * 100 * (c[i] >= c[i - n] ? 1 : -1); + } + return {out}; +} +static Lines f_efficiency(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 10); + Series c = closes(b), d = diff_s(c); + for (double& v : d) + if (fin(v)) v = std::fabs(v); + Series vol = rolling_sum(d, n), out(c.size(), nanv()); + for (size_t i = (size_t)n; i < c.size(); ++i) + if (fin(vol[i]) && vol[i] > 0) + out[i] = std::fabs(c[i] - c[i - n]) / vol[i] * 100; + return {out}; +} +static Lines f_tii(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 30); + Series c = closes(b), m = sma_s(c, n); + Series dev(c.size(), nanv()); + for (size_t i = 0; i < c.size(); ++i) + if (fin(m[i])) dev[i] = c[i] - m[i]; + Series out(c.size(), nanv()); + for (size_t i = (size_t)(n - 1); i < c.size(); ++i) { + double pos = 0, neg = 0; + bool ok = true; + for (int j = 0; j < n; ++j) { + if (!fin(dev[i - j])) ok = false; + if (dev[i - j] > 0) pos += dev[i - j]; + else neg -= dev[i - j]; + } + if (ok && pos + neg > 0) out[i] = pos / (pos + neg) * 100; + } + return {out}; +} +static Lines f_rolling_sum(const std::vector& b, + const std::vector& p) { + return {rolling_sum(closes(b), pi(p, 0, 14))}; +} +static Lines f_highest_high(const std::vector& b, + const std::vector& p) { + return {rolling_max(highs(b), pi(p, 0, 20))}; +} +static Lines f_lowest_low(const std::vector& b, + const std::vector& p) { + return {rolling_min(lows(b), pi(p, 0, 20))}; +} + +static void defs_final(std::vector& v) { + auto O = PlotKind::Overlay, P = PlotKind::Pane; + auto L = Style::Lines; + v.push_back({"VHF", P, L, 0, "28", f_vhf}); + v.push_back({"Disparity Index", P, L, 1, "14", f_disparity}); + v.push_back({"ATR Trailing Stop", O, L, 3, "14,3", f_atr_trail}); + v.push_back({"Polarized Fractal Efficiency", P, L, 4, "14", f_pfe}); + v.push_back({"Efficiency Ratio", P, L, 5, "10", f_efficiency}); + v.push_back({"Trend Intensity Index", P, L, 6, "30", f_tii}); + v.push_back({"Rolling Sum", P, L, 7, "14", f_rolling_sum}); + v.push_back({"Highest High", O, L, 8, "20", f_highest_high}); + v.push_back({"Lowest Low", O, L, 9, "20", f_lowest_low}); +} + +// ------------------------------------------------------------- registry + +inline const std::vector& all_defs() { + static const std::vector defs = [] { + std::vector v; + defs_averages(v); + defs_bands(v); + defs_momentum(v); + defs_volume(v); + defs_volatility(v); + defs_patterns(v); + defs_final(v); + return v; + }(); + return defs; +} + +static std::string lower_str(std::string s) { + for (char& ch : s) ch = (char)std::tolower((unsigned char)ch); + return s; +} +static std::string trim_str(const std::string& s) { + size_t b = s.find_first_not_of(" \t"); + size_t e = s.find_last_not_of(" \t"); + return b == std::string::npos ? "" : s.substr(b, e - b + 1); +} + +inline const Def* find_def(const std::string& name) { + std::string want = lower_str(trim_str(name)); + for (const Def& d : all_defs()) + if (lower_str(d.name) == want) return &d; + return nullptr; +} + +inline std::vector indicator_names() { + std::vector out; + for (const Def& d : all_defs()) out.push_back(d.name); + return out; +} + +static std::vector parse_params(const std::string& s) { + std::vector out; + size_t pos = 0; + while (pos <= s.size()) { + size_t comma = s.find(',', pos); + std::string tok = + trim_str(s.substr(pos, comma == std::string::npos + ? std::string::npos + : comma - pos)); + if (!tok.empty()) out.push_back(std::atof(tok.c_str())); + if (comma == std::string::npos) break; + pos = comma + 1; + } + return out; +} + +inline std::unique_ptr make_indicator(const std::string& spec, + std::string* err) { + std::string name = trim_str(spec); + std::string param_str; + size_t open = spec.find('('); + if (open != std::string::npos) { + size_t close = spec.rfind(')'); + name = trim_str(spec.substr(0, open)); + if (close != std::string::npos && close > open) + param_str = spec.substr(open + 1, close - open - 1); + } + const Def* def = find_def(name); + if (!def) { + if (err) *err = "unknown indicator '" + name + "'"; + return nullptr; + } + std::vector params = parse_params(def->defaults); + std::vector given = parse_params(param_str); + for (size_t i = 0; i < given.size(); ++i) { + if (i < params.size()) + params[i] = given[i]; + else + params.push_back(given[i]); + } + return std::unique_ptr(new Indicator(def, params)); +} + +} // namespace fdi diff --git a/src/indicators_embedded.h b/src/indicators_embedded.h new file mode 100644 index 0000000..db6d466 --- /dev/null +++ b/src/indicators_embedded.h @@ -0,0 +1,3890 @@ +// Generated from src/indicators.h by tools/embed_indicators.sh. +// Do not edit; run 'make' to regenerate. +#pragma once +static const char kIndicatorsHeader[] = R"FDINDICATORS7f3a( +// indicators.h — 250 technical indicator submodules for the FDesigner +// StockChart widget, in ONE self-contained header. +// +// Every indicator is a named module in the registry below: it consumes +// Massive-format OHLCV bars (the same data StockChart loads from CSV), +// computes one or more series, and plots itself onto the chart's canvas — +// as a price-pane overlay, in its own oscillator pane, or as markers. +// Indicators have no palette button of their own; a StockChart enables +// them by name, e.g. add_indicator("SMA(20)") or the designer's +// "indicators" property: "SMA(20), EMA(50), RSI, Bollinger Bands". +// +// Shared math primitives (rolling SMA/EMA/stdev/ATR...) live at the top; +// each indicator's own formula is its own function further down. + +#pragma once + +#include + +#include +#include +#include +#include +#include +#include + +namespace fdi { + +// One OHLCV bar (Massive aggregates: open/high/low/close/volume, +// window_start in ns). +struct Bar { + double open = 0, high = 0, low = 0, close = 0, volume = 0; + long long time = 0; +}; + +using Series = std::vector; +using Lines = std::vector; + +inline double nanv() { return std::numeric_limits::quiet_NaN(); } +inline bool fin(double v) { return std::isfinite(v); } + +// ------------------------------------------------------------- fields + +inline Series field(const std::vector& b, double Bar::* member) { + Series s(b.size()); + for (size_t i = 0; i < b.size(); ++i) s[i] = b[i].*member; + return s; +} +inline Series closes(const std::vector& b) { + Series s(b.size()); + for (size_t i = 0; i < b.size(); ++i) s[i] = b[i].close; + return s; +} +inline Series highs(const std::vector& b) { + Series s(b.size()); + for (size_t i = 0; i < b.size(); ++i) s[i] = b[i].high; + return s; +} +inline Series lows(const std::vector& b) { + Series s(b.size()); + for (size_t i = 0; i < b.size(); ++i) s[i] = b[i].low; + return s; +} +inline Series opens(const std::vector& b) { + Series s(b.size()); + for (size_t i = 0; i < b.size(); ++i) s[i] = b[i].open; + return s; +} +inline Series vols(const std::vector& b) { + Series s(b.size()); + for (size_t i = 0; i < b.size(); ++i) s[i] = b[i].volume; + return s; +} +inline Series hl2(const std::vector& b) { + Series s(b.size()); + for (size_t i = 0; i < b.size(); ++i) s[i] = (b[i].high + b[i].low) / 2; + return s; +} +inline Series hlc3(const std::vector& b) { + Series s(b.size()); + for (size_t i = 0; i < b.size(); ++i) + s[i] = (b[i].high + b[i].low + b[i].close) / 3; + return s; +} +inline Series ohlc4(const std::vector& b) { + Series s(b.size()); + for (size_t i = 0; i < b.size(); ++i) + s[i] = (b[i].open + b[i].high + b[i].low + b[i].close) / 4; + return s; +} +inline Series true_range(const std::vector& b) { + Series s(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) { + double tr = b[i].high - b[i].low; + if (i > 0) { + tr = std::max(tr, std::fabs(b[i].high - b[i - 1].close)); + tr = std::max(tr, std::fabs(b[i].low - b[i - 1].close)); + } + s[i] = tr; + } + return s; +} + +// ------------------------------------------------------------- rolling + +inline Series sma_s(const Series& in, int n) { + Series out(in.size(), nanv()); + if (n < 1) return out; + double sum = 0; + int cnt = 0; + for (size_t i = 0; i < in.size(); ++i) { + if (fin(in[i])) { + sum += in[i]; + ++cnt; + } + if (i >= (size_t)n && fin(in[i - n])) { + sum -= in[i - n]; + --cnt; + } + if (cnt == n) out[i] = sum / n; + } + return out; +} + +inline Series ema_s(const Series& in, int n) { + Series out(in.size(), nanv()); + if (n < 1) return out; + double k = 2.0 / (n + 1), e = 0; + bool started = false; + for (size_t i = 0; i < in.size(); ++i) { + if (!fin(in[i])) continue; + if (!started) { + e = in[i]; + started = true; + } else { + e = in[i] * k + e * (1 - k); + } + if (i >= (size_t)(n - 1)) out[i] = e; + } + return out; +} + +// Wilder's smoothing (SMMA / RMA). +inline Series smma_s(const Series& in, int n) { + Series out(in.size(), nanv()); + if (n < 1) return out; + double e = 0; + bool started = false; + for (size_t i = 0; i < in.size(); ++i) { + if (!fin(in[i])) continue; + if (!started) { + e = in[i]; + started = true; + } else { + e = (e * (n - 1) + in[i]) / n; + } + if (i >= (size_t)(n - 1)) out[i] = e; + } + return out; +} + +inline Series wma_s(const Series& in, int n) { + Series out(in.size(), nanv()); + if (n < 1) return out; + double denom = n * (n + 1) / 2.0; + for (size_t i = (size_t)(n - 1); i < in.size(); ++i) { + double sum = 0; + bool ok = true; + for (int j = 0; j < n; ++j) { + double v = in[i - j]; + if (!fin(v)) { + ok = false; + break; + } + sum += v * (n - j); + } + if (ok) out[i] = sum / denom; + } + return out; +} + +inline Series rolling_max(const Series& in, int n) { + Series out(in.size(), nanv()); + for (size_t i = 0; i < in.size(); ++i) { + if (i + 1 < (size_t)n) continue; + double m = -1e300; + bool ok = true; + for (int j = 0; j < n; ++j) { + if (!fin(in[i - j])) ok = false; + m = std::max(m, in[i - j]); + } + if (ok) out[i] = m; + } + return out; +} + +inline Series rolling_min(const Series& in, int n) { + Series out(in.size(), nanv()); + for (size_t i = 0; i < in.size(); ++i) { + if (i + 1 < (size_t)n) continue; + double m = 1e300; + bool ok = true; + for (int j = 0; j < n; ++j) { + if (!fin(in[i - j])) ok = false; + m = std::min(m, in[i - j]); + } + if (ok) out[i] = m; + } + return out; +} + +inline Series rolling_sum(const Series& in, int n) { + Series out(in.size(), nanv()); + double sum = 0; + int cnt = 0; + for (size_t i = 0; i < in.size(); ++i) { + if (fin(in[i])) { + sum += in[i]; + ++cnt; + } + if (i >= (size_t)n && fin(in[i - n])) { + sum -= in[i - n]; + --cnt; + } + if (cnt == n) out[i] = sum; + } + return out; +} + +inline Series stdev_s(const Series& in, int n, bool sample = false) { + Series out(in.size(), nanv()); + Series mean = sma_s(in, n); + for (size_t i = 0; i < in.size(); ++i) { + if (!fin(mean[i])) continue; + double ss = 0; + for (int j = 0; j < n; ++j) { + double d = in[i - j] - mean[i]; + ss += d * d; + } + out[i] = std::sqrt(ss / (sample ? n - 1 : n)); + } + return out; +} + +inline Series atr_s(const std::vector& b, int n) { + return smma_s(true_range(b), n); +} + +// RSI on a price series (Wilder). +inline Series rsi_s(const Series& in, int n) { + Series out(in.size(), nanv()); + if (n < 1 || in.size() < 2) return out; + double gain = 0, loss = 0; + for (size_t i = 1; i < in.size(); ++i) { + double d = in[i] - in[i - 1]; + double g = d > 0 ? d : 0, l = d < 0 ? -d : 0; + if (i == 1) { + gain = g; + loss = l; + } else { + gain = (gain * (n - 1) + g) / n; + loss = (loss * (n - 1) + l) / n; + } + if (i >= (size_t)n) { + if (loss == 0) + out[i] = 100; + else { + double rs = gain / loss; + out[i] = 100 - 100 / (1 + rs); + } + } + } + return out; +} + +// Element-wise helpers. +inline Series combine(const Series& a, const Series& b, char op) { + Series out(a.size(), nanv()); + for (size_t i = 0; i < a.size() && i < b.size(); ++i) { + if (!fin(a[i]) || !fin(b[i])) continue; + switch (op) { + case '+': out[i] = a[i] + b[i]; break; + case '-': out[i] = a[i] - b[i]; break; + case '*': out[i] = a[i] * b[i]; break; + case '/': out[i] = b[i] != 0 ? a[i] / b[i] : nanv(); break; + } + } + return out; +} +inline Series scale_s(const Series& a, double k) { + Series out(a.size(), nanv()); + for (size_t i = 0; i < a.size(); ++i) + if (fin(a[i])) out[i] = a[i] * k; + return out; +} +inline Series shift_s(const Series& a, int n) { // delay by n bars + Series out(a.size(), nanv()); + for (size_t i = 0; i < a.size(); ++i) { + long src = (long)i - n; + if (src >= 0 && src < (long)a.size() && fin(a[src])) + out[i] = a[src]; + } + return out; +} +inline Series diff_s(const Series& a, int n = 1) { + Series out(a.size(), nanv()); + for (size_t i = 0; i < a.size(); ++i) + if ((int)i - n >= 0 && fin(a[i]) && fin(a[i - n])) + out[i] = a[i] - a[i - n]; + return out; +} + +// ------------------------------------------------------------- framework + +enum class PlotKind { Overlay, Pane, Markers }; +enum class Style { Lines, Bars }; + +using ComputeFn = + std::function&, const std::vector&)>; + +struct Def { + const char* name; + PlotKind kind; + Style style; + int color; // palette index (line j uses color + j) + const char* defaults; // comma-separated default parameters + ComputeFn fn; +}; + +// Area an indicator draws into. For Overlay/Markers, lo/hi are the price +// scale; for Pane the indicator scales its own lines into the area. +struct PlotArea { + cairo_t* cr = nullptr; + double x = 0, y = 0, w = 0, h = 0; + size_t begin = 0, count = 0; + double lo = 0, hi = 1; +}; + +inline const double kPalette[][3] = { + {0.13, 0.35, 0.85}, // blue + {0.95, 0.55, 0.10}, // orange + {0.55, 0.25, 0.80}, // purple + {0.00, 0.60, 0.60}, // teal + {0.85, 0.20, 0.55}, // magenta + {0.55, 0.35, 0.15}, // brown + {0.20, 0.65, 0.25}, // green + {0.80, 0.15, 0.15}, // red + {0.10, 0.55, 0.80}, // steel blue + {0.65, 0.65, 0.10}, // olive + {0.35, 0.35, 0.35}, // grey + {0.90, 0.70, 0.10}, // gold + {0.25, 0.25, 0.70}, // indigo + {0.60, 0.20, 0.20}, // brick +}; +inline constexpr int kPaletteSize = 14; + +class Indicator { +public: + Indicator(const Def* def, std::vector params) + : def_(def), params_(std::move(params)) {} + + const char* name() const { return def_->name; } + PlotKind kind() const { return def_->kind; } + Style style() const { return def_->style; } + int color() const { return def_->color; } + const std::vector& params() const { return params_; } + + Lines compute(const std::vector& bars) const { + return def_->fn(bars, params_); + } + + // Draw this indicator into `area` (which covers bars + // [begin, begin+count) of `bars`). + void draw(const PlotArea& area, const std::vector& bars) const { + if (!area.cr || area.count == 0 || area.w < 4 || area.h < 4) return; + Lines lines = compute(bars); + if (lines.empty()) return; + cairo_t* cr = area.cr; + size_t end = std::min(bars.size(), area.begin + area.count); + auto X = [&](size_t i) { + return area.x + (i - area.begin + 0.5) * (area.w / area.count); + }; + if (def_->kind == PlotKind::Markers) { + const Series& m = lines[0]; + const double* col = kPalette[def_->color % kPaletteSize]; + double slot = area.w / area.count; + double hw = std::max(3.0, slot * 0.35); // visible at any zoom + for (size_t i = area.begin; i < end; ++i) { + if (i >= m.size() || !fin(m[i]) || m[i] == 0) continue; + double cx = X(i); + bool bull = m[i] > 0; + double py = bull ? area.y + area.h - + (bars[i].low - area.lo) / + (area.hi - area.lo) * area.h + + 8 + : area.y + + (area.hi - bars[i].high) / + (area.hi - area.lo) * area.h - + 8; + py = std::min(std::max(py, area.y + 8), + area.y + area.h - 8); + cairo_set_source_rgba(cr, col[0], col[1], col[2], 0.9); + cairo_move_to(cr, cx, py); + cairo_line_to(cr, cx - hw, py + (bull ? 7 : -7)); + cairo_line_to(cr, cx + hw, py + (bull ? 7 : -7)); + cairo_close_path(cr); + cairo_fill(cr); + } + return; + } + // Scale: overlays use the price scale; panes scale their own. + double lo = area.lo, hi = area.hi; + if (def_->kind == PlotKind::Pane) { + lo = 1e300; + hi = -1e300; + for (const Series& s : lines) + for (size_t i = area.begin; i < end && i < s.size(); ++i) + if (fin(s[i])) { + lo = std::min(lo, s[i]); + hi = std::max(hi, s[i]); + } + if (lo > hi) return; // nothing finite to draw + if (def_->style == Style::Bars) { + lo = std::min(lo, 0.0); + hi = std::max(hi, 0.0); + } + if (hi - lo < 1e-12) hi = lo + 1; + } + auto Y = [&](double v) { + return area.y + (hi - v) / (hi - lo) * area.h; + }; + for (size_t j = 0; j < lines.size(); ++j) { + const Series& s = lines[j]; + const double* col = + kPalette[(def_->color + (int)j) % kPaletteSize]; + cairo_set_source_rgba(cr, col[0], col[1], col[2], 1.0); + if (def_->style == Style::Bars) { + double bw = std::max(1.0, area.w / area.count * 0.7); + double y0 = Y(0); + for (size_t i = area.begin; i < end && i < s.size(); ++i) { + if (!fin(s[i])) continue; + double y1 = Y(s[i]); + cairo_rectangle(cr, X(i) - bw / 2, std::min(y0, y1), + bw, std::max(1.0, std::fabs(y1 - y0))); + cairo_fill(cr); + } + } else { + cairo_set_line_width(cr, 1.2); + bool pen = false; + for (size_t i = area.begin; i < end && i < s.size(); ++i) { + if (!fin(s[i])) { + pen = false; + continue; + } + if (!pen) { + cairo_move_to(cr, X(i), Y(s[i])); + pen = true; + } else { + cairo_line_to(cr, X(i), Y(s[i])); + } + } + cairo_stroke(cr); + } + } + } + +private: + const Def* def_; + std::vector params_; +}; + +// Registry (defined by the family tables at the bottom of this file). +inline const std::vector& all_defs(); +inline const Def* find_def(const std::string& name); +inline std::vector indicator_names(); +inline std::unique_ptr make_indicator( + const std::string& spec, std::string* err = nullptr); + +// ============================================================ averages +// Moving averages and price filters (price-pane overlays). + +static int pi(const std::vector& p, size_t i, int dflt) { + return i < p.size() && p[i] > 0 ? (int)p[i] : dflt; +} +static double pd(const std::vector& p, size_t i, double dflt) { + return i < p.size() ? p[i] : dflt; +} + +static Lines f_sma(const std::vector& b, const std::vector& p) { + return {sma_s(closes(b), pi(p, 0, 20))}; +} +static Lines f_ema(const std::vector& b, const std::vector& p) { + return {ema_s(closes(b), pi(p, 0, 20))}; +} +static Lines f_wma(const std::vector& b, const std::vector& p) { + return {wma_s(closes(b), pi(p, 0, 20))}; +} +static Lines f_smma(const std::vector& b, const std::vector& p) { + return {smma_s(closes(b), pi(p, 0, 20))}; +} +static Lines f_dema(const std::vector& b, const std::vector& p) { + Series e1 = ema_s(closes(b), pi(p, 0, 20)); + Series e2 = ema_s(e1, pi(p, 0, 20)); + return {combine(scale_s(e1, 2), e2, '-')}; +} +static Lines f_tema(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 20); + Series e1 = ema_s(closes(b), n), e2 = ema_s(e1, n), e3 = ema_s(e2, n); + return {combine(combine(scale_s(e1, 3), scale_s(e2, 3), '-'), e3, '+')}; +} +static Lines f_trima(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 20); + return {sma_s(sma_s(closes(b), (n + 1) / 2), n / 2 + 1)}; +} +static Lines f_kama(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 10); + Series c = closes(b), out(c.size(), nanv()); + Series ad = diff_s(c); + for (size_t i = 0; i < ad.size(); ++i) + if (fin(ad[i])) ad[i] = std::fabs(ad[i]); + Series vol = rolling_sum(ad, n); + double fast = 2.0 / 3, slow = 2.0 / 31; + for (size_t i = (size_t)n; i < c.size(); ++i) { + if (!fin(vol[i]) || vol[i] == 0) continue; + double er = std::fabs(c[i] - c[i - n]) / vol[i]; + double sc = er * (fast - slow) + slow; + sc *= sc; + out[i] = fin(out[i - 1]) ? out[i - 1] + sc * (c[i] - out[i - 1]) + : c[i]; + } + return {out}; +} +static Lines f_t3(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 10); + double a = pd(p, 1, 0.7); + Series e = closes(b); + for (int i = 0; i < 6; ++i) e = ema_s(e, n); + Series e3 = closes(b); + for (int i = 0; i < 3; ++i) e3 = ema_s(e3, n); + Series e4 = ema_s(e3, n), e5 = ema_s(e4, n), e6 = ema_s(e5, n); + double c1 = -a * a * a, c2 = 3 * a * a + 3 * a * a * a, + c3 = -6 * a * a - 3 * a - 3 * a * a * a, + c4 = 1 + 3 * a + a * a * a + 3 * a * a; + (void)e; + return {combine( + combine(scale_s(e6, c1), scale_s(e5, c2), '+'), + combine(scale_s(e4, c3), scale_s(e3, c4), '+'), '+')}; +} +static Lines f_hma(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 20); + Series diff = combine(scale_s(wma_s(closes(b), n / 2), 2), + wma_s(closes(b), n), '-'); + return {wma_s(diff, (int)std::sqrt(n))}; +} +static Lines f_zlema(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 20), lag = (n - 1) / 2; + Series adj = combine(scale_s(closes(b), 2), shift_s(closes(b), lag), '-'); + return {ema_s(adj, n)}; +} +static Lines f_vwma(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 20); + Series cv(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) cv[i] = b[i].close * b[i].volume; + return {combine(rolling_sum(cv, n), rolling_sum(vols(b), n), '/')}; +} +static Lines f_lsma(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 25); + Series c = closes(b), out(c.size(), nanv()); + for (size_t i = (size_t)(n - 1); i < c.size(); ++i) { + double sx = 0, sy = 0, sxx = 0, sxy = 0; + bool ok = true; + for (int j = 0; j < n; ++j) { + double x = j, y = c[i - n + 1 + j]; + if (!fin(y)) ok = false; + sx += x; + sy += y; + sxx += x * x; + sxy += x * y; + } + if (!ok) continue; + double den = n * sxx - sx * sx; + if (den == 0) continue; + double slope = (n * sxy - sx * sy) / den; + double inter = (sy - slope * sx) / n; + out[i] = inter + slope * (n - 1); + } + return {out}; +} +static Lines f_alma(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 9); + double off = pd(p, 1, 0.85), sig = pd(p, 2, 6.0); + Series c = closes(b), out(c.size(), nanv()); + double m = std::floor(off * (n - 1)), s = n / sig, wsum = 0; + std::vector w(n); + for (int j = 0; j < n; ++j) { + w[j] = std::exp(-((j - m) * (j - m)) / (2 * s * s)); + wsum += w[j]; + } + for (size_t i = (size_t)(n - 1); i < c.size(); ++i) { + double sum = 0; + for (int j = 0; j < n; ++j) sum += w[j] * c[i - n + 1 + j]; + out[i] = sum / wsum; + } + return {out}; +} +static Lines f_mcginley(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 14); + Series c = closes(b), out(c.size(), nanv()); + for (size_t i = 0; i < c.size(); ++i) { + if (i == 0 || !fin(out[i - 1])) { + out[i] = c[i]; + continue; + } + double ratio = c[i] / out[i - 1]; + double denom = n * std::pow(ratio, 4); + out[i] = out[i - 1] + (denom > 0 ? (c[i] - out[i - 1]) / denom : 0); + } + return {out}; +} +static Lines f_gma(const std::vector& b, const std::vector& p) { + Series lg = closes(b); + for (double& v : lg) v = v > 0 ? std::log(v) : nanv(); + Series m = sma_s(lg, pi(p, 0, 20)); + for (double& v : m) + if (fin(v)) v = std::exp(v); + return {m}; +} +static Lines f_median_price(const std::vector& b, + const std::vector&) { + return {hl2(b)}; +} +static Lines f_typical_price(const std::vector& b, + const std::vector&) { + return {hlc3(b)}; +} +static Lines f_weighted_close(const std::vector& b, + const std::vector&) { + Series s(b.size()); + for (size_t i = 0; i < b.size(); ++i) + s[i] = (b[i].high + b[i].low + 2 * b[i].close) / 4; + return {s}; +} +static Lines f_midprice(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 14); + return {scale_s(combine(rolling_max(highs(b), n), + rolling_min(lows(b), n), '+'), + 0.5)}; +} +static Lines f_midpoint(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 14); + return {scale_s(combine(rolling_max(closes(b), n), + rolling_min(closes(b), n), '+'), + 0.5)}; +} +static Lines f_vwap(const std::vector& b, const std::vector&) { + Series tp = hlc3(b), out(b.size(), nanv()); + double pv = 0, vv = 0; + for (size_t i = 0; i < b.size(); ++i) { + pv += tp[i] * b[i].volume; + vv += b[i].volume; + if (vv > 0) out[i] = pv / vv; + } + return {out}; +} +static Lines f_heikin_ashi(const std::vector& b, + const std::vector&) { + Series out(b.size(), nanv()); + double hao = 0; + for (size_t i = 0; i < b.size(); ++i) { + double hac = (b[i].open + b[i].high + b[i].low + b[i].close) / 4; + hao = i == 0 ? (b[i].open + b[i].close) / 2 + : (hao + out[i - 1]) / 2; + (void)hao; + out[i] = hac; + } + return {out}; +} +// Ehlers two-pole super smoother (price filter). +static Series super_smoother_s(const Series& in, int n) { + Series out(in.size(), nanv()); + double a = std::exp(-1.414 * M_PI / n); + double b = 2 * a * std::cos(1.414 * M_PI / n); + double c2 = b, c3 = -a * a, c1 = 1 - c2 - c3; + for (size_t i = 0; i < in.size(); ++i) { + if (!fin(in[i])) continue; + double x1 = i > 0 && fin(in[i - 1]) ? in[i - 1] : in[i]; + double y1 = i > 0 && fin(out[i - 1]) ? out[i - 1] : in[i]; + double y2 = i > 1 && fin(out[i - 2]) ? out[i - 2] : y1; + out[i] = c1 * (in[i] + x1) / 2 + c2 * y1 + c3 * y2; + } + return out; +} +static Lines f_super_smoother(const std::vector& b, + const std::vector& p) { + return {super_smoother_s(closes(b), pi(p, 0, 14))}; +} +static Lines f_decycler(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 30); + Series c = closes(b), hp(c.size(), nanv()), out(c.size(), nanv()); + double w = 0.707 * 2 * M_PI / n; + double alpha = (std::cos(w) + std::sin(w) - 1) / std::cos(w); + for (size_t i = 0; i < c.size(); ++i) { + double x1 = i > 0 ? c[i - 1] : c[i], x2 = i > 1 ? c[i - 2] : c[i]; + double h1 = i > 0 && fin(hp[i - 1]) ? hp[i - 1] : 0; + double h2 = i > 1 && fin(hp[i - 2]) ? hp[i - 2] : 0; + double k = 1 - alpha / 2; + hp[i] = k * k * (c[i] - 2 * x1 + x2) + 2 * (1 - alpha) * h1 - + (1 - alpha) * (1 - alpha) * h2; + out[i] = c[i] - hp[i]; + } + return {out}; +} +static Lines f_gaussian(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 14); + Series s = closes(b); + double alpha = 2.0 / (n + 1); + for (int pole = 0; pole < 4; ++pole) { // 4-pole cascade approximates + Series o(s.size(), nanv()); // a gaussian filter + for (size_t i = 0; i < s.size(); ++i) { + if (!fin(s[i])) continue; + double prev = i > 0 && fin(o[i - 1]) ? o[i - 1] : s[i]; + o[i] = prev + alpha * (s[i] - prev); + } + s = o; + } + return {s}; +} +static Lines f_butterworth(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 14); + Series c = closes(b), out(c.size(), nanv()); + double a = std::exp(-1.414 * M_PI / n); + double bb = 2 * a * std::cos(1.414 * M_PI / n), cc = a * a; + for (size_t i = 0; i < c.size(); ++i) { + double x1 = i > 0 ? c[i - 1] : c[i], x2 = i > 1 ? c[i - 2] : c[i]; + double y1 = i > 0 && fin(out[i - 1]) ? out[i - 1] : c[i]; + double y2 = i > 1 && fin(out[i - 2]) ? out[i - 2] : y1; + out[i] = (1 - bb + cc) / 4 * (c[i] + 2 * x1 + x2) + bb * y1 - + cc * y2; + } + return {out}; +} +static Lines f_laguerre(const std::vector& b, + const std::vector& p) { + double g = pd(p, 0, 0.8); + Series c = closes(b), out(c.size(), nanv()); + double L[4] = {0, 0, 0, 0}; + for (size_t i = 0; i < c.size(); ++i) { + double o0 = L[0], o1 = L[1], o2 = L[2], o3 = L[3]; + L[0] = (1 - g) * c[i] + g * o0; + L[1] = -g * L[0] + o0 + g * o1; + L[2] = -g * L[1] + o1 + g * o2; + L[3] = -g * L[2] + o2 + g * o3; + out[i] = (L[0] + 2 * L[1] + 2 * L[2] + L[3]) / 6; + } + return {out}; +} +static Lines f_vidya(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 14); + Series c = closes(b), out(c.size(), nanv()); + Series sd = stdev_s(c, n); + Series sd_lag = shift_s(sd, n); + for (size_t i = 0; i < c.size(); ++i) { + if (!fin(sd[i]) || !fin(sd_lag[i]) || sd_lag[i] == 0) continue; + double vr = sd[i] / sd_lag[i]; + double alpha = vr * 2.0 / (n + 1); + out[i] = fin(out[i - 1]) ? out[i - 1] + alpha * (c[i] - out[i - 1]) + : c[i]; + } + return {out}; +} +static Lines f_frama(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 16); + if (n % 2) ++n; + Series c = closes(b), out(c.size(), nanv()); + Series hh = highs(b), ll = lows(b); + for (size_t i = (size_t)(2 * n - 1); i < c.size(); ++i) { + auto range = [&](size_t from, size_t to) { + double mx = -1e300, mn = 1e300; + for (size_t k = from; k <= to; ++k) { + mx = std::max(mx, hh[k]); + mn = std::min(mn, ll[k]); + } + return mx - mn; + }; + double n1 = range(i - 2 * n + 1, i - n) / n; + double n2 = range(i - n + 1, i) / n; + double n3 = range(i - 2 * n + 1, i) / (2.0 * n); + if (n1 <= 0 || n2 <= 0 || n3 <= 0) continue; + double dim = (std::log(n1 + n2) - std::log(n3)) / std::log(2); + double alpha = std::exp(-4.6 * (dim - 1)); + alpha = std::min(1.0, std::max(0.01, alpha)); + out[i] = fin(out[i - 1]) ? alpha * c[i] + (1 - alpha) * out[i - 1] + : c[i]; + } + return {out}; +} +static Lines f_vma(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 14); + Series c = closes(b), out(c.size(), nanv()); + Series sd = stdev_s(c, n); + for (size_t i = 0; i < c.size(); ++i) { + double sdl = i >= (size_t)n && fin(sd[i - n]) ? sd[i - n] : nanv(); + if (!fin(sd[i]) || !fin(sdl) || sdl == 0) continue; + double vr = sd[i] / sdl; + double alpha = (2.0 / (n + 1)) * vr; + alpha = std::min(1.0, alpha); + out[i] = fin(out[i - 1]) ? out[i - 1] + alpha * (c[i] - out[i - 1]) + : c[i]; + } + return {out}; +} +static Lines f_jma(const std::vector& b, const std::vector& p) { + // Jurik MA (published approximation of the proprietary filter). + int n = pi(p, 0, 14); + double phase = pd(p, 1, 0); + double pr = phase < -100 ? 0.5 : phase > 100 ? 2.5 : phase / 100 + 1.5; + Series c = closes(b), out(c.size(), nanv()); + double beta = 0.45 * (n - 1) / (0.45 * (n - 1) + 2); + double alpha = std::pow(beta, pr); + double e0 = 0, e1 = 0, e2 = 0, jma = 0; + bool started = false; + for (size_t i = 0; i < c.size(); ++i) { + if (!started) { + e0 = jma = c[i]; + started = true; + } + e0 = (1 - alpha) * c[i] + alpha * e0; + e1 = (c[i] - e0) * (1 - beta) + beta * e1; + e2 = (e0 + pr * e1 - jma) * (1 - alpha) * (1 - alpha) + + alpha * alpha * e2; + jma += e2; + out[i] = jma; + } + return {out}; +} + +static void defs_averages(std::vector& v) { + auto O = PlotKind::Overlay; + auto L = Style::Lines; + v.push_back({"SMA", O, L, 0, "20", f_sma}); + v.push_back({"EMA", O, L, 1, "20", f_ema}); + v.push_back({"WMA", O, L, 2, "20", f_wma}); + v.push_back({"SMMA", O, L, 3, "20", f_smma}); + v.push_back({"DEMA", O, L, 4, "20", f_dema}); + v.push_back({"TEMA", O, L, 5, "20", f_tema}); + v.push_back({"TRIMA", O, L, 6, "20", f_trima}); + v.push_back({"KAMA", O, L, 7, "10", f_kama}); + v.push_back({"T3", O, L, 8, "10,0.7", f_t3}); + v.push_back({"HMA", O, L, 9, "20", f_hma}); + v.push_back({"ZLEMA", O, L, 10, "20", f_zlema}); + v.push_back({"VWMA", O, L, 11, "20", f_vwma}); + v.push_back({"LSMA", O, L, 12, "25", f_lsma}); + v.push_back({"ALMA", O, L, 13, "9,0.85,6", f_alma}); + v.push_back({"McGinley Dynamic", O, L, 0, "14", f_mcginley}); + v.push_back({"Geometric MA", O, L, 1, "20", f_gma}); + v.push_back({"Median Price", O, L, 2, "", f_median_price}); + v.push_back({"Typical Price", O, L, 3, "", f_typical_price}); + v.push_back({"Weighted Close", O, L, 4, "", f_weighted_close}); + v.push_back({"Midprice", O, L, 5, "14", f_midprice}); + v.push_back({"Midpoint", O, L, 6, "14", f_midpoint}); + v.push_back({"VWAP", O, L, 7, "", f_vwap}); + v.push_back({"Heikin Ashi Close", O, L, 8, "", f_heikin_ashi}); + v.push_back({"Super Smoother", O, L, 9, "14", f_super_smoother}); + v.push_back({"Decycler", O, L, 10, "30", f_decycler}); + v.push_back({"Gaussian Filter", O, L, 11, "14", f_gaussian}); + v.push_back({"Butterworth Filter", O, L, 12, "14", f_butterworth}); + v.push_back({"Laguerre Filter", O, L, 13, "0.8", f_laguerre}); + v.push_back({"VIDYA", O, L, 0, "14", f_vidya}); + v.push_back({"FRAMA", O, L, 1, "16", f_frama}); + v.push_back({"Variable MA", O, L, 2, "14", f_vma}); + v.push_back({"Jurik MA", O, L, 3, "14,0", f_jma}); +} + +// ============================================================ bands & channels + +static Lines f_bollinger(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 20); + double k = pd(p, 1, 2.0); + Series mid = sma_s(closes(b), n), sd = stdev_s(closes(b), n); + return {combine(mid, scale_s(sd, k), '+'), mid, + combine(mid, scale_s(sd, k), '-')}; +} +static Lines f_bollinger_pb(const std::vector& b, + const std::vector& p) { + Lines bb = f_bollinger(b, p); + Series width = combine(bb[0], bb[2], '-'); + return {combine(combine(closes(b), bb[2], '-'), width, '/')}; +} +static Lines f_bollinger_bw(const std::vector& b, + const std::vector& p) { + Lines bb = f_bollinger(b, p); + return {combine(combine(bb[0], bb[2], '-'), bb[1], '/')}; +} +static Lines f_keltner(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 20); + double k = pd(p, 1, 2.0); + Series mid = ema_s(closes(b), n), atr = atr_s(b, pi(p, 2, 10)); + return {combine(mid, scale_s(atr, k), '+'), mid, + combine(mid, scale_s(atr, k), '-')}; +} +static Lines f_donchian(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 20); + Series up = rolling_max(highs(b), n), dn = rolling_min(lows(b), n); + return {up, scale_s(combine(up, dn, '+'), 0.5), dn}; +} +static Lines f_starc(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 15); + double k = pd(p, 1, 2.0); + Series mid = sma_s(closes(b), n), atr = atr_s(b, pi(p, 2, 15)); + return {combine(mid, scale_s(atr, k), '+'), mid, + combine(mid, scale_s(atr, k), '-')}; +} +static Lines f_envelope(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 20); + double pct = pd(p, 1, 2.5) / 100; + Series mid = sma_s(closes(b), n); + Series up(mid.size(), nanv()), dn(mid.size(), nanv()); + for (size_t i = 0; i < mid.size(); ++i) + if (fin(mid[i])) { + up[i] = mid[i] * (1 + pct); + dn[i] = mid[i] * (1 - pct); + } + return {up, mid, dn}; +} +static Lines f_ichimoku(const std::vector& b, + const std::vector& p) { + int t = pi(p, 0, 9), k = pi(p, 1, 26), s = pi(p, 2, 52); + Series ten = f_midprice(b, {(double)t})[0]; + Series kij = f_midprice(b, {(double)k})[0]; + Series sen_b_raw = f_midprice(b, {(double)s})[0]; + Series sen_a_raw = scale_s(combine(ten, kij, '+'), 0.5); + Series sen_a(b.size(), nanv()), sen_b(b.size(), nanv()), + chikou(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) { + if (i >= (size_t)k) { + if (fin(sen_a_raw[i - k])) sen_a[i] = sen_a_raw[i - k]; + if (fin(sen_b_raw[i - k])) sen_b[i] = sen_b_raw[i - k]; + } + if (i + k < b.size()) chikou[i] = b[i + k].close; + } + return {ten, kij, sen_a, sen_b, chikou}; +} +static Lines f_supertrend(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 10); + double mult = pd(p, 1, 3.0); + Series atr = atr_s(b, n), out(b.size(), nanv()); + Series fup(b.size(), nanv()), flo(b.size(), nanv()); + int dir = 1; + for (size_t i = 0; i < b.size(); ++i) { + if (!fin(atr[i])) continue; + double mid = (b[i].high + b[i].low) / 2; + double bu = mid + mult * atr[i], bl = mid - mult * atr[i]; + if (i == 0 || !fin(fup[i - 1])) { + fup[i] = bu; + flo[i] = bl; + } else { + fup[i] = (bu < fup[i - 1] || b[i - 1].close > fup[i - 1]) + ? bu + : fup[i - 1]; + flo[i] = (bl > flo[i - 1] || b[i - 1].close < flo[i - 1]) + ? bl + : flo[i - 1]; + } + if (fin(out[i - 1 < b.size() ? i - 1 : 0]) && i > 0) { + if (out[i - 1] == fup[i - 1] && b[i].close > fup[i]) + dir = 1; + else if (out[i - 1] == flo[i - 1] && b[i].close < flo[i]) + dir = -1; + } + out[i] = dir > 0 ? flo[i] : fup[i]; + } + return {out}; +} +static Lines f_sar(const std::vector& b, const std::vector& p) { + double step = pd(p, 0, 0.02), mx = pd(p, 1, 0.2); + Series out(b.size(), nanv()); + if (b.size() < 2) return {out}; + bool up = b[1].close >= b[0].close; + double sar = up ? b[0].low : b[0].high; + double ep = up ? b[0].high : b[0].low, af = step; + for (size_t i = 1; i < b.size(); ++i) { + sar = sar + af * (ep - sar); + if (up) { + sar = std::min(sar, std::min(b[i - 1].low, + i > 1 ? b[i - 2].low : b[i - 1].low)); + if (b[i].low < sar) { + up = false; + sar = ep; + ep = b[i].low; + af = step; + } else { + if (b[i].high > ep) { + ep = b[i].high; + af = std::min(mx, af + step); + } + } + } else { + sar = std::max(sar, std::max(b[i - 1].high, + i > 1 ? b[i - 2].high : b[i - 1].high)); + if (b[i].high > sar) { + up = true; + sar = ep; + ep = b[i].high; + af = step; + } else { + if (b[i].low < ep) { + ep = b[i].low; + af = std::min(mx, af + step); + } + } + } + out[i] = sar; + } + return {out}; +} +// Pivot points from the PREVIOUS day (by window_start UTC day). +static Lines f_pivots(const std::vector& b, + const std::vector& p) { + int mode = pi(p, 0, 0); // 0 standard 1 woodie 2 camarilla 3 fib 4 demark + const int NL = 8; + Lines out(NL, Series(b.size(), nanv())); + double H = 0, L = 0, C = 0, O = 0; + bool have_prev = false, in_day = false; + long long day = -1; + double dH = 0, dL = 0, dC = 0, dO = 0; + for (size_t i = 0; i < b.size(); ++i) { + long long d = b[i].time / 86400000000000LL; + if (!in_day || d != day) { + if (in_day) { + H = dH; L = dL; C = dC; O = dO; + have_prev = true; + } + day = d; + in_day = true; + dH = b[i].high; dL = b[i].low; dO = b[i].open; + } + dH = std::max(dH, b[i].high); + dL = std::min(dL, b[i].low); + dC = b[i].close; + if (!have_prev) continue; + double P = 0, R1 = 0, S1 = 0, R2 = 0, S2 = 0, R3 = 0, S3 = 0, + R4 = 0, S4 = 0; + double range = H - L; + switch (mode) { + case 1: // Woodie + P = (H + L + 2 * C) / 4; + R1 = 2 * P - L; S1 = 2 * P - H; + R2 = P + range; S2 = P - range; + R3 = H + 2 * (P - L); S3 = L - 2 * (H - P); + break; + case 2: // Camarilla + P = (H + L + C) / 3; + R1 = C + range * 1.1 / 12; S1 = C - range * 1.1 / 12; + R2 = C + range * 1.1 / 6; S2 = C - range * 1.1 / 6; + R3 = C + range * 1.1 / 4; S3 = C - range * 1.1 / 4; + R4 = C + range * 1.1 / 2; S4 = C - range * 1.1 / 2; + break; + case 3: // Fibonacci + P = (H + L + C) / 3; + R1 = P + 0.382 * range; S1 = P - 0.382 * range; + R2 = P + 0.618 * range; S2 = P - 0.618 * range; + R3 = P + range; S3 = P - range; + break; + case 4: { // DeMark + double X = C < O ? H + 2 * L + C + : C > O ? 2 * H + L + C : H + L + 2 * C; + P = X / 4; + R1 = X / 2 - L; S1 = X / 2 - H; + break; + } + default: // Standard (floor) + P = (H + L + C) / 3; + R1 = 2 * P - L; S1 = 2 * P - H; + R2 = P + range; S2 = P - range; + R3 = H + 2 * (P - L); S3 = L - 2 * (H - P); + break; + } + double vals[8] = {P, R1, S1, R2, S2, R3, S3, mode == 2 ? R4 : nanv()}; + (void)S4; + for (int j = 0; j < NL; ++j) out[j][i] = vals[j]; + if (mode == 2) out[7][i] = R4, out[6][i] = S3; // keep R4 visible + } + return out; +} +static Lines f_lr_channel(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 20); + double k = pd(p, 1, 2.0); + Lines mid = f_lsma(b, {(double)n}); + Series sd = stdev_s(closes(b), n); + return {combine(mid[0], scale_s(sd, k), '+'), mid[0], + combine(mid[0], scale_s(sd, k), '-')}; +} +static Lines f_projection_bands(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 14); + Series c = closes(b), up(c.size(), nanv()), dn(c.size(), nanv()); + for (size_t i = (size_t)(n - 1); i < c.size(); ++i) { + double sx = 0, sy = 0, sxx = 0, sxy = 0; + for (int j = 0; j < n; ++j) { + double x = j, y = c[i - n + 1 + j]; + sx += x; sy += y; sxx += x * x; sxy += x * y; + } + double den = n * sxx - sx * sx; + if (den == 0) continue; + double slope = (n * sxy - sx * sy) / den; + double inter = (sy - slope * sx) / n; + double mx = -1e300, mn = 1e300; + for (int j = 0; j < n; ++j) { + double proj = b[i - n + 1 + j].high + slope * (n - 1 - j); + double projl = b[i - n + 1 + j].low + slope * (n - 1 - j); + mx = std::max(mx, proj); + mn = std::min(mn, projl); + } + (void)inter; + up[i] = mx; + dn[i] = mn; + } + return {up, dn}; +} +static Lines f_atr_bands(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 14); + double k = pd(p, 1, 2.0); + Series atr = atr_s(b, n), c = closes(b); + return {combine(c, scale_s(atr, k), '+'), + combine(c, scale_s(atr, k), '-')}; +} +static Lines f_hilo_bands(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 21); + Series hi = shift_s(sma_s(highs(b), n), 1), lo = shift_s(sma_s(lows(b), n), 1); + return {hi, lo}; +} +static Lines f_fractal_bands(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 21); + return {rolling_max(highs(b), n), rolling_min(lows(b), n)}; +} +static Lines f_chandelier(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 22); + double k = pd(p, 1, 3.0); + Series atr = atr_s(b, n); + Series lng = combine(rolling_max(highs(b), n), scale_s(atr, -k), '+'); + Series sht = combine(rolling_min(lows(b), n), scale_s(atr, k), '+'); + return {lng, sht}; +} +static Lines f_alligator(const std::vector& b, + const std::vector& p) { + (void)p; + Series med = hl2(b); + Series jaw = shift_s(smma_s(med, 13), 8); + Series teeth = shift_s(smma_s(med, 8), 5); + Series lips = shift_s(smma_s(med, 5), 3); + return {jaw, teeth, lips}; +} +static Lines f_guppy(const std::vector& b, + const std::vector& p) { + (void)p; + Series c = closes(b); + return {ema_s(c, 3), ema_s(c, 8), ema_s(c, 15), + ema_s(c, 30), ema_s(c, 45), ema_s(c, 60)}; +} +static Lines f_rainbow(const std::vector& b, + const std::vector& p) { + (void)p; + Series s = closes(b); + Lines out; + for (int i = 0; i < 6; ++i) { + s = sma_s(s, 2); + out.push_back(s); + } + return out; +} +static Lines f_vwap_bands(const std::vector& b, + const std::vector& p) { + double k = pd(p, 0, 1.0); + Series tp = hlc3(b), vw(b.size(), nanv()), up(b.size(), nanv()), + dn(b.size(), nanv()); + double pv = 0, vv = 0, mean = 0, m2 = 0; + for (size_t i = 0; i < b.size(); ++i) { + pv += tp[i] * b[i].volume; + vv += b[i].volume; + if (vv <= 0) continue; + vw[i] = pv / vv; + double d = tp[i] - mean; + mean += d / (i + 1); + m2 += d * (tp[i] - mean); + double sd = std::sqrt(m2 / (i + 1)); + up[i] = vw[i] + k * sd; + dn[i] = vw[i] - k * sd; + } + return {up, vw, dn}; +} +static Lines f_zigzag(const std::vector& b, + const std::vector& p) { + double pct = pd(p, 0, 5.0) / 100; + Series c = closes(b), out(c.size(), nanv()); + if (c.size() < 2) return {out}; + std::vector> piv; + int dir = 0; + double hi = c[0], lo = c[0]; + size_t hii = 0, loi = 0; + for (size_t i = 1; i < c.size(); ++i) { + if (dir >= 0 && c[i] > hi) { hi = c[i]; hii = i; } + if (dir <= 0 && c[i] < lo) { lo = c[i]; loi = i; } + if (dir >= 0 && c[i] < hi * (1 - pct)) { + piv.push_back({hii, hi}); + dir = -1; lo = c[i]; loi = i; + } else if (dir <= 0 && c[i] > lo * (1 + pct)) { + piv.push_back({loi, lo}); + dir = 1; hi = c[i]; hii = i; + } + } + piv.push_back({c.size() - 1, c.back()}); + for (size_t k = 1; k < piv.size(); ++k) { + size_t i0 = piv[k - 1].first, i1 = piv[k].first; + if (i1 == i0) continue; + for (size_t i = i0; i <= i1; ++i) + out[i] = piv[k - 1].second + + (piv[k].second - piv[k - 1].second) * (i - i0) / + (double)(i1 - i0); + } + return {out}; +} +static Lines f_fractals(const std::vector& b, + const std::vector&) { + Series out(b.size(), 0.0); + for (size_t i = 2; i + 2 < b.size(); ++i) { + if (b[i].high > b[i - 1].high && b[i].high > b[i - 2].high && + b[i].high > b[i + 1].high && b[i].high > b[i + 2].high) + out[i] = -1; // up-fractal (resistance) + if (b[i].low < b[i - 1].low && b[i].low < b[i - 2].low && + b[i].low < b[i + 1].low && b[i].low < b[i + 2].low) + out[i] = 1; // down-fractal (support) + } + return {out}; +} +static Lines f_impulse(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 13); + Series e = ema_s(closes(b), n); + Series de = diff_s(e); + Series out(b.size(), 0.0); + // MACD histogram slope as the momentum leg. + Series macd = combine(ema_s(closes(b), 12), ema_s(closes(b), 26), '-'); + Series dh = diff_s(macd); + for (size_t i = 0; i < b.size(); ++i) { + if (!fin(de[i]) || !fin(dh[i])) continue; + if (de[i] > 0 && dh[i] > 0) out[i] = 1; + else if (de[i] < 0 && dh[i] < 0) out[i] = -1; + } + return {out}; +} + +static void defs_bands(std::vector& v) { + auto O = PlotKind::Overlay, P = PlotKind::Pane, M = PlotKind::Markers; + auto L = Style::Lines, B = Style::Bars; + (void)B; + v.push_back({"Bollinger Bands", O, L, 0, "20,2", f_bollinger}); + v.push_back({"Bollinger %B", P, L, 1, "20,2", f_bollinger_pb}); + v.push_back({"Bollinger Bandwidth", P, L, 2, "20,2", f_bollinger_bw}); + v.push_back({"Keltner Channels", O, L, 3, "20,2,10", f_keltner}); + v.push_back({"Donchian Channels", O, L, 4, "20", f_donchian}); + v.push_back({"STARC Bands", O, L, 6, "15,2,15", f_starc}); + v.push_back({"MA Envelope", O, L, 7, "20,2.5", f_envelope}); + v.push_back({"Ichimoku", O, L, 8, "9,26,52", f_ichimoku}); + v.push_back({"Supertrend", O, L, 9, "10,3", f_supertrend}); + v.push_back({"Parabolic SAR", O, L, 10, "0.02,0.2", f_sar}); + v.push_back({"Pivot Points Standard", O, L, 11, "0", f_pivots}); + v.push_back({"Pivot Points Woodie", O, L, 12, "1", f_pivots}); + v.push_back({"Pivot Points Camarilla", O, L, 13, "2", f_pivots}); + v.push_back({"Pivot Points Fibonacci", O, L, 0, "3", f_pivots}); + v.push_back({"Pivot Points DeMark", O, L, 1, "4", f_pivots}); + v.push_back({"Linear Regression Channel", O, L, 2, "20,2", f_lr_channel}); + v.push_back({"Projection Bands", O, L, 3, "14", f_projection_bands}); + v.push_back({"ATR Bands", O, L, 4, "14,2", f_atr_bands}); + v.push_back({"High-Low Bands", O, L, 5, "21", f_hilo_bands}); + v.push_back({"Fractal Chaos Bands", O, L, 6, "21", f_fractal_bands}); + v.push_back({"Chandelier Exit", O, L, 7, "22,3", f_chandelier}); + v.push_back({"Alligator", O, L, 8, "", f_alligator}); + v.push_back({"Guppy MMA", O, L, 9, "", f_guppy}); + v.push_back({"Rainbow MA", O, L, 10, "", f_rainbow}); + v.push_back({"VWAP Bands", O, L, 11, "1", f_vwap_bands}); + v.push_back({"ZigZag", O, L, 12, "5", f_zigzag}); + v.push_back({"Williams Fractals", M, L, 13, "", f_fractals}); + v.push_back({"Elder Impulse", M, L, 0, "13", f_impulse}); +} + +// ============================================================ momentum + +static Series stoch_k(const std::vector& b, int n) { + Series hh = rolling_max(highs(b), n), ll = rolling_min(lows(b), n); + Series c = closes(b), out(c.size(), nanv()); + for (size_t i = 0; i < c.size(); ++i) + if (fin(hh[i]) && fin(ll[i]) && hh[i] > ll[i]) + out[i] = (c[i] - ll[i]) / (hh[i] - ll[i]) * 100; + return out; +} +static Lines f_rsi(const std::vector& b, const std::vector& p) { + return {rsi_s(closes(b), pi(p, 0, 14))}; +} +static Lines f_laguerre_rsi(const std::vector& b, + const std::vector& p) { + double g = pd(p, 0, 0.5); + Series c = closes(b), out(c.size(), nanv()); + double L[4] = {0, 0, 0, 0}; + bool started = false; + for (size_t i = 0; i < c.size(); ++i) { + if (!started) { + for (double& v : L) v = c[i]; + started = true; + } + double o0 = L[0], o1 = L[1], o2 = L[2], o3 = L[3]; + L[0] = (1 - g) * c[i] + g * o0; + L[1] = -g * L[0] + o0 + g * o1; + L[2] = -g * L[1] + o1 + g * o2; + L[3] = -g * L[2] + o2 + g * o3; + double cu = 0, cd = 0; + double lv[4] = {L[0], L[1], L[2], L[3]}; + for (int j = 0; j < 3; ++j) { + if (lv[j] > lv[j + 1]) cu += lv[j] - lv[j + 1]; + else cd += lv[j + 1] - lv[j]; + } + out[i] = (cu + cd) > 0 ? cu / (cu + cd) * 100 : 50; + } + return {out}; +} +static Lines f_connors_rsi(const std::vector& b, + const std::vector& p) { + (void)p; + Series c = closes(b); + Series r1 = rsi_s(c, 3); + // Up/down streak length series. + Series streak(c.size(), 0.0); + for (size_t i = 1; i < c.size(); ++i) { + if (c[i] > c[i - 1]) streak[i] = streak[i - 1] > 0 ? streak[i - 1] + 1 : 1; + else if (c[i] < c[i - 1]) streak[i] = streak[i - 1] < 0 ? streak[i - 1] - 1 : -1; + else streak[i] = 0; + } + Series r2 = rsi_s(streak, 2); + Series roc = diff_s(c); + Series rank(c.size(), nanv()); + for (size_t i = 100; i < c.size(); ++i) { + if (!fin(roc[i])) continue; + int below = 0, tot = 0; + for (int j = 0; j < 100; ++j) + if (fin(roc[i - j])) { + ++tot; + if (roc[i - j] < roc[i]) ++below; + } + if (tot) rank[i] = (double)below / tot * 100; + } + Series out(c.size(), nanv()); + for (size_t i = 0; i < c.size(); ++i) + if (fin(r1[i]) && fin(r2[i]) && fin(rank[i])) + out[i] = (r1[i] + r2[i] + rank[i]) / 3; + return {out}; +} +static Lines f_inv_fisher_rsi(const std::vector& b, + const std::vector& p) { + Series r = rsi_s(closes(b), pi(p, 0, 14)); + Series out(r.size(), nanv()); + for (size_t i = 0; i < r.size(); ++i) + if (fin(r[i])) { + double v = 0.1 * (r[i] - 50); + double e = std::exp(2 * v); + out[i] = ((e - 1) / (e + 1) + 1) * 50; + } + return {out}; +} +static Lines f_stoch_fast(const std::vector& b, + const std::vector& p) { + Series k = stoch_k(b, pi(p, 0, 14)); + return {k, sma_s(k, 3)}; +} +static Lines f_stoch_slow(const std::vector& b, + const std::vector& p) { + Series k = sma_s(stoch_k(b, pi(p, 0, 14)), 3); + return {k, sma_s(k, 3)}; +} +static Lines f_stoch_full(const std::vector& b, + const std::vector& p) { + Series k = sma_s(stoch_k(b, pi(p, 0, 14)), pi(p, 1, 3)); + return {k, sma_s(k, pi(p, 2, 3))}; +} +static Lines f_stoch_rsi(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 14); + Series r = rsi_s(closes(b), n); + Series mx = rolling_max(r, n), mn = rolling_min(r, n); + Series k(r.size(), nanv()); + for (size_t i = 0; i < r.size(); ++i) + if (fin(mx[i]) && fin(mn[i]) && mx[i] > mn[i]) + k[i] = (r[i] - mn[i]) / (mx[i] - mn[i]) * 100; + return {k, sma_s(k, 3)}; +} +static Lines f_smi(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 14); + Series c = closes(b); + Series hh = rolling_max(highs(b), n), ll = rolling_min(lows(b), n); + Series mid(c.size(), nanv()), rel(c.size(), nanv()), rng(c.size(), nanv()); + for (size_t i = 0; i < c.size(); ++i) + if (fin(hh[i]) && fin(ll[i])) { + mid[i] = (hh[i] + ll[i]) / 2; + rel[i] = c[i] - mid[i]; + rng[i] = hh[i] - ll[i]; + } + Series num = ema_s(ema_s(rel, 5), 5); + Series den = ema_s(ema_s(rng, 5), 5); + return {combine(scale_s(num, 100), den, '/')}; +} +static Lines f_williams_r(const std::vector& b, + const std::vector& p) { + Series k = stoch_k(b, pi(p, 0, 14)); + for (double& v : k) + if (fin(v)) v -= 100; + return {k}; +} +static Lines f_cci(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 20); + Series tp = hlc3(b), m = sma_s(tp, n), out(tp.size(), nanv()); + for (size_t i = 0; i < tp.size(); ++i) { + if (!fin(m[i])) continue; + double mad = 0; + for (int j = 0; j < n; ++j) mad += std::fabs(tp[i - j] - m[i]); + mad /= n; + if (mad > 0) out[i] = (tp[i] - m[i]) / (0.015 * mad); + } + return {out}; +} +static Lines f_cmo(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 14); + Series d = diff_s(closes(b)); + Series g(d.size(), nanv()), l(d.size(), nanv()); + for (size_t i = 0; i < d.size(); ++i) + if (fin(d[i])) { + g[i] = d[i] > 0 ? d[i] : 0; + l[i] = d[i] < 0 ? -d[i] : 0; + } + Series gs = rolling_sum(g, n), ls = rolling_sum(l, n); + return {combine(scale_s(combine(gs, ls, '-'), 100), + combine(gs, ls, '+'), '/')}; +} +static Lines f_roc(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 12); + Series c = closes(b), out(c.size(), nanv()); + for (size_t i = (size_t)n; i < c.size(); ++i) + if (c[i - n] != 0) out[i] = (c[i] - c[i - n]) / c[i - n] * 100; + return {out}; +} +static Lines f_momentum(const std::vector& b, + const std::vector& p) { + return {diff_s(closes(b), pi(p, 0, 10))}; +} +static Series macd_line(const std::vector& b, int fast, int slow) { + return combine(ema_s(closes(b), fast), ema_s(closes(b), slow), '-'); +} +static Lines f_macd(const std::vector& b, const std::vector& p) { + Series m = macd_line(b, pi(p, 0, 12), pi(p, 1, 26)); + Series sig = ema_s(m, pi(p, 2, 9)); + return {m, sig}; +} +static Lines f_macd_hist(const std::vector& b, + const std::vector& p) { + Lines m = f_macd(b, p); + return {combine(m[0], m[1], '-')}; +} +static Lines f_ppo(const std::vector& b, const std::vector& p) { + Series slow = ema_s(closes(b), pi(p, 1, 26)); + Series m = combine(macd_line(b, pi(p, 0, 12), pi(p, 1, 26)), slow, '/'); + m = scale_s(m, 100); + return {m, ema_s(m, pi(p, 2, 9))}; +} +static Lines f_apo(const std::vector& b, const std::vector& p) { + return {macd_line(b, pi(p, 0, 12), pi(p, 1, 26))}; +} +static Lines f_trix(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 15); + Series e = ema_s(ema_s(ema_s(closes(b), n), n), n); + Series out(e.size(), nanv()); + for (size_t i = 1; i < e.size(); ++i) + if (fin(e[i]) && fin(e[i - 1]) && e[i - 1] != 0) + out[i] = (e[i] - e[i - 1]) / e[i - 1] * 100; + return {out}; +} +static Lines f_tsi(const std::vector& b, const std::vector& p) { + int r = pi(p, 0, 25), s = pi(p, 1, 13); + Series d = diff_s(closes(b)); + Series ad(d.size(), nanv()); + for (size_t i = 0; i < d.size(); ++i) + if (fin(d[i])) ad[i] = std::fabs(d[i]); + Series num = ema_s(ema_s(d, r), s); + Series den = ema_s(ema_s(ad, r), s); + return {combine(scale_s(num, 100), den, '/')}; +} +static Lines f_rmi(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 14), m = pi(p, 1, 5); + Series d = diff_s(closes(b), m); + Series out(d.size(), nanv()); + double gain = 0, loss = 0; + for (size_t i = 0; i < d.size(); ++i) { + if (!fin(d[i])) continue; + double g = d[i] > 0 ? d[i] : 0, l = d[i] < 0 ? -d[i] : 0; + gain = gain == 0 && loss == 0 ? g : (gain * (n - 1) + g) / n; + loss = gain == g && loss == 0 ? l : (loss * (n - 1) + l) / n; + if (i >= (size_t)(n + m)) + out[i] = loss == 0 ? 100 : 100 - 100 / (1 + gain / loss); + } + return {out}; +} +static Lines f_ultimate(const std::vector& b, + const std::vector& p) { + (void)p; + Series bp(b.size(), nanv()), tr(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) { + double pc = i > 0 ? b[i - 1].close : b[i].open; + bp[i] = b[i].close - std::min(b[i].low, pc); + tr[i] = std::max(b[i].high, pc) - std::min(b[i].low, pc); + } + Series a1 = combine(rolling_sum(bp, 7), rolling_sum(tr, 7), '/'); + Series a2 = combine(rolling_sum(bp, 14), rolling_sum(tr, 14), '/'); + Series a3 = combine(rolling_sum(bp, 28), rolling_sum(tr, 28), '/'); + Series out(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (fin(a1[i]) && fin(a2[i]) && fin(a3[i])) + out[i] = (4 * a1[i] + 2 * a2[i] + a3[i]) / 7 * 100; + return {out}; +} +static Lines f_awesome(const std::vector& b, + const std::vector&) { + return {combine(sma_s(hl2(b), 5), sma_s(hl2(b), 34), '-')}; +} +static Lines f_accelerator(const std::vector& b, + const std::vector& p) { + Lines ao = f_awesome(b, p); + return {combine(ao[0], sma_s(ao[0], 5), '-')}; +} +static Lines f_fisher(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 10); + Series hh = rolling_max(highs(b), n), ll = rolling_min(lows(b), n); + Series c = closes(b), fish(c.size(), nanv()); + double x = 0, prev = 0; + for (size_t i = 0; i < c.size(); ++i) { + if (!fin(hh[i]) || !fin(ll[i]) || hh[i] <= ll[i]) continue; + double v = (c[i] - ll[i]) / (hh[i] - ll[i]); + x = 0.33 * 2 * (v - 0.5) + 0.67 * x; + x = std::min(0.999, std::max(-0.999, x)); + double f = 0.5 * std::log((1 + x) / (1 - x)) + 0.5 * prev; + fish[i] = f; + prev = f; + } + return {fish, shift_s(fish, 1)}; +} +static Lines f_stc(const std::vector& b, const std::vector& p) { + (void)p; + Series m = macd_line(b, 23, 50); + auto stoch_of = [&](const Series& s, int n) { + Series mx = rolling_max(s, n), mn = rolling_min(s, n); + Series out(s.size(), nanv()); + for (size_t i = 0; i < s.size(); ++i) + if (fin(mx[i]) && fin(mn[i]) && mx[i] > mn[i]) + out[i] = (s[i] - mn[i]) / (mx[i] - mn[i]); + return out; + }; + Series s1 = stoch_of(m, 10); + Series sm1 = ema_s(s1, 3); + Series s2 = stoch_of(sm1, 10); + return {scale_s(ema_s(s2, 3), 100)}; +} +static Lines f_coppock(const std::vector& b, + const std::vector& p) { + (void)p; + Series r = combine(f_roc(b, {14})[0], f_roc(b, {11})[0], '+'); + return {wma_s(r, 10)}; +} +static Lines f_kst(const std::vector& b, const std::vector& p) { + (void)p; + Series k = scale_s(sma_s(f_roc(b, {10})[0], 10), 1); + k = combine(k, scale_s(sma_s(f_roc(b, {15})[0], 10), 2), '+'); + k = combine(k, scale_s(sma_s(f_roc(b, {20})[0], 10), 3), '+'); + k = combine(k, scale_s(sma_s(f_roc(b, {30})[0], 15), 4), '+'); + return {k, sma_s(k, 9)}; +} +static Series sym_smooth(const Series& in) { // (x + 2x1 + 2x2 + x3)/6 + Series out(in.size(), nanv()); + for (size_t i = 3; i < in.size(); ++i) + out[i] = (in[i] + 2 * in[i - 1] + 2 * in[i - 2] + in[i - 3]) / 6; + return out; +} +static Lines f_rvi_vigor(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 10); + Series co(b.size()), hl(b.size()); + for (size_t i = 0; i < b.size(); ++i) { + co[i] = b[i].close - b[i].open; + hl[i] = b[i].high - b[i].low; + } + Series rvi = combine(sma_s(sym_smooth(co), n), + sma_s(sym_smooth(hl), n), '/'); + return {rvi, sym_smooth(rvi)}; +} +static Lines f_demarker(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 14); + Series mx(b.size(), 0.0), mn(b.size(), 0.0); + for (size_t i = 1; i < b.size(); ++i) { + mx[i] = std::max(0.0, b[i].high - b[i - 1].high); + mn[i] = std::max(0.0, b[i - 1].low - b[i].low); + } + Series a = sma_s(mx, n), d = sma_s(mn, n); + return {combine(a, combine(a, d, '+'), '/')}; +} +static Lines f_bop(const std::vector& b, const std::vector& p) { + Series out(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (b[i].high > b[i].low) + out[i] = (b[i].close - b[i].open) / (b[i].high - b[i].low); + return {sma_s(out, pi(p, 0, 14))}; +} +static Lines f_bull_bear(const std::vector& b, + const std::vector& p, bool bull) { + Series e = ema_s(closes(b), pi(p, 0, 13)); + Series out(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (fin(e[i])) out[i] = (bull ? b[i].high : b[i].low) - e[i]; + return {out}; +} +static Lines f_bull_power(const std::vector& b, + const std::vector& p) { + return f_bull_bear(b, p, true); +} +static Lines f_bear_power(const std::vector& b, + const std::vector& p) { + return f_bull_bear(b, p, false); +} +static Lines f_force(const std::vector& b, + const std::vector& p) { + Series d = diff_s(closes(b)); + Series f(d.size(), nanv()); + for (size_t i = 0; i < d.size(); ++i) + if (fin(d[i])) f[i] = d[i] * b[i].volume; + return {ema_s(f, pi(p, 0, 13))}; +} +static Lines f_eom(const std::vector& b, const std::vector& p) { + Series m = hl2(b), dm = diff_s(m); + Series out(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) { + if (!fin(dm[i]) || b[i].volume <= 0 || b[i].high <= b[i].low) + continue; + double box = b[i].volume / (b[i].high - b[i].low); + out[i] = dm[i] / box * 10000; + } + return {sma_s(out, pi(p, 0, 14))}; +} +static Lines f_mass_index(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 25); + Series range = combine(highs(b), lows(b), '-'); + Series e1 = ema_s(range, 9); + Series ratio = combine(e1, ema_s(e1, 9), '/'); + return {rolling_sum(ratio, n)}; +} +static Lines f_vortex(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 14); + Series vp(b.size(), nanv()), vm(b.size(), nanv()); + for (size_t i = 1; i < b.size(); ++i) { + vp[i] = std::fabs(b[i].high - b[i - 1].low); + vm[i] = std::fabs(b[i].low - b[i - 1].high); + } + Series trs = rolling_sum(true_range(b), n); + return {combine(rolling_sum(vp, n), trs, '/'), + combine(rolling_sum(vm, n), trs, '/')}; +} +static Lines f_aroon(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 25); + Series up(b.size(), nanv()), dn(b.size(), nanv()); + for (size_t i = (size_t)n; i < b.size(); ++i) { + int since_hi = 0, since_lo = 0; + double mx = -1e300, mn = 1e300; + for (int j = 0; j <= n; ++j) { + if (b[i - j].high >= mx) { mx = b[i - j].high; since_hi = j; } + if (b[i - j].low <= mn) { mn = b[i - j].low; since_lo = j; } + } + up[i] = (double)(n - since_hi) / n * 100; + dn[i] = (double)(n - since_lo) / n * 100; + } + return {up, dn}; +} +static Lines f_aroon_osc(const std::vector& b, + const std::vector& p) { + Lines a = f_aroon(b, p); + return {combine(a[0], a[1], '-')}; +} +struct DiAdx { + Series plus_di, minus_di, dx, adx; +}; +static DiAdx di_adx(const std::vector& b, int n) { + DiAdx r; + size_t N = b.size(); + r.plus_di.assign(N, nanv()); + r.minus_di.assign(N, nanv()); + r.dx.assign(N, nanv()); + r.adx.assign(N, nanv()); + Series pdm(N, 0.0), mdm(N, 0.0), tr = true_range(b); + for (size_t i = 1; i < N; ++i) { + double up = b[i].high - b[i - 1].high; + double dn = b[i - 1].low - b[i].low; + pdm[i] = up > dn && up > 0 ? up : 0; + mdm[i] = dn > up && dn > 0 ? dn : 0; + } + Series sp = smma_s(pdm, n), sm = smma_s(mdm, n), st = smma_s(tr, n); + for (size_t i = 0; i < N; ++i) + if (fin(st[i]) && st[i] > 0 && fin(sp[i]) && fin(sm[i])) { + r.plus_di[i] = 100 * sp[i] / st[i]; + r.minus_di[i] = 100 * sm[i] / st[i]; + double den = r.plus_di[i] + r.minus_di[i]; + if (den > 0) + r.dx[i] = 100 * std::fabs(r.plus_di[i] - r.minus_di[i]) / den; + } + r.adx = smma_s(r.dx, n); + return r; +} +static Lines f_adx(const std::vector& b, const std::vector& p) { + return {di_adx(b, pi(p, 0, 14)).adx}; +} +static Lines f_plus_di(const std::vector& b, + const std::vector& p) { + return {di_adx(b, pi(p, 0, 14)).plus_di}; +} +static Lines f_minus_di(const std::vector& b, + const std::vector& p) { + return {di_adx(b, pi(p, 0, 14)).minus_di}; +} +static Lines f_dx(const std::vector& b, const std::vector& p) { + return {di_adx(b, pi(p, 0, 14)).dx}; +} +static Lines f_adxr(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 14); + Series a = di_adx(b, n).adx; + return {scale_s(combine(a, shift_s(a, n), '+'), 0.5)}; +} +static Lines f_dpo(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 20); + Series m = sma_s(closes(b), n); + return {combine(shift_s(closes(b), n / 2 + 1), m, '-')}; +} +static Lines f_qstick(const std::vector& b, + const std::vector& p) { + Series co(b.size()); + for (size_t i = 0; i < b.size(); ++i) co[i] = b[i].close - b[i].open; + return {sma_s(co, pi(p, 0, 14))}; +} +static Lines f_imi(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 14); + Series g(b.size(), 0.0), l(b.size(), 0.0); + for (size_t i = 0; i < b.size(); ++i) { + double d = b[i].close - b[i].open; + if (d > 0) g[i] = d; + else l[i] = -d; + } + Series gs = rolling_sum(g, n), ls = rolling_sum(l, n); + return {combine(scale_s(gs, 100), combine(gs, ls, '+'), '/')}; +} +static Lines f_rwi(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 14); + Series atr = atr_s(b, n); + Series hi_s(b.size(), nanv()), lo_s(b.size(), nanv()); + double sq = std::sqrt(n); + for (size_t i = (size_t)n; i < b.size(); ++i) + if (fin(atr[i]) && atr[i] > 0) { + hi_s[i] = (b[i].high - b[i - n].low) / (atr[i] * sq); + lo_s[i] = (b[i - n].high - b[i].low) / (atr[i] * sq); + } + return {hi_s, lo_s}; +} +static Series swing_index_s(const std::vector& b) { + Series out(b.size(), nanv()); + for (size_t i = 1; i < b.size(); ++i) { + const Bar& c = b[i]; + const Bar& pc = b[i - 1]; + double r1 = std::fabs(c.high - pc.close); + double r2 = std::fabs(c.low - pc.close); + double r3 = std::fabs(c.high - c.low); + double R; + if (r1 > r2 && r1 > r3) + R = r1 - r2 / 2 + std::fabs(pc.close - pc.open) / 4; + else if (r2 > r1 && r2 > r3) + R = r2 - r1 / 2 + std::fabs(pc.close - pc.open) / 4; + else + R = r3 + std::fabs(pc.close - pc.open) / 4; + if (R == 0) { + out[i] = 0; + continue; + } + double K = std::max(std::fabs(c.high - pc.close), + std::fabs(c.low - pc.close)); + out[i] = 50 * ((c.close - pc.close) + 0.5 * (c.close - c.open) + + 0.25 * (pc.close - pc.open)) / + R * K / 10; + } + return out; +} +static Lines f_swing(const std::vector& b, + const std::vector&) { + return {swing_index_s(b)}; +} +static Lines f_aswing(const std::vector& b, + const std::vector&) { + Series si = swing_index_s(b), out(b.size(), nanv()); + double acc = 0; + for (size_t i = 0; i < b.size(); ++i) + if (fin(si[i])) { + acc += si[i]; + out[i] = acc; + } + return {out}; +} +static Lines f_gator(const std::vector& b, + const std::vector& p) { + Lines a = f_alligator(b, p); + Series up(b.size(), nanv()), dn(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) { + if (fin(a[0][i]) && fin(a[1][i])) + up[i] = std::fabs(a[0][i] - a[1][i]); + if (fin(a[1][i]) && fin(a[2][i])) + dn[i] = -std::fabs(a[1][i] - a[2][i]); + } + return {up, dn}; +} +static Lines f_projection_osc(const std::vector& b, + const std::vector& p) { + Lines bands = f_projection_bands(b, p); + Series c = closes(b), out(c.size(), nanv()); + for (size_t i = 0; i < c.size(); ++i) + if (fin(bands[0][i]) && fin(bands[1][i]) && + bands[0][i] > bands[1][i]) + out[i] = (c[i] - bands[1][i]) / (bands[0][i] - bands[1][i]) * 100; + return {out}; +} + +static void defs_momentum(std::vector& v) { + auto P = PlotKind::Pane; + auto L = Style::Lines, B = Style::Bars; + v.push_back({"RSI", P, L, 0, "14", f_rsi}); + v.push_back({"Laguerre RSI", P, L, 1, "0.5", f_laguerre_rsi}); + v.push_back({"Connors RSI", P, L, 2, "", f_connors_rsi}); + v.push_back({"Inverse Fisher RSI", P, L, 3, "14", f_inv_fisher_rsi}); + v.push_back({"Stochastic Fast", P, L, 4, "14", f_stoch_fast}); + v.push_back({"Stochastic Slow", P, L, 5, "14", f_stoch_slow}); + v.push_back({"Stochastic Full", P, L, 6, "14,3,3", f_stoch_full}); + v.push_back({"Stochastic RSI", P, L, 7, "14", f_stoch_rsi}); + v.push_back({"SMI", P, L, 8, "14", f_smi}); + v.push_back({"Williams %R", P, L, 9, "14", f_williams_r}); + v.push_back({"CCI", P, L, 10, "20", f_cci}); + v.push_back({"CMO", P, L, 11, "14", f_cmo}); + v.push_back({"ROC", P, L, 12, "12", f_roc}); + v.push_back({"Momentum", P, L, 13, "10", f_momentum}); + v.push_back({"MACD", P, L, 0, "12,26,9", f_macd}); + v.push_back({"MACD Histogram", P, B, 1, "12,26,9", f_macd_hist}); + v.push_back({"PPO", P, L, 2, "12,26,9", f_ppo}); + v.push_back({"APO", P, L, 3, "12,26", f_apo}); + v.push_back({"TRIX", P, L, 4, "15", f_trix}); + v.push_back({"TSI", P, L, 5, "25,13", f_tsi}); + v.push_back({"RMI", P, L, 6, "14,5", f_rmi}); + v.push_back({"Ultimate Oscillator", P, L, 7, "", f_ultimate}); + v.push_back({"Awesome Oscillator", P, B, 8, "", f_awesome}); + v.push_back({"Accelerator Oscillator", P, B, 9, "", f_accelerator}); + v.push_back({"Fisher Transform", P, L, 10, "10", f_fisher}); + v.push_back({"Schaff Trend Cycle", P, L, 11, "", f_stc}); + v.push_back({"Coppock Curve", P, L, 12, "", f_coppock}); + v.push_back({"KST", P, L, 13, "", f_kst}); + v.push_back({"Relative Vigor Index", P, L, 0, "10", f_rvi_vigor}); + v.push_back({"DeMarker", P, L, 1, "14", f_demarker}); + v.push_back({"Balance of Power", P, L, 2, "14", f_bop}); + v.push_back({"Bull Power", P, L, 3, "13", f_bull_power}); + v.push_back({"Bear Power", P, L, 4, "13", f_bear_power}); + v.push_back({"Force Index", P, L, 5, "13", f_force}); + v.push_back({"Ease of Movement", P, L, 6, "14", f_eom}); + v.push_back({"Mass Index", P, L, 7, "25", f_mass_index}); + v.push_back({"Vortex Indicator", P, L, 8, "14", f_vortex}); + v.push_back({"Aroon", P, L, 9, "25", f_aroon}); + v.push_back({"Aroon Oscillator", P, L, 10, "25", f_aroon_osc}); + v.push_back({"ADX", P, L, 11, "14", f_adx}); + v.push_back({"Plus DI", P, L, 12, "14", f_plus_di}); + v.push_back({"Minus DI", P, L, 13, "14", f_minus_di}); + v.push_back({"DX", P, L, 0, "14", f_dx}); + v.push_back({"ADXR", P, L, 1, "14", f_adxr}); + v.push_back({"DPO", P, L, 2, "20", f_dpo}); + v.push_back({"Qstick", P, L, 3, "14", f_qstick}); + v.push_back({"IMI", P, L, 4, "14", f_imi}); + v.push_back({"Random Walk Index", P, L, 5, "14", f_rwi}); + v.push_back({"Swing Index", P, L, 6, "", f_swing}); + v.push_back({"Accumulative Swing Index", P, L, 7, "", f_aswing}); + v.push_back({"Gator Oscillator", P, B, 8, "", f_gator}); + v.push_back({"Projection Oscillator", P, L, 9, "14", f_projection_osc}); +} + +// ============================================================ volume + +static Lines f_volume(const std::vector& b, + const std::vector&) { + return {vols(b)}; +} +static Lines f_volume_ma(const std::vector& b, + const std::vector& p) { + return {sma_s(vols(b), pi(p, 0, 20))}; +} +static Lines f_obv(const std::vector& b, const std::vector&) { + Series out(b.size(), nanv()); + double acc = 0; + for (size_t i = 0; i < b.size(); ++i) { + if (i > 0) { + if (b[i].close > b[i - 1].close) acc += b[i].volume; + else if (b[i].close < b[i - 1].close) acc -= b[i].volume; + } + out[i] = acc; + } + return {out}; +} +static Lines f_pvt(const std::vector& b, const std::vector&) { + Series out(b.size(), nanv()); + double acc = 0; + for (size_t i = 0; i < b.size(); ++i) { + if (i > 0 && b[i - 1].close != 0) + acc += b[i].volume * (b[i].close - b[i - 1].close) / + b[i - 1].close; + out[i] = acc; + } + return {out}; +} +static Series ad_line_s(const std::vector& b) { + Series out(b.size(), nanv()); + double acc = 0; + for (size_t i = 0; i < b.size(); ++i) { + if (b[i].high > b[i].low) { + double mfm = ((b[i].close - b[i].low) - (b[i].high - b[i].close)) / + (b[i].high - b[i].low); + acc += mfm * b[i].volume; + } + out[i] = acc; + } + return out; +} +static Lines f_ad_line(const std::vector& b, + const std::vector&) { + return {ad_line_s(b)}; +} +static Lines f_adosc(const std::vector& b, + const std::vector& p) { + Series ad = ad_line_s(b); + return {combine(ema_s(ad, pi(p, 0, 3)), ema_s(ad, pi(p, 1, 10)), '-')}; +} +static Lines f_cmf(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 20); + Series mfv(b.size(), 0.0); + for (size_t i = 0; i < b.size(); ++i) + if (b[i].high > b[i].low) + mfv[i] = ((b[i].close - b[i].low) - (b[i].high - b[i].close)) / + (b[i].high - b[i].low) * b[i].volume; + return {combine(rolling_sum(mfv, n), rolling_sum(vols(b), n), '/')}; +} +static Lines f_mfi(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 14); + Series tp = hlc3(b); + Series pos(b.size(), 0.0), neg(b.size(), 0.0); + for (size_t i = 1; i < b.size(); ++i) { + double flow = tp[i] * b[i].volume; + if (tp[i] > tp[i - 1]) pos[i] = flow; + else if (tp[i] < tp[i - 1]) neg[i] = flow; + } + Series ps = rolling_sum(pos, n), ns = rolling_sum(neg, n); + Series out(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (fin(ps[i]) && fin(ns[i])) + out[i] = ns[i] == 0 ? 100 : 100 - 100 / (1 + ps[i] / ns[i]); + return {out}; +} +static Lines f_vol_osc(const std::vector& b, + const std::vector& p) { + Series fast = sma_s(vols(b), pi(p, 0, 5)), slow = sma_s(vols(b), pi(p, 1, 34)); + Series out(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (fin(slow[i]) && slow[i] != 0) + out[i] = (fast[i] - slow[i]) / slow[i] * 100; + return {out}; +} +static Lines f_pvo(const std::vector& b, const std::vector& p) { + Series fast = ema_s(vols(b), pi(p, 0, 12)), slow = ema_s(vols(b), pi(p, 1, 26)); + Series out(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (fin(slow[i]) && slow[i] != 0) + out[i] = (fast[i] - slow[i]) / slow[i] * 100; + return {out}; +} +static Lines f_vol_roc(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 12); + Series v = vols(b), out(v.size(), nanv()); + for (size_t i = (size_t)n; i < v.size(); ++i) + if (v[i - n] != 0) out[i] = (v[i] - v[i - n]) / v[i - n] * 100; + return {out}; +} +static Lines f_nvi_pvi(const std::vector& b, + const std::vector&, bool nvi) { + Series out(b.size(), nanv()); + double acc = 1000; + for (size_t i = 0; i < b.size(); ++i) { + if (i > 0 && b[i - 1].close != 0) { + bool down_day = b[i].volume < b[i - 1].volume; + if (down_day == nvi) + acc *= 1 + (b[i].close - b[i - 1].close) / b[i - 1].close; + } + out[i] = acc; + } + return {out}; +} +static Lines f_nvi(const std::vector& b, const std::vector& p) { + return f_nvi_pvi(b, p, true); +} +static Lines f_pvi(const std::vector& b, const std::vector& p) { + return f_nvi_pvi(b, p, false); +} +static Lines f_klinger(const std::vector& b, + const std::vector& p) { + (void)p; + Series vf(b.size(), 0.0); + for (size_t i = 1; i < b.size(); ++i) { + double trend = (b[i].high + b[i].low + b[i].close) - + (b[i - 1].high + b[i - 1].low + b[i - 1].close); + vf[i] = trend >= 0 ? b[i].volume : -b[i].volume; + } + Series ko = combine(ema_s(vf, 34), ema_s(vf, 55), '-'); + return {ko, ema_s(ko, 13)}; +} +static Lines f_twiggs(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 21); + Series trl(b.size(), nanv()), adv(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) { + double pc = i > 0 ? b[i - 1].close : b[i].low; + trl[i] = std::max(b[i].high, pc) - std::min(b[i].low, pc); + adv[i] = trl[i] > 0 ? (b[i].close - std::min(b[i].low, pc)) / trl[i] * + b[i].volume + : 0; + } + Series num = ema_s(adv, n), den = ema_s(vols(b), n); + return {combine(num, den, '/')}; +} +static Lines f_williams_ad(const std::vector& b, + const std::vector&) { + Series out(b.size(), nanv()); + double acc = 0; + for (size_t i = 0; i < b.size(); ++i) { + if (i > 0) { + if (b[i].close > b[i - 1].close) + acc += b[i].close - std::min(b[i].low, b[i - 1].close); + else if (b[i].close < b[i - 1].close) + acc -= std::max(b[i].high, b[i - 1].close) - b[i].close; + } + out[i] = acc; + } + return {out}; +} +static Lines f_intensity(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 21); + Series ii(b.size(), 0.0); + for (size_t i = 0; i < b.size(); ++i) + if (b[i].high > b[i].low) + ii[i] = (2 * b[i].close - b[i].high - b[i].low) / + (b[i].high - b[i].low) * b[i].volume; + return {rolling_sum(ii, n)}; +} +static Lines f_ad_percent(const std::vector& b, + const std::vector&) { + Series ad = ad_line_s(b), cumv(b.size(), nanv()); + double acc = 0; + for (size_t i = 0; i < b.size(); ++i) { + acc += b[i].volume; + cumv[i] = acc; + } + return {combine(scale_s(ad, 100), cumv, '/')}; +} +static Lines f_demand(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 20); + Series bp(b.size(), 0.0), sp(b.size(), 0.0); + for (size_t i = 1; i < b.size(); ++i) { + double w = std::pow(1.0 + std::fabs(b[i].close - b[i - 1].close) / + b[i - 1].close, + 10); + if (b[i].close > b[i - 1].close) bp[i] = b[i].volume * w; + else sp[i] = b[i].volume * w; + } + Series bs = sma_s(bp, n), ss = sma_s(sp, n); + Series out(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (fin(bs[i]) && fin(ss[i])) + out[i] = bs[i] > ss[i] ? 1 - ss[i] / bs[i] + : -(1 - bs[i] / ss[i]); + return {scale_s(out, 100)}; +} +static Lines f_tvi(const std::vector& b, const std::vector&) { + Series out(b.size(), nanv()); + double acc = 0; + for (size_t i = 0; i < b.size(); ++i) { + if (i > 0) { + if (b[i].close > b[i - 1].close) acc += b[i].volume; + else if (b[i].close < b[i - 1].close) acc -= b[i].volume; + } + out[i] = acc; + } + return {out}; +} + +static void defs_volume(std::vector& v) { + auto P = PlotKind::Pane; + auto L = Style::Lines, B = Style::Bars; + v.push_back({"Volume", P, B, 10, "", f_volume}); + v.push_back({"Volume MA", P, L, 11, "20", f_volume_ma}); + v.push_back({"OBV", P, L, 0, "", f_obv}); + v.push_back({"Price Volume Trend", P, L, 1, "", f_pvt}); + v.push_back({"A/D Line", P, L, 2, "", f_ad_line}); + v.push_back({"Chaikin Oscillator", P, L, 3, "3,10", f_adosc}); + v.push_back({"Chaikin Money Flow", P, L, 4, "20", f_cmf}); + v.push_back({"MFI", P, L, 5, "14", f_mfi}); + v.push_back({"Volume Oscillator", P, L, 6, "5,34", f_vol_osc}); + v.push_back({"PVO", P, L, 7, "12,26", f_pvo}); + v.push_back({"Volume ROC", P, L, 8, "12", f_vol_roc}); + v.push_back({"NVI", P, L, 9, "", f_nvi}); + v.push_back({"PVI", P, L, 10, "", f_pvi}); + v.push_back({"Klinger Oscillator", P, L, 11, "", f_klinger}); + v.push_back({"Twiggs Money Flow", P, L, 12, "21", f_twiggs}); + v.push_back({"Williams A/D", P, L, 13, "", f_williams_ad}); + v.push_back({"Intraday Intensity", P, L, 0, "21", f_intensity}); + v.push_back({"Accumulation Percent", P, L, 1, "", f_ad_percent}); + v.push_back({"Demand Index", P, L, 2, "20", f_demand}); + v.push_back({"Trade Volume Index", P, L, 3, "", f_tvi}); +} + +// ============================================================ volatility & statistics + +static Lines f_atr(const std::vector& b, const std::vector& p) { + return {atr_s(b, pi(p, 0, 14))}; +} +static Lines f_natr(const std::vector& b, const std::vector& p) { + Series a = atr_s(b, pi(p, 0, 14)), c = closes(b); + return {combine(scale_s(a, 100), c, '/')}; +} +static Lines f_tr(const std::vector& b, const std::vector&) { + return {true_range(b)}; +} +static Lines f_stddev(const std::vector& b, + const std::vector& p) { + return {stdev_s(closes(b), pi(p, 0, 20))}; +} +static Lines f_variance(const std::vector& b, + const std::vector& p) { + Series sd = stdev_s(closes(b), pi(p, 0, 20)); + for (double& v : sd) + if (fin(v)) v *= v; + return {sd}; +} +static Series log_returns(const std::vector& b) { + Series c = closes(b), out(c.size(), nanv()); + for (size_t i = 1; i < c.size(); ++i) + if (c[i] > 0 && c[i - 1] > 0) out[i] = std::log(c[i] / c[i - 1]); + return out; +} +static Lines f_hist_vol(const std::vector& b, + const std::vector& p) { + return {scale_s(stdev_s(log_returns(b), pi(p, 0, 20)), 100)}; +} +static Lines f_chaikin_vol(const std::vector& b, + const std::vector& p) { + Series e = ema_s(combine(highs(b), lows(b), '-'), pi(p, 0, 10)); + Series out(e.size(), nanv()); + int n = pi(p, 1, 10); + for (size_t i = (size_t)n; i < e.size(); ++i) + if (fin(e[i]) && fin(e[i - n]) && e[i - n] != 0) + out[i] = (e[i] - e[i - n]) / e[i - n] * 100; + return {out}; +} +static Lines f_ulcer(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 14); + Series c = closes(b), mx = rolling_max(c, n), dd(c.size(), nanv()); + for (size_t i = 0; i < c.size(); ++i) + if (fin(mx[i]) && mx[i] > 0) { + double d = (c[i] - mx[i]) / mx[i] * 100; + dd[i] = d * d; + } + Series ms = sma_s(dd, n), out(c.size(), nanv()); + for (size_t i = 0; i < c.size(); ++i) + if (fin(ms[i])) out[i] = std::sqrt(ms[i]); + return {out}; +} +static Lines f_choppiness(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 14); + Series atr_sum = rolling_sum(true_range(b), n); + Series hh = rolling_max(highs(b), n), ll = rolling_min(lows(b), n); + Series out(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (fin(atr_sum[i]) && fin(hh[i]) && hh[i] > ll[i] && atr_sum[i] > 0) + out[i] = 100 * std::log10(atr_sum[i] / (hh[i] - ll[i])) / + std::log10(n); + return {out}; +} +static Lines f_rvol_idx(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 10); + Series d = diff_s(closes(b)); + Series up(b.size(), 0.0), dn(b.size(), 0.0); + for (size_t i = 0; i < d.size(); ++i) + if (fin(d[i])) { + if (d[i] > 0) up[i] = d[i]; + else dn[i] = -d[i]; + } + Series su = sma_s(stdev_s(up, n), n), sd = sma_s(stdev_s(dn, n), n); + Series out(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (fin(su[i]) && fin(sd[i]) && su[i] + sd[i] > 0) + out[i] = su[i] / (su[i] + sd[i]) * 100; + return {out}; +} +static Lines f_parkinson(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 20); + Series v(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (b[i].low > 0) { + double x = std::log(b[i].high / b[i].low); + v[i] = x * x; + } + Series m = sma_s(v, n), out(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (fin(m[i])) out[i] = std::sqrt(m[i] / (4 * std::log(2))) * 100; + return {out}; +} +static Lines f_garman_klass(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 20); + Series v(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (b[i].low > 0 && b[i].open > 0) { + double a = std::log(b[i].high / b[i].low); + double c = std::log(b[i].close / b[i].open); + v[i] = 0.5 * a * a - (2 * std::log(2) - 1) * c * c; + } + Series m = sma_s(v, n), out(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (fin(m[i]) && m[i] > 0) out[i] = std::sqrt(m[i]) * 100; + return {out}; +} +static Lines f_rogers_satchell(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 20); + Series v(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (b[i].open > 0 && b[i].close > 0) { + v[i] = std::log(b[i].high / b[i].close) * + std::log(b[i].high / b[i].open) + + std::log(b[i].low / b[i].close) * + std::log(b[i].low / b[i].open); + } + Series m = sma_s(v, n), out(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (fin(m[i]) && m[i] > 0) out[i] = std::sqrt(m[i]) * 100; + return {out}; +} +static Lines f_yang_zhang(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 20); + Series on(b.size(), 0.0), oc(b.size(), 0.0), rs(b.size(), 0.0); + for (size_t i = 0; i < b.size(); ++i) { + if (b[i].open <= 0 || b[i].close <= 0) continue; + if (i > 0 && b[i - 1].close > 0) { + double x = std::log(b[i].open / b[i - 1].close); + on[i] = x * x; + } + double y = std::log(b[i].close / b[i].open); + oc[i] = y * y; + if (b[i].high > 0 && b[i].low > 0) + rs[i] = std::log(b[i].high / b[i].close) * + std::log(b[i].high / b[i].open) + + std::log(b[i].low / b[i].close) * + std::log(b[i].low / b[i].open); + } + Series mo = sma_s(on, n), mc = sma_s(oc, n), mr = sma_s(rs, n); + Series out(b.size(), nanv()); + double k = 0.34; + for (size_t i = 0; i < b.size(); ++i) + if (fin(mo[i]) && fin(mc[i]) && fin(mr[i])) + out[i] = std::sqrt(std::max(0.0, mo[i] + k * mc[i] + + (1 - k) * mr[i])) * + 100; + return {out}; +} +static Lines f_thermometer(const std::vector& b, + const std::vector& p) { + Series m(b.size(), nanv()); + for (size_t i = 1; i < b.size(); ++i) + m[i] = std::max(std::fabs(b[i].high - b[i - 1].high), + std::fabs(b[i].low - b[i - 1].low)); + return {ema_s(m, pi(p, 0, 22))}; +} +static Lines f_std_error(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 20); + return {scale_s(stdev_s(closes(b), n), 1 / std::sqrt((double)n))}; +} +static Lines f_mean_dev(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 20); + Series c = closes(b), m = sma_s(c, n), out(c.size(), nanv()); + for (size_t i = 0; i < c.size(); ++i) { + if (!fin(m[i])) continue; + double s = 0; + for (int j = 0; j < n; ++j) s += std::fabs(c[i - j] - m[i]); + out[i] = s / n; + } + return {out}; +} +static Lines f_moment(const std::vector& b, + const std::vector& p, int power, bool norm) { + int n = pi(p, 0, 20); + Series c = closes(b), m = sma_s(c, n), sd = stdev_s(c, n); + Series out(c.size(), nanv()); + for (size_t i = 0; i < c.size(); ++i) { + if (!fin(m[i])) continue; + double s = 0; + for (int j = 0; j < n; ++j) s += std::pow(c[i - j] - m[i], power); + s /= n; + out[i] = norm && fin(sd[i]) && sd[i] > 0 + ? s / std::pow(sd[i], power) + : s; + } + return {out}; +} +static Lines f_skewness(const std::vector& b, + const std::vector& p) { + return f_moment(b, p, 3, true); +} +static Lines f_kurtosis(const std::vector& b, + const std::vector& p) { + Lines k = f_moment(b, p, 4, true); + for (double& v : k[0]) + if (fin(v)) v -= 3; + return k; +} +static Lines f_zscore(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 20); + Series c = closes(b); + return {combine(combine(c, sma_s(c, n), '-'), stdev_s(c, n), '/')}; +} +struct LinReg { + Series slope, intercept, value; +}; +static LinReg linreg(const Series& in, int n) { + LinReg r; + r.slope.assign(in.size(), nanv()); + r.intercept.assign(in.size(), nanv()); + r.value.assign(in.size(), nanv()); + for (size_t i = (size_t)(n - 1); i < in.size(); ++i) { + double sx = 0, sy = 0, sxx = 0, sxy = 0; + bool ok = true; + for (int j = 0; j < n; ++j) { + double x = j, y = in[i - n + 1 + j]; + if (!fin(y)) ok = false; + sx += x; sy += y; sxx += x * x; sxy += x * y; + } + if (!ok) continue; + double den = n * sxx - sx * sx; + if (den == 0) continue; + r.slope[i] = (n * sxy - sx * sy) / den; + r.intercept[i] = (sy - r.slope[i] * sx) / n; + r.value[i] = r.intercept[i] + r.slope[i] * (n - 1); + } + return r; +} +static Lines f_lr_slope(const std::vector& b, + const std::vector& p) { + return {linreg(closes(b), pi(p, 0, 20)).slope}; +} +static Lines f_lr_intercept(const std::vector& b, + const std::vector& p) { + return {linreg(closes(b), pi(p, 0, 20)).intercept}; +} +static Lines f_lr_angle(const std::vector& b, + const std::vector& p) { + Series s = linreg(closes(b), pi(p, 0, 20)).slope; + for (double& v : s) + if (fin(v)) v = std::atan(v) * 180 / M_PI; + return {s}; +} +static Lines f_r_squared(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 20); + Series c = closes(b), idx(c.size()); + for (size_t i = 0; i < c.size(); ++i) idx[i] = (double)i; + // Rolling correlation of close vs index, squared. + Series out(c.size(), nanv()); + for (size_t i = (size_t)(n - 1); i < c.size(); ++i) { + double mx = 0, my = 0; + for (int j = 0; j < n; ++j) { + mx += idx[i - j]; + my += c[i - j]; + } + mx /= n; my /= n; + double sxy = 0, sxx = 0, syy = 0; + for (int j = 0; j < n; ++j) { + double dx = idx[i - j] - mx, dy = c[i - j] - my; + sxy += dx * dy; sxx += dx * dx; syy += dy * dy; + } + if (sxx > 0 && syy > 0) out[i] = (sxy * sxy) / (sxx * syy); + } + return {out}; +} +static Lines f_tsf(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 20); + LinReg r = linreg(closes(b), n); + Series out(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (fin(r.intercept[i])) out[i] = r.intercept[i] + r.slope[i] * n; + return {out}; +} +static Lines f_forecast_osc(const std::vector& b, + const std::vector& p) { + Series tsf = f_tsf(b, p)[0]; + Series c = closes(b), out(c.size(), nanv()); + for (size_t i = 1; i < c.size(); ++i) + if (fin(tsf[i - 1]) && c[i] != 0) + out[i] = (c[i] - tsf[i - 1]) / c[i] * 100; + return {out}; +} + +// ============================================================ Ehlers / Hilbert + +struct HilbertData { + Series period, phase, inphase, quadrature; +}; +// Homodyne discriminator (Ehlers): dominant cycle period + phase. +static HilbertData hilbert(const Series& price) { + size_t N = price.size(); + HilbertData r; + r.period.assign(N, nanv()); + r.phase.assign(N, nanv()); + r.inphase.assign(N, nanv()); + r.quadrature.assign(N, nanv()); + std::vector smooth(N, 0), detr(N, 0), q1(N, 0), i1(N, 0), + ji(N, 0), jq(N, 0), re(N, 0), im(N, 0); + auto at = [&](const std::vector& v, long i) { + if (i < 0) return v.empty() ? 0.0 : v[0]; + if (i >= (long)v.size()) return v.back(); + return v[(size_t)i]; + }; + double period = 20, phase = 0; + for (size_t i = 0; i < N; ++i) { + long ii = (long)i; + smooth[i] = (4 * price[i] + 3 * at(price, ii - 1) + + 2 * at(price, ii - 2) + at(price, ii - 3)) / + 10; + double adj = 0.075 * period + 0.54; + detr[i] = (0.0962 * smooth[i] + 0.5769 * at(smooth, ii - 2) - + 0.5769 * at(smooth, ii - 4) - 0.0962 * at(smooth, ii - 6)) * + adj; + q1[i] = (0.0962 * detr[i] + 0.5769 * at(detr, ii - 2) - + 0.5769 * at(detr, ii - 4) - 0.0962 * at(detr, ii - 6)) * + adj; + i1[i] = at(detr, ii - 3); + ji[i] = (0.0962 * i1[i] + 0.5769 * at(i1, ii - 2) - + 0.5769 * at(i1, ii - 4) - 0.0962 * at(i1, ii - 6)) * + adj; + jq[i] = (0.0962 * q1[i] + 0.5769 * at(q1, ii - 2) - + 0.5769 * at(q1, ii - 4) - 0.0962 * at(q1, ii - 6)) * + adj; + double re_raw = i1[i] * ji[i] + q1[i] * jq[i]; + double im_raw = i1[i] * jq[i] - q1[i] * ji[i]; + re[i] = 0.2 * re_raw + 0.8 * at(re, ii - 1); + im[i] = 0.2 * im_raw + 0.8 * at(im, ii - 1); + if (im[i] != 0 && re[i] != 0) { + double p = 2 * M_PI / std::atan(im[i] / re[i]); + p = std::min(p, 1.5 * period); + p = std::max(p, 0.67 * period); + p = std::min(50.0, std::max(6.0, p)); + period = 0.2 * p + 0.8 * period; + } + if (i > 6) { + r.period[i] = period; + r.inphase[i] = i1[i]; + r.quadrature[i] = q1[i]; + double dp = i1[i] != 0 ? std::atan(q1[i] / i1[i]) * 180 / M_PI : 0; + phase += dp; + while (phase >= 360) phase -= 360; + while (phase < 0) phase += 360; + r.phase[i] = phase; + } + } + return r; +} +static Lines f_ht_period(const std::vector& b, + const std::vector&) { + return {hilbert(closes(b)).period}; +} +static Lines f_ht_phase(const std::vector& b, + const std::vector&) { + return {hilbert(closes(b)).phase}; +} +static Lines f_ht_phasor(const std::vector& b, + const std::vector&) { + HilbertData h = hilbert(closes(b)); + return {h.inphase, h.quadrature}; +} +static Lines f_ht_sine(const std::vector& b, + const std::vector&) { + HilbertData h = hilbert(closes(b)); + Series s1(b.size(), nanv()), s2(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (fin(h.phase[i])) { + double rad = h.phase[i] * M_PI / 180; + s1[i] = std::sin(rad); + s2[i] = std::sin(rad + M_PI / 4); + } + return {s1, s2}; +} +static Series instantaneous_trendline(const Series& price) { + Series out(price.size(), nanv()); + double alpha = 0.07; + for (size_t i = 0; i < price.size(); ++i) { + double p1 = i > 0 ? price[i - 1] : price[i]; + double p2 = i > 1 ? price[i - 2] : price[i]; + double t1 = i > 0 && fin(out[i - 1]) ? out[i - 1] : price[i]; + double t2 = i > 1 && fin(out[i - 2]) ? out[i - 2] : price[i]; + out[i] = (alpha - alpha * alpha / 4) * price[i] + + 0.5 * alpha * alpha * p1 - + (alpha - 0.75 * alpha * alpha) * p2 + + 2 * (1 - alpha) * t1 - (1 - alpha) * (1 - alpha) * t2; + } + return out; +} +static Lines f_ht_trendline(const std::vector& b, + const std::vector&) { + return {instantaneous_trendline(closes(b))}; +} +static Lines f_ht_trendmode(const std::vector& b, + const std::vector&) { + HilbertData h = hilbert(closes(b)); + Series it = instantaneous_trendline(closes(b)); + Series c = closes(b), out(c.size(), nanv()); + int age = 0; + for (size_t i = 1; i < c.size(); ++i) { + if (!fin(it[i])) continue; + if ((c[i] - it[i]) * (c[i - 1] - it[i - 1]) < 0) age = 0; + else ++age; + double per = fin(h.period[i]) ? h.period[i] : 20; + out[i] = age > per ? 1 : 0; // 1 = trending, 0 = cycling + } + return {out}; +} +static Lines f_mama(const std::vector& b, const std::vector& p, + bool fama) { + double fast_lim = pd(p, 0, 0.5), slow_lim = pd(p, 1, 0.05); + HilbertData h = hilbert(closes(b)); + Series c = closes(b), out(c.size(), nanv()); + double m = 0, f = 0; + bool started = false; + for (size_t i = 1; i < c.size(); ++i) { + if (!fin(h.phase[i]) || !fin(h.phase[i - 1])) continue; + double dp = h.phase[i - 1] - h.phase[i]; + if (dp < 0) dp += 360; + dp = std::max(1.0, dp); + double alpha = std::min(fast_lim, std::max(slow_lim, fast_lim / dp)); + if (!started) { + m = c[i]; + f = c[i]; + started = true; + } + m = alpha * c[i] + (1 - alpha) * m; + f = 0.5 * alpha * m + (1 - 0.5 * alpha) * f; + out[i] = fama ? f : m; + } + return {out}; +} +static Lines f_mama_line(const std::vector& b, + const std::vector& p) { + return f_mama(b, p, false); +} +static Lines f_fama_line(const std::vector& b, + const std::vector& p) { + return f_mama(b, p, true); +} +static Lines f_cyber_cycle(const std::vector& b, + const std::vector& p) { + double a = pd(p, 0, 0.07); + Series c = closes(b), cyc(c.size(), nanv()); + for (size_t i = 0; i < c.size(); ++i) { + auto sm = [&](long k) { + long i0 = (long)i - k; + double v0 = c[std::max(0L, i0)], v1 = c[std::max(0L, i0 - 1)], + v2 = c[std::max(0L, i0 - 2)], v3 = c[std::max(0L, i0 - 3)]; + return (v0 + 2 * v1 + 2 * v2 + v3) / 6; + }; + double s0 = sm(0), s1 = sm(1), s2 = sm(2); + double c1 = i > 0 && fin(cyc[i - 1]) ? cyc[i - 1] : 0; + double c2 = i > 1 && fin(cyc[i - 2]) ? cyc[i - 2] : 0; + double k = 1 - 0.5 * a; + cyc[i] = k * k * (s0 - 2 * s1 + s2) + 2 * (1 - a) * c1 - + (1 - a) * (1 - a) * c2; + } + return {cyc, shift_s(cyc, 1)}; +} +static Lines f_cg(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 10); + Series c = closes(b), out(c.size(), nanv()); + for (size_t i = (size_t)(n - 1); i < c.size(); ++i) { + double num = 0, den = 0; + for (int j = 0; j < n; ++j) { + num += (j + 1) * c[i - j]; + den += c[i - j]; + } + if (den != 0) out[i] = -num / den + (n + 1) / 2.0; + } + return {out, shift_s(out, 1)}; +} +static Lines f_ehlers_stoch(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 14); + Series sm = super_smoother_s(closes(b), 10); + Series mx = rolling_max(sm, n), mn = rolling_min(sm, n); + Series st(sm.size(), nanv()); + for (size_t i = 0; i < sm.size(); ++i) + if (fin(mx[i]) && fin(mn[i]) && mx[i] > mn[i]) + st[i] = (sm[i] - mn[i]) / (mx[i] - mn[i]); + Series s1 = super_smoother_s(st, 5); + return {s1, super_smoother_s(s1, 5)}; +} +static Series highpass_s(const Series& in, int n) { + Series out(in.size(), nanv()); + double w = 0.707 * 2 * M_PI / n; + double a = (std::cos(w) + std::sin(w) - 1) / std::cos(w); + for (size_t i = 0; i < in.size(); ++i) { + double x1 = i > 0 ? in[i - 1] : in[i], x2 = i > 1 ? in[i - 2] : in[i]; + double h1 = i > 0 && fin(out[i - 1]) ? out[i - 1] : 0; + double h2 = i > 1 && fin(out[i - 2]) ? out[i - 2] : 0; + double k = 1 - a / 2; + out[i] = k * k * (in[i] - 2 * x1 + x2) + 2 * (1 - a) * h1 - + (1 - a) * (1 - a) * h2; + } + return out; +} +static Lines f_roofing(const std::vector& b, + const std::vector& p) { + (void)p; + return {super_smoother_s(highpass_s(closes(b), 48), 10)}; +} +static Lines f_bandpass(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 20); + double bw = pd(p, 1, 0.3); + Series c = closes(b), out(c.size(), nanv()); + double beta = std::cos(2 * M_PI / n); + double gamma = 1 / std::cos(2 * M_PI * bw / n); + double alpha = gamma - std::sqrt(gamma * gamma - 1); + for (size_t i = 0; i < c.size(); ++i) { + double x2 = i > 1 ? c[i - 2] : c[i]; + double b1 = i > 0 && fin(out[i - 1]) ? out[i - 1] : 0; + double b2 = i > 1 && fin(out[i - 2]) ? out[i - 2] : 0; + out[i] = 0.5 * (1 - alpha) * (c[i] - x2) + + beta * (1 + alpha) * b1 - alpha * b2; + } + return {out}; +} +static Lines f_highpass(const std::vector& b, + const std::vector& p) { + return {highpass_s(closes(b), pi(p, 0, 48))}; +} +static Lines f_squeeze(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 20); + Series c = closes(b); + Series mid = scale_s(combine(rolling_max(highs(b), n), + rolling_min(lows(b), n), '+'), + 0.5); + Series d = combine(c, scale_s(combine(mid, sma_s(c, n), '+'), 0.5), '-'); + LinReg r = linreg(d, n); + return {r.value}; +} +static Lines f_wavetrend(const std::vector& b, + const std::vector& p) { + (void)p; + Series ap = hlc3(b); + Series esa = ema_s(ap, 10); + Series d = ema_s(combine(ap, esa, '-'), 10); + Series absd(b.size(), nanv()); + Series diff = combine(ap, esa, '-'); + for (size_t i = 0; i < b.size(); ++i) + if (fin(diff[i])) absd[i] = std::fabs(diff[i]); + Series dd = ema_s(absd, 10); + Series ci(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (fin(dd[i]) && dd[i] > 0 && fin(d[i])) + ci[i] = d[i] / (0.015 * dd[i]); + Series wt1 = ema_s(ci, 21); + return {wt1, sma_s(wt1, 4)}; +} +static Lines f_pmo(const std::vector& b, const std::vector& p) { + (void)p; + Series roc1(b.size(), nanv()); + Series c = closes(b); + for (size_t i = 1; i < c.size(); ++i) + if (c[i - 1] != 0) roc1[i] = (c[i] / c[i - 1] - 1) * 100; + Series s1 = ema_s(roc1, 35); + Series s2 = ema_s(scale_s(s1, 10), 20); + return {s2, ema_s(s2, 10)}; +} + +static void defs_volatility(std::vector& v) { + auto O = PlotKind::Overlay, P = PlotKind::Pane; + auto L = Style::Lines, B = Style::Bars; + v.push_back({"ATR", P, L, 0, "14", f_atr}); + v.push_back({"NATR", P, L, 1, "14", f_natr}); + v.push_back({"True Range", P, L, 2, "", f_tr}); + v.push_back({"Standard Deviation", P, L, 3, "20", f_stddev}); + v.push_back({"Variance", P, L, 4, "20", f_variance}); + v.push_back({"Historical Volatility", P, L, 5, "20", f_hist_vol}); + v.push_back({"Chaikin Volatility", P, L, 6, "10,10", f_chaikin_vol}); + v.push_back({"Ulcer Index", P, L, 7, "14", f_ulcer}); + v.push_back({"Choppiness Index", P, L, 8, "14", f_choppiness}); + v.push_back({"Relative Volatility Index", P, L, 9, "10", f_rvol_idx}); + v.push_back({"Parkinson Volatility", P, L, 10, "20", f_parkinson}); + v.push_back({"Garman-Klass Volatility", P, L, 11, "20", f_garman_klass}); + v.push_back({"Rogers-Satchell Volatility", P, L, 12, "20", f_rogers_satchell}); + v.push_back({"Yang-Zhang Volatility", P, L, 13, "20", f_yang_zhang}); + v.push_back({"Market Thermometer", P, L, 0, "22", f_thermometer}); + v.push_back({"Standard Error", P, L, 1, "20", f_std_error}); + v.push_back({"Mean Deviation", P, L, 2, "20", f_mean_dev}); + v.push_back({"Skewness", P, L, 3, "20", f_skewness}); + v.push_back({"Kurtosis", P, L, 4, "20", f_kurtosis}); + v.push_back({"Z-Score", P, L, 5, "20", f_zscore}); + v.push_back({"Linear Regression Slope", P, L, 6, "20", f_lr_slope}); + v.push_back({"Linear Regression Intercept", P, L, 7, "20", f_lr_intercept}); + v.push_back({"Linear Regression Angle", P, L, 8, "20", f_lr_angle}); + v.push_back({"R-Squared", P, L, 9, "20", f_r_squared}); + v.push_back({"TSF", O, L, 10, "20", f_tsf}); + v.push_back({"Forecast Oscillator", P, L, 11, "20", f_forecast_osc}); + v.push_back({"HT DC Period", P, L, 12, "", f_ht_period}); + v.push_back({"HT DC Phase", P, L, 13, "", f_ht_phase}); + v.push_back({"HT Phasor", P, L, 0, "", f_ht_phasor}); + v.push_back({"HT Sine", P, L, 1, "", f_ht_sine}); + v.push_back({"HT Trend Mode", P, L, 2, "", f_ht_trendmode}); + v.push_back({"HT Trendline", O, L, 3, "", f_ht_trendline}); + v.push_back({"MAMA", O, L, 4, "0.5,0.05", f_mama_line}); + v.push_back({"FAMA", O, L, 5, "0.5,0.05", f_fama_line}); + v.push_back({"Cyber Cycle", P, L, 6, "0.07", f_cyber_cycle}); + v.push_back({"CG Oscillator", P, L, 7, "10", f_cg}); + v.push_back({"Ehlers Stochastic", P, L, 8, "14", f_ehlers_stoch}); + v.push_back({"Roofing Filter", P, L, 9, "", f_roofing}); + v.push_back({"Band-Pass Filter", P, L, 10, "20,0.3", f_bandpass}); + v.push_back({"High-Pass Filter", P, L, 11, "48", f_highpass}); + v.push_back({"Squeeze Momentum", P, B, 12, "20", f_squeeze}); + v.push_back({"WaveTrend", P, L, 13, "", f_wavetrend}); + v.push_back({"PMO", P, L, 0, "", f_pmo}); +} + +// ============================================================ candlestick patterns +// TA-Lib's 61 patterns plus five extras. Each returns markers: +// +1 bullish (triangle under the bar), -1 bearish (triangle above). + +struct PC { + double o, h, l, c, body, range, upper, lower; + bool bull; +}; +static std::vector pcs_of(const std::vector& b) { + std::vector v(b.size()); + for (size_t i = 0; i < b.size(); ++i) { + PC& p = v[i]; + p.o = b[i].open; p.h = b[i].high; p.l = b[i].low; p.c = b[i].close; + p.body = std::fabs(p.c - p.o); + p.range = p.h - p.l; + p.upper = p.h - std::max(p.o, p.c); + p.lower = std::min(p.o, p.c) - p.l; + p.bull = p.c >= p.o; + } + return v; +} +static double avg_before(const std::vector& v, size_t i, int n, + bool body) { + double s = 0; + int cnt = 0; + for (size_t k = i >= (size_t)n ? i - n : 0; k < i; ++k) { + s += body ? v[k].body : v[k].range; + ++cnt; + } + return cnt ? s / cnt : 0; +} +static Series marks(const std::vector& b) { + return Series(b.size(), 0.0); +} +static bool is_doji(const PC& p) { return p.range > 0 && p.body <= 0.1 * p.range; } +static bool long_body(const std::vector& v, size_t i) { + return v[i].body > avg_before(v, i, 10, true) && v[i].body > 0; +} +static bool small_body(const std::vector& v, size_t i) { + return v[i].body < avg_before(v, i, 10, true); +} + +static Lines pat_doji(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 0; i < v.size(); ++i) + if (is_doji(v[i])) m[i] = 1; + return {m}; +} +static Lines pat_dragonfly(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 0; i < v.size(); ++i) + if (is_doji(v[i]) && v[i].lower > 3 * v[i].body && + v[i].upper < v[i].lower * 0.25) + m[i] = 1; + return {m}; +} +static Lines pat_gravestone(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 0; i < v.size(); ++i) + if (is_doji(v[i]) && v[i].upper > 3 * v[i].body && + v[i].lower < v[i].upper * 0.25) + m[i] = -1; + return {m}; +} +static Lines pat_longlegged(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 0; i < v.size(); ++i) + if (is_doji(v[i]) && v[i].upper > v[i].range * 0.3 && + v[i].lower > v[i].range * 0.3) + m[i] = 1; + return {m}; +} +static Lines pat_rickshaw(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 0; i < v.size(); ++i) + if (is_doji(v[i]) && v[i].upper > v[i].range * 0.35 && + v[i].lower > v[i].range * 0.35) + m[i] = 1; + return {m}; +} +static Lines pat_highwave(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) + if (small_body(v, i) && v[i].upper > v[i].body * 2 && + v[i].lower > v[i].body * 2) + m[i] = 1; + return {m}; +} +static Lines pat_spinningtop(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) + if (small_body(v, i) && v[i].upper > v[i].body && + v[i].lower > v[i].body && !is_doji(v[i])) + m[i] = 1; + return {m}; +} +static Lines pat_hammer(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) + if (small_body(v, i) && v[i].lower > 2 * v[i].body && + v[i].upper < v[i].body && v[i - 1].c < v[i - 1].o + v[i - 1].body) + m[i] = 1; + return {m}; +} +static Lines pat_takuri(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 0; i < v.size(); ++i) + if (small_body(v, i) && v[i].body > 0 && + v[i].lower > 3 * avg_before(v, i, 10, false) * 0.3 && + v[i].upper < v[i].body) + m[i] = 1; + return {m}; +} +static Lines pat_inv_hammer(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 0; i < v.size(); ++i) + if (small_body(v, i) && v[i].upper > 2 * v[i].body && + v[i].lower < v[i].body) + m[i] = 1; + return {m}; +} +static Lines pat_shooting_star(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) + if (small_body(v, i) && v[i].upper > 2 * v[i].body && + v[i].lower < v[i].body && v[i].l > v[i - 1].h) + m[i] = -1; + return {m}; +} +static Lines pat_hanging_man(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) + if (small_body(v, i) && v[i].lower > 2 * v[i].body && + v[i].upper < v[i].body && v[i - 1].bull) + m[i] = -1; + return {m}; +} +static Lines pat_engulfing(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) { + const PC& a = v[i - 1]; + const PC& c = v[i]; + if (!a.bull && c.bull && c.c >= a.o && c.o <= a.c) m[i] = 1; + if (a.bull && !c.bull && c.o >= a.c && c.c <= a.o) m[i] = -1; + } + return {m}; +} +static Lines pat_harami(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) { + const PC& a = v[i - 1]; + const PC& c = v[i]; + if (!long_body(v, i - 1)) continue; + bool inside = std::max(c.o, c.c) <= std::max(a.o, a.c) && + std::min(c.o, c.c) >= std::min(a.o, a.c); + if (inside) m[i] = a.bull ? -1 : 1; + } + return {m}; +} +static Lines pat_harami_cross(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) { + const PC& a = v[i - 1]; + const PC& c = v[i]; + if (!long_body(v, i - 1) || !is_doji(c)) continue; + bool inside = std::max(c.o, c.c) <= std::max(a.o, a.c) && + std::min(c.o, c.c) >= std::min(a.o, a.c); + if (inside) m[i] = a.bull ? -1 : 1; + } + return {m}; +} +static Lines pat_homing_pigeon(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) { + const PC& a = v[i - 1]; + const PC& c = v[i]; + if (!a.bull && !c.bull && c.o < a.o && c.c > a.c) m[i] = 1; + } + return {m}; +} +static Lines pat_piercing(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) { + const PC& a = v[i - 1]; + const PC& c = v[i]; + if (!a.bull && long_body(v, i - 1) && c.bull && c.o < a.l && + c.c > (a.o + a.c) / 2 && c.c < a.o) + m[i] = 1; + } + return {m}; +} +static Lines pat_dark_cloud(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) { + const PC& a = v[i - 1]; + const PC& c = v[i]; + if (a.bull && long_body(v, i - 1) && !c.bull && c.o > a.h && + c.c < (a.o + a.c) / 2 && c.c > a.o) + m[i] = -1; + } + return {m}; +} +static Lines pat_thrusting(const std::vector& b, const std::vector&) { + // Long white candle, then a black candle opening above its high and + // closing into its body but NOT past the midpoint (that would be + // Dark Cloud Cover). + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) { + const PC& a = v[i - 1]; + const PC& c = v[i]; + if (a.bull && long_body(v, i - 1) && !c.bull && c.o > a.h && + c.c < a.c && c.c > (a.o + a.c) / 2) + m[i] = -1; + } + return {m}; +} +static Lines pat_in_neck(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) { + const PC& a = v[i - 1]; + const PC& c = v[i]; + if (!a.bull && long_body(v, i - 1) && c.bull && small_body(v, i) && + std::fabs(c.c - a.l) <= a.range * 0.05) + m[i] = -1; + } + return {m}; +} +static Lines pat_on_neck(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) { + const PC& a = v[i - 1]; + const PC& c = v[i]; + if (!a.bull && long_body(v, i - 1) && c.bull && small_body(v, i) && + c.c > a.l && c.c <= a.l + a.range * 0.1) + m[i] = -1; + } + return {m}; +} +static Lines pat_counterattack(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) { + const PC& a = v[i - 1]; + const PC& c = v[i]; + if (long_body(v, i - 1) && long_body(v, i) && a.bull != c.bull && + std::fabs(c.c - a.c) <= a.range * 0.05) + m[i] = c.bull ? 1 : -1; + } + return {m}; +} +static Lines pat_separating(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) { + const PC& a = v[i - 1]; + const PC& c = v[i]; + if (a.bull != c.bull && std::fabs(c.o - a.o) <= a.range * 0.05 && + long_body(v, i)) + m[i] = c.bull ? 1 : -1; + } + return {m}; +} +static Lines pat_matching_low(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) { + const PC& a = v[i - 1]; + const PC& c = v[i]; + if (!a.bull && !c.bull && + std::fabs(c.c - a.c) <= a.range * 0.02) + m[i] = 1; + } + return {m}; +} +static Lines pat_belt_hold(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 0; i < v.size(); ++i) { + if (!long_body(v, i)) continue; + if (v[i].bull && v[i].lower < v[i].body * 0.1) m[i] = 1; + if (!v[i].bull && v[i].upper < v[i].body * 0.1) m[i] = -1; + } + return {m}; +} +static Lines pat_marubozu(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) + if (long_body(v, i) && v[i].upper < v[i].body * 0.1 && + v[i].lower < v[i].body * 0.1) + m[i] = v[i].bull ? 1 : -1; + return {m}; +} +static Lines pat_closing_marubozu(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) { + if (!long_body(v, i)) continue; + if (v[i].bull && v[i].upper < v[i].body * 0.1) m[i] = 1; + if (!v[i].bull && v[i].lower < v[i].body * 0.1) m[i] = -1; + } + return {m}; +} +static Lines pat_long_line(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) + if (long_body(v, i) && !is_doji(v[i])) m[i] = v[i].bull ? 1 : -1; + return {m}; +} +static Lines pat_short_line(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) + if (small_body(v, i) && v[i].range < avg_before(v, i, 10, false) && + !is_doji(v[i])) + m[i] = v[i].bull ? 1 : -1; + return {m}; +} +static Lines pat_doji_star(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) { + const PC& a = v[i - 1]; + const PC& c = v[i]; + if (!long_body(v, i - 1) || !is_doji(c)) continue; + if (a.bull && std::min(c.o, c.c) > a.c) m[i] = -1; + if (!a.bull && std::max(c.o, c.c) < a.c) m[i] = 1; + } + return {m}; +} +static Lines pat_morning_star(const std::vector& b, const std::vector&, bool doji) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& a = v[i - 2]; + const PC& s = v[i - 1]; + const PC& c = v[i]; + bool star_ok = doji ? is_doji(s) : small_body(v, i - 1); + if (!a.bull && long_body(v, i - 2) && star_ok && c.bull && + long_body(v, i) && std::max(s.o, s.c) < a.c && + c.c > (a.o + a.c) / 2) + m[i] = 1; + } + return {m}; +} +static Lines pat_evening_star(const std::vector& b, const std::vector&, bool doji) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& a = v[i - 2]; + const PC& s = v[i - 1]; + const PC& c = v[i]; + bool star_ok = doji ? is_doji(s) : small_body(v, i - 1); + if (a.bull && long_body(v, i - 2) && star_ok && !c.bull && + long_body(v, i) && std::min(s.o, s.c) > a.c && + c.c < (a.o + a.c) / 2) + m[i] = -1; + } + return {m}; +} +static Lines pat_morning_star_w(const std::vector& b, const std::vector& p) { + return pat_morning_star(b, p, false); +} +static Lines pat_morning_doji_star_w(const std::vector& b, const std::vector& p) { + return pat_morning_star(b, p, true); +} +static Lines pat_evening_star_w(const std::vector& b, const std::vector& p) { + return pat_evening_star(b, p, false); +} +static Lines pat_evening_doji_star_w(const std::vector& b, const std::vector& p) { + return pat_evening_star(b, p, true); +} +static Lines pat_abandoned_baby(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& a = v[i - 2]; + const PC& s = v[i - 1]; + const PC& c = v[i]; + if (!is_doji(s)) continue; + if (!a.bull && c.bull && s.h < a.l && s.h < c.l) m[i] = 1; + if (a.bull && !c.bull && s.l > a.h && s.l > c.h) m[i] = -1; + } + return {m}; +} +static Lines pat_soldiers(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& a = v[i - 2]; + const PC& d = v[i - 1]; + const PC& c = v[i]; + if (a.bull && d.bull && c.bull && d.o > a.o && d.o < a.c && + c.o > d.o && c.o < d.c && c.c > d.c && d.c > a.c) + m[i] = 1; + } + return {m}; +} +static Lines pat_crows(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& a = v[i - 2]; + const PC& d = v[i - 1]; + const PC& c = v[i]; + if (!a.bull && !d.bull && !c.bull && d.o < a.o && d.o > a.c && + c.o < d.o && c.o > d.c && c.c < d.c && d.c < a.c) + m[i] = -1; + } + return {m}; +} +static Lines pat_identical_crows(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& a = v[i - 2]; + const PC& d = v[i - 1]; + const PC& c = v[i]; + if (!a.bull && !d.bull && !c.bull && + std::fabs(d.o - a.c) <= a.range * 0.02 && + std::fabs(c.o - d.c) <= d.range * 0.02) + m[i] = -1; + } + return {m}; +} +static Lines pat_two_crows(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& a = v[i - 2]; + const PC& d = v[i - 1]; + const PC& c = v[i]; + if (a.bull && long_body(v, i - 2) && !d.bull && !c.bull && + d.o > a.c && c.o > d.c && c.c < d.c && c.c > a.o && c.c < a.c) + m[i] = -1; + } + return {m}; +} +static Lines pat_upside_gap_crows(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& a = v[i - 2]; + const PC& d = v[i - 1]; + const PC& c = v[i]; + if (a.bull && !d.bull && !c.bull && d.l > a.h && c.o > d.c && + c.c < d.c && c.c > a.h) + m[i] = -1; + } + return {m}; +} +static Lines pat_advance_block(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& a = v[i - 2]; + const PC& d = v[i - 1]; + const PC& c = v[i]; + if (a.bull && d.bull && c.bull && long_body(v, i - 2) && + d.body < a.body && c.body < d.body && d.upper > d.body && + c.upper > c.body) + m[i] = -1; + } + return {m}; +} +static Lines pat_stalled(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& a = v[i - 2]; + const PC& d = v[i - 1]; + const PC& c = v[i]; + if (a.bull && d.bull && long_body(v, i - 2) && + (is_doji(c) || small_body(v, i)) && c.o > d.o && c.o < d.c) + m[i] = -1; + } + return {m}; +} +static Lines pat_three_inside(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& a = v[i - 2]; + const PC& s = v[i - 1]; + const PC& c = v[i]; + bool inside = std::max(s.o, s.c) <= std::max(a.o, a.c) && + std::min(s.o, s.c) >= std::min(a.o, a.c); + if (!inside || !long_body(v, i - 2)) continue; + if (!a.bull && c.bull && c.c > a.o) m[i] = 1; + if (a.bull && !c.bull && c.c < a.o) m[i] = -1; + } + return {m}; +} +static Lines pat_three_outside(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& a = v[i - 2]; + const PC& e = v[i - 1]; + const PC& c = v[i]; + bool engulf = (!a.bull && e.bull && e.c >= a.o && e.o <= a.c) || + (a.bull && !e.bull && e.o >= a.c && e.c <= a.o); + if (!engulf) continue; + if (e.bull && c.bull && c.c > e.c) m[i] = 1; + if (!e.bull && !c.bull && c.c < e.c) m[i] = -1; + } + return {m}; +} +static Lines pat_line_strike(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 3; i < v.size(); ++i) { + const PC& a = v[i - 3]; + const PC& d = v[i - 2]; + const PC& e = v[i - 1]; + const PC& c = v[i]; + if (a.bull && d.bull && e.bull && !c.bull && c.o > e.c && + c.c < a.o) + m[i] = -1; + if (!a.bull && !d.bull && !e.bull && c.bull && c.o < e.c && + c.c > a.o) + m[i] = 1; + } + return {m}; +} +static Lines pat_stars_south(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& a = v[i - 2]; + const PC& d = v[i - 1]; + const PC& c = v[i]; + if (!a.bull && !d.bull && !c.bull && long_body(v, i - 2) && + d.body < a.body && c.body < d.body && d.lower > d.body && + c.lower > c.body && c.l > d.l) + m[i] = 1; + } + return {m}; +} +static Lines pat_concealing_swallow(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 3; i < v.size(); ++i) { + const PC& a = v[i - 3]; + const PC& d = v[i - 2]; + const PC& e = v[i - 1]; + const PC& c = v[i]; + if (!a.bull && !d.bull && !e.bull && !c.bull && + a.upper < a.body * 0.1 && a.lower < a.body * 0.1 && + e.h > d.c && c.o > e.h && c.c < e.l) + m[i] = 1; + } + return {m}; +} +static Lines pat_ladder_bottom(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 4; i < v.size(); ++i) { + const PC& a = v[i - 4]; + const PC& d = v[i - 3]; + const PC& e = v[i - 2]; + const PC& f = v[i - 1]; + const PC& c = v[i]; + if (!a.bull && !d.bull && !e.bull && !f.bull && f.upper > f.body && + c.bull && c.o > f.o && c.c > f.h) + m[i] = 1; + } + return {m}; +} +static Lines pat_breakaway(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 4; i < v.size(); ++i) { + const PC& a = v[i - 4]; + const PC& c = v[i]; + bool mid_same = v[i - 3].bull == a.bull && v[i - 2].bull == a.bull && + v[i - 1].bull == a.bull; + if (!mid_same) continue; + if (!a.bull && c.bull && v[i - 3].h < a.l && c.c < a.c && + c.c > v[i - 3].l) + m[i] = 1; + if (a.bull && !c.bull && v[i - 3].l > a.h && c.c > a.c && + c.c < v[i - 3].h) + m[i] = -1; + } + return {m}; +} +static Lines pat_mat_hold(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 4; i < v.size(); ++i) { + const PC& a = v[i - 4]; + const PC& c = v[i]; + bool smalls = small_body(v, i - 3) && small_body(v, i - 2) && + small_body(v, i - 1); + if (!smalls || !long_body(v, i - 4) || !long_body(v, i)) continue; + if (a.bull && c.bull && v[i - 3].o > a.c && c.c > a.c) m[i] = 1; + if (!a.bull && !c.bull && v[i - 3].o < a.c && c.c < a.c) m[i] = -1; + } + return {m}; +} +static Lines pat_risefall_methods(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 4; i < v.size(); ++i) { + const PC& a = v[i - 4]; + const PC& c = v[i]; + bool smalls = small_body(v, i - 3) && small_body(v, i - 2) && + small_body(v, i - 1); + bool within = std::max(v[i - 2].h, v[i - 1].h) <= a.h && + std::min(v[i - 2].l, v[i - 1].l) >= a.l; + if (!smalls || !within || !long_body(v, i - 4) || + !long_body(v, i)) + continue; + if (a.bull && c.bull && !v[i - 2].bull && c.c > a.c) m[i] = 1; + if (!a.bull && !c.bull && v[i - 2].bull && c.c < a.c) m[i] = -1; + } + return {m}; +} +static Lines pat_kicking(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) { + const PC& a = v[i - 1]; + const PC& c = v[i]; + bool maru_a = a.upper < a.body * 0.1 && a.lower < a.body * 0.1; + bool maru_c = c.upper < c.body * 0.1 && c.lower < c.body * 0.1; + if (!maru_a || !maru_c || a.bull == c.bull) continue; + bool gap = c.bull ? c.l > a.h : c.h < a.l; + if (gap) m[i] = c.bull ? 1 : -1; + } + return {m}; +} +static Lines pat_kicking_length(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) { + const PC& a = v[i - 1]; + const PC& c = v[i]; + bool maru_a = a.upper < a.body * 0.1 && a.lower < a.body * 0.1; + bool maru_c = c.upper < c.body * 0.1 && c.lower < c.body * 0.1; + if (!maru_a || !maru_c || a.bull == c.bull) continue; + bool gap = c.bull ? c.l > a.h : c.h < a.l; + if (gap) m[i] = c.body >= a.body ? (c.bull ? 1 : -1) + : (a.bull ? 1 : -1); + } + return {m}; +} +static Lines pat_tasuki_gap(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& a = v[i - 2]; + const PC& d = v[i - 1]; + const PC& c = v[i]; + if (a.bull && d.bull && !c.bull && d.l > a.h && c.o < d.c && + c.o > d.o && c.c > a.h && c.c < d.l) + m[i] = 1; + if (!a.bull && !d.bull && c.bull && d.h < a.l && c.o > d.c && + c.o < d.o && c.c < a.l && c.c > d.h) + m[i] = -1; + } + return {m}; +} +static Lines pat_gap_side_white(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& a = v[i - 2]; + const PC& d = v[i - 1]; + const PC& c = v[i]; + if (d.bull && c.bull && d.l > a.h && + std::fabs(c.o - d.o) <= d.range * 0.1) + m[i] = 1; + if (!d.bull && !c.bull && d.h < a.l && + std::fabs(c.o - d.o) <= d.range * 0.1) + m[i] = -1; + } + return {m}; +} +static Lines pat_xside_gap_methods(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& d = v[i - 1]; + const PC& c = v[i]; + const PC& a = v[i - 2]; + if (d.bull && !c.bull && d.l > a.h && c.o > d.c && c.c < d.o) + m[i] = -1; + if (!d.bull && c.bull && d.h < a.l && c.o < d.c && c.c > d.o) + m[i] = 1; + } + return {m}; +} +static Lines pat_hikkake(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& a = v[i - 2]; + const PC& in = v[i - 1]; + const PC& c = v[i]; + bool inside = in.h < a.h && in.l > a.l; + if (!inside) continue; + if (c.c < in.l) m[i] = 1; // failed breakdown -> bullish hikkake + if (c.c > in.h) m[i] = -1; // failed breakout -> bearish + } + return {m}; +} +static Lines pat_hikkake_mod(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 3; i < v.size(); ++i) { + const PC& a = v[i - 3]; + const PC& in = v[i - 2]; + const PC& f = v[i - 1]; + const PC& c = v[i]; + bool inside = in.h < a.h && in.l > a.l; + if (!inside) continue; + if (f.c < in.l && c.c > in.l && c.c < in.h) m[i] = 1; + if (f.c > in.h && c.c < in.h && c.c > in.l) m[i] = -1; + } + return {m}; +} +static Lines pat_tristar(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + if (is_doji(v[i - 2]) && is_doji(v[i - 1]) && is_doji(v[i])) { + if (v[i - 1].l > v[i - 2].h && v[i - 1].l > v[i].h) m[i] = -1; + else if (v[i - 1].h < v[i - 2].l && v[i - 1].h < v[i].l) + m[i] = 1; + else m[i] = 1; + } + } + return {m}; +} +static Lines pat_unique_river(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& a = v[i - 2]; + const PC& d = v[i - 1]; + const PC& c = v[i]; + if (!a.bull && long_body(v, i - 2) && small_body(v, i - 1) && + d.lower > d.body * 2 && c.bull && c.c > d.h) + m[i] = 1; + } + return {m}; +} +static Lines pat_stick_sandwich(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 2; i < v.size(); ++i) { + const PC& a = v[i - 2]; + const PC& d = v[i - 1]; + const PC& c = v[i]; + if (!a.bull && d.bull && !c.bull && + std::fabs(c.c - a.c) <= a.range * 0.02) + m[i] = 1; + } + return {m}; +} +// Extra patterns (beyond TA-Lib's 61). +static Lines pat_inside_bar(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) + if (v[i].h < v[i - 1].h && v[i].l > v[i - 1].l) + m[i] = v[i].bull ? 1 : -1; + return {m}; +} +static Lines pat_outside_bar(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) + if (v[i].h > v[i - 1].h && v[i].l < v[i - 1].l) + m[i] = v[i].bull ? 1 : -1; + return {m}; +} +static Lines pat_pin_bar(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 0; i < v.size(); ++i) { + if (v[i].range <= 0) continue; + if (v[i].lower > v[i].range * 0.6) m[i] = 1; + if (v[i].upper > v[i].range * 0.6) m[i] = -1; + } + return {m}; +} +static Lines pat_nr7(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 6; i < v.size(); ++i) { + bool narrowest = true; + for (int j = 1; j < 7; ++j) + if (v[i - j].range < v[i].range) narrowest = false; + if (narrowest) m[i] = 1; + } + return {m}; +} +static Lines pat_gap(const std::vector& b, const std::vector&) { + auto v = pcs_of(b); + Series m = marks(b); + for (size_t i = 1; i < v.size(); ++i) { + if (v[i].l > v[i - 1].h) m[i] = 1; + if (v[i].h < v[i - 1].l) m[i] = -1; + } + return {m}; +} + +static void defs_patterns(std::vector& v) { + auto M = PlotKind::Markers; + auto L = Style::Lines; + v.push_back({"Doji", M, L, 0, "", pat_doji}); + v.push_back({"Dragonfly Doji", M, L, 1, "", pat_dragonfly}); + v.push_back({"Gravestone Doji", M, L, 2, "", pat_gravestone}); + v.push_back({"Long Legged Doji", M, L, 3, "", pat_longlegged}); + v.push_back({"Rickshaw Man", M, L, 4, "", pat_rickshaw}); + v.push_back({"High Wave", M, L, 5, "", pat_highwave}); + v.push_back({"Spinning Top", M, L, 6, "", pat_spinningtop}); + v.push_back({"Hammer", M, L, 7, "", pat_hammer}); + v.push_back({"Takuri", M, L, 8, "", pat_takuri}); + v.push_back({"Inverted Hammer", M, L, 9, "", pat_inv_hammer}); + v.push_back({"Shooting Star", M, L, 10, "", pat_shooting_star}); + v.push_back({"Hanging Man", M, L, 11, "", pat_hanging_man}); + v.push_back({"Engulfing", M, L, 12, "", pat_engulfing}); + v.push_back({"Harami", M, L, 13, "", pat_harami}); + v.push_back({"Harami Cross", M, L, 0, "", pat_harami_cross}); + v.push_back({"Homing Pigeon", M, L, 1, "", pat_homing_pigeon}); + v.push_back({"Piercing Line", M, L, 2, "", pat_piercing}); + v.push_back({"Dark Cloud Cover", M, L, 3, "", pat_dark_cloud}); + v.push_back({"Thrusting", M, L, 4, "", pat_thrusting}); + v.push_back({"In-Neck", M, L, 5, "", pat_in_neck}); + v.push_back({"On-Neck", M, L, 6, "", pat_on_neck}); + v.push_back({"Counterattack", M, L, 7, "", pat_counterattack}); + v.push_back({"Separating Lines", M, L, 8, "", pat_separating}); + v.push_back({"Matching Low", M, L, 9, "", pat_matching_low}); + v.push_back({"Belt Hold", M, L, 10, "", pat_belt_hold}); + v.push_back({"Marubozu", M, L, 11, "", pat_marubozu}); + v.push_back({"Closing Marubozu", M, L, 12, "", pat_closing_marubozu}); + v.push_back({"Long Line", M, L, 13, "", pat_long_line}); + v.push_back({"Short Line", M, L, 0, "", pat_short_line}); + v.push_back({"Doji Star", M, L, 1, "", pat_doji_star}); + v.push_back({"Morning Star", M, L, 2, "", pat_morning_star_w}); + v.push_back({"Morning Doji Star", M, L, 3, "", pat_morning_doji_star_w}); + v.push_back({"Evening Star", M, L, 4, "", pat_evening_star_w}); + v.push_back({"Evening Doji Star", M, L, 5, "", pat_evening_doji_star_w}); + v.push_back({"Abandoned Baby", M, L, 6, "", pat_abandoned_baby}); + v.push_back({"Three White Soldiers", M, L, 7, "", pat_soldiers}); + v.push_back({"Three Black Crows", M, L, 8, "", pat_crows}); + v.push_back({"Identical Three Crows", M, L, 9, "", pat_identical_crows}); + v.push_back({"Two Crows", M, L, 10, "", pat_two_crows}); + v.push_back({"Upside Gap Two Crows", M, L, 11, "", pat_upside_gap_crows}); + v.push_back({"Advance Block", M, L, 12, "", pat_advance_block}); + v.push_back({"Stalled Pattern", M, L, 13, "", pat_stalled}); + v.push_back({"Three Inside", M, L, 0, "", pat_three_inside}); + v.push_back({"Three Outside", M, L, 1, "", pat_three_outside}); + v.push_back({"Three Line Strike", M, L, 2, "", pat_line_strike}); + v.push_back({"Three Stars In The South", M, L, 3, "", pat_stars_south}); + v.push_back({"Concealing Baby Swallow", M, L, 4, "", pat_concealing_swallow}); + v.push_back({"Ladder Bottom", M, L, 5, "", pat_ladder_bottom}); + v.push_back({"Breakaway", M, L, 6, "", pat_breakaway}); + v.push_back({"Mat Hold", M, L, 7, "", pat_mat_hold}); + v.push_back({"Rising/Falling Three Methods", M, L, 8, "", pat_risefall_methods}); + v.push_back({"Kicking", M, L, 9, "", pat_kicking}); + v.push_back({"Kicking By Length", M, L, 10, "", pat_kicking_length}); + v.push_back({"Tasuki Gap", M, L, 11, "", pat_tasuki_gap}); + v.push_back({"Gap Side Side White", M, L, 12, "", pat_gap_side_white}); + v.push_back({"X Side Gap Three Methods", M, L, 13, "", pat_xside_gap_methods}); + v.push_back({"Hikkake", M, L, 0, "", pat_hikkake}); + v.push_back({"Hikkake Modified", M, L, 1, "", pat_hikkake_mod}); + v.push_back({"Tristar", M, L, 2, "", pat_tristar}); + v.push_back({"Unique Three River", M, L, 3, "", pat_unique_river}); + v.push_back({"Stick Sandwich", M, L, 4, "", pat_stick_sandwich}); + v.push_back({"Inside Bar", M, L, 5, "", pat_inside_bar}); + v.push_back({"Outside Bar", M, L, 6, "", pat_outside_bar}); + v.push_back({"Pin Bar", M, L, 7, "", pat_pin_bar}); + v.push_back({"NR7", M, L, 8, "", pat_nr7}); + v.push_back({"Gap", M, L, 9, "", pat_gap}); +} + +// ============================================================ final entries + registry + +static Lines f_vhf(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 28); + Series hh = rolling_max(closes(b), n), ll = rolling_min(closes(b), n); + Series d = diff_s(closes(b)); + for (double& v : d) + if (fin(v)) v = std::fabs(v); + Series sum = rolling_sum(d, n); + Series out(b.size(), nanv()); + for (size_t i = 0; i < b.size(); ++i) + if (fin(hh[i]) && fin(sum[i]) && sum[i] > 0) + out[i] = (hh[i] - ll[i]) / sum[i]; + return {out}; +} +static Lines f_disparity(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 14); + Series c = closes(b), m = sma_s(c, n); + Series out(c.size(), nanv()); + for (size_t i = 0; i < c.size(); ++i) + if (fin(m[i]) && m[i] != 0) out[i] = (c[i] - m[i]) / m[i] * 100; + return {out}; +} +static Lines f_atr_trail(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 14); + double k = pd(p, 1, 3.0); + Series atr = atr_s(b, n), c = closes(b), out(c.size(), nanv()); + for (size_t i = 0; i < c.size(); ++i) { + if (!fin(atr[i])) continue; + double stop = c[i] - k * atr[i]; + out[i] = fin(out[i - 1 < c.size() ? i - 1 : 0]) && i > 0 + ? std::max(stop, out[i - 1]) + : stop; + if (c[i] < out[i]) out[i] = stop; // reset on close-through + } + return {out}; +} +static Lines f_pfe(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 14); + Series c = closes(b), out(c.size(), nanv()); + for (size_t i = (size_t)n; i < c.size(); ++i) { + double dist = 0; + for (int j = 0; j < n; ++j) { + double d = c[i - j] - c[i - j - 1]; + dist += std::sqrt(1 + d * d); + } + double direct = std::sqrt(std::pow(c[i] - c[i - n], 2) + n * n); + if (dist > 0) + out[i] = direct / dist * 100 * (c[i] >= c[i - n] ? 1 : -1); + } + return {out}; +} +static Lines f_efficiency(const std::vector& b, + const std::vector& p) { + int n = pi(p, 0, 10); + Series c = closes(b), d = diff_s(c); + for (double& v : d) + if (fin(v)) v = std::fabs(v); + Series vol = rolling_sum(d, n), out(c.size(), nanv()); + for (size_t i = (size_t)n; i < c.size(); ++i) + if (fin(vol[i]) && vol[i] > 0) + out[i] = std::fabs(c[i] - c[i - n]) / vol[i] * 100; + return {out}; +} +static Lines f_tii(const std::vector& b, const std::vector& p) { + int n = pi(p, 0, 30); + Series c = closes(b), m = sma_s(c, n); + Series dev(c.size(), nanv()); + for (size_t i = 0; i < c.size(); ++i) + if (fin(m[i])) dev[i] = c[i] - m[i]; + Series out(c.size(), nanv()); + for (size_t i = (size_t)(n - 1); i < c.size(); ++i) { + double pos = 0, neg = 0; + bool ok = true; + for (int j = 0; j < n; ++j) { + if (!fin(dev[i - j])) ok = false; + if (dev[i - j] > 0) pos += dev[i - j]; + else neg -= dev[i - j]; + } + if (ok && pos + neg > 0) out[i] = pos / (pos + neg) * 100; + } + return {out}; +} +static Lines f_rolling_sum(const std::vector& b, + const std::vector& p) { + return {rolling_sum(closes(b), pi(p, 0, 14))}; +} +static Lines f_highest_high(const std::vector& b, + const std::vector& p) { + return {rolling_max(highs(b), pi(p, 0, 20))}; +} +static Lines f_lowest_low(const std::vector& b, + const std::vector& p) { + return {rolling_min(lows(b), pi(p, 0, 20))}; +} + +static void defs_final(std::vector& v) { + auto O = PlotKind::Overlay, P = PlotKind::Pane; + auto L = Style::Lines; + v.push_back({"VHF", P, L, 0, "28", f_vhf}); + v.push_back({"Disparity Index", P, L, 1, "14", f_disparity}); + v.push_back({"ATR Trailing Stop", O, L, 3, "14,3", f_atr_trail}); + v.push_back({"Polarized Fractal Efficiency", P, L, 4, "14", f_pfe}); + v.push_back({"Efficiency Ratio", P, L, 5, "10", f_efficiency}); + v.push_back({"Trend Intensity Index", P, L, 6, "30", f_tii}); + v.push_back({"Rolling Sum", P, L, 7, "14", f_rolling_sum}); + v.push_back({"Highest High", O, L, 8, "20", f_highest_high}); + v.push_back({"Lowest Low", O, L, 9, "20", f_lowest_low}); +} + +// ------------------------------------------------------------- registry + +inline const std::vector& all_defs() { + static const std::vector defs = [] { + std::vector v; + defs_averages(v); + defs_bands(v); + defs_momentum(v); + defs_volume(v); + defs_volatility(v); + defs_patterns(v); + defs_final(v); + return v; + }(); + return defs; +} + +static std::string lower_str(std::string s) { + for (char& ch : s) ch = (char)std::tolower((unsigned char)ch); + return s; +} +static std::string trim_str(const std::string& s) { + size_t b = s.find_first_not_of(" \t"); + size_t e = s.find_last_not_of(" \t"); + return b == std::string::npos ? "" : s.substr(b, e - b + 1); +} + +inline const Def* find_def(const std::string& name) { + std::string want = lower_str(trim_str(name)); + for (const Def& d : all_defs()) + if (lower_str(d.name) == want) return &d; + return nullptr; +} + +inline std::vector indicator_names() { + std::vector out; + for (const Def& d : all_defs()) out.push_back(d.name); + return out; +} + +static std::vector parse_params(const std::string& s) { + std::vector out; + size_t pos = 0; + while (pos <= s.size()) { + size_t comma = s.find(',', pos); + std::string tok = + trim_str(s.substr(pos, comma == std::string::npos + ? std::string::npos + : comma - pos)); + if (!tok.empty()) out.push_back(std::atof(tok.c_str())); + if (comma == std::string::npos) break; + pos = comma + 1; + } + return out; +} + +inline std::unique_ptr make_indicator(const std::string& spec, + std::string* err) { + std::string name = trim_str(spec); + std::string param_str; + size_t open = spec.find('('); + if (open != std::string::npos) { + size_t close = spec.rfind(')'); + name = trim_str(spec.substr(0, open)); + if (close != std::string::npos && close > open) + param_str = spec.substr(open + 1, close - open - 1); + } + const Def* def = find_def(name); + if (!def) { + if (err) *err = "unknown indicator '" + name + "'"; + return nullptr; + } + std::vector params = parse_params(def->defaults); + std::vector given = parse_params(param_str); + for (size_t i = 0; i < given.size(); ++i) { + if (i < params.size()) + params[i] = given[i]; + else + params.push_back(given[i]); + } + return std::unique_ptr(new Indicator(def, params)); +} + +} // namespace fdi +)FDINDICATORS7f3a"; diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..413b9d5 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,2443 @@ +// FDesigner (C++/GTK) — Phase 2: property editor + bulk parameter ops. +// Main window loads from the Glade file ui/designer.ui via GtkBuilder. +// +// Phase 1: palette places real GTK widgets on a GtkFixed canvas; click to +// select, drag to move, Delete removes. +// Phase 2: the property pane edits the selected widget's properties +// (per-type tables in props.h, applied live), edits the window/root +// properties when nothing is selected, and offers the original's bulk +// operations: set a parameter on all / selected / filtered widgets, and +// clear a parameter everywhere. +// +// Headless check: FDESIGNER_SELFTEST=1 exercises both phases through the +// same functions the UI calls, prints SELFTEST PASS/FAIL. + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "model.h" +#include "props.h" +#include "drawings.h" +#include "generator.h" +#include "indicators.h" +#include "project_io.h" + +// ---------------------------------------------------------------- state + +// One OHLC bar (Massive aggregates format). +struct Candle { + double open = 0, high = 0, low = 0, close = 0, volume = 0; + long long time = 0; // window_start, ns +}; + +// Parse a Massive-format aggregates CSV (columns matched by header name: +// ticker,volume,open,close,high,low,window_start,transactions). +static std::vector parse_candles_csv(const std::string& path) { + std::vector out; + std::ifstream f(path); + if (!f) return out; + auto strip = [](std::string s) { + while (!s.empty() && + (s.back() == ' ' || s.back() == '\t' || s.back() == '\r' || + s.back() == '\n')) + s.pop_back(); + size_t b = s.find_first_not_of(" \t"); + return b == std::string::npos ? std::string() : s.substr(b); + }; + auto split = [&](const std::string& line) { + std::vector fields; + std::stringstream ss(line); + std::string field; + while (std::getline(ss, field, ',')) fields.push_back(strip(field)); + return fields; + }; + std::string header; + if (!std::getline(f, header)) return out; + auto cols = split(header); + auto index_of = [&](const char* name) { + for (size_t i = 0; i < cols.size(); ++i) + if (cols[i] == name) return (int)i; + return -1; + }; + int io = index_of("open"), ih = index_of("high"), il = index_of("low"), + ic = index_of("close"), it = index_of("window_start"), + iv = index_of("volume"); + if (io < 0 || ih < 0 || il < 0 || ic < 0) return out; + std::string line; + while (std::getline(f, line)) { + if (line.empty()) continue; + auto fields = split(line); + int need = std::max(std::max(io, ih), std::max(il, ic)); + if ((int)fields.size() <= need) continue; + Candle c; + c.open = std::atof(fields[io].c_str()); + c.high = std::atof(fields[ih].c_str()); + c.low = std::atof(fields[il].c_str()); + c.close = std::atof(fields[ic].c_str()); + if (iv >= 0 && (int)fields.size() > iv) + c.volume = std::atof(fields[iv].c_str()); + if (it >= 0 && (int)fields.size() > it) + c.time = std::atoll(fields[it].c_str()); + if (c.high == 0 && c.low == 0 && c.close == 0) continue; + out.push_back(c); + } + return out; +} + +// The ticker symbol from a Massive CSV's first data row ("" if none). +static std::string csv_ticker(const std::string& path) { + std::ifstream f(path); + if (!f) return ""; + std::string header, line; + if (!std::getline(f, header)) return ""; + int ik = -1, idx = 0; + std::stringstream hs(header); + std::string col; + while (std::getline(hs, col, ',')) { + if (col == "ticker") ik = idx; + ++idx; + } + if (ik < 0 || !std::getline(f, line)) return ""; + std::stringstream ls(line); + std::string field; + for (int i = 0; std::getline(ls, field, ','); ++i) + if (i == ik) return field; + return ""; +} + +// Close at-or-before time t in a time-sorted series (NaN if the series +// has not started) — comparison series align by timestamp. +static double close_at_or_before(const std::vector& rows, + long long t) { + if (rows.empty() || t < rows.front().time) + return std::numeric_limits::quiet_NaN(); + size_t lo = 0, hi = rows.size(); + while (lo + 1 < hi) { + size_t mid = (lo + hi) / 2; + if (rows[mid].time <= t) + lo = mid; + else + hi = mid; + } + return rows[lo].close; +} + +// Deterministic pseudo-random walk for the design-time chart preview. +static std::vector fake_candles(int n = 90) { + std::vector out; + unsigned s = 12345; + auto rnd = [&]() { + s = s * 1103515245u + 12345u; + return ((s >> 16) & 0x7fff) / 32768.0; // 0..1 + }; + double price = 660.0; + for (int i = 0; i < n; ++i) { + Candle c; + c.open = price; + c.close = price + (rnd() - 0.48) * 1.8; + c.high = std::max(c.open, c.close) + rnd() * 0.45; + c.low = std::min(c.open, c.close) - rnd() * 0.45; + c.volume = 5000 + rnd() * 20000; + c.time = 1794493800000000000LL + (long long)i * 60000000000LL; + out.push_back(c); + price = c.close; + } + return out; +} + +struct Item { + Widget model; // name/type/geometry/props (source of truth) + GtkWidget* eventbox; // canvas child: intercepts events, shows selection + GtkWidget* inner; // the real widget (or its scrolled wrapper) + GtkWidget* target; // widget that receives g_object_set properties + GtkCssProvider* css = nullptr; // per-widget bg/fg provider + std::vector candles; // StockChart data (preview or loaded) + // StockChart extras: comparison series (timestamp-aligned), the + // main series' ticker, and the wheel-zoom view (transient). + std::vector> compares; + std::vector compare_names; + std::string ticker; + double chart_begin = -1, chart_count = 0; +}; + +static Project g_project; +static GtkWidget* g_window = nullptr; +static GtkWidget* g_canvas = nullptr; // GtkFixed +static GtkWidget* g_viewport = nullptr; // canvas parent (window background) +static GtkWidget* g_statusbar = nullptr; +static GtkWidget* g_props_grid = nullptr; // GtkGrid in the property pane +static GtkWidget* g_props_box = nullptr; +static GtkWidget* g_selected_label = nullptr; +static guint g_status_ctx = 0; +static GtkCssProvider* g_canvas_css = nullptr; + +static std::vector g_items; +static Item* g_selected = nullptr; +static std::string g_current_file; // .fdj path, "" = untitled +static std::map g_counters; // per-type name counters +static int g_cascade = 0; + +// Drag state +static bool g_dragging = false; +static double g_press_root_x = 0, g_press_root_y = 0; +static int g_drag_start_x = 0, g_drag_start_y = 0; + +// Property-pane widgets and their bindings (freed on every pane rebuild) +static GtkWidget *g_spin_x = nullptr, *g_spin_y = nullptr; +static GtkWidget *g_spin_w = nullptr, *g_spin_h = nullptr; +static bool g_syncing_props = false; + +struct PropBinding { + Item* item; // nullptr = window/root property + const PropDef* def; +}; +struct PickBinding { + GtkEntry* entry; +}; +static std::vector g_prop_bindings; +static std::vector g_pick_bindings; + +// Bulk-parameter inputs +static GtkWidget* g_bulk_param = nullptr; // editable combo +static GtkWidget* g_bulk_value = nullptr; // entry +static GtkWidget* g_bulk_filter = nullptr; // entry + +static int g_selftest_rc = 1; // set to 0 by the self-test on success + +// ---------------------------------------------------------------- helpers + +static void status(const std::string& text) { + gtk_statusbar_pop(GTK_STATUSBAR(g_statusbar), g_status_ctx); + gtk_statusbar_push(GTK_STATUSBAR(g_statusbar), g_status_ctx, text.c_str()); +} + +static std::string next_name(const std::string& type) { + int n = ++g_counters[type]; + char buf[64]; + std::snprintf(buf, sizeof buf, "%s_%03d", type.c_str(), n); + return buf; +} + +static std::string trim(const std::string& s) { + size_t a = s.find_first_not_of(" \t\r\n"); + if (a == std::string::npos) return ""; + size_t b = s.find_last_not_of(" \t\r\n"); + return s.substr(a, b - a + 1); +} + +static std::string lower(std::string s) { + for (char& c : s) c = (char)std::tolower((unsigned char)c); + return s; +} + +// ------------------------------------------------------- property engine + +// Validate + canonicalize a raw value for a property definition. +static bool canonicalize(const PropDef& def, const std::string& raw, + std::string& out) { + std::string v = trim(raw); + switch (def.kind) { + case PropKind::Str: + out = v; + return true; + case PropKind::Bool: { + std::string l = lower(v); + if (l == "true" || l == "1" || l == "yes" || l == "on") { + out = "true"; + return true; + } + if (l == "false" || l == "0" || l == "no" || l == "off") { + out = "false"; + return true; + } + return false; + } + case PropKind::Int: { + if (v.empty()) return false; + char* end = nullptr; + long n = std::strtol(v.c_str(), &end, 10); + if (!end || *end) return false; + // Enforce the table's range when it carries one: out-of-range + // values make GTK reject the property at runtime. + if (def.hi > def.lo && (n < def.lo || n > def.hi)) return false; + out = std::to_string(n); + return true; + } + case PropKind::Double: { + if (v.empty()) return false; + char* end = nullptr; + double d = std::strtod(v.c_str(), &end); + if (!end || *end) return false; + if (def.hi > def.lo && (d < def.lo || d > def.hi)) return false; + char buf[64]; + std::snprintf(buf, sizeof buf, "%g", d); + out = buf; + return true; + } + case PropKind::Color: { + GdkRGBA rgba; + if (!gdk_rgba_parse(&rgba, v.c_str())) return false; + gchar* s = gdk_rgba_to_string(&rgba); + out = s; + g_free(s); + return true; + } + case PropKind::Enum: { + // Accept a numeric value from the table, or a label. + if (!v.empty() && (std::isdigit((unsigned char)v[0]) || v[0] == '-')) { + char* end = nullptr; + long n = std::strtol(v.c_str(), &end, 10); + if (end && !*end) { + for (int i = 0; def.enum_labels; ++i) { + // count labels to bound the values array + (void)i; + break; + } + out = std::to_string(n); + return true; + } + } + if (def.enum_labels) { + std::string labels = def.enum_labels; + size_t pos = 0; + int idx = 0; + while (true) { + size_t bar = labels.find('|', pos); + std::string one = labels.substr( + pos, bar == std::string::npos ? bar : bar - pos); + if (lower(one) == lower(v)) { + out = std::to_string(def.enum_values[idx]); + return true; + } + if (bar == std::string::npos) break; + pos = bar + 1; + ++idx; + } + } + return false; + } + } + return false; +} + +// Rebuild an item's bg/fg CSS from its stored props. +static void refresh_item_css(Item* it) { + if (!it->css) return; + std::string css; + auto bg = it->model.props.find("background"); + auto fg = it->model.props.find("foreground"); + if (bg != it->model.props.end() || fg != it->model.props.end()) { + css = "* { "; + if (bg != it->model.props.end()) css += "background: " + bg->second + "; "; + if (fg != it->model.props.end()) css += "color: " + fg->second + "; "; + css += "}"; + } + gtk_css_provider_load_from_data(it->css, css.c_str(), -1, nullptr); +} + +// Read a string property from the model with a fallback. +static std::string prop_or_str(Item* it, const char* name, + const std::string& dflt) { + auto f = it->model.props.find(name); + return f == it->model.props.end() ? dflt : f->second; +} + +// Read an int property from the model with a fallback. +static int prop_int(Item* it, const char* name, int dflt) { + auto f = it->model.props.find(name); + return f == it->model.props.end() ? dflt : std::atoi(f->second.c_str()); +} + +// (Re)build the design-time cell grid of a Spreadsheet widget. +static void build_spreadsheet_grid(GtkWidget* grid, int rows, int cols) { + GList* kids = gtk_container_get_children(GTK_CONTAINER(grid)); + for (GList* l = kids; l; l = l->next) + gtk_widget_destroy(GTK_WIDGET(l->data)); + g_list_free(kids); + for (int r = 0; r < rows; ++r) + for (int c = 0; c < cols; ++c) { + GtkWidget* e = gtk_entry_new(); + gtk_entry_set_width_chars(GTK_ENTRY(e), 8); + gtk_widget_set_sensitive(e, FALSE); + if (r == 0 && c == 0) gtk_entry_set_text(GTK_ENTRY(e), "SS"); + gtk_grid_attach(GTK_GRID(grid), e, c, r, 1, 1); + } + gtk_widget_show_all(grid); +} + +// The GtkGrid inside a Spreadsheet's scrolled window (viewport-unwrapped). +static GtkWidget* spreadsheet_grid(Item* it) { + GtkWidget* child = gtk_bin_get_child(GTK_BIN(it->inner)); + if (child && GTK_IS_VIEWPORT(child)) + child = gtk_bin_get_child(GTK_BIN(child)); + return child; +} + +// The title label inside a Toplevel's design-time frame. +static GtkWidget* toplevel_header(Item* it) { + GtkWidget* box = gtk_bin_get_child(GTK_BIN(it->inner)); + if (!box) return nullptr; + GList* kids = gtk_container_get_children(GTK_CONTAINER(box)); + GtkWidget* first = kids ? GTK_WIDGET(kids->data) : nullptr; + g_list_free(kids); + return first; +} + +// Push one stored (already canonical) value onto the live widget. +static void apply_prop(Item* it, const PropDef& def, const std::string& value) { + const char* name = def.name; + if (std::strcmp(name, "background") == 0 || + std::strcmp(name, "foreground") == 0) { + refresh_item_css(it); + return; + } + // GtkRange has no "value" GObject property in GTK 3 — the value lives + // in its adjustment. + if (std::strcmp(name, "value") == 0 && GTK_IS_RANGE(it->target)) { + gtk_range_set_value(GTK_RANGE(it->target), std::atof(value.c_str())); + return; + } + // Toplevel is a window, not a child widget: its title goes to the + // design-time header label; other props (resizable) are model-only. + if (it->model.type == "Toplevel") { + if (std::strcmp(name, "title") == 0) { + GtkWidget* hdr = toplevel_header(it); + if (hdr) + gtk_label_set_text( + GTK_LABEL(hdr), + value.empty() ? it->model.name.c_str() : value.c_str()); + } + return; + } + // StockChart: a data-file (re)loads the candles; compare-files + // (re)loads the comparison series; everything redraws. + if (it->model.type == "StockChart") { + if (std::strcmp(name, "data-file") == 0 && !value.empty()) { + std::vector rows = parse_candles_csv(value); + if (!rows.empty()) { + it->candles = rows; + it->ticker = csv_ticker(value); + it->chart_begin = -1; + it->chart_count = 0; + } + } + if (std::strcmp(name, "data-file") == 0 || + std::strcmp(name, "compare-files") == 0) { + it->compares.clear(); + it->compare_names.clear(); + std::string spec = prop_or_str(it, "compare-files", ""); + size_t start = 0; + for (size_t i = 0; i <= spec.size(); ++i) { + if (i != spec.size() && spec[i] != ',') continue; + std::string one = spec.substr(start, i - start); + start = i + 1; + size_t b = one.find_first_not_of(" \t"); + size_t e = one.find_last_not_of(" \t"); + if (b == std::string::npos) continue; + one = one.substr(b, e - b + 1); + std::vector rows = parse_candles_csv(one); + if (rows.empty()) continue; + std::string nm = csv_ticker(one); + if (nm.empty()) nm = one; + it->compares.push_back(rows); + it->compare_names.push_back(nm); + } + } + gtk_widget_queue_draw(it->target); + return; + } + // Spreadsheet rows/columns rebuild the design-time grid. + if (it->model.type == "Spreadsheet" && + (std::strcmp(name, "rows") == 0 || + std::strcmp(name, "columns") == 0)) { + GtkWidget* grid = spreadsheet_grid(it); + if (grid) + build_spreadsheet_grid(grid, prop_int(it, "rows", 5), + prop_int(it, "columns", 4)); + return; + } + GObject* o = G_OBJECT(it->target); + switch (def.kind) { + case PropKind::Str: + g_object_set(o, name, value.c_str(), nullptr); + break; + case PropKind::Bool: + g_object_set(o, name, value == "true" ? TRUE : FALSE, nullptr); + break; + case PropKind::Int: + case PropKind::Enum: + g_object_set(o, name, std::atoi(value.c_str()), nullptr); + break; + case PropKind::Double: + g_object_set(o, name, std::atof(value.c_str()), nullptr); + break; + case PropKind::Color: + break; // only the pseudo-properties are colours + } +} + +// Set a property from raw user input: validate, store, apply. +static bool set_prop(Item* it, const std::string& name, + const std::string& raw, std::string& err) { + const PropDef* def = find_prop(it->model.type, name); + if (!def) { + err = it->model.type + " has no property '" + name + "'"; + return false; + } + std::string canon; + if (!canonicalize(*def, raw, canon)) { + err = "'" + raw + "' is not a valid value for " + name; + return false; + } + it->model.props[name] = canon; + apply_prop(it, *def, canon); + return true; +} + +// Remove a property: drop it from the model and restore the default live. +static bool clear_prop(Item* it, const std::string& name) { + const PropDef* def = find_prop(it->model.type, name); + if (!def) return false; + it->model.props.erase(name); + if (name == "background" || name == "foreground") { + refresh_item_css(it); + } else if (def->def && def->def[0]) { + apply_prop(it, *def, def->def); + } else if (def->kind == PropKind::Str) { + apply_prop(it, *def, ""); + } + return true; +} + +// ------------------------------------------------------- bulk operations + +enum class BulkMode { All, Selected, Filtered }; + +static bool bulk_match(Item* it, BulkMode mode, const std::string& filter) { + if (mode == BulkMode::All) return true; + if (mode == BulkMode::Selected) return it == g_selected; + std::string f = lower(filter); + return lower(it->model.type).find(f) != std::string::npos || + lower(it->model.name).find(f) != std::string::npos; +} + +// Set one parameter on every matching widget that supports it. +// Returns the number of widgets changed, or -1 on a value error. +static int bulk_set(const std::string& param, const std::string& value, + BulkMode mode, const std::string& filter, + std::string& err) { + int n = 0; + for (Item* it : g_items) { + if (!bulk_match(it, mode, filter)) continue; + if (!find_prop(it->model.type, param)) continue; + if (!set_prop(it, param, value, err)) return -1; + ++n; + } + return n; +} + +static int bulk_clear(const std::string& param) { + int n = 0; + for (Item* it : g_items) + if (clear_prop(it, param)) ++n; + return n; +} + +// ------------------------------------------------------- window properties + +static void set_project_title(const std::string& t) { g_project.title = t; } + +static void set_project_class(const std::string& c) { g_project.class_name = c; } + +static void set_project_size(int w, int h) { + g_project.win_width = w; + g_project.win_height = h; + gtk_widget_set_size_request(g_canvas, w, h); +} + +static void set_project_background(const std::string& raw) { + GdkRGBA rgba; + if (!raw.empty() && !gdk_rgba_parse(&rgba, raw.c_str())) return; + g_project.background = raw; + std::string css = raw.empty() ? "" : "* { background: " + raw + "; }"; + if (g_canvas_css && g_viewport) + gtk_css_provider_load_from_data(g_canvas_css, css.c_str(), -1, nullptr); +} + +// ------------------------------------------------------- chart plotting + +// Parse the StockChart "indicators" property ("SMA(20), RSI, ...") into +// indicator submodules from indicators.h. +static std::vector> chart_indicators( + Item* it) { + std::vector> out; + std::string spec = prop_or_str(it, "indicators", ""); + // Split on commas that are not inside parentheses. + std::vector parts; + int depth = 0; + size_t start = 0; + for (size_t i = 0; i <= spec.size(); ++i) { + if (i == spec.size() || (spec[i] == ',' && depth == 0)) { + parts.push_back(spec.substr(start, i - start)); + start = i + 1; + } else if (spec[i] == '(') { + ++depth; + } else if (spec[i] == ')') { + --depth; + } + } + for (std::string one : parts) { + size_t b = one.find_first_not_of(" \t"); + size_t e = one.find_last_not_of(" \t"); + if (b == std::string::npos) continue; + auto ind = fdi::make_indicator(one.substr(b, e - b + 1)); + if (ind) out.push_back(std::move(ind)); + } + return out; +} + +// Draw the chart: candles in the price pane, overlay indicators on the +// same scale, pane indicators in labelled strips below, comparison +// series rebased onto the first visible close, annotations from the +// drawings property, a price/percent value axis on the right and a +// time axis along the bottom (same layout as the generated class). +static void plot_chart(cairo_t* cr, int width, int height, Item* it) { + const std::vector& all = it->candles; + std::string up_s = prop_or_str(it, "up-color", "rgb(0,200,0)"); + std::string down_s = prop_or_str(it, "down-color", "rgb(220,0,0)"); + GdkRGBA up, down; + if (!gdk_rgba_parse(&up, up_s.c_str())) gdk_rgba_parse(&up, "rgb(0,200,0)"); + if (!gdk_rgba_parse(&down, down_s.c_str())) + gdk_rgba_parse(&down, "rgb(220,0,0)"); + cairo_set_source_rgb(cr, 1, 1, 1); + cairo_paint(cr); + if (all.empty()) { + cairo_set_source_rgb(cr, 0.4, 0.4, 0.4); + cairo_move_to(cr, 8, 20); + cairo_show_text(cr, "StockChart — no data"); + return; + } + const double axis_w = 56, axis_h = 18, pad = 4; + double plot_w = std::max(10.0, width - axis_w); + double total_h = std::max(10.0, height - axis_h); + int max_n = 120; + auto mf = it->model.props.find("max-candles"); + if (mf != it->model.props.end()) max_n = std::atoi(mf->second.c_str()); + if (max_n < 1) max_n = 1; + size_t begin, n; + if (it->chart_count > 0) { + double vc = std::min(it->chart_count, (double)all.size()); + double vb = it->chart_begin; + if (vb < 0) vb = 0; + if (vb + vc > (double)all.size()) vb = (double)all.size() - vc; + begin = (size_t)vb; + n = (size_t)vc; + } else { + begin = all.size() > (size_t)max_n ? all.size() - max_n : 0; + n = all.size() - begin; + } + bool percent = prop_or_str(it, "scale-mode", "0") == "1"; + double base = all[begin].close; + if (base == 0) base = all[begin].open; + + auto inds = chart_indicators(it); + std::vector bars(all.size()); + for (size_t i = 0; i < all.size(); ++i) { + bars[i].open = all[i].open; + bars[i].high = all[i].high; + bars[i].low = all[i].low; + bars[i].close = all[i].close; + bars[i].volume = all[i].volume; + bars[i].time = all[i].time; + } + int panes = 0; + for (auto& ind : inds) + if (ind->kind() == fdi::PlotKind::Pane) ++panes; + double price_h = panes ? total_h * 0.66 : total_h; + double pane_h = panes ? (total_h - price_h) / panes : 0; + + double lo = all[begin].low, hi = all[begin].high; + for (size_t i = begin; i < begin + n; ++i) { + lo = std::min(lo, all[i].low); + hi = std::max(hi, all[i].high); + } + // Comparison series extend the range with their rebased values. + std::vector cmp_base; + for (const auto& rows : it->compares) { + double c0 = std::numeric_limits::quiet_NaN(); + for (size_t i = begin; i < begin + n; ++i) { + double v = close_at_or_before(rows, all[i].time); + if (v == v && v != 0) { + c0 = v; + break; + } + } + cmp_base.push_back(c0); + if (c0 != c0 || c0 == 0 || base == 0) continue; + for (size_t i = begin; i < begin + n; ++i) { + double v = close_at_or_before(rows, all[i].time); + if (v == v) { + double eq = base * v / c0; + lo = std::min(lo, eq); + hi = std::max(hi, eq); + } + } + } + if (hi - lo < 1e-9) hi = lo + 1; + auto y = [&](double v) { + return pad + (hi - v) / (hi - lo) * (price_h - 2 * pad); + }; + double slot = plot_w / (double)n; + + // Gridlines + value axis (price or percent). + { + double raw = (hi - lo) / 5; + double p10 = std::pow(10, std::floor(std::log10(raw))); + double m = raw / p10; + double step = (m < 1.5 ? 1 : m < 3.5 ? 2 : m < 7.5 ? 5 : 10) * p10; + cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL, + CAIRO_FONT_WEIGHT_NORMAL); + cairo_set_font_size(cr, 9.5); + for (double v = std::ceil(lo / step) * step; v <= hi; v += step) { + double gy = y(v); + cairo_set_source_rgba(cr, 0, 0, 0, 0.08); + cairo_set_line_width(cr, 1); + cairo_move_to(cr, 0, gy); + cairo_line_to(cr, plot_w, gy); + cairo_stroke(cr); + char buf[32]; + if (percent && base > 0) + std::snprintf(buf, sizeof buf, "%+.1f%%", + (v / base - 1) * 100); + else + std::snprintf(buf, sizeof buf, "%.2f", v); + cairo_set_source_rgb(cr, 0.35, 0.35, 0.35); + cairo_move_to(cr, plot_w + 4, gy + 3); + cairo_show_text(cr, buf); + } + } + // Time axis (UTC; date on the first tick and at day changes). + if (all[begin].time > 0) { + static const long kSteps[] = {1, 2, 5, 10, 15, 30, + 60, 120, 240, 480, 960, 1920, + 3840, 7680}; + long want = std::max(1L, (long)n / 6); + long step_b = kSteps[sizeof(kSteps) / sizeof(kSteps[0]) - 1]; + for (long s : kSteps) + if (s >= want) { + step_b = s; + break; + } + cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL, + CAIRO_FONT_WEIGHT_NORMAL); + cairo_set_font_size(cr, 9.5); + long long prev_day = -1; + size_t first = (begin + (size_t)step_b - 1) / (size_t)step_b * + (size_t)step_b; + for (size_t i = first; i < begin + n; i += (size_t)step_b) { + double gx = slot * (double)(i - begin) + slot / 2; + cairo_set_source_rgba(cr, 0, 0, 0, 0.06); + cairo_set_line_width(cr, 1); + cairo_move_to(cr, gx, 0); + cairo_line_to(cr, gx, price_h); + cairo_stroke(cr); + long long day = all[i].time / 86400000000000LL; + bool with_date = prev_day < 0 || day != prev_day; + prev_day = day; + std::time_t t = (std::time_t)(all[i].time / 1000000000LL); + std::tm tmv; + gmtime_r(&t, &tmv); + char buf[24]; + std::strftime(buf, sizeof buf, + with_date ? "%m-%d %H:%M" : "%H:%M", &tmv); + cairo_text_extents_t te; + cairo_text_extents(cr, buf, &te); + double tx = std::max( + 0.0, std::min(gx - te.width / 2, (double)width - te.width)); + cairo_set_source_rgb(cr, 0.35, 0.35, 0.35); + cairo_move_to(cr, tx, height - 5); + cairo_show_text(cr, buf); + } + cairo_set_source_rgba(cr, 0, 0, 0, 0.25); + cairo_set_line_width(cr, 1); + cairo_move_to(cr, 0, height - axis_h + 0.5); + cairo_line_to(cr, plot_w, height - axis_h + 0.5); + cairo_stroke(cr); + } + + // Candles. + double body_w = std::max(1.0, slot * 0.7); + for (size_t k = 0; k < n; ++k) { + const Candle& c = all[begin + k]; + const GdkRGBA& col = c.close >= c.open ? up : down; + double cx = slot * (double)k + slot / 2; + cairo_set_source_rgba(cr, col.red, col.green, col.blue, 1.0); + cairo_set_line_width(cr, 1.0); + cairo_move_to(cr, cx, y(c.high)); + cairo_line_to(cr, cx, y(c.low)); + cairo_stroke(cr); + double y1 = y(std::max(c.open, c.close)); + double y2 = y(std::min(c.open, c.close)); + if (y2 - y1 < 1) y2 = y1 + 1; + cairo_rectangle(cr, cx - body_w / 2, y1, body_w, y2 - y1); + cairo_fill(cr); + } + + // Comparison lines (same palette as the generated class). + static const double kCmpCols[][3] = {{0.90, 0.45, 0.05}, + {0.55, 0.20, 0.75}, + {0.00, 0.55, 0.60}, + {0.85, 0.20, 0.65}, + {0.35, 0.35, 0.35}}; + for (size_t ci = 0; ci < it->compares.size(); ++ci) { + double c0 = cmp_base[ci]; + if (c0 != c0 || c0 == 0 || base == 0) continue; + const double* col = kCmpCols[ci % 5]; + cairo_set_source_rgb(cr, col[0], col[1], col[2]); + cairo_set_line_width(cr, 1.5); + bool started = false; + for (size_t k = 0; k < n; ++k) { + double v = close_at_or_before(it->compares[ci], + all[begin + k].time); + if (v != v) { + started = false; + continue; + } + double eq = base * v / c0; + double px = slot * (double)k + slot / 2; + if (!started) { + cairo_move_to(cr, px, y(eq)); + started = true; + } else { + cairo_line_to(cr, px, y(eq)); + } + } + cairo_stroke(cr); + } + + // Indicators. + int pane_idx = 0; + for (auto& ind : inds) { + fdi::PlotArea area; + area.cr = cr; + area.begin = begin; + area.count = n; + if (ind->kind() == fdi::PlotKind::Pane) { + area.x = 0; + area.y = price_h + pane_idx * pane_h + 10; + area.w = plot_w; + area.h = pane_h - 12; + cairo_set_source_rgb(cr, 0.75, 0.75, 0.75); + cairo_move_to(cr, 0, price_h + pane_idx * pane_h); + cairo_line_to(cr, plot_w, price_h + pane_idx * pane_h); + cairo_stroke(cr); + cairo_set_source_rgb(cr, 0.35, 0.35, 0.35); + cairo_move_to(cr, 2, price_h + pane_idx * pane_h + 9); + cairo_show_text(cr, ind->name()); + ind->draw(area, bars); + ++pane_idx; + } else { + area.x = 0; + area.y = pad; + area.w = plot_w; + area.h = price_h - 2 * pad; + area.lo = lo; + area.hi = hi; + ind->draw(area, bars); + } + } + + // Annotations from the drawings property (data coordinates). + { + std::vector ds = + fdw::parse_specs(prop_or_str(it, "drawings", "")); + fdw::Map map; + map.x = 0; + map.y = pad; + map.w = plot_w; + map.h = price_h - 2 * pad; + map.begin = (double)begin; + map.count = (double)n; + map.lo = lo; + map.hi = hi; + for (const fdw::Drawing& d : ds) fdw::draw(cr, map, d); + } + + // Legend. + { + cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL, + CAIRO_FONT_WEIGHT_NORMAL); + cairo_set_font_size(cr, 11); + std::vector> entries; + std::string main_name = + it->ticker.empty() ? std::string("MAIN") : it->ticker; + char lastbuf[48]; + double last = all[begin + n - 1].close; + if (percent && base > 0) + std::snprintf(lastbuf, sizeof lastbuf, " %+.2f%%", + (last / base - 1) * 100); + else + std::snprintf(lastbuf, sizeof lastbuf, " %.2f", last); + entries.push_back( + {main_name + lastbuf, GdkRGBA{0.15, 0.15, 0.15, 1.0}}); + for (size_t ci = 0; ci < it->compare_names.size(); ++ci) { + const double* col = kCmpCols[ci % 5]; + entries.push_back({it->compare_names[ci], + GdkRGBA{col[0], col[1], col[2], 1.0}}); + } + double tw = 0; + for (auto& e : entries) { + cairo_text_extents_t te; + cairo_text_extents(cr, e.first.c_str(), &te); + tw = std::max(tw, te.width); + } + cairo_set_source_rgba(cr, 1, 1, 1, 0.78); + cairo_rectangle(cr, 4, 4, tw + 10, + (double)entries.size() * 14 + 6); + cairo_fill(cr); + double ly = 15; + for (auto& e : entries) { + cairo_set_source_rgba(cr, e.second.red, e.second.green, + e.second.blue, 1.0); + cairo_move_to(cr, 8, ly); + cairo_show_text(cr, e.first.c_str()); + ly += 14; + } + } +} + +// Mouse wheel zooms the design-time chart preview around the cursor +// (the generated widget has the same behaviour, plus drag panning). +static gboolean on_chart_scroll(GtkWidget*, GdkEventScroll* ev, + gpointer data) { + Item* it = (Item*)data; + if (it->candles.empty()) return FALSE; + GtkAllocation alloc; + gtk_widget_get_allocation(it->target, &alloc); + double plot_w = std::max(10.0, alloc.width - 56.0); + double frac = std::max(0.0, std::min(1.0, ev->x / plot_w)); + if (it->chart_count <= 0) { + int max_n = 120; + auto mf = it->model.props.find("max-candles"); + if (mf != it->model.props.end()) + max_n = std::atoi(mf->second.c_str()); + if (max_n < 1) max_n = 1; + it->chart_count = + std::min((double)max_n, (double)it->candles.size()); + it->chart_begin = (double)it->candles.size() - it->chart_count; + } + double factor; + if (ev->direction == GDK_SCROLL_UP) + factor = 0.8; + else if (ev->direction == GDK_SCROLL_DOWN) + factor = 1.25; + else + factor = ev->delta_y < 0 ? 0.8 : 1.25; + double anchor = it->chart_begin + frac * it->chart_count; + double nc = std::round(std::min( + std::max(it->chart_count * factor, 5.0), + (double)it->candles.size())); + double nb = std::round(anchor - frac * nc); + if (nb < 0) nb = 0; + if (nb + nc > (double)it->candles.size()) + nb = (double)it->candles.size() - nc; + it->chart_begin = nb; + it->chart_count = nc; + gtk_widget_queue_draw(it->target); + return TRUE; +} +static gboolean on_chart_draw(GtkWidget*, cairo_t* cr, gpointer data) { + Item* it = (Item*)data; + GtkAllocation alloc; + gtk_widget_get_allocation(it->target, &alloc); + plot_chart(cr, alloc.width, alloc.height, it); + return FALSE; +} + +// ------------------------------------------------------- widget factory + +// Create the real GTK widget for a palette type, in its design-time dress. +static GtkWidget* create_inner(const TypeInfo& t, const std::string& name) { + const std::string key = t.key; + if (key == "Button") return gtk_button_new_with_label(name.c_str()); + if (key == "Label") return gtk_label_new(name.c_str()); + if (key == "Entry") { + GtkWidget* e = gtk_entry_new(); + gtk_entry_set_placeholder_text(GTK_ENTRY(e), "Entry"); + return e; + } + if (key == "Checkbutton") + return gtk_check_button_new_with_label(name.c_str()); + if (key == "Radiobutton") + return gtk_radio_button_new_with_label(nullptr, name.c_str()); + if (key == "Frame") return gtk_frame_new(name.c_str()); + if (key == "Combobox") { + GtkWidget* c = gtk_combo_box_text_new(); + gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(c), "Item 1"); + gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(c), "Item 2"); + gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(c), "Item 3"); + gtk_combo_box_set_active(GTK_COMBO_BOX(c), 0); + return c; + } + if (key == "Listbox") { + GtkWidget* sw = gtk_scrolled_window_new(nullptr, nullptr); + gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), + GTK_POLICY_AUTOMATIC, + GTK_POLICY_AUTOMATIC); + gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw), + GTK_SHADOW_IN); + GtkWidget* lb = gtk_list_box_new(); + for (int i = 1; i <= 3; ++i) { + char txt[32]; + std::snprintf(txt, sizeof txt, "Item %d", i); + gtk_list_box_insert(GTK_LIST_BOX(lb), gtk_label_new(txt), -1); + } + gtk_container_add(GTK_CONTAINER(sw), lb); + return sw; + } + if (key == "Menubutton") { + GtkWidget* b = gtk_menu_button_new(); + gtk_button_set_label(GTK_BUTTON(b), name.c_str()); + return b; + } + if (key == "Canvas" || key == "StockChart") { + GtkWidget* f = gtk_frame_new(nullptr); + gtk_frame_set_shadow_type(GTK_FRAME(f), GTK_SHADOW_IN); + gtk_container_add(GTK_CONTAINER(f), gtk_drawing_area_new()); + return f; + } + if (key == "Scale") + return gtk_scale_new_with_range(GTK_ORIENTATION_HORIZONTAL, 0, 100, 1); + if (key == "Scrollbar") + return gtk_scrollbar_new(GTK_ORIENTATION_HORIZONTAL, + gtk_adjustment_new(0, 0, 100, 1, 10, 10)); + if (key == "Text") { + GtkWidget* sw = gtk_scrolled_window_new(nullptr, nullptr); + gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), + GTK_POLICY_AUTOMATIC, + GTK_POLICY_AUTOMATIC); + gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw), + GTK_SHADOW_IN); + GtkWidget* tv = gtk_text_view_new(); + gtk_text_buffer_set_text( + gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv)), name.c_str(), -1); + gtk_container_add(GTK_CONTAINER(sw), tv); + return sw; + } + if (key == "Toplevel") { + GtkWidget* frame = gtk_frame_new(nullptr); + gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_OUT); + GtkWidget* box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); + GtkWidget* title = gtk_label_new(name.c_str()); + GtkWidget* hint = gtk_label_new("(secondary window)"); + gtk_box_pack_start(GTK_BOX(box), title, FALSE, FALSE, 2); + gtk_box_pack_start(GTK_BOX(box), hint, TRUE, TRUE, 0); + gtk_container_add(GTK_CONTAINER(frame), box); + return frame; + } + if (key == "Spreadsheet") { + GtkWidget* sw = gtk_scrolled_window_new(nullptr, nullptr); + gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), + GTK_POLICY_AUTOMATIC, + GTK_POLICY_AUTOMATIC); + gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw), + GTK_SHADOW_IN); + GtkWidget* grid = gtk_grid_new(); + build_spreadsheet_grid(grid, 5, 4); + gtk_container_add(GTK_CONTAINER(sw), grid); + return sw; + } + return gtk_label_new(name.c_str()); // fallback +} + +// The widget that receives properties: unwrap scrolled wrappers. +static GtkWidget* prop_target(const std::string& type, GtkWidget* inner) { + if (type == "Listbox" || type == "Text") { + GtkWidget* child = + gtk_bin_get_child(GTK_BIN(inner)); // viewport or direct child + if (child && GTK_IS_VIEWPORT(child)) + child = gtk_bin_get_child(GTK_BIN(child)); + return child ? child : inner; + } + if (type == "Canvas" || type == "StockChart") { + GtkWidget* child = gtk_bin_get_child(GTK_BIN(inner)); + return child ? child : inner; + } + return inner; +} + +// ------------------------------------------------------- property pane + +static void on_spin_changed(GtkSpinButton*, gpointer which); +static void on_delete_clicked(GtkButton*, gpointer); +static void on_prop_entry_changed(GtkEntry*, gpointer binding); +static void on_prop_bool_toggled(GtkToggleButton*, gpointer binding); +static void on_prop_spin_changed(GtkSpinButton*, gpointer binding); +static void on_prop_enum_changed(GtkComboBoxText*, gpointer binding); +static void on_prop_pick_clicked(GtkButton*, gpointer binding); +static void on_win_entry_changed(GtkEntry*, gpointer which); +static void on_win_spin_changed(GtkSpinButton*, gpointer which); +static void on_edit_menus_clicked(GtkButton*, gpointer); +static void on_win_bg_pick_clicked(GtkButton*, gpointer entry_ptr); + +static void clear_props() { + GList* kids = gtk_container_get_children(GTK_CONTAINER(g_props_grid)); + for (GList* p = kids; p; p = p->next) + gtk_container_remove(GTK_CONTAINER(g_props_grid), GTK_WIDGET(p->data)); + g_list_free(kids); + for (PropBinding* b : g_prop_bindings) delete b; + for (PickBinding* b : g_pick_bindings) delete b; + g_prop_bindings.clear(); + g_pick_bindings.clear(); + g_spin_x = g_spin_y = g_spin_w = g_spin_h = nullptr; +} + +static void add_row(int row, const char* label, GtkWidget* control) { + GtkWidget* lab = gtk_label_new(label); + gtk_widget_set_halign(lab, GTK_ALIGN_START); + gtk_grid_attach(GTK_GRID(g_props_grid), lab, 0, row, 1, 1); + gtk_grid_attach(GTK_GRID(g_props_grid), control, 1, row, 1, 1); +} + +static GtkWidget* make_geo_spin(double value, double lo, double hi, + const char* which) { + GtkWidget* s = gtk_spin_button_new_with_range(lo, hi, 1); + gtk_spin_button_set_value(GTK_SPIN_BUTTON(s), value); + g_signal_connect(s, "value-changed", G_CALLBACK(on_spin_changed), + (gpointer)which); + return s; +} + +// One editor widget for a property definition, bound to an item. +static GtkWidget* make_prop_editor(Item* it, const PropDef& def) { + PropBinding* b = new PropBinding{it, &def}; + g_prop_bindings.push_back(b); + auto cur = it->model.props.find(def.name); + std::string value = + cur != it->model.props.end() ? cur->second : (def.def ? def.def : ""); + switch (def.kind) { + case PropKind::Str: { + GtkWidget* e = gtk_entry_new(); + gtk_entry_set_text(GTK_ENTRY(e), value.c_str()); + g_signal_connect(e, "changed", G_CALLBACK(on_prop_entry_changed), + b); + return e; + } + case PropKind::Color: { + GtkWidget* box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 4); + GtkWidget* e = gtk_entry_new(); + gtk_entry_set_text(GTK_ENTRY(e), value.c_str()); + g_signal_connect(e, "changed", G_CALLBACK(on_prop_entry_changed), + b); + GtkWidget* pick = gtk_button_new_with_label("Pick…"); + PickBinding* pb = new PickBinding{GTK_ENTRY(e)}; + g_pick_bindings.push_back(pb); + g_signal_connect(pick, "clicked", + G_CALLBACK(on_prop_pick_clicked), pb); + gtk_box_pack_start(GTK_BOX(box), e, TRUE, TRUE, 0); + gtk_box_pack_start(GTK_BOX(box), pick, FALSE, FALSE, 0); + return box; + } + case PropKind::Bool: { + GtkWidget* c = gtk_check_button_new(); + gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(c), + value == "true"); + g_signal_connect(c, "toggled", G_CALLBACK(on_prop_bool_toggled), + b); + return c; + } + case PropKind::Int: { + GtkWidget* s = + gtk_spin_button_new_with_range(def.lo, def.hi, 1); + gtk_spin_button_set_value( + GTK_SPIN_BUTTON(s), value.empty() ? 0 : std::atof(value.c_str())); + g_signal_connect(s, "value-changed", + G_CALLBACK(on_prop_spin_changed), b); + return s; + } + case PropKind::Double: { + double step = def.hi <= 1.0 ? 0.05 : 1.0; + GtkWidget* s = + gtk_spin_button_new_with_range(def.lo, def.hi, step); + gtk_spin_button_set_digits(GTK_SPIN_BUTTON(s), 2); + gtk_spin_button_set_value( + GTK_SPIN_BUTTON(s), value.empty() ? 0 : std::atof(value.c_str())); + g_signal_connect(s, "value-changed", + G_CALLBACK(on_prop_spin_changed), b); + return s; + } + case PropKind::Enum: { + GtkWidget* c = gtk_combo_box_text_new(); + std::string labels = def.enum_labels ? def.enum_labels : ""; + size_t pos = 0; + int idx = 0, active = 0; + while (true) { + size_t bar = labels.find('|', pos); + std::string one = labels.substr( + pos, bar == std::string::npos ? bar : bar - pos); + gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(c), + one.c_str()); + if (!value.empty() && + def.enum_values[idx] == std::atoi(value.c_str())) + active = idx; + if (bar == std::string::npos) break; + pos = bar + 1; + ++idx; + } + gtk_combo_box_set_active(GTK_COMBO_BOX(c), active); + g_signal_connect(c, "changed", G_CALLBACK(on_prop_enum_changed), + b); + return c; + } + } + return gtk_label_new(""); +} + +// Rebuild the property pane: window properties when nothing is selected, +// geometry + property editors for the selected widget otherwise. +static void refresh_props() { + clear_props(); + g_syncing_props = true; + int row = 0; + if (!g_selected) { + gtk_label_set_text(GTK_LABEL(g_selected_label), "Window (Root)"); + GtkWidget* title = gtk_entry_new(); + gtk_entry_set_text(GTK_ENTRY(title), g_project.title.c_str()); + g_signal_connect(title, "changed", G_CALLBACK(on_win_entry_changed), + (gpointer) "title"); + add_row(row++, "title", title); + GtkWidget* cls = gtk_entry_new(); + gtk_entry_set_text(GTK_ENTRY(cls), g_project.class_name.c_str()); + g_signal_connect(cls, "changed", G_CALLBACK(on_win_entry_changed), + (gpointer) "class"); + add_row(row++, "class name", cls); + GtkWidget* w = gtk_spin_button_new_with_range(200, 5000, 1); + gtk_spin_button_set_value(GTK_SPIN_BUTTON(w), g_project.win_width); + g_signal_connect(w, "value-changed", G_CALLBACK(on_win_spin_changed), + (gpointer) "w"); + add_row(row++, "width", w); + GtkWidget* h = gtk_spin_button_new_with_range(200, 5000, 1); + gtk_spin_button_set_value(GTK_SPIN_BUTTON(h), g_project.win_height); + g_signal_connect(h, "value-changed", G_CALLBACK(on_win_spin_changed), + (gpointer) "h"); + add_row(row++, "height", h); + GtkWidget* box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 4); + GtkWidget* bg = gtk_entry_new(); + gtk_entry_set_text(GTK_ENTRY(bg), g_project.background.c_str()); + g_signal_connect(bg, "changed", G_CALLBACK(on_win_entry_changed), + (gpointer) "background"); + GtkWidget* pick = gtk_button_new_with_label("Pick…"); + PickBinding* pb = new PickBinding{GTK_ENTRY(bg)}; + g_pick_bindings.push_back(pb); + g_signal_connect(pick, "clicked", + G_CALLBACK(on_win_bg_pick_clicked), pb); + gtk_box_pack_start(GTK_BOX(box), bg, TRUE, TRUE, 0); + gtk_box_pack_start(GTK_BOX(box), pick, FALSE, FALSE, 0); + add_row(row++, "background", box); + GtkWidget* menus = gtk_button_new_with_label("Edit menus…"); + g_signal_connect(menus, "clicked", + G_CALLBACK(on_edit_menus_clicked), nullptr); + add_row(row++, "menu bar", menus); + gtk_widget_show_all(g_props_grid); + g_syncing_props = false; + return; + } + const Widget& m = g_selected->model; + gtk_label_set_text(GTK_LABEL(g_selected_label), + (m.name + " (" + m.type + ")").c_str()); + g_spin_x = make_geo_spin(m.x, -100, 5000, "x"); + add_row(row++, "x", g_spin_x); + g_spin_y = make_geo_spin(m.y, -100, 5000, "y"); + add_row(row++, "y", g_spin_y); + g_spin_w = make_geo_spin(m.width, 10, 5000, "w"); + add_row(row++, "width", g_spin_w); + g_spin_h = make_geo_spin(m.height, 10, 5000, "h"); + add_row(row++, "height", g_spin_h); + + GtkWidget* hdr = gtk_label_new("Widget properties"); + gtk_label_set_use_markup(GTK_LABEL(hdr), TRUE); + gtk_widget_set_halign(hdr, GTK_ALIGN_START); + gtk_grid_attach(GTK_GRID(g_props_grid), hdr, 0, row++, 2, 1); + + std::vector defs; + for (const PropDef& d : props_for(m.type)) defs.push_back(&d); + for (const PropDef& d : common_props()) defs.push_back(&d); + for (const PropDef* d : defs) + add_row(row++, d->name, make_prop_editor(g_selected, *d)); + + GtkWidget* del = gtk_button_new_with_label("Delete widget"); + g_signal_connect(del, "clicked", G_CALLBACK(on_delete_clicked), nullptr); + gtk_grid_attach(GTK_GRID(g_props_grid), del, 0, row, 2, 1); + gtk_widget_show_all(g_props_grid); + g_syncing_props = false; +} + +// Push the selected item's geometry into the spins (during drags). +static void sync_spins() { + if (!g_selected || !g_spin_x) return; + g_syncing_props = true; + gtk_spin_button_set_value(GTK_SPIN_BUTTON(g_spin_x), g_selected->model.x); + gtk_spin_button_set_value(GTK_SPIN_BUTTON(g_spin_y), g_selected->model.y); + gtk_spin_button_set_value(GTK_SPIN_BUTTON(g_spin_w), + g_selected->model.width); + gtk_spin_button_set_value(GTK_SPIN_BUTTON(g_spin_h), + g_selected->model.height); + g_syncing_props = false; +} + +// ------------------------------------------------------- canvas ops + +static void move_item(Item* it, int x, int y) { + if (x < 0) x = 0; + if (y < 0) y = 0; + it->model.x = x; + it->model.y = y; + gtk_fixed_move(GTK_FIXED(g_canvas), it->eventbox, x, y); +} + +static void resize_item(Item* it, int w, int h) { + if (w < 10) w = 10; + if (h < 10) h = 10; + it->model.width = w; + it->model.height = h; + gtk_widget_set_size_request(it->eventbox, w, h); +} + +static void select_item(Item* it) { + if (g_selected) + gtk_style_context_remove_class( + gtk_widget_get_style_context(g_selected->eventbox), "sel"); + g_selected = it; + if (g_selected) { + gtk_style_context_add_class( + gtk_widget_get_style_context(g_selected->eventbox), "sel"); + status(g_selected->model.name + " selected — drag to move, " + + "Delete removes it"); + } + refresh_props(); +} + +static void delete_item(Item* it) { + if (!it) return; + std::string name = it->model.name; + if (g_selected == it) select_item(nullptr); + gtk_container_remove(GTK_CONTAINER(g_canvas), it->eventbox); + for (auto p = g_items.begin(); p != g_items.end(); ++p) + if (*p == it) { + g_items.erase(p); + break; + } + if (it->css) g_object_unref(it->css); + delete it; + status(name + " deleted — " + std::to_string(g_items.size()) + + " widget(s) on canvas"); +} + +// Event handlers on each placed widget's event box. +static gboolean on_item_press(GtkWidget*, GdkEventButton* e, gpointer data) { + Item* it = (Item*)data; + if (e->button != 1) return FALSE; + select_item(it); + g_dragging = true; + g_press_root_x = e->x_root; + g_press_root_y = e->y_root; + g_drag_start_x = it->model.x; + g_drag_start_y = it->model.y; + return TRUE; +} + +static gboolean on_item_motion(GtkWidget*, GdkEventMotion* e, gpointer data) { + Item* it = (Item*)data; + if (!g_dragging || !(e->state & GDK_BUTTON1_MASK)) return FALSE; + int nx = g_drag_start_x + (int)(e->x_root - g_press_root_x); + int ny = g_drag_start_y + (int)(e->y_root - g_press_root_y); + move_item(it, nx, ny); + sync_spins(); + return TRUE; +} + +static gboolean on_item_release(GtkWidget*, GdkEventButton* e, gpointer) { + if (e->button == 1) g_dragging = false; + return FALSE; +} + +// Build a canvas item (widgets, event box, signals) without properties. +static Item* create_item(const TypeInfo& t, const std::string& name, int x, + int y, int w, int h) { + Item* it = new Item(); + it->model.name = name; + it->model.type = t.key; + it->model.x = x; + it->model.y = y; + it->model.width = w; + it->model.height = h; + + it->inner = create_inner(t, name); + it->target = prop_target(t.key, it->inner); + it->css = gtk_css_provider_new(); + gtk_style_context_add_provider(gtk_widget_get_style_context(it->inner), + GTK_STYLE_PROVIDER(it->css), + GTK_STYLE_PROVIDER_PRIORITY_USER); + + it->eventbox = gtk_event_box_new(); + gtk_event_box_set_above_child(GTK_EVENT_BOX(it->eventbox), TRUE); + gtk_widget_add_events(it->eventbox, + GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | + GDK_POINTER_MOTION_MASK); + gtk_container_add(GTK_CONTAINER(it->eventbox), it->inner); + gtk_widget_set_size_request(it->eventbox, w, h); + g_signal_connect(it->eventbox, "button-press-event", + G_CALLBACK(on_item_press), it); + g_signal_connect(it->eventbox, "motion-notify-event", + G_CALLBACK(on_item_motion), it); + g_signal_connect(it->eventbox, "button-release-event", + G_CALLBACK(on_item_release), it); + + gtk_fixed_put(GTK_FIXED(g_canvas), it->eventbox, x, y); + gtk_widget_show_all(it->eventbox); + if (std::strcmp(t.key, "StockChart") == 0) { + it->candles = fake_candles(); // preview until a data-file loads + g_signal_connect(it->target, "draw", G_CALLBACK(on_chart_draw), it); + gtk_widget_add_events(it->target, GDK_SCROLL_MASK); + g_signal_connect(it->target, "scroll-event", + G_CALLBACK(on_chart_scroll), it); + } + g_items.push_back(it); + return it; +} + +// Add a widget of the given palette type to the canvas. Used by the palette +// buttons and by the self-test. +static Item* add_widget(const std::string& type_key) { + const TypeInfo* t = find_type(type_key); + if (!t) return nullptr; + if (!t->phase1) { + status(std::string(t->key) + " arrives in Phase 5 — not placed"); + return nullptr; + } + int off = 20 + (g_cascade++ % 8) * 24; + Item* it = create_item(*t, next_name(type_key), off, off, t->def_width, + t->def_height); + + // Seed the model's property defaults (the label-ish types were created + // showing their name, which is their label default). + std::vector defs; + for (const PropDef& d : props_for(type_key)) defs.push_back(&d); + for (const PropDef& d : common_props()) defs.push_back(&d); + for (const PropDef* d : defs) { + if (std::strcmp(d->name, "label") == 0) { + it->model.props["label"] = it->model.name; + } else if (d->def && d->def[0]) { + it->model.props[d->name] = d->def; + apply_prop(it, *d, d->def); + } + } + + select_item(it); + status(it->model.name + " placed — " + + std::to_string(g_items.size()) + " widget(s) on canvas"); + return it; +} + +// Recreate one widget from a loaded project: same name and geometry, with +// every stored property re-validated through set_prop as it is applied. +static Item* add_widget_loaded(const Widget& m) { + const TypeInfo* t = find_type(m.type); + if (!t || !t->phase1) return nullptr; + Item* it = create_item(*t, m.name, m.x, m.y, m.width, m.height); + it->model = m; + if (find_prop(m.type, "label") && !it->model.props.count("label")) + it->model.props["label"] = m.name; + auto props_copy = it->model.props; + it->model.props.clear(); + std::string err; + for (const auto& [k, v] : props_copy) set_prop(it, k, v, err); + return it; +} + +static void clear_canvas() { + while (!g_items.empty()) delete_item(g_items.front()); + g_counters.clear(); + g_cascade = 0; + select_item(nullptr); +} + +// After a load, name counters must continue past the loaded names so new +// widgets don't collide (Button_001 loaded -> next new one is Button_002). +static void recompute_counters() { + g_counters.clear(); + for (Item* it : g_items) { + const std::string& n = it->model.name; + size_t us = n.rfind('_'); + if (us == std::string::npos) continue; + std::string num = n.substr(us + 1); + if (num.empty() || + !std::all_of(num.begin(), num.end(), + [](unsigned char c) { return std::isdigit(c); })) + continue; + std::string type = n.substr(0, us); + int v = std::atoi(num.c_str()); + if (v > g_counters[type]) g_counters[type] = v; + } +} + +// Replace the whole canvas + window state with a loaded project. +static void load_into_canvas(const Project& p) { + clear_canvas(); + g_project.title = p.title; + g_project.class_name = p.class_name; + set_project_size(p.win_width, p.win_height); + g_project.background = p.background; + set_project_background(p.background); + int skipped = 0; + for (const Widget& w : p.widgets) + if (!add_widget_loaded(w)) ++skipped; + recompute_counters(); + select_item(nullptr); + refresh_props(); + status("Loaded " + std::to_string(g_items.size()) + " widget(s)" + + (skipped ? " — " + std::to_string(skipped) + + " unknown widget(s) skipped" + : "")); +} + +// ------------------------------------------------------- pane callbacks + +static void on_spin_changed(GtkSpinButton* spin, gpointer which) { + if (g_syncing_props || !g_selected) return; + int v = gtk_spin_button_get_value_as_int(spin); + const char* w = (const char*)which; + if (std::strcmp(w, "x") == 0) + move_item(g_selected, v, g_selected->model.y); + else if (std::strcmp(w, "y") == 0) + move_item(g_selected, g_selected->model.x, v); + else if (std::strcmp(w, "w") == 0) + resize_item(g_selected, v, g_selected->model.height); + else if (std::strcmp(w, "h") == 0) + resize_item(g_selected, g_selected->model.width, v); +} + +static void on_delete_clicked(GtkButton*, gpointer) { delete_item(g_selected); } + +static void report_set(Item* it, const std::string& name, bool ok, + const std::string& err) { + if (ok) + status(it->model.name + ": " + name + " = " + + it->model.props[name]); + else if (!err.empty()) + status("Not set — " + err); +} + +static void on_prop_entry_changed(GtkEntry* e, gpointer binding) { + if (g_syncing_props) return; + PropBinding* b = (PropBinding*)binding; + std::string err; + bool ok = set_prop(b->item, b->def->name, gtk_entry_get_text(e), err); + if (b->def->kind == PropKind::Str) + report_set(b->item, b->def->name, ok, err); + else if (ok) + status(b->item->model.name + ": " + b->def->name + " = " + + b->item->model.props[b->def->name]); +} + +static void on_prop_bool_toggled(GtkToggleButton* t, gpointer binding) { + if (g_syncing_props) return; + PropBinding* b = (PropBinding*)binding; + std::string err; + set_prop(b->item, b->def->name, + gtk_toggle_button_get_active(t) ? "true" : "false", err); +} + +static void on_prop_spin_changed(GtkSpinButton* s, gpointer binding) { + if (g_syncing_props) return; + PropBinding* b = (PropBinding*)binding; + char buf[64]; + if (b->def->kind == PropKind::Int) + std::snprintf(buf, sizeof buf, "%d", + gtk_spin_button_get_value_as_int(s)); + else + std::snprintf(buf, sizeof buf, "%g", + gtk_spin_button_get_value(s)); + std::string err; + set_prop(b->item, b->def->name, buf, err); +} + +static void on_prop_enum_changed(GtkComboBoxText* c, gpointer binding) { + if (g_syncing_props) return; + PropBinding* b = (PropBinding*)binding; + int idx = gtk_combo_box_get_active(GTK_COMBO_BOX(c)); + if (idx < 0) return; + std::string err; + set_prop(b->item, b->def->name, std::to_string(b->def->enum_values[idx]), + err); +} + +// Shared colour-picker: fills the bound entry with the chosen #rrggbb. +static void run_color_pick(GtkEntry* entry) { + GtkWidget* dlg = gtk_color_chooser_dialog_new("Pick a colour", + GTK_WINDOW(g_window)); + if (gtk_dialog_run(GTK_DIALOG(dlg)) == GTK_RESPONSE_OK) { + GdkRGBA rgba; + gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(dlg), &rgba); + char buf[32]; + std::snprintf(buf, sizeof buf, "#%02x%02x%02x", + (int)(rgba.red * 255 + 0.5), + (int)(rgba.green * 255 + 0.5), + (int)(rgba.blue * 255 + 0.5)); + gtk_entry_set_text(entry, buf); // fires "changed" -> applies + } + gtk_widget_destroy(dlg); +} + +static void on_prop_pick_clicked(GtkButton*, gpointer binding) { + run_color_pick(((PickBinding*)binding)->entry); +} + +static void on_win_bg_pick_clicked(GtkButton*, gpointer binding) { + run_color_pick(((PickBinding*)binding)->entry); +} + +static void on_win_entry_changed(GtkEntry* e, gpointer which) { + if (g_syncing_props) return; + const char* w = (const char*)which; + const char* text = gtk_entry_get_text(e); + if (std::strcmp(w, "title") == 0) set_project_title(text); + if (std::strcmp(w, "class") == 0) set_project_class(text); + if (std::strcmp(w, "background") == 0) set_project_background(text); +} + +static void on_win_spin_changed(GtkSpinButton* s, gpointer which) { + if (g_syncing_props) return; + int v = gtk_spin_button_get_value_as_int(s); + if (std::strcmp((const char*)which, "w") == 0) + set_project_size(v, g_project.win_height); + else + set_project_size(g_project.win_width, v); +} + +// ------------------------------------------------------- bulk UI callbacks + +static std::string bulk_param_text() { + gchar* s = gtk_combo_box_text_get_active_text( + GTK_COMBO_BOX_TEXT(g_bulk_param)); + std::string out = s ? trim(s) : ""; + if (s) g_free(s); + return out; +} + +static void on_bulk_set_clicked(GtkButton*, gpointer mode_ptr) { + BulkMode mode = (BulkMode)(long)mode_ptr; + std::string param = bulk_param_text(); + std::string value = gtk_entry_get_text(GTK_ENTRY(g_bulk_value)); + std::string filter = gtk_entry_get_text(GTK_ENTRY(g_bulk_filter)); + if (param.empty()) { + status("Bulk: type a parameter name first"); + return; + } + if (mode == BulkMode::Selected && !g_selected) { + status("Bulk: nothing selected"); + return; + } + std::string err; + int n = bulk_set(param, value, mode, filter, err); + if (n < 0) { + status("Bulk not applied — " + err); + return; + } + status("Bulk: set " + param + " on " + std::to_string(n) + " widget(s)"); + if (g_selected) refresh_props(); +} + +static void on_bulk_clear_clicked(GtkButton*, gpointer) { + std::string param = bulk_param_text(); + if (param.empty()) { + status("Bulk: type a parameter name first"); + return; + } + int n = bulk_clear(param); + status("Bulk: cleared " + param + " on " + std::to_string(n) + + " widget(s)"); + if (g_selected) refresh_props(); +} + +static void build_bulk_ui() { + GtkWidget* sep = gtk_separator_new(GTK_ORIENTATION_HORIZONTAL); + gtk_box_pack_start(GTK_BOX(g_props_box), sep, FALSE, FALSE, 6); + GtkWidget* hdr = gtk_label_new("Bulk parameter"); + gtk_label_set_use_markup(GTK_LABEL(hdr), TRUE); + gtk_widget_set_halign(hdr, GTK_ALIGN_START); + gtk_box_pack_start(GTK_BOX(g_props_box), hdr, FALSE, FALSE, 0); + + g_bulk_param = gtk_combo_box_text_new_with_entry(); + std::set names; + for (const TypeInfo& t : kTypes) { + for (const PropDef& d : props_for(t.key)) names.insert(d.name); + } + for (const PropDef& d : common_props()) names.insert(d.name); + for (const std::string& n : names) + gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(g_bulk_param), + n.c_str()); + gtk_box_pack_start(GTK_BOX(g_props_box), g_bulk_param, FALSE, FALSE, 0); + + g_bulk_value = gtk_entry_new(); + gtk_entry_set_placeholder_text(GTK_ENTRY(g_bulk_value), "value"); + gtk_box_pack_start(GTK_BOX(g_props_box), g_bulk_value, FALSE, FALSE, 0); + + g_bulk_filter = gtk_entry_new(); + gtk_entry_set_placeholder_text(GTK_ENTRY(g_bulk_filter), + "filter: type or name contains…"); + gtk_box_pack_start(GTK_BOX(g_props_box), g_bulk_filter, FALSE, FALSE, 0); + + GtkWidget* grid = gtk_grid_new(); + gtk_grid_set_row_spacing(GTK_GRID(grid), 4); + gtk_grid_set_column_spacing(GTK_GRID(grid), 4); + struct { + const char* label; + BulkMode mode; + } sets[] = {{"Set all", BulkMode::All}, + {"Set selected", BulkMode::Selected}, + {"Set filtered", BulkMode::Filtered}}; + for (int i = 0; i < 3; ++i) { + GtkWidget* b = gtk_button_new_with_label(sets[i].label); + g_signal_connect(b, "clicked", G_CALLBACK(on_bulk_set_clicked), + (gpointer)(long)sets[i].mode); + gtk_grid_attach(GTK_GRID(grid), b, i % 2, i / 2, 1, 1); + } + GtkWidget* clr = gtk_button_new_with_label("Clear param"); + g_signal_connect(clr, "clicked", G_CALLBACK(on_bulk_clear_clicked), + nullptr); + gtk_grid_attach(GTK_GRID(grid), clr, 1, 1, 1, 1); + gtk_box_pack_start(GTK_BOX(g_props_box), grid, FALSE, FALSE, 0); +} + +// ------------------------------------------------------- colour palette + +static GtkWidget* g_colour_target = nullptr; + +// Apply a colour from the palette: to the selected widget, or to the +// window background when nothing is selected (background target only). +static void apply_colour(const std::string& hex, const std::string& which) { + std::string err; + if (g_selected) { + if (set_prop(g_selected, which, hex, err)) + status(which + " of " + g_selected->model.name + " set to " + + g_selected->model.props[which]); + else + status("Colour: " + err); + } else if (which == "background") { + set_project_background(hex); + if (g_project.background == hex) + status("Window background set to " + hex); + else + status("Colour: '" + hex + "' is not a colour"); + } else { + status("Select a widget first to set its foreground"); + } +} + +static void on_swatch_clicked(GtkButton* b, gpointer) { + const char* hex = + (const char*)g_object_get_data(G_OBJECT(b), "fd-colour"); + const gchar* which = + g_colour_target + ? gtk_combo_box_get_active_id(GTK_COMBO_BOX(g_colour_target)) + : nullptr; + apply_colour(hex ? hex : "", which ? which : "background"); +} + +static void build_colour_ui() { + GtkWidget* sep = gtk_separator_new(GTK_ORIENTATION_HORIZONTAL); + gtk_box_pack_start(GTK_BOX(g_props_box), sep, FALSE, FALSE, 6); + GtkWidget* hdr = gtk_label_new("Colour palette"); + gtk_label_set_use_markup(GTK_LABEL(hdr), TRUE); + gtk_widget_set_halign(hdr, GTK_ALIGN_START); + gtk_box_pack_start(GTK_BOX(g_props_box), hdr, FALSE, FALSE, 0); + + g_colour_target = gtk_combo_box_text_new(); + gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(g_colour_target), + "background", "Background"); + gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(g_colour_target), + "foreground", "Foreground"); + gtk_combo_box_set_active_id(GTK_COMBO_BOX(g_colour_target), "background"); + gtk_box_pack_start(GTK_BOX(g_props_box), g_colour_target, FALSE, FALSE, 0); + + static const char* kColours[] = { + "#000000", "#ffffff", "#c0c0c0", "#808080", "#ff0000", "#ff8000", + "#ffff00", "#00ff00", "#00ffff", "#0000ff", "#ff00ff", "#800080", + "#008080", "#000080", "#808000", "#800000", + }; + GtkWidget* grid = gtk_grid_new(); + gtk_grid_set_row_spacing(GTK_GRID(grid), 3); + gtk_grid_set_column_spacing(GTK_GRID(grid), 3); + for (int i = 0; i < 16; ++i) { + GtkWidget* b = gtk_button_new(); + gtk_widget_set_size_request(b, 24, 24); + GtkCssProvider* p = gtk_css_provider_new(); + std::string css = + "button { background: " + std::string(kColours[i]) + "; }"; + gtk_css_provider_load_from_data(p, css.c_str(), -1, nullptr); + gtk_style_context_add_provider(gtk_widget_get_style_context(b), + GTK_STYLE_PROVIDER(p), + GTK_STYLE_PROVIDER_PRIORITY_USER); + g_object_set_data(G_OBJECT(b), "fd-colour", (gpointer)kColours[i]); + g_signal_connect(b, "clicked", G_CALLBACK(on_swatch_clicked), + nullptr); + gtk_grid_attach(GTK_GRID(grid), b, i % 8, i / 8, 1, 1); + } + gtk_box_pack_start(GTK_BOX(g_props_box), grid, FALSE, FALSE, 0); + GtkWidget* hint = gtk_label_new( + "Applies to the selected widget,\nor the window background if none."); + gtk_label_set_xalign(GTK_LABEL(hint), 0); + gtk_box_pack_start(GTK_BOX(g_props_box), hint, FALSE, FALSE, 0); +} + +// ------------------------------------------------------- menu designer + +static void on_edit_menus_clicked(GtkButton*, gpointer) { + GtkWidget* dlg = gtk_dialog_new_with_buttons( + "Menu bar", GTK_WINDOW(g_window), GTK_DIALOG_MODAL, "_Cancel", + GTK_RESPONSE_CANCEL, "_OK", GTK_RESPONSE_OK, nullptr); + gtk_window_set_default_size(GTK_WINDOW(dlg), 440, 260); + GtkWidget* content = gtk_dialog_get_content_area(GTK_DIALOG(dlg)); + GtkWidget* help = gtk_label_new( + "One menu per line: File: New | Open | - | Quit\n" + "\"-\" is a separator. Leave empty for no menu bar.\n" + "The menu bar appears in generated projects."); + gtk_label_set_xalign(GTK_LABEL(help), 0); + gtk_box_pack_start(GTK_BOX(content), help, FALSE, FALSE, 4); + GtkWidget* sw = gtk_scrolled_window_new(nullptr, nullptr); + gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw), + GTK_SHADOW_IN); + GtkWidget* tv = gtk_text_view_new(); + gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv)), + g_project.menubar.c_str(), -1); + gtk_container_add(GTK_CONTAINER(sw), tv); + gtk_box_pack_start(GTK_BOX(content), sw, TRUE, TRUE, 4); + gtk_widget_show_all(content); + if (gtk_dialog_run(GTK_DIALOG(dlg)) == GTK_RESPONSE_OK) { + GtkTextBuffer* buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv)); + GtkTextIter a, b; + gtk_text_buffer_get_bounds(buf, &a, &b); + gchar* txt = gtk_text_buffer_get_text(buf, &a, &b, FALSE); + g_project.menubar = txt ? txt : ""; + g_free(txt); + while (!g_project.menubar.empty() && + std::isspace((unsigned char)g_project.menubar.back())) + g_project.menubar.pop_back(); + status(g_project.menubar.empty() + ? "Menu bar cleared" + : "Menu bar updated — see it in generated projects"); + } + gtk_widget_destroy(dlg); +} + +// ------------------------------------------------------- other callbacks + +static void on_palette_clicked(GtkButton*, gpointer data) { + add_widget((const char*)data); +} + +static gboolean on_canvas_press(GtkWidget*, GdkEventButton* e, gpointer) { + if (e->button == 1) select_item(nullptr); // clicked empty canvas + return FALSE; +} + +static gboolean on_key_press(GtkWidget*, GdkEventKey* e, gpointer) { + if ((e->keyval == GDK_KEY_Delete || e->keyval == GDK_KEY_KP_Delete) && + g_selected) { + // Don't eat Delete while typing in a property entry. + GtkWidget* focus = gtk_window_get_focus(GTK_WINDOW(g_window)); + if (focus && GTK_IS_ENTRY(focus)) return FALSE; + delete_item(g_selected); + return TRUE; + } + return FALSE; +} + +static void on_quit(GtkMenuItem*, gpointer) { gtk_main_quit(); } + +// ------------------------------------------------------- code generation + +// Snapshot the live canvas as a Project for the generator / saver. +static Project snapshot_project() { + Project p = g_project; + p.widgets.clear(); + for (Item* it : g_items) p.widgets.push_back(it->model); + return p; +} + +static void on_generate(GtkMenuItem*, gpointer) { + GtkWidget* dlg = gtk_file_chooser_dialog_new( + "Generate C++/GTK — choose a folder", GTK_WINDOW(g_window), + GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER, "_Cancel", GTK_RESPONSE_CANCEL, + "_Generate", GTK_RESPONSE_ACCEPT, nullptr); + if (gtk_dialog_run(GTK_DIALOG(dlg)) != GTK_RESPONSE_ACCEPT) { + gtk_widget_destroy(dlg); + return; + } + gchar* folder = + gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dlg)); + gtk_widget_destroy(dlg); + if (!folder) return; + Project p = snapshot_project(); + GenResult r = generate_project(p, p.widgets, folder); + g_free(folder); + if (!r.ok) { + status("Generate failed — " + r.error); + return; + } + std::string list; + for (const std::string& f : r.files) list += f + "\n"; + status("Generated " + std::to_string(r.files.size()) + + " files — build with make in that folder"); + GtkWidget* msg = gtk_message_dialog_new( + GTK_WINDOW(g_window), GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, + GTK_BUTTONS_OK, "Generated C++/GTK project:\n\n%s", list.c_str()); + gtk_dialog_run(GTK_DIALOG(msg)); + gtk_widget_destroy(msg); +} + +// ------------------------------------------------------- file operations + +static void update_title() { + std::string name = "Untitled"; + if (!g_current_file.empty()) { + size_t slash = g_current_file.find_last_of('/'); + name = slash == std::string::npos + ? g_current_file + : g_current_file.substr(slash + 1); + } + gtk_window_set_title(GTK_WINDOW(g_window), + ("FDesigner — C++/GTK — " + name).c_str()); +} + +static bool save_to(const std::string& path) { + Project p = snapshot_project(); + std::string err; + if (!save_project_file(path, p, err)) { + status("Save failed — " + err); + return false; + } + g_current_file = path; + update_title(); + status("Saved " + path); + return true; +} + +static GtkWidget* make_fdj_filter_dialog(const char* title, + GtkFileChooserAction action) { + GtkWidget* dlg = gtk_file_chooser_dialog_new( + title, GTK_WINDOW(g_window), action, "_Cancel", GTK_RESPONSE_CANCEL, + action == GTK_FILE_CHOOSER_ACTION_SAVE ? "_Save" : "_Open", + GTK_RESPONSE_ACCEPT, nullptr); + GtkFileFilter* filter = gtk_file_filter_new(); + gtk_file_filter_set_name(filter, "FDesigner projects (*.fdj)"); + gtk_file_filter_add_pattern(filter, "*.fdj"); + gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dlg), filter); + return dlg; +} + +static void on_save(GtkMenuItem*, gpointer) { + if (!g_current_file.empty()) { + save_to(g_current_file); + return; + } + GtkWidget* dlg = + make_fdj_filter_dialog("Save project", GTK_FILE_CHOOSER_ACTION_SAVE); + gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dlg), + TRUE); + std::string defname = lower(g_project.class_name) + ".fdj"; + gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dlg), defname.c_str()); + if (gtk_dialog_run(GTK_DIALOG(dlg)) == GTK_RESPONSE_ACCEPT) { + gchar* path = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dlg)); + if (path) { + std::string p = path; + g_free(path); + if (p.size() < 4 || p.substr(p.size() - 4) != ".fdj") p += ".fdj"; + save_to(p); + } + } + gtk_widget_destroy(dlg); +} + +static void on_open(GtkMenuItem*, gpointer) { + GtkWidget* dlg = + make_fdj_filter_dialog("Open project", GTK_FILE_CHOOSER_ACTION_OPEN); + if (gtk_dialog_run(GTK_DIALOG(dlg)) == GTK_RESPONSE_ACCEPT) { + gchar* path = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dlg)); + if (path) { + std::string p = path; + g_free(path); + Project proj; + std::string err; + if (load_project_file(p, proj, err)) { + load_into_canvas(proj); + g_current_file = p; + update_title(); + } else { + status("Open failed — " + err); + } + } + } + gtk_widget_destroy(dlg); +} + +static void on_new(GtkMenuItem*, gpointer) { + if (!g_items.empty()) { + GtkWidget* ask = gtk_message_dialog_new( + GTK_WINDOW(g_window), GTK_DIALOG_MODAL, GTK_MESSAGE_QUESTION, + GTK_BUTTONS_YES_NO, + "Start a new design? The current canvas will be cleared."); + if (gtk_dialog_run(GTK_DIALOG(ask)) != GTK_RESPONSE_YES) { + gtk_widget_destroy(ask); + return; + } + gtk_widget_destroy(ask); + } + clear_canvas(); + g_project = Project(); + set_project_size(g_project.win_width, g_project.win_height); + set_project_background(""); + g_current_file.clear(); + update_title(); + refresh_props(); + status("New design — pick a widget from the palette to place it"); +} + +// ------------------------------------------------------- self-test + +static gboolean selftest_step(gpointer) { + bool ok = true; + auto check = [&](bool cond) { + if (!cond) ok = false; + }; + std::string err; + + // Phase 1: add / select / move / resize / delete + Item* btn = add_widget("Button"); + Item* lbl = add_widget("Label"); + check(btn && lbl && g_items.size() == 2); + select_item(btn); + move_item(btn, 120, 90); + resize_item(btn, 140, 40); + delete_item(lbl); + check(g_items.size() == 1 && g_selected == btn && btn->model.x == 120 && + btn->model.y == 90 && btn->model.width == 140 && + btn->model.height == 40 && btn->model.name == "Button_001"); + + // Phase 2: single-property set, live on the widget + check(set_prop(btn, "label", "Hello", err)); + check(btn->model.props["label"] == "Hello"); + check(std::strcmp(gtk_button_get_label(GTK_BUTTON(btn->inner)), + "Hello") == 0); + check(set_prop(btn, "relief", "None", err)); + check(btn->model.props["relief"] == "2"); + check(!set_prop(btn, "relief", "bogus", err)); + check(!set_prop(btn, "nosuchprop", "x", err)); + + // Phase 2: bulk ops + Item* ent = add_widget("Entry"); + Item* lbl2 = add_widget("Label"); + check(ent && lbl2 && g_items.size() == 3); + check(bulk_set("background", "#00ff00", BulkMode::All, "", err) == 3); + check(btn->model.props["background"] == "rgb(0,255,0)"); + select_item(btn); + check(bulk_set("label", "SelBtn", BulkMode::Selected, "", err) == 1); + check(btn->model.props["label"] == "SelBtn"); + check(bulk_set("label", "Clicked", BulkMode::Filtered, "Butt", err) == 1); + check(btn->model.props["label"] == "Clicked"); + check(lbl2->model.props["label"] == lbl2->model.name); + check(bulk_clear("background") == 3); + check(btn->model.props.count("background") == 0); + + // Phase 2: window properties + set_project_title("Test Title"); + set_project_size(1024, 700); + set_project_background("#102030"); + check(g_project.title == "Test Title" && g_project.win_width == 1024 && + g_project.win_height == 700 && g_project.background == "#102030"); + + // Phase 3: generate a project from the current canvas and verify the + // output shape (the caller compiles + runs it as the real proof). + { + Item* combo = add_widget("Combobox"); + Item* scale = add_widget("Scale"); + Item* text = add_widget("Text"); + check(combo && scale && text); + Project snap = snapshot_project(); + GenResult gr = generate_project(snap, snap.widgets, + "/tmp/fdesigner_gen_test"); + check(gr.ok && gr.files.size() == 4 + 2 * snap.widgets.size()); + check(!g_file_test("/tmp/fdesigner_gen_test/mywindow.ui", + G_FILE_TEST_EXISTS)); // all code, no .ui + gchar* code_text = nullptr; + check(g_file_get_contents("/tmp/fdesigner_gen_test/MyWindow.cpp", + &code_text, nullptr, nullptr) && + code_text != nullptr); + if (code_text) { + std::string code = code_text; + check(code.find("new Button_001(fixed_)") != std::string::npos); + check(code.find("Root_001_buttonRelease3") != + std::string::npos); + check(code.find("\"Test Title\"") != std::string::npos); + g_free(code_text); + } + gchar* btn_text = nullptr; + check(g_file_get_contents("/tmp/fdesigner_gen_test/Button_001.cpp", + &btn_text, nullptr, nullptr) && + btn_text != nullptr); + if (btn_text) { + std::string code = btn_text; + check(code.find("gtk_button_new_with_label") != + std::string::npos); + check(code.find("gtk_fixed_put") != std::string::npos); + check(code.find("Button_001::buttonPress1") != + std::string::npos); + check(code.find("util_error_print") != std::string::npos); + g_free(btn_text); + } + std::printf("GENERATED /tmp/fdesigner_gen_test\n"); + } + + // Phase 4: save -> clear -> load round-trip, counter continuation, + // and bad-file rejection. + { + Project snap = snapshot_project(); + std::string err; + check(save_project_file("/tmp/fdesigner_test.fdj", snap, err)); + clear_canvas(); + check(g_items.empty()); + Project loaded; + check(load_project_file("/tmp/fdesigner_test.fdj", loaded, err)); + load_into_canvas(loaded); + check(g_items.size() == 6); + Item* b = nullptr; + for (Item* it : g_items) + if (it->model.name == "Button_001") b = it; + check(b && b->model.x == 120 && b->model.y == 90 && + b->model.width == 140 && b->model.height == 40 && + b->model.props["label"] == "Clicked" && + b->model.props["relief"] == "2"); + check(g_project.title == "Test Title" && + g_project.win_width == 1024 && g_project.win_height == 700 && + g_project.background == "#102030"); + Item* b2 = add_widget("Button"); + check(b2 && b2->model.name == "Button_002"); + delete_item(b2); + check(g_items.size() == 6); + check(g_file_set_contents("/tmp/fdesigner_bad.fdj", + "this is not json", -1, nullptr)); + Project junk; + check(!load_project_file("/tmp/fdesigner_bad.fdj", junk, err)); + } + + // Phase 5: Toplevel + Spreadsheet widgets, colour palette, menu bar. + { + std::string err; + // Property ranges are enforced: out-of-range values are rejected + // so generated code never hands GTK an invalid property value. + Item* lab = nullptr; + Item* ent = nullptr; + for (Item* it : g_items) { + if (it->model.type == "Label") lab = it; + if (it->model.type == "Entry") ent = it; + } + check(lab && !set_prop(lab, "xalign", "42.5", err)); + check(lab && set_prop(lab, "xalign", "0.5", err)); + check(ent && !set_prop(ent, "max-length", "-5", err)); + + Item* top = add_widget("Toplevel"); + check(top != nullptr); + check(set_prop(top, "title", "Settings", err)); + check(top->model.props["title"] == "Settings"); + GtkWidget* hdr = toplevel_header(top); + check(hdr && std::string(gtk_label_get_text(GTK_LABEL(hdr))) == + "Settings"); + + Item* ss = add_widget("Spreadsheet"); + check(ss != nullptr); + check(set_prop(ss, "rows", "3", err)); + check(set_prop(ss, "columns", "2", err)); + GtkWidget* grid = spreadsheet_grid(ss); + check(grid != nullptr); + GList* cells = gtk_container_get_children(GTK_CONTAINER(grid)); + check(g_list_length(cells) == 6); + g_list_free(cells); + + // Colour palette path: applies to the current selection. + Item* btn = nullptr; + for (Item* it : g_items) + if (it->model.name == "Button_001") btn = it; + check(btn != nullptr); + select_item(btn); + apply_colour("#ff0000", "background"); + check(btn->model.props["background"] == "rgb(255,0,0)"); + apply_colour("#0000ff", "foreground"); + check(btn->model.props["foreground"] == "rgb(0,0,255)"); + + // Menu bar + full Phase 5 generation. + g_project.menubar = "File: New | Open | - | Quit\nHelp: About"; + Project snap = snapshot_project(); + GenResult gr = generate_project(snap, snap.widgets, + "/tmp/fdesigner_gen_test5"); + check(gr.ok); + gchar* code_text = nullptr; + check(g_file_get_contents("/tmp/fdesigner_gen_test5/MyWindow.cpp", + &code_text, nullptr, nullptr) && + code_text != nullptr); + if (code_text) { + std::string code = code_text; + check(code.find("gtk_menu_bar_new") != std::string::npos); + check(code.find("menu_file_quit") != std::string::npos); + check(code.find("gtk_separator_menu_item_new") != + std::string::npos); + g_free(code_text); + } + gchar* top_text = nullptr; + check(g_file_get_contents( + "/tmp/fdesigner_gen_test5/Toplevel_001.cpp", &top_text, + nullptr, nullptr) && + top_text != nullptr); + if (top_text) { + check(std::string(top_text).find("\"Settings\"") != + std::string::npos); + g_free(top_text); + } + gchar* ss_text = nullptr; + check(g_file_get_contents( + "/tmp/fdesigner_gen_test5/Spreadsheet_001.cpp", &ss_text, + nullptr, nullptr) && + ss_text != nullptr); + if (ss_text) { + check(std::string(ss_text).find("gtk_grid_new") != + std::string::npos); + g_free(ss_text); + } + std::printf("GENERATED5 /tmp/fdesigner_gen_test5\n"); + + // Round-trip with the Phase 5 pieces included. + check(save_project_file("/tmp/fdesigner_test5.fdj", snap, err)); + clear_canvas(); + Project loaded; + check(load_project_file("/tmp/fdesigner_test5.fdj", loaded, err)); + load_into_canvas(loaded); + check(g_items.size() == 8); + check(g_project.menubar == snap.menubar); + bool found_top = false; + for (Item* it : g_items) + if (it->model.type == "Toplevel" && + it->model.props["title"] == "Settings") + found_top = true; + check(found_top); + } + + // Phase 6: StockChart — preview series, Massive CSV load, generation. + { + std::string err; + Item* chart = add_widget("StockChart"); + check(chart != nullptr); + { // stage the fake Massive CSV (an earlier session left it in + // /tmp; a fresh VM does not have it) + std::ifstream srcf("tests/data/SPY_fake.csv", + std::ios::binary); + std::ofstream dstf("/tmp/SPY_fake.csv", std::ios::binary); + dstf << srcf.rdbuf(); + } + check(!chart->candles.empty()); // built-in preview series + check(set_prop(chart, "data-file", "/tmp/SPY_fake.csv", err)); + check(chart->candles.size() == 120); + check(chart->candles[0].open > 659 && + chart->candles[0].open < 661); + check(set_prop(chart, "up-color", "#00ff00", err)); + check(set_prop(chart, "indicators", + "SMA(20), RSI, MACD(12,26,9), Engulfing", err)); + // The designer-side parser resolves all four specs (two of them + // carry commas inside their parameter lists). + check(chart_indicators(chart).size() == 4); + check(fdi::all_defs().size() == 250); + // Phase 7: drawings module, comparison series, percent scale. + check(fdw::parse_specs("trend 1 2 3 4 red; hline 650; " + "text 5 660 purple Breakout") + .size() == 3); + check(set_prop(chart, "scale-mode", "1", err)); + check(set_prop(chart, "drawings", + "trend 2 660.5 100 668.25 red; hline 655 blue", + err)); + if (g_file_test("tests/data/QQQ_fake_long.csv", + G_FILE_TEST_EXISTS)) { + check(set_prop(chart, "compare-files", + "tests/data/QQQ_fake_long.csv", err)); + check(chart->compares.size() == 1); + check(chart->compare_names.size() == 1 && + chart->compare_names[0] == "QQQ"); + check(chart->ticker == "SPY"); + } + // Wheel zoom on the preview: one up-scroll shrinks the window. + { + GdkEventScroll ev{}; + ev.type = GDK_SCROLL; + ev.direction = GDK_SCROLL_UP; + ev.x = 60; + ev.y = 10; + on_chart_scroll(nullptr, &ev, chart); + check(chart->chart_count > 0 && + chart->chart_count < (double)chart->candles.size()); + } + Project snap3 = snapshot_project(); + GenResult gr3 = generate_project(snap3, snap3.widgets, + "/tmp/fdesigner_gen_test6"); + check(gr3.ok); + check(g_file_test("/tmp/fdesigner_gen_test6/indicators.h", + G_FILE_TEST_EXISTS)); + check(g_file_test("/tmp/fdesigner_gen_test6/drawings.h", + G_FILE_TEST_EXISTS)); + gchar* chart_text = nullptr; + check(g_file_get_contents( + "/tmp/fdesigner_gen_test6/StockChart_001.cpp", + &chart_text, nullptr, nullptr) && + chart_text != nullptr); + if (chart_text) { + std::string code = chart_text; + check(code.find("load_csv") != std::string::npos); + check(code.find("cairo_rectangle") != std::string::npos); + check(code.find("add_indicator(\"SMA(20)\")") != + std::string::npos); + check(code.find("add_indicator(\"MACD(12,26,9)\")") != + std::string::npos); + check(code.find("add_drawing(\"trend 2 660.5 100 668.25 " + "red\")") != std::string::npos); + check(code.find("set_scale_mode(1)") != std::string::npos); + check(code.find("on_chart_scroll") != std::string::npos); + check(code.find("fmt_axis_time") != std::string::npos); + if (g_file_test("tests/data/QQQ_fake_long.csv", + G_FILE_TEST_EXISTS)) + check(code.find("add_compare(") != std::string::npos); + g_free(chart_text); + } + gchar* chart_h = nullptr; + check(g_file_get_contents( + "/tmp/fdesigner_gen_test6/StockChart_001.h", &chart_h, + nullptr, nullptr) && + chart_h != nullptr); + if (chart_h) { + check(std::string(chart_h).find("struct Candle") != + std::string::npos); + g_free(chart_h); + } + } + + std::printf("SELFTEST %s items=%zu\n", ok ? "PASS" : "FAIL", + g_items.size()); + std::fflush(stdout); + g_selftest_rc = ok ? 0 : 1; + gtk_main_quit(); + return G_SOURCE_REMOVE; +} + +// ------------------------------------------------------- main + +int main(int argc, char** argv) { + gtk_init(&argc, &argv); + + // Selection outline for placed widgets. + GtkCssProvider* css = gtk_css_provider_new(); + gtk_css_provider_load_from_data( + css, + ".sel { border: 2px solid #3584e4; " + "background: rgba(53,132,228,0.10); }", + -1, nullptr); + gtk_style_context_add_provider_for_screen( + gdk_screen_get_default(), GTK_STYLE_PROVIDER(css), + GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); + + GtkBuilder* builder = gtk_builder_new(); + GError* error = nullptr; + if (!gtk_builder_add_from_file(builder, "ui/designer.ui", &error)) { + g_clear_error(&error); + if (!gtk_builder_add_from_file(builder, "../ui/designer.ui", &error)) { + std::fprintf(stderr, "fdesigner: cannot load designer.ui: %s\n", + error ? error->message : "unknown error"); + return 1; + } + } + + g_window = GTK_WIDGET(gtk_builder_get_object(builder, "main_window")); + g_canvas = GTK_WIDGET(gtk_builder_get_object(builder, "canvas")); + g_statusbar = GTK_WIDGET(gtk_builder_get_object(builder, "statusbar")); + g_props_grid = GTK_WIDGET(gtk_builder_get_object(builder, "props_grid")); + g_props_box = GTK_WIDGET(gtk_builder_get_object(builder, "props_box")); + g_selected_label = + GTK_WIDGET(gtk_builder_get_object(builder, "selected_label")); + if (!g_window || !g_canvas || !g_statusbar || !g_props_grid || + !g_props_box || !g_selected_label) { + std::fprintf(stderr, "fdesigner: designer.ui is missing core objects\n"); + return 1; + } + g_status_ctx = gtk_statusbar_get_context_id(GTK_STATUSBAR(g_statusbar), + "main"); + + // Palette buttons -> add_widget(type) + for (const TypeInfo& t : kTypes) { + GObject* b = gtk_builder_get_object(builder, t.palette_id); + if (b) + g_signal_connect(b, "clicked", G_CALLBACK(on_palette_clicked), + (gpointer)t.key); + } + + // Clicking empty canvas deselects: the GtkFixed has no window of its + // own, so empty-area presses land on its parent GtkViewport. + g_viewport = gtk_widget_get_parent(g_canvas); + if (g_viewport) { + gtk_widget_add_events(g_viewport, GDK_BUTTON_PRESS_MASK); + g_signal_connect(g_viewport, "button-press-event", + G_CALLBACK(on_canvas_press), nullptr); + g_canvas_css = gtk_css_provider_new(); + gtk_style_context_add_provider( + gtk_widget_get_style_context(g_viewport), + GTK_STYLE_PROVIDER(g_canvas_css), GTK_STYLE_PROVIDER_PRIORITY_USER); + } + + GObject* quit = gtk_builder_get_object(builder, "mi_quit"); + if (quit) g_signal_connect(quit, "activate", G_CALLBACK(on_quit), nullptr); + GObject* gen = gtk_builder_get_object(builder, "mi_generate"); + if (gen) + g_signal_connect(gen, "activate", G_CALLBACK(on_generate), nullptr); + GObject* mi_new = gtk_builder_get_object(builder, "mi_new"); + if (mi_new) + g_signal_connect(mi_new, "activate", G_CALLBACK(on_new), nullptr); + GObject* mi_open = gtk_builder_get_object(builder, "mi_open"); + if (mi_open) + g_signal_connect(mi_open, "activate", G_CALLBACK(on_open), nullptr); + GObject* mi_save = gtk_builder_get_object(builder, "mi_save"); + if (mi_save) + g_signal_connect(mi_save, "activate", G_CALLBACK(on_save), nullptr); + + g_signal_connect(g_window, "destroy", G_CALLBACK(gtk_main_quit), nullptr); + g_signal_connect(g_window, "key-press-event", G_CALLBACK(on_key_press), + nullptr); + + build_bulk_ui(); + build_colour_ui(); + refresh_props(); // window properties until something is selected + status("Phase 2 — pick a widget from the palette to place it"); + gtk_widget_show_all(g_window); + + bool selftest = std::getenv("FDESIGNER_SELFTEST") != nullptr; + if (selftest) g_idle_add(selftest_step, nullptr); + + gtk_main(); + return selftest ? g_selftest_rc : 0; +} diff --git a/src/model.h b/src/model.h new file mode 100644 index 0000000..ff92755 --- /dev/null +++ b/src/model.h @@ -0,0 +1,65 @@ +// FDesigner model — the designed window as data. +// Mirrors the Python original's widget dict: every placed widget has a name, +// a type, absolute geometry (Tk .place() style), and a property map. + +#pragma once + +#include +#include +#include + +struct Widget { + std::string name; // e.g. "Button_001" + std::string type; // palette type key, e.g. "Button" + int x = 0, y = 0; + int width = 100, height = 30; + std::map props; // GTK properties, as strings + std::vector signals; // signals to stub in generated code +}; + +struct Project { + std::string title = "Main Window"; + std::string class_name = "MyWindow"; + int win_width = 800; + int win_height = 600; + std::string background; // window background colour, "" = default + // Menu bar spec, one menu per line: "File: New | Open | - | Quit" + // ("-" is a separator). Empty = no menu bar. + std::string menubar; + std::vector widgets; +}; + +// One row of the palette: type key, button label in the .ui, default size, +// and whether this phase implements it (Toplevel/Spreadsheet arrive later). +struct TypeInfo { + const char* key; + const char* palette_id; // GtkBuilder id of the palette button + int def_width; + int def_height; + bool phase1; +}; + +inline const TypeInfo kTypes[] = { + {"Button", "pal_button", 100, 30, true }, + {"Label", "pal_label", 100, 24, true }, + {"Entry", "pal_entry", 160, 28, true }, + {"Checkbutton", "pal_checkbutton", 120, 26, true }, + {"Radiobutton", "pal_radiobutton", 120, 26, true }, + {"Frame", "pal_frame", 200, 120, true }, + {"Combobox", "pal_combobox", 160, 28, true }, + {"Listbox", "pal_listbox", 180, 110, true }, + {"Menubutton", "pal_menubutton", 110, 30, true }, + {"Canvas", "pal_canvas", 200, 140, true }, + {"Scale", "pal_scale", 160, 28, true }, + {"Scrollbar", "pal_scrollbar", 160, 18, true }, + {"Text", "pal_text", 220, 120, true }, + {"Toplevel", "pal_toplevel", 300, 200, true }, + {"Spreadsheet", "pal_spreadsheet", 320, 180, true }, + {"StockChart", "pal_stockchart", 320, 200, true }, +}; + +inline const TypeInfo* find_type(const std::string& key) { + for (const TypeInfo& t : kTypes) + if (key == t.key) return &t; + return nullptr; +} diff --git a/src/project_io.cpp b/src/project_io.cpp new file mode 100644 index 0000000..9947c2b --- /dev/null +++ b/src/project_io.cpp @@ -0,0 +1,358 @@ +// FDesigner project files (.fdj) — see project_io.h. + +#include "project_io.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +// ---------------------------------------------------------------- writing + +static std::string json_escape(const std::string& s) { + std::string out; + for (unsigned char c : s) { + switch (c) { + case '"': out += "\\\""; break; + case '\\': out += "\\\\"; break; + case '\n': out += "\\n"; break; + case '\r': out += "\\r"; break; + case '\t': out += "\\t"; break; + default: + if (c < 0x20) { + char buf[8]; + std::snprintf(buf, sizeof buf, "\\u%04x", c); + out += buf; + } else { + out += (char)c; + } + } + } + return out; +} + +bool save_project_file(const std::string& path, const Project& p, + std::string& err) { + std::ostringstream o; + o << "{\n" + << " \"format\": \"fdesigner\",\n" + << " \"version\": 1,\n" + << " \"window\": {\n" + << " \"title\": \"" << json_escape(p.title) << "\",\n" + << " \"class_name\": \"" << json_escape(p.class_name) << "\",\n" + << " \"width\": " << p.win_width << ",\n" + << " \"height\": " << p.win_height << ",\n" + << " \"background\": \"" << json_escape(p.background) << "\",\n" + << " \"menubar\": \"" << json_escape(p.menubar) << "\"\n" + << " },\n" + << " \"widgets\": ["; + for (size_t i = 0; i < p.widgets.size(); ++i) { + const Widget& w = p.widgets[i]; + o << (i ? ",\n" : "\n") << " {\n" + << " \"name\": \"" << json_escape(w.name) << "\",\n" + << " \"type\": \"" << json_escape(w.type) << "\",\n" + << " \"x\": " << w.x << ", \"y\": " << w.y + << ", \"width\": " << w.width << ", \"height\": " << w.height + << ",\n" + << " \"props\": {"; + bool first = true; + for (const auto& [k, v] : w.props) { + o << (first ? "" : ",") << "\n \"" << json_escape(k) + << "\": \"" << json_escape(v) << "\""; + first = false; + } + o << (w.props.empty() ? "" : "\n ") << "}\n" + << " }"; + } + o << (p.widgets.empty() ? "" : "\n ") << "]\n}\n"; + + std::ofstream f(path); + if (!f) { + err = "cannot open " + path + " for writing"; + return false; + } + f << o.str(); + if (!f) { + err = "write failed for " + path; + return false; + } + return true; +} + +// ---------------------------------------------------------------- reading + +struct Json { + enum Type { Null, Bool, Num, Str, Arr, Obj } type = Null; + bool boolean = false; + double num = 0; + std::string str; + std::vector arr; + std::map obj; + const Json* find(const std::string& key) const { + if (type != Obj) return nullptr; + auto it = obj.find(key); + return it == obj.end() ? nullptr : &it->second; + } +}; + +struct Parser { + const char* p; + std::string err; + + void ws() { + while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r') ++p; + } + bool consume(char c) { + ws(); + if (*p == c) { + ++p; + return true; + } + return false; + } + static void utf8_encode(std::string& out, unsigned cp) { + if (cp < 0x80) { + out += (char)cp; + } else if (cp < 0x800) { + out += (char)(0xC0 | (cp >> 6)); + out += (char)(0x80 | (cp & 0x3F)); + } else if (cp < 0x10000) { + out += (char)(0xE0 | (cp >> 12)); + out += (char)(0x80 | ((cp >> 6) & 0x3F)); + out += (char)(0x80 | (cp & 0x3F)); + } else { + out += (char)(0xF0 | (cp >> 18)); + out += (char)(0x80 | ((cp >> 12) & 0x3F)); + out += (char)(0x80 | ((cp >> 6) & 0x3F)); + out += (char)(0x80 | (cp & 0x3F)); + } + } + bool parse_string(std::string& out) { + if (*p != '"') { + err = "expected string"; + return false; + } + ++p; + while (*p && *p != '"') { + unsigned char c = (unsigned char)*p; + if (c == '\\') { + ++p; + switch (*p) { + case 'n': out += '\n'; break; + case 't': out += '\t'; break; + case 'r': out += '\r'; break; + case 'b': out += '\b'; break; + case 'f': out += '\f'; break; + case '/': out += '/'; break; + case '\\': out += '\\'; break; + case '"': out += '"'; break; + case 'u': { + unsigned cp = 0; + for (int i = 0; i < 4; ++i) { + ++p; + if (!std::isxdigit((unsigned char)*p)) { + err = "bad \\u escape"; + return false; + } + cp = cp * 16 + + (unsigned)(std::isdigit((unsigned char)*p) + ? *p - '0' + : (std::tolower( + (unsigned char)*p) - + 'a' + 10)); + } + // Surrogate pair? + if (cp >= 0xD800 && cp <= 0xDBFF && p[1] == '\\' && + p[2] == 'u') { + unsigned lo = 0; + bool okpair = true; + const char* save = p; + p += 2; + for (int i = 0; i < 4; ++i) { + ++p; + if (!std::isxdigit((unsigned char)*p)) { + okpair = false; + break; + } + lo = lo * 16 + + (unsigned)(std::isdigit( + (unsigned char)*p) + ? *p - '0' + : (std::tolower( + (unsigned char)*p) - + 'a' + 10)); + } + if (okpair && lo >= 0xDC00 && lo <= 0xDFFF) + cp = 0x10000 + ((cp - 0xD800) << 10) + + (lo - 0xDC00); + else + p = save; + } + utf8_encode(out, cp); + break; + } + default: + err = "bad escape"; + return false; + } + ++p; + } else { + out += (char)c; + ++p; + } + } + if (*p != '"') { + err = "unterminated string"; + return false; + } + ++p; + return true; + } + bool parse_value(Json& v) { + ws(); + if (*p == '{') { + ++p; + v.type = Json::Obj; + if (consume('}')) return true; + while (true) { + ws(); + std::string key; + if (!parse_string(key)) return false; + if (!consume(':')) { + err = "expected ':'"; + return false; + } + Json child; + if (!parse_value(child)) return false; + v.obj[key] = child; + if (consume(',')) continue; + if (consume('}')) return true; + err = "expected ',' or '}'"; + return false; + } + } + if (*p == '[') { + ++p; + v.type = Json::Arr; + if (consume(']')) return true; + while (true) { + Json child; + if (!parse_value(child)) return false; + v.arr.push_back(child); + if (consume(',')) continue; + if (consume(']')) return true; + err = "expected ',' or ']'"; + return false; + } + } + if (*p == '"') { + v.type = Json::Str; + return parse_string(v.str); + } + if (std::strncmp(p, "true", 4) == 0) { + p += 4; + v.type = Json::Bool; + v.boolean = true; + return true; + } + if (std::strncmp(p, "false", 5) == 0) { + p += 5; + v.type = Json::Bool; + v.boolean = false; + return true; + } + if (std::strncmp(p, "null", 4) == 0) { + p += 4; + v.type = Json::Null; + return true; + } + char* end = nullptr; + v.num = std::strtod(p, &end); + if (end == p) { + err = "unexpected character"; + return false; + } + p = end; + v.type = Json::Num; + return true; + } +}; + +static std::string jstr(const Json* j, const std::string& dflt) { + return j && j->type == Json::Str ? j->str : dflt; +} +static int jint(const Json* j, int dflt) { + return j && j->type == Json::Num ? (int)j->num : dflt; +} + +bool load_project_file(const std::string& path, Project& out, + std::string& err) { + std::ifstream f(path); + if (!f) { + err = "cannot open " + path; + return false; + } + std::stringstream ss; + ss << f.rdbuf(); + std::string text = ss.str(); + + Parser parser{text.c_str(), ""}; + Json root; + if (!parser.parse_value(root)) { + err = "invalid JSON in " + path + ": " + parser.err; + return false; + } + parser.ws(); + if (*parser.p) { + err = "trailing data after JSON in " + path; + return false; + } + if (jstr(root.find("format"), "") != "fdesigner") { + err = path + " is not an fdesigner project file"; + return false; + } + if (jint(root.find("version"), 0) != 1) { + err = path + " has an unsupported format version"; + return false; + } + + Project p; + if (const Json* win = root.find("window")) { + p.title = jstr(win->find("title"), p.title); + p.class_name = jstr(win->find("class_name"), p.class_name); + p.win_width = jint(win->find("width"), p.win_width); + p.win_height = jint(win->find("height"), p.win_height); + p.background = jstr(win->find("background"), ""); + p.menubar = jstr(win->find("menubar"), ""); + } + if (p.win_width < 50 || p.win_width > 20000) p.win_width = 800; + if (p.win_height < 50 || p.win_height > 20000) p.win_height = 600; + + if (const Json* widgets = root.find("widgets"); + widgets && widgets->type == Json::Arr) { + for (const Json& jw : widgets->arr) { + Widget w; + w.name = jstr(jw.find("name"), ""); + w.type = jstr(jw.find("type"), ""); + if (w.name.empty() || w.type.empty()) continue; + w.x = jint(jw.find("x"), 0); + w.y = jint(jw.find("y"), 0); + w.width = jint(jw.find("width"), 100); + w.height = jint(jw.find("height"), 30); + if (w.width < 10) w.width = 10; + if (w.height < 10) w.height = 10; + if (const Json* props = jw.find("props"); + props && props->type == Json::Obj) { + for (const auto& [k, v] : props->obj) + if (v.type == Json::Str) w.props[k] = v.str; + } + p.widgets.push_back(w); + } + } + out = p; + return true; +} diff --git a/src/project_io.h b/src/project_io.h new file mode 100644 index 0000000..60974fe --- /dev/null +++ b/src/project_io.h @@ -0,0 +1,24 @@ +// FDesigner project files (.fdj) — JSON save/load, replacing the Python +// original's pickle files. Hand-rolled JSON (the model is small; no new +// dependencies). Format version 1: +// +// { "format": "fdesigner", "version": 1, +// "window": { "title", "class_name", "width", "height", "background" }, +// "widgets": [ { "name", "type", "x", "y", "width", "height", +// "props": { name: value, ... } } ] } +// +// Loading is tolerant of unknown keys and missing fields, but strict about +// the format tag. Property values are NOT validated here — the designer +// re-validates every property through set_prop() when it rebuilds the +// canvas, so a hand-edited file can't smuggle in bad values. + +#pragma once + +#include + +#include "model.h" + +bool save_project_file(const std::string& path, const Project& project, + std::string& err); +bool load_project_file(const std::string& path, Project& project_out, + std::string& err); diff --git a/src/props.h b/src/props.h new file mode 100644 index 0000000..215d7fa --- /dev/null +++ b/src/props.h @@ -0,0 +1,153 @@ +// FDesigner property tables — the GTK counterpart of the original's +// per-widget parameter mapping (the Entry_001..019 tables in fdesigner7.py). +// Each palette type lists the properties the designer can edit; values are +// stored in Widget.props as canonical strings and applied live with +// g_object_set (or, for the two pseudo-properties background/foreground, +// through a per-widget CSS provider — GTK's equivalent of Tk's bg/fg config). + +#pragma once + +#include +#include + +enum class PropKind { Str, Bool, Int, Double, Color, Enum }; + +struct PropDef { + const char* name; // GTK property name, or background/foreground + PropKind kind; + const char* def; // default value (canonical string), "" = none + double lo = 0, hi = 0; // spin range for Int/Double + const char* enum_labels = nullptr; // Enum: "Label A|Label B|..." + const int* enum_values = nullptr; // Enum: value per label +}; + +// Enum value tables (GTK 3 constants, written out so this header stays +// independent of the GTK headers). +inline const int kReliefValues[] = {0, 1, 2}; // NORMAL, HALF, NONE +inline const int kShadowValues[] = {0, 1, 2, 3, 4}; // NONE, IN, OUT, ETCHED_IN, ETCHED_OUT +inline const int kSelModeValues[] = {0, 1, 2, 3}; // NONE, SINGLE, BROWSE, MULTIPLE +inline const int kArrowValues[] = {0, 1, 2, 3, 4}; // UP, DOWN, LEFT, RIGHT, NONE +inline const int kWrapValues[] = {0, 1, 2, 3}; // NONE, CHAR, WORD, WORD_CHAR +inline const int kScaleModeValues[] = {0, 1}; // PRICE, PERCENT + +// Properties every widget gets (the original configures bg/fg on nearly +// every widget type). +inline const std::vector& common_props() { + static const std::vector v = { + {"background", PropKind::Color, ""}, + {"foreground", PropKind::Color, ""}, + {"tooltip-text", PropKind::Str, ""}, + }; + return v; +} + +inline const std::vector& props_for(const std::string& type) { + using K = PropKind; + static const std::vector none; + static const std::vector button = { + {"label", K::Str, ""}, + {"relief", K::Enum, "0", 0, 0, "Normal|Half|None", kReliefValues}, + {"focus-on-click", K::Bool, "true"}, + }; + static const std::vector label = { + {"label", K::Str, ""}, + {"xalign", K::Double, "0.5", 0.0, 1.0}, + {"selectable", K::Bool, "false"}, + {"wrap", K::Bool, "false"}, + }; + static const std::vector entry = { + {"text", K::Str, ""}, + {"placeholder-text", K::Str, ""}, + {"max-length", K::Int, "0", 0, 100000}, + {"width-chars", K::Int, "-1", -1, 1000}, + {"editable", K::Bool, "true"}, + {"visibility", K::Bool, "true"}, + }; + static const std::vector checkbutton = { + {"label", K::Str, ""}, + {"active", K::Bool, "false"}, + {"inconsistent", K::Bool, "false"}, + }; + static const std::vector radiobutton = { + {"label", K::Str, ""}, + {"active", K::Bool, "false"}, + }; + static const std::vector frame = { + {"label", K::Str, ""}, + {"shadow-type", K::Enum, "3", 0, 0, "None|In|Out|Etched in|Etched out", + kShadowValues}, + }; + static const std::vector combobox = { + {"active", K::Int, "0", -1, 1000}, + }; + static const std::vector listbox = { + {"selection-mode", K::Enum, "1", 0, 0, "None|Single|Browse|Multiple", + kSelModeValues}, + }; + static const std::vector menubutton = { + {"label", K::Str, ""}, + {"direction", K::Enum, "1", 0, 0, "Up|Down|Left|Right|None", + kArrowValues}, + }; + static const std::vector scale = { + {"value", K::Double, "0", 0, 100}, + {"digits", K::Int, "1", -1, 10}, + {"draw-value", K::Bool, "true"}, + {"inverted", K::Bool, "false"}, + }; + static const std::vector scrollbar = { + {"value", K::Double, "0", 0, 100}, + {"inverted", K::Bool, "false"}, + }; + static const std::vector text = { + {"editable", K::Bool, "true"}, + {"cursor-visible", K::Bool, "true"}, + {"wrap-mode", K::Enum, "0", 0, 0, "None|Char|Word|Word-char", + kWrapValues}, + }; + if (type == "Button") return button; + if (type == "Label") return label; + if (type == "Entry") return entry; + if (type == "Checkbutton") return checkbutton; + if (type == "Radiobutton") return radiobutton; + if (type == "Frame") return frame; + if (type == "Combobox") return combobox; + if (type == "Listbox") return listbox; + if (type == "Menubutton") return menubutton; + if (type == "Scale") return scale; + if (type == "Scrollbar") return scrollbar; + if (type == "Text") return text; + static const std::vector toplevel = { + {"title", K::Str, ""}, + {"resizable", K::Bool, "true"}, + }; + static const std::vector spreadsheet = { + {"rows", K::Int, "5", 1, 50}, + {"columns", K::Int, "4", 1, 26}, + }; + if (type == "Toplevel") return toplevel; + if (type == "Spreadsheet") return spreadsheet; + static const std::vector stockchart = { + {"data-file", K::Str, ""}, + {"up-color", K::Color, "rgb(0,200,0)"}, + {"down-color", K::Color, "rgb(220,0,0)"}, + {"max-candles", K::Int, "120", 1, 5000}, + {"indicators", K::Str, ""}, + {"scale-mode", K::Enum, "0", 0, 0, "Price|Percent", + kScaleModeValues}, + {"compare-files", K::Str, ""}, + {"drawings", K::Str, ""}, + }; + if (type == "StockChart") return stockchart; + return none; // Canvas: common props only +} + +// Find a property definition for a widget type (type table, then common). +inline const PropDef* find_prop(const std::string& type, + const std::string& name) { + for (const PropDef& d : props_for(type)) + if (name == d.name) return &d; + for (const PropDef& d : common_props()) + if (name == d.name) return &d; + return nullptr; +} diff --git a/tests/candle_render_test.cpp b/tests/candle_render_test.cpp new file mode 100644 index 0000000..3742c4c --- /dev/null +++ b/tests/candle_render_test.cpp @@ -0,0 +1,50 @@ +// Renders the generated StockChart class offscreen and verifies that +// up-coloured and down-coloured candle pixels actually appear — i.e. the +// widget doesn't just compile, it PLOTS. +#include + +#include + +#include "StockChart_001.h" + +int main(int argc, char** argv) { + gtk_init(&argc, &argv); + GtkWidget* win = gtk_window_new(GTK_WINDOW_TOPLEVEL); + GtkWidget* fixed = gtk_fixed_new(); + gtk_container_add(GTK_CONTAINER(win), fixed); + gtk_window_set_default_size(GTK_WINDOW(win), 900, 700); + StockChart_001 chart(fixed); + if (chart.candle_count() == 0) { + std::printf("NO DATA LOADED\n"); + return 1; + } + gtk_widget_show_all(win); + for (int i = 0; i < 20; ++i) + while (gtk_events_pending()) gtk_main_iteration(); + GtkWidget* w = chart.widget(); + int width = gtk_widget_get_allocated_width(w); + int height = gtk_widget_get_allocated_height(w); + if (width < 50 || height < 50) { + std::printf("BAD SIZE %dx%d\n", width, height); + return 1; + } + cairo_surface_t* surf = + cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height); + cairo_t* cr = cairo_create(surf); + gtk_widget_draw(w, cr); + cairo_destroy(cr); + cairo_surface_flush(surf); + unsigned char* px = cairo_image_surface_get_data(surf); + int stride = cairo_image_surface_get_stride(surf); + long green = 0, red = 0; + for (int y = 0; y < height; ++y) + for (int x = 0; x < width; ++x) { + unsigned char* p = px + y * stride + x * 4; // BGRA + int b = p[0], g = p[1], r = p[2]; + if (g > 150 && r < 110 && b < 110) ++green; + if (r > 170 && g < 110 && b < 110) ++red; + } + std::printf("candles=%d size=%dx%d GREEN %ld RED %ld\n", + chart.candle_count(), width, height, green, red); + return (green > 200 && red > 200) ? 0 : 1; +} diff --git a/tests/chart_pro_render_test.cpp b/tests/chart_pro_render_test.cpp new file mode 100644 index 0000000..9a1bcaa --- /dev/null +++ b/tests/chart_pro_render_test.cpp @@ -0,0 +1,129 @@ +// Renders the generated StockChart Pro (matrix case 16: percent scale, +// QQQ comparison, seven annotations, SMA + RSI) offscreen and verifies +// every feature actually draws — then exercises wheel-zoom math via +// zoom_at() and re-renders at a smaller size to prove the annotations +// survive resizes. +#include + +#include +#include + +#include "StockChart_001.h" + +static void pump() { + for (int i = 0; i < 25; ++i) + while (gtk_events_pending()) gtk_main_iteration(); +} + +struct Counts { + long green = 0, red = 0, orange = 0, magenta = 0, purple = 0; + long axis_text = 0, time_text = 0; +}; + +static Counts render(StockChart_001& chart, int W, int H) { + GtkWidget* w = chart.widget(); + gtk_widget_queue_draw(w); + pump(); + int width = gtk_widget_get_allocated_width(w); + int height = gtk_widget_get_allocated_height(w); + cairo_surface_t* surf = + cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height); + cairo_t* cr = cairo_create(surf); + gtk_widget_draw(w, cr); + cairo_destroy(cr); + cairo_surface_flush(surf); + unsigned char* px = cairo_image_surface_get_data(surf); + int stride = cairo_image_surface_get_stride(surf); + Counts c; + (void)W; + (void)H; + for (int y = 0; y < height; ++y) + for (int x = 0; x < width; ++x) { + unsigned char* p = px + y * stride + x * 4; // BGRA + int b = p[0], g = p[1], r = p[2]; + if (g > 150 && r < 110 && b < 110) ++c.green; + if (r > 170 && g < 110 && b < 110) ++c.red; + if (r > 200 && g > 80 && g < 170 && b < 90) ++c.orange; + if (r > 180 && g > 30 && g < 95 && b > 130 && b < 210) + ++c.magenta; + if (r > 105 && r < 175 && g > 25 && g < 85 && b > 165) + ++c.purple; + // Axis labels are anti-aliased 0.35-grey text on white: + // count unsaturated mid-grey pixels (gridlines at 0.08 + // alpha land ~235 and stay excluded). + bool grey_text = r > 50 && r < 205 && std::abs(r - g) < 15 && + std::abs(g - b) < 15; + // In the right strip nothing but label text draws (grid- + // lines stop at the plot edge), so a looser bound is safe. + bool axis_grey = r > 40 && r < 225 && std::abs(r - g) < 18 && + std::abs(g - b) < 18; + if (axis_grey && x >= width - 56 && y < height - 18) + ++c.axis_text; + if (grey_text && y >= height - 18) ++c.time_text; + } + cairo_surface_destroy(surf); + return c; +} + +int main(int argc, char** argv) { + gtk_init(&argc, &argv); + int fails = 0; + auto check = [&](bool cond, const char* what) { + std::printf("%s %s\n", cond ? "OK " : "FAIL", what); + if (!cond) ++fails; + }; + GtkWidget* win = gtk_window_new(GTK_WINDOW_TOPLEVEL); + GtkWidget* fixed = gtk_fixed_new(); + gtk_container_add(GTK_CONTAINER(win), fixed); + gtk_window_set_default_size(GTK_WINDOW(win), 1024, 768); + StockChart_001 chart(fixed); + check(chart.candle_count() == 1170, "1170 candles loaded"); + check(chart.compare_count() == 1, "QQQ comparison loaded"); + check(chart.drawing_count() == 7, "7 annotations from design"); + check(chart.scale_mode() == 1, "percent scale mode"); + check(chart.visible_count() == 400, "initial window = max-candles"); + gtk_widget_show_all(win); + pump(); + + Counts c = render(chart, 1024, 768); + std::printf(" green=%ld red=%ld orange=%ld magenta=%ld " + "purple=%ld axis=%ld time=%ld\n", + c.green, c.red, c.orange, c.magenta, c.purple, + c.axis_text, c.time_text); + check(c.green > 200 && c.red > 200, "candles plotted"); + check(c.orange > 100, "QQQ comparison line plotted"); + check(c.magenta > 25, "arrow annotation plotted"); + check(c.purple > 25, "fib annotation plotted"); + check(c.axis_text > 40, "percent axis labels plotted"); + check(c.time_text > 60, "time axis labels plotted"); + + // Wheel-zoom math (what on_scroll calls): in, back out, clamp. + chart.zoom_at(0.5, 0.5); + check(chart.visible_count() == 200, "zoom in halves the window"); + chart.zoom_at(2.0, 0.5); + check(chart.visible_count() == 400, "zoom out restores the window"); + chart.zoom_at(10.0, 0.5); + check(chart.visible_count() == 1170, "zoom clamps at the full series"); + chart.reset_view(); + check(chart.visible_count() == 400, "reset_view restores max-candles"); + + // Resize: shrink the chart widget itself (its frame in the fixed + // layout); annotations are data-anchored, so the arrow and fib + // must still draw at their remapped positions. + gtk_widget_set_size_request(gtk_widget_get_parent(chart.widget()), + 620, 460); + gtk_window_resize(GTK_WINDOW(win), 660, 520); + pump(); + check(gtk_widget_get_allocated_width(chart.widget()) < 700, + "chart widget actually resized"); + Counts s = render(chart, 640, 480); + std::printf(" small: magenta=%ld purple=%ld orange=%ld\n", + s.magenta, s.purple, s.orange); + check(s.magenta > 12, "arrow survives resize"); + check(s.purple > 12, "fib survives resize"); + check(s.orange > 50, "comparison survives resize"); + + std::printf("CHART PRO TEST %s (%d failures)\n", + fails ? "FAIL" : "PASS", fails); + return fails ? 1 : 0; +} diff --git a/tests/chart_screenshot.cpp b/tests/chart_screenshot.cpp new file mode 100644 index 0000000..93305cb --- /dev/null +++ b/tests/chart_screenshot.cpp @@ -0,0 +1,37 @@ +// Captures the generated case-16 application window (MyWindow with +// the StockChart Pro) at exactly 1024x768 to a PNG, for a visual +// check of the whole feature set working together. +#include + +#include + +#include "MyWindow.h" + +int main(int argc, char** argv) { + const char* out = argc > 1 ? argv[1] : "chart_pro_1024x768.png"; + gtk_init(&argc, &argv); + MyWindow app; + app.show(); + GtkWidget* win = app.window(); + gtk_window_resize(GTK_WINDOW(win), 1024, 768); + // Let layout + first draws settle. + for (int i = 0; i < 60; ++i) { + while (gtk_events_pending()) gtk_main_iteration(); + g_usleep(10000); + } + GdkWindow* gw = gtk_widget_get_window(win); + int w = gdk_window_get_width(gw); + int h = gdk_window_get_height(gw); + GdkPixbuf* pb = gdk_pixbuf_get_from_window(gw, 0, 0, w, h); + if (!pb) { + std::printf("SCREENSHOT FAIL: no pixbuf\n"); + return 1; + } + GError* err = nullptr; + if (!gdk_pixbuf_save(pb, out, "png", &err, nullptr)) { + std::printf("SCREENSHOT FAIL: %s\n", err ? err->message : "?"); + return 1; + } + std::printf("SCREENSHOT OK %s (%dx%d)\n", out, w, h); + return (w == 1024 && h == 768) ? 0 : 1; +} diff --git a/tests/data/QQQ_fake_long.csv b/tests/data/QQQ_fake_long.csv new file mode 100644 index 0000000..474d670 --- /dev/null +++ b/tests/data/QQQ_fake_long.csv @@ -0,0 +1,1171 @@ +ticker,volume,open,close,high,low,window_start,transactions +QQQ,11108,512.00,512.08,512.24,511.51,1794493800000000000,1586 +QQQ,24080,512.08,511.73,512.68,511.11,1794493860000000000,3440 +QQQ,7671,511.73,511.40,512.01,511.08,1794493920000000000,1095 +QQQ,8523,511.40,512.05,512.52,511.10,1794493980000000000,1217 +QQQ,15550,512.05,512.38,512.40,511.66,1794494040000000000,2221 +QQQ,26859,512.38,512.88,513.56,511.98,1794494100000000000,3837 +QQQ,16353,512.88,512.08,513.23,511.44,1794494160000000000,2336 +QQQ,29396,512.08,511.84,512.58,511.47,1794494220000000000,4199 +QQQ,19967,511.84,512.71,513.15,511.74,1794494280000000000,2852 +QQQ,14199,512.71,513.92,514.24,512.65,1794494340000000000,2028 +QQQ,6497,513.92,512.88,514.45,512.32,1794494400000000000,928 +QQQ,5823,512.88,512.72,513.25,512.17,1794494460000000000,831 +QQQ,22473,512.72,511.55,513.26,511.28,1794494520000000000,3210 +QQQ,30170,511.55,512.65,512.73,511.06,1794494580000000000,4310 +QQQ,13651,512.65,513.42,513.55,512.13,1794494640000000000,1950 +QQQ,7249,513.42,514.25,514.79,512.85,1794494700000000000,1035 +QQQ,11652,514.25,513.63,514.34,513.40,1794494760000000000,1664 +QQQ,17171,513.63,512.49,513.97,511.84,1794494820000000000,2453 +QQQ,5068,512.49,511.95,513.07,511.84,1794494880000000000,724 +QQQ,15817,511.95,511.99,512.18,511.48,1794494940000000000,2259 +QQQ,13103,511.99,510.80,512.42,510.37,1794495000000000000,1871 +QQQ,27294,510.80,510.07,511.16,509.73,1794495060000000000,3899 +QQQ,19333,510.07,510.13,510.83,509.86,1794495120000000000,2761 +QQQ,23461,510.13,510.55,510.60,510.09,1794495180000000000,3351 +QQQ,29030,510.55,511.73,512.20,509.97,1794495240000000000,4147 +QQQ,8577,511.73,512.33,512.37,511.72,1794495300000000000,1225 +QQQ,11223,512.33,511.96,512.82,511.90,1794495360000000000,1603 +QQQ,21593,511.96,511.98,511.99,511.57,1794495420000000000,3084 +QQQ,16105,511.98,511.54,512.21,511.38,1794495480000000000,2300 +QQQ,10227,511.54,512.25,512.49,511.28,1794495540000000000,1461 +QQQ,7978,512.25,511.97,512.85,511.50,1794495600000000000,1139 +QQQ,4471,511.97,511.09,512.44,510.51,1794495660000000000,638 +QQQ,22369,511.09,511.72,512.42,510.92,1794495720000000000,3195 +QQQ,25083,511.72,512.81,513.11,511.52,1794495780000000000,3583 +QQQ,26662,512.81,512.27,513.38,511.83,1794495840000000000,3808 +QQQ,9156,512.27,512.82,513.07,512.23,1794495900000000000,1308 +QQQ,21284,512.82,513.28,513.60,512.31,1794495960000000000,3040 +QQQ,22416,513.28,513.00,513.90,512.86,1794496020000000000,3202 +QQQ,20417,513.00,513.10,513.17,512.66,1794496080000000000,2916 +QQQ,18975,513.10,512.00,513.62,511.51,1794496140000000000,2710 +QQQ,28618,512.00,513.04,513.22,511.63,1794496200000000000,4088 +QQQ,23896,513.04,514.39,514.91,512.90,1794496260000000000,3413 +QQQ,27492,514.39,515.44,516.10,514.21,1794496320000000000,3927 +QQQ,27212,515.44,516.00,516.19,515.38,1794496380000000000,3887 +QQQ,19924,516.00,515.86,516.03,515.56,1794496440000000000,2846 +QQQ,15593,515.86,516.75,517.38,515.62,1794496500000000000,2227 +QQQ,3217,516.75,517.12,517.76,516.34,1794496560000000000,459 +QQQ,14314,517.12,517.25,517.33,516.71,1794496620000000000,2044 +QQQ,10237,517.25,518.34,518.61,516.70,1794496680000000000,1462 +QQQ,25650,518.34,519.07,519.43,518.12,1794496740000000000,3664 +QQQ,5306,519.07,519.45,519.78,518.76,1794496800000000000,758 +QQQ,10191,519.45,520.50,520.90,519.16,1794496860000000000,1455 +QQQ,30285,520.50,521.65,521.95,520.28,1794496920000000000,4326 +QQQ,23697,521.65,522.57,523.06,521.26,1794496980000000000,3385 +QQQ,24713,522.57,522.06,523.07,521.70,1794497040000000000,3530 +QQQ,30428,522.06,523.16,523.67,521.61,1794497100000000000,4346 +QQQ,15119,523.16,524.31,524.61,523.09,1794497160000000000,2159 +QQQ,8597,524.31,524.99,525.04,524.22,1794497220000000000,1228 +QQQ,25845,524.99,526.27,526.89,524.56,1794497280000000000,3692 +QQQ,11911,526.27,525.93,526.31,525.66,1794497340000000000,1701 +QQQ,27629,525.93,525.13,526.52,524.74,1794497400000000000,3947 +QQQ,21138,525.13,526.22,526.74,524.59,1794497460000000000,3019 +QQQ,25301,526.22,526.22,526.73,526.12,1794497520000000000,3614 +QQQ,19038,526.22,526.73,527.39,525.69,1794497580000000000,2719 +QQQ,30357,526.73,527.83,528.00,526.04,1794497640000000000,4336 +QQQ,5756,527.83,528.56,528.63,527.63,1794497700000000000,822 +QQQ,29375,528.56,529.62,530.30,528.24,1794497760000000000,4196 +QQQ,32663,529.62,529.00,530.21,528.87,1794497820000000000,4666 +QQQ,17320,529.00,528.40,529.51,528.22,1794497880000000000,2474 +QQQ,16572,528.40,528.43,528.91,527.79,1794497940000000000,2367 +QQQ,17582,528.43,528.58,529.12,527.90,1794498000000000000,2511 +QQQ,32010,528.58,527.98,528.79,527.47,1794498060000000000,4572 +QQQ,32432,527.98,527.62,528.54,527.40,1794498120000000000,4633 +QQQ,13533,527.62,527.85,527.91,527.31,1794498180000000000,1933 +QQQ,10925,527.85,527.98,528.46,527.62,1794498240000000000,1560 +QQQ,16216,527.98,528.42,528.44,527.72,1794498300000000000,2316 +QQQ,22985,528.42,527.65,528.54,527.09,1794498360000000000,3283 +QQQ,15687,527.65,527.24,528.24,526.95,1794498420000000000,2241 +QQQ,15179,527.24,528.34,528.57,526.79,1794498480000000000,2168 +QQQ,7305,528.34,529.15,529.32,528.12,1794498540000000000,1043 +QQQ,25269,529.15,530.30,530.50,528.84,1794498600000000000,3609 +QQQ,9591,530.30,530.08,530.45,529.58,1794498660000000000,1370 +QQQ,8482,530.08,529.40,530.40,528.96,1794498720000000000,1211 +QQQ,32646,529.40,528.32,529.53,528.09,1794498780000000000,4663 +QQQ,7494,528.32,529.16,529.73,528.13,1794498840000000000,1070 +QQQ,30007,529.16,530.44,530.96,529.13,1794498900000000000,4286 +QQQ,24825,530.44,530.17,530.47,529.52,1794498960000000000,3546 +QQQ,30694,530.17,530.16,530.49,529.50,1794499020000000000,4384 +QQQ,18491,530.16,530.07,530.56,529.40,1794499080000000000,2641 +QQQ,14698,530.07,531.21,531.84,529.85,1794499140000000000,2099 +QQQ,3092,531.21,532.08,532.61,531.19,1794499200000000000,441 +QQQ,17685,532.08,531.52,532.58,531.01,1794499260000000000,2526 +QQQ,26452,531.52,530.52,532.16,530.39,1794499320000000000,3778 +QQQ,25454,530.52,531.64,532.18,530.47,1794499380000000000,3636 +QQQ,10486,531.64,531.97,532.25,531.53,1794499440000000000,1498 +QQQ,8399,531.97,532.40,532.86,531.65,1794499500000000000,1199 +QQQ,3488,532.40,533.50,533.56,532.08,1794499560000000000,498 +QQQ,14043,533.50,533.88,533.97,532.81,1794499620000000000,2006 +QQQ,12663,533.88,532.86,534.38,532.78,1794499680000000000,1809 +QQQ,18462,532.86,531.92,533.42,531.78,1794499740000000000,2637 +QQQ,27239,531.92,532.96,533.31,531.38,1794499800000000000,3891 +QQQ,19897,532.96,533.39,533.73,532.76,1794499860000000000,2842 +QQQ,16443,533.39,532.36,533.90,532.21,1794499920000000000,2349 +QQQ,20617,532.36,531.70,532.70,531.13,1794499980000000000,2945 +QQQ,30893,531.70,532.52,532.91,531.43,1794500040000000000,4413 +QQQ,30077,532.52,531.78,533.11,531.18,1794500100000000000,4296 +QQQ,21525,531.78,532.64,533.20,531.67,1794500160000000000,3075 +QQQ,24617,532.64,532.53,533.15,531.92,1794500220000000000,3516 +QQQ,18774,532.53,533.21,533.22,532.28,1794500280000000000,2682 +QQQ,32829,533.21,532.82,533.61,532.61,1794500340000000000,4689 +QQQ,14742,532.82,532.96,533.51,532.57,1794500400000000000,2106 +QQQ,6841,532.96,533.34,533.66,532.46,1794500460000000000,977 +QQQ,13337,533.34,532.58,533.75,532.42,1794500520000000000,1905 +QQQ,6371,532.58,532.01,532.74,531.62,1794500580000000000,910 +QQQ,9552,532.01,531.13,532.28,530.78,1794500640000000000,1364 +QQQ,28714,531.13,530.94,531.47,530.79,1794500700000000000,4102 +QQQ,5390,530.94,530.40,531.61,530.19,1794500760000000000,770 +QQQ,8604,530.40,531.75,532.08,530.08,1794500820000000000,1229 +QQQ,31797,531.75,531.70,532.17,531.02,1794500880000000000,4542 +QQQ,18536,531.70,532.15,532.16,531.40,1794500940000000000,2648 +QQQ,27439,532.15,533.20,533.26,531.62,1794501000000000000,3919 +QQQ,27912,533.20,532.11,533.72,531.62,1794501060000000000,3987 +QQQ,3747,532.11,532.65,532.70,531.57,1794501120000000000,535 +QQQ,12765,532.65,533.69,534.07,532.29,1794501180000000000,1823 +QQQ,32053,533.69,534.66,535.11,533.29,1794501240000000000,4579 +QQQ,27005,534.66,533.82,534.87,533.47,1794501300000000000,3857 +QQQ,17916,533.82,532.62,534.40,532.13,1794501360000000000,2559 +QQQ,26663,532.62,531.46,532.81,531.38,1794501420000000000,3809 +QQQ,25512,531.46,531.19,531.77,531.08,1794501480000000000,3644 +QQQ,13761,531.19,531.89,532.29,530.51,1794501540000000000,1965 +QQQ,26239,531.89,531.52,532.12,530.89,1794501600000000000,3748 +QQQ,17662,531.52,531.65,532.02,531.00,1794501660000000000,2523 +QQQ,19180,531.65,532.20,532.72,531.27,1794501720000000000,2740 +QQQ,4488,532.20,531.76,532.70,531.42,1794501780000000000,641 +QQQ,19077,531.76,530.75,532.07,530.55,1794501840000000000,2725 +QQQ,5426,530.75,531.09,531.18,530.45,1794501900000000000,775 +QQQ,28687,531.09,531.02,531.49,530.45,1794501960000000000,4098 +QQQ,18162,531.02,531.25,531.27,530.87,1794502020000000000,2594 +QQQ,12372,531.25,530.33,531.28,529.82,1794502080000000000,1767 +QQQ,14715,530.33,530.59,530.61,529.63,1794502140000000000,2102 +QQQ,26615,530.59,529.49,531.22,528.93,1794502200000000000,3802 +QQQ,19278,529.49,528.66,529.99,528.63,1794502260000000000,2754 +QQQ,23923,528.66,527.82,528.96,527.43,1794502320000000000,3417 +QQQ,30676,527.82,527.90,527.97,527.63,1794502380000000000,4382 +QQQ,7812,527.90,526.71,528.47,526.08,1794502440000000000,1116 +QQQ,16386,526.71,527.80,527.93,526.66,1794502500000000000,2340 +QQQ,11393,527.80,527.39,528.17,527.39,1794502560000000000,1627 +QQQ,21933,527.39,528.56,528.59,527.20,1794502620000000000,3133 +QQQ,19984,528.56,527.67,529.22,527.13,1794502680000000000,2854 +QQQ,5488,527.67,526.77,527.77,526.61,1794502740000000000,784 +QQQ,30584,526.77,527.54,528.16,526.71,1794502800000000000,4369 +QQQ,12148,527.54,528.46,529.05,527.49,1794502860000000000,1735 +QQQ,7013,528.46,528.17,528.46,527.86,1794502920000000000,1001 +QQQ,4132,528.17,528.40,528.54,527.69,1794502980000000000,590 +QQQ,20188,528.40,528.22,528.49,527.92,1794503040000000000,2884 +QQQ,12672,528.22,529.26,529.87,528.05,1794503100000000000,1810 +QQQ,23876,529.26,529.49,529.62,528.97,1794503160000000000,3410 +QQQ,32166,529.49,528.63,529.62,528.25,1794503220000000000,4595 +QQQ,32341,528.63,529.69,529.69,528.14,1794503280000000000,4620 +QQQ,6050,529.69,528.90,529.84,528.73,1794503340000000000,864 +QQQ,29826,528.90,529.21,529.36,528.82,1794503400000000000,4260 +QQQ,28806,529.21,530.54,531.13,528.94,1794503460000000000,4115 +QQQ,31709,530.54,531.67,531.78,530.34,1794503520000000000,4529 +QQQ,29710,531.67,530.44,531.68,530.09,1794503580000000000,4244 +QQQ,21925,530.44,530.58,530.92,529.76,1794503640000000000,3132 +QQQ,27638,530.58,530.46,530.66,530.08,1794503700000000000,3948 +QQQ,27342,530.46,530.66,531.15,530.43,1794503760000000000,3906 +QQQ,17094,530.66,530.19,531.04,529.74,1794503820000000000,2442 +QQQ,31961,530.19,529.27,530.82,529.09,1794503880000000000,4565 +QQQ,6400,529.27,529.60,529.64,528.82,1794503940000000000,914 +QQQ,17614,529.60,530.68,531.33,529.54,1794504000000000000,2516 +QQQ,24250,530.68,531.57,531.71,530.28,1794504060000000000,3464 +QQQ,23457,531.57,530.34,532.26,529.97,1794504120000000000,3351 +QQQ,32810,530.34,530.39,530.41,529.84,1794504180000000000,4687 +QQQ,6416,530.39,530.58,531.02,529.96,1794504240000000000,916 +QQQ,3606,530.58,529.37,530.62,529.15,1794504300000000000,515 +QQQ,26858,529.37,529.17,530.04,528.94,1794504360000000000,3836 +QQQ,11860,529.17,529.75,530.38,529.11,1794504420000000000,1694 +QQQ,22799,529.75,528.66,530.20,528.03,1794504480000000000,3257 +QQQ,25841,528.66,529.02,529.45,528.28,1794504540000000000,3691 +QQQ,8245,529.02,529.11,529.65,528.45,1794504600000000000,1177 +QQQ,9406,529.11,529.12,529.49,528.73,1794504660000000000,1343 +QQQ,3202,529.12,528.36,529.62,527.78,1794504720000000000,457 +QQQ,10276,528.36,529.28,529.48,527.84,1794504780000000000,1468 +QQQ,21419,529.28,529.22,529.35,528.63,1794504840000000000,3059 +QQQ,8136,529.22,529.84,530.52,529.09,1794504900000000000,1162 +QQQ,8367,529.84,529.57,530.53,528.98,1794504960000000000,1195 +QQQ,8800,529.57,528.76,529.82,528.21,1794505020000000000,1257 +QQQ,25899,528.76,529.20,529.74,528.20,1794505080000000000,3699 +QQQ,12605,529.20,530.46,530.58,528.51,1794505140000000000,1800 +QQQ,20992,530.46,530.92,531.14,529.78,1794505200000000000,2998 +QQQ,17772,530.92,530.99,531.17,530.55,1794505260000000000,2538 +QQQ,3261,530.99,530.08,531.13,530.00,1794505320000000000,465 +QQQ,13237,530.08,530.17,530.59,529.56,1794505380000000000,1891 +QQQ,19545,530.17,529.76,530.46,529.23,1794505440000000000,2792 +QQQ,19308,529.76,531.04,531.44,529.76,1794505500000000000,2758 +QQQ,20648,531.04,530.32,531.74,529.73,1794505560000000000,2949 +QQQ,5576,530.32,530.41,530.67,530.16,1794505620000000000,796 +QQQ,32641,530.41,530.99,531.63,529.94,1794505680000000000,4663 +QQQ,3828,530.99,531.75,531.89,530.89,1794505740000000000,546 +QQQ,11360,531.75,532.25,532.51,531.14,1794505800000000000,1622 +QQQ,26459,532.25,532.03,532.81,531.50,1794505860000000000,3779 +QQQ,13307,532.03,532.78,533.31,531.90,1794505920000000000,1901 +QQQ,13529,532.78,531.61,533.00,531.27,1794505980000000000,1932 +QQQ,4585,531.61,532.02,532.27,531.09,1794506040000000000,655 +QQQ,18656,532.02,530.80,532.47,530.28,1794506100000000000,2665 +QQQ,4506,530.80,530.76,531.37,530.06,1794506160000000000,643 +QQQ,20226,530.76,531.37,531.90,530.38,1794506220000000000,2889 +QQQ,31517,531.37,532.15,532.55,531.02,1794506280000000000,4502 +QQQ,9094,532.15,532.04,532.69,531.97,1794506340000000000,1299 +QQQ,17640,532.04,531.52,532.31,531.25,1794506400000000000,2520 +QQQ,16101,531.52,532.18,532.44,531.20,1794506460000000000,2300 +QQQ,23791,532.18,533.15,533.57,531.57,1794506520000000000,3398 +QQQ,26780,533.15,533.89,534.47,532.61,1794506580000000000,3825 +QQQ,16007,533.89,532.77,534.37,532.08,1794506640000000000,2286 +QQQ,19401,532.77,533.09,533.53,532.58,1794506700000000000,2771 +QQQ,17207,533.09,531.95,533.25,531.55,1794506760000000000,2458 +QQQ,12232,531.95,532.80,533.29,531.46,1794506820000000000,1747 +QQQ,8080,532.80,533.06,533.61,532.25,1794506880000000000,1154 +QQQ,21411,533.06,534.27,534.37,532.37,1794506940000000000,3058 +QQQ,29531,534.27,534.12,534.27,533.65,1794507000000000000,4218 +QQQ,22732,534.12,533.39,534.70,532.95,1794507060000000000,3247 +QQQ,32186,533.39,533.86,533.92,533.33,1794507120000000000,4598 +QQQ,19151,533.86,534.10,534.73,533.24,1794507180000000000,2735 +QQQ,13505,534.10,534.53,534.86,533.47,1794507240000000000,1929 +QQQ,29793,534.53,534.31,534.88,533.76,1794507300000000000,4256 +QQQ,11046,534.31,534.22,534.89,534.20,1794507360000000000,1578 +QQQ,15763,534.22,535.12,535.46,533.93,1794507420000000000,2251 +QQQ,18861,535.12,534.36,535.72,533.96,1794507480000000000,2694 +QQQ,13884,534.36,535.06,535.34,533.81,1794507540000000000,1983 +QQQ,26260,535.06,535.51,535.69,534.68,1794507600000000000,3751 +QQQ,10646,535.51,535.36,536.09,535.31,1794507660000000000,1520 +QQQ,16005,535.36,536.00,536.47,535.11,1794507720000000000,2286 +QQQ,6735,536.00,534.89,536.47,534.62,1794507780000000000,962 +QQQ,25997,534.89,534.25,535.23,533.58,1794507840000000000,3713 +QQQ,15097,534.25,533.76,534.68,533.33,1794507900000000000,2156 +QQQ,20025,533.76,533.14,533.79,532.67,1794507960000000000,2860 +QQQ,21905,533.14,532.95,533.44,532.59,1794508020000000000,3129 +QQQ,10388,532.95,531.74,533.17,531.61,1794508080000000000,1484 +QQQ,13463,531.74,531.35,532.36,530.70,1794508140000000000,1923 +QQQ,25483,531.35,530.53,531.79,530.43,1794508200000000000,3640 +QQQ,32298,530.53,531.76,532.23,530.35,1794508260000000000,4614 +QQQ,11230,531.76,532.07,532.56,531.26,1794508320000000000,1604 +QQQ,6473,532.07,531.56,532.27,531.55,1794508380000000000,924 +QQQ,14467,531.56,531.54,531.81,530.96,1794508440000000000,2066 +QQQ,13829,531.54,531.03,531.58,530.71,1794508500000000000,1975 +QQQ,25028,531.03,531.87,532.52,530.80,1794508560000000000,3575 +QQQ,8745,531.87,530.84,532.18,530.20,1794508620000000000,1249 +QQQ,13554,530.84,529.61,531.32,528.92,1794508680000000000,1936 +QQQ,20359,529.61,529.79,530.27,529.44,1794508740000000000,2908 +QQQ,12505,529.79,529.22,530.25,528.83,1794508800000000000,1786 +QQQ,19212,529.22,530.21,530.35,529.14,1794508860000000000,2744 +QQQ,30181,530.21,530.64,530.99,529.78,1794508920000000000,4311 +QQQ,9560,530.64,530.42,531.25,530.19,1794508980000000000,1365 +QQQ,26600,530.42,530.71,530.94,530.37,1794509040000000000,3800 +QQQ,3748,530.71,531.72,532.14,530.09,1794509100000000000,535 +QQQ,10793,531.72,532.15,532.36,531.59,1794509160000000000,1541 +QQQ,5647,532.15,533.14,533.45,531.66,1794509220000000000,806 +QQQ,29463,533.14,533.93,533.95,532.45,1794509280000000000,4209 +QQQ,26468,533.93,533.15,534.61,533.06,1794509340000000000,3781 +QQQ,28274,533.15,531.98,533.46,531.57,1794509400000000000,4039 +QQQ,17724,531.98,530.81,532.08,530.14,1794509460000000000,2532 +QQQ,7493,530.81,531.88,532.28,530.26,1794509520000000000,1070 +QQQ,20693,531.88,531.23,532.35,531.21,1794509580000000000,2956 +QQQ,7624,531.23,531.09,531.75,530.94,1794509640000000000,1089 +QQQ,31260,531.09,531.31,531.94,530.95,1794509700000000000,4465 +QQQ,18998,531.31,531.33,531.34,530.91,1794509760000000000,2714 +QQQ,27604,531.33,530.89,531.59,530.67,1794509820000000000,3943 +QQQ,19535,530.89,531.39,531.68,530.65,1794509880000000000,2790 +QQQ,23489,531.39,532.38,532.56,530.93,1794509940000000000,3355 +QQQ,19310,532.38,532.74,533.41,531.72,1794510000000000000,2758 +QQQ,31456,532.74,532.67,533.20,532.66,1794510060000000000,4493 +QQQ,23893,532.67,533.51,533.62,532.33,1794510120000000000,3413 +QQQ,24937,533.51,532.38,534.18,532.29,1794510180000000000,3562 +QQQ,11335,532.38,532.33,533.05,532.32,1794510240000000000,1619 +QQQ,17483,532.33,531.38,532.56,531.15,1794510300000000000,2497 +QQQ,17331,531.38,532.58,532.95,530.79,1794510360000000000,2475 +QQQ,16593,532.58,531.63,533.03,530.99,1794510420000000000,2370 +QQQ,26708,531.63,531.75,532.08,531.57,1794510480000000000,3815 +QQQ,31543,531.75,530.63,532.38,530.52,1794510540000000000,4506 +QQQ,11637,530.63,529.94,530.70,529.36,1794510600000000000,1662 +QQQ,11724,529.94,529.92,530.58,529.73,1794510660000000000,1674 +QQQ,12122,529.92,530.77,531.18,529.62,1794510720000000000,1731 +QQQ,11387,530.77,531.67,531.99,530.23,1794510780000000000,1626 +QQQ,27411,531.67,531.38,532.14,530.72,1794510840000000000,3915 +QQQ,7731,531.38,531.07,531.90,530.79,1794510900000000000,1104 +QQQ,7955,531.07,532.31,532.79,531.07,1794510960000000000,1136 +QQQ,15536,532.31,532.74,533.42,532.25,1794511020000000000,2219 +QQQ,21683,532.74,533.85,533.90,532.49,1794511080000000000,3097 +QQQ,20432,533.85,534.04,534.14,533.44,1794511140000000000,2918 +QQQ,14479,534.04,534.30,534.58,533.44,1794511200000000000,2068 +QQQ,13867,534.30,534.15,534.68,533.61,1794511260000000000,1981 +QQQ,31466,534.15,534.38,535.01,533.60,1794511320000000000,4495 +QQQ,9549,534.38,534.49,534.71,534.08,1794511380000000000,1364 +QQQ,12052,534.49,534.33,534.86,533.77,1794511440000000000,1721 +QQQ,29939,534.33,534.84,535.02,534.27,1794511500000000000,4277 +QQQ,31107,534.84,536.05,536.41,534.40,1794511560000000000,4443 +QQQ,24658,536.05,537.23,537.49,535.69,1794511620000000000,3522 +QQQ,23406,537.23,536.15,537.26,535.63,1794511680000000000,3343 +QQQ,10250,536.15,535.54,536.39,535.26,1794511740000000000,1464 +QQQ,26375,535.54,536.55,536.63,535.27,1794511800000000000,3767 +QQQ,29965,536.55,535.54,536.66,535.43,1794511860000000000,4280 +QQQ,19343,535.54,534.69,535.59,534.44,1794511920000000000,2763 +QQQ,20270,534.69,533.53,535.35,532.98,1794511980000000000,2895 +QQQ,5934,533.53,534.58,534.73,532.98,1794512040000000000,847 +QQQ,7336,534.58,535.59,536.12,533.94,1794512100000000000,1048 +QQQ,24422,535.59,536.38,536.39,535.46,1794512160000000000,3488 +QQQ,20261,536.38,536.70,537.05,536.26,1794512220000000000,2894 +QQQ,25561,536.70,537.70,538.20,536.17,1794512280000000000,3651 +QQQ,22306,537.70,536.70,538.27,536.38,1794512340000000000,3186 +QQQ,30031,536.70,537.59,537.92,536.48,1794512400000000000,4290 +QQQ,25354,537.59,537.98,538.33,537.12,1794512460000000000,3622 +QQQ,11177,537.98,539.21,539.34,537.48,1794512520000000000,1596 +QQQ,6111,539.21,539.70,539.70,538.92,1794512580000000000,873 +QQQ,32049,539.70,539.14,540.22,538.94,1794512640000000000,4578 +QQQ,4802,539.14,538.68,539.35,538.34,1794512700000000000,686 +QQQ,7796,538.68,537.81,538.98,537.49,1794512760000000000,1113 +QQQ,10223,537.81,537.68,538.39,537.47,1794512820000000000,1460 +QQQ,4856,537.68,538.55,538.73,537.17,1794512880000000000,693 +QQQ,4743,538.55,538.22,538.95,538.02,1794512940000000000,677 +QQQ,14320,538.22,537.61,538.51,537.36,1794513000000000000,2045 +QQQ,31650,537.61,536.55,537.74,536.25,1794513060000000000,4521 +QQQ,13363,536.55,537.11,537.19,536.08,1794513120000000000,1909 +QQQ,3322,537.11,536.79,537.44,536.62,1794513180000000000,474 +QQQ,13120,536.79,535.80,537.05,535.28,1794513240000000000,1874 +QQQ,17174,535.80,535.23,536.07,534.81,1794513300000000000,2453 +QQQ,7138,535.23,535.23,535.84,534.74,1794513360000000000,1019 +QQQ,23459,535.23,535.64,536.01,535.12,1794513420000000000,3351 +QQQ,18715,535.64,536.97,537.63,535.14,1794513480000000000,2673 +QQQ,27307,536.97,536.01,537.60,535.95,1794513540000000000,3901 +QQQ,15522,536.01,537.11,537.42,535.67,1794513600000000000,2217 +QQQ,7145,537.11,536.33,537.75,536.07,1794513660000000000,1020 +QQQ,19131,536.33,536.47,537.04,535.97,1794513720000000000,2733 +QQQ,12675,536.47,536.13,537.00,535.77,1794513780000000000,1810 +QQQ,18369,536.13,535.99,536.67,535.75,1794513840000000000,2624 +QQQ,23242,535.99,535.89,536.36,535.62,1794513900000000000,3320 +QQQ,8563,535.89,536.09,536.56,535.57,1794513960000000000,1223 +QQQ,22201,536.09,536.97,537.16,535.80,1794514020000000000,3171 +QQQ,3629,536.97,537.24,537.51,536.41,1794514080000000000,518 +QQQ,6852,537.24,537.44,538.10,537.03,1794514140000000000,978 +QQQ,28996,537.44,537.08,537.92,536.85,1794514200000000000,4142 +QQQ,32593,537.08,537.93,537.97,536.81,1794514260000000000,4656 +QQQ,17253,537.93,536.79,538.39,536.28,1794514320000000000,2464 +QQQ,26742,536.79,537.35,538.04,536.69,1794514380000000000,3820 +QQQ,28792,537.35,537.77,538.02,536.72,1794514440000000000,4113 +QQQ,14432,537.77,536.97,538.32,536.56,1794514500000000000,2061 +QQQ,10237,536.97,536.18,537.52,535.55,1794514560000000000,1462 +QQQ,17072,536.18,535.64,536.60,535.27,1794514620000000000,2438 +QQQ,8031,535.64,534.89,536.05,534.85,1794514680000000000,1147 +QQQ,24537,534.89,535.78,536.41,534.26,1794514740000000000,3505 +QQQ,13071,535.78,536.90,536.91,535.54,1794514800000000000,1867 +QQQ,4423,536.90,536.08,537.00,535.78,1794514860000000000,631 +QQQ,21937,536.08,537.24,537.39,535.65,1794514920000000000,3133 +QQQ,21108,537.24,537.54,537.59,536.84,1794514980000000000,3015 +QQQ,3624,537.54,537.66,538.02,537.06,1794515040000000000,517 +QQQ,7901,537.66,536.80,537.94,536.19,1794515100000000000,1128 +QQQ,19381,536.80,537.86,538.31,536.57,1794515160000000000,2768 +QQQ,27876,537.86,538.54,538.59,537.43,1794515220000000000,3982 +QQQ,20249,538.54,538.12,538.76,537.90,1794515280000000000,2892 +QQQ,32354,538.12,539.35,539.97,537.61,1794515340000000000,4622 +QQQ,17309,539.35,540.44,540.72,539.22,1794515400000000000,2472 +QQQ,15977,540.44,541.76,542.15,540.14,1794515460000000000,2282 +QQQ,22795,541.76,541.51,542.08,540.82,1794515520000000000,3256 +QQQ,16731,541.51,541.98,542.29,541.00,1794515580000000000,2390 +QQQ,29636,541.98,542.11,542.71,541.59,1794515640000000000,4233 +QQQ,25074,542.11,543.26,543.58,541.49,1794515700000000000,3582 +QQQ,20245,543.26,543.47,543.77,542.94,1794515760000000000,2892 +QQQ,10400,543.47,543.71,544.31,543.07,1794515820000000000,1485 +QQQ,31296,543.71,543.81,543.91,543.19,1794515880000000000,4470 +QQQ,9249,543.81,544.25,544.80,543.29,1794515940000000000,1321 +QQQ,20236,544.25,544.97,545.27,543.64,1794516000000000000,2890 +QQQ,5238,544.97,545.58,545.62,544.64,1794516060000000000,748 +QQQ,22298,545.58,545.49,545.74,545.34,1794516120000000000,3185 +QQQ,18550,545.49,546.02,546.32,545.09,1794516180000000000,2650 +QQQ,28675,546.02,545.66,546.31,545.46,1794516240000000000,4096 +QQQ,28186,545.66,546.80,547.44,545.24,1794516300000000000,4026 +QQQ,19373,546.80,546.52,547.41,546.20,1794516360000000000,2767 +QQQ,32798,546.52,545.49,547.03,545.27,1794516420000000000,4685 +QQQ,24735,545.49,544.29,545.71,543.98,1794516480000000000,3533 +QQQ,19016,544.29,543.18,544.36,542.99,1794516540000000000,2716 +QQQ,20948,543.18,542.20,543.38,541.93,1794516600000000000,2992 +QQQ,12915,542.20,542.65,542.90,541.63,1794516660000000000,1845 +QQQ,8269,542.65,544.01,544.39,541.98,1794516720000000000,1181 +QQQ,9044,544.01,544.72,545.24,544.00,1794516780000000000,1292 +QQQ,5363,544.72,546.08,546.64,544.21,1794516840000000000,766 +QQQ,31246,546.08,546.78,547.22,545.60,1794516900000000000,4463 +QQQ,3446,546.78,547.94,548.13,546.77,1794516960000000000,492 +QQQ,13673,547.94,547.53,548.53,547.27,1794517020000000000,1953 +QQQ,9339,547.53,548.63,549.23,547.36,1794517080000000000,1334 +QQQ,31519,548.63,549.68,549.68,548.04,1794517140000000000,4502 +QQQ,21343,549.68,550.48,550.99,549.29,1794580200000000000,3049 +QQQ,12038,550.48,549.52,550.77,548.89,1794580260000000000,1719 +QQQ,27218,549.52,550.03,550.13,549.12,1794580320000000000,3888 +QQQ,11810,550.03,550.92,551.35,549.76,1794580380000000000,1687 +QQQ,23395,550.92,551.58,551.92,550.40,1794580440000000000,3342 +QQQ,30599,551.58,550.46,551.91,550.39,1794580500000000000,4371 +QQQ,5978,550.46,551.28,551.74,550.04,1794580560000000000,854 +QQQ,21907,551.28,551.54,551.96,550.85,1794580620000000000,3129 +QQQ,28921,551.54,551.66,552.26,550.99,1794580680000000000,4131 +QQQ,31277,551.66,551.22,551.99,550.69,1794580740000000000,4468 +QQQ,8226,551.22,550.00,551.61,549.41,1794580800000000000,1175 +QQQ,17874,550.00,549.65,550.08,549.43,1794580860000000000,2553 +QQQ,12904,549.65,549.80,550.19,549.00,1794580920000000000,1843 +QQQ,13538,549.80,548.74,549.90,548.14,1794580980000000000,1934 +QQQ,15395,548.74,548.48,549.11,548.48,1794581040000000000,2199 +QQQ,9626,548.48,548.67,548.69,548.46,1794581100000000000,1375 +QQQ,17785,548.67,548.50,549.04,547.95,1794581160000000000,2540 +QQQ,32275,548.50,549.13,549.48,548.39,1794581220000000000,4610 +QQQ,17804,549.13,547.98,549.82,547.93,1794581280000000000,2543 +QQQ,5083,547.98,549.29,549.79,547.59,1794581340000000000,726 +QQQ,23701,549.29,549.28,549.39,549.07,1794581400000000000,3385 +QQQ,8399,549.28,549.58,550.23,548.81,1794581460000000000,1199 +QQQ,24165,549.58,549.87,550.45,549.12,1794581520000000000,3452 +QQQ,7552,549.87,548.93,550.47,548.36,1794581580000000000,1078 +QQQ,21900,548.93,548.69,549.24,548.05,1794581640000000000,3128 +QQQ,12321,548.69,548.22,548.80,547.92,1794581700000000000,1760 +QQQ,15501,548.22,549.50,549.56,548.09,1794581760000000000,2214 +QQQ,14778,549.50,548.89,550.08,548.55,1794581820000000000,2111 +QQQ,8697,548.89,548.75,548.94,548.29,1794581880000000000,1242 +QQQ,24786,548.75,549.33,549.76,548.21,1794581940000000000,3540 +QQQ,10014,549.33,548.27,549.51,547.71,1794582000000000000,1430 +QQQ,29696,548.27,547.72,548.41,547.56,1794582060000000000,4242 +QQQ,6979,547.72,548.72,548.83,547.51,1794582120000000000,997 +QQQ,14920,548.72,549.61,549.66,548.09,1794582180000000000,2131 +QQQ,16615,549.61,549.33,549.69,549.08,1794582240000000000,2373 +QQQ,10685,549.33,550.18,550.28,548.75,1794582300000000000,1526 +QQQ,23028,550.18,549.10,550.68,548.97,1794582360000000000,3289 +QQQ,10684,549.10,550.41,550.41,548.41,1794582420000000000,1526 +QQQ,10403,550.41,550.37,550.72,549.92,1794582480000000000,1486 +QQQ,12072,550.37,550.91,551.05,549.90,1794582540000000000,1724 +QQQ,22973,550.91,549.98,550.98,549.44,1794582600000000000,3281 +QQQ,11219,549.98,549.29,550.18,549.16,1794582660000000000,1602 +QQQ,21411,549.29,550.02,550.66,549.18,1794582720000000000,3058 +QQQ,21128,550.02,549.78,550.33,549.16,1794582780000000000,3018 +QQQ,4993,549.78,550.21,550.41,549.40,1794582840000000000,713 +QQQ,4664,550.21,549.72,550.30,549.28,1794582900000000000,666 +QQQ,21675,549.72,549.91,550.33,549.04,1794582960000000000,3096 +QQQ,14249,549.91,549.91,550.08,549.58,1794583020000000000,2035 +QQQ,26637,549.91,550.72,551.30,549.83,1794583080000000000,3805 +QQQ,32708,550.72,551.79,552.15,550.23,1794583140000000000,4672 +QQQ,16552,551.79,550.71,552.38,550.57,1794583200000000000,2364 +QQQ,15975,550.71,551.99,552.18,550.04,1794583260000000000,2282 +QQQ,6700,551.99,551.79,552.40,551.33,1794583320000000000,957 +QQQ,32103,551.79,551.86,551.87,551.23,1794583380000000000,4586 +QQQ,11585,551.86,552.90,553.24,551.33,1794583440000000000,1655 +QQQ,12752,552.90,554.14,554.67,552.69,1794583500000000000,1821 +QQQ,8018,554.14,555.40,555.97,553.81,1794583560000000000,1145 +QQQ,30207,555.40,555.79,556.23,555.03,1794583620000000000,4315 +QQQ,28532,555.79,554.81,556.29,554.29,1794583680000000000,4076 +QQQ,15602,554.81,555.67,556.15,554.25,1794583740000000000,2228 +QQQ,19257,555.67,556.53,556.81,555.26,1794583800000000000,2751 +QQQ,17883,556.53,557.35,557.73,556.34,1794583860000000000,2554 +QQQ,4997,557.35,556.42,557.65,556.11,1794583920000000000,713 +QQQ,26093,556.42,556.06,556.59,555.59,1794583980000000000,3727 +QQQ,10258,556.06,555.61,556.54,555.41,1794584040000000000,1465 +QQQ,5705,555.61,556.27,556.46,554.99,1794584100000000000,815 +QQQ,19172,556.27,555.61,556.89,555.22,1794584160000000000,2738 +QQQ,21061,555.61,555.09,555.81,554.59,1794584220000000000,3008 +QQQ,30616,555.09,555.83,555.99,554.90,1794584280000000000,4373 +QQQ,26055,555.83,555.44,556.39,554.92,1794584340000000000,3722 +QQQ,5133,555.44,554.54,555.64,553.95,1794584400000000000,733 +QQQ,5741,554.54,554.99,555.35,554.08,1794584460000000000,820 +QQQ,11471,554.99,556.25,556.26,554.85,1794584520000000000,1638 +QQQ,28000,556.25,556.59,556.83,555.99,1794584580000000000,4000 +QQQ,15512,556.59,557.74,558.31,556.42,1794584640000000000,2216 +QQQ,18386,557.74,557.63,558.26,557.35,1794584700000000000,2626 +QQQ,25714,557.63,557.51,558.09,556.82,1794584760000000000,3673 +QQQ,11033,557.51,558.39,558.51,557.36,1794584820000000000,1576 +QQQ,3675,558.39,557.35,559.02,557.32,1794584880000000000,525 +QQQ,24440,557.35,556.96,557.83,556.89,1794584940000000000,3491 +QQQ,26352,556.96,556.78,557.43,556.59,1794585000000000000,3764 +QQQ,19932,556.78,557.46,557.60,556.10,1794585060000000000,2847 +QQQ,32395,557.46,557.68,558.24,557.01,1794585120000000000,4627 +QQQ,13219,557.68,557.55,557.73,557.40,1794585180000000000,1888 +QQQ,21878,557.55,557.96,558.18,557.27,1794585240000000000,3125 +QQQ,31356,557.96,558.28,558.58,557.56,1794585300000000000,4479 +QQQ,29327,558.28,559.45,559.88,557.97,1794585360000000000,4189 +QQQ,6649,559.45,560.14,560.81,558.94,1794585420000000000,949 +QQQ,24190,560.14,561.12,561.25,559.71,1794585480000000000,3455 +QQQ,21288,561.12,560.87,561.35,560.37,1794585540000000000,3041 +QQQ,21355,560.87,561.05,561.20,560.45,1794585600000000000,3050 +QQQ,3751,561.05,562.00,562.56,560.57,1794585660000000000,535 +QQQ,23813,562.00,563.03,563.18,561.44,1794585720000000000,3401 +QQQ,17895,563.03,562.53,563.68,562.23,1794585780000000000,2556 +QQQ,26517,562.53,562.28,562.59,562.18,1794585840000000000,3788 +QQQ,7452,562.28,562.57,563.07,561.66,1794585900000000000,1064 +QQQ,23231,562.57,563.19,563.50,562.56,1794585960000000000,3318 +QQQ,24333,563.19,562.53,563.67,561.87,1794586020000000000,3476 +QQQ,26851,562.53,561.61,563.03,561.60,1794586080000000000,3835 +QQQ,32502,561.61,562.76,563.45,561.58,1794586140000000000,4643 +QQQ,15822,562.76,563.07,563.31,562.53,1794586200000000000,2260 +QQQ,15655,563.07,562.92,563.37,562.85,1794586260000000000,2236 +QQQ,13522,562.92,563.67,564.10,562.30,1794586320000000000,1931 +QQQ,30958,563.67,562.53,563.97,562.07,1794586380000000000,4422 +QQQ,3269,562.53,561.37,563.01,561.29,1794586440000000000,467 +QQQ,30590,561.37,561.40,562.03,561.08,1794586500000000000,4370 +QQQ,31740,561.40,560.61,561.58,560.59,1794586560000000000,4534 +QQQ,26778,560.61,560.03,561.16,559.56,1794586620000000000,3825 +QQQ,26608,560.03,559.70,560.57,559.21,1794586680000000000,3801 +QQQ,3939,559.70,559.08,560.25,559.00,1794586740000000000,562 +QQQ,7388,559.08,558.78,559.29,558.75,1794586800000000000,1055 +QQQ,22541,558.78,558.98,559.27,558.53,1794586860000000000,3220 +QQQ,30723,558.98,560.21,560.38,558.28,1794586920000000000,4389 +QQQ,10776,560.21,560.51,560.66,559.54,1794586980000000000,1539 +QQQ,26343,560.51,561.54,561.56,560.33,1794587040000000000,3763 +QQQ,29571,561.54,562.32,563.00,561.45,1794587100000000000,4224 +QQQ,14377,562.32,561.90,562.93,561.49,1794587160000000000,2053 +QQQ,22564,561.90,561.11,562.58,560.68,1794587220000000000,3223 +QQQ,25510,561.11,560.01,561.12,559.55,1794587280000000000,3644 +QQQ,11005,560.01,560.86,561.34,559.43,1794587340000000000,1572 +QQQ,28803,560.86,560.25,561.24,559.56,1794587400000000000,4114 +QQQ,24650,560.25,559.29,560.50,559.23,1794587460000000000,3521 +QQQ,31043,559.29,560.59,561.05,559.23,1794587520000000000,4434 +QQQ,11909,560.59,559.39,561.17,559.12,1794587580000000000,1701 +QQQ,11005,559.39,559.81,560.10,558.79,1794587640000000000,1572 +QQQ,3624,559.81,560.37,560.54,559.76,1794587700000000000,517 +QQQ,15369,560.37,561.15,561.26,560.07,1794587760000000000,2195 +QQQ,8166,561.15,561.72,562.27,560.80,1794587820000000000,1166 +QQQ,20274,561.72,561.79,561.92,561.48,1794587880000000000,2896 +QQQ,32635,561.79,562.97,563.24,561.65,1794587940000000000,4662 +QQQ,27569,562.97,562.60,563.55,562.51,1794588000000000000,3938 +QQQ,25149,562.60,563.20,563.59,562.54,1794588060000000000,3592 +QQQ,6208,563.20,563.68,564.12,562.51,1794588120000000000,886 +QQQ,24550,563.68,563.23,564.09,562.98,1794588180000000000,3507 +QQQ,24912,563.23,563.16,563.38,562.75,1794588240000000000,3558 +QQQ,10393,563.16,562.59,563.81,562.01,1794588300000000000,1484 +QQQ,31853,562.59,562.05,562.95,561.55,1794588360000000000,4550 +QQQ,27103,562.05,562.45,562.74,562.03,1794588420000000000,3871 +QQQ,16916,562.45,561.96,562.54,561.89,1794588480000000000,2416 +QQQ,12613,561.96,561.80,562.59,561.35,1794588540000000000,1801 +QQQ,4233,561.80,561.41,562.38,560.90,1794588600000000000,604 +QQQ,14962,561.41,561.49,561.89,561.18,1794588660000000000,2137 +QQQ,10980,561.49,560.52,561.55,560.43,1794588720000000000,1568 +QQQ,22063,560.52,561.26,561.41,560.22,1794588780000000000,3151 +QQQ,25235,561.26,560.87,561.59,560.38,1794588840000000000,3605 +QQQ,29444,560.87,562.01,562.53,560.18,1794588900000000000,4206 +QQQ,24311,562.01,562.29,562.47,561.86,1794588960000000000,3473 +QQQ,13330,562.29,561.68,562.42,561.58,1794589020000000000,1904 +QQQ,14469,561.68,562.47,562.99,561.30,1794589080000000000,2067 +QQQ,11684,562.47,563.02,563.37,562.44,1794589140000000000,1669 +QQQ,9095,563.02,562.33,563.04,561.81,1794589200000000000,1299 +QQQ,12651,562.33,563.21,563.61,562.32,1794589260000000000,1807 +QQQ,26156,563.21,563.20,563.57,562.97,1794589320000000000,3736 +QQQ,23246,563.20,562.50,563.84,561.82,1794589380000000000,3320 +QQQ,7066,562.50,561.81,563.14,561.47,1794589440000000000,1009 +QQQ,25564,561.81,562.63,563.16,561.38,1794589500000000000,3652 +QQQ,7418,562.63,561.91,563.24,561.24,1794589560000000000,1059 +QQQ,31798,561.91,561.52,562.19,561.30,1794589620000000000,4542 +QQQ,14871,561.52,562.05,562.59,561.03,1794589680000000000,2124 +QQQ,22441,562.05,561.00,562.52,560.37,1794589740000000000,3205 +QQQ,26096,561.00,560.87,561.23,560.47,1794589800000000000,3728 +QQQ,9447,560.87,560.61,561.44,560.00,1794589860000000000,1349 +QQQ,30744,560.61,560.17,561.00,559.60,1794589920000000000,4392 +QQQ,10019,560.17,560.71,561.38,559.66,1794589980000000000,1431 +QQQ,16951,560.71,559.72,560.89,559.35,1794590040000000000,2421 +QQQ,28510,559.72,560.70,561.38,559.13,1794590100000000000,4072 +QQQ,15736,560.70,561.57,562.14,560.17,1794590160000000000,2248 +QQQ,31065,561.57,562.22,562.24,561.30,1794590220000000000,4437 +QQQ,23344,562.22,563.58,563.88,562.19,1794590280000000000,3334 +QQQ,10810,563.58,564.28,564.96,563.48,1794590340000000000,1544 +QQQ,6430,564.28,565.28,565.80,564.14,1794590400000000000,918 +QQQ,4173,565.28,565.80,566.17,565.23,1794590460000000000,596 +QQQ,8080,565.80,566.29,566.67,565.25,1794590520000000000,1154 +QQQ,16849,566.29,565.70,566.51,565.63,1794590580000000000,2407 +QQQ,18836,565.70,566.17,566.25,565.50,1794590640000000000,2690 +QQQ,8342,566.17,566.36,567.04,565.98,1794590700000000000,1191 +QQQ,20119,566.36,565.18,566.42,564.98,1794590760000000000,2874 +QQQ,24048,565.18,564.12,565.72,564.12,1794590820000000000,3435 +QQQ,27559,564.12,563.04,564.74,562.43,1794590880000000000,3937 +QQQ,13986,563.04,563.74,563.81,562.88,1794590940000000000,1998 +QQQ,5112,563.74,564.09,564.67,563.47,1794591000000000000,730 +QQQ,25395,564.09,565.26,565.41,563.92,1794591060000000000,3627 +QQQ,20058,565.26,565.07,565.34,564.98,1794591120000000000,2865 +QQQ,28249,565.07,564.39,565.10,563.92,1794591180000000000,4035 +QQQ,24371,564.39,563.50,564.81,563.37,1794591240000000000,3481 +QQQ,3643,563.50,564.09,564.63,563.12,1794591300000000000,520 +QQQ,17209,564.09,563.20,564.66,562.92,1794591360000000000,2458 +QQQ,26608,563.20,562.65,563.27,562.46,1794591420000000000,3801 +QQQ,20799,562.65,562.51,562.80,562.13,1794591480000000000,2971 +QQQ,8776,562.51,562.54,563.09,562.12,1794591540000000000,1253 +QQQ,24338,562.54,561.44,562.63,561.08,1794591600000000000,3476 +QQQ,21617,561.44,560.22,561.92,559.99,1794591660000000000,3088 +QQQ,13884,560.22,560.59,561.24,559.90,1794591720000000000,1983 +QQQ,32482,560.59,561.10,561.31,560.16,1794591780000000000,4640 +QQQ,23181,561.10,561.73,562.14,560.52,1794591840000000000,3311 +QQQ,15191,561.73,561.91,562.22,561.35,1794591900000000000,2170 +QQQ,28717,561.91,561.32,562.13,561.07,1794591960000000000,4102 +QQQ,18594,561.32,560.40,561.94,560.25,1794592020000000000,2656 +QQQ,17771,560.40,560.63,560.97,559.86,1794592080000000000,2538 +QQQ,6874,560.63,561.29,561.75,560.34,1794592140000000000,982 +QQQ,27192,561.29,561.09,561.68,561.07,1794592200000000000,3884 +QQQ,29461,561.09,561.79,562.40,560.44,1794592260000000000,4208 +QQQ,26307,561.79,562.29,562.91,561.77,1794592320000000000,3758 +QQQ,28610,562.29,561.94,562.33,561.90,1794592380000000000,4087 +QQQ,25724,561.94,561.12,561.96,560.60,1794592440000000000,3674 +QQQ,20058,561.12,560.40,561.17,560.31,1794592500000000000,2865 +QQQ,7628,560.40,559.64,560.80,559.43,1794592560000000000,1089 +QQQ,17653,559.64,560.90,561.44,559.31,1794592620000000000,2521 +QQQ,5629,560.90,561.47,561.86,560.33,1794592680000000000,804 +QQQ,11746,561.47,561.04,561.94,560.89,1794592740000000000,1678 +QQQ,5301,561.04,560.74,561.25,560.11,1794592800000000000,757 +QQQ,24299,560.74,560.87,561.45,560.30,1794592860000000000,3471 +QQQ,13434,560.87,559.78,561.12,559.60,1794592920000000000,1919 +QQQ,23138,559.78,559.28,560.20,558.70,1794592980000000000,3305 +QQQ,10820,559.28,559.36,559.56,558.73,1794593040000000000,1545 +QQQ,32996,559.36,560.69,561.07,559.21,1794593100000000000,4713 +QQQ,15220,560.69,560.56,560.77,560.21,1794593160000000000,2174 +QQQ,30103,560.56,559.43,560.70,559.09,1794593220000000000,4300 +QQQ,6957,559.43,560.26,560.33,558.88,1794593280000000000,993 +QQQ,10333,560.26,560.90,561.04,560.18,1794593340000000000,1476 +QQQ,18612,560.90,561.37,561.85,560.26,1794593400000000000,2658 +QQQ,27435,561.37,562.05,562.30,561.36,1794593460000000000,3919 +QQQ,31877,562.05,563.01,563.45,561.57,1794593520000000000,4553 +QQQ,28290,563.01,562.21,563.61,562.12,1794593580000000000,4041 +QQQ,6352,562.21,563.24,563.25,561.57,1794593640000000000,907 +QQQ,9405,563.24,563.90,564.25,562.67,1794593700000000000,1343 +QQQ,32683,563.90,563.75,564.48,563.18,1794593760000000000,4669 +QQQ,21319,563.75,562.56,563.88,561.94,1794593820000000000,3045 +QQQ,12374,562.56,563.10,563.48,562.33,1794593880000000000,1767 +QQQ,10570,563.10,564.23,564.85,562.49,1794593940000000000,1510 +QQQ,14894,564.23,563.60,564.73,562.91,1794594000000000000,2127 +QQQ,16976,563.60,562.45,563.87,561.83,1794594060000000000,2425 +QQQ,29037,562.45,563.38,563.97,561.78,1794594120000000000,4148 +QQQ,20969,563.38,564.60,565.20,563.36,1794594180000000000,2995 +QQQ,5257,564.60,565.44,566.11,564.19,1794594240000000000,751 +QQQ,24523,565.44,565.81,566.03,564.90,1794594300000000000,3503 +QQQ,32425,565.81,566.73,567.29,565.25,1794594360000000000,4632 +QQQ,14252,566.73,567.41,567.86,566.56,1794594420000000000,2036 +QQQ,26853,567.41,568.04,568.06,567.37,1794594480000000000,3836 +QQQ,17677,568.04,568.99,569.45,567.97,1794594540000000000,2525 +QQQ,18326,568.99,568.52,569.00,567.89,1794594600000000000,2618 +QQQ,23170,568.52,567.34,569.14,566.68,1794594660000000000,3310 +QQQ,30390,567.34,568.21,568.35,567.34,1794594720000000000,4341 +QQQ,17583,568.21,567.79,568.54,567.33,1794594780000000000,2511 +QQQ,31544,567.79,567.58,568.31,567.21,1794594840000000000,4506 +QQQ,17324,567.58,567.61,567.77,567.36,1794594900000000000,2474 +QQQ,29153,567.61,567.47,567.61,566.88,1794594960000000000,4164 +QQQ,16212,567.47,568.26,568.77,567.24,1794595020000000000,2316 +QQQ,15490,568.26,569.44,569.79,568.05,1794595080000000000,2212 +QQQ,9109,569.44,569.23,569.83,569.09,1794595140000000000,1301 +QQQ,31140,569.23,568.38,569.72,567.96,1794595200000000000,4448 +QQQ,3598,568.38,567.53,568.39,567.32,1794595260000000000,514 +QQQ,14697,567.53,567.55,568.14,567.37,1794595320000000000,2099 +QQQ,27284,567.55,568.43,569.06,567.19,1794595380000000000,3897 +QQQ,12683,568.43,569.78,569.91,567.74,1794595440000000000,1811 +QQQ,14495,569.78,569.92,570.54,569.54,1794595500000000000,2070 +QQQ,20529,569.92,569.56,570.42,569.23,1794595560000000000,2932 +QQQ,17576,569.56,568.35,569.72,567.72,1794595620000000000,2510 +QQQ,22938,568.35,569.53,569.92,568.07,1794595680000000000,3276 +QQQ,16911,569.53,568.65,569.54,568.01,1794595740000000000,2415 +QQQ,15030,568.65,568.11,568.82,567.65,1794595800000000000,2147 +QQQ,25673,568.11,569.13,569.52,567.49,1794595860000000000,3667 +QQQ,12726,569.13,568.50,569.40,568.42,1794595920000000000,1818 +QQQ,22448,568.50,569.81,569.83,567.81,1794595980000000000,3206 +QQQ,29308,569.81,570.79,570.91,569.72,1794596040000000000,4186 +QQQ,25354,570.79,570.24,571.31,569.55,1794596100000000000,3622 +QQQ,25069,570.24,570.84,571.33,569.67,1794596160000000000,3581 +QQQ,14443,570.84,571.03,571.57,570.34,1794596220000000000,2063 +QQQ,17529,571.03,571.20,571.68,570.67,1794596280000000000,2504 +QQQ,21596,571.20,570.28,571.65,569.83,1794596340000000000,3085 +QQQ,10232,570.28,571.08,571.13,570.17,1794596400000000000,1461 +QQQ,23762,571.08,570.46,571.68,569.83,1794596460000000000,3394 +QQQ,6383,570.46,569.82,570.59,569.58,1794596520000000000,911 +QQQ,16512,569.82,570.79,571.06,569.47,1794596580000000000,2358 +QQQ,15039,570.79,569.91,570.94,569.52,1794596640000000000,2148 +QQQ,10786,569.91,571.28,571.53,569.63,1794596700000000000,1540 +QQQ,20432,571.28,570.50,571.94,569.92,1794596760000000000,2918 +QQQ,5242,570.50,569.78,570.86,569.17,1794596820000000000,748 +QQQ,31491,569.78,570.52,571.08,569.56,1794596880000000000,4498 +QQQ,18804,570.52,570.01,571.00,569.41,1794596940000000000,2686 +QQQ,16888,570.01,570.23,570.35,569.56,1794597000000000000,2412 +QQQ,29644,570.23,570.21,570.30,569.72,1794597060000000000,4234 +QQQ,15168,570.21,569.03,570.75,568.89,1794597120000000000,2166 +QQQ,15609,569.03,568.08,569.55,567.66,1794597180000000000,2229 +QQQ,28308,568.08,569.14,569.62,567.64,1794597240000000000,4044 +QQQ,26249,569.14,569.44,570.04,568.68,1794597300000000000,3749 +QQQ,21831,569.44,570.77,570.88,569.19,1794597360000000000,3118 +QQQ,8828,570.77,571.83,571.98,570.50,1794597420000000000,1261 +QQQ,24992,571.83,570.74,572.35,570.48,1794597480000000000,3570 +QQQ,16316,570.74,569.95,571.25,569.62,1794597540000000000,2330 +QQQ,17083,569.95,570.07,570.77,569.46,1794597600000000000,2440 +QQQ,9063,570.07,569.45,570.33,569.09,1794597660000000000,1294 +QQQ,7796,569.45,569.83,570.38,569.32,1794597720000000000,1113 +QQQ,26731,569.83,570.17,570.48,569.75,1794597780000000000,3818 +QQQ,12882,570.17,571.02,571.55,569.91,1794597840000000000,1840 +QQQ,27634,571.02,570.70,571.57,570.47,1794597900000000000,3947 +QQQ,7435,570.70,570.28,571.07,569.73,1794597960000000000,1062 +QQQ,14881,570.28,571.51,571.57,570.25,1794598020000000000,2125 +QQQ,8930,571.51,571.92,572.60,571.50,1794598080000000000,1275 +QQQ,10002,571.92,572.34,572.72,571.62,1794598140000000000,1428 +QQQ,16133,572.34,572.31,572.52,572.27,1794598200000000000,2304 +QQQ,5187,572.31,572.10,572.33,572.02,1794598260000000000,741 +QQQ,24567,572.10,573.37,573.90,571.81,1794598320000000000,3509 +QQQ,20242,573.37,572.62,573.59,572.09,1794598380000000000,2891 +QQQ,20035,572.62,573.41,573.89,572.21,1794598440000000000,2862 +QQQ,7237,573.41,572.44,574.04,572.13,1794598500000000000,1033 +QQQ,4183,572.44,571.51,572.55,571.38,1794598560000000000,597 +QQQ,23770,571.51,572.60,573.02,571.10,1794598620000000000,3395 +QQQ,19807,572.60,572.84,573.28,572.40,1794598680000000000,2829 +QQQ,29661,572.84,572.72,573.24,572.29,1794598740000000000,4237 +QQQ,11083,572.72,573.35,573.94,572.59,1794598800000000000,1583 +QQQ,17963,573.35,573.92,574.38,573.32,1794598860000000000,2566 +QQQ,26146,573.92,574.70,575.27,573.70,1794598920000000000,3735 +QQQ,15170,574.70,575.32,575.72,574.22,1794598980000000000,2167 +QQQ,3932,575.32,575.13,575.47,574.84,1794599040000000000,561 +QQQ,27451,575.13,576.12,576.26,574.62,1794599100000000000,3921 +QQQ,6353,576.12,575.15,576.79,575.00,1794599160000000000,907 +QQQ,6285,575.15,575.32,575.99,574.89,1794599220000000000,897 +QQQ,21964,575.32,575.60,576.28,575.21,1794599280000000000,3137 +QQQ,8970,575.60,575.99,576.23,575.41,1794599340000000000,1281 +QQQ,8392,575.99,576.06,576.23,575.52,1794599400000000000,1198 +QQQ,31519,576.06,575.61,576.71,574.96,1794599460000000000,4502 +QQQ,30605,575.61,574.88,575.88,574.47,1794599520000000000,4372 +QQQ,24167,574.88,574.92,575.21,574.56,1794599580000000000,3452 +QQQ,24980,574.92,576.28,576.71,574.72,1794599640000000000,3568 +QQQ,5785,576.28,576.99,577.61,576.23,1794599700000000000,826 +QQQ,15825,576.99,575.85,577.54,575.84,1794599760000000000,2260 +QQQ,12544,575.85,575.65,575.99,575.64,1794599820000000000,1792 +QQQ,10580,575.65,574.81,576.10,574.16,1794599880000000000,1511 +QQQ,15169,574.81,574.23,575.24,573.59,1794599940000000000,2167 +QQQ,30231,574.23,574.35,574.54,573.94,1794600000000000000,4318 +QQQ,32157,574.35,574.95,575.55,574.18,1794600060000000000,4593 +QQQ,23504,574.95,575.02,575.32,574.52,1794600120000000000,3357 +QQQ,10437,575.02,576.02,576.07,574.87,1794600180000000000,1491 +QQQ,18894,576.02,576.83,577.00,575.70,1794600240000000000,2699 +QQQ,9856,576.83,576.36,577.46,576.02,1794600300000000000,1408 +QQQ,30218,576.36,577.64,578.26,575.78,1794600360000000000,4316 +QQQ,12032,577.64,577.15,578.33,576.55,1794600420000000000,1718 +QQQ,26345,577.15,578.21,578.52,576.81,1794600480000000000,3763 +QQQ,30584,578.21,577.34,578.55,577.21,1794600540000000000,4369 +QQQ,9204,577.34,576.20,577.38,576.03,1794600600000000000,1314 +QQQ,12605,576.20,576.31,576.51,575.63,1794600660000000000,1800 +QQQ,6208,576.31,575.40,576.75,575.07,1794600720000000000,886 +QQQ,21509,575.40,575.64,575.91,575.39,1794600780000000000,3072 +QQQ,31986,575.64,575.91,576.14,575.48,1794600840000000000,4569 +QQQ,14982,575.91,575.64,576.43,575.62,1794600900000000000,2140 +QQQ,28962,575.64,575.97,576.43,575.06,1794600960000000000,4137 +QQQ,4236,575.97,575.31,576.47,575.16,1794601020000000000,605 +QQQ,24425,575.31,575.21,575.88,574.55,1794601080000000000,3489 +QQQ,28969,575.21,574.86,575.53,574.26,1794601140000000000,4138 +QQQ,24002,574.86,574.04,575.25,573.88,1794601200000000000,3428 +QQQ,11171,574.04,574.12,574.49,573.35,1794601260000000000,1595 +QQQ,24813,574.12,573.55,574.15,573.36,1794601320000000000,3544 +QQQ,31661,573.55,573.63,574.28,573.01,1794601380000000000,4523 +QQQ,30681,573.63,573.23,573.76,573.13,1794601440000000000,4383 +QQQ,9141,573.23,574.44,574.61,572.93,1794601500000000000,1305 +QQQ,15513,574.44,573.74,575.12,573.25,1794601560000000000,2216 +QQQ,12882,573.74,574.43,575.06,573.17,1794601620000000000,1840 +QQQ,25413,574.43,573.25,574.87,572.76,1794601680000000000,3630 +QQQ,13822,573.25,573.81,574.18,572.88,1794601740000000000,1974 +QQQ,31992,573.81,573.42,573.98,572.86,1794601800000000000,4570 +QQQ,4963,573.42,574.43,574.91,573.00,1794601860000000000,709 +QQQ,10792,574.43,573.93,574.45,573.54,1794601920000000000,1541 +QQQ,20830,573.93,574.54,574.68,573.45,1794601980000000000,2975 +QQQ,3533,574.54,575.84,575.98,574.31,1794602040000000000,504 +QQQ,29917,575.84,576.52,576.77,575.27,1794602100000000000,4273 +QQQ,13943,576.52,576.70,577.07,576.26,1794602160000000000,1991 +QQQ,31326,576.70,576.80,576.96,576.40,1794602220000000000,4475 +QQQ,14656,576.80,576.25,576.88,576.06,1794602280000000000,2093 +QQQ,15283,576.25,576.56,577.15,575.80,1794602340000000000,2183 +QQQ,29639,576.56,577.14,577.48,576.12,1794602400000000000,4234 +QQQ,10772,577.14,578.48,578.86,576.84,1794602460000000000,1538 +QQQ,29351,578.48,579.33,579.40,578.13,1794602520000000000,4193 +QQQ,12173,579.33,579.82,579.98,578.69,1794602580000000000,1739 +QQQ,14165,579.82,579.61,579.93,579.09,1794602640000000000,2023 +QQQ,3057,579.61,579.42,580.25,578.79,1794602700000000000,436 +QQQ,32460,579.42,580.77,581.28,579.06,1794602760000000000,4637 +QQQ,13346,580.77,579.94,580.84,579.31,1794602820000000000,1906 +QQQ,22589,579.94,581.20,581.55,579.43,1794602880000000000,3227 +QQQ,21596,581.20,581.30,581.59,580.51,1794602940000000000,3085 +QQQ,8088,581.30,582.07,582.69,581.09,1794603000000000000,1155 +QQQ,11324,582.07,580.84,582.07,580.29,1794603060000000000,1617 +QQQ,3843,580.84,580.65,581.52,580.11,1794603120000000000,549 +QQQ,26951,580.65,579.65,581.16,579.62,1794603180000000000,3850 +QQQ,17985,579.65,580.99,581.25,579.56,1794603240000000000,2569 +QQQ,30594,580.99,580.00,581.08,579.33,1794603300000000000,4370 +QQQ,27258,580.00,580.29,580.59,579.99,1794603360000000000,3894 +QQQ,6322,580.29,581.49,582.04,579.65,1794603420000000000,903 +QQQ,20161,581.49,581.71,582.38,581.34,1794603480000000000,2880 +QQQ,22151,581.71,580.97,581.75,580.32,1794603540000000000,3164 +QQQ,15040,580.97,580.14,581.61,580.01,1794839400000000000,2148 +QQQ,6249,580.14,579.54,580.80,579.18,1794839460000000000,892 +QQQ,31994,579.54,579.49,579.69,579.21,1794839520000000000,4570 +QQQ,32155,579.49,578.55,579.93,578.52,1794839580000000000,4593 +QQQ,16372,578.55,579.15,579.27,578.32,1794839640000000000,2338 +QQQ,16691,579.15,578.31,579.64,577.75,1794839700000000000,2384 +QQQ,5881,578.31,579.29,579.30,577.62,1794839760000000000,840 +QQQ,31853,579.29,579.64,579.87,579.26,1794839820000000000,4550 +QQQ,11913,579.64,580.03,580.21,579.49,1794839880000000000,1701 +QQQ,8379,580.03,579.62,580.62,579.33,1794839940000000000,1197 +QQQ,28173,579.62,578.83,580.19,578.37,1794840000000000000,4024 +QQQ,29190,578.83,579.68,579.84,578.45,1794840060000000000,4170 +QQQ,16260,579.68,580.11,580.21,579.42,1794840120000000000,2322 +QQQ,27071,580.11,580.06,580.34,579.53,1794840180000000000,3867 +QQQ,30633,580.06,580.83,581.11,579.91,1794840240000000000,4376 +QQQ,15390,580.83,580.15,581.36,579.64,1794840300000000000,2198 +QQQ,16394,580.15,580.98,581.49,579.77,1794840360000000000,2342 +QQQ,25478,580.98,579.75,581.02,579.54,1794840420000000000,3639 +QQQ,13166,579.75,578.87,579.93,578.65,1794840480000000000,1880 +QQQ,10931,578.87,578.66,579.13,578.19,1794840540000000000,1561 +QQQ,26543,578.66,578.75,579.21,578.38,1794840600000000000,3791 +QQQ,22746,578.75,578.52,579.44,578.34,1794840660000000000,3249 +QQQ,29657,578.52,578.20,578.75,577.86,1794840720000000000,4236 +QQQ,14421,578.20,578.42,578.43,577.74,1794840780000000000,2060 +QQQ,25944,578.42,578.56,578.86,578.31,1794840840000000000,3706 +QQQ,19688,578.56,578.52,578.90,578.02,1794840900000000000,2812 +QQQ,25077,578.52,578.91,579.46,578.44,1794840960000000000,3582 +QQQ,20914,578.91,578.71,579.10,578.59,1794841020000000000,2987 +QQQ,27211,578.71,579.01,579.67,578.25,1794841080000000000,3887 +QQQ,26653,579.01,579.16,579.82,578.91,1794841140000000000,3807 +QQQ,20773,579.16,579.02,579.84,578.58,1794841200000000000,2967 +QQQ,6443,579.02,580.38,580.44,578.98,1794841260000000000,920 +QQQ,15583,580.38,580.61,581.01,580.26,1794841320000000000,2226 +QQQ,16771,580.61,579.77,580.94,579.44,1794841380000000000,2395 +QQQ,19074,579.77,579.35,579.94,578.98,1794841440000000000,2724 +QQQ,28204,579.35,578.86,579.95,578.72,1794841500000000000,4029 +QQQ,31324,578.86,578.04,579.15,577.60,1794841560000000000,4474 +QQQ,7392,578.04,577.09,578.21,577.03,1794841620000000000,1056 +QQQ,3682,577.09,575.98,577.66,575.77,1794841680000000000,526 +QQQ,4367,575.98,575.48,576.01,575.01,1794841740000000000,623 +QQQ,25053,575.48,574.29,576.11,573.65,1794841800000000000,3579 +QQQ,10412,574.29,574.98,575.31,573.74,1794841860000000000,1487 +QQQ,28365,574.98,574.46,575.28,574.33,1794841920000000000,4052 +QQQ,32328,574.46,575.24,575.66,574.16,1794841980000000000,4618 +QQQ,21402,575.24,576.34,576.84,575.00,1794842040000000000,3057 +QQQ,15624,576.34,576.07,576.97,576.00,1794842100000000000,2232 +QQQ,22710,576.07,575.61,576.68,575.13,1794842160000000000,3244 +QQQ,15475,575.61,576.60,576.72,575.55,1794842220000000000,2210 +QQQ,20432,576.60,575.68,576.86,574.99,1794842280000000000,2918 +QQQ,32833,575.68,574.71,575.73,574.12,1794842340000000000,4690 +QQQ,24201,574.71,575.76,576.02,574.68,1794842400000000000,3457 +QQQ,10881,575.76,576.39,576.46,575.22,1794842460000000000,1554 +QQQ,3755,576.39,577.38,577.86,576.08,1794842520000000000,536 +QQQ,20645,577.38,577.09,577.91,576.56,1794842580000000000,2949 +QQQ,3570,577.09,578.30,578.33,576.57,1794842640000000000,510 +QQQ,16044,578.30,578.69,579.09,578.07,1794842700000000000,2292 +QQQ,11583,578.69,579.54,579.56,578.00,1794842760000000000,1654 +QQQ,21675,579.54,579.92,580.32,579.22,1794842820000000000,3096 +QQQ,17852,579.92,580.19,580.88,579.80,1794842880000000000,2550 +QQQ,20986,580.19,579.22,580.56,579.12,1794842940000000000,2998 +QQQ,4182,579.22,579.48,579.65,578.72,1794843000000000000,597 +QQQ,26119,579.48,579.15,579.68,579.13,1794843060000000000,3731 +QQQ,25763,579.15,579.08,579.69,578.60,1794843120000000000,3680 +QQQ,26274,579.08,578.62,579.13,578.13,1794843180000000000,3753 +QQQ,32584,578.62,578.60,578.98,578.36,1794843240000000000,4654 +QQQ,13545,578.60,578.92,579.25,578.52,1794843300000000000,1935 +QQQ,25270,578.92,580.09,580.40,578.50,1794843360000000000,3610 +QQQ,4115,580.09,580.01,580.66,579.41,1794843420000000000,587 +QQQ,5818,580.01,579.37,580.36,579.30,1794843480000000000,831 +QQQ,7008,579.37,579.30,579.99,579.07,1794843540000000000,1001 +QQQ,19044,579.30,578.99,579.87,578.85,1794843600000000000,2720 +QQQ,30757,578.99,580.02,580.31,578.80,1794843660000000000,4393 +QQQ,29967,580.02,581.19,581.80,579.64,1794843720000000000,4281 +QQQ,23434,581.19,580.41,581.67,579.87,1794843780000000000,3347 +QQQ,27271,580.41,581.56,581.75,579.93,1794843840000000000,3895 +QQQ,19603,581.56,580.71,582.07,580.13,1794843900000000000,2800 +QQQ,9159,580.71,581.46,581.93,580.13,1794843960000000000,1308 +QQQ,12107,581.46,582.50,582.59,581.12,1794844020000000000,1729 +QQQ,13533,582.50,583.34,583.92,582.22,1794844080000000000,1933 +QQQ,29889,583.34,583.15,583.36,582.93,1794844140000000000,4269 +QQQ,18618,583.15,582.10,583.61,581.95,1794844200000000000,2659 +QQQ,26016,582.10,582.46,582.64,581.73,1794844260000000000,3716 +QQQ,5596,582.46,582.69,582.98,581.92,1794844320000000000,799 +QQQ,9572,582.69,582.08,582.70,581.46,1794844380000000000,1367 +QQQ,26866,582.08,581.57,582.50,580.89,1794844440000000000,3838 +QQQ,7769,581.57,580.83,582.12,580.41,1794844500000000000,1109 +QQQ,24698,580.83,579.68,581.25,579.61,1794844560000000000,3528 +QQQ,25288,579.68,580.74,581.15,579.45,1794844620000000000,3612 +QQQ,15172,580.74,581.51,581.80,580.64,1794844680000000000,2167 +QQQ,23222,581.51,581.56,581.93,580.96,1794844740000000000,3317 +QQQ,7161,581.56,582.54,582.95,581.27,1794844800000000000,1023 +QQQ,16062,582.54,583.56,583.60,581.88,1794844860000000000,2294 +QQQ,22422,583.56,582.46,583.87,582.01,1794844920000000000,3203 +QQQ,17098,582.46,583.73,584.08,582.35,1794844980000000000,2442 +QQQ,18186,583.73,582.73,583.96,582.04,1794845040000000000,2598 +QQQ,5984,582.73,583.93,584.51,582.55,1794845100000000000,854 +QQQ,29694,583.93,583.64,584.59,583.16,1794845160000000000,4242 +QQQ,23828,583.64,584.60,584.66,582.99,1794845220000000000,3404 +QQQ,23481,584.60,584.25,585.03,583.78,1794845280000000000,3354 +QQQ,31374,584.25,585.46,586.04,583.74,1794845340000000000,4482 +QQQ,30752,585.46,585.80,585.89,585.16,1794845400000000000,4393 +QQQ,26932,585.80,585.36,586.35,584.97,1794845460000000000,3847 +QQQ,21745,585.36,586.20,586.42,585.08,1794845520000000000,3106 +QQQ,19862,586.20,586.26,586.47,585.58,1794845580000000000,2837 +QQQ,13896,586.26,586.73,587.31,586.19,1794845640000000000,1985 +QQQ,5762,586.73,587.11,587.78,586.46,1794845700000000000,823 +QQQ,4871,587.11,586.92,587.62,586.45,1794845760000000000,695 +QQQ,31681,586.92,587.91,587.96,586.67,1794845820000000000,4525 +QQQ,7366,587.91,587.36,587.99,587.26,1794845880000000000,1052 +QQQ,28376,587.36,588.41,588.52,586.76,1794845940000000000,4053 +QQQ,3572,588.41,589.33,589.63,587.90,1794846000000000000,510 +QQQ,32526,589.33,589.03,590.00,588.55,1794846060000000000,4646 +QQQ,21252,589.03,588.57,589.39,588.03,1794846120000000000,3036 +QQQ,22760,588.57,588.35,588.69,587.76,1794846180000000000,3251 +QQQ,23184,588.35,589.66,589.90,588.05,1794846240000000000,3312 +QQQ,19328,589.66,590.24,590.47,589.32,1794846300000000000,2761 +QQQ,32729,590.24,589.22,590.90,588.63,1794846360000000000,4675 +QQQ,10541,589.22,588.13,589.60,587.83,1794846420000000000,1505 +QQQ,31737,588.13,589.47,589.61,587.48,1794846480000000000,4533 +QQQ,7603,589.47,590.83,591.24,588.91,1794846540000000000,1086 +QQQ,4390,590.83,589.82,591.13,589.57,1794846600000000000,627 +QQQ,20276,589.82,590.89,591.27,589.24,1794846660000000000,2896 +QQQ,16252,590.89,589.89,591.08,589.36,1794846720000000000,2321 +QQQ,5118,589.89,588.83,589.93,588.24,1794846780000000000,731 +QQQ,32530,588.83,589.95,589.97,588.51,1794846840000000000,4647 +QQQ,21796,589.95,588.95,590.49,588.47,1794846900000000000,3113 +QQQ,29737,588.95,589.35,589.98,588.82,1794846960000000000,4248 +QQQ,24779,589.35,590.17,590.69,589.00,1794847020000000000,3539 +QQQ,10931,590.17,591.21,591.55,589.58,1794847080000000000,1561 +QQQ,3300,591.21,591.91,591.93,591.04,1794847140000000000,471 +QQQ,30463,591.91,591.60,592.40,591.35,1794847200000000000,4351 +QQQ,26008,591.60,590.55,592.00,590.17,1794847260000000000,3715 +QQQ,14626,590.55,591.24,591.80,590.20,1794847320000000000,2089 +QQQ,10228,591.24,591.67,591.90,590.67,1794847380000000000,1461 +QQQ,12039,591.67,592.58,592.63,591.36,1794847440000000000,1719 +QQQ,25351,592.58,592.80,593.27,592.20,1794847500000000000,3621 +QQQ,30931,592.80,594.03,594.05,592.65,1794847560000000000,4418 +QQQ,26916,594.03,595.19,595.48,593.81,1794847620000000000,3845 +QQQ,15717,595.19,596.31,596.47,594.88,1794847680000000000,2245 +QQQ,30768,596.31,595.88,596.95,595.39,1794847740000000000,4395 +QQQ,4750,595.88,596.37,596.41,595.27,1794847800000000000,678 +QQQ,13810,596.37,596.54,596.58,595.96,1794847860000000000,1972 +QQQ,14043,596.54,595.63,596.86,595.40,1794847920000000000,2006 +QQQ,20898,595.63,595.63,595.91,595.08,1794847980000000000,2985 +QQQ,10879,595.63,596.57,596.94,595.05,1794848040000000000,1554 +QQQ,13589,596.57,597.35,598.02,595.93,1794848100000000000,1941 +QQQ,32710,597.35,598.18,598.82,597.14,1794848160000000000,4672 +QQQ,6781,598.18,597.77,598.64,597.66,1794848220000000000,968 +QQQ,15645,597.77,597.93,598.30,597.50,1794848280000000000,2235 +QQQ,25725,597.93,598.67,599.21,597.26,1794848340000000000,3675 +QQQ,8540,598.67,599.40,600.06,598.38,1794848400000000000,1220 +QQQ,23036,599.40,599.86,600.53,599.19,1794848460000000000,3290 +QQQ,20390,599.86,599.03,600.27,598.59,1794848520000000000,2912 +QQQ,21691,599.03,599.06,599.35,598.83,1794848580000000000,3098 +QQQ,29005,599.06,598.27,599.26,597.87,1794848640000000000,4143 +QQQ,22461,598.27,598.91,599.46,597.71,1794848700000000000,3208 +QQQ,29786,598.91,598.08,599.20,597.97,1794848760000000000,4255 +QQQ,24516,598.08,597.50,598.43,596.85,1794848820000000000,3502 +QQQ,29903,597.50,598.47,598.94,596.87,1794848880000000000,4271 +QQQ,5635,598.47,599.79,599.83,598.01,1794848940000000000,805 +QQQ,24582,599.79,599.61,600.13,598.95,1794849000000000000,3511 +QQQ,14301,599.61,599.88,600.37,598.98,1794849060000000000,2043 +QQQ,28390,599.88,600.11,600.45,599.54,1794849120000000000,4055 +QQQ,10771,600.11,600.64,601.23,599.43,1794849180000000000,1538 +QQQ,18435,600.64,601.11,601.63,599.97,1794849240000000000,2633 +QQQ,6141,601.11,602.16,602.21,600.99,1794849300000000000,877 +QQQ,3938,602.16,602.06,602.35,601.69,1794849360000000000,562 +QQQ,25371,602.06,602.98,603.56,601.95,1794849420000000000,3624 +QQQ,32144,602.98,604.11,604.54,602.80,1794849480000000000,4592 +QQQ,11975,604.11,604.85,605.47,603.46,1794849540000000000,1710 +QQQ,31457,604.85,603.65,605.51,603.34,1794849600000000000,4493 +QQQ,19526,603.65,602.72,604.15,602.41,1794849660000000000,2789 +QQQ,30711,602.72,601.63,603.23,601.01,1794849720000000000,4387 +QQQ,10946,601.63,600.46,602.17,600.20,1794849780000000000,1563 +QQQ,8423,600.46,599.45,600.75,598.97,1794849840000000000,1203 +QQQ,6894,599.45,599.88,600.13,598.77,1794849900000000000,984 +QQQ,29364,599.88,598.74,600.27,598.41,1794849960000000000,4194 +QQQ,31927,598.74,599.40,599.81,598.37,1794850020000000000,4561 +QQQ,21719,599.40,598.76,599.60,598.58,1794850080000000000,3102 +QQQ,17286,598.76,598.21,599.43,597.68,1794850140000000000,2469 +QQQ,8241,598.21,598.39,598.85,597.61,1794850200000000000,1177 +QQQ,26055,598.39,598.56,598.69,598.36,1794850260000000000,3722 +QQQ,29661,598.56,597.47,598.64,596.95,1794850320000000000,4237 +QQQ,25064,597.47,598.58,599.01,597.18,1794850380000000000,3580 +QQQ,6355,598.58,598.46,599.02,597.99,1794850440000000000,907 +QQQ,26634,598.46,598.95,598.98,598.12,1794850500000000000,3804 +QQQ,12798,598.95,600.19,600.65,598.29,1794850560000000000,1828 +QQQ,5812,600.19,601.02,601.12,599.77,1794850620000000000,830 +QQQ,20339,601.02,601.05,601.27,600.40,1794850680000000000,2905 +QQQ,13474,601.05,602.35,602.90,600.71,1794850740000000000,1924 +QQQ,11672,602.35,603.24,603.47,602.28,1794850800000000000,1667 +QQQ,19830,603.24,602.85,603.39,602.41,1794850860000000000,2832 +QQQ,11427,602.85,602.99,603.46,602.36,1794850920000000000,1632 +QQQ,12175,602.99,601.88,603.42,601.81,1794850980000000000,1739 +QQQ,32403,601.88,600.95,602.54,600.61,1794851040000000000,4629 +QQQ,22207,600.95,601.91,602.01,600.81,1794851100000000000,3172 +QQQ,21938,601.91,601.11,602.36,600.78,1794851160000000000,3134 +QQQ,24952,601.11,600.15,601.39,599.82,1794851220000000000,3564 +QQQ,17139,600.15,599.04,600.64,598.45,1794851280000000000,2448 +QQQ,12758,599.04,600.15,600.48,598.58,1794851340000000000,1822 +QQQ,32225,600.15,599.56,600.83,599.11,1794851400000000000,4603 +QQQ,29401,599.56,598.55,600.08,597.92,1794851460000000000,4200 +QQQ,26353,598.55,597.46,598.87,597.16,1794851520000000000,3764 +QQQ,27595,597.46,596.34,597.59,596.30,1794851580000000000,3942 +QQQ,30410,596.34,595.70,596.81,595.03,1794851640000000000,4344 +QQQ,28392,595.70,594.69,596.16,594.57,1794851700000000000,4056 +QQQ,10470,594.69,594.11,595.19,593.82,1794851760000000000,1495 +QQQ,15073,594.11,595.28,595.36,593.74,1794851820000000000,2153 +QQQ,11275,595.28,594.35,595.49,593.88,1794851880000000000,1610 +QQQ,14053,594.35,595.05,595.31,593.67,1794851940000000000,2007 +QQQ,18330,595.05,594.22,595.55,593.77,1794852000000000000,2618 +QQQ,31694,594.22,593.51,594.66,593.24,1794852060000000000,4527 +QQQ,6448,593.51,594.41,594.76,592.87,1794852120000000000,921 +QQQ,5327,594.41,594.17,595.03,594.16,1794852180000000000,761 +QQQ,25115,594.17,594.47,594.49,594.01,1794852240000000000,3587 +QQQ,25134,594.47,593.41,594.53,592.87,1794852300000000000,3590 +QQQ,32805,593.41,593.10,593.46,592.51,1794852360000000000,4686 +QQQ,14348,593.10,593.78,594.42,592.86,1794852420000000000,2049 +QQQ,20453,593.78,592.96,594.13,592.95,1794852480000000000,2921 +QQQ,10029,592.96,591.85,593.30,591.64,1794852540000000000,1432 +QQQ,13946,591.85,592.15,592.70,591.81,1794852600000000000,1992 +QQQ,4596,592.15,593.18,593.59,591.68,1794852660000000000,656 +QQQ,7267,593.18,592.83,593.37,592.79,1794852720000000000,1038 +QQQ,24773,592.83,593.19,593.69,592.79,1794852780000000000,3539 +QQQ,25557,593.19,593.25,593.70,592.84,1794852840000000000,3651 +QQQ,25943,593.25,592.17,593.79,591.82,1794852900000000000,3706 +QQQ,31528,592.17,591.88,592.18,591.82,1794852960000000000,4504 +QQQ,19023,591.88,591.60,592.16,591.18,1794853020000000000,2717 +QQQ,25568,591.60,591.57,592.01,591.44,1794853080000000000,3652 +QQQ,23749,591.57,592.27,592.38,591.29,1794853140000000000,3392 +QQQ,31081,592.27,592.76,592.84,591.97,1794853200000000000,4440 +QQQ,24060,592.76,592.85,593.01,592.61,1794853260000000000,3437 +QQQ,16099,592.85,593.43,593.61,592.60,1794853320000000000,2299 +QQQ,19733,593.43,592.51,594.05,592.20,1794853380000000000,2819 +QQQ,8269,592.51,593.40,593.83,592.42,1794853440000000000,1181 +QQQ,4993,593.40,593.28,593.62,593.10,1794853500000000000,713 +QQQ,32011,593.28,592.93,593.34,592.58,1794853560000000000,4573 +QQQ,18124,592.93,591.88,593.09,591.69,1794853620000000000,2589 +QQQ,5421,591.88,592.88,593.28,591.37,1794853680000000000,774 +QQQ,13454,592.88,592.89,592.90,592.29,1794853740000000000,1922 +QQQ,21472,592.89,593.76,593.84,592.24,1794853800000000000,3067 +QQQ,20220,593.76,592.57,594.01,591.94,1794853860000000000,2888 +QQQ,13816,592.57,593.38,593.70,592.29,1794853920000000000,1973 +QQQ,22041,593.38,594.15,594.81,592.93,1794853980000000000,3148 +QQQ,4339,594.15,593.07,594.77,592.71,1794854040000000000,619 +QQQ,22267,593.07,594.29,594.71,593.04,1794854100000000000,3181 +QQQ,12394,594.29,593.38,594.55,592.77,1794854160000000000,1770 +QQQ,30420,593.38,593.38,593.93,592.94,1794854220000000000,4345 +QQQ,14336,593.38,593.55,593.90,592.72,1794854280000000000,2048 +QQQ,17134,593.55,592.59,593.73,592.22,1794854340000000000,2447 +QQQ,25385,592.59,593.19,593.84,592.04,1794854400000000000,3626 +QQQ,13838,593.19,594.30,594.89,592.70,1794854460000000000,1976 +QQQ,14422,594.30,594.33,594.94,594.20,1794854520000000000,2060 +QQQ,3511,594.33,595.30,595.38,593.93,1794854580000000000,501 +QQQ,21470,595.30,595.82,596.09,594.63,1794854640000000000,3067 +QQQ,5934,595.82,595.36,596.36,595.10,1794854700000000000,847 +QQQ,15153,595.36,596.19,596.68,594.78,1794854760000000000,2164 +QQQ,4645,596.19,597.48,597.85,595.78,1794854820000000000,663 +QQQ,20190,597.48,597.66,598.06,596.83,1794854880000000000,2884 +QQQ,10707,597.66,597.49,597.73,596.86,1794854940000000000,1529 +QQQ,21112,597.49,598.12,598.23,596.83,1794855000000000000,3016 +QQQ,25334,598.12,599.22,599.72,597.86,1794855060000000000,3619 +QQQ,5752,599.22,598.59,599.35,597.96,1794855120000000000,821 +QQQ,31263,598.59,597.82,598.95,597.24,1794855180000000000,4466 +QQQ,20287,597.82,597.22,598.32,596.98,1794855240000000000,2898 +QQQ,18308,597.22,597.09,597.24,596.81,1794855300000000000,2615 +QQQ,19588,597.09,596.16,597.72,595.51,1794855360000000000,2798 +QQQ,20069,596.16,596.45,596.93,595.86,1794855420000000000,2867 +QQQ,11015,596.45,596.02,596.54,595.36,1794855480000000000,1573 +QQQ,26275,596.02,597.19,597.68,595.43,1794855540000000000,3753 +QQQ,32083,597.19,597.19,597.43,596.55,1794855600000000000,4583 +QQQ,10907,597.19,596.93,597.61,596.67,1794855660000000000,1558 +QQQ,21617,596.93,596.14,597.26,595.88,1794855720000000000,3088 +QQQ,27348,596.14,595.51,596.25,595.21,1794855780000000000,3906 +QQQ,22451,595.51,595.40,595.99,595.26,1794855840000000000,3207 +QQQ,14967,595.40,594.17,595.70,593.59,1794855900000000000,2138 +QQQ,25232,594.17,594.47,594.88,593.93,1794855960000000000,3604 +QQQ,28967,594.47,595.30,595.51,594.41,1794856020000000000,4138 +QQQ,30347,595.30,594.84,595.88,594.60,1794856080000000000,4335 +QQQ,30294,594.84,593.62,595.50,593.28,1794856140000000000,4327 +QQQ,29062,593.62,594.73,595.13,593.59,1794856200000000000,4151 +QQQ,17533,594.73,595.29,595.73,594.28,1794856260000000000,2504 +QQQ,10024,595.29,594.41,595.86,594.24,1794856320000000000,1432 +QQQ,4759,594.41,593.32,594.49,592.75,1794856380000000000,679 +QQQ,22636,593.32,594.05,594.63,592.90,1794856440000000000,3233 +QQQ,13281,594.05,594.50,594.93,593.37,1794856500000000000,1897 +QQQ,9017,594.50,594.94,595.34,594.02,1794856560000000000,1288 +QQQ,21945,594.94,594.80,595.29,594.59,1794856620000000000,3135 +QQQ,32457,594.80,594.65,595.11,594.15,1794856680000000000,4636 +QQQ,26533,594.65,595.28,595.55,594.26,1794856740000000000,3790 +QQQ,30885,595.28,594.73,595.55,594.32,1794856800000000000,4412 +QQQ,23636,594.73,594.86,594.90,594.24,1794856860000000000,3376 +QQQ,4284,594.86,595.84,596.09,594.26,1794856920000000000,612 +QQQ,16871,595.84,596.36,596.59,595.16,1794856980000000000,2410 +QQQ,11089,596.36,596.95,597.53,595.79,1794857040000000000,1584 +QQQ,5880,596.95,597.14,597.40,596.72,1794857100000000000,840 +QQQ,29179,597.14,597.31,597.83,596.45,1794857160000000000,4168 +QQQ,10800,597.31,596.74,597.35,596.21,1794857220000000000,1542 +QQQ,4253,596.74,596.96,596.98,596.67,1794857280000000000,607 +QQQ,27802,596.96,596.90,597.57,596.45,1794857340000000000,3971 +QQQ,15413,596.90,598.04,598.22,596.49,1794857400000000000,2201 +QQQ,25565,598.04,598.55,598.88,597.79,1794857460000000000,3652 +QQQ,22576,598.55,599.45,599.81,598.43,1794857520000000000,3225 +QQQ,3693,599.45,599.96,600.40,599.14,1794857580000000000,527 +QQQ,12259,599.96,599.92,600.65,599.79,1794857640000000000,1751 +QQQ,3439,599.92,599.84,600.30,599.70,1794857700000000000,491 +QQQ,30729,599.84,599.25,600.12,598.57,1794857760000000000,4389 +QQQ,29119,599.25,599.54,599.86,598.71,1794857820000000000,4159 +QQQ,7526,599.54,599.80,600.47,599.08,1794857880000000000,1075 +QQQ,7585,599.80,600.73,601.13,599.70,1794857940000000000,1083 +QQQ,22198,600.73,600.52,601.03,599.95,1794858000000000000,3171 +QQQ,11965,600.52,600.37,601.19,599.99,1794858060000000000,1709 +QQQ,6919,600.37,601.23,601.30,600.34,1794858120000000000,988 +QQQ,12780,601.23,602.08,602.78,600.60,1794858180000000000,1825 +QQQ,29972,602.08,602.06,602.10,601.92,1794858240000000000,4281 +QQQ,11581,602.06,602.00,602.68,601.73,1794858300000000000,1654 +QQQ,10703,602.00,602.57,603.12,601.40,1794858360000000000,1529 +QQQ,24374,602.57,602.02,602.59,601.73,1794858420000000000,3482 +QQQ,29619,602.02,602.28,602.47,601.62,1794858480000000000,4231 +QQQ,24646,602.28,603.62,604.13,602.05,1794858540000000000,3520 +QQQ,27928,603.62,604.48,605.16,603.39,1794858600000000000,3989 +QQQ,11123,604.48,603.45,604.96,602.93,1794858660000000000,1589 +QQQ,10339,603.45,602.48,603.72,602.21,1794858720000000000,1477 +QQQ,15095,602.48,602.77,602.87,601.81,1794858780000000000,2156 +QQQ,25345,602.77,602.24,603.30,601.84,1794858840000000000,3620 +QQQ,32160,602.24,603.00,603.11,601.97,1794858900000000000,4594 +QQQ,12448,603.00,603.06,603.73,602.73,1794858960000000000,1778 +QQQ,26487,603.06,603.78,604.27,602.59,1794859020000000000,3783 +QQQ,6722,603.78,603.42,604.42,603.39,1794859080000000000,960 +QQQ,17645,603.42,603.99,604.68,602.84,1794859140000000000,2520 +QQQ,29633,603.99,603.90,604.26,603.72,1794859200000000000,4233 +QQQ,5612,603.90,604.50,604.73,603.57,1794859260000000000,801 +QQQ,4272,604.50,605.08,605.41,604.43,1794859320000000000,610 +QQQ,28878,605.08,605.81,606.29,604.45,1794859380000000000,4125 +QQQ,31572,605.81,605.85,605.89,605.57,1794859440000000000,4510 +QQQ,27796,605.85,604.68,606.46,604.23,1794859500000000000,3970 +QQQ,23784,604.68,605.85,606.51,604.08,1794859560000000000,3397 +QQQ,17525,605.85,606.16,606.64,605.75,1794859620000000000,2503 +QQQ,8523,606.16,606.16,606.57,606.11,1794859680000000000,1217 +QQQ,21142,606.16,606.57,606.65,605.68,1794859740000000000,3020 +QQQ,4442,606.57,607.59,607.65,606.04,1794859800000000000,634 +QQQ,19980,607.59,608.60,609.25,606.90,1794859860000000000,2854 +QQQ,27552,608.60,609.24,609.76,608.44,1794859920000000000,3936 +QQQ,15588,609.24,610.24,610.89,608.86,1794859980000000000,2226 +QQQ,25651,610.24,609.37,610.29,609.07,1794860040000000000,3664 +QQQ,10601,609.37,608.22,609.96,607.94,1794860100000000000,1514 +QQQ,15023,608.22,607.84,608.75,607.39,1794860160000000000,2146 +QQQ,8527,607.84,608.40,608.54,607.64,1794860220000000000,1218 +QQQ,7607,608.40,609.41,609.95,607.73,1794860280000000000,1086 +QQQ,22672,609.41,610.66,610.67,609.31,1794860340000000000,3238 +QQQ,9838,610.66,610.76,611.28,609.99,1794860400000000000,1405 +QQQ,10916,610.76,610.58,611.00,610.41,1794860460000000000,1559 +QQQ,27773,610.58,610.43,610.95,610.20,1794860520000000000,3967 +QQQ,13373,610.43,611.14,611.49,610.40,1794860580000000000,1910 +QQQ,24898,611.14,610.19,611.28,610.09,1794860640000000000,3556 +QQQ,11902,610.19,609.90,610.63,609.41,1794860700000000000,1700 +QQQ,5252,609.90,609.91,610.32,609.24,1794860760000000000,750 +QQQ,23507,609.91,611.20,611.88,609.48,1794860820000000000,3358 +QQQ,5748,611.20,610.41,611.46,609.73,1794860880000000000,821 +QQQ,30833,610.41,611.03,611.27,609.77,1794860940000000000,4404 +QQQ,23964,611.03,610.28,611.65,609.86,1794861000000000000,3423 +QQQ,23316,610.28,609.61,610.65,609.35,1794861060000000000,3330 +QQQ,14368,609.61,610.35,610.43,609.46,1794861120000000000,2052 +QQQ,22139,610.35,609.96,610.52,609.56,1794861180000000000,3162 +QQQ,27515,609.96,609.18,610.13,608.79,1794861240000000000,3930 +QQQ,10926,609.18,608.98,609.69,608.30,1794861300000000000,1560 +QQQ,20281,608.98,610.11,610.41,608.87,1794861360000000000,2897 +QQQ,17594,610.11,610.35,610.99,609.67,1794861420000000000,2513 +QQQ,20796,610.35,609.54,610.42,609.40,1794861480000000000,2970 +QQQ,16599,609.54,610.45,610.87,609.39,1794861540000000000,2371 +QQQ,5456,610.45,611.00,611.04,609.98,1794861600000000000,779 +QQQ,6634,611.00,610.49,611.17,610.35,1794861660000000000,947 +QQQ,20608,610.49,611.36,611.75,609.96,1794861720000000000,2944 +QQQ,26876,611.36,612.25,612.70,610.98,1794861780000000000,3839 +QQQ,9928,612.25,611.09,612.54,610.95,1794861840000000000,1418 +QQQ,4468,611.09,611.41,611.74,610.66,1794861900000000000,638 +QQQ,3295,611.41,611.87,611.95,611.11,1794861960000000000,470 +QQQ,30421,611.87,612.73,612.75,611.85,1794862020000000000,4345 +QQQ,21750,612.73,613.47,613.97,612.67,1794862080000000000,3107 +QQQ,11107,613.47,613.55,613.61,613.33,1794862140000000000,1586 +QQQ,30621,613.55,614.53,614.95,613.20,1794862200000000000,4374 +QQQ,10032,614.53,614.72,614.80,614.45,1794862260000000000,1433 +QQQ,7224,614.72,614.17,614.86,613.87,1794862320000000000,1032 +QQQ,6514,614.17,613.65,614.65,613.42,1794862380000000000,930 +QQQ,26679,613.65,612.49,613.95,611.85,1794862440000000000,3811 +QQQ,19905,612.49,613.09,613.28,611.91,1794862500000000000,2843 +QQQ,4865,613.09,612.40,613.30,611.98,1794862560000000000,695 +QQQ,10310,612.40,612.81,612.89,612.21,1794862620000000000,1472 +QQQ,16680,612.81,613.02,613.05,612.66,1794862680000000000,2382 +QQQ,5718,613.02,613.92,614.42,612.95,1794862740000000000,816 diff --git a/tests/data/SPY_fake.csv b/tests/data/SPY_fake.csv new file mode 100644 index 0000000..5f4e7a2 --- /dev/null +++ b/tests/data/SPY_fake.csv @@ -0,0 +1,121 @@ +ticker,volume,open,close,high,low,window_start,transactions +SPY,17171,660.00,659.49,660.24,659.31,1794493800000000000,1561 +SPY,31882,659.49,660.23,660.35,659.32,1794493860000000000,1275 +SPY,2460,660.23,659.71,660.35,659.51,1794493920000000000,102 +SPY,17173,659.71,659.37,659.77,659.06,1794493980000000000,1561 +SPY,1878,659.37,659.11,659.75,658.68,1794494040000000000,170 +SPY,27854,659.11,659.36,659.52,658.72,1794494100000000000,1392 +SPY,42048,659.36,659.00,659.40,658.66,1794494160000000000,2628 +SPY,37479,659.00,658.42,659.05,658.12,1794494220000000000,3747 +SPY,15216,658.42,658.14,658.62,658.02,1794494280000000000,1268 +SPY,16665,658.14,658.42,658.44,657.88,1794494340000000000,724 +SPY,10169,658.42,658.37,658.82,658.33,1794494400000000000,635 +SPY,31564,658.37,658.67,658.93,658.17,1794494460000000000,2630 +SPY,12123,658.67,658.20,658.98,657.98,1794494520000000000,505 +SPY,10316,658.20,657.57,658.27,657.32,1794494580000000000,1289 +SPY,10175,657.57,657.13,657.91,656.92,1794494640000000000,484 +SPY,35318,657.13,656.29,657.25,655.89,1794494700000000000,3924 +SPY,28367,656.29,657.04,657.30,655.92,1794494760000000000,1289 +SPY,30316,657.04,657.20,657.37,656.74,1794494820000000000,3368 +SPY,10098,657.20,658.09,658.44,657.15,1794494880000000000,1262 +SPY,42874,658.09,658.94,659.35,657.86,1794494940000000000,2256 +SPY,10492,658.94,659.24,659.41,658.54,1794495000000000000,1165 +SPY,35345,659.24,659.39,659.80,659.01,1794495060000000000,2718 +SPY,25115,659.39,659.97,660.00,659.33,1794495120000000000,1793 +SPY,22783,659.97,659.91,660.30,659.71,1794495180000000000,1340 +SPY,19127,659.91,660.09,660.54,659.73,1794495240000000000,796 +SPY,4644,660.09,659.78,660.18,659.47,1794495300000000000,258 +SPY,16670,659.78,660.12,660.52,659.64,1794495360000000000,1515 +SPY,14061,660.12,660.54,660.81,659.91,1794495420000000000,937 +SPY,4331,660.54,661.48,661.89,660.28,1794495480000000000,188 +SPY,11557,661.48,661.81,661.89,661.22,1794495540000000000,502 +SPY,32484,661.81,662.63,662.85,661.65,1794495600000000000,4060 +SPY,44926,662.63,661.75,662.94,661.40,1794495660000000000,4492 +SPY,30274,661.75,661.76,662.08,661.50,1794495720000000000,3784 +SPY,33649,661.76,661.11,662.06,660.93,1794495780000000000,2243 +SPY,35639,661.11,661.90,662.00,661.04,1794495840000000000,2969 +SPY,38016,661.90,661.09,662.30,660.98,1794495900000000000,2000 +SPY,42237,661.09,662.00,662.12,660.89,1794495960000000000,3249 +SPY,14868,662.00,661.48,662.32,661.38,1794496020000000000,1062 +SPY,15920,661.48,662.33,662.57,661.31,1794496080000000000,796 +SPY,6394,662.33,662.69,663.13,661.97,1794496140000000000,710 +SPY,23912,662.69,661.86,663.07,661.81,1794496200000000000,1494 +SPY,3717,661.86,660.98,661.92,660.67,1794496260000000000,154 +SPY,40972,660.98,660.67,661.38,660.49,1794496320000000000,2926 +SPY,9986,660.67,660.44,660.86,660.31,1794496380000000000,907 +SPY,39576,660.44,660.91,660.95,660.21,1794496440000000000,2826 +SPY,22013,660.91,660.02,661.01,659.99,1794496500000000000,1000 +SPY,42255,660.02,660.22,660.31,660.00,1794496560000000000,3521 +SPY,42488,660.22,659.69,660.45,659.40,1794496620000000000,1847 +SPY,26294,659.69,659.54,659.78,659.20,1794496680000000000,2921 +SPY,24471,659.54,658.85,659.70,658.44,1794496740000000000,2447 +SPY,26095,658.85,659.61,659.86,658.53,1794496800000000000,1863 +SPY,30384,659.61,659.45,659.67,659.36,1794496860000000000,3798 +SPY,21961,659.45,659.31,659.76,659.20,1794496920000000000,1464 +SPY,21675,659.31,659.60,659.67,658.94,1794496980000000000,2709 +SPY,42310,659.60,659.60,659.84,659.43,1794497040000000000,2115 +SPY,32772,659.60,659.97,660.01,659.21,1794497100000000000,1724 +SPY,2744,659.97,659.15,660.08,659.02,1794497160000000000,182 +SPY,35643,659.15,658.31,659.34,657.92,1794497220000000000,3960 +SPY,36255,658.31,658.98,659.18,657.95,1794497280000000000,1812 +SPY,22461,658.98,658.82,659.20,658.57,1794497340000000000,2041 +SPY,36668,658.82,659.34,659.37,658.43,1794497400000000000,1666 +SPY,22626,659.34,659.97,660.35,659.06,1794497460000000000,1740 +SPY,24273,659.97,660.81,660.99,659.68,1794497520000000000,1213 +SPY,30522,660.81,660.99,661.40,660.54,1794497580000000000,1526 +SPY,35566,660.99,660.46,661.25,660.36,1794497640000000000,3951 +SPY,14789,660.46,660.05,660.77,659.92,1794497700000000000,821 +SPY,36057,660.05,660.49,660.53,659.79,1794497760000000000,3004 +SPY,42294,660.49,660.65,660.71,660.16,1794497820000000000,2226 +SPY,40939,660.65,660.81,660.94,660.53,1794497880000000000,4093 +SPY,35446,660.81,661.01,661.18,660.55,1794497940000000000,2085 +SPY,22412,661.01,660.74,661.45,660.42,1794498000000000000,974 +SPY,15587,660.74,659.93,660.78,659.65,1794498060000000000,677 +SPY,37522,659.93,659.15,660.01,659.03,1794498120000000000,4169 +SPY,44124,659.15,659.29,659.32,658.71,1794498180000000000,2595 +SPY,30318,659.29,659.35,659.44,659.13,1794498240000000000,2165 +SPY,14773,659.35,659.57,659.65,659.12,1794498300000000000,984 +SPY,17174,659.57,660.12,660.19,659.23,1794498360000000000,2146 +SPY,15354,660.12,660.79,661.12,659.80,1794498420000000000,1096 +SPY,16247,660.79,660.53,661.20,660.42,1794498480000000000,1477 +SPY,12390,660.53,660.84,660.89,660.46,1794498540000000000,516 +SPY,41682,660.84,660.93,661.13,660.44,1794498600000000000,2977 +SPY,19672,660.93,660.14,661.35,659.99,1794498660000000000,894 +SPY,3291,660.14,659.43,660.51,659.30,1794498720000000000,205 +SPY,42129,659.43,658.99,659.63,658.66,1794498780000000000,5266 +SPY,6277,658.99,659.32,659.37,658.79,1794498840000000000,251 +SPY,20502,659.32,659.61,659.74,659.05,1794498900000000000,976 +SPY,24747,659.61,658.86,659.88,658.78,1794498960000000000,1455 +SPY,33696,658.86,658.99,659.21,658.47,1794499020000000000,2246 +SPY,21472,658.99,658.22,659.07,657.94,1794499080000000000,894 +SPY,24628,658.22,657.72,658.31,657.40,1794499140000000000,1231 +SPY,21257,657.72,657.38,658.12,657.03,1794499200000000000,1180 +SPY,43832,657.38,657.07,657.51,656.73,1794499260000000000,5479 +SPY,13251,657.07,656.61,657.20,656.44,1794499320000000000,662 +SPY,22692,656.61,657.09,657.34,656.49,1794499380000000000,1031 +SPY,19955,657.09,656.62,657.43,656.26,1794499440000000000,1425 +SPY,20763,656.62,655.86,656.92,655.62,1794499500000000000,1038 +SPY,22494,655.86,655.01,656.13,654.79,1794499560000000000,1730 +SPY,6533,655.01,654.72,655.19,654.32,1794499620000000000,435 +SPY,27082,654.72,655.13,655.39,654.32,1794499680000000000,1593 +SPY,43522,655.13,655.23,655.58,655.04,1794499740000000000,3108 +SPY,31582,655.23,655.56,655.61,654.83,1794499800000000000,1503 +SPY,10061,655.56,656.47,656.65,655.51,1794499860000000000,914 +SPY,14964,656.47,656.26,656.62,656.06,1794499920000000000,598 +SPY,16125,656.26,655.51,656.64,655.13,1794499980000000000,645 +SPY,21701,655.51,654.91,655.72,654.67,1794500040000000000,1085 +SPY,36817,654.91,655.68,655.92,654.71,1794500100000000000,1753 +SPY,39741,655.68,655.68,655.84,655.53,1794500160000000000,2207 +SPY,13451,655.68,656.26,656.47,655.24,1794500220000000000,791 +SPY,26108,656.26,656.23,656.57,655.86,1794500280000000000,2610 +SPY,40304,656.23,655.80,656.45,655.71,1794500340000000000,3664 +SPY,17056,655.80,655.48,655.87,655.27,1794500400000000000,682 +SPY,2739,655.48,654.67,655.80,654.24,1794500460000000000,210 +SPY,34501,654.67,654.78,655.03,654.45,1794500520000000000,3450 +SPY,6849,654.78,655.44,655.49,654.50,1794500580000000000,342 +SPY,2777,655.44,655.19,655.54,655.13,1794500640000000000,173 +SPY,35876,655.19,654.85,655.57,654.77,1794500700000000000,2562 +SPY,17708,654.85,655.57,655.93,654.44,1794500760000000000,1180 +SPY,27839,655.57,656.02,656.21,655.26,1794500820000000000,2141 +SPY,14012,656.02,656.12,656.14,655.81,1794500880000000000,1401 +SPY,15679,656.12,655.88,656.53,655.62,1794500940000000000,1742 diff --git a/tests/data/SPY_fake_long.csv b/tests/data/SPY_fake_long.csv new file mode 100644 index 0000000..ae66b96 --- /dev/null +++ b/tests/data/SPY_fake_long.csv @@ -0,0 +1,1171 @@ +ticker,volume,open,close,high,low,window_start,transactions +SPY,42575,655.00,653.98,655.50,653.75,1794493800000000000,5321 +SPY,26455,653.98,653.87,654.30,653.79,1794493860000000000,2939 +SPY,58868,653.87,654.06,654.43,653.83,1794493920000000000,2559 +SPY,20694,654.06,652.51,654.48,651.99,1794493980000000000,862 +SPY,3872,652.51,653.19,653.46,652.34,1794494040000000000,168 +SPY,19331,653.19,654.34,654.59,652.77,1794494100000000000,1610 +SPY,24220,654.34,655.35,655.90,653.81,1794494160000000000,1053 +SPY,26700,655.35,656.20,656.75,655.18,1794494220000000000,1483 +SPY,28182,656.20,656.30,656.62,655.79,1794494280000000000,1483 +SPY,2539,656.30,656.47,656.54,655.80,1794494340000000000,317 +SPY,56395,656.47,657.28,657.70,656.06,1794494400000000000,4699 +SPY,34429,657.28,655.92,657.86,655.34,1794494460000000000,2459 +SPY,22141,655.92,655.58,656.34,655.44,1794494520000000000,2214 +SPY,34939,655.58,656.79,657.25,655.06,1794494580000000000,2687 +SPY,54250,656.79,657.87,658.04,656.48,1794494640000000000,5425 +SPY,40982,657.87,658.90,659.41,657.39,1794494700000000000,1639 +SPY,59539,658.90,659.40,659.91,658.40,1794494760000000000,4579 +SPY,9666,659.40,660.66,661.18,658.82,1794494820000000000,402 +SPY,4258,660.66,659.73,660.97,659.33,1794494880000000000,170 +SPY,49737,659.73,661.23,661.26,659.37,1794494940000000000,2486 +SPY,41672,661.23,660.80,661.55,660.22,1794495000000000000,2451 +SPY,9445,660.80,659.54,660.95,659.07,1794495060000000000,393 +SPY,59644,659.54,659.07,660.04,659.03,1794495120000000000,4260 +SPY,57234,659.07,660.52,660.73,658.93,1794495180000000000,4769 +SPY,23106,660.52,662.02,662.28,660.12,1794495240000000000,2310 +SPY,18219,662.02,663.43,663.98,661.99,1794495300000000000,1821 +SPY,43183,663.43,663.66,664.23,662.85,1794495360000000000,2272 +SPY,46473,663.66,663.68,663.83,663.13,1794495420000000000,3574 +SPY,21543,663.68,664.06,664.30,663.23,1794495480000000000,936 +SPY,42705,664.06,664.31,664.40,663.71,1794495540000000000,4270 +SPY,11666,664.31,664.93,665.12,664.08,1794495600000000000,1296 +SPY,4662,664.93,665.66,666.09,664.50,1794495660000000000,202 +SPY,10916,665.66,664.60,665.72,664.17,1794495720000000000,682 +SPY,36816,664.60,663.60,664.85,663.51,1794495780000000000,3068 +SPY,57436,663.60,663.08,663.64,663.01,1794495840000000000,7179 +SPY,34514,663.08,661.78,663.27,661.18,1794495900000000000,2300 +SPY,39371,661.78,661.90,662.36,661.68,1794495960000000000,2460 +SPY,46741,661.90,660.87,662.10,660.57,1794496020000000000,2032 +SPY,26084,660.87,662.25,662.77,660.53,1794496080000000000,1242 +SPY,40136,662.25,663.59,664.13,662.07,1794496140000000000,2866 +SPY,40809,663.59,662.87,664.03,662.46,1794496200000000000,4080 +SPY,44259,662.87,661.70,663.12,661.65,1794496260000000000,2212 +SPY,12144,661.70,663.17,663.54,661.37,1794496320000000000,1349 +SPY,9459,663.17,662.70,663.62,662.37,1794496380000000000,472 +SPY,41576,662.70,664.17,664.39,662.17,1794496440000000000,5197 +SPY,38585,664.17,663.98,664.65,663.62,1794496500000000000,4287 +SPY,53308,663.98,664.87,665.37,663.76,1794496560000000000,3807 +SPY,13443,664.87,665.51,665.59,664.81,1794496620000000000,707 +SPY,23938,665.51,665.53,665.75,665.41,1794496680000000000,2992 +SPY,58018,665.53,666.34,666.41,665.06,1794496740000000000,3223 +SPY,48794,666.34,667.58,667.68,665.88,1794496800000000000,4435 +SPY,51659,667.58,666.72,668.13,666.33,1794496860000000000,3443 +SPY,38856,666.72,666.51,666.83,666.08,1794496920000000000,2988 +SPY,47333,666.51,667.25,667.64,666.21,1794496980000000000,1893 +SPY,58782,667.25,667.58,667.74,666.78,1794497040000000000,2449 +SPY,9082,667.58,666.52,667.78,665.95,1794497100000000000,454 +SPY,3037,666.52,666.88,666.95,666.36,1794497160000000000,121 +SPY,37385,666.88,665.35,667.21,664.79,1794497220000000000,3738 +SPY,3249,665.35,663.80,665.88,663.44,1794497280000000000,162 +SPY,57871,663.80,664.45,664.65,663.29,1794497340000000000,3045 +SPY,24509,664.45,664.69,664.85,663.91,1794497400000000000,1633 +SPY,1189,664.69,663.73,665.24,663.31,1794497460000000000,148 +SPY,10021,663.73,662.41,664.02,662.00,1794497520000000000,501 +SPY,56777,662.41,662.55,663.13,662.13,1794497580000000000,4731 +SPY,25424,662.55,663.30,663.68,662.51,1794497640000000000,1155 +SPY,52909,663.30,664.68,665.01,663.12,1794497700000000000,2939 +SPY,52029,664.68,665.85,666.09,664.46,1794497760000000000,2081 +SPY,59410,665.85,665.71,666.07,665.31,1794497820000000000,5941 +SPY,24922,665.71,665.33,666.03,665.17,1794497880000000000,1083 +SPY,10556,665.33,666.17,666.73,665.01,1794497940000000000,703 +SPY,11428,666.17,667.60,667.99,665.63,1794498000000000000,761 +SPY,5224,667.60,668.26,668.34,667.34,1794498060000000000,307 +SPY,18397,668.26,666.73,668.66,666.16,1794498120000000000,1082 +SPY,10288,666.73,666.04,666.88,665.77,1794498180000000000,643 +SPY,19458,666.04,667.01,667.44,665.87,1794498240000000000,1768 +SPY,23631,667.01,666.70,667.10,666.67,1794498300000000000,1687 +SPY,56059,666.70,668.13,668.15,666.11,1794498360000000000,4312 +SPY,44824,668.13,667.03,668.23,666.81,1794498420000000000,1792 +SPY,37510,667.03,668.33,668.82,666.48,1794498480000000000,3410 +SPY,13515,668.33,668.29,668.63,668.12,1794498540000000000,795 +SPY,44403,668.29,667.01,668.63,666.50,1794498600000000000,1850 +SPY,6960,667.01,667.41,667.70,666.52,1794498660000000000,773 +SPY,893,667.41,666.86,667.84,666.54,1794498720000000000,42 +SPY,1170,666.86,668.37,668.72,666.81,1794498780000000000,97 +SPY,1149,668.37,668.09,668.74,667.85,1794498840000000000,45 +SPY,22452,668.09,668.64,669.12,667.84,1794498900000000000,1122 +SPY,4290,668.64,668.70,668.96,668.52,1794498960000000000,186 +SPY,57862,668.70,668.74,668.99,668.23,1794499020000000000,3214 +SPY,37387,668.74,670.27,670.69,668.19,1794499080000000000,1967 +SPY,58716,670.27,669.82,670.41,669.29,1794499140000000000,2796 +SPY,9309,669.82,669.47,670.00,668.94,1794499200000000000,1163 +SPY,28240,669.47,670.32,670.73,669.42,1794499260000000000,1227 +SPY,29405,670.32,670.41,670.84,670.07,1794499320000000000,1837 +SPY,50130,670.41,670.70,671.16,670.02,1794499380000000000,3580 +SPY,53810,670.70,671.60,671.99,670.28,1794499440000000000,5978 +SPY,51013,671.60,671.07,671.66,670.98,1794499500000000000,3000 +SPY,12993,671.07,671.43,671.64,670.48,1794499560000000000,1082 +SPY,17414,671.43,670.32,671.53,670.03,1794499620000000000,791 +SPY,38006,670.32,670.56,671.01,669.78,1794499680000000000,3800 +SPY,17055,670.56,670.63,671.02,670.21,1794499740000000000,682 +SPY,33609,670.63,669.68,670.91,669.51,1794499800000000000,1680 +SPY,4504,669.68,669.13,670.05,668.98,1794499860000000000,409 +SPY,3236,669.13,667.59,669.34,667.45,1794499920000000000,140 +SPY,56918,667.59,667.03,668.05,666.57,1794499980000000000,5691 +SPY,34949,667.03,668.55,669.05,666.83,1794500040000000000,4368 +SPY,54543,668.55,669.86,670.45,668.02,1794500100000000000,4195 +SPY,16395,669.86,670.38,670.62,669.40,1794500160000000000,1261 +SPY,45889,670.38,671.55,671.80,670.07,1794500220000000000,2415 +SPY,20314,671.55,671.59,672.19,671.43,1794500280000000000,2031 +SPY,45653,671.59,671.42,672.12,671.35,1794500340000000000,4150 +SPY,16092,671.42,671.18,671.93,670.98,1794500400000000000,1005 +SPY,43722,671.18,671.82,671.93,671.04,1794500460000000000,1900 +SPY,8412,671.82,671.27,671.91,670.70,1794500520000000000,365 +SPY,37962,671.27,669.98,671.69,669.67,1794500580000000000,2233 +SPY,14605,669.98,671.53,671.59,669.64,1794500640000000000,730 +SPY,22964,671.53,672.59,672.65,671.06,1794500700000000000,2551 +SPY,31924,672.59,671.44,673.03,670.86,1794500760000000000,2660 +SPY,33882,671.44,671.46,671.84,671.06,1794500820000000000,3764 +SPY,19682,671.46,670.79,671.78,670.57,1794500880000000000,2186 +SPY,44684,670.79,671.76,672.12,670.69,1794500940000000000,2031 +SPY,29329,671.76,673.39,673.76,671.57,1794501000000000000,1222 +SPY,42342,673.39,674.80,675.09,673.09,1794501060000000000,2490 +SPY,44950,674.80,675.45,675.98,674.24,1794501120000000000,2043 +SPY,24444,675.45,675.74,675.86,675.05,1794501180000000000,977 +SPY,55774,675.74,674.28,676.21,674.18,1794501240000000000,6197 +SPY,30977,674.28,672.72,674.58,672.40,1794501300000000000,1475 +SPY,10935,672.72,672.01,672.74,671.94,1794501360000000000,841 +SPY,28666,672.01,673.33,673.49,671.67,1794501420000000000,2047 +SPY,56979,673.33,671.92,673.84,671.50,1794501480000000000,7122 +SPY,40652,671.92,671.63,672.16,671.40,1794501540000000000,2258 +SPY,27019,671.63,671.32,672.20,670.92,1794501600000000000,3002 +SPY,50990,671.32,669.75,671.76,669.61,1794501660000000000,2317 +SPY,32649,669.75,671.19,671.70,669.72,1794501720000000000,2040 +SPY,51985,671.19,672.25,672.68,671.06,1794501780000000000,2888 +SPY,44491,672.25,673.09,673.36,672.00,1794501840000000000,2471 +SPY,1088,673.09,672.87,673.58,672.76,1794501900000000000,136 +SPY,26183,672.87,671.30,673.22,671.28,1794501960000000000,1190 +SPY,9632,671.30,672.42,672.87,671.23,1794502020000000000,740 +SPY,43205,672.42,673.51,673.84,671.95,1794502080000000000,2057 +SPY,7665,673.51,673.62,673.98,672.98,1794502140000000000,638 +SPY,20026,673.62,673.77,674.04,673.54,1794502200000000000,910 +SPY,49022,673.77,672.31,673.91,671.76,1794502260000000000,2042 +SPY,46566,672.31,673.03,673.26,671.85,1794502320000000000,2739 +SPY,32697,673.03,674.64,675.00,672.74,1794502380000000000,1362 +SPY,6411,674.64,673.42,674.78,673.30,1794502440000000000,291 +SPY,28317,673.42,672.24,673.47,671.95,1794502500000000000,1887 +SPY,17537,672.24,673.01,673.31,671.67,1794502560000000000,835 +SPY,1408,673.01,671.79,673.47,671.25,1794502620000000000,67 +SPY,44476,671.79,672.60,673.11,671.27,1794502680000000000,2779 +SPY,42418,672.60,671.17,672.88,671.13,1794502740000000000,3534 +SPY,2444,671.17,672.30,672.50,671.13,1794502800000000000,305 +SPY,29416,672.30,673.01,673.32,672.11,1794502860000000000,1470 +SPY,9523,673.01,671.88,673.17,671.61,1794502920000000000,634 +SPY,39625,671.88,671.30,672.04,671.17,1794502980000000000,2476 +SPY,5898,671.30,671.86,672.16,670.82,1794503040000000000,536 +SPY,53333,671.86,673.10,673.67,671.48,1794503100000000000,3809 +SPY,56434,673.10,674.65,674.81,672.63,1794503160000000000,4031 +SPY,36371,674.65,674.85,675.17,674.25,1794503220000000000,1515 +SPY,9273,674.85,675.56,675.86,674.72,1794503280000000000,441 +SPY,35792,675.56,675.39,675.96,675.28,1794503340000000000,1883 +SPY,3206,675.39,673.85,675.80,673.39,1794503400000000000,267 +SPY,54493,673.85,672.92,674.30,672.45,1794503460000000000,2724 +SPY,16087,672.92,672.38,673.05,672.21,1794503520000000000,1787 +SPY,867,672.38,672.88,673.44,672.03,1794503580000000000,54 +SPY,9893,672.88,673.99,674.21,672.65,1794503640000000000,1236 +SPY,5600,673.99,674.98,675.36,673.84,1794503700000000000,350 +SPY,58214,674.98,675.27,675.50,674.91,1794503760000000000,2425 +SPY,5133,675.27,674.29,675.70,673.85,1794503820000000000,256 +SPY,37081,674.29,675.66,675.92,673.73,1794503880000000000,1483 +SPY,20335,675.66,676.46,676.71,675.57,1794503940000000000,1270 +SPY,41783,676.46,675.70,676.62,675.48,1794504000000000000,3481 +SPY,35568,675.70,675.82,675.86,675.62,1794504060000000000,1976 +SPY,48066,675.82,677.08,677.57,675.56,1794504120000000000,6008 +SPY,30727,677.08,676.32,677.26,675.76,1794504180000000000,1707 +SPY,51856,676.32,677.29,677.62,676.12,1794504240000000000,3704 +SPY,1621,677.29,678.80,679.04,676.78,1794504300000000000,70 +SPY,5377,678.80,680.18,680.55,678.58,1794504360000000000,413 +SPY,38809,680.18,680.11,680.41,679.75,1794504420000000000,1940 +SPY,44437,680.11,679.50,680.40,679.44,1794504480000000000,4937 +SPY,58111,679.50,678.08,679.96,677.78,1794504540000000000,5811 +SPY,25074,678.08,677.69,678.52,677.42,1794504600000000000,1002 +SPY,54465,677.69,677.38,677.73,677.05,1794504660000000000,3203 +SPY,12795,677.38,677.06,677.90,676.84,1794504720000000000,1279 +SPY,4655,677.06,677.49,677.84,676.65,1794504780000000000,517 +SPY,8274,677.49,678.40,678.89,677.10,1794504840000000000,591 +SPY,43303,678.40,677.99,678.70,677.85,1794504900000000000,3093 +SPY,57696,677.99,678.96,679.43,677.48,1794504960000000000,2508 +SPY,11494,678.96,677.53,679.01,677.22,1794505020000000000,638 +SPY,46013,677.53,677.12,678.05,676.62,1794505080000000000,2875 +SPY,34492,677.12,676.59,677.70,676.38,1794505140000000000,3135 +SPY,23356,676.59,675.05,677.17,674.84,1794505200000000000,1946 +SPY,58084,675.05,674.99,675.19,674.41,1794505260000000000,2525 +SPY,11326,674.99,675.55,675.75,674.51,1794505320000000000,596 +SPY,52820,675.55,674.51,675.78,674.46,1794505380000000000,3107 +SPY,13607,674.51,673.86,674.86,673.58,1794505440000000000,1360 +SPY,28671,673.86,672.38,673.98,672.18,1794505500000000000,2867 +SPY,43178,672.38,671.27,672.86,671.16,1794505560000000000,2398 +SPY,41026,671.27,671.31,671.84,671.00,1794505620000000000,2413 +SPY,47928,671.31,671.62,671.72,671.16,1794505680000000000,3686 +SPY,46327,671.62,671.25,671.81,671.04,1794505740000000000,2725 +SPY,27673,671.25,672.84,673.15,671.22,1794505800000000000,1203 +SPY,4628,672.84,673.65,673.96,672.77,1794505860000000000,330 +SPY,10930,673.65,672.93,674.19,672.51,1794505920000000000,728 +SPY,23826,672.93,672.58,673.37,672.20,1794505980000000000,1588 +SPY,46109,672.58,672.74,672.83,672.00,1794506040000000000,3546 +SPY,48983,672.74,674.34,674.61,672.25,1794506100000000000,5442 +SPY,57968,674.34,675.07,675.38,674.17,1794506160000000000,2318 +SPY,42576,675.07,675.31,675.58,674.95,1794506220000000000,4257 +SPY,54614,675.31,675.13,675.45,674.88,1794506280000000000,6068 +SPY,17036,675.13,675.68,676.21,675.04,1794506340000000000,1064 +SPY,37637,675.68,675.09,675.90,674.50,1794506400000000000,1980 +SPY,43874,675.09,674.76,675.16,674.54,1794506460000000000,3133 +SPY,25746,674.76,675.23,675.27,674.65,1794506520000000000,2860 +SPY,1534,675.23,676.22,676.32,674.85,1794506580000000000,61 +SPY,48616,676.22,677.63,678.12,675.72,1794506640000000000,2209 +SPY,11699,677.63,678.80,678.86,677.23,1794506700000000000,1462 +SPY,29216,678.80,679.32,679.64,678.68,1794506760000000000,1270 +SPY,40767,679.32,680.70,681.05,678.90,1794506820000000000,2717 +SPY,37253,680.70,680.14,681.20,679.91,1794506880000000000,1773 +SPY,26909,680.14,679.05,680.74,678.94,1794506940000000000,2069 +SPY,46323,679.05,679.78,680.09,678.99,1794507000000000000,4632 +SPY,55873,679.78,680.73,681.20,679.62,1794507060000000000,6208 +SPY,26899,680.73,681.04,681.30,680.33,1794507120000000000,1415 +SPY,56152,681.04,679.61,681.22,679.42,1794507180000000000,2807 +SPY,27607,679.61,679.27,679.90,678.77,1794507240000000000,1533 +SPY,8855,679.27,678.65,679.62,678.44,1794507300000000000,385 +SPY,50215,678.65,677.73,678.81,677.39,1794507360000000000,3347 +SPY,880,677.73,676.51,678.27,676.00,1794507420000000000,38 +SPY,58311,676.51,677.84,678.24,676.42,1794507480000000000,6479 +SPY,54823,677.84,677.58,678.19,677.55,1794507540000000000,3224 +SPY,3934,677.58,677.66,677.76,677.15,1794507600000000000,196 +SPY,43794,677.66,678.76,678.87,677.59,1794507660000000000,1904 +SPY,35839,678.76,679.61,679.66,678.18,1794507720000000000,3583 +SPY,49469,679.61,679.20,679.66,678.87,1794507780000000000,5496 +SPY,22169,679.20,680.02,680.08,679.10,1794507840000000000,1007 +SPY,17378,680.02,680.07,680.11,679.82,1794507900000000000,1737 +SPY,29930,680.07,681.57,681.71,679.53,1794507960000000000,1496 +SPY,52772,681.57,682.04,682.38,681.41,1794508020000000000,3104 +SPY,15252,682.04,682.62,683.17,681.90,1794508080000000000,847 +SPY,14989,682.62,682.95,683.02,682.28,1794508140000000000,1153 +SPY,53644,682.95,684.25,684.44,682.83,1794508200000000000,4126 +SPY,53694,684.25,683.44,684.52,682.98,1794508260000000000,2334 +SPY,36691,683.44,684.97,685.21,683.33,1794508320000000000,2293 +SPY,38870,684.97,684.99,685.15,684.84,1794508380000000000,3533 +SPY,5554,684.99,684.56,685.50,684.45,1794508440000000000,462 +SPY,31386,684.56,684.24,685.04,684.11,1794508500000000000,2615 +SPY,33890,684.24,683.40,684.43,683.25,1794508560000000000,2259 +SPY,52366,683.40,681.83,683.82,681.60,1794508620000000000,3740 +SPY,52493,681.83,682.01,682.46,681.27,1794508680000000000,2099 +SPY,45846,682.01,680.69,682.13,680.32,1794508740000000000,2183 +SPY,40771,680.69,680.47,680.72,680.40,1794508800000000000,2548 +SPY,37092,680.47,679.76,680.85,679.35,1794508860000000000,3372 +SPY,51862,679.76,679.88,680.11,679.56,1794508920000000000,3241 +SPY,35444,679.88,681.25,681.54,679.65,1794508980000000000,4430 +SPY,35908,681.25,682.17,682.44,680.69,1794509040000000000,1496 +SPY,26748,682.17,681.54,682.73,681.30,1794509100000000000,1337 +SPY,20940,681.54,682.12,682.39,681.21,1794509160000000000,1903 +SPY,39753,682.12,680.52,682.41,680.23,1794509220000000000,1987 +SPY,10523,680.52,679.06,680.93,678.80,1794509280000000000,956 +SPY,10206,679.06,677.81,679.25,677.37,1794509340000000000,600 +SPY,19034,677.81,677.41,678.10,677.01,1794509400000000000,865 +SPY,17092,677.41,678.64,679.06,677.32,1794509460000000000,1424 +SPY,12511,678.64,678.64,678.67,678.44,1794509520000000000,543 +SPY,1022,678.64,679.64,679.89,678.34,1794509580000000000,53 +SPY,16259,679.64,678.44,679.72,678.10,1794509640000000000,706 +SPY,50472,678.44,676.86,678.60,676.75,1794509700000000000,3882 +SPY,46425,676.86,677.50,677.66,676.40,1794509760000000000,3571 +SPY,11273,677.50,677.56,677.65,677.10,1794509820000000000,704 +SPY,31990,677.56,676.32,677.77,676.07,1794509880000000000,1599 +SPY,36148,676.32,676.37,676.87,676.30,1794509940000000000,1571 +SPY,42604,676.37,676.35,676.68,676.03,1794510000000000000,1775 +SPY,19110,676.35,677.41,677.55,676.30,1794510060000000000,1911 +SPY,48005,677.41,677.37,677.63,677.08,1794510120000000000,2400 +SPY,4262,677.37,678.08,678.38,676.86,1794510180000000000,327 +SPY,37656,678.08,678.02,678.57,677.52,1794510240000000000,1637 +SPY,24563,678.02,678.58,678.74,677.85,1794510300000000000,2729 +SPY,16618,678.58,680.09,680.55,678.32,1794510360000000000,755 +SPY,31415,680.09,679.44,680.20,679.15,1794510420000000000,2416 +SPY,16875,679.44,678.43,679.71,678.20,1794510480000000000,1534 +SPY,49202,678.43,678.72,679.20,678.29,1794510540000000000,6150 +SPY,17446,678.72,680.04,680.44,678.44,1794510600000000000,1586 +SPY,33770,680.04,680.56,680.83,679.89,1794510660000000000,2110 +SPY,51492,680.56,679.84,681.11,679.36,1794510720000000000,3028 +SPY,25293,679.84,678.84,680.40,678.70,1794510780000000000,1099 +SPY,20211,678.84,680.15,680.28,678.42,1794510840000000000,2245 +SPY,43658,680.15,680.18,680.64,679.72,1794510900000000000,1898 +SPY,59677,680.18,681.20,681.58,679.96,1794510960000000000,3315 +SPY,19357,681.20,680.95,681.48,680.67,1794511020000000000,841 +SPY,43693,680.95,680.85,681.14,680.75,1794511080000000000,4369 +SPY,28222,680.85,681.30,681.37,680.35,1794511140000000000,1567 +SPY,19848,681.30,681.19,681.35,680.95,1794511200000000000,1102 +SPY,13240,681.19,680.07,681.34,679.54,1794511260000000000,1203 +SPY,48296,680.07,679.58,680.46,679.51,1794511320000000000,4024 +SPY,46125,679.58,678.73,679.94,678.24,1794511380000000000,1921 +SPY,35044,678.73,680.12,680.44,678.57,1794511440000000000,2190 +SPY,2928,680.12,679.07,680.58,678.58,1794511500000000000,154 +SPY,47018,679.07,678.19,679.53,677.80,1794511560000000000,3918 +SPY,18381,678.19,677.87,678.73,677.44,1794511620000000000,875 +SPY,43419,677.87,676.69,678.42,676.29,1794511680000000000,3101 +SPY,18633,676.69,675.59,677.11,675.11,1794511740000000000,1552 +SPY,18784,675.59,676.22,676.80,675.46,1794511800000000000,988 +SPY,26079,676.22,674.92,676.30,674.61,1794511860000000000,1241 +SPY,3470,674.92,673.66,675.25,673.32,1794511920000000000,182 +SPY,16391,673.66,673.92,674.30,673.62,1794511980000000000,712 +SPY,34292,673.92,672.62,674.48,672.21,1794512040000000000,3117 +SPY,14553,672.62,671.55,673.17,671.29,1794512100000000000,1212 +SPY,38480,671.55,669.96,671.99,669.71,1794512160000000000,4810 +SPY,55237,669.96,669.89,670.16,669.84,1794512220000000000,2907 +SPY,25330,669.89,669.80,670.15,669.34,1794512280000000000,3166 +SPY,52502,669.80,671.20,671.26,669.75,1794512340000000000,3500 +SPY,56045,671.20,671.85,672.17,670.80,1794512400000000000,2547 +SPY,43381,671.85,672.49,672.71,671.53,1794512460000000000,2892 +SPY,5898,672.49,673.35,673.91,672.03,1794512520000000000,368 +SPY,48207,673.35,672.85,673.53,672.72,1794512580000000000,2835 +SPY,52167,672.85,672.48,673.37,672.15,1794512640000000000,2898 +SPY,26398,672.48,673.73,673.78,672.11,1794512700000000000,1199 +SPY,5894,673.73,673.88,674.38,673.50,1794512760000000000,654 +SPY,44283,673.88,674.50,674.90,673.56,1794512820000000000,2952 +SPY,6429,674.50,675.98,676.50,674.17,1794512880000000000,642 +SPY,28041,675.98,675.84,676.23,675.33,1794512940000000000,1168 +SPY,47643,675.84,674.86,676.04,674.36,1794513000000000000,3403 +SPY,47833,674.86,675.04,675.45,674.33,1794513060000000000,2657 +SPY,16779,675.04,675.20,675.77,674.48,1794513120000000000,987 +SPY,48770,675.20,676.30,676.80,674.74,1794513180000000000,2438 +SPY,27709,676.30,675.08,676.78,674.77,1794513240000000000,2131 +SPY,54420,675.08,675.36,675.85,674.88,1794513300000000000,3023 +SPY,51543,675.36,674.49,675.81,674.20,1794513360000000000,2454 +SPY,51799,674.49,674.11,674.93,674.00,1794513420000000000,3237 +SPY,42637,674.11,675.60,675.83,673.57,1794513480000000000,2664 +SPY,40121,675.60,675.53,676.07,675.36,1794513540000000000,2228 +SPY,59114,675.53,675.00,675.75,674.61,1794513600000000000,2687 +SPY,28302,675.00,676.23,676.72,674.70,1794513660000000000,2358 +SPY,37852,676.23,675.14,676.47,674.61,1794513720000000000,2102 +SPY,21563,675.14,676.51,676.92,674.77,1794513780000000000,1796 +SPY,17049,676.51,675.33,676.63,674.79,1794513840000000000,2131 +SPY,55452,675.33,674.71,675.40,674.17,1794513900000000000,2310 +SPY,53761,674.71,675.96,676.10,674.13,1794513960000000000,2337 +SPY,22699,675.96,674.45,676.12,673.94,1794514020000000000,1746 +SPY,47973,674.45,673.71,674.51,673.27,1794514080000000000,2821 +SPY,58630,673.71,673.05,673.90,672.62,1794514140000000000,3908 +SPY,9382,673.05,673.97,674.22,672.64,1794514200000000000,469 +SPY,36106,673.97,674.07,674.49,673.82,1794514260000000000,2123 +SPY,31034,674.07,672.94,674.57,672.69,1794514320000000000,1349 +SPY,8888,672.94,672.81,673.01,672.79,1794514380000000000,987 +SPY,33255,672.81,672.26,673.38,671.87,1794514440000000000,1662 +SPY,48649,672.26,672.70,673.18,672.08,1794514500000000000,2211 +SPY,55051,672.70,671.79,673.15,671.39,1794514560000000000,3440 +SPY,55769,671.79,671.56,672.26,671.15,1794514620000000000,2935 +SPY,29254,671.56,670.75,671.62,670.68,1794514680000000000,2089 +SPY,10167,670.75,670.50,671.29,670.48,1794514740000000000,726 +SPY,32007,670.50,669.14,670.87,668.59,1794514800000000000,2286 +SPY,7115,669.14,670.27,670.77,669.03,1794514860000000000,374 +SPY,21904,670.27,670.22,670.74,669.78,1794514920000000000,876 +SPY,24891,670.22,669.52,670.26,669.39,1794514980000000000,1244 +SPY,56280,669.52,670.16,670.34,669.49,1794515040000000000,5116 +SPY,49248,670.16,670.86,671.40,669.87,1794515100000000000,2052 +SPY,44986,670.86,672.01,672.11,670.43,1794515160000000000,2142 +SPY,17130,672.01,670.92,672.23,670.51,1794515220000000000,901 +SPY,40883,670.92,670.75,671.52,670.52,1794515280000000000,1635 +SPY,30417,670.75,671.02,671.22,670.48,1794515340000000000,2172 +SPY,9234,671.02,670.66,671.10,670.30,1794515400000000000,659 +SPY,19101,670.66,670.90,671.34,670.15,1794515460000000000,1364 +SPY,11337,670.90,672.25,672.55,670.49,1794515520000000000,755 +SPY,17309,672.25,673.28,673.75,671.68,1794515580000000000,911 +SPY,42347,673.28,672.40,673.83,672.35,1794515640000000000,3528 +SPY,26465,672.40,670.82,672.92,670.76,1794515700000000000,1260 +SPY,56648,670.82,671.20,671.24,670.56,1794515760000000000,4357 +SPY,15283,671.20,670.55,671.39,669.97,1794515820000000000,804 +SPY,8389,670.55,670.58,670.72,670.47,1794515880000000000,559 +SPY,14082,670.58,671.50,671.62,670.07,1794515940000000000,1760 +SPY,52341,671.50,672.64,672.80,671.41,1794516000000000000,2617 +SPY,40096,672.64,673.84,674.23,672.13,1794516060000000000,2864 +SPY,16968,673.84,674.38,674.83,673.48,1794516120000000000,808 +SPY,27178,674.38,672.81,674.60,672.23,1794516180000000000,1509 +SPY,50335,672.81,672.87,672.99,672.57,1794516240000000000,2396 +SPY,54000,672.87,674.14,674.33,672.82,1794516300000000000,4500 +SPY,23748,674.14,674.58,675.12,674.08,1794516360000000000,1583 +SPY,37412,674.58,675.69,675.96,674.11,1794516420000000000,2200 +SPY,28124,675.69,675.67,676.18,675.61,1794516480000000000,1654 +SPY,20293,675.67,674.68,675.85,674.52,1794516540000000000,2029 +SPY,52084,674.68,674.08,675.14,673.52,1794516600000000000,2893 +SPY,47932,674.08,672.86,674.51,672.35,1794516660000000000,5325 +SPY,20050,672.86,671.68,673.22,671.55,1794516720000000000,1002 +SPY,32257,671.68,671.62,672.06,671.34,1794516780000000000,3584 +SPY,27329,671.62,671.90,672.04,671.36,1794516840000000000,2102 +SPY,10593,671.90,670.35,672.03,670.10,1794516900000000000,662 +SPY,12705,670.35,671.19,671.33,669.77,1794516960000000000,605 +SPY,31309,671.19,669.94,671.38,669.88,1794517020000000000,1252 +SPY,41472,669.94,668.78,670.54,668.43,1794517080000000000,1658 +SPY,28286,668.78,668.88,668.94,668.70,1794517140000000000,1285 +SPY,52804,668.88,669.98,670.25,668.67,1794580200000000000,3106 +SPY,32189,669.98,671.39,671.93,669.59,1794580260000000000,1609 +SPY,47975,671.39,671.04,671.91,670.67,1794580320000000000,2180 +SPY,50714,671.04,671.69,671.84,670.90,1794580380000000000,2113 +SPY,5391,671.69,672.28,672.51,671.39,1794580440000000000,256 +SPY,38125,672.28,670.87,672.58,670.51,1794580500000000000,2242 +SPY,12480,670.87,670.84,671.09,670.33,1794580560000000000,1386 +SPY,7456,670.84,672.46,672.48,670.59,1794580620000000000,372 +SPY,20814,672.46,672.84,673.37,671.89,1794580680000000000,1601 +SPY,57852,672.84,672.68,673.00,672.61,1794580740000000000,5259 +SPY,31742,672.68,672.21,673.02,671.99,1794580800000000000,1322 +SPY,27144,672.21,672.21,672.61,672.03,1794580860000000000,3393 +SPY,31829,672.21,673.54,674.04,671.85,1794580920000000000,1768 +SPY,14473,673.54,674.96,675.15,673.01,1794580980000000000,1206 +SPY,6390,674.96,673.94,675.35,673.83,1794581040000000000,399 +SPY,12304,673.94,674.02,674.46,673.92,1794581100000000000,615 +SPY,46584,674.02,672.97,674.08,672.80,1794581160000000000,2588 +SPY,15497,672.97,673.41,673.63,672.90,1794581220000000000,1106 +SPY,52149,673.41,674.80,675.38,673.03,1794581280000000000,2370 +SPY,41278,674.80,676.29,676.78,674.41,1794581340000000000,4127 +SPY,21209,676.29,675.02,676.57,674.80,1794581400000000000,1247 +SPY,54318,675.02,675.30,675.46,674.42,1794581460000000000,2361 +SPY,35627,675.30,674.52,675.33,674.22,1794581520000000000,1425 +SPY,13915,674.52,675.62,675.78,674.23,1794581580000000000,556 +SPY,35712,675.62,676.89,677.46,675.04,1794581640000000000,3968 +SPY,21923,676.89,676.65,676.99,676.29,1794581700000000000,2740 +SPY,37231,676.65,677.22,677.25,676.10,1794581760000000000,4653 +SPY,20190,677.22,677.33,677.90,677.02,1794581820000000000,841 +SPY,20042,677.33,676.17,677.35,676.15,1794581880000000000,2226 +SPY,31933,676.17,677.75,678.05,676.06,1794581940000000000,2661 +SPY,7594,677.75,678.17,678.75,677.34,1794582000000000000,316 +SPY,56049,678.17,679.66,680.25,677.75,1794582060000000000,4670 +SPY,54598,679.66,680.57,680.73,679.11,1794582120000000000,3211 +SPY,5764,680.57,680.26,680.87,679.85,1794582180000000000,640 +SPY,59812,680.26,680.00,680.41,679.79,1794582240000000000,2600 +SPY,13522,680.00,679.71,680.34,679.37,1794582300000000000,1040 +SPY,6186,679.71,680.09,680.35,679.44,1794582360000000000,281 +SPY,14499,680.09,678.91,680.50,678.87,1794582420000000000,966 +SPY,58349,678.91,679.51,679.87,678.52,1794582480000000000,3646 +SPY,59880,679.51,679.43,679.70,679.34,1794582540000000000,3992 +SPY,49181,679.43,680.19,680.38,679.19,1794582600000000000,4098 +SPY,17537,680.19,678.83,680.39,678.58,1794582660000000000,923 +SPY,2212,678.83,680.47,680.60,678.58,1794582720000000000,96 +SPY,3794,680.47,680.03,680.54,679.78,1794582780000000000,344 +SPY,26824,680.03,679.32,680.39,679.18,1794582840000000000,1219 +SPY,57192,679.32,679.38,679.60,678.82,1794582900000000000,4085 +SPY,4386,679.38,678.12,679.82,677.52,1794582960000000000,230 +SPY,52262,678.12,678.88,679.45,677.97,1794583020000000000,2272 +SPY,58708,678.88,678.19,679.36,678.09,1794583080000000000,3913 +SPY,26152,678.19,679.01,679.23,677.89,1794583140000000000,1245 +SPY,20913,679.01,678.87,679.27,678.66,1794583200000000000,1742 +SPY,1858,678.87,678.65,679.08,678.33,1794583260000000000,80 +SPY,6586,678.65,679.31,679.80,678.13,1794583320000000000,411 +SPY,45121,679.31,679.28,679.84,679.02,1794583380000000000,2148 +SPY,2549,679.28,680.76,681.25,678.72,1794583440000000000,254 +SPY,10566,680.76,679.51,680.84,679.06,1794583500000000000,1174 +SPY,29426,679.51,678.50,679.90,677.98,1794583560000000000,1471 +SPY,32031,678.50,679.78,680.34,678.06,1794583620000000000,1281 +SPY,19454,679.78,680.99,681.07,679.32,1794583680000000000,1023 +SPY,7115,680.99,682.58,683.04,680.61,1794583740000000000,395 +SPY,41224,682.58,683.68,684.20,682.47,1794583800000000000,2424 +SPY,45087,683.68,682.31,684.25,682.16,1794583860000000000,3005 +SPY,26552,682.31,681.05,682.55,680.58,1794583920000000000,2950 +SPY,53027,681.05,679.55,681.64,679.26,1794583980000000000,2525 +SPY,982,679.55,680.76,681.24,679.07,1794584040000000000,51 +SPY,19668,680.76,679.68,680.86,679.22,1794584100000000000,786 +SPY,12942,679.68,680.61,681.08,679.58,1794584160000000000,924 +SPY,42807,680.61,679.29,681.15,678.92,1794584220000000000,2853 +SPY,44814,679.29,678.83,679.45,678.46,1794584280000000000,3734 +SPY,6014,678.83,678.46,678.89,678.28,1794584340000000000,400 +SPY,20569,678.46,677.27,679.01,677.22,1794584400000000000,1582 +SPY,59936,677.27,677.53,677.93,677.14,1794584460000000000,3329 +SPY,53541,677.53,677.63,678.07,677.09,1794584520000000000,2549 +SPY,26163,677.63,677.53,677.91,677.13,1794584580000000000,2907 +SPY,33213,677.53,676.72,678.11,676.33,1794584640000000000,2214 +SPY,1248,676.72,675.48,676.76,675.31,1794584700000000000,54 +SPY,8166,675.48,675.22,675.79,674.90,1794584760000000000,340 +SPY,33272,675.22,676.25,676.54,674.80,1794584820000000000,1584 +SPY,55208,676.25,676.71,676.79,676.03,1794584880000000000,3450 +SPY,56521,676.71,678.25,678.28,676.22,1794584940000000000,2260 +SPY,40604,678.25,678.58,679.11,677.83,1794585000000000000,3383 +SPY,46536,678.58,677.65,679.14,677.38,1794585060000000000,3579 +SPY,55300,677.65,678.35,678.62,677.26,1794585120000000000,3456 +SPY,13213,678.35,677.36,678.52,677.02,1794585180000000000,1651 +SPY,51491,677.36,676.91,677.83,676.54,1794585240000000000,2710 +SPY,52041,676.91,676.54,677.51,676.31,1794585300000000000,2081 +SPY,4826,676.54,677.73,677.81,676.17,1794585360000000000,536 +SPY,24881,677.73,676.15,678.03,675.56,1794585420000000000,1081 +SPY,43629,676.15,677.28,677.82,675.85,1794585480000000000,2908 +SPY,30701,677.28,677.50,677.70,677.17,1794585540000000000,1535 +SPY,50502,677.50,678.43,678.62,677.30,1794585600000000000,6312 +SPY,3195,678.43,677.84,678.54,677.77,1794585660000000000,152 +SPY,7694,677.84,678.09,678.64,677.49,1794585720000000000,961 +SPY,2884,678.09,679.26,679.33,678.02,1794585780000000000,169 +SPY,45037,679.26,680.09,680.57,679.00,1794585840000000000,2370 +SPY,50268,680.09,680.42,680.50,680.06,1794585900000000000,5026 +SPY,35711,680.42,680.89,681.18,680.16,1794585960000000000,2550 +SPY,10626,680.89,682.21,682.76,680.61,1794586020000000000,1328 +SPY,4957,682.21,682.62,682.93,681.78,1794586080000000000,291 +SPY,24314,682.62,681.47,682.78,680.93,1794586140000000000,1279 +SPY,51110,681.47,683.04,683.15,680.96,1794586200000000000,2690 +SPY,48667,683.04,683.59,683.74,682.69,1794586260000000000,3476 +SPY,7197,683.59,685.21,685.51,683.27,1794586320000000000,654 +SPY,17136,685.21,685.50,685.82,685.18,1794586380000000000,1428 +SPY,32369,685.50,685.65,685.92,685.12,1794586440000000000,2942 +SPY,44205,685.65,685.24,685.80,684.83,1794586500000000000,2210 +SPY,35934,685.24,686.32,686.39,684.72,1794586560000000000,1891 +SPY,32256,686.32,686.65,687.10,686.18,1794586620000000000,1792 +SPY,28378,686.65,685.64,687.12,685.49,1794586680000000000,1289 +SPY,51463,685.64,685.66,686.24,685.22,1794586740000000000,5718 +SPY,49700,685.66,684.71,685.76,684.52,1794586800000000000,2160 +SPY,45592,684.71,684.55,684.84,684.34,1794586860000000000,2279 +SPY,47054,684.55,684.42,684.68,683.95,1794586920000000000,2240 +SPY,52860,684.42,684.88,685.45,683.87,1794586980000000000,4405 +SPY,21908,684.88,684.78,685.31,684.28,1794587040000000000,2190 +SPY,12574,684.78,684.50,685.37,684.20,1794587100000000000,898 +SPY,29471,684.50,685.83,686.32,683.90,1794587160000000000,1551 +SPY,33642,685.83,686.90,687.49,685.58,1794587220000000000,2587 +SPY,46193,686.90,685.58,687.02,685.51,1794587280000000000,2099 +SPY,42821,685.58,684.61,685.96,684.48,1794587340000000000,2378 +SPY,55211,684.61,684.18,685.11,683.92,1794587400000000000,6134 +SPY,39412,684.18,684.83,685.19,683.87,1794587460000000000,3284 +SPY,14503,684.83,684.80,684.98,684.57,1794587520000000000,630 +SPY,51411,684.80,685.22,685.28,684.47,1794587580000000000,2142 +SPY,21966,685.22,686.79,687.10,685.11,1794587640000000000,1689 +SPY,29231,686.79,687.60,687.77,686.68,1794587700000000000,1461 +SPY,46651,687.60,686.76,688.19,686.46,1794587760000000000,4665 +SPY,47028,686.76,688.07,688.17,686.39,1794587820000000000,3617 +SPY,29432,688.07,687.67,688.28,687.48,1794587880000000000,1226 +SPY,14717,687.67,687.29,687.71,686.79,1794587940000000000,865 +SPY,6625,687.29,688.74,688.87,686.86,1794588000000000000,662 +SPY,2541,688.74,689.00,689.17,688.44,1794588060000000000,115 +SPY,23481,689.00,687.69,689.04,687.14,1794588120000000000,939 +SPY,17398,687.69,686.82,688.22,686.47,1794588180000000000,1933 +SPY,34155,686.82,686.43,687.10,686.25,1794588240000000000,1552 +SPY,35707,686.43,686.98,687.30,686.34,1794588300000000000,2550 +SPY,4717,686.98,687.13,687.60,686.52,1794588360000000000,205 +SPY,46784,687.13,687.93,688.30,686.92,1794588420000000000,2924 +SPY,19247,687.93,688.33,688.73,687.65,1794588480000000000,1603 +SPY,19088,688.33,689.53,689.84,688.08,1794588540000000000,1193 +SPY,56991,689.53,688.28,689.94,688.22,1794588600000000000,7123 +SPY,50345,688.28,688.65,689.20,687.76,1794588660000000000,3146 +SPY,22526,688.65,688.95,689.05,688.54,1794588720000000000,1251 +SPY,50624,688.95,689.14,689.46,688.58,1794588780000000000,2109 +SPY,26161,689.14,687.90,689.38,687.51,1794588840000000000,1137 +SPY,40116,687.90,687.29,688.22,687.20,1794588900000000000,2005 +SPY,14535,687.29,687.48,687.63,686.88,1794588960000000000,631 +SPY,19876,687.48,686.09,687.51,685.63,1794589020000000000,1987 +SPY,34278,686.09,685.92,686.15,685.77,1794589080000000000,1804 +SPY,32362,685.92,684.48,685.97,684.12,1794589140000000000,1797 +SPY,47677,684.48,684.00,684.59,683.54,1794589200000000000,2509 +SPY,35730,684.00,682.96,684.24,682.80,1794589260000000000,4466 +SPY,847,682.96,682.77,683.26,682.45,1794589320000000000,40 +SPY,30841,682.77,682.41,683.06,682.24,1794589380000000000,2202 +SPY,39988,682.41,682.96,683.39,682.19,1794589440000000000,3635 +SPY,50590,682.96,681.86,683.09,681.57,1794589500000000000,2199 +SPY,51383,681.86,681.98,682.50,681.67,1794589560000000000,5138 +SPY,14026,681.98,681.80,682.49,681.44,1794589620000000000,561 +SPY,18277,681.80,683.41,683.53,681.44,1794589680000000000,731 +SPY,40384,683.41,683.56,683.67,683.30,1794589740000000000,2125 +SPY,8676,683.56,683.18,683.66,683.16,1794589800000000000,377 +SPY,43388,683.18,681.80,683.40,681.31,1794589860000000000,1886 +SPY,29748,681.80,680.54,681.91,680.40,1794589920000000000,1749 +SPY,15761,680.54,680.97,681.53,680.34,1794589980000000000,875 +SPY,10553,680.97,679.39,681.21,679.17,1794590040000000000,439 +SPY,3967,679.39,677.92,679.47,677.76,1794590100000000000,172 +SPY,40050,677.92,677.00,678.21,676.43,1794590160000000000,1907 +SPY,13555,677.00,677.05,677.29,676.89,1794590220000000000,1355 +SPY,17179,677.05,676.94,677.56,676.86,1794590280000000000,715 +SPY,21331,676.94,675.76,677.15,675.67,1794590340000000000,1185 +SPY,15794,675.76,677.24,677.63,675.69,1794590400000000000,631 +SPY,15473,677.24,677.35,677.90,677.10,1794590460000000000,1105 +SPY,10812,677.35,676.37,677.42,675.96,1794590520000000000,636 +SPY,24992,676.37,677.13,677.22,676.15,1794590580000000000,1470 +SPY,31866,677.13,677.99,678.33,676.62,1794590640000000000,3983 +SPY,18588,677.99,677.75,678.26,677.41,1794590700000000000,978 +SPY,49283,677.75,677.05,678.23,676.74,1794590760000000000,3520 +SPY,16844,677.05,677.25,677.50,676.62,1794590820000000000,673 +SPY,5998,677.25,678.78,678.95,676.78,1794590880000000000,599 +SPY,15230,678.78,677.26,679.30,677.18,1794590940000000000,761 +SPY,25780,677.26,675.96,677.51,675.91,1794591000000000000,1432 +SPY,15603,675.96,674.47,676.27,674.34,1794591060000000000,917 +SPY,36800,674.47,673.79,674.87,673.28,1794591120000000000,4600 +SPY,36756,673.79,672.20,673.86,671.94,1794591180000000000,1670 +SPY,36852,672.20,672.99,673.38,672.11,1794591240000000000,2303 +SPY,47627,672.99,672.67,673.41,672.12,1794591300000000000,2506 +SPY,37686,672.67,672.10,673.07,671.66,1794591360000000000,3768 +SPY,43626,672.10,672.54,672.96,671.70,1794591420000000000,3635 +SPY,25473,672.54,672.16,673.10,671.88,1794591480000000000,2830 +SPY,43206,672.16,673.37,673.91,671.94,1794591540000000000,2400 +SPY,22485,673.37,673.01,673.72,672.88,1794591600000000000,2044 +SPY,33447,673.01,672.96,673.31,672.74,1794591660000000000,1393 +SPY,13987,672.96,672.82,673.00,672.49,1794591720000000000,608 +SPY,36057,672.82,673.31,673.78,672.47,1794591780000000000,1717 +SPY,35846,673.31,673.11,673.39,672.70,1794591840000000000,1558 +SPY,20032,673.11,674.02,674.06,672.96,1794591900000000000,870 +SPY,42406,674.02,674.86,675.13,673.74,1794591960000000000,2231 +SPY,36507,674.86,675.29,675.71,674.82,1794592020000000000,1521 +SPY,52043,675.29,675.41,675.98,674.86,1794592080000000000,2168 +SPY,59793,675.41,674.77,675.80,674.66,1794592140000000000,6643 +SPY,46936,674.77,674.55,674.98,673.97,1794592200000000000,3352 +SPY,40911,674.55,673.29,674.61,672.70,1794592260000000000,3409 +SPY,5304,673.29,671.71,673.51,671.26,1794592320000000000,353 +SPY,15511,671.71,672.61,672.95,671.37,1794592380000000000,912 +SPY,1308,672.61,671.53,672.79,671.10,1794592440000000000,54 +SPY,48252,671.53,672.13,672.52,671.30,1794592500000000000,3015 +SPY,22527,672.13,673.40,673.55,671.74,1794592560000000000,1023 +SPY,38791,673.40,672.03,673.91,671.54,1794592620000000000,2155 +SPY,9478,672.03,671.74,672.20,671.50,1794592680000000000,677 +SPY,6323,671.74,672.06,672.11,671.15,1794592740000000000,252 +SPY,4226,672.06,672.80,673.35,671.76,1794592800000000000,176 +SPY,59425,672.80,671.59,672.86,671.29,1794592860000000000,4244 +SPY,2704,671.59,670.64,671.67,670.41,1794592920000000000,108 +SPY,2443,670.64,670.45,671.18,670.04,1794592980000000000,122 +SPY,8591,670.45,669.29,670.90,668.86,1794593040000000000,572 +SPY,44831,669.29,668.12,669.82,667.85,1794593100000000000,2637 +SPY,7952,668.12,668.28,668.82,667.95,1794593160000000000,568 +SPY,46311,668.28,668.39,668.79,668.02,1794593220000000000,2105 +SPY,7365,668.39,669.37,669.83,668.27,1794593280000000000,433 +SPY,12681,669.37,670.43,670.80,668.97,1794593340000000000,603 +SPY,17136,670.43,672.05,672.21,669.87,1794593400000000000,816 +SPY,23594,672.05,672.73,673.19,671.70,1794593460000000000,2359 +SPY,54580,672.73,673.35,673.78,672.13,1794593520000000000,2274 +SPY,18663,673.35,674.34,674.66,672.84,1794593580000000000,1866 +SPY,20757,674.34,674.73,675.11,674.03,1794593640000000000,1596 +SPY,54424,674.73,676.02,676.09,674.60,1794593700000000000,2366 +SPY,32089,676.02,677.25,677.67,675.86,1794593760000000000,1604 +SPY,58650,677.25,677.30,677.46,677.13,1794593820000000000,3665 +SPY,52697,677.30,676.63,677.51,676.58,1794593880000000000,2291 +SPY,13570,676.63,677.82,678.33,676.27,1794593940000000000,848 +SPY,6251,677.82,678.49,678.71,677.68,1794594000000000000,329 +SPY,3727,678.49,677.70,679.05,677.38,1794594060000000000,196 +SPY,10291,677.70,677.08,678.15,676.87,1794594120000000000,541 +SPY,15284,677.08,675.75,677.35,675.32,1794594180000000000,664 +SPY,57233,675.75,677.39,677.62,675.52,1794594240000000000,3179 +SPY,6593,677.39,678.87,679.37,676.83,1794594300000000000,439 +SPY,57086,678.87,677.81,679.06,677.52,1794594360000000000,2854 +SPY,21902,677.81,677.00,678.11,676.85,1794594420000000000,1216 +SPY,34306,677.00,678.23,678.62,676.42,1794594480000000000,2450 +SPY,3606,678.23,679.75,680.13,678.11,1794594540000000000,171 +SPY,14002,679.75,678.84,680.03,678.60,1794594600000000000,1000 +SPY,30640,678.84,679.57,679.93,678.80,1794594660000000000,2785 +SPY,38646,679.57,679.03,680.10,678.74,1794594720000000000,1756 +SPY,57921,679.03,677.96,679.27,677.81,1794594780000000000,3217 +SPY,47474,677.96,678.99,679.49,677.76,1794594840000000000,3651 +SPY,36472,678.99,678.40,679.15,677.86,1794594900000000000,1823 +SPY,41061,678.40,678.67,678.83,678.17,1794594960000000000,4562 +SPY,11593,678.67,677.30,678.83,676.75,1794595020000000000,644 +SPY,38431,677.30,677.89,678.25,676.73,1794595080000000000,4270 +SPY,39537,677.89,678.42,678.85,677.85,1794595140000000000,1882 +SPY,27142,678.42,677.37,678.83,677.34,1794595200000000000,2087 +SPY,23503,677.37,678.50,678.68,677.31,1794595260000000000,1068 +SPY,45836,678.50,678.67,679.02,678.05,1794595320000000000,3819 +SPY,20939,678.67,677.98,679.21,677.60,1794595380000000000,997 +SPY,7858,677.98,678.55,678.78,677.91,1794595440000000000,604 +SPY,12449,678.55,679.71,680.20,678.45,1794595500000000000,497 +SPY,17132,679.71,679.89,680.42,679.24,1794595560000000000,713 +SPY,15039,679.89,679.78,680.38,679.47,1794595620000000000,653 +SPY,1848,679.78,681.07,681.32,679.55,1794595680000000000,123 +SPY,13234,681.07,680.77,681.27,680.17,1794595740000000000,882 +SPY,14044,680.77,681.96,682.31,680.67,1794595800000000000,1170 +SPY,50445,681.96,682.37,682.52,681.82,1794595860000000000,5044 +SPY,49101,682.37,681.17,682.58,680.97,1794595920000000000,3068 +SPY,46846,681.17,681.22,681.77,681.07,1794595980000000000,3123 +SPY,58231,681.22,680.70,681.75,680.13,1794596040000000000,7278 +SPY,23527,680.70,679.76,680.74,679.52,1794596100000000000,1307 +SPY,2471,679.76,678.96,679.85,678.83,1794596160000000000,154 +SPY,33678,678.96,680.32,680.56,678.83,1794596220000000000,1530 +SPY,52646,680.32,681.43,681.87,679.90,1794596280000000000,2105 +SPY,50753,681.43,682.61,683.20,681.20,1794596340000000000,2671 +SPY,7507,682.61,681.32,682.75,681.24,1794596400000000000,441 +SPY,8114,681.32,681.45,681.75,681.03,1794596460000000000,338 +SPY,52101,681.45,680.70,681.52,680.59,1794596520000000000,2605 +SPY,56475,680.70,681.29,681.77,680.28,1794596580000000000,2567 +SPY,45127,681.29,680.91,681.37,680.34,1794596640000000000,2051 +SPY,13933,680.91,680.22,681.33,679.83,1794596700000000000,633 +SPY,48972,680.22,681.66,682.19,679.82,1794596760000000000,2129 +SPY,7840,681.66,680.49,681.75,680.13,1794596820000000000,373 +SPY,5690,680.49,679.29,681.08,679.18,1794596880000000000,284 +SPY,45127,679.29,680.16,680.24,679.03,1794596940000000000,2507 +SPY,1117,680.16,680.86,681.12,679.74,1794597000000000000,62 +SPY,32120,680.86,681.73,682.12,680.84,1794597060000000000,3212 +SPY,1992,681.73,680.58,681.77,680.46,1794597120000000000,166 +SPY,8690,680.58,679.85,680.85,679.57,1794597180000000000,413 +SPY,5785,679.85,679.49,680.10,679.21,1794597240000000000,241 +SPY,38303,679.49,681.00,681.52,679.31,1794597300000000000,3830 +SPY,50240,681.00,679.61,681.08,679.41,1794597360000000000,4567 +SPY,52714,679.61,681.19,681.30,679.50,1794597420000000000,3765 +SPY,2718,681.19,682.03,682.62,680.84,1794597480000000000,302 +SPY,25704,682.03,682.90,683.32,681.49,1794597540000000000,2856 +SPY,27942,682.90,683.88,684.25,682.77,1794597600000000000,2794 +SPY,29198,683.88,683.61,684.14,683.52,1794597660000000000,2433 +SPY,57750,683.61,683.31,683.95,683.29,1794597720000000000,6416 +SPY,21116,683.31,684.59,684.80,683.24,1794597780000000000,1173 +SPY,35452,684.59,684.61,684.87,684.29,1794597840000000000,1772 +SPY,23296,684.61,685.59,685.93,684.56,1794597900000000000,970 +SPY,34030,685.59,684.53,685.99,684.22,1794597960000000000,3093 +SPY,4855,684.53,686.13,686.25,684.44,1794598020000000000,404 +SPY,40403,686.13,685.54,686.58,685.01,1794598080000000000,1756 +SPY,3340,685.54,686.26,686.71,684.97,1794598140000000000,145 +SPY,12769,686.26,686.24,686.28,686.18,1794598200000000000,709 +SPY,4754,686.24,686.71,687.27,686.14,1794598260000000000,528 +SPY,3019,686.71,685.92,687.00,685.37,1794598320000000000,301 +SPY,50575,685.92,687.21,687.42,685.66,1794598380000000000,2298 +SPY,4227,687.21,687.34,687.88,686.78,1794598440000000000,422 +SPY,42146,687.34,687.10,687.82,687.04,1794598500000000000,1685 +SPY,23130,687.10,688.55,688.91,686.95,1794598560000000000,2891 +SPY,12123,688.55,689.07,689.31,688.25,1794598620000000000,673 +SPY,54589,689.07,690.52,690.86,688.83,1794598680000000000,2599 +SPY,1081,690.52,691.76,692.22,690.42,1794598740000000000,47 +SPY,31786,691.76,690.62,692.17,690.41,1794598800000000000,3531 +SPY,7534,690.62,690.62,690.88,690.49,1794598860000000000,327 +SPY,47443,690.62,690.28,691.10,689.80,1794598920000000000,3388 +SPY,51968,690.28,690.81,690.91,690.11,1794598980000000000,2362 +SPY,27942,690.81,692.39,692.44,690.35,1794599040000000000,2540 +SPY,11011,692.39,690.83,692.72,690.48,1794599100000000000,524 +SPY,43055,690.83,689.72,691.31,689.43,1794599160000000000,2266 +SPY,30480,689.72,691.15,691.36,689.37,1794599220000000000,2540 +SPY,8317,691.15,690.12,691.36,689.87,1794599280000000000,1039 +SPY,10689,690.12,689.08,690.64,688.95,1794599340000000000,628 +SPY,48334,689.08,690.61,691.12,689.05,1794599400000000000,1933 +SPY,33756,690.61,690.67,690.82,690.52,1794599460000000000,2250 +SPY,31036,690.67,689.52,691.18,689.39,1794599520000000000,3103 +SPY,53745,689.52,689.10,689.84,689.04,1794599580000000000,6718 +SPY,28504,689.10,688.05,689.65,687.78,1794599640000000000,2192 +SPY,40233,688.05,688.74,689.15,687.56,1794599700000000000,1609 +SPY,13507,688.74,687.50,689.09,687.27,1794599760000000000,1227 +SPY,2797,687.50,686.91,687.99,686.70,1794599820000000000,186 +SPY,8734,686.91,685.53,687.40,685.02,1794599880000000000,582 +SPY,55441,685.53,686.77,687.12,684.98,1794599940000000000,2217 +SPY,9553,686.77,686.06,687.25,685.91,1794600000000000000,502 +SPY,48924,686.06,685.77,686.57,685.49,1794600060000000000,3261 +SPY,53169,685.77,684.70,685.88,684.17,1794600120000000000,2215 +SPY,41195,684.70,684.49,685.08,684.44,1794600180000000000,2942 +SPY,56947,684.49,683.38,684.74,683.20,1794600240000000000,3163 +SPY,42639,683.38,682.02,683.87,681.77,1794600300000000000,4263 +SPY,56880,682.02,681.54,682.24,681.06,1794600360000000000,5170 +SPY,12083,681.54,682.86,683.26,681.50,1794600420000000000,863 +SPY,24251,682.86,683.23,683.58,682.79,1794600480000000000,1347 +SPY,14644,683.23,682.44,683.37,682.34,1794600540000000000,1046 +SPY,24465,682.44,683.53,683.95,682.10,1794600600000000000,978 +SPY,19199,683.53,682.45,683.82,681.91,1794600660000000000,2399 +SPY,22098,682.45,683.48,684.06,682.18,1794600720000000000,1052 +SPY,43557,683.48,683.69,683.74,683.38,1794600780000000000,3111 +SPY,52514,683.69,684.38,684.53,683.49,1794600840000000000,2188 +SPY,9544,684.38,685.97,686.15,683.90,1794600900000000000,636 +SPY,26009,685.97,686.54,686.56,685.95,1794600960000000000,1300 +SPY,33985,686.54,685.67,686.66,685.09,1794601020000000000,3398 +SPY,40114,685.67,684.41,686.04,683.97,1794601080000000000,2359 +SPY,8766,684.41,685.21,685.35,683.85,1794601140000000000,876 +SPY,32865,685.21,685.87,686.41,684.64,1794601200000000000,1643 +SPY,21726,685.87,685.62,686.24,685.54,1794601260000000000,1551 +SPY,32231,685.62,687.26,687.47,685.36,1794601320000000000,1342 +SPY,3549,687.26,688.44,688.49,686.99,1794601380000000000,322 +SPY,21235,688.44,689.64,689.83,688.24,1794601440000000000,1249 +SPY,841,689.64,688.77,690.15,688.18,1794601500000000000,76 +SPY,18125,688.77,689.24,689.74,688.29,1794601560000000000,1294 +SPY,2629,689.24,690.75,691.32,688.74,1794601620000000000,328 +SPY,44422,690.75,692.20,692.57,690.55,1794601680000000000,2613 +SPY,53819,692.20,693.08,693.48,692.05,1794601740000000000,3165 +SPY,2941,693.08,692.79,693.41,692.20,1794601800000000000,122 +SPY,17067,692.79,691.51,693.37,691.32,1794601860000000000,1896 +SPY,49836,691.51,691.52,691.76,691.07,1794601920000000000,2373 +SPY,15241,691.52,691.56,691.71,691.17,1794601980000000000,1693 +SPY,24705,691.56,690.43,692.02,690.22,1794602040000000000,2745 +SPY,35346,690.43,689.70,691.00,689.28,1794602100000000000,1767 +SPY,37873,689.70,689.50,690.23,689.23,1794602160000000000,1721 +SPY,22077,689.50,689.09,689.75,688.87,1794602220000000000,1576 +SPY,13100,689.09,689.24,689.38,688.59,1794602280000000000,935 +SPY,3525,689.24,688.62,689.53,688.20,1794602340000000000,146 +SPY,19227,688.62,689.23,689.32,688.38,1794602400000000000,1747 +SPY,9921,689.23,688.21,689.38,687.78,1794602460000000000,450 +SPY,13522,688.21,686.92,688.55,686.44,1794602520000000000,614 +SPY,12618,686.92,687.28,687.53,686.62,1794602580000000000,1577 +SPY,10681,687.28,686.99,687.60,686.68,1794602640000000000,534 +SPY,12797,686.99,688.40,688.87,686.71,1794602700000000000,1066 +SPY,57686,688.40,689.24,689.77,687.83,1794602760000000000,4120 +SPY,22834,689.24,688.28,689.80,688.08,1794602820000000000,992 +SPY,27167,688.28,687.37,688.70,687.18,1794602880000000000,1697 +SPY,13048,687.37,686.62,687.73,686.57,1794602940000000000,1449 +SPY,33513,686.62,685.09,687.11,684.63,1794603000000000000,2234 +SPY,23258,685.09,686.27,686.70,684.65,1794603060000000000,1107 +SPY,58510,686.27,686.87,687.13,686.04,1794603120000000000,4179 +SPY,41773,686.87,685.35,687.06,684.76,1794603180000000000,1898 +SPY,17150,685.35,686.59,687.18,684.96,1794603240000000000,745 +SPY,47431,686.59,685.73,686.70,685.65,1794603300000000000,1976 +SPY,47156,685.73,685.23,686.32,684.91,1794603360000000000,5894 +SPY,45462,685.23,685.08,685.78,684.84,1794603420000000000,3247 +SPY,49477,685.08,683.87,685.61,683.57,1794603480000000000,2473 +SPY,52580,683.87,683.09,684.31,683.03,1794603540000000000,2103 +SPY,34284,683.09,682.85,683.40,682.82,1794839400000000000,2142 +SPY,24302,682.85,682.20,683.34,682.14,1794839460000000000,1056 +SPY,57608,682.20,681.58,682.29,681.45,1794839520000000000,2743 +SPY,38269,681.58,682.95,683.18,681.30,1794839580000000000,1739 +SPY,7706,682.95,683.18,683.55,682.41,1794839640000000000,856 +SPY,3123,683.18,681.73,683.37,681.69,1794839700000000000,156 +SPY,8016,681.73,682.81,683.30,681.30,1794839760000000000,334 +SPY,14251,682.81,684.10,684.42,682.35,1794839820000000000,1096 +SPY,54510,684.10,683.10,684.19,682.73,1794839880000000000,2725 +SPY,56662,683.10,684.53,684.85,682.78,1794839940000000000,5666 +SPY,8250,684.53,684.88,685.08,684.20,1794840000000000000,434 +SPY,27826,684.88,684.37,685.41,684.19,1794840060000000000,2529 +SPY,13030,684.37,685.75,686.23,684.33,1794840120000000000,651 +SPY,35960,685.75,686.67,687.16,685.19,1794840180000000000,1712 +SPY,55606,686.67,687.85,687.92,686.65,1794840240000000000,2647 +SPY,33777,687.85,688.73,688.96,687.71,1794840300000000000,2111 +SPY,1191,688.73,689.49,689.95,688.45,1794840360000000000,51 +SPY,58936,689.49,689.14,689.71,688.98,1794840420000000000,2357 +SPY,49639,689.14,689.43,689.58,688.72,1794840480000000000,2919 +SPY,26891,689.43,690.97,691.09,689.13,1794840540000000000,2444 +SPY,39725,690.97,691.16,691.46,690.38,1794840600000000000,1891 +SPY,12425,691.16,689.79,691.48,689.66,1794840660000000000,887 +SPY,50434,689.79,690.49,691.07,689.43,1794840720000000000,3362 +SPY,11442,690.49,689.43,690.78,688.87,1794840780000000000,715 +SPY,10073,689.43,690.49,691.01,688.91,1794840840000000000,719 +SPY,52808,690.49,689.30,690.60,688.70,1794840900000000000,2779 +SPY,17153,689.30,688.79,689.75,688.69,1794840960000000000,1905 +SPY,46927,688.79,690.07,690.54,688.33,1794841020000000000,3910 +SPY,42389,690.07,689.07,690.47,688.97,1794841080000000000,2649 +SPY,54830,689.07,689.62,690.14,688.49,1794841140000000000,2610 +SPY,30886,689.62,690.87,691.18,689.44,1794841200000000000,1342 +SPY,25616,690.87,689.75,691.33,689.70,1794841260000000000,1024 +SPY,15003,689.75,689.67,690.08,689.27,1794841320000000000,937 +SPY,55846,689.67,690.54,691.07,689.64,1794841380000000000,3989 +SPY,14958,690.54,689.47,690.62,689.14,1794841440000000000,879 +SPY,27307,689.47,688.78,689.50,688.63,1794841500000000000,1706 +SPY,32311,688.78,689.37,689.39,688.51,1794841560000000000,3590 +SPY,33928,689.37,690.91,691.39,689.00,1794841620000000000,3392 +SPY,26760,690.91,690.60,691.02,690.37,1794841680000000000,1672 +SPY,59863,690.60,691.16,691.67,690.40,1794841740000000000,2602 +SPY,20984,691.16,691.55,691.93,690.58,1794841800000000000,1165 +SPY,57679,691.55,693.17,693.73,691.13,1794841860000000000,2883 +SPY,42225,693.17,692.84,693.24,692.34,1794841920000000000,2010 +SPY,8877,692.84,692.99,693.11,692.78,1794841980000000000,355 +SPY,8461,692.99,694.04,694.50,692.73,1794842040000000000,445 +SPY,31434,694.04,694.61,694.68,693.62,1794842100000000000,2245 +SPY,10303,694.61,693.55,694.67,693.09,1794842160000000000,490 +SPY,59771,693.55,692.09,694.02,691.58,1794842220000000000,4269 +SPY,52546,692.09,690.55,692.59,690.19,1794842280000000000,2388 +SPY,32041,690.55,690.12,690.99,689.89,1794842340000000000,2288 +SPY,38493,690.12,689.96,690.29,689.69,1794842400000000000,3849 +SPY,56624,689.96,690.85,691.21,689.67,1794842460000000000,4355 +SPY,13473,690.85,691.70,692.26,690.27,1794842520000000000,585 +SPY,37456,691.70,691.51,691.78,691.14,1794842580000000000,3121 +SPY,19408,691.51,690.64,691.73,690.49,1794842640000000000,808 +SPY,45522,690.64,691.30,691.58,690.06,1794842700000000000,3793 +SPY,41450,691.30,690.98,691.43,690.63,1794842760000000000,4145 +SPY,34483,690.98,690.78,691.53,690.61,1794842820000000000,1642 +SPY,44622,690.78,691.25,691.77,690.46,1794842880000000000,2788 +SPY,48177,691.25,691.75,692.29,690.86,1794842940000000000,4817 +SPY,50715,691.75,691.00,692.02,690.83,1794843000000000000,2535 +SPY,29708,691.00,689.59,691.57,689.49,1794843060000000000,2475 +SPY,48147,689.59,688.58,689.79,688.01,1794843120000000000,3439 +SPY,2663,688.58,688.61,689.09,688.10,1794843180000000000,133 +SPY,38619,688.61,689.52,689.81,688.27,1794843240000000000,4827 +SPY,38196,689.52,688.60,690.04,688.31,1794843300000000000,2122 +SPY,44514,688.60,689.91,690.28,688.33,1794843360000000000,5564 +SPY,37952,689.91,689.55,690.33,688.97,1794843420000000000,2710 +SPY,47147,689.55,688.31,689.81,687.87,1794843480000000000,2481 +SPY,21660,688.31,689.33,689.64,687.99,1794843540000000000,1666 +SPY,44826,689.33,687.83,689.73,687.27,1794843600000000000,2490 +SPY,2165,687.83,686.97,688.39,686.76,1794843660000000000,180 +SPY,11532,686.97,686.87,687.45,686.65,1794843720000000000,524 +SPY,48821,686.87,686.66,686.97,686.60,1794843780000000000,3487 +SPY,36599,686.66,685.24,687.11,684.85,1794843840000000000,2614 +SPY,19582,685.24,683.81,685.55,683.40,1794843900000000000,1780 +SPY,58508,683.81,682.96,683.94,682.82,1794843960000000000,5850 +SPY,41933,682.96,683.91,684.11,682.79,1794844020000000000,2795 +SPY,43935,683.91,685.19,685.23,683.56,1794844080000000000,5491 +SPY,40398,685.19,684.88,685.28,684.79,1794844140000000000,3366 +SPY,17177,684.88,685.41,685.73,684.50,1794844200000000000,1321 +SPY,52043,685.41,684.25,685.96,684.05,1794844260000000000,2081 +SPY,26475,684.25,682.83,684.69,682.77,1794844320000000000,1765 +SPY,48156,682.83,684.15,684.45,682.76,1794844380000000000,5350 +SPY,28232,684.15,684.00,684.43,683.87,1794844440000000000,2171 +SPY,35677,684.00,684.26,684.50,683.42,1794844500000000000,3243 +SPY,43405,684.26,683.42,684.54,683.24,1794844560000000000,1808 +SPY,21724,683.42,683.54,683.67,683.32,1794844620000000000,1277 +SPY,56644,683.54,683.53,683.69,683.06,1794844680000000000,4357 +SPY,1460,683.53,684.49,684.78,682.99,1794844740000000000,97 +SPY,23530,684.49,684.27,685.00,684.13,1794844800000000000,1810 +SPY,59028,684.27,682.79,684.46,682.34,1794844860000000000,2683 +SPY,40888,682.79,683.25,683.45,682.35,1794844920000000000,1635 +SPY,20891,683.25,683.96,684.34,682.66,1794844980000000000,1160 +SPY,48182,683.96,683.49,684.39,683.40,1794845040000000000,3011 +SPY,40871,683.49,684.70,685.08,682.98,1794845100000000000,5108 +SPY,9795,684.70,684.16,685.23,683.76,1794845160000000000,515 +SPY,11321,684.16,684.45,684.78,683.61,1794845220000000000,870 +SPY,33526,684.45,683.38,685.03,682.95,1794845280000000000,1396 +SPY,54394,683.38,684.65,684.87,683.14,1794845340000000000,3021 +SPY,48011,684.65,684.39,685.15,684.00,1794845400000000000,2400 +SPY,10185,684.39,684.07,684.95,683.51,1794845460000000000,565 +SPY,21927,684.07,683.31,684.29,682.96,1794845520000000000,996 +SPY,6073,683.31,683.80,684.13,682.97,1794845580000000000,759 +SPY,3515,683.80,684.70,684.87,683.45,1794845640000000000,439 +SPY,51678,684.70,685.62,685.81,684.42,1794845700000000000,2719 +SPY,13661,685.62,687.19,687.39,685.03,1794845760000000000,683 +SPY,26669,687.19,686.02,687.37,685.81,1794845820000000000,1111 +SPY,11613,686.02,686.88,687.25,685.72,1794845880000000000,504 +SPY,13086,686.88,685.87,687.45,685.73,1794845940000000000,1189 +SPY,44722,685.87,687.33,687.77,685.28,1794846000000000000,1944 +SPY,12127,687.33,686.87,687.92,686.82,1794846060000000000,932 +SPY,22698,686.87,685.86,687.12,685.52,1794846120000000000,1621 +SPY,8824,685.86,685.11,686.04,684.96,1794846180000000000,802 +SPY,33106,685.11,685.52,685.99,684.56,1794846240000000000,1504 +SPY,39525,685.52,685.31,685.72,684.89,1794846300000000000,2470 +SPY,12437,685.31,686.18,686.22,684.95,1794846360000000000,956 +SPY,50237,686.18,686.29,686.78,685.95,1794846420000000000,3588 +SPY,1171,686.29,687.56,687.63,686.17,1794846480000000000,53 +SPY,7977,687.56,687.21,687.68,686.64,1794846540000000000,997 +SPY,57603,687.21,686.50,687.38,685.93,1794846600000000000,2743 +SPY,9474,686.50,685.07,686.75,684.93,1794846660000000000,498 +SPY,13033,685.07,684.53,685.45,684.17,1794846720000000000,1448 +SPY,35690,684.53,683.27,684.61,682.73,1794846780000000000,3965 +SPY,12464,683.27,682.90,683.54,682.51,1794846840000000000,498 +SPY,9937,682.90,681.74,682.93,681.55,1794846900000000000,397 +SPY,14933,681.74,680.94,682.06,680.80,1794846960000000000,597 +SPY,7562,680.94,680.67,681.16,680.28,1794847020000000000,630 +SPY,6030,680.67,680.70,680.87,680.37,1794847080000000000,274 +SPY,50940,680.70,679.35,681.13,678.94,1794847140000000000,2681 +SPY,46575,679.35,679.25,679.65,679.16,1794847200000000000,2117 +SPY,25211,679.25,678.17,679.39,677.86,1794847260000000000,1008 +SPY,23009,678.17,677.59,678.30,677.46,1794847320000000000,2876 +SPY,54698,677.59,676.56,677.85,676.50,1794847380000000000,2878 +SPY,50337,676.56,677.24,677.53,676.51,1794847440000000000,2397 +SPY,34798,677.24,678.70,678.95,676.99,1794847500000000000,1739 +SPY,42359,678.70,677.25,679.22,677.19,1794847560000000000,1841 +SPY,20549,677.25,677.74,677.90,676.69,1794847620000000000,2568 +SPY,41210,677.74,676.72,677.99,676.28,1794847680000000000,3434 +SPY,10283,676.72,677.04,677.29,676.31,1794847740000000000,571 +SPY,27413,677.04,675.77,677.47,675.41,1794847800000000000,2108 +SPY,5423,675.77,675.78,676.31,675.24,1794847860000000000,338 +SPY,43054,675.78,676.79,677.35,675.53,1794847920000000000,2391 +SPY,23112,676.79,677.36,677.65,676.29,1794847980000000000,2101 +SPY,21495,677.36,677.67,678.05,677.06,1794848040000000000,934 +SPY,39265,677.67,678.46,678.55,677.13,1794848100000000000,3272 +SPY,34850,678.46,679.08,679.38,678.29,1794848160000000000,1742 +SPY,18721,679.08,678.53,679.13,678.11,1794848220000000000,1872 +SPY,50591,678.53,679.18,679.75,678.25,1794848280000000000,2975 +SPY,49555,679.18,679.54,679.86,678.67,1794848340000000000,2477 +SPY,55138,679.54,677.95,679.84,677.50,1794848400000000000,5012 +SPY,1605,677.95,678.10,678.41,677.72,1794848460000000000,107 +SPY,47926,678.10,676.82,678.39,676.24,1794848520000000000,5990 +SPY,49570,676.82,677.48,677.79,676.35,1794848580000000000,1982 +SPY,38297,677.48,678.55,678.69,677.33,1794848640000000000,2945 +SPY,38121,678.55,677.26,678.59,677.15,1794848700000000000,3812 +SPY,16589,677.26,678.55,678.83,676.94,1794848760000000000,663 +SPY,14540,678.55,677.38,678.65,677.01,1794848820000000000,605 +SPY,32223,677.38,678.94,679.26,677.23,1794848880000000000,2929 +SPY,45703,678.94,678.95,679.38,678.67,1794848940000000000,3046 +SPY,58927,678.95,680.07,680.17,678.56,1794849000000000000,2455 +SPY,1033,680.07,679.22,680.23,678.69,1794849060000000000,64 +SPY,17268,679.22,679.30,679.61,679.12,1794849120000000000,1918 +SPY,27003,679.30,678.89,679.62,678.78,1794849180000000000,1687 +SPY,26866,678.89,678.31,679.38,677.88,1794849240000000000,2442 +SPY,48480,678.31,678.55,678.99,677.94,1794849300000000000,4040 +SPY,35617,678.55,679.13,679.43,678.43,1794849360000000000,3957 +SPY,37363,679.13,678.44,679.40,678.05,1794849420000000000,3113 +SPY,56778,678.44,679.86,680.34,678.36,1794849480000000000,4367 +SPY,29563,679.86,679.80,680.36,679.40,1794849540000000000,2956 +SPY,3453,679.80,678.82,679.88,678.38,1794849600000000000,230 +SPY,53589,678.82,678.36,679.28,677.77,1794849660000000000,3827 +SPY,2462,678.36,680.00,680.56,677.84,1794849720000000000,246 +SPY,49010,680.00,679.95,680.35,679.70,1794849780000000000,2579 +SPY,30489,679.95,679.35,680.46,679.08,1794849840000000000,2540 +SPY,15145,679.35,679.26,679.79,679.20,1794849900000000000,841 +SPY,37001,679.26,678.81,679.40,678.71,1794849960000000000,2312 +SPY,54323,678.81,680.17,680.40,678.30,1794850020000000000,3195 +SPY,7464,680.17,681.52,681.77,679.80,1794850080000000000,311 +SPY,44777,681.52,680.62,681.88,680.10,1794850140000000000,2798 +SPY,9675,680.62,679.87,681.16,679.71,1794850200000000000,1209 +SPY,40959,679.87,678.53,680.34,678.08,1794850260000000000,1780 +SPY,4599,678.53,679.72,680.02,678.06,1794850320000000000,242 +SPY,41586,679.72,678.92,679.98,678.52,1794850380000000000,3465 +SPY,8790,678.92,679.87,680.21,678.67,1794850440000000000,879 +SPY,4503,679.87,679.21,679.98,678.69,1794850500000000000,346 +SPY,32283,679.21,680.31,680.62,678.88,1794850560000000000,2152 +SPY,16768,680.31,679.37,680.45,679.32,1794850620000000000,1197 +SPY,33266,679.37,680.98,681.09,679.32,1794850680000000000,2376 +SPY,25651,680.98,680.12,681.56,679.86,1794850740000000000,2137 +SPY,41733,680.12,679.44,680.35,679.01,1794850800000000000,3793 +SPY,36789,679.44,680.03,680.34,679.04,1794850860000000000,2299 +SPY,31980,680.03,680.20,680.41,679.80,1794850920000000000,2907 +SPY,44630,680.20,679.07,680.53,678.77,1794850980000000000,4057 +SPY,41276,679.07,680.01,680.47,678.98,1794851040000000000,2751 +SPY,32063,680.01,678.42,680.22,677.96,1794851100000000000,2671 +SPY,27272,678.42,679.57,679.60,678.26,1794851160000000000,1298 +SPY,17293,679.57,679.62,679.92,679.20,1794851220000000000,960 +SPY,51812,679.62,678.40,679.78,678.31,1794851280000000000,5756 +SPY,14431,678.40,678.87,679.06,677.92,1794851340000000000,1030 +SPY,41098,678.87,678.06,679.28,677.91,1794851400000000000,2054 +SPY,20620,678.06,679.32,679.69,677.52,1794851460000000000,1586 +SPY,23824,679.32,680.15,680.66,679.05,1794851520000000000,952 +SPY,30287,680.15,680.53,680.58,680.02,1794851580000000000,3365 +SPY,44362,680.53,681.30,681.37,680.16,1794851640000000000,4436 +SPY,5386,681.30,682.26,682.45,680.80,1794851700000000000,244 +SPY,50488,682.26,682.14,682.48,682.09,1794851760000000000,3365 +SPY,38345,682.14,682.60,683.18,681.59,1794851820000000000,2255 +SPY,35245,682.60,683.51,683.59,682.57,1794851880000000000,2349 +SPY,17471,683.51,684.69,684.81,683.26,1794851940000000000,873 +SPY,7396,684.69,685.61,685.91,684.10,1794852000000000000,389 +SPY,39966,685.61,684.13,685.89,683.85,1794852060000000000,2497 +SPY,1724,684.13,685.66,686.07,684.05,1794852120000000000,191 +SPY,15679,685.66,685.52,685.97,685.13,1794852180000000000,746 +SPY,7694,685.52,686.92,687.23,685.25,1794852240000000000,699 +SPY,3255,686.92,686.58,687.13,686.28,1794852300000000000,406 +SPY,23498,686.58,686.43,686.73,686.18,1794852360000000000,1021 +SPY,32167,686.43,684.84,686.98,684.58,1794852420000000000,1286 +SPY,27557,684.84,685.19,685.66,684.79,1794852480000000000,2296 +SPY,1818,685.19,683.62,685.29,683.10,1794852540000000000,106 +SPY,56355,683.62,684.27,684.84,683.41,1794852600000000000,2817 +SPY,16324,684.27,684.80,685.07,684.16,1794852660000000000,1813 +SPY,30948,684.80,686.29,686.50,684.67,1794852720000000000,2063 +SPY,2776,686.29,684.96,686.73,684.50,1794852780000000000,277 +SPY,40159,684.96,686.04,686.45,684.78,1794852840000000000,1606 +SPY,14881,686.04,685.76,686.59,685.44,1794852900000000000,1062 +SPY,47476,685.76,687.04,687.25,685.39,1794852960000000000,3391 +SPY,18655,687.04,687.62,687.93,686.55,1794853020000000000,1332 +SPY,24651,687.62,686.09,687.67,685.72,1794853080000000000,3081 +SPY,52019,686.09,686.79,687.11,685.71,1794853140000000000,5201 +SPY,49588,686.79,685.27,686.96,684.91,1794853200000000000,2916 +SPY,36245,685.27,684.25,685.32,683.80,1794853260000000000,4027 +SPY,56969,684.25,685.19,685.45,683.80,1794853320000000000,4382 +SPY,43269,685.19,684.12,685.71,683.72,1794853380000000000,2060 +SPY,13014,684.12,683.70,684.32,683.55,1794853440000000000,1183 +SPY,3422,683.70,684.95,685.09,683.57,1794853500000000000,171 +SPY,20665,684.95,684.30,685.35,684.11,1794853560000000000,2583 +SPY,56751,684.30,683.21,684.62,682.69,1794853620000000000,3152 +SPY,40779,683.21,684.02,684.50,682.65,1794853680000000000,2146 +SPY,38898,684.02,683.72,684.34,683.52,1794853740000000000,2047 +SPY,10041,683.72,683.27,684.08,682.99,1794853800000000000,401 +SPY,18693,683.27,683.57,683.80,682.90,1794853860000000000,890 +SPY,18073,683.57,682.04,683.78,681.54,1794853920000000000,1807 +SPY,18597,682.04,682.72,682.93,681.93,1794853980000000000,774 +SPY,52634,682.72,683.98,684.33,682.40,1794854040000000000,4386 +SPY,53755,683.98,683.77,684.30,683.51,1794854100000000000,6719 +SPY,54576,683.77,683.23,684.33,683.15,1794854160000000000,2728 +SPY,55157,683.23,684.84,685.34,683.17,1794854220000000000,6128 +SPY,26681,684.84,684.60,684.92,684.41,1794854280000000000,1569 +SPY,2171,684.60,684.27,685.03,683.68,1794854340000000000,86 +SPY,10479,684.27,683.25,684.57,682.67,1794854400000000000,698 +SPY,47495,683.25,682.89,683.66,682.51,1794854460000000000,1899 +SPY,54554,682.89,682.74,683.33,682.15,1794854520000000000,4959 +SPY,25145,682.74,681.22,683.14,681.16,1794854580000000000,1093 +SPY,1412,681.22,680.68,681.78,680.46,1794854640000000000,56 +SPY,29256,680.68,681.90,682.22,680.09,1794854700000000000,1720 +SPY,44006,681.90,681.07,682.09,680.54,1794854760000000000,2933 +SPY,9755,681.07,681.57,681.97,680.97,1794854820000000000,650 +SPY,5662,681.57,681.58,682.02,681.45,1794854880000000000,435 +SPY,9255,681.58,680.53,682.09,680.04,1794854940000000000,771 +SPY,15374,680.53,680.95,681.08,680.03,1794855000000000000,1182 +SPY,26181,680.95,679.88,681.55,679.54,1794855060000000000,1138 +SPY,42557,679.88,678.62,680.13,678.47,1794855120000000000,2026 +SPY,50972,678.62,680.00,680.51,678.37,1794855180000000000,6371 +SPY,43532,680.00,680.60,680.71,679.59,1794855240000000000,2560 +SPY,26899,680.60,679.48,680.99,678.92,1794855300000000000,3362 +SPY,37297,679.48,680.62,680.89,679.36,1794855360000000000,4662 +SPY,42448,680.62,679.80,680.64,679.77,1794855420000000000,1768 +SPY,42256,679.80,679.89,680.27,679.73,1794855480000000000,1760 +SPY,55810,679.89,680.76,680.82,679.57,1794855540000000000,2657 +SPY,32340,680.76,679.71,681.11,679.36,1794855600000000000,3593 +SPY,49192,679.71,678.45,680.24,678.24,1794855660000000000,3074 +SPY,23739,678.45,678.59,678.67,678.17,1794855720000000000,1079 +SPY,4841,678.59,678.86,679.16,678.24,1794855780000000000,322 +SPY,52672,678.86,678.43,679.11,677.86,1794855840000000000,4389 +SPY,32057,678.43,678.96,679.01,678.34,1794855900000000000,2914 +SPY,29898,678.96,679.33,679.40,678.48,1794855960000000000,2718 +SPY,31095,679.33,677.95,679.91,677.92,1794856020000000000,2073 +SPY,52449,677.95,678.24,678.57,677.88,1794856080000000000,2622 +SPY,9073,678.24,678.06,678.43,677.70,1794856140000000000,394 +SPY,29280,678.06,679.23,679.54,677.58,1794856200000000000,2661 +SPY,23152,679.23,679.58,679.60,679.17,1794856260000000000,1929 +SPY,35162,679.58,678.34,679.95,678.16,1794856320000000000,1406 +SPY,27989,678.34,679.94,680.13,678.05,1794856380000000000,2153 +SPY,10001,679.94,681.47,682.06,679.47,1794856440000000000,454 +SPY,34003,681.47,682.30,682.69,680.92,1794856500000000000,1619 +SPY,16638,682.30,680.95,682.77,680.69,1794856560000000000,875 +SPY,55345,680.95,681.55,681.92,680.51,1794856620000000000,3459 +SPY,5265,681.55,680.45,681.64,680.09,1794856680000000000,219 +SPY,15716,680.45,679.18,680.90,678.66,1794856740000000000,1428 +SPY,32561,679.18,679.43,680.00,678.85,1794856800000000000,1302 +SPY,34907,679.43,679.73,680.07,679.14,1794856860000000000,2908 +SPY,50115,679.73,678.47,680.12,678.41,1794856920000000000,4176 +SPY,34774,678.47,678.49,678.87,678.16,1794856980000000000,3161 +SPY,49725,678.49,677.48,678.67,676.99,1794857040000000000,2486 +SPY,10718,677.48,678.89,679.07,676.95,1794857100000000000,466 +SPY,15970,678.89,678.50,679.11,678.12,1794857160000000000,665 +SPY,54730,678.50,677.17,679.01,676.61,1794857220000000000,2606 +SPY,11637,677.17,677.70,678.21,676.81,1794857280000000000,831 +SPY,40828,677.70,677.89,678.08,677.55,1794857340000000000,1944 +SPY,53715,677.89,678.35,678.40,677.48,1794857400000000000,6714 +SPY,30238,678.35,677.04,678.79,676.58,1794857460000000000,2519 +SPY,10072,677.04,676.35,677.49,676.33,1794857520000000000,402 +SPY,40238,676.35,676.57,676.93,676.11,1794857580000000000,4023 +SPY,49328,676.57,676.54,676.83,676.26,1794857640000000000,2348 +SPY,49265,676.54,677.27,677.81,676.16,1794857700000000000,2592 +SPY,39523,677.27,675.83,677.85,675.55,1794857760000000000,2195 +SPY,7797,675.83,674.44,676.16,674.04,1794857820000000000,708 +SPY,19122,674.44,674.33,674.85,673.82,1794857880000000000,1006 +SPY,1589,674.33,675.52,675.91,674.20,1794857940000000000,122 +SPY,33163,675.52,674.05,675.59,673.65,1794858000000000000,1745 +SPY,47007,674.05,674.31,674.48,673.60,1794858060000000000,2238 +SPY,57708,674.31,675.24,675.44,674.14,1794858120000000000,4439 +SPY,966,675.24,674.31,675.67,673.98,1794858180000000000,38 +SPY,6828,674.31,674.60,674.66,674.14,1794858240000000000,758 +SPY,55659,674.60,674.96,675.23,674.09,1794858300000000000,2650 +SPY,19106,674.96,674.69,675.49,674.14,1794858360000000000,2388 +SPY,58990,674.69,674.09,675.06,673.71,1794858420000000000,3686 +SPY,39446,674.09,672.60,674.12,672.45,1794858480000000000,3944 +SPY,11881,672.60,674.01,674.43,672.30,1794858540000000000,565 +SPY,15655,674.01,673.16,674.38,672.84,1794858600000000000,652 +SPY,29847,673.16,673.72,674.12,673.07,1794858660000000000,2984 +SPY,57537,673.72,672.86,673.80,672.46,1794858720000000000,2501 +SPY,4659,672.86,673.34,673.53,672.65,1794858780000000000,358 +SPY,7367,673.34,674.60,674.64,673.00,1794858840000000000,566 +SPY,9847,674.60,673.93,674.91,673.88,1794858900000000000,547 +SPY,51135,673.93,673.97,674.33,673.67,1794858960000000000,2223 +SPY,28178,673.97,675.05,675.17,673.71,1794859020000000000,1280 +SPY,13406,675.05,674.95,675.30,674.91,1794859080000000000,582 +SPY,57293,674.95,674.91,675.04,674.81,1794859140000000000,3580 +SPY,51951,674.91,675.48,675.68,674.77,1794859200000000000,2078 +SPY,43063,675.48,674.37,675.80,673.78,1794859260000000000,4306 +SPY,5430,674.37,673.73,674.96,673.51,1794859320000000000,493 +SPY,8015,673.73,672.86,673.93,672.49,1794859380000000000,572 +SPY,14973,672.86,673.13,673.21,672.60,1794859440000000000,748 +SPY,14776,673.13,671.66,673.50,671.49,1794859500000000000,1136 +SPY,49235,671.66,672.17,672.32,671.28,1794859560000000000,2735 +SPY,43979,672.17,673.05,673.12,671.64,1794859620000000000,3664 +SPY,25185,673.05,672.95,673.41,672.42,1794859680000000000,3148 +SPY,42729,672.95,673.25,673.30,672.77,1794859740000000000,1942 +SPY,46808,673.25,673.06,673.36,672.55,1794859800000000000,2925 +SPY,58850,673.06,672.82,673.31,672.76,1794859860000000000,2354 +SPY,18395,672.82,671.70,673.25,671.40,1794859920000000000,919 +SPY,3665,671.70,672.18,672.20,671.39,1794859980000000000,166 +SPY,37266,672.18,671.90,672.34,671.57,1794860040000000000,2070 +SPY,48502,671.90,673.55,674.12,671.43,1794860100000000000,2020 +SPY,34345,673.55,675.02,675.24,672.98,1794860160000000000,2146 +SPY,56101,675.02,676.02,676.17,674.84,1794860220000000000,2337 +SPY,17686,676.02,676.80,676.84,675.53,1794860280000000000,803 +SPY,56252,676.80,675.81,676.83,675.36,1794860340000000000,2343 +SPY,12593,675.81,675.79,676.12,675.69,1794860400000000000,1399 +SPY,52804,675.79,677.23,677.77,675.66,1794860460000000000,6600 +SPY,48599,677.23,678.81,679.41,677.15,1794860520000000000,2314 +SPY,54652,678.81,679.93,680.01,678.32,1794860580000000000,6072 +SPY,36453,679.93,680.51,681.03,679.83,1794860640000000000,1822 +SPY,47045,680.51,679.60,680.72,679.40,1794860700000000000,3360 +SPY,39825,679.60,679.87,680.35,679.32,1794860760000000000,3982 +SPY,26675,679.87,681.10,681.65,679.44,1794860820000000000,1067 +SPY,24940,681.10,681.24,681.53,680.96,1794860880000000000,1084 +SPY,46159,681.24,681.54,681.59,681.16,1794860940000000000,2884 +SPY,8494,681.54,680.58,681.96,680.32,1794861000000000000,1061 +SPY,2942,680.58,681.73,681.87,680.37,1794861060000000000,367 +SPY,24273,681.73,682.88,683.16,681.20,1794861120000000000,2022 +SPY,35546,682.88,683.87,684.46,682.49,1794861180000000000,2734 +SPY,35241,683.87,683.38,684.22,682.99,1794861240000000000,2710 +SPY,26664,683.38,683.67,684.23,683.10,1794861300000000000,1212 +SPY,44656,683.67,682.95,684.06,682.54,1794861360000000000,4059 +SPY,54971,682.95,684.08,684.36,682.63,1794861420000000000,2617 +SPY,39192,684.08,685.12,685.24,683.53,1794861480000000000,2062 +SPY,55024,685.12,686.76,687.18,684.85,1794861540000000000,2200 +SPY,58271,686.76,686.76,686.99,686.42,1794861600000000000,2774 +SPY,49799,686.76,687.74,688.00,686.21,1794861660000000000,3830 +SPY,33846,687.74,687.35,688.28,687.05,1794861720000000000,2820 +SPY,45672,687.35,687.74,688.10,687.21,1794861780000000000,2854 +SPY,53094,687.74,686.55,688.19,686.53,1794861840000000000,3792 +SPY,57216,686.55,686.27,686.88,686.13,1794861900000000000,5721 +SPY,15596,686.27,684.98,686.61,684.93,1794861960000000000,779 +SPY,50007,684.98,684.98,685.45,684.48,1794862020000000000,6250 +SPY,58696,684.98,684.90,685.06,684.64,1794862080000000000,4192 +SPY,29062,684.90,686.08,686.64,684.69,1794862140000000000,2235 +SPY,38115,686.08,685.10,686.62,684.78,1794862200000000000,1657 +SPY,26148,685.10,685.71,686.14,685.02,1794862260000000000,1245 +SPY,1003,685.71,686.44,686.75,685.27,1794862320000000000,125 +SPY,50628,686.44,686.22,686.95,685.92,1794862380000000000,3375 +SPY,28826,686.22,686.53,686.60,685.92,1794862440000000000,2217 +SPY,5059,686.53,685.52,686.87,684.96,1794862500000000000,297 +SPY,35508,685.52,686.74,687.07,685.20,1794862560000000000,2219 +SPY,14964,686.74,686.22,686.99,685.74,1794862620000000000,712 +SPY,17571,686.22,684.79,686.47,684.77,1794862680000000000,1255 +SPY,48586,684.79,686.23,686.61,684.26,1794862740000000000,1943 diff --git a/tests/drawings_test.cpp b/tests/drawings_test.cpp new file mode 100644 index 0000000..25080f4 --- /dev/null +++ b/tests/drawings_test.cpp @@ -0,0 +1,155 @@ +// Unit + render test for the drawings.h annotation module: every +// kind parses from its spec and draws visible ink in its palette +// colour; a trend line re-maps correctly when the plot size changes +// (resize safety, since anchors are data coordinates); hit testing +// finds lines and misses empty space. +#include + +#include +#include +#include + +#include "drawings.h" + +static long count_ink(cairo_surface_t* surf, int ci) { + const double* col = fdw::kPalette[ci]; + int w = cairo_image_surface_get_width(surf); + int h = cairo_image_surface_get_height(surf); + int stride = cairo_image_surface_get_stride(surf); + unsigned char* d = cairo_image_surface_get_data(surf); + long n = 0; + for (int y = 0; y < h; ++y) + for (int x = 0; x < w; ++x) { + unsigned char* px = d + y * stride + x * 4; // BGRA + double r = px[2] / 255.0, g = px[1] / 255.0, b = px[0] / 255.0; + double dr = r - col[0], dg = g - col[1], db = b - col[2]; + if (dr * dr + dg * dg + db * db < 0.09) ++n; + } + return n; +} + +static long count_dark(cairo_surface_t* surf) { + int w = cairo_image_surface_get_width(surf); + int h = cairo_image_surface_get_height(surf); + int stride = cairo_image_surface_get_stride(surf); + unsigned char* d = cairo_image_surface_get_data(surf); + long n = 0; + for (int y = 0; y < h; ++y) + for (int x = 0; x < w; ++x) { + unsigned char* px = d + y * stride + x * 4; + if (px[0] < 90 && px[1] < 90 && px[2] < 90) ++n; + } + return n; +} + +// Ink of palette colour ci within a small box around (cx, cy). +static long ink_near(cairo_surface_t* surf, int ci, double cx, double cy) { + const double* col = fdw::kPalette[ci]; + int stride = cairo_image_surface_get_stride(surf); + unsigned char* d = cairo_image_surface_get_data(surf); + long n = 0; + for (int y = (int)cy - 6; y <= (int)cy + 6; ++y) + for (int x = (int)cx - 6; x <= (int)cx + 6; ++x) { + if (x < 0 || y < 0) continue; + unsigned char* px = d + y * stride + x * 4; + double r = px[2] / 255.0, g = px[1] / 255.0, b = px[0] / 255.0; + double dr = r - col[0], dg = g - col[1], db = b - col[2]; + if (dr * dr + dg * dg + db * db < 0.09) ++n; + } + return n; +} + +int main() { + int fails = 0; + auto check = [&](bool cond, const char* what) { + std::printf("%s %s\n", cond ? "OK " : "FAIL", what); + if (!cond) ++fails; + }; + fdw::Map map; + map.x = 0; + map.y = 0; + map.w = 480; + map.h = 300; + map.begin = 0; + map.count = 100; + map.lo = 90; + map.hi = 110; + + struct Case { + const char* spec; + int color; // palette index, or -1 for dark-text check + }; + const Case cases[] = { + {"trend 5 95 90 108 red", 1}, + {"ray 5 95 20 100 orange", 3}, + {"hline 105 blue", 0}, + {"vline 50 green", 2}, + {"rect 10 96 60 106 teal", 5}, + {"ellipse 10 96 60 106 purple", 4}, + {"arrow 5 95 90 108 magenta", 6}, + {"text 10 100 black Hello chart", -1}, + {"fib 5 95 90 108 brown", 7}, + {"free 5 95 30 108 60 97 90 104 slate", 9}, + {"measure 5 95 90 108 red", 1}, + }; + for (const Case& c : cases) { + auto d = fdw::parse_spec(c.spec); + check(d.has_value(), c.spec); + if (!d) continue; + cairo_surface_t* surf = + cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 480, 300); + cairo_t* cr = cairo_create(surf); + cairo_set_source_rgb(cr, 1, 1, 1); + cairo_paint(cr); + fdw::draw(cr, map, *d); + cairo_destroy(cr); + cairo_surface_flush(surf); + long ink = c.color >= 0 ? count_ink(surf, c.color) : count_dark(surf); + std::printf(" ink=%ld\n", ink); + check(ink > 15, " draws ink"); + cairo_surface_destroy(surf); + } + + // Resize safety: the same trend line rendered at 480x300 and at + // 960x600 must put ink at the scaled endpoint positions. + auto trend = fdw::parse_spec("trend 10 95 90 108 red"); + check(trend.has_value(), "resize trend parses"); + for (double scale : {1.0, 2.0}) { + int W = (int)(480 * scale), H = (int)(300 * scale); + fdw::Map m2 = map; + m2.w = W; + m2.h = H; + cairo_surface_t* surf = + cairo_image_surface_create(CAIRO_FORMAT_ARGB32, W, H); + cairo_t* cr = cairo_create(surf); + cairo_set_source_rgb(cr, 1, 1, 1); + cairo_paint(cr); + fdw::draw(cr, m2, *trend); + cairo_destroy(cr); + cairo_surface_flush(surf); + double x1 = m2.px(10), y1 = m2.py(95); + double x2 = m2.px(90), y2 = m2.py(108); + long a = ink_near(surf, 1, x1, y1); + long b = ink_near(surf, 1, x2, y2); + std::printf(" scale=%.0f endpoints ink=%ld/%ld\n", scale, a, b); + check(a > 2 && b > 2, " endpoints remapped"); + cairo_surface_destroy(surf); + } + + // Hit testing: on the line / off the line. + { + double mx = map.px(47.5), my = map.py(101.5); + check(fdw::hit_test(map, *trend, mx, my), "hit on trend midpoint"); + check(!fdw::hit_test(map, *trend, map.px(47.5), map.py(92)), + "miss away from trend"); + auto hl = fdw::parse_spec("hline 105 blue"); + check(hl && fdw::hit_test(map, *hl, 200, map.py(105)), + "hit on hline"); + check(hl && !fdw::hit_test(map, *hl, 200, map.py(100)), + "miss away from hline"); + } + + std::printf("DRAWINGS TEST %s (%d failures)\n", + fails ? "FAIL" : "PASS", fails); + return fails ? 1 : 0; +} diff --git a/tests/gen_matrix.cpp b/tests/gen_matrix.cpp new file mode 100644 index 0000000..24214e8 --- /dev/null +++ b/tests/gen_matrix.cpp @@ -0,0 +1,340 @@ +// Extensive generator test matrix: builds many designs through the real +// generator API and writes each as a project under tests/out//. +// run_matrix.sh then compiles every project (with -Wextra) and runs it. + +#include +#include +#include +#include +#include + +#include "../src/generator.h" +#include "../src/props.h" + +static int g_case = 0; +static std::string g_base; + +static Widget mk(const std::string& type, const std::string& name, int x, + int y) { + Widget w; + w.name = name; + w.type = type; + w.x = x; + w.y = y; + const TypeInfo* t = find_type(type); + w.width = t ? t->def_width : 100; + w.height = t ? t->def_height : 30; + for (const PropDef& d : props_for(type)) + if (d.def && d.def[0]) w.props[d.name] = d.def; + for (const PropDef& d : common_props()) + if (d.def && d.def[0]) w.props[d.name] = d.def; + if (find_prop(type, "label")) w.props["label"] = name; + return w; +} + +static void emit(const std::string& case_name, Project p) { + std::string dir = g_base + "/" + case_name; + GenResult r = generate_project(p, p.widgets, dir); + std::printf("case %-28s %s (%zu files)\n", case_name.c_str(), + r.ok ? "generated" : ("FAILED: " + r.error).c_str(), + r.files.size()); + if (!r.ok) ++g_case; // count failures in exit code via marker +} + +int main(int argc, char** argv) { + g_base = argc > 1 ? argv[1] : "tests/out"; + + // 1. Empty design. + emit("01_empty", Project()); + + // 2. One of every widget type, default properties. + { + Project p; + int i = 0; + for (const TypeInfo& t : kTypes) { + std::string name = std::string(t.key) + "_001"; + p.widgets.push_back(mk(t.key, name, 10 + (i % 4) * 190, + 10 + (i / 4) * 130)); + ++i; + } + emit("02_all_types", p); + } + + // 3. Every property of every type set to a non-default test value. + { + Project p; + int i = 0; + for (const TypeInfo& t : kTypes) { + Widget w = mk(t.key, std::string(t.key) + "_001", + 10 + (i % 4) * 190, 10 + (i / 4) * 130); + std::vector defs; + for (const PropDef& d : props_for(t.key)) defs.push_back(&d); + for (const PropDef& d : common_props()) defs.push_back(&d); + for (const PropDef* d : defs) { + switch (d->kind) { + case PropKind::Str: + w.props[d->name] = "Test value"; + break; + case PropKind::Bool: + w.props[d->name] = + std::string(d->def) == "true" ? "false" : "true"; + break; + case PropKind::Int: + w.props[d->name] = d->def; + break; + case PropKind::Double: { + // 42.5, clamped into the property's valid range + // (the designer's set_prop rejects out-of-range). + double v = 42.5; + if (d->hi > d->lo) { + if (v < d->lo) v = d->lo; + if (v > d->hi) v = d->hi; + } + char buf[64]; + std::snprintf(buf, sizeof buf, "%g", v); + w.props[d->name] = buf; + break; + } + case PropKind::Enum: { + int last = 0; + for (int k = 0; d->enum_values && d->enum_values[k]; + ++k) + last = d->enum_values[k]; + w.props[d->name] = std::to_string(last); + break; + } + case PropKind::Color: + w.props[d->name] = "#336699"; + break; + } + } + if (find_prop(t.key, "label")) w.props["label"] = "Test value"; + w.props["background"] = "#336699"; + w.props["foreground"] = "white"; + w.props["tooltip-text"] = "tip with \"quotes\" and \\ slash"; + p.widgets.push_back(w); + ++i; + } + emit("03_all_props", p); + } + + // 4. Every enum property at every value. + { + Project p; + int x = 10; + auto add = [&](const std::string& type, const std::string& name, + const std::string& prop, const std::string& val) { + Widget w = mk(type, name, x, 10); + w.props[prop] = val; + p.widgets.push_back(w); + x += 130; + }; + for (int v = 0; v <= 2; ++v) + add("Button", "Btn_relief_" + std::to_string(v), "relief", + std::to_string(v)); + for (int v = 0; v <= 4; ++v) + add("Frame", "Frm_shadow_" + std::to_string(v), "shadow-type", + std::to_string(v)); + for (int v = 0; v <= 3; ++v) + add("Listbox", "Lst_sel_" + std::to_string(v), "selection-mode", + std::to_string(v)); + for (int v = 0; v <= 4; ++v) + add("Menubutton", "Mnu_dir_" + std::to_string(v), "direction", + std::to_string(v)); + for (int v = 0; v <= 3; ++v) + add("Text", "Txt_wrap_" + std::to_string(v), "wrap-mode", + std::to_string(v)); + emit("04_enum_values", p); + } + + // 5. Menus: several menus, separators, spaces and symbols in labels, + // plus junk lines that must be ignored. + { + Project p; + p.menubar = + "File: New | Open | Save As | - | Quit\n" + "Edit: Cut | Copy & Paste | Select All\n" + "this line has no colon and is ignored\n" + ": empty menu label ignored\n" + "Help: About FDesigner\n"; + p.widgets.push_back(mk("Button", "Button_001", 10, 10)); + emit("05_menus", p); + } + + // 6. Hostile strings: quotes, backslashes, newline, tab, UTF-8, + // format-specifier lookalikes. + { + Project p; + p.title = "My \"Quoted\" \\ App\nSecond line"; + p.background = "#102030"; + Widget b = mk("Button", "Button_001", 10, 10); + b.props["label"] = "Click \"me\" \\ 100% %s %d"; + p.widgets.push_back(b); + Widget l = mk("Label", "Label_001", 10, 60); + l.props["label"] = "café — ☕\t tabbed"; + p.widgets.push_back(l); + Widget e = mk("Entry", "Entry_001", 10, 110); + e.props["text"] = "a\"b\\c\nd"; + e.props["placeholder-text"] = "type \"here\""; + p.widgets.push_back(e); + emit("06_hostile_strings", p); + } + + // 7. Class-name sanitization: spaces/punctuation, leading digit, + // empty (falls back to MyWindow). + { + Project p; + p.class_name = "my cool app!"; + p.widgets.push_back(mk("Button", "Button_001", 10, 10)); + emit("07_class_spaces", p); + } + { + Project p; + p.class_name = "9lives"; + p.widgets.push_back(mk("Label", "Label_001", 10, 10)); + emit("08_class_digit", p); + } + { + Project p; + p.class_name = ""; + p.widgets.push_back(mk("Entry", "Entry_001", 10, 10)); + emit("09_class_empty", p); + } + + // 8. Spreadsheet extremes + several toplevels. + { + Project p; + Widget big = mk("Spreadsheet", "Spreadsheet_001", 10, 10); + big.props["rows"] = "50"; + big.props["columns"] = "26"; + big.width = 900; + big.height = 500; + p.widgets.push_back(big); + Widget tiny = mk("Spreadsheet", "Spreadsheet_002", 10, 520); + tiny.props["rows"] = "1"; + tiny.props["columns"] = "1"; + p.widgets.push_back(tiny); + for (int i = 1; i <= 3; ++i) { + Widget t = mk("Toplevel", "Toplevel_00" + std::to_string(i), + 100 * i, 100 * i); + t.props["title"] = "Window " + std::to_string(i); + t.props["resizable"] = i == 2 ? "false" : "true"; + p.widgets.push_back(t); + } + emit("10_spreadsheet_toplevels", p); + } + + // 9. Forty mixed widgets. + { + Project p; + const char* types[] = {"Button", "Label", "Entry", "Checkbutton", + "Frame", "Combobox", "Scale", "Text"}; + for (int i = 0; i < 40; ++i) { + std::string type = types[i % 8]; + char buf[32]; + std::snprintf(buf, sizeof buf, "%s_%03d", type.c_str(), i + 1); + p.widgets.push_back( + mk(type, buf, 10 + (i % 5) * 170, 10 + (i / 5) * 70)); + } + emit("11_forty_widgets", p); + } + + // 10. Window variations: no background, huge size, tiny size. + { + Project p; + p.win_width = 3840; + p.win_height = 2160; + p.widgets.push_back(mk("Label", "Label_001", 10, 10)); + emit("12_window_huge", p); + } + { + Project p; + p.win_width = 50; + p.win_height = 50; + p.background = "red"; + emit("13_window_tiny", p); + } + + // 11. StockChart fed with the fake Massive-format CSV. + { + Project p; + Widget chart = mk("StockChart", "StockChart_001", 10, 10); + chart.width = 640; + chart.height = 320; + std::string csv = std::filesystem::absolute( + g_base + "/14_stockchart/SPY_fake.csv").string(); + chart.props["data-file"] = csv; + p.widgets.push_back(chart); + p.widgets.push_back(mk("Label", "Label_001", 10, 340)); + emit("14_stockchart", p); + std::ifstream src("tests/data/SPY_fake.csv", std::ios::binary); + std::ofstream dst(g_base + "/14_stockchart/SPY_fake.csv", + std::ios::binary); + dst << src.rdbuf(); + } + + // 12. StockChart with indicator submodules attached (overlay + pane + // + markers), fed with the 3-day series. + { + Project p; + Widget chart = mk("StockChart", "StockChart_001", 10, 10); + chart.width = 900; + chart.height = 500; + std::string csv = std::filesystem::absolute( + g_base + "/15_stockchart_indicators/SPY_fake_long.csv") + .string(); + chart.props["data-file"] = csv; + chart.props["max-candles"] = "400"; + chart.props["indicators"] = + "SMA(20), EMA(50), Bollinger Bands, RSI, MACD, Engulfing"; + p.widgets.push_back(chart); + emit("15_stockchart_indicators", p); + std::ifstream src("tests/data/SPY_fake_long.csv", + std::ios::binary); + std::ofstream dst(g_base + + "/15_stockchart_indicators/SPY_fake_long.csv", + std::ios::binary); + dst << src.rdbuf(); + } + + // 13. StockChart Pro: percent scale, QQQ comparison series, and a + // full set of annotations, in a 1024x768 window. + { + Project p; + p.win_width = 1024; + p.win_height = 768; + Widget chart = mk("StockChart", "StockChart_001", 10, 10); + chart.width = 1004; + chart.height = 748; + std::string csv = std::filesystem::absolute( + g_base + "/16_stockchart_pro/SPY_fake_long.csv").string(); + std::string qqq = std::filesystem::absolute( + g_base + "/16_stockchart_pro/QQQ_fake_long.csv").string(); + chart.props["data-file"] = csv; + chart.props["max-candles"] = "400"; + chart.props["indicators"] = "SMA(20), RSI"; + chart.props["scale-mode"] = "1"; + chart.props["compare-files"] = qqq; + chart.props["drawings"] = + "trend 790 676 1150 690 red; hline 688 blue; " + "fib 800 674 1000 692 purple; " + "text 830 691 orange Resistance zone; " + "rect 860 678 960 684 teal; " + "arrow 920 690 1090 680 magenta; " + "measure 840 677 1010 689 black"; + p.widgets.push_back(chart); + emit("16_stockchart_pro", p); + std::ifstream src("tests/data/SPY_fake_long.csv", + std::ios::binary); + std::ofstream dst(g_base + "/16_stockchart_pro/SPY_fake_long.csv", + std::ios::binary); + dst << src.rdbuf(); + std::ifstream src2("tests/data/QQQ_fake_long.csv", + std::ios::binary); + std::ofstream dst2(g_base + "/16_stockchart_pro/QQQ_fake_long.csv", + std::ios::binary); + dst2 << src2.rdbuf(); + } + + return g_case == 0 ? 0 : 1; +} diff --git a/tests/indicator_render_test.cpp b/tests/indicator_render_test.cpp new file mode 100644 index 0000000..52c4cae --- /dev/null +++ b/tests/indicator_render_test.cpp @@ -0,0 +1,59 @@ +// Renders the generated StockChart (matrix case 15, with indicator +// submodules attached by design) offscreen and verifies the indicators +// actually PLOT: the SMA's palette-blue line must appear in the price +// pane, and palette-coloured pixels must appear in the oscillator pane +// strip at the bottom (RSI/MACD live there). +#include + +#include + +#include "StockChart_001.h" + +int main(int argc, char** argv) { + gtk_init(&argc, &argv); + GtkWidget* win = gtk_window_new(GTK_WINDOW_TOPLEVEL); + GtkWidget* fixed = gtk_fixed_new(); + gtk_container_add(GTK_CONTAINER(win), fixed); + gtk_window_set_default_size(GTK_WINDOW(win), 1000, 700); + StockChart_001 chart(fixed); + if (chart.candle_count() == 0) { + std::printf("NO DATA LOADED\n"); + return 1; + } + gtk_widget_show_all(win); + for (int i = 0; i < 20; ++i) + while (gtk_events_pending()) gtk_main_iteration(); + GtkWidget* w = chart.widget(); + int width = gtk_widget_get_allocated_width(w); + int height = gtk_widget_get_allocated_height(w); + if (width < 50 || height < 50) { + std::printf("BAD SIZE %dx%d\n", width, height); + return 1; + } + cairo_surface_t* surf = + cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height); + cairo_t* cr = cairo_create(surf); + gtk_widget_draw(w, cr); + cairo_destroy(cr); + cairo_surface_flush(surf); + unsigned char* px = cairo_image_surface_get_data(surf); + int stride = cairo_image_surface_get_stride(surf); + // Palette blue (SMA and RSI line colour): b high, r low, g mid. + long blue_top = 0, blue_bottom = 0; + int split = (int)(height * 0.66); + for (int y = 0; y < height; ++y) + for (int x = 0; x < width; ++x) { + unsigned char* p = px + y * stride + x * 4; // BGRA + int b = p[0], g = p[1], r = p[2]; + if (b > 150 && r < 110 && g > 40 && g < 170) { + if (y < split) + ++blue_top; + else + ++blue_bottom; + } + } + std::printf("candles=%d size=%dx%d BLUE price-pane %ld pane %ld\n", + chart.candle_count(), width, height, blue_top, + blue_bottom); + return (blue_top > 100 && blue_bottom > 50) ? 0 : 1; +} diff --git a/tests/indicator_test.cpp b/tests/indicator_test.cpp new file mode 100644 index 0000000..9fb8e8f --- /dev/null +++ b/tests/indicator_test.cpp @@ -0,0 +1,433 @@ +// indicator_test.cpp — visual + numeric test of all 250 StockChart +// indicator submodules in src/indicators.h. +// +// For every registered indicator: +// 1. compute() over the 3-day fake Massive series must produce finite +// values (line/bar indicators) — patterns may legitimately not fire; +// 2. the indicator is rendered (with candles) onto its own cairo +// surface and we count pixels painted in the indicator's palette +// colours — proof it actually draws; +// 3. a labelled contact sheet of all 250 renders is written to +// tests/out/indicators_contact_sheet.png for eyeballing. +// Plus crafted-bar detection checks for famous patterns and range +// checks for bounded oscillators. +// +// Build: g++ -O2 -std=c++17 -I src tests/indicator_test.cpp -o /tmp/itest +// $(pkg-config --cflags --libs gtk+-3.0) +// Run: /tmp/itest tests/data/SPY_fake_long.csv tests/out + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "indicators.h" + +using fdi::Bar; + +static std::vector load_csv(const std::string& path) { + std::ifstream in(path); + std::vector out; + std::string line; + if (!std::getline(in, line)) return out; // header + while (std::getline(in, line)) { + std::stringstream ss(line); + std::string f[8]; + for (int i = 0; i < 8; ++i) + if (!std::getline(ss, f[i], ',')) f[i] = "0"; + Bar b; + b.volume = std::atof(f[1].c_str()); + b.open = std::atof(f[2].c_str()); + b.close = std::atof(f[3].c_str()); + b.high = std::atof(f[4].c_str()); + b.low = std::atof(f[5].c_str()); + b.time = std::atoll(f[6].c_str()); + out.push_back(b); + } + return out; +} + +// Draw light-grey candles for bars [begin, begin+count) into an area. +static void draw_candles(cairo_t* cr, const std::vector& bars, + size_t begin, size_t count, double x, double y, + double w, double h, double& lo, double& hi) { + lo = 1e300; + hi = -1e300; + for (size_t i = begin; i < begin + count && i < bars.size(); ++i) { + lo = std::min(lo, bars[i].low); + hi = std::max(hi, bars[i].high); + } + if (hi - lo < 1e-9) hi = lo + 1; + double slot = w / count; + for (size_t k = 0; k < count && begin + k < bars.size(); ++k) { + const Bar& b = bars[begin + k]; + double cx = x + slot * k + slot / 2; + auto Y = [&](double v) { return y + (hi - v) / (hi - lo) * h; }; + cairo_set_source_rgb(cr, 0.78, 0.78, 0.78); + cairo_set_line_width(cr, 1.0); + cairo_move_to(cr, cx, Y(b.high)); + cairo_line_to(cr, cx, Y(b.low)); + cairo_stroke(cr); + double y1 = Y(std::max(b.open, b.close)); + double y2 = Y(std::min(b.open, b.close)); + if (y2 - y1 < 1) y2 = y1 + 1; + cairo_rectangle(cr, cx - std::max(0.5, slot * 0.35), y1, + std::max(1.0, slot * 0.7), y2 - y1); + cairo_fill(cr); + } +} + +// Count pixels close to any palette colour (indicator ink). +static long count_ink(cairo_surface_t* surf) { + int w = cairo_image_surface_get_width(surf); + int h = cairo_image_surface_get_height(surf); + int stride = cairo_image_surface_get_stride(surf); + unsigned char* data = cairo_image_surface_get_data(surf); + long ink = 0; + for (int yy = 0; yy < h; ++yy) { + for (int xx = 0; xx < w; ++xx) { + unsigned char* px = data + yy * stride + xx * 4; + double r = px[2] / 255.0, g = px[1] / 255.0, b = px[0] / 255.0; + for (int c = 0; c < fdi::kPaletteSize; ++c) { + double dr = r - fdi::kPalette[c][0]; + double dg = g - fdi::kPalette[c][1]; + double db = b - fdi::kPalette[c][2]; + if (dr * dr + dg * dg + db * db < 0.09) { + ++ink; + break; + } + } + } + } + return ink; +} + +// A pattern-rich synthetic series: overnight gaps, varied bodies and +// occasional dojis — minute bars from a continuous feed rarely gap, so +// gap patterns (Kicking, Abandoned Baby, stars...) can never fire on +// the plain random walk. Patterns are tested on both series. +static std::vector make_gapped_bars() { + std::vector out; + unsigned long long s = 88172645463325252ULL; + auto rnd = [&]() { + s ^= s << 13; + s ^= s >> 7; + s ^= s << 17; + return (s >> 11) / 9007199254740992.0; + }; + double price = 100; + for (int i = 0; i < 3000; ++i) { + Bar b; + double gap = (rnd() - 0.5) * 1.6; + b.open = price + gap; + double body = (rnd() - 0.48) * 3.0; + if (i % 37 == 0) body = (rnd() - 0.5) * 0.06; // forced doji + if (i % 53 == 0) body = (rnd() > 0.5 ? 1 : -1) * (2.5 + rnd()); + b.close = b.open + body; + b.high = std::max(b.open, b.close) + rnd() * 0.9; + b.low = std::min(b.open, b.close) - rnd() * 0.9; + b.volume = 1000 + rnd() * 40000; + b.time = (long long)i * 60000000000LL; + out.push_back(b); + price = b.close; + if (price < 20) price = 100; + } + return out; +} + +int main(int argc, char** argv) { + std::string csv = argc > 1 ? argv[1] : "tests/data/SPY_fake_long.csv"; + std::string outdir = argc > 2 ? argv[2] : "tests/out"; + std::vector bars = load_csv(csv); + if (bars.size() < 200) { + std::printf("FAIL: only %zu bars loaded from %s\n", bars.size(), + csv.c_str()); + return 1; + } + std::printf("loaded %zu bars\n", bars.size()); + std::vector gapped = make_gapped_bars(); + std::printf("plus %zu gapped pattern-test bars\n", gapped.size()); + const size_t VISIBLE = 400; + size_t begin = bars.size() - VISIBLE; + + int passed = 0, failed = 0, nofire = 0; + std::vector fail_names, nofire_names; + + // Contact sheet geometry: 5 columns x 50 rows. + const int COLS = 5, CELL_W = 380, CELL_H = 112; + int rows = (int)((fdi::all_defs().size() + COLS - 1) / COLS); + cairo_surface_t* sheet = cairo_image_surface_create( + CAIRO_FORMAT_ARGB32, COLS * CELL_W, rows * CELL_H); + cairo_t* scr = cairo_create(sheet); + cairo_set_source_rgb(scr, 1, 1, 1); + cairo_paint(scr); + + size_t idx = 0; + for (const fdi::Def& def : fdi::all_defs()) { + auto ind = fdi::make_indicator(def.name); + if (!ind) { + std::printf("FAIL %-28s cannot construct\n", def.name); + ++failed; + fail_names.push_back(def.name); + ++idx; + continue; + } + // 1. Compute. Patterns also run on the gapped series. + fdi::Lines lines = ind->compute(bars); + long finite = 0, markers_fired = 0; + for (const auto& s : lines) + for (double v : s) { + if (std::isfinite(v)) ++finite; + if (def.kind == fdi::PlotKind::Markers && v != 0 && + std::isfinite(v)) + ++markers_fired; + } + // Render dataset: patterns render on the gapped series, windowed + // to end at the last firing so a triangle is in view. + const std::vector* rbars = &bars; + size_t rbegin = begin; + if (def.kind == fdi::PlotKind::Markers) { + fdi::Lines gl = ind->compute(gapped); + long gfired = 0; + size_t last_fire = 0; + if (!gl.empty()) + for (size_t i = 0; i < gl[0].size(); ++i) + if (gl[0][i] != 0 && std::isfinite(gl[0][i])) { + ++gfired; + last_fire = i; + } + markers_fired += gfired; + if (gfired > 0) { + rbars = &gapped; + rbegin = last_fire >= VISIBLE - 1 ? last_fire - VISIBLE + 1 + : 0; + } + } + // 2. Render on its own surface and count ink. + cairo_surface_t* surf = + cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 480, 300); + cairo_t* cr = cairo_create(surf); + cairo_set_source_rgb(cr, 1, 1, 1); + cairo_paint(cr); + double lo, hi; + bool is_pane = def.kind == fdi::PlotKind::Pane; + draw_candles(cr, *rbars, rbegin, VISIBLE, 4, 4, 472, + is_pane ? 190 : 292, lo, hi); + fdi::PlotArea area; + area.cr = cr; + area.begin = rbegin; + area.count = VISIBLE; + if (is_pane) { + area.x = 4; + area.y = 206; + area.w = 472; + area.h = 90; + } else { + area.x = 4; + area.y = 4; + area.w = 472; + area.h = 292; + area.lo = lo; + area.hi = hi; + } + ind->draw(area, *rbars); + cairo_surface_flush(surf); + long ink = count_ink(surf); + cairo_destroy(cr); + cairo_surface_destroy(surf); + + bool ok = true; + const char* note = ""; + if (def.kind == fdi::PlotKind::Markers) { + if (markers_fired == 0) { + ok = false; + note = "pattern did not fire on this data"; + ++nofire; + nofire_names.push_back(def.name); + } else if (ink == 0) { + ok = false; + note = "fired but drew nothing"; + } + } else { + if (finite == 0) { + ok = false; + note = "no finite values"; + } else if (ink == 0) { + ok = false; + note = "computed but drew nothing"; + } + } + if (ok) { + ++passed; + } else if (def.kind != fdi::PlotKind::Markers || + markers_fired > 0) { + ++failed; + fail_names.push_back(std::string(def.name) + " (" + note + ")"); + } + std::printf("%-4s %-28s finite=%-7ld fired=%-4ld ink=%-6ld %s\n", + ok ? "PASS" : (def.kind == fdi::PlotKind::Markers && + markers_fired == 0 + ? "NOFIRE" + : "FAIL"), + def.name, finite, markers_fired, ink, note); + + // 3. Contact sheet cell. + int cx = (int)(idx % COLS) * CELL_W, cy = (int)(idx / COLS) * CELL_H; + cairo_set_source_rgb(scr, 1, 1, 1); + cairo_rectangle(scr, cx, cy, CELL_W, CELL_H); + cairo_fill(scr); + cairo_set_source_rgb(scr, 0.6, 0.6, 0.6); + cairo_set_line_width(scr, 0.5); + cairo_rectangle(scr, cx + 0.5, cy + 0.5, CELL_W - 1, CELL_H - 1); + cairo_stroke(scr); + cairo_set_source_rgb(scr, 0.15, 0.15, 0.15); + cairo_select_font_face(scr, "Sans", CAIRO_FONT_SLANT_NORMAL, + CAIRO_FONT_WEIGHT_BOLD); + cairo_set_font_size(scr, 10); + cairo_move_to(scr, cx + 5, cy + 13); + cairo_show_text(scr, def.name); + size_t sbegin = bars.size() - 300; + const std::vector* cbars = &bars; + if (def.kind == fdi::PlotKind::Markers && rbars == &gapped) { + cbars = &gapped; + sbegin = rbegin + 100; + } + double slo, shi; + draw_candles(scr, *cbars, sbegin, 300, cx + 4, cy + 18, CELL_W - 8, + is_pane ? 52 : CELL_H - 24, slo, shi); + fdi::PlotArea cell; + cell.cr = scr; + cell.begin = sbegin; + cell.count = 300; + if (is_pane) { + cell.x = cx + 4; + cell.y = cy + 74; + cell.w = CELL_W - 8; + cell.h = CELL_H - 78; + } else { + cell.x = cx + 4; + cell.y = cy + 18; + cell.w = CELL_W - 8; + cell.h = CELL_H - 24; + cell.lo = slo; + cell.hi = shi; + } + ind->draw(cell, *cbars); + ++idx; + } + std::string sheet_path = outdir + "/indicators_contact_sheet.png"; + cairo_surface_write_to_png(sheet, sheet_path.c_str()); + cairo_destroy(scr); + cairo_surface_destroy(sheet); + std::printf("contact sheet: %s\n", sheet_path.c_str()); + + // 4. Crafted-bar pattern detection checks. + auto mk = [](double o, double h, double l, double c) { + Bar b; + b.open = o; + b.high = h; + b.low = l; + b.close = c; + b.volume = 1000; + return b; + }; + int crafted_fail = 0; + auto expect = [&](const char* name, const std::vector& seq, + size_t at, double sign) { + auto ind = fdi::make_indicator(name); + fdi::Lines lines = ind->compute(seq); + double got = lines.empty() || at >= lines[0].size() ? 0 : lines[0][at]; + bool ok = (sign > 0 && got > 0) || (sign < 0 && got < 0); + std::printf("crafted %-16s at %zu: got %+.0f %s\n", name, at, got, + ok ? "OK" : "MISMATCH"); + if (!ok) ++crafted_fail; + }; + expect("Doji", {mk(100, 101, 99, 100.05)}, 0, +1); + expect("Engulfing", + {mk(102, 102.5, 99.5, 100), mk(99.5, 103, 99, 102.5)}, 1, +1); + expect("Hammer", + {mk(100, 100.5, 96.5, 97), mk(100, 100.5, 96.8, 100.4)}, 1, +1); + expect("Morning Star", + {mk(110, 110.5, 99.5, 100), mk(98, 98.8, 97.5, 98.5), + mk(99, 107.5, 98.5, 107)}, + 2, +1); + expect("Shooting Star", + {mk(100, 101, 99, 100.8), mk(102, 105, 101.9, 102.15)}, 1, + -1); + expect("Takuri", + {mk(100, 101.3, 99.7, 101), mk(101, 102.3, 100.7, 102), + mk(102, 103.3, 101.7, 103), mk(100, 100.4, 97.5, 100.3)}, + 3, +1); + expect("Thrusting", + {mk(100, 110.4, 99.6, 110), mk(111, 111.3, 106.5, 107)}, 1, + -1); + expect("Two Crows", + {mk(100, 110.4, 99.6, 110), mk(112, 112.3, 107.6, 108), + mk(111, 111.3, 104.6, 105)}, + 2, -1); + expect("Concealing Baby Swallow", + {mk(110, 110.2, 103.8, 104), mk(103, 103.4, 98.6, 99), + mk(100.5, 101, 96.6, 97), mk(102, 102.3, 94.7, 95)}, + 3, +1); + expect("Kicking By Length", + {mk(103, 103.2, 99.8, 100), mk(106, 112.2, 105.8, 112)}, 1, + +1); + expect("Gap Side Side White", + {mk(100, 101.3, 99.7, 101), mk(103, 105.4, 102.5, 105), + mk(103.1, 106.3, 102.9, 106)}, + 2, +1); + expect("Piercing Line", + {mk(110, 110.5, 99.5, 100), mk(99, 106.5, 98.8, 106)}, 1, +1); + expect("Dark Cloud Cover", + {mk(100, 110.5, 99.5, 110), mk(111, 111.5, 103.5, 104)}, 1, + -1); + expect("Three White Soldiers", + {mk(100, 104.5, 99.5, 104), mk(103, 107.5, 102.5, 107), + mk(106, 110.5, 105.5, 110)}, + 2, +1); + expect("Abandoned Baby", + {mk(110, 110.5, 99.5, 100), mk(97, 97.3, 96.7, 97.05), + mk(99, 106.5, 98.5, 106)}, + 2, +1); + expect("Kicking", + {mk(105, 105.2, 99.8, 100), mk(107, 112.2, 106.8, 112)}, 1, + +1); + expect("Tristar", + {mk(100, 100.6, 99.4, 100), mk(102, 102.6, 101.4, 102), + mk(100, 100.6, 99.4, 100)}, + 2, -1); + + // 5. Bounded-oscillator range checks. + auto range_check = [&](const char* name, double lo, double hi) { + auto ind = fdi::make_indicator(name); + fdi::Lines lines = ind->compute(bars); + bool ok = true, any = false; + for (const auto& s : lines) + for (double v : s) + if (std::isfinite(v)) { + any = true; + if (v < lo - 1e-6 || v > hi + 1e-6) ok = false; + } + std::printf("range %-16s in [%g, %g]: %s\n", name, lo, hi, + ok && any ? "OK" : "MISMATCH"); + if (!ok || !any) ++crafted_fail; + }; + range_check("RSI", 0, 100); + range_check("Stochastic Fast", 0, 100); + range_check("Williams %R", -100, 0); + range_check("MFI", 0, 100); + range_check("Stochastic RSI", 0, 100); + + std::printf("\nSUMMARY: %d passed, %d failed, %d patterns not fired, " + "%d crafted/range mismatches\n", + passed, failed, nofire, crafted_fail); + for (auto& f : fail_names) std::printf(" FAIL: %s\n", f.c_str()); + for (auto& f : nofire_names) std::printf(" NOFIRE: %s\n", f.c_str()); + return (failed || crafted_fail) ? 1 : 0; +} diff --git a/tests/make_compare_data.py b/tests/make_compare_data.py new file mode 100644 index 0000000..1a36650 --- /dev/null +++ b/tests/make_compare_data.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 +"""Generate QQQ_fake_long.csv: a comparison series on the exact same +window_start grid as SPY_fake_long.csv (different ticker, price level +and walk), for testing the StockChart comparative plot.""" +import csv +import random +import sys + +src_path = sys.argv[1] if len(sys.argv) > 1 else "tests/data/SPY_fake_long.csv" +dst_path = sys.argv[2] if len(sys.argv) > 2 else "tests/data/QQQ_fake_long.csv" + +with open(src_path) as f: + rows = list(csv.DictReader(f)) + +random.seed(424242) +price = 512.0 +out = [] +for r in rows: + o = price + c = price + (random.random() - 0.475) * 2.6 + h = max(o, c) + random.random() * 0.7 + lo = min(o, c) - random.random() * 0.7 + vol = int(3000 + random.random() * 30000) + out.append( + { + "ticker": "QQQ", + "volume": vol, + "open": f"{o:.2f}", + "close": f"{c:.2f}", + "high": f"{h:.2f}", + "low": f"{lo:.2f}", + "window_start": r["window_start"], + "transactions": int(vol / 7), + } + ) + price = c + +with open(dst_path, "w", newline="") as f: + w = csv.DictWriter( + f, + fieldnames=[ + "ticker", + "volume", + "open", + "close", + "high", + "low", + "window_start", + "transactions", + ], + ) + w.writeheader() + w.writerows(out) +print(f"wrote {len(out)} QQQ bars to {dst_path}") diff --git a/tests/run_matrix.sh b/tests/run_matrix.sh new file mode 100644 index 0000000..9b77188 --- /dev/null +++ b/tests/run_matrix.sh @@ -0,0 +1,123 @@ +#!/usr/bin/env bash +# Extensive generator compile test: generate the matrix of designs, then +# compile EVERY generated project with -Wall -Wextra and run it briefly +# under a virtual display. A generated app that starts and stays up +# (killed by timeout, exit 124) counts as a run pass. +set -u +cd "$(dirname "$0")/.." + +echo "== building harness ==" +g++ -O2 -Wall -std=c++17 -o tests/gen_matrix tests/gen_matrix.cpp src/generator.cpp || exit 1 + +rm -rf tests/out +echo "== generating ==" +tests/gen_matrix tests/out || exit 1 + +pass=0; fail=0; failed="" +for d in tests/out/*/; do + name=$(basename "$d") + if ! (cd "$d" && make CXXFLAGS="-O2 -Wall -Wextra -std=c++17" >build.log 2>&1); then + echo "FAIL $name (compile) — see $d/build.log" + fail=$((fail+1)); failed="$failed $name(compile)"; continue + fi + if grep -q "warning" "$d/build.log"; then + echo "FAIL $name (warnings) — see $d/build.log" + fail=$((fail+1)); failed="$failed $name(warnings)"; continue + fi + timeout 3 xvfb-run -a "$d/app" >"$d/run.log" 2>&1 + rc=$? + if [ $rc -ne 124 ]; then + echo "FAIL $name (run exit $rc) — see $d/run.log" + fail=$((fail+1)); failed="$failed $name(run:$rc)"; continue + fi + # The Adwaita pixbuf-loader warning is VM environment noise (missing + # mime database under Xvfb), not a defect in generated code; any other + # CRITICAL/WARNING fails the case. + bad=$(grep -E "CRITICAL|WARNING \*\*" "$d/run.log" | grep -v "Could not load a pixbuf" || true) + if [ -n "$bad" ]; then + echo "FAIL $name (runtime warnings) — see $d/run.log" + fail=$((fail+1)); failed="$failed $name(runtime)"; continue + fi + echo "PASS $name" + pass=$((pass+1)) +done +echo "== candle render test ==" +if g++ -O2 -Wall -std=c++17 -o tests/out/14_stockchart/render_test \ + tests/candle_render_test.cpp tests/out/14_stockchart/StockChart_001.cpp \ + -Itests/out/14_stockchart $(pkg-config --cflags --libs gtk+-3.0) \ + 2>tests/out/14_stockchart/render_build.log; then + if timeout 10 xvfb-run -a tests/out/14_stockchart/render_test; then + echo "PASS candle-render"; pass=$((pass+1)) + else + echo "FAIL candle-render"; fail=$((fail+1)) + fi +else + echo "FAIL candle-render (build) — see tests/out/14_stockchart/render_build.log" + fail=$((fail+1)) +fi + +echo "== indicator render test ==" +if g++ -O2 -Wall -std=c++17 -o tests/out/15_stockchart_indicators/render_test \ + tests/indicator_render_test.cpp tests/out/15_stockchart_indicators/StockChart_001.cpp \ + -Itests/out/15_stockchart_indicators $(pkg-config --cflags --libs gtk+-3.0) \ + 2>tests/out/15_stockchart_indicators/render_build.log; then + if timeout 10 xvfb-run -a tests/out/15_stockchart_indicators/render_test; then + echo "PASS indicator-render"; pass=$((pass+1)) + else + echo "FAIL indicator-render"; fail=$((fail+1)) + fi +else + echo "FAIL indicator-render (build) — see tests/out/15_stockchart_indicators/render_build.log" + fail=$((fail+1)) +fi + +echo "== drawings module test ==" +if g++ -O2 -Wall -Wextra -std=c++17 -o tests/out/drawings_test \ + tests/drawings_test.cpp -I src \ + $(pkg-config --cflags --libs cairo) \ + 2>tests/out/drawings_build.log; then + if tests/out/drawings_test >tests/out/drawings_test.log 2>&1; then + echo "PASS drawings"; pass=$((pass+1)) + else + echo "FAIL drawings — see tests/out/drawings_test.log" + fail=$((fail+1)) + fi +else + echo "FAIL drawings (build) — see tests/out/drawings_build.log" + fail=$((fail+1)) +fi + +echo "== chart pro render test ==" +if g++ -O2 -Wall -std=c++17 -o tests/out/16_stockchart_pro/render_test \ + tests/chart_pro_render_test.cpp tests/out/16_stockchart_pro/StockChart_001.cpp \ + -Itests/out/16_stockchart_pro $(pkg-config --cflags --libs gtk+-3.0) \ + 2>tests/out/16_stockchart_pro/render_build.log; then + if timeout 15 xvfb-run -a tests/out/16_stockchart_pro/render_test; then + echo "PASS chart-pro-render"; pass=$((pass+1)) + else + echo "FAIL chart-pro-render"; fail=$((fail+1)) + fi +else + echo "FAIL chart-pro-render (build) — see tests/out/16_stockchart_pro/render_build.log" + fail=$((fail+1)) +fi + +echo "== chart pro screenshot (1024x768) ==" +if g++ -O2 -Wall -std=c++17 -o tests/out/16_stockchart_pro/screenshot \ + tests/chart_screenshot.cpp tests/out/16_stockchart_pro/MyWindow.cpp \ + tests/out/16_stockchart_pro/StockChart_001.cpp \ + -Itests/out/16_stockchart_pro $(pkg-config --cflags --libs gtk+-3.0) \ + 2>tests/out/16_stockchart_pro/shot_build.log; then + if timeout 20 xvfb-run -a tests/out/16_stockchart_pro/screenshot \ + tests/out/chart_pro_1024x768.png; then + echo "PASS chart-pro-screenshot"; pass=$((pass+1)) + else + echo "FAIL chart-pro-screenshot"; fail=$((fail+1)) + fi +else + echo "FAIL chart-pro-screenshot (build) — see tests/out/16_stockchart_pro/shot_build.log" + fail=$((fail+1)) +fi + +echo "== matrix: $pass passed, $fail failed ==" +[ $fail -eq 0 ] diff --git a/tools/embed_drawings.sh b/tools/embed_drawings.sh new file mode 100644 index 0000000..3700642 --- /dev/null +++ b/tools/embed_drawings.sh @@ -0,0 +1,17 @@ +#!/bin/sh +# Wrap src/drawings.h as a C++ raw-string header so the generator can +# emit it into generated projects. Usage: embed_drawings.sh IN OUT +set -e +delim="FDDRAWINGS9b2c" +if grep -q "$delim" "$1"; then + echo "embed_drawings: delimiter collision in $1" >&2 + exit 1 +fi +{ + echo "// Generated from src/drawings.h by tools/embed_drawings.sh." + echo "// Do not edit; run 'make' to regenerate." + echo "#pragma once" + printf 'static const char kDrawingsHeader[] = R"%s(\n' "$delim" + cat "$1" + printf ')%s";\n' "$delim" +} > "$2" diff --git a/tools/embed_indicators.sh b/tools/embed_indicators.sh new file mode 100644 index 0000000..c704764 --- /dev/null +++ b/tools/embed_indicators.sh @@ -0,0 +1,17 @@ +#!/bin/sh +# Wrap src/indicators.h as a C++ raw-string header so the generator can +# emit it into generated projects. Usage: embed_indicators.sh IN OUT +set -e +delim="FDINDICATORS7f3a" +if grep -q "$delim" "$1"; then + echo "embed_indicators: delimiter collision in $1" >&2 + exit 1 +fi +{ + echo "// Generated from src/indicators.h by tools/embed_indicators.sh." + echo "// Do not edit; run 'make' to regenerate." + echo "#pragma once" + printf 'static const char kIndicatorsHeader[] = R"%s(\n' "$delim" + cat "$1" + printf ')%s";\n' "$delim" +} > "$2" diff --git a/ui/designer.ui b/ui/designer.ui new file mode 100644 index 0000000..91c9e2d --- /dev/null +++ b/ui/designer.ui @@ -0,0 +1,282 @@ + + + + + + FDesigner — C++/GTK + 1150 + 720 + + + vertical + True + + + True + + + File + True + + + True + + + New + True + + + + + Open… + True + + + + + Save… + True + + + + + True + + + + + Generate C++/GTK… + True + + + + + True + + + + + Quit + True + + + + + + + + + False + True + 0 + + + + + horizontal + True + + + 170 + True + + + vertical + 4 + 6 + 6 + 6 + True + + + <b>Widgets</b> + True + True + + + + + Button + True + + + + + Label + True + + + + + Entry + True + + + + + CheckButton + True + + + + + RadioButton + True + + + + + Frame + True + + + + + ComboBox + True + + + + + ListBox + True + + + + + MenuButton + True + + + + + Canvas + True + + + + + Scale + True + + + + + Scrollbar + True + + + + + Text + True + + + + + Toplevel + True + + + + + Spreadsheet + True + + + + + StockChart + True + + + + + + + False + False + + + + + horizontal + True + + + True + + + 800 + 600 + True + + + + + True + True + + + + + vertical + 280 + 4 + 6 + 6 + 6 + True + + + <b>Properties</b> + True + True + + + + + No widget selected + True + + + + + 4 + 6 + True + + + + + False + False + + + + + True + True + + + + + True + True + 1 + + + + + True + + + False + True + 2 + + + + + +