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

sclass BollingerBands extends CandleBasedIndicator<DoubleRange> {
  replace Candle with TradingCandle.
  
  // length parameter is in base class
  
  // The magic average + standard deviation generator
  new Welford welford;
  
  // Probably wrong word. Defines how many times the standard deviation
  // we use as the tunnel size (well, twice that, actually)
  settable double deviation = 2;
  
  // result
  settable DoubleRange range;
  
  // Bands collected as tickers
  gettable TickerSequence upperBand = new TickerSequence("Upper Bollinger Band");
  gettable TickerSequence lowerBand = new TickerSequence("Lower Bollinger Band");

  gettable AverageAndStandardDeviation as;
  
  {
    onCandleRemoved(candle -> welford.remove(candle.endPrice()));

    onCandleAdded(candle -> {
      welford.add(candle.endPrice());
      
      if (l(candles()) >= length) {
        var bCandles = candles().asList();
        //var prices = mapToDoubleArray(bCandles, ->.endPrice());
        //as = averageAndStandardDeviation(prices);
        as = welford!;
        range(bollingerRange(as, deviation));
        upperBand?.addIfPriceChanged(range.end(), candle.endTime().toLong());
        lowerBand?.addIfPriceChanged(range.start(), candle.endTime().toLong());
        change();
      }
    });
  }
  
  L<TickerSequence> bandsAsTickers(L<TradingCandle> candles) {
    feed(candles);
    ret ll(upperBand, lowerBand);
  }
  
  DoubleRange value() { ret range(); }
  
  void reset :: after {
    resetFields(this, "welford range upperBand lowerBand as");
  }
}

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