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

59
LINES

< > BotCompany Repo | #1036446 // CandleBasedIndicator

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

Libraryless. Click here for Pure Java version (18685L/108K).

1  
// A is the value type we get for each candle
2  
asclass CandleBasedIndicator<A> extends MetaWithChangeListeners {
3  
  replace Candle with TradingCandle.
4  
  
5  
  // Number of candles we need to look back to
6  
  settable int length = 20;
7  
  
8  
  event candleAdded(Candle candle);
9  
  event candleRemoved(Candle candle);
10  
  
11  
  // get current indicator value
12  
  abstract A value aka get();
13  
  
14  
  simplyCached SimpleCircularBuffer<Candle> candles() {
15  
    ret new SimpleCircularBuffer(candlesNeeded());
16  
  }
17  
  
18  
  int candlesNeeded() { ret length; }
19  
  
20  
  Candle lastCandle() { ret candles().last(); }
21  
  
22  
  selfType feed(TradingCandleMaker candleMaker) {
23  
    if (candleMaker != null)
24  
      // TODO: cut list down first
25  
      for (candle : candleMaker.candles())
26  
        add(candle);
27  
    this;
28  
  }
29  
  
30  
  selfType add aka feed(Iterable<TradingCandle> candles) {
31  
    fOr (candle : candles)
32  
      add(candle);
33  
    this;
34  
  }
35  
  
36  
  // Can add candles twice, will automatically ignore the second time
37  
  selfType add aka feed(TradingCandle candle) {
38  
    if (candle.ongoing()) this;
39  
    if (lastCandle() != null && lessOrEq(candle.startTime, lastCandle().startTime))
40  
      this;
41  
      
42  
    while (candles().size() > max(length-1, 0))
43  
      candleRemoved(candles().remove());
44  
    candles().add(candle);
45  
    
46  
    candleAdded(candle);
47  
    this;
48  
  }
49  
  
50  
  void reset {
51  
    resetFields(this, "candles_cache");
52  
  }
53  
  
54  
  selfType emptyClone() {
55  
    var clone = shallowNonTransientClone(this);
56  
    clone.reset();
57  
    ret clone;
58  
  }
59  
}

Author comment

Began life as a copy of #1036443

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1036446
Snippet name: CandleBasedIndicator
Eternal ID of this version: #1036446/17
Text MD5: 81176d4657b89ebc90f10e1ae0b33e27
Transpilation MD5: 7c072bf0a0037aeb9f4fcfaf87d30574
Author: stefan
Category: javax / trading
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-03-28 19:33:05
Source code size: 1545 bytes / 59 lines
Pitched / IR pitched: No / No
Views / Downloads: 175 / 305
Version history: 16 change(s)
Referenced in: [show references]