mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-21 22:43:52 +00:00
strategy/pivotshort: make strategy controller work
This commit is contained in:
parent
f132666738
commit
98bd6ca1d2
|
@ -50,6 +50,9 @@ type BreakLow struct {
|
|||
|
||||
orderExecutor *bbgo.GeneralOrderExecutor
|
||||
session *bbgo.ExchangeSession
|
||||
|
||||
// StrategyController
|
||||
bbgo.StrategyController
|
||||
}
|
||||
|
||||
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.orderExecutor = orderExecutor
|
||||
|
||||
// StrategyController
|
||||
s.Status = types.StrategyStatusRunning
|
||||
|
||||
position := orderExecutor.Position()
|
||||
symbol := position.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)
|
||||
breakPrice := previousLow.Mul(ratio)
|
||||
|
||||
// StrategyController
|
||||
if s.Status != types.StrategyStatusRunning {
|
||||
return
|
||||
}
|
||||
|
||||
openPrice := kline.Open
|
||||
closePrice := kline.Close
|
||||
|
||||
|
|
|
@ -45,6 +45,9 @@ type FailedBreakHigh struct {
|
|||
|
||||
orderExecutor *bbgo.GeneralOrderExecutor
|
||||
session *bbgo.ExchangeSession
|
||||
|
||||
// StrategyController
|
||||
bbgo.StrategyController
|
||||
}
|
||||
|
||||
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.pivotHigh = standardIndicator.PivotHigh(s.IntervalWindow)
|
||||
|
||||
// StrategyController
|
||||
s.Status = types.StrategyStatusRunning
|
||||
|
||||
if s.VWMA != nil {
|
||||
s.vwma = standardIndicator.VWMA(types.IntervalWindow{
|
||||
Interval: s.BreakInterval,
|
||||
|
@ -122,6 +128,11 @@ func (s *FailedBreakHigh) Bind(session *bbgo.ExchangeSession, orderExecutor *bbg
|
|||
return
|
||||
}
|
||||
|
||||
// StrategyController
|
||||
if s.Status != types.StrategyStatusRunning {
|
||||
return
|
||||
}
|
||||
|
||||
// make sure the position is opened, and it's a short position
|
||||
if !position.IsOpened(k.Close) || !position.IsShort() {
|
||||
return
|
||||
|
@ -150,6 +161,11 @@ func (s *FailedBreakHigh) Bind(session *bbgo.ExchangeSession, orderExecutor *bbg
|
|||
return
|
||||
}
|
||||
|
||||
// StrategyController
|
||||
if s.Status != types.StrategyStatusRunning {
|
||||
return
|
||||
}
|
||||
|
||||
previousHigh := s.lastHigh
|
||||
ratio := fixedpoint.One.Add(s.Ratio)
|
||||
breakPrice := previousHigh.Mul(ratio)
|
||||
|
|
|
@ -35,6 +35,9 @@ type ResistanceShort struct {
|
|||
currentResistancePrice fixedpoint.Value
|
||||
|
||||
activeOrders *bbgo.ActiveOrderBook
|
||||
|
||||
// StrategyController
|
||||
bbgo.StrategyController
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
// StrategyController
|
||||
s.Status = types.StrategyStatusRunning
|
||||
|
||||
if s.TrendEMA != nil {
|
||||
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()))
|
||||
|
||||
session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, s.Interval, func(kline types.KLine) {
|
||||
// StrategyController
|
||||
if s.Status != types.StrategyStatusRunning {
|
||||
return
|
||||
}
|
||||
|
||||
// trend EMA protection
|
||||
if s.TrendEMA != nil && !s.TrendEMA.GradientAllowed() {
|
||||
return
|
||||
|
|
|
@ -125,6 +125,18 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
|||
s.OnSuspend(func() {
|
||||
// Cancel active orders
|
||||
_ = 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() {
|
||||
|
@ -132,6 +144,18 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
|||
_ = s.orderExecutor.GracefulCancel(ctx)
|
||||
// Close 100% position
|
||||
_ = 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
|
||||
|
|
Loading…
Reference in New Issue
Block a user