2022-03-31 10:03:26 +00:00
|
|
|
package indicator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
2022-04-04 09:19:17 +00:00
|
|
|
// 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
|
2022-03-31 10:03:26 +00:00
|
|
|
type Line struct {
|
2022-06-29 12:49:02 +00:00
|
|
|
types.SeriesBase
|
2022-03-31 10:03:26 +00:00
|
|
|
types.IntervalWindow
|
|
|
|
start float64
|
|
|
|
end float64
|
2022-04-06 10:36:53 +00:00
|
|
|
startIndex int
|
|
|
|
endIndex int
|
2022-03-31 10:03:26 +00:00
|
|
|
currentTime time.Time
|
|
|
|
Interval types.Interval
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Line) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
|
|
|
|
if interval != l.Interval {
|
|
|
|
return
|
|
|
|
}
|
2022-04-06 10:36:53 +00:00
|
|
|
newTime := window.Last().EndTime.Time()
|
|
|
|
delta := int(newTime.Sub(l.currentTime).Minutes()) / l.Interval.Minutes()
|
|
|
|
l.startIndex += delta
|
|
|
|
l.endIndex += delta
|
|
|
|
l.currentTime = newTime
|
2022-03-31 10:03:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Line) Bind(updater KLineWindowUpdater) {
|
|
|
|
updater.OnKLineWindowUpdate(l.handleKLineWindowUpdate)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Line) Last() float64 {
|
2022-04-19 10:22:22 +00:00
|
|
|
return (l.end-l.start)/float64(l.startIndex-l.endIndex)*float64(l.endIndex) + l.end
|
2022-03-31 10:03:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Line) Index(i int) float64 {
|
2022-04-19 10:22:22 +00:00
|
|
|
return (l.end-l.start)/float64(l.startIndex-l.endIndex)*float64(l.endIndex-i) + l.end
|
2022-03-31 10:03:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Line) Length() int {
|
2022-04-06 10:36:53 +00:00
|
|
|
if l.startIndex > l.endIndex {
|
|
|
|
return l.startIndex - l.endIndex
|
|
|
|
} else {
|
|
|
|
return l.endIndex - l.startIndex
|
|
|
|
}
|
2022-03-31 10:03:26 +00:00
|
|
|
}
|
|
|
|
|
2022-04-06 10:36:53 +00:00
|
|
|
func (l *Line) SetXY1(index int, value float64) {
|
|
|
|
l.startIndex = index
|
2022-04-04 09:19:17 +00:00
|
|
|
l.start = value
|
|
|
|
}
|
|
|
|
|
2022-04-06 10:36:53 +00:00
|
|
|
func (l *Line) SetXY2(index int, value float64) {
|
|
|
|
l.endIndex = index
|
2022-04-04 09:19:17 +00:00
|
|
|
l.end = value
|
|
|
|
}
|
|
|
|
|
2022-04-06 10:36:53 +00:00
|
|
|
func NewLine(startIndex int, startValue float64, endIndex int, endValue float64, interval types.Interval) *Line {
|
2022-06-29 12:49:02 +00:00
|
|
|
line := &Line{
|
2022-04-04 09:19:17 +00:00
|
|
|
start: startValue,
|
|
|
|
end: endValue,
|
2022-04-06 10:36:53 +00:00
|
|
|
startIndex: startIndex,
|
|
|
|
endIndex: endIndex,
|
|
|
|
currentTime: time.Time{},
|
2022-04-04 09:19:17 +00:00
|
|
|
Interval: interval,
|
|
|
|
}
|
2022-06-29 12:49:02 +00:00
|
|
|
line.SeriesBase.Series = line
|
|
|
|
return line
|
2022-04-04 09:19:17 +00:00
|
|
|
}
|
|
|
|
|
2022-06-29 12:49:02 +00:00
|
|
|
var _ types.SeriesExtend = &Line{}
|