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

76
LINES

< > BotCompany Repo | #1036452 // RSI [trading indicator]

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

Libraryless. Click here for Pure Java version (19044L/110K).

sclass RSI extends CandleBasedIndicator<Double> {
  settable int smoothingPeriod = 14;
  
  // For first phase
  settable double upSum;
  settable double downSum;
  settable long steps;
  
  // For second phase
  settable ExponentialMovingAverage up;
  settable ExponentialMovingAverage down;
  
  settable SimpleMovingAverage postSmoother;
  
  // RSI value, unsmoothed
  settable double rsi = Double.NaN;
  
  // RSI value, smoothed
  settable double rsiSmoothed = Double.NaN;
  
  // RSI values collected as ticker
  gettable TickerSequence rsiHistory = new TickerSequence("RSI");
  gettable TickerSequence rsiSmoothedHistory = new TickerSequence("RSI Smoothed");

  Double value() { ret rsiSmoothed(); }

  // Do we have enough data for a proper value calculation?
  bool complete() {
    ret steps >= length();
  }
  
  {
    length = 25;
    onCandleAdded((IVF1<TradingCandle>) candle -> {
      var x = candle.move();
      var u = max(x, 0.0);
      var d = neg(min(x, 0.0));
      
      if (!complete()) {
        upSum += u;
        downSum += d;
      } else {
        if (up == null) {
          up = new ExponentialMovingAverage(length());
          down = new ExponentialMovingAverage(length());
          up.add(upSum/steps);
          down.add(downSum/steps);
        } else {
          up().add(u);
          down().add(d);
        }
        
        var rs = div(up()!, down()!);
        rsi(100-100/(1+rs));
        long time = candle.endTime().toLong();
        rsiHistory?.addIfPriceChanged(rsi, time);
        if (postSmoother == null)
          postSmoother = new SimpleMovingAverage(smoothingPeriod);
        postSmoother.add(rsi);
        rsiSmoothed(postSmoother!);
        rsiSmoothedHistory?.addIfPriceChanged(rsiSmoothed, time);
      }
      
      ++steps;
    });
  }
  
  TickerSequence asTicker(L<TradingCandle> candles) {
    feed(candles);
    ret rsiHistory;
  }
  
  void reset :: after {
    resetFields(this, "upSum downSum steps up down postSmoother rsi rsiSmoothed rsiHistory rsiSmoothedHistory");
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): elmgxqgtpvxh, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1036452
Snippet name: RSI [trading indicator]
Eternal ID of this version: #1036452/20
Text MD5: f123facea8456dad9d29343017bd86b3
Transpilation MD5: 7216e75a5f3ca26a929f5fdac63f23d8
Author: stefan
Category: javax / trading
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2024-02-17 21:52:56
Source code size: 2121 bytes / 76 lines
Pitched / IR pitched: No / No
Views / Downloads: 231 / 391
Version history: 19 change(s)
Referenced in: [show references]