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

51
LINES

< > BotCompany Repo | #1036442 // BollingerBands

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

Libraryless. Click here for Pure Java version (17719L/102K).

1  
sclass BollingerBands extends CandleBasedIndicator<DoubleRange> {
2  
  replace Candle with TradingCandle.
3  
  
4  
  // length parameter is in base class
5  
  
6  
  // The magic average + standard deviation generator
7  
  new Welford welford;
8  
  
9  
  // Probably wrong word. Defines how many times the standard deviation
10  
  // we use as the tunnel size (well, twice that, actually)
11  
  settable double deviation = 2;
12  
  
13  
  // result
14  
  settable DoubleRange range;
15  
  
16  
  // Bands collected as tickers
17  
  gettable TickerSequence upperBand = new TickerSequence("Upper Bollinger Band");
18  
  gettable TickerSequence lowerBand = new TickerSequence("Lower Bollinger Band");
19  
20  
  gettable AverageAndStandardDeviation as;
21  
  
22  
  {
23  
    onCandleRemoved(candle -> welford.remove(candle.endPrice()));
24  
25  
    onCandleAdded(candle -> {
26  
      welford.add(candle.endPrice());
27  
      
28  
      if (l(candles()) >= length) {
29  
        var bCandles = candles().asList();
30  
        //var prices = mapToDoubleArray(bCandles, ->.endPrice());
31  
        //as = averageAndStandardDeviation(prices);
32  
        as = welford!;
33  
        range(bollingerRange(as, deviation));
34  
        upperBand?.addIfPriceChanged(range.end(), candle.endTime().toLong());
35  
        lowerBand?.addIfPriceChanged(range.start(), candle.endTime().toLong());
36  
        change();
37  
      }
38  
    });
39  
  }
40  
  
41  
  L<TickerSequence> bandsAsTickers(L<TradingCandle> candles) {
42  
    feed(candles);
43  
    ret ll(upperBand, lowerBand);
44  
  }
45  
  
46  
  DoubleRange value() { ret range(); }
47  
  
48  
  void reset :: after {
49  
    resetFields(this, "welford range upperBand lowerBand as");
50  
  }
51  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1036442
Snippet name: BollingerBands
Eternal ID of this version: #1036442/26
Text MD5: cb6d7bee9dca0e84ad246ad924140696
Transpilation MD5: bbea99efb18702e680699ef730799b9c
Author: stefan
Category: javax / trading
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-12-16 06:23:57
Source code size: 1602 bytes / 51 lines
Pitched / IR pitched: No / No
Views / Downloads: 204 / 360
Version history: 25 change(s)
Referenced in: [show references]