mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
util: move math util functions to util
This commit is contained in:
parent
94615f7ecf
commit
88f243c91b
|
@ -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))
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user