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

1  
sclass Supertrend extends CandleBasedIndicator<Double> {
2  
  replace Candle with TradingCandle.
3  
  
4  
  // length parameter is in base class
5  
  
6  
  // We need ATR for the calculation
7  
  gettable ATR atr;
8  
  
9  
  settable double multiplier = 3;
10  
  
11  
  settable DoubleRange bands;
12  
  
13  
  // The final value
14  
  settable double supertrend = Double.NaN;
15  
  
16  
  // The buy/sell signal (-1/0/1)
17  
  settable int signal;
18  
  
19  
  // Final bands collected as tickers
20  
  gettable TickerSequence upperBandHistory = new TickerSequence("Final upper band");
21  
  gettable TickerSequence lowerBandHistory = new TickerSequence("Final lower band");
22  
  gettable TickerSequence basicUpperBandHistory = new TickerSequence("Basic upper band");
23  
  gettable TickerSequence basicLowerBandHistory = new TickerSequence("Basic lower band");
24  
  gettable TickerSequence supertrendHistory = new TickerSequence("Supertrend");
25  
  gettable TickerSequence signalHistory = new TickerSequence("Signal");
26  
  
27  
  int candlesNeeded() { ret 2; }
28  
29  
  {
30  
    length = 10;
31  
    
32  
    onCandleAdded(candle -> {
33  
      if (atr == null)
34  
        atr = new ATR(length);
35  
      atr().add(candle);
36  
      
37  
      var atrValue = atr!;
38  
      if (isNaN(atrValue)) ret;
39  
      
40  
      Candle prev = candles().nextToLast();
41  
      double hl2 = candle.hl2();
42  
      double basicUpperBand = hl2+multiplier*atrValue;
43  
      double basicLowerBand = hl2-multiplier*atrValue;
44  
      
45  
      var previousBands = bands;
46  
      
47  
      double finalUpperBand = previousBands == null
48  
        || basicUpperBand < previousBands.end
49  
        || prev.close() > previousBands.end
50  
          ? basicUpperBand
51  
          : previousBands.end;
52  
        
53  
      double finalLowerBand = previousBands == null
54  
        || basicLowerBand > previousBands.start
55  
        || prev.close() < previousBands.start
56  
          ? basicLowerBand
57  
          : previousBands.start;
58  
          
59  
      if (previousBands == null || supertrend == previousBands.end)
60  
        supertrend(candle.close() < finalUpperBand ? finalUpperBand : finalLowerBand);
61  
      else
62  
        supertrend(candle.close() > finalLowerBand ? finalLowerBand : finalUpperBand);
63  
        
64  
      // Literature suggests candle.close() instead of hl2
65  
      signal(sign(candle.close()-supertrend));
66  
      //signal(sign(hl2-supertrend));
67  
      
68  
      bands(doubleRange(finalLowerBand, finalUpperBand));
69  
      
70  
      upperBandHistory?.addIfPriceChanged(finalUpperBand, candle.endTime().toLong());
71  
      lowerBandHistory?.addIfPriceChanged(finalLowerBand, candle.endTime().toLong());
72  
      basicUpperBandHistory?.addIfPriceChanged(basicUpperBand, candle.endTime().toLong());
73  
      basicLowerBandHistory?.addIfPriceChanged(basicLowerBand, candle.endTime().toLong());
74  
      supertrendHistory?.add(supertrend, candle.endTime().toLong());
75  
      signalHistory?.add(signal, candle.endTime().toLong());
76  
      change();
77  
    });
78  
  }
79  
  
80  
  L<TickerSequence> bandsAsTickers(L<TradingCandle> candles) {
81  
    feed(candles);
82  
    ret ll(upperBandHistory, lowerBandHistory);
83  
  }
84  
  
85  
  Double value() { ret supertrend(); }
86  
  
87  
  void reset :: after {
88  
    resetFields(this, "previousBands bands upperBandHistory lowerBandHistory basicUpperBandHistory basicLowerBandHistory supertrendHistory supertrend atr signal");
89  
  }
90  
}

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: 203 / 328
Version history: 22 change(s)
Referenced in: [show references]