xgap: add SimulateVolume

This commit is contained in:
c9s 2021-12-28 01:47:07 +08:00
parent e911530eab
commit 958dd97f52

View File

@ -64,16 +64,17 @@ type Strategy struct {
DailyFeeBudgets map[string]fixedpoint.Value `json:"dailyFeeBudgets,omitempty"` DailyFeeBudgets map[string]fixedpoint.Value `json:"dailyFeeBudgets,omitempty"`
DailyMaxVolume fixedpoint.Value `json:"dailyMaxVolume,omitempty"` DailyMaxVolume fixedpoint.Value `json:"dailyMaxVolume,omitempty"`
UpdateInterval types.Duration `json:"updateInterval"` UpdateInterval types.Duration `json:"updateInterval"`
SimulateVolume bool `json:"simulateVolume"`
sourceSession, tradingSession *bbgo.ExchangeSession sourceSession, tradingSession *bbgo.ExchangeSession
sourceMarket, tradingMarket types.Market sourceMarket, tradingMarket types.Market
state *State state *State
mu sync.Mutex mu sync.Mutex
lastKLine types.KLine lastSourceKLine, lastTradingKLine types.KLine
sourceBook, tradingBook *types.StreamOrderBook sourceBook, tradingBook *types.StreamOrderBook
groupID uint32 groupID uint32
stopC chan struct{} stopC chan struct{}
} }
@ -200,9 +201,15 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se
// from here, set data binding // from here, set data binding
s.sourceSession.MarketDataStream.OnKLine(func(kline types.KLine) { s.sourceSession.MarketDataStream.OnKLine(func(kline types.KLine) {
log.Infof("source exchange %s price: %f", s.Symbol, kline.Close) log.Infof("source exchange %s price: %f volume: %f", s.Symbol, kline.Close, kline.Volume)
s.mu.Lock() s.mu.Lock()
s.lastKLine = kline s.lastSourceKLine = kline
s.mu.Unlock()
})
s.tradingSession.MarketDataStream.OnKLine(func(kline types.KLine) {
log.Infof("trading exchange %s price: %f volume: %f", s.Symbol, kline.Close, kline.Volume)
s.mu.Lock()
s.lastTradingKLine = kline
s.mu.Unlock() s.mu.Unlock()
}) })
@ -284,6 +291,16 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se
if s.Quantity > 0 { if s.Quantity > 0 {
quantity = s.Quantity.Float64() quantity = s.Quantity.Float64()
quantity = math.Min(quantity, s.tradingMarket.MinQuantity) quantity = math.Min(quantity, s.tradingMarket.MinQuantity)
} else if s.SimulateVolume {
s.mu.Lock()
if s.lastTradingKLine.Volume > 0 && s.lastSourceKLine.Volume > 0 {
volumeDiff := s.lastSourceKLine.Volume - s.lastTradingKLine.Volume
// change the current quantity only diff is positive
if volumeDiff > 0 {
quantity = volumeDiff
}
}
s.mu.Unlock()
} }
var quoteAmount = price * quantity var quoteAmount = price * quantity
@ -294,23 +311,23 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se
} }
createdOrders, err := tradingSession.Exchange.SubmitOrders(ctx, types.SubmitOrder{ createdOrders, err := tradingSession.Exchange.SubmitOrders(ctx, types.SubmitOrder{
Symbol: s.Symbol, Symbol: s.Symbol,
Side: types.SideTypeBuy, Side: types.SideTypeBuy,
Type: types.OrderTypeLimit, Type: types.OrderTypeLimit,
Quantity: quantity, Quantity: quantity,
Price: price, Price: price,
Market: s.tradingMarket, Market: s.tradingMarket,
// TimeInForce: "GTC", // TimeInForce: "GTC",
GroupID: s.groupID, GroupID: s.groupID,
}, types.SubmitOrder{ }, types.SubmitOrder{
Symbol: s.Symbol, Symbol: s.Symbol,
Side: types.SideTypeSell, Side: types.SideTypeSell,
Type: types.OrderTypeLimit, Type: types.OrderTypeLimit,
Quantity: quantity, Quantity: quantity,
Price: price, Price: price,
Market: s.tradingMarket, Market: s.tradingMarket,
// TimeInForce: "GTC", // TimeInForce: "GTC",
GroupID: s.groupID, GroupID: s.groupID,
}) })
if err != nil { if err != nil {
log.WithError(err).Error("order submit error") log.WithError(err).Error("order submit error")