mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 01:01:56 +00:00
grid2: add stopLossPrice handler
This commit is contained in:
parent
5f0c45093c
commit
4f3a160bbf
|
@ -49,7 +49,7 @@ exchangeStrategies:
|
|||
## this is useful when you don't want to create a grid from a higher price.
|
||||
## for example, when the last price hit 17_000.0 then open a grid with the price range 13_000 to 20_000
|
||||
triggerPrice: 17_000.0
|
||||
|
||||
|
||||
## profitSpread is the profit spread of the arbitrage order (sell order)
|
||||
## greater the profitSpread, greater the profit you make when the sell order is filled.
|
||||
## you can set this instead of the default grid profit spread.
|
||||
|
|
|
@ -64,7 +64,8 @@ type Strategy struct {
|
|||
// BaseInvestment is the total base quantity you want to place as the sell order.
|
||||
BaseInvestment fixedpoint.Value `json:"baseInvestment"`
|
||||
|
||||
TriggerPrice fixedpoint.Value `json:"triggerPrice"`
|
||||
TriggerPrice fixedpoint.Value `json:"triggerPrice"`
|
||||
|
||||
StopLossPrice fixedpoint.Value `json:"stopLossPrice"`
|
||||
TakeProfitPrice fixedpoint.Value `json:"takeProfitPrice"`
|
||||
|
||||
|
@ -498,6 +499,25 @@ func (s *Strategy) newTriggerPriceHandler(ctx context.Context, session *bbgo.Exc
|
|||
|
||||
if err := s.openGrid(ctx, session); err != nil {
|
||||
s.logger.WithError(err).Errorf("failed to setup grid orders")
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Strategy) newStopLossPriceHandler(ctx context.Context, session *bbgo.ExchangeSession) types.KLineCallback {
|
||||
return types.KLineWith(s.Symbol, types.Interval1m, func(k types.KLine) {
|
||||
if s.StopLossPrice.Compare(k.Low) < 0 {
|
||||
return
|
||||
}
|
||||
|
||||
if err := s.closeGrid(ctx); err != nil {
|
||||
s.logger.WithError(err).Errorf("can not close grid")
|
||||
return
|
||||
}
|
||||
|
||||
if err := s.orderExecutor.ClosePosition(ctx, fixedpoint.One, "grid2:stopLoss"); err != nil {
|
||||
s.logger.WithError(err).Errorf("can not close position")
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -772,6 +792,10 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
|||
session.MarketDataStream.OnKLineClosed(s.newTriggerPriceHandler(ctx, session))
|
||||
}
|
||||
|
||||
if !s.StopLossPrice.IsZero() {
|
||||
session.MarketDataStream.OnKLineClosed(s.newStopLossPriceHandler(ctx, session))
|
||||
}
|
||||
|
||||
session.UserDataStream.OnStart(func() {
|
||||
if !s.TriggerPrice.IsZero() {
|
||||
return
|
||||
|
|
Loading…
Reference in New Issue
Block a user