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

98
LINES

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

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

Libraryless. Click here for Pure Java version (10759L/60K).

1  
sclass TradingCandle {
2  
  // min, max, start and end prices
3  
  settable double min = infinity();
4  
  settable double max;
5  
  settable double start;
6  
  settable double end;
7  
  
8  
  settable Timestamp startTime;
9  
  settable Timestamp endTime;
10  
  settable long projectedEndTime;
11  
  settable bool ongoing;
12  
  
13  
  settable S coin;
14  
  
15  
  double openingPrice aka startingPrice aka open() { ret start; }
16  
  double closingPrice aka endPrice aka close() { ret end; }
17  
  
18  
  selfType open(double x) { ret start(x); }
19  
  selfType close(double x) { ret end(x); }
20  
  selfType high(double x) { ret max(x); }
21  
  selfType low(double x) { ret min(x); }
22  
  
23  
  bool isGreen() {
24  
    ret end > start;
25  
  }
26  
  
27  
  bool isRed() {
28  
    ret end < start;
29  
  }
30  
  
31  
  bool isWhite() {
32  
    ret end == start;
33  
  }
34  
  
35  
  S colorText() {
36  
    ret isGreen() ? "green"
37  
      : isRed() ? "red"
38  
      : "white";
39  
  }
40  
  
41  
  double durationInSeconds() {
42  
    ret toSeconds(endTime.minus(startTime));
43  
  }
44  
  
45  
  void addValue(double price, Timestamp timestamp) {
46  
    if (price < min) min = price;
47  
    if (price > max) max = price;
48  
    end = price;
49  
    endTime = timestamp;
50  
    if (startTime == null) {
51  
      startTime = timestamp;
52  
      start = price;
53  
    }
54  
  }
55  
  
56  
  S myType() { ret "candle"; }
57  
  
58  
  toString {
59  
    S text = firstToUpper(colorText()) + " " + myType();
60  
    if (startTime != null) {
61  
      text += " starting " + startTime + ", duration " +
62  
        iround(toSeconds(endTime.minus(startTime))) + "s";
63  
      text += ", starting price " + formatPrice(start) + ", end price " + formatPrice(end);
64  
      text += ", min " + formatPrice(min) + ", max " + formatPrice(max);
65  
    }
66  
    ret text;
67  
  }
68  
  
69  
  double changeRatio() {
70  
    ret doubleRatio(end, start);
71  
  }
72  
  
73  
  public TradingCandle clone() {
74  
    ret shallowClone(this);
75  
  }
76  
  
77  
  Color color() {
78  
    ret directionToCandleColor(end-start);
79  
  }
80  
  
81  
  double hl2 aka mid() { ret avg(min, max); }
82  
  double delta aka move() { ret end-start; }
83  
  double high() { ret max; }
84  
  double low() { ret min; }
85  
  
86  
  selfType startTime(long startTime) { ret startTime(toTimestamp(startTime)); }
87  
  selfType endTime(long endTime) { ret endTime(toTimestamp(endTime)); }
88  
  
89  
  Timestamp openingTime() { ret startTime; }
90  
  Timestamp closingTime() { ret endTime; }
91  
  
92  
  Timestamp middleTime() { ret new Timestamp((startTime.toLong()+endTime.toLong())/2); }
93  
  
94  
  // whichever one is later
95  
  long endOrProjectedEndTime() {
96  
    ret main max(endTime().unixDate(), projectedEndTime);
97  
  }
98  
}

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/31
Text MD5: 52ff03c3d354664bdc9469fa38b49ec8
Transpilation MD5: c34b04b317bde129fa4b63382f0c3662
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-03-20 21:42:36
Source code size: 2541 bytes / 98 lines
Pitched / IR pitched: No / No
Views / Downloads: 253 / 451
Version history: 30 change(s)
Referenced in: [show references]