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

90
LINES

< > BotCompany Repo | #1036470 // Supertrend - Trading indicator by Olivier Seban [OK]

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

Libraryless. Click here for Pure Java version (17911L/104K).

sclass Supertrend extends CandleBasedIndicator<Double> {
  replace Candle with TradingCandle.
  
  // length parameter is in base class
  
  // We need ATR for the calculation
  gettable ATR atr;
  
  settable double multiplier = 3;
  
  settable DoubleRange bands;
  
  // The final value
  settable double supertrend = Double.NaN;
  
  // The buy/sell signal (-1/0/1)
  settable int signal;
  
  // Final bands collected as tickers
  gettable TickerSequence upperBandHistory = new TickerSequence("Final upper band");
  gettable TickerSequence lowerBandHistory = new TickerSequence("Final lower band");
  gettable TickerSequence basicUpperBandHistory = new TickerSequence("Basic upper band");
  gettable TickerSequence basicLowerBandHistory = new TickerSequence("Basic lower band");
  gettable TickerSequence supertrendHistory = new TickerSequence("Supertrend");
  gettable TickerSequence signalHistory = new TickerSequence("Signal");
  
  int candlesNeeded() { ret 2; }

  {
    length = 10;
    
    onCandleAdded(candle -> {
      if (atr == null)
        atr = new ATR(length);
      atr().add(candle);
      
      var atrValue = atr!;
      if (isNaN(atrValue)) ret;
      
      Candle prev = candles().nextToLast();
      double hl2 = candle.hl2();
      double basicUpperBand = hl2+multiplier*atrValue;
      double basicLowerBand = hl2-multiplier*atrValue;
      
      var previousBands = bands;
      
      double finalUpperBand = previousBands == null
        || basicUpperBand < previousBands.end
        || prev.close() > previousBands.end
          ? basicUpperBand
          : previousBands.end;
        
      double finalLowerBand = previousBands == null
        || basicLowerBand > previousBands.start
        || prev.close() < previousBands.start
          ? basicLowerBand
          : previousBands.start;
          
      if (previousBands == null || supertrend == previousBands.end)
        supertrend(candle.close() < finalUpperBand ? finalUpperBand : finalLowerBand);
      else
        supertrend(candle.close() > finalLowerBand ? finalLowerBand : finalUpperBand);
        
      // Literature suggests candle.close() instead of hl2
      signal(sign(candle.close()-supertrend));
      //signal(sign(hl2-supertrend));
      
      bands(doubleRange(finalLowerBand, finalUpperBand));
      
      upperBandHistory?.addIfPriceChanged(finalUpperBand, candle.endTime().toLong());
      lowerBandHistory?.addIfPriceChanged(finalLowerBand, candle.endTime().toLong());
      basicUpperBandHistory?.addIfPriceChanged(basicUpperBand, candle.endTime().toLong());
      basicLowerBandHistory?.addIfPriceChanged(basicLowerBand, candle.endTime().toLong());
      supertrendHistory?.add(supertrend, candle.endTime().toLong());
      signalHistory?.add(signal, candle.endTime().toLong());
      change();
    });
  }
  
  L<TickerSequence> bandsAsTickers(L<TradingCandle> candles) {
    feed(candles);
    ret ll(upperBandHistory, lowerBandHistory);
  }
  
  Double value() { ret supertrend(); }
  
  void reset :: after {
    resetFields(this, "previousBands bands upperBandHistory lowerBandHistory basicUpperBandHistory basicLowerBandHistory supertrendHistory supertrend atr signal");
  }
}

Author comment

Began life as a copy of #1036442

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1036470
Snippet name: Supertrend - Trading indicator by Olivier Seban [OK]
Eternal ID of this version: #1036470/23
Text MD5: 45c50ee348afb2482a9e9c7364e2ed2b
Transpilation MD5: be44a3ecdb68b32f957f7645164ae2be
Author: stefan
Category: javax / trading
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-12-23 22:19:15
Source code size: 3295 bytes / 90 lines
Pitched / IR pitched: No / No
Views / Downloads: 202 / 326
Version history: 22 change(s)
Referenced in: #1003674 - Standard Classes + Interfaces (LIVE continued in #1034167)