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).

// A is the value type we get for each candle
asclass CandleBasedIndicator<A> extends MetaWithChangeListeners {
  replace Candle with TradingCandle.
  
  // Number of candles we need to look back to
  settable int length = 20;
  
  event candleAdded(Candle candle);
  event candleRemoved(Candle candle);
  
  // get current indicator value
  abstract A value aka get();
  
  simplyCached SimpleCircularBuffer<Candle> candles() {
    ret new SimpleCircularBuffer(candlesNeeded());
  }
  
  int candlesNeeded() { ret length; }
  
  Candle lastCandle() { ret candles().last(); }
  
  selfType feed(TradingCandleMaker candleMaker) {
    if (candleMaker != null)
      // TODO: cut list down first
      for (candle : candleMaker.candles())
        add(candle);
    this;
  }
  
  selfType add aka feed(Iterable<TradingCandle> candles) {
    fOr (candle : candles)
      add(candle);
    this;
  }
  
  // Can add candles twice, will automatically ignore the second time
  selfType add aka feed(TradingCandle candle) {
    if (candle.ongoing()) this;
    if (lastCandle() != null && lessOrEq(candle.startTime, lastCandle().startTime))
      this;
      
    while (candles().size() > max(length-1, 0))
      candleRemoved(candles().remove());
    candles().add(candle);
    
    candleAdded(candle);
    this;
  }
  
  void reset {
    resetFields(this, "candles_cache");
  }
  
  selfType emptyClone() {
    var clone = shallowNonTransientClone(this);
    clone.reset();
    ret clone;
  }
}

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: 244 / 392
Version history: 16 change(s)
Referenced in: [show references]