forked from c9s/bbgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
line.go
80 lines (67 loc) · 1.77 KB
/
line.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
73
74
75
76
77
78
79
80
package indicator
import (
"time"
"github.com/c9s/bbgo/pkg/types"
)
// Line indicator is a utility that helps to simulate either the
// 1. trend
// 2. support
// 3. resistance
// of the market data, defined with series interface
type Line struct {
types.SeriesBase
types.IntervalWindow
start float64
end float64
startIndex int
endIndex int
currentTime time.Time
Interval types.Interval
}
func (l *Line) handleKLineWindowUpdate(interval types.Interval, allKLines types.KLineWindow) {
if interval != l.Interval {
return
}
newTime := allKLines.Last().EndTime.Time()
delta := int(newTime.Sub(l.currentTime).Minutes()) / l.Interval.Minutes()
l.startIndex += delta
l.endIndex += delta
l.currentTime = newTime
}
func (l *Line) Bind(updater KLineWindowUpdater) {
updater.OnKLineWindowUpdate(l.handleKLineWindowUpdate)
}
func (l *Line) Last() float64 {
return (l.end-l.start)/float64(l.startIndex-l.endIndex)*float64(l.endIndex) + l.end
}
func (l *Line) Index(i int) float64 {
return (l.end-l.start)/float64(l.startIndex-l.endIndex)*float64(l.endIndex-i) + l.end
}
func (l *Line) Length() int {
if l.startIndex > l.endIndex {
return l.startIndex - l.endIndex
} else {
return l.endIndex - l.startIndex
}
}
func (l *Line) SetXY1(index int, value float64) {
l.startIndex = index
l.start = value
}
func (l *Line) SetXY2(index int, value float64) {
l.endIndex = index
l.end = value
}
func NewLine(startIndex int, startValue float64, endIndex int, endValue float64, interval types.Interval) *Line {
line := &Line{
start: startValue,
end: endValue,
startIndex: startIndex,
endIndex: endIndex,
currentTime: time.Time{},
Interval: interval,
}
line.SeriesBase.Series = line
return line
}
var _ types.SeriesExtend = &Line{}