fix grid strategy order placing

This commit is contained in:
c9s 2020-12-29 18:18:32 +08:00
parent d91285d583
commit f485c1ba7f
7 changed files with 47 additions and 49 deletions

View File

@ -1,22 +1,4 @@
--- ---
notifications:
slack:
defaultChannel: "dev-bbgo"
errorChannel: "bbgo-error"
# if you want to route channel by symbol
symbolChannels:
"^BTC": "btc"
"^ETH": "eth"
"^BNB": "bnb"
# object routing rules
routing:
trade: "$symbol"
order: "$symbol"
submitOrder: "$session" # not supported yet
pnL: "bbgo-pnl"
sessions: sessions:
binance: binance:
exchange: binance exchange: binance
@ -63,7 +45,7 @@ exchangeStrategies:
- on: max - on: max
grid: grid:
symbol: BTCUSDT symbol: BTCUSDT
quantity: 0.01 quantity: 0.001
gridNumber: 10 gridNumber: 10
profitSpread: 50.0 profitSpread: 50.0
upperPrice: 11000.0 upperPrice: 11000.0

View File

@ -241,7 +241,7 @@ func (environ *Environment) Init(ctx context.Context) (err error) {
if lastPriceTime == emptyTime { if lastPriceTime == emptyTime {
session.lastPrices[symbol] = lastKLine.Close session.lastPrices[symbol] = lastKLine.Close
lastPriceTime = lastKLine.EndTime lastPriceTime = lastKLine.EndTime
} else if lastPriceTime.Before(lastKLine.EndTime) { } else if lastKLine.EndTime.After(lastPriceTime) {
session.lastPrices[symbol] = lastKLine.Close session.lastPrices[symbol] = lastKLine.Close
lastPriceTime = lastKLine.EndTime lastPriceTime = lastKLine.EndTime
} }
@ -251,6 +251,8 @@ func (environ *Environment) Init(ctx context.Context) (err error) {
marketDataStore.AddKLine(k) marketDataStore.AddKLine(k)
} }
} }
log.Infof("last price: %f", session.lastPrices[symbol])
} }
if environ.TradeService != nil { if environ.TradeService != nil {

View File

@ -153,9 +153,11 @@ func (s *WebSocketService) read(ctx context.Context) {
continue continue
} }
if m != nil {
s.dispatch(m) s.dispatch(m)
} }
} }
}
} }
func (s *WebSocketService) dispatch(msg interface{}) { func (s *WebSocketService) dispatch(msg interface{}) {

View File

@ -65,16 +65,14 @@ type Strategy struct {
} }
func (s *Strategy) placeGridOrders(orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) { func (s *Strategy) placeGridOrders(orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) {
log.Infof("placing grid orders...")
quoteCurrency := s.Market.QuoteCurrency quoteCurrency := s.Market.QuoteCurrency
balances := session.Account.Balances() balances := session.Account.Balances()
balance, ok := balances[quoteCurrency]
if !ok || balance.Available <= 0 {
return
}
currentPrice, ok := session.LastPrice(s.Symbol) currentPrice, ok := session.LastPrice(s.Symbol)
if !ok { if !ok {
log.Warn("last price not found, skipping")
return return
} }
@ -83,6 +81,10 @@ func (s *Strategy) placeGridOrders(orderExecutor bbgo.OrderExecutor, session *bb
gridSize := priceRange.Div(fixedpoint.NewFromInt(s.GridNum)) gridSize := priceRange.Div(fixedpoint.NewFromInt(s.GridNum))
var orders []types.SubmitOrder var orders []types.SubmitOrder
baseBalance, ok := balances[s.Market.BaseCurrency]
if ok && baseBalance.Available > 0 {
log.Infof("placing sell order from %f ~ %f per grid %f", (currentPriceF + gridSize).Float64(), s.UpperPrice.Float64(), gridSize.Float64())
for price := currentPriceF + gridSize; price <= s.UpperPrice; price += gridSize { for price := currentPriceF + gridSize; price <= s.UpperPrice; price += gridSize {
order := types.SubmitOrder{ order := types.SubmitOrder{
Symbol: s.Symbol, Symbol: s.Symbol,
@ -95,8 +97,15 @@ func (s *Strategy) placeGridOrders(orderExecutor bbgo.OrderExecutor, session *bb
} }
orders = append(orders, order) orders = append(orders, order)
} }
} else {
log.Warnf("base balance is not enough, we can't place ask orders")
}
for price := currentPriceF - gridSize; price <= s.LowerPrice; price -= gridSize { quoteBalance, ok := balances[quoteCurrency]
if ok && quoteBalance.Available > 0 {
log.Infof("placing buy order from %f ~ %f per grid %f", (currentPriceF - gridSize).Float64(), s.LowerPrice.Float64(), gridSize.Float64())
for price := currentPriceF - gridSize; price >= s.LowerPrice; price -= gridSize {
order := types.SubmitOrder{ order := types.SubmitOrder{
Symbol: s.Symbol, Symbol: s.Symbol,
Side: types.SideTypeBuy, Side: types.SideTypeBuy,
@ -108,6 +117,9 @@ func (s *Strategy) placeGridOrders(orderExecutor bbgo.OrderExecutor, session *bb
} }
orders = append(orders, order) orders = append(orders, order)
} }
} else {
log.Warnf("quote balance is not enough, we can't place bid orders")
}
createdOrders, err := orderExecutor.SubmitOrders(context.Background(), orders...) createdOrders, err := orderExecutor.SubmitOrders(context.Background(), orders...)
if err != nil { if err != nil {

View File

@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
osf=$(uname | tr '[:upper:]' '[:lower:]') osf=$(uname | tr '[:upper:]' '[:lower:]')
version=v1.3.1 version=v1.5.0
echo "Downloading bbgo" echo "Downloading bbgo"
curl -L -o bbgo https://github.com/c9s/bbgo/releases/download/$version/bbgo-$osf curl -L -o bbgo https://github.com/c9s/bbgo/releases/download/$version/bbgo-$osf

View File

@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
osf=$(uname | tr '[:upper:]' '[:lower:]') osf=$(uname | tr '[:upper:]' '[:lower:]')
version=v1.3.1 version=v1.5.0
echo "Downloading bbgo" echo "Downloading bbgo"
curl -L -o bbgo https://github.com/c9s/bbgo/releases/download/$version/bbgo-$osf curl -L -o bbgo https://github.com/c9s/bbgo/releases/download/$version/bbgo-$osf

View File

@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
osf=$(uname | tr '[:upper:]' '[:lower:]') osf=$(uname | tr '[:upper:]' '[:lower:]')
version=v1.3.1 version=v1.5.0
echo "Downloading bbgo" echo "Downloading bbgo"
curl -L -o bbgo https://github.com/c9s/bbgo/releases/download/$version/bbgo-$osf curl -L -o bbgo https://github.com/c9s/bbgo/releases/download/$version/bbgo-$osf