mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 06:53:52 +00:00
add drift exit condition
This commit is contained in:
parent
6a9e00ebd4
commit
69b45e90e9
|
@ -13,6 +13,28 @@ exchangeStrategies:
|
|||
symbol: ETHUSDT
|
||||
# kline interval for indicators
|
||||
interval: 15m
|
||||
window: 3
|
||||
exits:
|
||||
- roiStopLoss:
|
||||
percentage: 0.8%
|
||||
- roiTakeProfit:
|
||||
percentage: 35%
|
||||
- protectiveStopLoss:
|
||||
activationRatio: 0.6%
|
||||
stopLossRatio: 0.1%
|
||||
placeStopOrder: false
|
||||
- protectiveStopLoss:
|
||||
activationRatio: 5%
|
||||
stopLossRatio: 1%
|
||||
placeStopOrder: false
|
||||
- cumulatedVolumeTakeProfit:
|
||||
interval: 5m
|
||||
window: 2
|
||||
minQuoteVolume: 200_000_000
|
||||
#- protectiveStopLoss:
|
||||
# activationRatio: 2%
|
||||
# stopLossRatio: 1%
|
||||
# placeStopOrder: false
|
||||
|
||||
sync:
|
||||
userDataStream:
|
||||
|
@ -32,7 +54,7 @@ backtest:
|
|||
accounts:
|
||||
binance:
|
||||
#makerFeeRate: 0
|
||||
#takerFeeRate: 15
|
||||
#takerFeeRate: 0
|
||||
balances:
|
||||
ETH: 10.0
|
||||
ETH: 0.0
|
||||
USDT: 5000.0
|
||||
|
|
|
@ -40,7 +40,8 @@ type Strategy struct {
|
|||
midPrice fixedpoint.Value
|
||||
lock sync.RWMutex
|
||||
|
||||
Session *bbgo.ExchangeSession
|
||||
ExitMethods bbgo.ExitMethodSet `json:"exits"`
|
||||
Session *bbgo.ExchangeSession
|
||||
*bbgo.GeneralOrderExecutor
|
||||
*bbgo.ActiveOrderBook
|
||||
}
|
||||
|
@ -64,6 +65,7 @@ func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
|
|||
if !bbgo.IsBackTesting {
|
||||
session.Subscribe(types.MarketTradeChannel, s.Symbol, types.SubscribeOptions{})
|
||||
}
|
||||
s.ExitMethods.SetAndSubscribe(session, s)
|
||||
}
|
||||
|
||||
var Three fixedpoint.Value = fixedpoint.NewFromInt(3)
|
||||
|
@ -155,12 +157,15 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
|||
bbgo.Sync(s)
|
||||
})
|
||||
s.GeneralOrderExecutor.Bind()
|
||||
for _, method := range s.ExitMethods {
|
||||
method.Bind(session, s.GeneralOrderExecutor)
|
||||
}
|
||||
s.ActiveOrderBook = bbgo.NewActiveOrderBook(s.Symbol)
|
||||
s.ActiveOrderBook.BindStream(session.UserDataStream)
|
||||
|
||||
store, _ := session.MarketDataStore(s.Symbol)
|
||||
|
||||
s.drift = &indicator.Drift{IntervalWindow: types.IntervalWindow{Interval: s.Interval, Window: 5}}
|
||||
s.drift = &indicator.Drift{IntervalWindow: types.IntervalWindow{Interval: s.Interval, Window: s.Window}}
|
||||
s.atr = &indicator.ATR{IntervalWindow: types.IntervalWindow{Interval: s.Interval, Window: 34}}
|
||||
s.atr.Bind(store)
|
||||
|
||||
|
@ -203,7 +208,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
|||
hlc3 := kline.High.Add(kline.Low).Add(kline.Close).Div(Three)
|
||||
s.drift.Update(hlc3.Float64())
|
||||
price := s.GetLastPrice()
|
||||
if s.drift.Last() < 0 && s.drift.Index(1) >= 0 {
|
||||
if s.drift.Last() < 0 && s.drift.Index(1) > 0 {
|
||||
if s.ActiveOrderBook.NumOfOrders() > 0 {
|
||||
if err := s.GeneralOrderExecutor.GracefulCancelActiveOrderBook(ctx, s.ActiveOrderBook); err != nil {
|
||||
log.WithError(err).Errorf("cannot cancel orders")
|
||||
|
@ -222,6 +227,9 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
|||
if s.Market.IsDustQuantity(baseBalance.Available, hlc3) {
|
||||
return
|
||||
}
|
||||
if !s.Position.IsClosed() && !s.Position.IsDust(hlc3) {
|
||||
return
|
||||
}
|
||||
_, err := s.GeneralOrderExecutor.SubmitOrders(ctx, types.SubmitOrder{
|
||||
Symbol: s.Symbol,
|
||||
Side: types.SideTypeSell,
|
||||
|
@ -235,7 +243,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
|||
return
|
||||
}
|
||||
}
|
||||
if s.drift.Last() > 0 && s.drift.Index(1) <= 0 {
|
||||
if s.drift.Last() > 0 && s.drift.Index(1) < 0 {
|
||||
if s.ActiveOrderBook.NumOfOrders() > 0 {
|
||||
if err := s.GeneralOrderExecutor.GracefulCancelActiveOrderBook(ctx, s.ActiveOrderBook); err != nil {
|
||||
log.WithError(err).Errorf("cannot cancel orders")
|
||||
|
@ -254,6 +262,9 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
|||
quoteBalance.Available.Div(hlc3), hlc3) {
|
||||
return
|
||||
}
|
||||
if !s.Position.IsClosed() && !s.Position.IsDust(hlc3) {
|
||||
return
|
||||
}
|
||||
_, err := s.GeneralOrderExecutor.SubmitOrders(ctx, types.SubmitOrder{
|
||||
Symbol: s.Symbol,
|
||||
Side: types.SideTypeBuy,
|
||||
|
|
Loading…
Reference in New Issue
Block a user