types: move cross result to a single file

This commit is contained in:
c9s 2023-06-04 14:18:47 +08:00
parent 24003139f4
commit 7f3f2c1217
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 56 additions and 55 deletions

56
pkg/types/cross.go Normal file
View 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}
}

View File

@ -100,61 +100,6 @@ func NextCross(a Series, b Series, lookback int) (int, float64, bool) {
return int(math.Ceil(-indexf)), alpha1 + beta1*indexf, true 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 { func Highest(a Series, lookback int) float64 {
if lookback > a.Length() { if lookback > a.Length() {
lookback = a.Length() lookback = a.Length()