refactor xpuremaker strategy

This commit is contained in:
c9s 2020-10-26 10:08:58 +08:00
parent fbba9b12ce
commit aa6ccbf905
3 changed files with 25 additions and 22 deletions

View File

@ -38,5 +38,5 @@ exchangeStrategies:
numOrders: 2 numOrders: 2
side: both side: both
behindVolume: 1000.0 behindVolume: 1000.0
priceTick: 0.01 priceTick: 0.001
baseQuantity: 100.0 baseQuantity: 100.0

View File

@ -85,12 +85,12 @@ func (s *Strategy) update(orderExecutor bbgo.OrderExecutor, session *bbgo.Exchan
switch s.Side { switch s.Side {
case "buy": case "buy":
s.updateOrders(orderExecutor, types.SideTypeBuy) s.updateOrders(orderExecutor, session, types.SideTypeBuy)
case "sell": case "sell":
s.updateOrders(orderExecutor, types.SideTypeSell) s.updateOrders(orderExecutor, session, types.SideTypeSell)
case "both": case "both":
s.updateOrders(orderExecutor, types.SideTypeBuy) s.updateOrders(orderExecutor, session, types.SideTypeBuy)
s.updateOrders(orderExecutor, types.SideTypeSell) s.updateOrders(orderExecutor, session, types.SideTypeSell)
default: default:
log.Panicf("undefined side: %s", s.Side) log.Panicf("undefined side: %s", s.Side)
@ -99,27 +99,18 @@ func (s *Strategy) update(orderExecutor bbgo.OrderExecutor, session *bbgo.Exchan
s.book.C.Drain(1*time.Second, 3*time.Second) s.book.C.Drain(1*time.Second, 3*time.Second)
} }
func (s *Strategy) updateOrders(orderExecutor bbgo.OrderExecutor, side types.SideType) { func (s *Strategy) updateOrders(orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession, side types.SideType) {
book := s.book.Copy() var book = s.book.Copy()
var pvs = book.PriceVolumesBySide(side)
var pvs types.PriceVolumeSlice
switch side {
case types.SideTypeBuy:
pvs = book.Bids
case types.SideTypeSell:
pvs = book.Asks
}
if pvs == nil || len(pvs) == 0 { if pvs == nil || len(pvs) == 0 {
log.Warn("empty bids or asks") log.Warnf("empty side: %s", side)
return return
} }
log.Infof("placing order behind volume: %f", s.BehindVolume.Float64()) log.Infof("placing order behind volume: %f", s.BehindVolume.Float64())
idx := pvs.IndexByVolumeDepth(s.BehindVolume) idx := pvs.IndexByVolumeDepth(s.BehindVolume)
if idx == -1 { if idx == -1 || idx > len(pvs)-1 {
// do not place orders // do not place orders
log.Warn("depth is not enough") log.Warn("depth is not enough")
return return
@ -144,7 +135,7 @@ func (s *Strategy) updateOrders(orderExecutor bbgo.OrderExecutor, side types.Sid
} }
} }
func (s *Strategy) generateOrders(symbol string, side types.SideType, price, priceTick, baseVolume fixedpoint.Value, numOrders int) (orders []types.SubmitOrder) { func (s *Strategy) generateOrders(symbol string, side types.SideType, price, priceTick, baseQuantity fixedpoint.Value, numOrders int) (orders []types.SubmitOrder) {
var expBase = fixedpoint.NewFromFloat(0.0) var expBase = fixedpoint.NewFromFloat(0.0)
switch side { switch side {
@ -160,7 +151,7 @@ func (s *Strategy) generateOrders(symbol string, side types.SideType, price, pri
} }
for i := 0; i < numOrders; i++ { for i := 0; i < numOrders; i++ {
volume := math.Exp(expBase.Float64()) * baseVolume.Float64() volume := math.Exp(expBase.Float64()) * baseQuantity.Float64()
// skip order less than 10usd // skip order less than 10usd
if volume*price.Float64() < 10.0 { if volume*price.Float64() < 10.0 {
@ -185,7 +176,6 @@ func (s *Strategy) generateOrders(symbol string, side types.SideType, price, pri
price = price + priceTick price = price + priceTick
declog := math.Log10(math.Abs(priceTick.Float64())) declog := math.Log10(math.Abs(priceTick.Float64()))
expBase += fixedpoint.NewFromFloat(math.Pow10(-int(declog)) * math.Abs(priceTick.Float64())) expBase += fixedpoint.NewFromFloat(math.Pow10(-int(declog)) * math.Abs(priceTick.Float64()))
// log.Infof("expBase: %f", expBase.Float64())
} }
return orders return orders

View File

@ -116,6 +116,19 @@ type OrderBook struct {
asksChangeCallbacks []func(pvs PriceVolumeSlice) asksChangeCallbacks []func(pvs PriceVolumeSlice)
} }
func (b *OrderBook) PriceVolumesBySide(side SideType) PriceVolumeSlice {
switch side {
case SideTypeBuy:
return b.Bids
case SideTypeSell:
return b.Asks
}
return nil
}
func (b *OrderBook) Copy() (book OrderBook) { func (b *OrderBook) Copy() (book OrderBook) {
book = *b book = *b
book.Bids = b.Bids.Copy() book.Bids = b.Bids.Copy()