mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 14:55:16 +00:00
Merge pull request #930 from andycheng123/fix/pivotshort-trendema
Fix: Pivotshort
This commit is contained in:
commit
3c4bad6124
|
@ -44,7 +44,7 @@ func (s *TrendEMA) Bind(session *ExchangeSession, orderExecutor *GeneralOrderExe
|
||||||
|
|
||||||
func (s *TrendEMA) Gradient() float64 {
|
func (s *TrendEMA) Gradient() float64 {
|
||||||
if s.last > 0.0 && s.current > 0.0 {
|
if s.last > 0.0 && s.current > 0.0 {
|
||||||
return s.last / s.current
|
return s.current / s.last
|
||||||
}
|
}
|
||||||
return 0.0
|
return 0.0
|
||||||
}
|
}
|
||||||
|
@ -58,11 +58,7 @@ func (s *TrendEMA) GradientAllowed() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.MaxGradient > 0.0 && gradient < s.MaxGradient {
|
if s.MaxGradient > 0.0 && gradient < s.MaxGradient && gradient > s.MinGradient {
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
if s.MinGradient > 0.0 && gradient > s.MinGradient {
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
21
pkg/bbgo/trend_ema_test.go
Normal file
21
pkg/bbgo/trend_ema_test.go
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
package bbgo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/c9s/bbgo/pkg/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_TrendEMA(t *testing.T) {
|
||||||
|
t.Run("Test Trend EMA", func(t *testing.T) {
|
||||||
|
trendEMA_test := TrendEMA{
|
||||||
|
IntervalWindow: types.IntervalWindow{Window: 1},
|
||||||
|
}
|
||||||
|
trendEMA_test.last = 1000.0
|
||||||
|
trendEMA_test.current = 1200.0
|
||||||
|
|
||||||
|
if trendEMA_test.Gradient() != 1.2 {
|
||||||
|
t.Errorf("Gradient() = %v, want %v", trendEMA_test.Gradient(), 1.2)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
|
@ -50,6 +50,9 @@ type BreakLow struct {
|
||||||
|
|
||||||
orderExecutor *bbgo.GeneralOrderExecutor
|
orderExecutor *bbgo.GeneralOrderExecutor
|
||||||
session *bbgo.ExchangeSession
|
session *bbgo.ExchangeSession
|
||||||
|
|
||||||
|
// StrategyController
|
||||||
|
bbgo.StrategyController
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *BreakLow) Subscribe(session *bbgo.ExchangeSession) {
|
func (s *BreakLow) Subscribe(session *bbgo.ExchangeSession) {
|
||||||
|
@ -73,6 +76,9 @@ func (s *BreakLow) Bind(session *bbgo.ExchangeSession, orderExecutor *bbgo.Gener
|
||||||
s.session = session
|
s.session = session
|
||||||
s.orderExecutor = orderExecutor
|
s.orderExecutor = orderExecutor
|
||||||
|
|
||||||
|
// StrategyController
|
||||||
|
s.Status = types.StrategyStatusRunning
|
||||||
|
|
||||||
position := orderExecutor.Position()
|
position := orderExecutor.Position()
|
||||||
symbol := position.Symbol
|
symbol := position.Symbol
|
||||||
standardIndicator := session.StandardIndicatorSet(s.Symbol)
|
standardIndicator := session.StandardIndicatorSet(s.Symbol)
|
||||||
|
@ -146,6 +152,11 @@ func (s *BreakLow) Bind(session *bbgo.ExchangeSession, orderExecutor *bbgo.Gener
|
||||||
ratio := fixedpoint.One.Add(s.Ratio)
|
ratio := fixedpoint.One.Add(s.Ratio)
|
||||||
breakPrice := previousLow.Mul(ratio)
|
breakPrice := previousLow.Mul(ratio)
|
||||||
|
|
||||||
|
// StrategyController
|
||||||
|
if s.Status != types.StrategyStatusRunning {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
openPrice := kline.Open
|
openPrice := kline.Open
|
||||||
closePrice := kline.Close
|
closePrice := kline.Close
|
||||||
|
|
||||||
|
|
|
@ -45,6 +45,9 @@ type FailedBreakHigh struct {
|
||||||
|
|
||||||
orderExecutor *bbgo.GeneralOrderExecutor
|
orderExecutor *bbgo.GeneralOrderExecutor
|
||||||
session *bbgo.ExchangeSession
|
session *bbgo.ExchangeSession
|
||||||
|
|
||||||
|
// StrategyController
|
||||||
|
bbgo.StrategyController
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *FailedBreakHigh) Subscribe(session *bbgo.ExchangeSession) {
|
func (s *FailedBreakHigh) Subscribe(session *bbgo.ExchangeSession) {
|
||||||
|
@ -80,6 +83,9 @@ func (s *FailedBreakHigh) Bind(session *bbgo.ExchangeSession, orderExecutor *bbg
|
||||||
s.lastHigh = fixedpoint.Zero
|
s.lastHigh = fixedpoint.Zero
|
||||||
s.pivotHigh = standardIndicator.PivotHigh(s.IntervalWindow)
|
s.pivotHigh = standardIndicator.PivotHigh(s.IntervalWindow)
|
||||||
|
|
||||||
|
// StrategyController
|
||||||
|
s.Status = types.StrategyStatusRunning
|
||||||
|
|
||||||
if s.VWMA != nil {
|
if s.VWMA != nil {
|
||||||
s.vwma = standardIndicator.VWMA(types.IntervalWindow{
|
s.vwma = standardIndicator.VWMA(types.IntervalWindow{
|
||||||
Interval: s.BreakInterval,
|
Interval: s.BreakInterval,
|
||||||
|
@ -122,6 +128,11 @@ func (s *FailedBreakHigh) Bind(session *bbgo.ExchangeSession, orderExecutor *bbg
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StrategyController
|
||||||
|
if s.Status != types.StrategyStatusRunning {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// make sure the position is opened, and it's a short position
|
// make sure the position is opened, and it's a short position
|
||||||
if !position.IsOpened(k.Close) || !position.IsShort() {
|
if !position.IsOpened(k.Close) || !position.IsShort() {
|
||||||
return
|
return
|
||||||
|
@ -150,6 +161,11 @@ func (s *FailedBreakHigh) Bind(session *bbgo.ExchangeSession, orderExecutor *bbg
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StrategyController
|
||||||
|
if s.Status != types.StrategyStatusRunning {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
previousHigh := s.lastHigh
|
previousHigh := s.lastHigh
|
||||||
ratio := fixedpoint.One.Add(s.Ratio)
|
ratio := fixedpoint.One.Add(s.Ratio)
|
||||||
breakPrice := previousHigh.Mul(ratio)
|
breakPrice := previousHigh.Mul(ratio)
|
||||||
|
|
|
@ -35,6 +35,9 @@ type ResistanceShort struct {
|
||||||
currentResistancePrice fixedpoint.Value
|
currentResistancePrice fixedpoint.Value
|
||||||
|
|
||||||
activeOrders *bbgo.ActiveOrderBook
|
activeOrders *bbgo.ActiveOrderBook
|
||||||
|
|
||||||
|
// StrategyController
|
||||||
|
bbgo.StrategyController
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ResistanceShort) Subscribe(session *bbgo.ExchangeSession) {
|
func (s *ResistanceShort) Subscribe(session *bbgo.ExchangeSession) {
|
||||||
|
@ -59,6 +62,9 @@ func (s *ResistanceShort) Bind(session *bbgo.ExchangeSession, orderExecutor *bbg
|
||||||
})
|
})
|
||||||
s.activeOrders.BindStream(session.UserDataStream)
|
s.activeOrders.BindStream(session.UserDataStream)
|
||||||
|
|
||||||
|
// StrategyController
|
||||||
|
s.Status = types.StrategyStatusRunning
|
||||||
|
|
||||||
if s.TrendEMA != nil {
|
if s.TrendEMA != nil {
|
||||||
s.TrendEMA.Bind(session, orderExecutor)
|
s.TrendEMA.Bind(session, orderExecutor)
|
||||||
}
|
}
|
||||||
|
@ -69,6 +75,11 @@ func (s *ResistanceShort) Bind(session *bbgo.ExchangeSession, orderExecutor *bbg
|
||||||
s.updateResistanceOrders(fixedpoint.NewFromFloat(s.resistancePivot.Last()))
|
s.updateResistanceOrders(fixedpoint.NewFromFloat(s.resistancePivot.Last()))
|
||||||
|
|
||||||
session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, s.Interval, func(kline types.KLine) {
|
session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, s.Interval, func(kline types.KLine) {
|
||||||
|
// StrategyController
|
||||||
|
if s.Status != types.StrategyStatusRunning {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// trend EMA protection
|
// trend EMA protection
|
||||||
if s.TrendEMA != nil && !s.TrendEMA.GradientAllowed() {
|
if s.TrendEMA != nil && !s.TrendEMA.GradientAllowed() {
|
||||||
return
|
return
|
||||||
|
|
|
@ -125,6 +125,18 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
||||||
s.OnSuspend(func() {
|
s.OnSuspend(func() {
|
||||||
// Cancel active orders
|
// Cancel active orders
|
||||||
_ = s.orderExecutor.GracefulCancel(ctx)
|
_ = s.orderExecutor.GracefulCancel(ctx)
|
||||||
|
|
||||||
|
if s.BreakLow != nil {
|
||||||
|
s.BreakLow.Suspend()
|
||||||
|
}
|
||||||
|
|
||||||
|
if s.ResistanceShort != nil {
|
||||||
|
s.ResistanceShort.Suspend()
|
||||||
|
}
|
||||||
|
|
||||||
|
if s.FailedBreakHigh != nil {
|
||||||
|
s.FailedBreakHigh.Suspend()
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
s.OnEmergencyStop(func() {
|
s.OnEmergencyStop(func() {
|
||||||
|
@ -132,6 +144,18 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
||||||
_ = s.orderExecutor.GracefulCancel(ctx)
|
_ = s.orderExecutor.GracefulCancel(ctx)
|
||||||
// Close 100% position
|
// Close 100% position
|
||||||
_ = s.ClosePosition(ctx, fixedpoint.One)
|
_ = s.ClosePosition(ctx, fixedpoint.One)
|
||||||
|
|
||||||
|
if s.BreakLow != nil {
|
||||||
|
s.BreakLow.EmergencyStop()
|
||||||
|
}
|
||||||
|
|
||||||
|
if s.ResistanceShort != nil {
|
||||||
|
s.ResistanceShort.EmergencyStop()
|
||||||
|
}
|
||||||
|
|
||||||
|
if s.FailedBreakHigh != nil {
|
||||||
|
s.FailedBreakHigh.EmergencyStop()
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// initial required information
|
// initial required information
|
||||||
|
|
Loading…
Reference in New Issue
Block a user