util: move math util functions to util

This commit is contained in:
c9s 2022-08-24 11:34:55 +08:00
parent 94615f7ecf
commit 88f243c91b
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
3 changed files with 69 additions and 69 deletions

View File

@ -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))
}

View File

@ -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)
}

View File

@ -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))
}