From 7f3f2c1217b5214fbecbccb6b3c781cddfabc9d5 Mon Sep 17 00:00:00 2001 From: c9s Date: Sun, 4 Jun 2023 14:18:47 +0800 Subject: [PATCH] types: move cross result to a single file --- pkg/types/cross.go | 56 ++++++++++++++++++++++++++++++++++++++++++ pkg/types/indicator.go | 55 ----------------------------------------- 2 files changed, 56 insertions(+), 55 deletions(-) create mode 100644 pkg/types/cross.go diff --git a/pkg/types/cross.go b/pkg/types/cross.go new file mode 100644 index 000000000..5504b035b --- /dev/null +++ b/pkg/types/cross.go @@ -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} +} diff --git a/pkg/types/indicator.go b/pkg/types/indicator.go index 4dd396b05..593f6bec8 100644 --- a/pkg/types/indicator.go +++ b/pkg/types/indicator.go @@ -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()