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

72
LINES

< > BotCompany Repo | #1036115 // TradingCandle - a candle in a trading view

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

Libraryless. Click here for Pure Java version (9742L/54K).

sclass TradingCandle {
  // min, max, start and end prices
  settable double min = infinity();
  settable double max;
  settable double start;
  settable double end;
  
  settable Timestamp startTime;
  settable Timestamp endTime;
  settable bool ongoing;
  
  settable S coin;
  
  bool isGreen() {
    ret end > start;
  }
  
  bool isRed() {
    ret end < start;
  }
  
  bool isWhite() {
    ret end == start;
  }
  
  S colorText() {
    ret isGreen() ? "green"
      : isRed() ? "red"
      : "white";
  }
  
  double durationInSeconds() {
    ret toSeconds(endTime.minus(startTime));
  }
  
  void addValue(double price, Timestamp timestamp) {
    if (price < min) min = price;
    if (price > max) max = price;
    end = price;
    endTime = timestamp;
    if (startTime == null) {
      startTime = timestamp;
      start = price;
    }
  }
  
  S myType() { ret "candle"; }
  
  toString {
    S text = firstToUpper(colorText()) + " " + myType();
    if (startTime != null) {
      text += " starting " + startTime + ", duration " +
        iround(toSeconds(endTime.minus(startTime))) + "s";
      text += ", starting price " + start + ", end price " + end;
    }
    ret text;
  }
  
  double changeRatio() {
    ret doubleRatio(end, start);
  }
  
  public TradingCandle clone() {
    ret shallowClone(this);
  }
  
  Color color() {
    ret isRed() ? colorFromHex("f1493f")
      : isGreen() ? colorFromHex("1da2b4")
      : Color.white;
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): elmgxqgtpvxh, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1036115
Snippet name: TradingCandle - a candle in a trading view
Eternal ID of this version: #1036115/14
Text MD5: cc7f0abecf5431970e9bbeb8cec2609d
Transpilation MD5: e8f1119c85647a264d15c4fc20d6c971
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-09-28 19:58:52
Source code size: 1527 bytes / 72 lines
Pitched / IR pitched: No / No
Views / Downloads: 32 / 102
Version history: 13 change(s)
Referenced in: [show references]