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

68
LINES

< > BotCompany Repo | #1036489 // ADX [average directional movement index, trading indicator, dev.]

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

Libraryless. Click here for Pure Java version (17876L/103K).

persistable sclass ADX extends CandleBasedIndicator<Double> {
  // ATR is required to calculate ADX
  new ATR atr;
  
  // Smoothed moving averages
  settable SmoothedMovingAverage smaPlusDM;
  settable SmoothedMovingAverage smaMinusDM;
  settable SmoothedMovingAverage smaAbsoluteDI;
  
  settable double upMove = Double.NaN;
  settable double downMove = Double.NaN;
  settable double plusDM = Double.NaN;
  settable double minusDM = Double.NaN;
  settable double plusDI = Double.NaN;
  settable double minusDI = Double.NaN;

  // ADX value
  settable double adx = Double.NaN;
  
  // ADX values collected as ticker
  gettable TickerSequence history = new TickerSequence("ADX");

  Double value() { ret adx(); }
  
  *(int *length) {}

  {
    length = 14;
    
    onCandleAdded((IVF1<TradingCandle>) candle -> {
      atr.add(candle);
      
      if (candles().size() < 2) ret;
      var today = candle;
      var yesterday = candles().nextToLast();
      
      upMove(today.high()-yesterday.high());
      downMove(yesterday.low()-today.low());
      plusDM(upMove > downMove && upMove > 0 ? upMove : 0);
      minusDM(downMove > upMove && downMove > 0 ? downMove : 0);
      
      if (smaPlusDM == null) smaPlusDM(new SmoothedMovingAverage(length()));
      if (smaMinusDM == null) smaMinusDM(new SmoothedMovingAverage(length()));
      smaPlusDM.add(plusDM);
      smaMinusDM.add(minusDM);
      
      plusDI(100*smaPlusDM!/atr!);
      minusDI(100*smaMinusDM!/atr!);
      
      if (smaAbsoluteDI == null) smaAbsoluteDI(new SmoothedMovingAverage(length()));
      smaAbsoluteDI.add(absDiff(plusDI, minusDI));
      
      adx(100*smaAbsoluteDI!/(plusDI+minusDI));
      
      long time = candle.endTime().toLong();
      history?.addIfPriceChanged(adx, time);
    });
  }
  
  TickerSequence asTicker(L<TradingCandle> candles) {
    feed(candles);
    ret history;
  }
  
  void reset :: after {
    resetFields(this, "atr adx history smaPlusDM smaMinusDM smaAbsoluteDI upMove downMove plusDM minusDM plusDI minusDI");
  }
}

Author comment

Began life as a copy of #1036469

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1036489
Snippet name: ADX [average directional movement index, trading indicator, dev.]
Eternal ID of this version: #1036489/5
Text MD5: 1c8066e8615076ba966097a9ed8b0cea
Transpilation MD5: e2e64055a532f581e56daa7f8b8e1238
Author: stefan
Category: javax / trading
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-12-18 23:11:57
Source code size: 2104 bytes / 68 lines
Pitched / IR pitched: No / No
Views / Downloads: 147 / 220
Version history: 4 change(s)
Referenced in: [show references]