xmaker: add sourceDepthLevel option

This commit is contained in:
c9s 2024-09-06 21:47:43 +08:00
parent a282654c02
commit e14f09a914
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -91,11 +91,12 @@ type Strategy struct {
SignalConfigList []SignalConfig `json:"signals"` SignalConfigList []SignalConfig `json:"signals"`
SignalMarginScale *bbgo.SlideRule `json:"signalMarginScale,omitempty"` SignalMarginScale *bbgo.SlideRule `json:"signalMarginScale,omitempty"`
Margin fixedpoint.Value `json:"margin"` Margin fixedpoint.Value `json:"margin"`
BidMargin fixedpoint.Value `json:"bidMargin"` BidMargin fixedpoint.Value `json:"bidMargin"`
AskMargin fixedpoint.Value `json:"askMargin"` AskMargin fixedpoint.Value `json:"askMargin"`
UseDepthPrice bool `json:"useDepthPrice"` UseDepthPrice bool `json:"useDepthPrice"`
DepthQuantity fixedpoint.Value `json:"depthQuantity"` DepthQuantity fixedpoint.Value `json:"depthQuantity"`
SourceDepthLevel types.Depth `json:"sourceDepthLevel"`
EnableBollBandMargin bool `json:"enableBollBandMargin"` EnableBollBandMargin bool `json:"enableBollBandMargin"`
BollBandInterval types.Interval `json:"bollBandInterval"` BollBandInterval types.Interval `json:"bollBandInterval"`
@ -159,7 +160,7 @@ type Strategy struct {
ProfitStats *ProfitStats `json:"profitStats,omitempty" persistence:"profit_stats"` ProfitStats *ProfitStats `json:"profitStats,omitempty" persistence:"profit_stats"`
CoveredPosition fixedpoint.Value `json:"coveredPosition,omitempty" persistence:"covered_position"` CoveredPosition fixedpoint.Value `json:"coveredPosition,omitempty" persistence:"covered_position"`
book *types.StreamOrderBook sourceBook *types.StreamOrderBook
activeMakerOrders *bbgo.ActiveOrderBook activeMakerOrders *bbgo.ActiveOrderBook
hedgeErrorLimiter *rate.Limiter hedgeErrorLimiter *rate.Limiter
@ -199,7 +200,10 @@ func (s *Strategy) CrossSubscribe(sessions map[string]*bbgo.ExchangeSession) {
panic(fmt.Errorf("source session %s is not defined", s.SourceExchange)) panic(fmt.Errorf("source session %s is not defined", s.SourceExchange))
} }
sourceSession.Subscribe(types.BookChannel, s.Symbol, types.SubscribeOptions{}) sourceSession.Subscribe(types.BookChannel, s.Symbol, types.SubscribeOptions{
Depth: s.SourceDepthLevel,
})
sourceSession.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: "1m"}) sourceSession.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: "1m"})
makerSession, ok := sessions[s.MakerExchange] makerSession, ok := sessions[s.MakerExchange]
@ -212,6 +216,8 @@ func (s *Strategy) CrossSubscribe(sessions map[string]*bbgo.ExchangeSession) {
for _, sig := range s.SignalConfigList { for _, sig := range s.SignalConfigList {
if sig.TradeVolumeWindowSignal != nil { if sig.TradeVolumeWindowSignal != nil {
sourceSession.Subscribe(types.MarketTradeChannel, s.Symbol, types.SubscribeOptions{}) sourceSession.Subscribe(types.MarketTradeChannel, s.Symbol, types.SubscribeOptions{})
} else if sig.BollingerBandTrendSignal != nil {
sourceSession.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: sig.BollingerBandTrendSignal.Interval})
} }
} }
} }
@ -435,7 +441,7 @@ func (s *Strategy) updateQuote(ctx context.Context) {
} }
} }
bestBid, bestAsk, hasPrice := s.book.BestBidAndAsk() bestBid, bestAsk, hasPrice := s.sourceBook.BestBidAndAsk()
if !hasPrice { if !hasPrice {
s.logger.Warnf("no valid price, skip quoting") s.logger.Warnf("no valid price, skip quoting")
return return
@ -446,7 +452,7 @@ func (s *Strategy) updateQuote(ctx context.Context) {
s.priceSolver.Update(s.Symbol, s.lastPrice) s.priceSolver.Update(s.Symbol, s.lastPrice)
bookLastUpdateTime := s.book.LastUpdateTime() bookLastUpdateTime := s.sourceBook.LastUpdateTime()
if _, err := s.bidPriceHeartBeat.Update(bestBid); err != nil { if _, err := s.bidPriceHeartBeat.Update(bestBid); err != nil {
s.logger.WithError(err).Errorf("quote update error, %s price not updating, order book last update: %s ago", s.logger.WithError(err).Errorf("quote update error, %s price not updating, order book last update: %s ago",
@ -462,7 +468,7 @@ func (s *Strategy) updateQuote(ctx context.Context) {
return return
} }
sourceBook := s.book.CopyDepth(10) sourceBook := s.sourceBook.CopyDepth(10)
if valid, err := sourceBook.IsValid(); !valid { if valid, err := sourceBook.IsValid(); !valid {
s.logger.WithError(err).Errorf("%s invalid copied order book, skip quoting: %v", s.Symbol, err) s.logger.WithError(err).Errorf("%s invalid copied order book, skip quoting: %v", s.Symbol, err)
return return
@ -906,7 +912,7 @@ func (s *Strategy) Hedge(ctx context.Context, pos fixedpoint.Value) {
} }
lastPrice := s.lastPrice lastPrice := s.lastPrice
sourceBook := s.book.CopyDepth(1) sourceBook := s.sourceBook.CopyDepth(1)
switch side { switch side {
case types.SideTypeBuy: case types.SideTypeBuy:
@ -1029,6 +1035,10 @@ func (s *Strategy) Defaults() error {
s.BollBandInterval = types.Interval1m s.BollBandInterval = types.Interval1m
} }
if s.SourceDepthLevel == "" {
s.SourceDepthLevel = types.DepthLevelMedium
}
if s.BollBandMarginFactor.IsZero() { if s.BollBandMarginFactor.IsZero() {
s.BollBandMarginFactor = fixedpoint.One s.BollBandMarginFactor = fixedpoint.One
} }
@ -1350,8 +1360,8 @@ func (s *Strategy) CrossRun(
s.ProfitStats.ProfitStats = profitStats s.ProfitStats.ProfitStats = profitStats
} }
s.book = types.NewStreamBook(s.Symbol, s.sourceSession.ExchangeName) s.sourceBook = types.NewStreamBook(s.Symbol, s.sourceSession.ExchangeName)
s.book.BindStream(s.sourceSession.MarketDataStream) s.sourceBook.BindStream(s.sourceSession.MarketDataStream)
if s.EnableSignalMargin { if s.EnableSignalMargin {
scale, err := s.SignalMarginScale.Scale() scale, err := s.SignalMarginScale.Scale()
@ -1365,7 +1375,7 @@ func (s *Strategy) CrossRun(
for _, signalConfig := range s.SignalConfigList { for _, signalConfig := range s.SignalConfigList {
if signalConfig.OrderBookBestPriceSignal != nil { if signalConfig.OrderBookBestPriceSignal != nil {
signalConfig.OrderBookBestPriceSignal.book = s.book signalConfig.OrderBookBestPriceSignal.book = s.sourceBook
if err := signalConfig.OrderBookBestPriceSignal.Bind(ctx, s.sourceSession, s.Symbol); err != nil { if err := signalConfig.OrderBookBestPriceSignal.Bind(ctx, s.sourceSession, s.Symbol); err != nil {
return err return err
} }