#pragma once #include "core/xframe_core.h" #include "../StockData.h" #include #include #include #include #include // StockParallelChannel — XFrame Stock Market drawing/annotation widget. // Draws a light candlestick background from an OHLCVSeries, then its // annotation in a strong colour. Attribution: XFrame. class StockParallelChannel { private: Display* display_; int screen_; Window window_; GC gc_; SStyle_Manager* styles_; int x1_, y1_, x2_, y2_; std::string name_ = "StockParallelChannel"; std::string text_ = "StockParallelChannel"; std::string style_name_; bool visible_ = true; bool enabled_ = true; bool focused_ = false; xframe::OHLCVSeries series_; struct Anchor { int bar = 0; double price = 0.0; }; std::vector anchors_; Srgb annot_ = Srgb{0,120,200}; std::string text__ = ""; std::vector> brush_; bool dragging_ = false; int drag_anchor_ = -1; int axis_w_ = 56; int axis_h_ = 20; // Getter/setter pass (Group C): render state exposed as properties. Srgb bg_color_ = Srgb{255, 255, 255}; Srgb grid_color_ = Srgb{228, 228, 228}; Srgb axis_color_ = Srgb{150, 150, 150}; Srgb candle_up_color_ = Srgb{120, 190, 120}; Srgb candle_down_color_ = Srgb{200, 120, 120}; int line_width_ = 2; public: StockParallelChannel(Display* display, int screen, Window window, GC gc, SStyle_Manager* styles, int x, int y, int width, int height) : display_(display), screen_(screen), window_(window), gc_(gc), styles_(styles), x1_(x), y1_(y), x2_(x + width), y2_(y + height) { reset_anchors(); } ~StockParallelChannel() {} void set_bounds(int x, int y, int w, int h) { x1_=x; y1_=y; x2_=x+w; y2_=y+h; } int width() { return x2_-x1_; } int height() { return y2_-y1_; } bool contains(int x, int y) { return visible_ && x>=x1_ && x<=x2_ && y>=y1_ && y<=y2_; } void set_visible(bool v) { visible_=v; } bool get_visible() { return visible_; } void set_enabled(bool e) { enabled_=e; } bool get_enabled() { return enabled_; } void set_style_name(std::string s) { style_name_=s; } void set_text(std::string t) { text_=t; text__=t; } std::string get_text() { return text_; } // Render properties (getter/setter pass, Group C): defaults preserve pre-pass rendering. void set_bg_color(Srgb c) { bg_color_ = c; } Srgb get_bg_color() { return bg_color_; } void set_grid_color(Srgb c) { grid_color_ = c; } Srgb get_grid_color() { return grid_color_; } void set_axis_color(Srgb c) { axis_color_ = c; } Srgb get_axis_color() { return axis_color_; } void set_candle_up_color(Srgb c) { candle_up_color_ = c; } Srgb get_candle_up_color() { return candle_up_color_; } void set_candle_down_color(Srgb c) { candle_down_color_ = c; } Srgb get_candle_down_color() { return candle_down_color_; } void set_line_width(int w) { line_width_ = w; } int get_line_width() { return line_width_; } void set_axis_w(int w) { axis_w_ = w; } int get_axis_w() { return axis_w_; } void set_axis_h(int h) { axis_h_ = h; } int get_axis_h() { return axis_h_; } SStyle style() { return styles_->resolve(style_name_); } void set_series(const xframe::OHLCVSeries& s) { series_=s; reset_anchors(); } const xframe::OHLCVSeries& series() const { return series_; } int bar_count() { return (int)series_.size(); } void set_anchors(int b1, double p1, int b2, double p2) { anchors_.clear(); anchors_.push_back({b1,p1}); anchors_.push_back({b2,p2}); } void set_anchors3(int b1, double p1, int b2, double p2, int b3, double p3) { anchors_.clear(); anchors_.push_back({b1,p1}); anchors_.push_back({b2,p2}); anchors_.push_back({b3,p3}); } void set_anchor(int i, int bar, double price) { if (i>=0 && i<(int)anchors_.size()) anchors_[(size_t)i]={bar,price}; } Anchor anchor(int i) const { return (i>=0 && i<(int)anchors_.size()) ? anchors_[(size_t)i] : Anchor{}; } int anchor_count() { return (int)anchors_.size(); } void set_color(Srgb c) { annot_=c; } Srgb get_color() { return annot_; } void add_point(int bar, double price) { brush_.push_back({bar,price}); } void clear_points() { brush_.clear(); } int point_count() { return (int)brush_.size(); } void reset_anchors() { anchors_.clear(); if (series_.empty()) { anchors_.push_back({0,100.0}); anchors_.push_back({10,100.0}); anchors_.push_back({20,100.0}); return; } int n=(int)series_.size(); double lo=series_[0].low, hi=series_[0].high; for (auto& b: series_) { lo=std::min(lo,b.low); hi=std::max(hi,b.high); } int b1=std::min(5,n-1), b2=std::min(25,n-1), b3=std::min(15,n-1); anchors_.push_back({b1, series_[(size_t)b1].close}); anchors_.push_back({b2, series_[(size_t)b2].close}); anchors_.push_back({b3, (hi+lo)/2.0}); // Fib widgets use anchors 0/1 as high/low; pitchfork uses all three. if (!series_.empty()) { anchors_[2].price=anchors_[0].price+(hi-lo)*0.15; anchors_[2].bar=anchors_[1].bar; } } int plot_x() { return x1_+axis_w_; } int plot_y() { return y1_+6; } int plot_w() { return width()-axis_w_-8; } int plot_h() { return height()-axis_h_-12; } double item_w() { double c=series_.empty()?1.0:(double)series_.size(); return plot_w()/c; } int x_center(int i) { return plot_x()+(int)((i+0.5)*item_w()); } int x_at_bar(int i) { return plot_x()+(int)(i*item_w()); } void y_range(double& lo, double& hi) { lo=1e300; hi=-1e300; for (auto& b: series_) { lo=std::min(lo,b.low); hi=std::max(hi,b.high); } if (lo>hi) { lo=0.0; hi=1.0; return; } double pad=(hi-lo)*0.06+1e-9; lo-=pad; hi+=pad; } int y_at(double v, double lo, double hi) { return plot_y()+(int)((hi-v)/(hi-lo)*plot_h()); } double value_at_y(int y, double lo, double hi) { return hi-(double)(y-plot_y())/plot_h()*(hi-lo); } int bar_at_x(int x) { int b=(int)((x-plot_x())/item_w()); return std::max(0,std::min(std::max(0,(int)series_.size()-1),b)); } static std::string fmt(double v) { std::ostringstream o; o.precision(2); o<get_color_Srgb(b.close>=b.open ? candle_up_color_ : candle_down_color_); XSetForeground(display_,gc_,c); XDrawLine(display_,window_,gc_,cx,y_at(b.high,lo,hi),cx,y_at(b.low,lo,hi)); int top=y_at(std::max(b.open,b.close),lo,hi), bot=y_at(std::min(b.open,b.close),lo,hi); XFillRectangle(display_,window_,gc_,cx-bw/2,top,bw,std::max(1,bot-top)); } } void draw() { if (!visible_) return; SStyle st=style(); unsigned long white=styles_->get_color_Srgb(bg_color_); unsigned long grid=styles_->get_color_Srgb(grid_color_); unsigned long axis_col=styles_->get_color_Srgb(axis_color_); XSetForeground(display_,gc_,white); XFillRectangle(display_,window_,gc_,x1_,y1_,width(),height()); double lo=0.0, hi=1.0; y_range(lo,hi); int px=plot_x(), py=plot_y(), pw=plot_w(), ph=plot_h(); double step=(hi-lo)/5.0; if(!(step>0)) step=1.0; for (int k=0;k<=5;++k) { double v=lo+k*step; int gy=y_at(v,lo,hi); XSetForeground(display_,gc_,grid); XDrawLine(display_,window_,gc_,px,gy,px+pw,gy); xf::draw_text_at(display_,window_,gc_,st,px-xf::text_width(st,fmt(v))-6,gy+4,fmt(v)); } draw_candles(lo,hi); unsigned long acol=styles_->get_color_Srgb(annot_); auto X=[&](int bar){ return x_center(bar); }; auto Y=[&](double v){ return y_at(v,lo,hi); }; (void)X; (void)Y; (void)px; (void)py; (void)pw; (void)ph; // Flag 13: set_anchors() leaves only 2 anchors, so anchors_[2] // is out of bounds (aborted without -DNDEBUG). With 2 anchors // the outer channel line falls back to the midline. double p0=anchors_[0].price, p1=anchors_[1].price, mid=(p0+p1)/2.0; double off=anchors_.size()>2?anchors_[2].price:mid; int b0=anchors_[0].bar, b1=anchors_[1].bar; double slope=(b1!=b0)?(p1-p0)/(b1-b0):0.0; auto line_at=[&](double base){ double v0=base+slope*(0-b0), v1=base+slope*((int)series_.size()-1-b0); return std::pair(v0,v1); }; XSetForeground(display_,gc_,acol); xf::set_line_width(display_, gc_, line_width_); auto c0=line_at(p0); XDrawLine(display_,window_,gc_,px,Y(c0.first),px+pw,Y(c0.second)); auto cm=line_at(mid); set_dash(true); XDrawLine(display_,window_,gc_,px,Y(cm.first),px+pw,Y(cm.second)); set_dash(false); if (anchors_.size()>2) { auto c2=line_at(off); XDrawLine(display_,window_,gc_,px,Y(c2.first),px+pw,Y(c2.second)); } xf::set_line_width(display_,gc_,1); for (int i=0;i<3 && i<(int)anchors_.size();++i){ int cx=X(anchors_[i].bar), cy=Y(anchors_[i].price); XFillArc(display_,window_,gc_,cx-4,cy-4,8,8,0,360*64); } XSetForeground(display_,gc_,axis_col); XDrawRectangle(display_,window_,gc_,px,py,pw,ph); XSetForeground(display_,gc_,st.lineColor_); XDrawRectangle(display_,window_,gc_,x1_,y1_,width(),height()); xf::draw_text_at(display_,window_,gc_,st,x1_+8,y1_+14,name_); XFlush(display_); } bool on_button_press(int x, int y, unsigned int button) { if (!contains(x,y)||!enabled_) return false; focused_=true; if (button==1) { double lo=0.0, hi=1.0; y_range(lo,hi); int bar=bar_at_x(x); double price=value_at_y(y,lo,hi); if (!brush_.empty() || name_=="StockBrush") { add_point(bar,price); dragging_=true; drag_anchor_=-1; return true; } int best=-1; double bd=1e300; for (size_t i=0;i=0) { anchors_[(size_t)best]={bar,price}; dragging_=true; drag_anchor_=best; } return true; } return false; } int X_dummy(size_t i) { return x_center(anchors_[i].bar); } bool on_button_release(int x, int y, unsigned int button) { (void)x; (void)y; if (button==1&&dragging_) { dragging_=false; drag_anchor_=-1; return true; } return false; } bool on_motion(int x, int y) { if (!enabled_) return false; if (dragging_ && drag_anchor_>=0) { double lo=0.0, hi=1.0; y_range(lo,hi); anchors_[(size_t)drag_anchor_]={bar_at_x(x), value_at_y(y,lo,hi)}; return true; } if (dragging_ && name_=="StockBrush") { double lo=0.0, hi=1.0; y_range(lo,hi); add_point(bar_at_x(x), value_at_y(y,lo,hi)); return true; } return true; } bool on_key(KeySym key, const char* str) { (void)str; if (!focused_||!enabled_) return false; double lo=0.0, hi=1.0; y_range(lo,hi); double step=(hi-lo)*0.02; switch (key) { case XK_Left: for (auto& a: anchors_) a.bar=std::max(0,a.bar-1); return true; case XK_Right: for (auto& a: anchors_) a.bar=std::min(std::max(0,(int)series_.size()-1),a.bar+1); return true; case XK_Up: for (auto& a: anchors_) a.price+=step; return true; case XK_Down: for (auto& a: anchors_) a.price-=step; return true; case XK_r: case XK_R: case XK_Home: reset_anchors(); return true; } return false; } };