Libraryless. Click here for Pure Java version (17876L/103K).
1 | persistable sclass ADX extends CandleBasedIndicator<Double> { |
2 | // ATR is required to calculate ADX |
3 | new ATR atr; |
4 | |
5 | // Smoothed moving averages |
6 | settable SmoothedMovingAverage smaPlusDM; |
7 | settable SmoothedMovingAverage smaMinusDM; |
8 | settable SmoothedMovingAverage smaAbsoluteDI; |
9 | |
10 | settable double upMove = Double.NaN; |
11 | settable double downMove = Double.NaN; |
12 | settable double plusDM = Double.NaN; |
13 | settable double minusDM = Double.NaN; |
14 | settable double plusDI = Double.NaN; |
15 | settable double minusDI = Double.NaN; |
16 | |
17 | // ADX value |
18 | settable double adx = Double.NaN; |
19 | |
20 | // ADX values collected as ticker |
21 | gettable TickerSequence history = new TickerSequence("ADX"); |
22 | |
23 | Double value() { ret adx(); } |
24 | |
25 | *(int *length) {} |
26 | |
27 | { |
28 | length = 14; |
29 | |
30 | onCandleAdded((IVF1<TradingCandle>) candle -> { |
31 | atr.add(candle); |
32 | |
33 | if (candles().size() < 2) ret; |
34 | var today = candle; |
35 | var yesterday = candles().nextToLast(); |
36 | |
37 | upMove(today.high()-yesterday.high()); |
38 | downMove(yesterday.low()-today.low()); |
39 | plusDM(upMove > downMove && upMove > 0 ? upMove : 0); |
40 | minusDM(downMove > upMove && downMove > 0 ? downMove : 0); |
41 | |
42 | if (smaPlusDM == null) smaPlusDM(new SmoothedMovingAverage(length())); |
43 | if (smaMinusDM == null) smaMinusDM(new SmoothedMovingAverage(length())); |
44 | smaPlusDM.add(plusDM); |
45 | smaMinusDM.add(minusDM); |
46 | |
47 | plusDI(100*smaPlusDM!/atr!); |
48 | minusDI(100*smaMinusDM!/atr!); |
49 | |
50 | if (smaAbsoluteDI == null) smaAbsoluteDI(new SmoothedMovingAverage(length())); |
51 | smaAbsoluteDI.add(absDiff(plusDI, minusDI)); |
52 | |
53 | adx(100*smaAbsoluteDI!/(plusDI+minusDI)); |
54 | |
55 | long time = candle.endTime().toLong(); |
56 | history?.addIfPriceChanged(adx, time); |
57 | }); |
58 | } |
59 | |
60 | TickerSequence asTicker(L<TradingCandle> candles) { |
61 | feed(candles); |
62 | ret history; |
63 | } |
64 | |
65 | void reset :: after { |
66 | resetFields(this, "atr adx history smaPlusDM smaMinusDM smaAbsoluteDI upMove downMove plusDM minusDM plusDI minusDI"); |
67 | } |
68 | } |
Began life as a copy of #1036469
download show line numbers debug dex old transpilations
Travelled to 2 computer(s): mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1036489 |
Snippet name: | ADX [average directional movement index, trading indicator, dev.] |
Eternal ID of this version: | #1036489/5 |
Text MD5: | 1c8066e8615076ba966097a9ed8b0cea |
Transpilation MD5: | e2e64055a532f581e56daa7f8b8e1238 |
Author: | stefan |
Category: | javax / trading |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-12-18 23:11:57 |
Source code size: | 2104 bytes / 68 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 149 / 223 |
Version history: | 4 change(s) |
Referenced in: | [show references] |