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

57
LINES

< > BotCompany Repo | #1036559 // MAIndicator [simple moving average, trading indicator]

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

Transpiled version (19023L) is out of date.

1  
persistable sclass MAIndicator extends CandleBasedIndicator<Double> is IntegrityCheckable {
2  
  // The MA calculator
3  
  settable SimpleMovingAverage ma;
4  
  
5  
  // MA values collected as ticker
6  
  settableWithVar TickerSequence history = new TickerSequence("MA");
7  
  
8  
  // MA value
9  
  double value = Double.NaN;
10  
  
11  
  // How many historic items to keep
12  
  settable int maxHistoryLength = 1000;
13  
  
14  
  Double value() { ret value; }
15  
16  
  *(int *length) {}
17  
18  
  {
19  
    length = 7;
20  
    
21  
    onCandleAdded((IVF1<TradingCandle>) candle -> {
22  
      if (ma == null) makeMA();
23  
      
24  
      ma.add(candle.close());
25  
      value = ma.complete() ? ma! : Double.NaN;
26  
      if (!isNaN(value)) {
27  
        long time = candle.endTime().toLong();
28  
        history?.addIfPriceChanged(value, time);
29  
        shortenHistory();
30  
      }
31  
    });
32  
  }
33  
  
34  
  TickerSequence asTicker(L<TradingCandle> candles) {
35  
    feed(candles);
36  
    ret history;
37  
  }
38  
  
39  
  void makeMA {
40  
    ma = new SimpleMovingAverage(length);
41  
  }
42  
  
43  
  void reset :: after {
44  
    resetFields(this, "value history");
45  
    makeMA();
46  
  }
47  
  
48  
  public void integrityCheck {
49  
    history?.integrityCheck();
50  
  }
51  
  
52  
  void shortenHistory {
53  
    int increment = 10;
54  
    if (history != null && history.size() > maxHistoryLength+increment)
55  
      history(history.subSequence(history.size()-maxHistoryLength));
56  
  }
57  
}

Author comment

Began life as a copy of #1036532

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): mqqgnosmbjvj, wnsclhtenguj

No comments. add comment

Snippet ID: #1036559
Snippet name: MAIndicator [simple moving average, trading indicator]
Eternal ID of this version: #1036559/7
Text MD5: 3e11eeec306052ffb833566511b4d7e8
Author: stefan
Category: javax / trading
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-03-28 19:37:56
Source code size: 1372 bytes / 57 lines
Pitched / IR pitched: No / No
Views / Downloads: 166 / 248
Version history: 6 change(s)
Referenced in: [show references]