Skip to content

Commit

Permalink
xgap: refactor with common strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
narumiruna committed Jan 6, 2024
1 parent 29f3642 commit cb87467
Showing 1 changed file with 31 additions and 23 deletions.
54 changes: 31 additions & 23 deletions pkg/strategy/xgap/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/strategy/common"
"github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/util"
)
Expand All @@ -32,6 +33,10 @@ func (s *Strategy) ID() string {
return ID
}

func (s *Strategy) InstanceID() string {
return fmt.Sprintf("%s:%s", ID, s.Symbol)
}

type State struct {
AccumulatedFeeStartedAt time.Time `json:"accumulatedFeeStartedAt,omitempty"`
AccumulatedFees map[string]fixedpoint.Value `json:"accumulatedFees,omitempty"`
Expand All @@ -54,6 +59,10 @@ func (s *State) Reset() {
}

type Strategy struct {
*common.Strategy

Environment *bbgo.Environment

Symbol string `json:"symbol"`
SourceExchange string `json:"sourceExchange"`
TradingExchange string `json:"tradingExchange"`
Expand All @@ -73,13 +82,17 @@ type Strategy struct {
mu sync.Mutex
lastSourceKLine, lastTradingKLine types.KLine
sourceBook, tradingBook *types.StreamOrderBook
groupID uint32

activeOrderBook *bbgo.ActiveOrderBook

stopC chan struct{}
}

func (s *Strategy) Initialize() error {
if s.Strategy == nil {
s.Strategy = &common.Strategy{}
}
return nil
}

func (s *Strategy) isBudgetAllowed() bool {
if s.DailyFeeBudgets == nil {
return true
Expand Down Expand Up @@ -167,6 +180,9 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se
return fmt.Errorf("trading session market %s is not defined", s.Symbol)
}

instanceID := s.InstanceID()
s.Strategy.Initialize(ctx, s.Environment, tradingSession, s.tradingMarket, ID, instanceID)

s.stopC = make(chan struct{})

if s.State == nil {
Expand Down Expand Up @@ -209,13 +225,6 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se

s.tradingSession.UserDataStream.OnTradeUpdate(s.handleTradeUpdate)

instanceID := fmt.Sprintf("%s-%s", ID, s.Symbol)
s.groupID = util.FNV32(instanceID) % math.MaxInt32
log.Infof("using group id %d from fnv32(%s)", s.groupID, instanceID)

s.activeOrderBook = bbgo.NewActiveOrderBook(s.Symbol)
s.activeOrderBook.BindStream(s.tradingSession.UserDataStream)

go func() {
ticker := time.NewTicker(
util.MillisecondsJitter(s.UpdateInterval.Duration(), 1000),
Expand Down Expand Up @@ -334,40 +343,39 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se
s.tradingMarket.MinNotional.Mul(NotionModifier).Div(price))
}

createdOrders, _, err := bbgo.BatchPlaceOrder(ctx, tradingSession.Exchange, nil, types.SubmitOrder{
orderForm := []types.SubmitOrder{{
Symbol: s.Symbol,
Side: types.SideTypeBuy,
Type: types.OrderTypeLimit,
Quantity: quantity,
Price: price,
Market: s.tradingMarket,
// TimeInForce: types.TimeInForceGTC,
GroupID: s.groupID,
}, types.SubmitOrder{
}, {
Symbol: s.Symbol,
Side: types.SideTypeSell,
Type: types.OrderTypeLimit,
Quantity: quantity,
Price: price,
Market: s.tradingMarket,
// TimeInForce: types.TimeInForceGTC,
GroupID: s.groupID,
})

}}
createdOrders, err := s.OrderExecutor.SubmitOrders(ctx, orderForm...)
if err != nil {
log.WithError(err).Error("order submit error")
}

s.activeOrderBook.Add(createdOrders...)
log.Infof("created orders: %+v", createdOrders)

time.Sleep(time.Second)

if err := s.activeOrderBook.GracefulCancel(ctx, s.tradingSession.Exchange); err != nil {
log.WithError(err).Error("cancel order error")
}
s.cancelOrders(ctx)
}
}
}()

return nil
}

func (s *Strategy) cancelOrders(ctx context.Context) {
if err := s.OrderExecutor.GracefulCancel(ctx); err != nil {
log.WithError(err).Error("cancel order error")
}
}

0 comments on commit cb87467

Please sign in to comment.