diff --git a/config/grid.yaml b/config/grid.yaml index d6c09e8f3..88d1ffd06 100644 --- a/config/grid.yaml +++ b/config/grid.yaml @@ -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: binance: exchange: binance @@ -63,7 +45,7 @@ exchangeStrategies: - on: max grid: symbol: BTCUSDT - quantity: 0.01 + quantity: 0.001 gridNumber: 10 profitSpread: 50.0 upperPrice: 11000.0 diff --git a/pkg/bbgo/environment.go b/pkg/bbgo/environment.go index d0552475b..25843fc12 100644 --- a/pkg/bbgo/environment.go +++ b/pkg/bbgo/environment.go @@ -241,7 +241,7 @@ func (environ *Environment) Init(ctx context.Context) (err error) { if lastPriceTime == emptyTime { session.lastPrices[symbol] = lastKLine.Close lastPriceTime = lastKLine.EndTime - } else if lastPriceTime.Before(lastKLine.EndTime) { + } else if lastKLine.EndTime.After(lastPriceTime) { session.lastPrices[symbol] = lastKLine.Close lastPriceTime = lastKLine.EndTime } @@ -251,6 +251,8 @@ func (environ *Environment) Init(ctx context.Context) (err error) { marketDataStore.AddKLine(k) } } + + log.Infof("last price: %f", session.lastPrices[symbol]) } if environ.TradeService != nil { diff --git a/pkg/exchange/max/maxapi/websocket.go b/pkg/exchange/max/maxapi/websocket.go index 9f7cf8ded..dcdf7143c 100644 --- a/pkg/exchange/max/maxapi/websocket.go +++ b/pkg/exchange/max/maxapi/websocket.go @@ -153,7 +153,9 @@ func (s *WebSocketService) read(ctx context.Context) { continue } - s.dispatch(m) + if m != nil { + s.dispatch(m) + } } } } diff --git a/pkg/strategy/grid/strategy.go b/pkg/strategy/grid/strategy.go index 1e565332a..0b29d6eb8 100644 --- a/pkg/strategy/grid/strategy.go +++ b/pkg/strategy/grid/strategy.go @@ -65,16 +65,14 @@ type Strategy struct { } func (s *Strategy) placeGridOrders(orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) { + log.Infof("placing grid orders...") + quoteCurrency := s.Market.QuoteCurrency balances := session.Account.Balances() - balance, ok := balances[quoteCurrency] - if !ok || balance.Available <= 0 { - return - } - currentPrice, ok := session.LastPrice(s.Symbol) if !ok { + log.Warn("last price not found, skipping") return } @@ -83,30 +81,44 @@ func (s *Strategy) placeGridOrders(orderExecutor bbgo.OrderExecutor, session *bb gridSize := priceRange.Div(fixedpoint.NewFromInt(s.GridNum)) var orders []types.SubmitOrder - for price := currentPriceF + gridSize; price <= s.UpperPrice; price += gridSize { - order := types.SubmitOrder{ - Symbol: s.Symbol, - Side: types.SideTypeSell, - Type: types.OrderTypeLimit, - Market: s.Market, - Quantity: s.Quantity, - Price: price.Float64(), - TimeInForce: "GTC", + + 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 { + order := types.SubmitOrder{ + Symbol: s.Symbol, + Side: types.SideTypeSell, + 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 { - order := types.SubmitOrder{ - Symbol: s.Symbol, - Side: types.SideTypeBuy, - Type: types.OrderTypeLimit, - Market: s.Market, - Quantity: s.Quantity, - Price: price.Float64(), - TimeInForce: "GTC", + 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{ + Symbol: s.Symbol, + Side: types.SideTypeBuy, + 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...) diff --git a/scripts/setup-bollgrid.sh b/scripts/setup-bollgrid.sh index 62ebb8efb..f75b9f21f 100755 --- a/scripts/setup-bollgrid.sh +++ b/scripts/setup-bollgrid.sh @@ -1,6 +1,6 @@ #!/bin/bash osf=$(uname | tr '[:upper:]' '[:lower:]') -version=v1.3.1 +version=v1.5.0 echo "Downloading bbgo" curl -L -o bbgo https://github.com/c9s/bbgo/releases/download/$version/bbgo-$osf diff --git a/scripts/setup-grid.sh b/scripts/setup-grid.sh index d0edf05c6..498c8bb78 100755 --- a/scripts/setup-grid.sh +++ b/scripts/setup-grid.sh @@ -1,6 +1,6 @@ #!/bin/bash osf=$(uname | tr '[:upper:]' '[:lower:]') -version=v1.3.1 +version=v1.5.0 echo "Downloading bbgo" curl -L -o bbgo https://github.com/c9s/bbgo/releases/download/$version/bbgo-$osf diff --git a/scripts/setup.sh b/scripts/setup.sh index 9a71c4b55..c80585c4f 100755 --- a/scripts/setup.sh +++ b/scripts/setup.sh @@ -1,6 +1,6 @@ #!/bin/bash osf=$(uname | tr '[:upper:]' '[:lower:]') -version=v1.3.1 +version=v1.5.0 echo "Downloading bbgo" curl -L -o bbgo https://github.com/c9s/bbgo/releases/download/$version/bbgo-$osf