Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

63
LINES

< > BotCompany Repo | #1036145 // TradingCandlePainter

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (17441L/102K).

sclass TradingCandlePainter extends AbstractTickerPainter is IToolTipMaker {
  settable L<TradingCandle> candles;
  
  int spikeWidth = 1;
  Color shortColor = java.awt.Color.blue;
  Color longColor = java.awt.Color.yellow;
  
  public void drawOn(Graphics2D g) {
    if (empty(candles)) ret;
    
    var run = new TradingRun(candles);
    verticalRange(doubleRange(run.min(), run.max()));
    horizontalRange(doubleRange(run.startTime().unixDate(), run.endTime().unixDate()));
    
    drawPercentLines(g);

    // Draw candles
    
    var nCandles = l(candles);
    
    for i to nCandles: {
      var candle = candles.get(i);
      var x1 = xRange().start()+doubleRatio(i, nCandles)*xRange().length();
      var x2 = xRange().start()+doubleRatio(i+1, nCandles)*xRange().length();
      x2 = blend(x1, x2, 0.75);
      var xCenter = avg(x1, x2);
      var y1 = yToScreen(candle.max());
      var y2 = yToScreen(candle.min());
      var yStart = yToScreen(candle.start());
      var yEnd = yToScreen(candle.end());
      
      // draw "spikes"
      var r = doubleRectFromPoints(xCenter, y1, xCenter+spikeWidth, y2);
      fillRect(g, growRectBottom(1, toRect_round(r)), candle.color());
      
      // draw candle body
      r = doubleRectFromPoints(x1, min(yStart, yEnd), x2, max(yStart, yEnd));
      fillRect(g, growRectBottom(1, toRect_round(r)), candle.color());
    }
    
    drawPositions(g);
  }
  
  public BufferedImage render() {
    var img = super.render();
    var img2 = cloneBufferedImageWithMeta(img);
    metaSet(img2, IToolTipMaker, this);
    ret img2;
  }
  
  public swappable S getToolTip(Pt p) {
    if (!hasScale()) ret "No candles yet";
    double time = xFromScreen(p.x);
    //double price = yFromScreen(p.y);
    
    TradingCandle candle = firstThat(candles, c -> c.startTime().unixDate() >= time);
    
    if (candle == null)
      ret formatLocalDateWithSeconds(lround(time));
      
    ret str(candle);
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): elmgxqgtpvxh, mqqgnosmbjvj, wnsclhtenguj

No comments. add comment

Snippet ID: #1036145
Snippet name: TradingCandlePainter
Eternal ID of this version: #1036145/27
Text MD5: a2ec5cacea6ec9576e0e4b1d109e29af
Transpilation MD5: 586d9f2d7a052737d1e0dc9afd5e57bd
Author: stefan
Category: javax / gazelle 22
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-11-26 23:03:49
Source code size: 2012 bytes / 63 lines
Pitched / IR pitched: No / No
Views / Downloads: 71 / 217
Version history: 26 change(s)
Referenced in: [show references]