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
|
|
|
|
}
|
|
|
|
|
2022-07-13 17:12:36 +00:00
|
|
|
func (l *Line) handleKLineWindowUpdate(interval types.Interval, allKLines types.KLineWindow) {
|
2022-03-31 10:03:26 +00:00
|
|
|
if interval != l.Interval {
|
|
|
|
return
|
|
|
|
}
|
2022-07-13 17:12:36 +00:00
|
|
|
|
|
|
|
newTime := allKLines.Last().EndTime.Time()
|
2022-04-06 10:36:53 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-05-31 11:35:44 +00:00
|
|
|
func (l *Line) Last(i int) float64 {
|
|
|
|
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) Index(i int) float64 {
|
2023-05-31 11:35:44 +00:00
|
|
|
return l.Last(i)
|
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{}
|