-
-
Notifications
You must be signed in to change notification settings - Fork 296
/
heikinashi_stream.go
72 lines (66 loc) · 1.94 KB
/
heikinashi_stream.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package types
import (
"github.com/c9s/bbgo/pkg/fixedpoint"
)
var Four fixedpoint.Value = fixedpoint.NewFromInt(4)
type HeikinAshiStream struct {
StandardStreamEmitter
lastAshi map[string]map[Interval]*KLine
LastOrigin map[string]map[Interval]*KLine
}
func (s *HeikinAshiStream) EmitKLineClosed(kline KLine) {
ashi := kline
if s.lastAshi == nil {
s.lastAshi = make(map[string]map[Interval]*KLine)
s.LastOrigin = make(map[string]map[Interval]*KLine)
}
if s.lastAshi[kline.Symbol] == nil {
s.lastAshi[kline.Symbol] = make(map[Interval]*KLine)
s.LastOrigin[kline.Symbol] = make(map[Interval]*KLine)
}
lastAshi := s.lastAshi[kline.Symbol][kline.Interval]
if lastAshi == nil {
ashi.Close = kline.Close.Add(kline.High).
Add(kline.Low).
Add(kline.Open).
Div(Four)
// High and Low are the same
s.lastAshi[kline.Symbol][kline.Interval] = &ashi
s.LastOrigin[kline.Symbol][kline.Interval] = &kline
} else {
ashi.Close = kline.Close.Add(kline.High).
Add(kline.Low).
Add(kline.Open).
Div(Four)
ashi.Open = lastAshi.Open.Add(lastAshi.Close).Div(Two)
// High and Low are the same
s.lastAshi[kline.Symbol][kline.Interval] = &ashi
s.LastOrigin[kline.Symbol][kline.Interval] = &kline
}
s.StandardStreamEmitter.EmitKLineClosed(ashi)
}
// No writeback to lastAshi
func (s *HeikinAshiStream) EmitKLine(kline KLine) {
ashi := kline
if s.lastAshi == nil {
s.lastAshi = make(map[string]map[Interval]*KLine)
}
if s.lastAshi[kline.Symbol] == nil {
s.lastAshi[kline.Symbol] = make(map[Interval]*KLine)
}
lastAshi := s.lastAshi[kline.Symbol][kline.Interval]
if lastAshi == nil {
ashi.Close = kline.Close.Add(kline.High).
Add(kline.Low).
Add(kline.Open).
Div(Four)
} else {
ashi.Close = kline.Close.Add(kline.High).
Add(kline.Low).
Add(kline.Open).
Div(Four)
ashi.Open = lastAshi.Open.Add(lastAshi.Close).Div(Two)
}
s.StandardStreamEmitter.EmitKLine(ashi)
}
var _ StandardStreamEmitter = &HeikinAshiStream{}