mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 00:35:15 +00:00
types: move cross result to a single file
This commit is contained in:
parent
24003139f4
commit
7f3f2c1217
56
pkg/types/cross.go
Normal file
56
pkg/types/cross.go
Normal file
|
@ -0,0 +1,56 @@
|
|||
package types
|
||||
|
||||
// The result structure that maps to the crossing result of `CrossOver` and `CrossUnder`
|
||||
// Accessible through BoolSeries interface
|
||||
type CrossResult struct {
|
||||
a Series
|
||||
b Series
|
||||
isOver bool
|
||||
}
|
||||
|
||||
func (c *CrossResult) Last() bool {
|
||||
if c.Length() == 0 {
|
||||
return false
|
||||
}
|
||||
if c.isOver {
|
||||
return c.a.Last(0)-c.b.Last(0) > 0 && c.a.Last(1)-c.b.Last(1) < 0
|
||||
} else {
|
||||
return c.a.Last(0)-c.b.Last(0) < 0 && c.a.Last(1)-c.b.Last(1) > 0
|
||||
}
|
||||
}
|
||||
|
||||
func (c *CrossResult) Index(i int) bool {
|
||||
if i >= c.Length() {
|
||||
return false
|
||||
}
|
||||
if c.isOver {
|
||||
return c.a.Last(i)-c.b.Last(i) > 0 && c.a.Last(i+1)-c.b.Last(i+1) < 0
|
||||
} else {
|
||||
return c.a.Last(i)-c.b.Last(i) < 0 && c.a.Last(i+1)-c.b.Last(i+1) > 0
|
||||
}
|
||||
}
|
||||
|
||||
func (c *CrossResult) Length() int {
|
||||
la := c.a.Length()
|
||||
lb := c.b.Length()
|
||||
if la > lb {
|
||||
return lb
|
||||
}
|
||||
return la
|
||||
}
|
||||
|
||||
// a series cross above b series.
|
||||
// If in current KLine, a is higher than b, and in previous KLine, a is lower than b, then return true.
|
||||
// Otherwise return false.
|
||||
// If accessing index <= length, will always return false
|
||||
func CrossOver(a Series, b Series) BoolSeries {
|
||||
return &CrossResult{a, b, true}
|
||||
}
|
||||
|
||||
// a series cross under b series.
|
||||
// If in current KLine, a is lower than b, and in previous KLine, a is higher than b, then return true.
|
||||
// Otherwise return false.
|
||||
// If accessing index <= length, will always return false
|
||||
func CrossUnder(a Series, b Series) BoolSeries {
|
||||
return &CrossResult{a, b, false}
|
||||
}
|
|
@ -100,61 +100,6 @@ func NextCross(a Series, b Series, lookback int) (int, float64, bool) {
|
|||
return int(math.Ceil(-indexf)), alpha1 + beta1*indexf, true
|
||||
}
|
||||
|
||||
// The result structure that maps to the crossing result of `CrossOver` and `CrossUnder`
|
||||
// Accessible through BoolSeries interface
|
||||
type CrossResult struct {
|
||||
a Series
|
||||
b Series
|
||||
isOver bool
|
||||
}
|
||||
|
||||
func (c *CrossResult) Last() bool {
|
||||
if c.Length() == 0 {
|
||||
return false
|
||||
}
|
||||
if c.isOver {
|
||||
return c.a.Last(0)-c.b.Last(0) > 0 && c.a.Last(1)-c.b.Last(1) < 0
|
||||
} else {
|
||||
return c.a.Last(0)-c.b.Last(0) < 0 && c.a.Last(1)-c.b.Last(1) > 0
|
||||
}
|
||||
}
|
||||
|
||||
func (c *CrossResult) Index(i int) bool {
|
||||
if i >= c.Length() {
|
||||
return false
|
||||
}
|
||||
if c.isOver {
|
||||
return c.a.Last(i)-c.b.Last(i) > 0 && c.a.Last(i+1)-c.b.Last(i+1) < 0
|
||||
} else {
|
||||
return c.a.Last(i)-c.b.Last(i) < 0 && c.a.Last(i+1)-c.b.Last(i+1) > 0
|
||||
}
|
||||
}
|
||||
|
||||
func (c *CrossResult) Length() int {
|
||||
la := c.a.Length()
|
||||
lb := c.b.Length()
|
||||
if la > lb {
|
||||
return lb
|
||||
}
|
||||
return la
|
||||
}
|
||||
|
||||
// a series cross above b series.
|
||||
// If in current KLine, a is higher than b, and in previous KLine, a is lower than b, then return true.
|
||||
// Otherwise return false.
|
||||
// If accessing index <= length, will always return false
|
||||
func CrossOver(a Series, b Series) BoolSeries {
|
||||
return &CrossResult{a, b, true}
|
||||
}
|
||||
|
||||
// a series cross under b series.
|
||||
// If in current KLine, a is lower than b, and in previous KLine, a is higher than b, then return true.
|
||||
// Otherwise return false.
|
||||
// If accessing index <= length, will always return false
|
||||
func CrossUnder(a Series, b Series) BoolSeries {
|
||||
return &CrossResult{a, b, false}
|
||||
}
|
||||
|
||||
func Highest(a Series, lookback int) float64 {
|
||||
if lookback > a.Length() {
|
||||
lookback = a.Length()
|
||||
|
|
Loading…
Reference in New Issue
Block a user