@@ -15,6 +15,7 @@ import (
15
15
//go:generate callbackgen -type DMI
16
16
type DMI struct {
17
17
types.IntervalWindow
18
+
18
19
ADXSmoothing int
19
20
atr * ATR
20
21
DMP types.UpdatableSeriesExtend
@@ -23,7 +24,8 @@ type DMI struct {
23
24
DIMinus * types.Queue
24
25
ADX types.UpdatableSeriesExtend
25
26
PrevHigh , PrevLow float64
26
- UpdateCallbacks []func (diplus , diminus , adx float64 )
27
+
28
+ updateCallbacks []func (diplus , diminus , adx float64 )
27
29
}
28
30
29
31
func (inc * DMI ) Update (high , low , cloze float64 ) {
@@ -32,6 +34,7 @@ func (inc *DMI) Update(high, low, cloze float64) {
32
34
inc .DMN = & RMA {IntervalWindow : inc .IntervalWindow , Adjust : true }
33
35
inc .ADX = & RMA {IntervalWindow : types.IntervalWindow {Window : inc .ADXSmoothing }, Adjust : true }
34
36
}
37
+
35
38
if inc .atr == nil {
36
39
inc .atr = & ATR {IntervalWindow : inc .IntervalWindow }
37
40
inc .atr .Update (high , low , cloze )
@@ -41,6 +44,7 @@ func (inc *DMI) Update(high, low, cloze float64) {
41
44
inc .DIMinus = types .NewQueue (500 )
42
45
return
43
46
}
47
+
44
48
inc .atr .Update (high , low , cloze )
45
49
up := high - inc .PrevHigh
46
50
dn := inc .PrevLow - low
@@ -87,15 +91,20 @@ func (inc *DMI) Length() int {
87
91
return inc .ADX .Length ()
88
92
}
89
93
90
- func (inc * DMI ) calculateAndUpdate (allKLines []types.KLine ) {
94
+ func (inc * DMI ) PushK (k types.KLine ) {
95
+ inc .Update (k .High .Float64 (), k .Low .Float64 (), k .Close .Float64 ())
96
+ }
97
+
98
+ func (inc * DMI ) CalculateAndUpdate (allKLines []types.KLine ) {
99
+ last := allKLines [len (allKLines )- 1 ]
100
+
91
101
if inc .ADX == nil {
92
102
for _ , k := range allKLines {
93
- inc .Update ( k . High . Float64 (), k . Low . Float64 (), k . Close . Float64 () )
103
+ inc .PushK ( k )
94
104
inc .EmitUpdate (inc .DIPlus .Last (), inc .DIMinus .Last (), inc .ADX .Last ())
95
105
}
96
106
} else {
97
- k := allKLines [len (allKLines )- 1 ]
98
- inc .Update (k .High .Float64 (), k .Low .Float64 (), k .Close .Float64 ())
107
+ inc .PushK (last )
99
108
inc .EmitUpdate (inc .DIPlus .Last (), inc .DIMinus .Last (), inc .ADX .Last ())
100
109
}
101
110
}
@@ -105,7 +114,7 @@ func (inc *DMI) handleKLineWindowUpdate(interval types.Interval, window types.KL
105
114
return
106
115
}
107
116
108
- inc .calculateAndUpdate (window )
117
+ inc .CalculateAndUpdate (window )
109
118
}
110
119
111
120
func (inc * DMI ) Bind (updater KLineWindowUpdater ) {
0 commit comments