mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 06:53:52 +00:00
floats: move floats related functions and add crossover, crossunder funcs
This commit is contained in:
parent
a2ab9db4eb
commit
52d245ecf1
96
pkg/datatype/floats/funcs.go
Normal file
96
pkg/datatype/floats/funcs.go
Normal file
|
@ -0,0 +1,96 @@
|
|||
package floats
|
||||
|
||||
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))
|
||||
}
|
||||
|
||||
|
||||
// CrossOver returns true if series1 is crossing over series2.
|
||||
//
|
||||
// NOTE: Usually this is used with Media Average Series to check if it crosses for buy signals.
|
||||
// It assumes first values are the most recent.
|
||||
// The crossover function does not use most recent value, since usually it's not a complete candle.
|
||||
// The second recent values and the previous are used, instead.
|
||||
func CrossOver(series1 []float64, series2 []float64) bool {
|
||||
if len(series1) < 3 || len(series2) < 3 {
|
||||
return false
|
||||
}
|
||||
|
||||
N := len(series1)
|
||||
|
||||
return series1[N-2] <= series2[N-2] && series1[N-1] > series2[N-1]
|
||||
}
|
||||
|
||||
// CrossUnder returns true if series1 is crossing under series2.
|
||||
//
|
||||
// NOTE: Usually this is used with Media Average Series to check if it crosses for sell signals.
|
||||
func CrossUnder(series1 []float64, series2 []float64) bool {
|
||||
if len(series1) < 3 || len(series2) < 3 {
|
||||
return false
|
||||
}
|
||||
|
||||
N := len(series1)
|
||||
|
||||
return series1[N-1] <= series2[N-1] && series1[N-2] > series2[N-2]
|
||||
}
|
|
@ -4,11 +4,11 @@ import (
|
|||
"context"
|
||||
|
||||
"github.com/c9s/bbgo/pkg/bbgo"
|
||||
"github.com/c9s/bbgo/pkg/datatype/floats"
|
||||
"github.com/c9s/bbgo/pkg/fixedpoint"
|
||||
"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 {
|
||||
|
@ -190,9 +190,9 @@ func (s *ResistanceShort) placeResistanceOrders(ctx context.Context, resistanceP
|
|||
}
|
||||
|
||||
func findPossibleSupportPrices(closePrice float64, groupDistance float64, lows []float64) []float64 {
|
||||
return util.Group(util.Lower(lows, closePrice), groupDistance)
|
||||
return floats.Group(floats.Lower(lows, closePrice), groupDistance)
|
||||
}
|
||||
|
||||
func findPossibleResistancePrices(closePrice float64, groupDistance float64, lows []float64) []float64 {
|
||||
return util.Group(util.Higher(lows, closePrice), groupDistance)
|
||||
return floats.Group(floats.Higher(lows, closePrice), groupDistance)
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ package util
|
|||
|
||||
import (
|
||||
"math"
|
||||
"sort"
|
||||
"strconv"
|
||||
|
||||
"github.com/c9s/bbgo/pkg/fixedpoint"
|
||||
|
@ -59,65 +58,3 @@ 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