mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-24 07:45:15 +00:00
make rightWindow possible to be set as zero
This commit is contained in:
parent
d617bf8ae5
commit
4c69dccf09
|
@ -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) {
|
func FindPivot(values Slice, left, right int, f func(a, pivot float64) bool) (float64, bool) {
|
||||||
length := len(values)
|
length := len(values)
|
||||||
|
|
||||||
if right == 0 {
|
|
||||||
right = left
|
|
||||||
}
|
|
||||||
|
|
||||||
if length == 0 || length < left+right+1 {
|
if length == 0 || length < left+right+1 {
|
||||||
return 0.0, false
|
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
|
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 {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,7 +39,11 @@ func (inc *PivotLow) Update(value float64) {
|
||||||
return
|
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 {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,8 +36,8 @@ func Test_calculatePivotLow(t *testing.T) {
|
||||||
assert.Equal(t, 0.0, low)
|
assert.Equal(t, 0.0, low)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("right window 0", func(t *testing.T) {
|
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, 0)
|
low, ok := calculatePivotLow([]float64{15.0, 13.0, 12.0, 10.0, 14.0, 15.0}, 2, 2)
|
||||||
assert.True(t, ok)
|
assert.True(t, ok)
|
||||||
assert.Equal(t, 10.0, low)
|
assert.Equal(t, 10.0, low)
|
||||||
})
|
})
|
||||||
|
|
|
@ -14,7 +14,7 @@ type TrendLine struct {
|
||||||
Market types.Market `json:"-"`
|
Market types.Market `json:"-"`
|
||||||
types.IntervalWindow
|
types.IntervalWindow
|
||||||
|
|
||||||
PivotRightWindow fixedpoint.Value `json:"pivotRightWindow"`
|
PivotRightWindow int `json:"pivotRightWindow"`
|
||||||
|
|
||||||
// MarketOrder is the option to enable market order short.
|
// MarketOrder is the option to enable market order short.
|
||||||
MarketOrder bool `json:"marketOrder"`
|
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: s.Interval})
|
||||||
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: types.Interval1m})
|
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})
|
// session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
|
||||||
//}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TrendLine) Bind(session *bbgo.ExchangeSession, orderExecutor *bbgo.GeneralOrderExecutor) {
|
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()
|
position := orderExecutor.Position()
|
||||||
symbol := position.Symbol
|
symbol := position.Symbol
|
||||||
standardIndicator := session.StandardIndicatorSet(s.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)
|
resistancePrices := types.NewQueue(3)
|
||||||
pivotHighDurationCounter := 0.
|
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)
|
market, _ := s.session.Market(symbol)
|
||||||
_, err := s.orderExecutor.SubmitOrders(ctx, types.SubmitOrder{
|
_, err := s.orderExecutor.SubmitOrders(ctx, types.SubmitOrder{
|
||||||
Symbol: symbol,
|
Symbol: symbol,
|
||||||
|
|
|
@ -179,7 +179,7 @@ type IntervalWindow struct {
|
||||||
Window int `json:"window"`
|
Window int `json:"window"`
|
||||||
|
|
||||||
// RightWindow is used by the pivot indicator
|
// RightWindow is used by the pivot indicator
|
||||||
RightWindow int `json:"rightWindow"`
|
RightWindow *int `json:"rightWindow"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type IntervalWindowBandWidth struct {
|
type IntervalWindowBandWidth struct {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user