diff --git a/pkg/strategy/pivotshort/math.go b/pkg/strategy/pivotshort/math.go deleted file mode 100644 index 9f5df82f2..000000000 --- a/pkg/strategy/pivotshort/math.go +++ /dev/null @@ -1,66 +0,0 @@ -package pivotshort - -import "sort" - -func lower(arr []float64, x float64) []float64 { - sort.Float64s(arr) - - var rst []float64 - for _, a := range arr { - // filter prices that are lower than the current closed price - if a > x { - continue - } - - rst = append(rst, a) - } - - return rst -} - -func higher(arr []float64, x float64) []float64 { - sort.Float64s(arr) - - var rst []float64 - for _, a := range arr { - // filter prices that are lower than the current closed price - if a < x { - continue - } - rst = append(rst, a) - } - - return rst -} - -func group(arr []float64, minDistance float64) []float64 { - if len(arr) == 0 { - return nil - } - - var groups []float64 - var grp = []float64{arr[0]} - for _, price := range arr { - avg := average(grp) - if (price / avg) > (1.0 + minDistance) { - groups = append(groups, avg) - grp = []float64{price} - } else { - grp = append(grp, price) - } - } - - if len(grp) > 0 { - groups = append(groups, average(grp)) - } - - return groups -} - -func average(arr []float64) float64 { - s := 0.0 - for _, a := range arr { - s += a - } - return s / float64(len(arr)) -} diff --git a/pkg/strategy/pivotshort/resistance.go b/pkg/strategy/pivotshort/resistance.go index 9bc927981..7b1aabcf4 100644 --- a/pkg/strategy/pivotshort/resistance.go +++ b/pkg/strategy/pivotshort/resistance.go @@ -8,6 +8,7 @@ import ( "github.com/c9s/bbgo/pkg/indicator" "github.com/c9s/bbgo/pkg/risk" "github.com/c9s/bbgo/pkg/types" + "github.com/c9s/bbgo/pkg/util" ) type ResistanceShort struct { @@ -189,9 +190,9 @@ func (s *ResistanceShort) placeResistanceOrders(ctx context.Context, resistanceP } func findPossibleSupportPrices(closePrice float64, groupDistance float64, lows []float64) []float64 { - return group(lower(lows, closePrice), groupDistance) + return util.Group(util.Lower(lows, closePrice), groupDistance) } func findPossibleResistancePrices(closePrice float64, groupDistance float64, lows []float64) []float64 { - return group(higher(lows, closePrice), groupDistance) + return util.Group(util.Higher(lows, closePrice), groupDistance) } diff --git a/pkg/util/math.go b/pkg/util/math.go index 31e49b541..82d6fd645 100644 --- a/pkg/util/math.go +++ b/pkg/util/math.go @@ -1,9 +1,11 @@ package util import ( - "github.com/c9s/bbgo/pkg/fixedpoint" "math" + "sort" "strconv" + + "github.com/c9s/bbgo/pkg/fixedpoint" ) const MaxDigits = 18 // MAX_INT64 ~ 9 * 10^18 @@ -56,3 +58,66 @@ func Zero(v float64) bool { func NotZero(v float64) bool { return math.Abs(v) > epsilon } + +func Lower(arr []float64, x float64) []float64 { + sort.Float64s(arr) + + var rst []float64 + for _, a := range arr { + // filter prices that are Lower than the current closed price + if a > x { + continue + } + + rst = append(rst, a) + } + + return rst +} + +func Higher(arr []float64, x float64) []float64 { + sort.Float64s(arr) + + var rst []float64 + for _, a := range arr { + // filter prices that are Lower than the current closed price + if a < x { + continue + } + rst = append(rst, a) + } + + return rst +} + +func Group(arr []float64, minDistance float64) []float64 { + if len(arr) == 0 { + return nil + } + + var groups []float64 + var grp = []float64{arr[0]} + for _, price := range arr { + avg := Average(grp) + if (price / avg) > (1.0 + minDistance) { + groups = append(groups, avg) + grp = []float64{price} + } else { + grp = append(grp, price) + } + } + + if len(grp) > 0 { + groups = append(groups, Average(grp)) + } + + return groups +} + +func Average(arr []float64) float64 { + s := 0.0 + for _, a := range arr { + s += a + } + return s / float64(len(arr)) +}