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

116
LINES

< > BotCompany Repo | #1036168 // AbstractTickerPainter

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

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

asclass AbstractTickerPainter extends ScaledDiagram is G2Drawable, MakesBufferedImage {
  settable TickerSequence ticker;
  
  // how many lines are drawn to indicate percent changes
  // (old, use priceCells instead)
  settable double percentLineDistance = 0.1;
  
  settable bool drawPercentLines = true;
  
  settable PriceCells priceCells;
  
  new L<TradingPosition> positions;
  
  new L<G2Drawable> additionalObjects;
  
  public BufferedImage render aka getBufferedImage() {
    // Make black image
    var img = blackImage(w, h);
    var g = img.createGraphics();
    drawOn(g);
    ret img;
  }
  
  void drawPercentLines(Graphics2D g) {
    if (priceCells != null) {
      int cellNumber = ifloor(priceCells.toCellNumber(verticalRange.end));
      int safety = 250;
      while (safety-- > 0) {
        double y = priceCells.fromCellNumber(cellNumber);
        if (y < verticalRange.start) break;
        var yy = iround(yToScreen(y));
        drawLine(g, 0, yy, w-1, yy, Color.gray);
        cellNumber--;
      }
    } else if (drawPercentLines && percentLineDistance != 0) {
      double y = verticalRange.end;
      int safety = 100;
      while (safety-- > 0 && y > verticalRange.start) {
        y /= 1+percentLineDistance/100;
        var yy = iround(yToScreen(y));
        drawLine(g, 0, yy, w-1, yy, Color.gray);
      }
    }
  }
  
  void drawPositions(Graphics2D g) {
    for (position : positions) {
      var time = position.openingTime();
      var price = position.openingPrice();
      var x = xToScreen(time);
      var y = yToScreen(price);
      
      //bool green = position.relativeValue() > 0;
      //bool green = position.isLong();
      //var color = colorFromHex(green ? "1da2b4" : "f1493f");
      var color = directionToCandleColor(position.relativeValue());
      
      // Mark opening point
      fillRect(g, rectAroundPt(iround(x), iround(y), 10), color);
          
      if (not(position.isOpen())) {
        time = position.closingTime();
        price = position.closingPrice();
        var x2 = xToScreen(time);
        var y2 = yToScreen(price);
        drawArrowBetweenPoints(g, toPt_round(doublePt(x, y)), toPt_round(doublePt(x2, y2)), color);
      }
    }
  }
  
  void drawAdditionalObjects(Graphics2D g) {
    for (o : additionalObjects) o.drawOn(g);
  }
  
  void addVerticalLine(double x, Color color default Color.gray) {
    additionalObjects.add(g -> drawVerticalLine(g, x, color));
  }
  
  void drawVerticalLine(Graphics2D g, double x, Color color default Color.gray) {
    int xScreen = iround(xToScreen(x));
    drawLine(g, xScreen, 0, xScreen, h-1, color);
  }
  
  void addHorizontalLine(double y, Color color default Color.gray) {
    additionalObjects.add(g -> drawHorizontalLine(g, y, color));
  }
  
  void drawHorizontalLine(Graphics2D g, double y, Color color default Color.gray) {
    int yScreen = iround(yToScreen(y));
    drawLine(g, 0, yScreen, w-1, yScreen, color);
  }
  
  DoubleRange horizontalRangeForTicker(TickerSequence ticker) {
    var timeRange = ticker.timeRange();
    if (timeRange == null) null;
    ret doubleRange(timeRange.startTime().unixDate(), timeRange.endTime().unixDate());
  }
  
  DoubleRange verticalRangeForTicker(TickerSequence ticker) {
    ret doubleRange(ticker.minPrice(), ticker.maxPrice());
  }
  
  void addTimeGrid(double minutes) {
    additionalObjects.add(g -> drawTimeGrid(g, minutes));
  }
  
  void drawTimeGrid(Graphics2D g, double minutes) {
    double ms = minutesToMS(minutes);
    assertTrue(ms >= 100);
    double time = roundUpTo(ms, horizontalRange().start);
    while (time < horizontalRange().end) {
      drawVerticalLine(g, time);
      time += ms;
    }
  }
}

Author comment

Began life as a copy of #1036145

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): elmgxqgtpvxh, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1036168
Snippet name: AbstractTickerPainter
Eternal ID of this version: #1036168/28
Text MD5: b2e30c846f4733e12aeaa9c2afc06e7a
Transpilation MD5: e06e156cccba84255ed7a31336306e4b
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-30 17:59:36
Source code size: 3809 bytes / 116 lines
Pitched / IR pitched: No / No
Views / Downloads: 78 / 198
Version history: 27 change(s)
Referenced in: [show references]