mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
Merge pull request #1342 from c9s/improve/pivot-right-window-usage
CHORE: make rightWindow possible to be set as zero
This commit is contained in:
commit
d50e50991e
|
@ -7,10 +7,6 @@ func (s Slice) Pivot(left, right int, f func(a, pivot float64) bool) (float64, b
|
|||
func FindPivot(values Slice, left, right int, f func(a, pivot float64) bool) (float64, bool) {
|
||||
length := len(values)
|
||||
|
||||
if right == 0 {
|
||||
right = left
|
||||
}
|
||||
|
||||
if length == 0 || length < left+right+1 {
|
||||
return 0.0, false
|
||||
}
|
||||
|
|
29
pkg/datatype/floats/pivot_test.go
Normal file
29
pkg/datatype/floats/pivot_test.go
Normal file
|
@ -0,0 +1,29 @@
|
|||
package floats
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestFindPivot(t *testing.T) {
|
||||
|
||||
t.Run("middle", func(t *testing.T) {
|
||||
pv, ok := FindPivot(Slice{10, 20, 30, 40, 30, 20}, 2, 2, func(a, pivot float64) bool {
|
||||
return a < pivot
|
||||
})
|
||||
if assert.True(t, ok) {
|
||||
assert.Equal(t, 40., pv)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("last", func(t *testing.T) {
|
||||
pv, ok := FindPivot(Slice{10, 20, 30, 40, 30, 45}, 2, 0, func(a, pivot float64) bool {
|
||||
return a < pivot
|
||||
})
|
||||
if assert.True(t, ok) {
|
||||
assert.Equal(t, 45., pv)
|
||||
}
|
||||
})
|
||||
|
||||
}
|
|
@ -39,7 +39,11 @@ func (inc *PivotHigh) Update(value float64) {
|
|||
return
|
||||
}
|
||||
|
||||
high, ok := calculatePivotHigh(inc.Highs, inc.Window, inc.RightWindow)
|
||||
if inc.RightWindow == nil {
|
||||
inc.RightWindow = &inc.Window
|
||||
}
|
||||
|
||||
high, ok := calculatePivotHigh(inc.Highs, inc.Window, *inc.RightWindow)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
|
|
@ -39,7 +39,11 @@ func (inc *PivotLow) Update(value float64) {
|
|||
return
|
||||
}
|
||||
|
||||
low, ok := calculatePivotLow(inc.Lows, inc.Window, inc.RightWindow)
|
||||
if inc.RightWindow == nil {
|
||||
inc.RightWindow = &inc.Window
|
||||
}
|
||||
|
||||
low, ok := calculatePivotLow(inc.Lows, inc.Window, *inc.RightWindow)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
|
|
@ -36,8 +36,8 @@ func Test_calculatePivotLow(t *testing.T) {
|
|||
assert.Equal(t, 0.0, low)
|
||||
})
|
||||
|
||||
t.Run("right window 0", func(t *testing.T) {
|
||||
low, ok := calculatePivotLow([]float64{15.0, 13.0, 12.0, 10.0, 14.0, 15.0}, 2, 0)
|
||||
t.Run("right window same", func(t *testing.T) {
|
||||
low, ok := calculatePivotLow([]float64{15.0, 13.0, 12.0, 10.0, 14.0, 15.0}, 2, 2)
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, 10.0, low)
|
||||
})
|
||||
|
|
|
@ -11,7 +11,12 @@ type PivotHighStream struct {
|
|||
window, rightWindow int
|
||||
}
|
||||
|
||||
func PivotHigh2(source types.Float64Source, window, rightWindow int) *PivotHighStream {
|
||||
func PivotHigh(source types.Float64Source, window int, args ...int) *PivotHighStream {
|
||||
rightWindow := window
|
||||
if len(args) > 0 {
|
||||
rightWindow = args[0]
|
||||
}
|
||||
|
||||
s := &PivotHighStream{
|
||||
Float64Series: types.NewFloat64Series(),
|
||||
window: window,
|
||||
|
|
|
@ -11,7 +11,12 @@ type PivotLowStream struct {
|
|||
window, rightWindow int
|
||||
}
|
||||
|
||||
func PivotLow(source types.Float64Source, window, rightWindow int) *PivotLowStream {
|
||||
func PivotLow(source types.Float64Source, window int, args ...int) *PivotLowStream {
|
||||
rightWindow := window
|
||||
if len(args) > 0 {
|
||||
rightWindow = args[0]
|
||||
}
|
||||
|
||||
s := &PivotLowStream{
|
||||
Float64Series: types.NewFloat64Series(),
|
||||
window: window,
|
||||
|
|
|
@ -14,7 +14,7 @@ type TrendLine struct {
|
|||
Market types.Market `json:"-"`
|
||||
types.IntervalWindow
|
||||
|
||||
PivotRightWindow fixedpoint.Value `json:"pivotRightWindow"`
|
||||
PivotRightWindow int `json:"pivotRightWindow"`
|
||||
|
||||
// MarketOrder is the option to enable market order short.
|
||||
MarketOrder bool `json:"marketOrder"`
|
||||
|
@ -35,9 +35,9 @@ func (s *TrendLine) Subscribe(session *bbgo.ExchangeSession) {
|
|||
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
|
||||
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: types.Interval1m})
|
||||
|
||||
//if s.pivot != nil {
|
||||
// if s.pivot != nil {
|
||||
// session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
|
||||
//}
|
||||
// }
|
||||
}
|
||||
|
||||
func (s *TrendLine) Bind(session *bbgo.ExchangeSession, orderExecutor *bbgo.GeneralOrderExecutor) {
|
||||
|
@ -47,8 +47,14 @@ func (s *TrendLine) Bind(session *bbgo.ExchangeSession, orderExecutor *bbgo.Gene
|
|||
position := orderExecutor.Position()
|
||||
symbol := position.Symbol
|
||||
standardIndicator := session.StandardIndicatorSet(s.Symbol)
|
||||
s.pivotHigh = standardIndicator.PivotHigh(types.IntervalWindow{s.Interval, int(3. * s.PivotRightWindow.Float64()), int(s.PivotRightWindow.Float64())})
|
||||
s.pivotLow = standardIndicator.PivotLow(types.IntervalWindow{s.Interval, int(3. * s.PivotRightWindow.Float64()), int(s.PivotRightWindow.Float64())})
|
||||
|
||||
s.pivotHigh = standardIndicator.PivotHigh(types.IntervalWindow{
|
||||
Interval: s.Interval,
|
||||
Window: int(3. * s.PivotRightWindow), RightWindow: &s.PivotRightWindow})
|
||||
|
||||
s.pivotLow = standardIndicator.PivotLow(types.IntervalWindow{
|
||||
Interval: s.Interval,
|
||||
Window: int(3. * s.PivotRightWindow), RightWindow: &s.PivotRightWindow})
|
||||
|
||||
resistancePrices := types.NewQueue(3)
|
||||
pivotHighDurationCounter := 0.
|
||||
|
@ -124,7 +130,9 @@ func (s *TrendLine) Bind(session *bbgo.ExchangeSession, orderExecutor *bbgo.Gene
|
|||
}
|
||||
}
|
||||
|
||||
func (s *TrendLine) placeOrder(ctx context.Context, side types.SideType, quantity fixedpoint.Value, symbol string) error {
|
||||
func (s *TrendLine) placeOrder(
|
||||
ctx context.Context, side types.SideType, quantity fixedpoint.Value, symbol string,
|
||||
) error {
|
||||
market, _ := s.session.Market(symbol)
|
||||
_, err := s.orderExecutor.SubmitOrders(ctx, types.SubmitOrder{
|
||||
Symbol: symbol,
|
||||
|
|
|
@ -179,7 +179,7 @@ type IntervalWindow struct {
|
|||
Window int `json:"window"`
|
||||
|
||||
// RightWindow is used by the pivot indicator
|
||||
RightWindow int `json:"rightWindow"`
|
||||
RightWindow *int `json:"rightWindow"`
|
||||
}
|
||||
|
||||
type IntervalWindowBandWidth struct {
|
||||
|
|
Loading…
Reference in New Issue
Block a user