1 | sclass RSI extends CandleBasedIndicator<Double> { |
2 | settable int smoothingPeriod; |
3 | settable SmoothedMovingAverage up; |
4 | settable SmoothedMovingAverage down; |
5 | settable double rsi = Double.NaN; |
6 | |
7 | // RSI values collected as ticker |
8 | gettable TickerSequence rsiHistory = new TickerSequence("RSI"); |
9 | |
10 | Double value() { ret rsi(); } |
11 | |
12 | // Do we have enough data for a proper value calculation? |
13 | bool complete() { |
14 | ret up != null && up.steps() >= length(); |
15 | } |
16 | |
17 | { |
18 | length = 25; |
19 | onCandleAdded((IVF1<TradingCandle>) candle -> { |
20 | if (isNull(up())) init(); |
21 | var x = candle.move(); |
22 | var u = max(x, 0.0); |
23 | var d = neg(min(x, 0.0)); |
24 | up().add(u); |
25 | down().add(d); |
26 | if (complete()) { |
27 | var rs = div(up()!, down()!); |
28 | rsi(100-100/(1+rs)); |
29 | rsiHistory?.addIfPriceChanged(rsi, candle.endTime().toLong()); |
30 | } |
31 | }); |
32 | } |
33 | |
34 | void init() { |
35 | up = new SmoothedMovingAverage(this.length()); |
36 | down = new SmoothedMovingAverage(this.length()); |
37 | } |
38 | |
39 | TickerSequence asTicker(L<TradingCandle> candles) { |
40 | feed(candles); |
41 | ret rsiHistory; |
42 | } |
43 | } |
Began life as a copy of #1036452
download show line numbers debug dex old transpilations
Travelled to 2 computer(s): mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1036460 |
Snippet name: | RSI backup [trading indicator, probably wrong formula] |
Eternal ID of this version: | #1036460/1 |
Text MD5: | bc6e51c5180d041727212fca595a732f |
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 02:04:55 |
Source code size: | 1153 bytes / 43 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 136 / 145 |
Referenced in: | [show references] |