Libraryless. Click here for Pure Java version (19044L/110K).
1 | sclass RSI extends CandleBasedIndicator<Double> { |
2 | settable int smoothingPeriod = 14; |
3 | |
4 | // For first phase |
5 | settable double upSum; |
6 | settable double downSum; |
7 | settable long steps; |
8 | |
9 | // For second phase |
10 | settable ExponentialMovingAverage up; |
11 | settable ExponentialMovingAverage down; |
12 | |
13 | settable SimpleMovingAverage postSmoother; |
14 | |
15 | // RSI value, unsmoothed |
16 | settable double rsi = Double.NaN; |
17 | |
18 | // RSI value, smoothed |
19 | settable double rsiSmoothed = Double.NaN; |
20 | |
21 | // RSI values collected as ticker |
22 | gettable TickerSequence rsiHistory = new TickerSequence("RSI"); |
23 | gettable TickerSequence rsiSmoothedHistory = new TickerSequence("RSI Smoothed"); |
24 | |
25 | Double value() { ret rsiSmoothed(); } |
26 | |
27 | // Do we have enough data for a proper value calculation? |
28 | bool complete() { |
29 | ret steps >= length(); |
30 | } |
31 | |
32 | { |
33 | length = 25; |
34 | onCandleAdded((IVF1<TradingCandle>) candle -> { |
35 | var x = candle.move(); |
36 | var u = max(x, 0.0); |
37 | var d = neg(min(x, 0.0)); |
38 | |
39 | if (!complete()) { |
40 | upSum += u; |
41 | downSum += d; |
42 | } else { |
43 | if (up == null) { |
44 | up = new ExponentialMovingAverage(length()); |
45 | down = new ExponentialMovingAverage(length()); |
46 | up.add(upSum/steps); |
47 | down.add(downSum/steps); |
48 | } else { |
49 | up().add(u); |
50 | down().add(d); |
51 | } |
52 | |
53 | var rs = div(up()!, down()!); |
54 | rsi(100-100/(1+rs)); |
55 | long time = candle.endTime().toLong(); |
56 | rsiHistory?.addIfPriceChanged(rsi, time); |
57 | if (postSmoother == null) |
58 | postSmoother = new SimpleMovingAverage(smoothingPeriod); |
59 | postSmoother.add(rsi); |
60 | rsiSmoothed(postSmoother!); |
61 | rsiSmoothedHistory?.addIfPriceChanged(rsiSmoothed, time); |
62 | } |
63 | |
64 | ++steps; |
65 | }); |
66 | } |
67 | |
68 | TickerSequence asTicker(L<TradingCandle> candles) { |
69 | feed(candles); |
70 | ret rsiHistory; |
71 | } |
72 | |
73 | void reset :: after { |
74 | resetFields(this, "upSum downSum steps up down postSmoother rsi rsiSmoothed rsiHistory rsiSmoothedHistory"); |
75 | } |
76 | } |
download show line numbers debug dex old transpilations
Travelled to 3 computer(s): elmgxqgtpvxh, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1036452 |
Snippet name: | RSI [trading indicator] |
Eternal ID of this version: | #1036452/20 |
Text MD5: | f123facea8456dad9d29343017bd86b3 |
Transpilation MD5: | 7216e75a5f3ca26a929f5fdac63f23d8 |
Author: | stefan |
Category: | javax / trading |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2024-02-17 21:52:56 |
Source code size: | 2121 bytes / 76 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 233 / 393 |
Version history: | 19 change(s) |
Referenced in: | [show references] |