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,7 +153,9 @@ func (s *WebSocketService) read(ctx context.Context) {
continue continue
} }
s.dispatch(m) if m != nil {
s.dispatch(m)
}
} }
} }
} }

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,30 +81,44 @@ 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
for price := currentPriceF + gridSize; price <= s.UpperPrice; price += gridSize {
order := types.SubmitOrder{ baseBalance, ok := balances[s.Market.BaseCurrency]
Symbol: s.Symbol, if ok && baseBalance.Available > 0 {
Side: types.SideTypeSell, log.Infof("placing sell order from %f ~ %f per grid %f", (currentPriceF + gridSize).Float64(), s.UpperPrice.Float64(), gridSize.Float64())
Type: types.OrderTypeLimit, for price := currentPriceF + gridSize; price <= s.UpperPrice; price += gridSize {
Market: s.Market, order := types.SubmitOrder{
Quantity: s.Quantity, Symbol: s.Symbol,
Price: price.Float64(), Side: types.SideTypeSell,
TimeInForce: "GTC", Type: types.OrderTypeLimit,
Market: s.Market,
Quantity: s.Quantity,
Price: price.Float64(),
TimeInForce: "GTC",
}
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]
order := types.SubmitOrder{ if ok && quoteBalance.Available > 0 {
Symbol: s.Symbol, log.Infof("placing buy order from %f ~ %f per grid %f", (currentPriceF - gridSize).Float64(), s.LowerPrice.Float64(), gridSize.Float64())
Side: types.SideTypeBuy,
Type: types.OrderTypeLimit, for price := currentPriceF - gridSize; price >= s.LowerPrice; price -= gridSize {
Market: s.Market, order := types.SubmitOrder{
Quantity: s.Quantity, Symbol: s.Symbol,
Price: price.Float64(), Side: types.SideTypeBuy,
TimeInForce: "GTC", Type: types.OrderTypeLimit,
Market: s.Market,
Quantity: s.Quantity,
Price: price.Float64(),
TimeInForce: "GTC",
}
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...)

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