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:
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

View File

@ -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 {

View File

@ -153,7 +153,9 @@ func (s *WebSocketService) read(ctx context.Context) {
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) {
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...)

View File

@ -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

View File

@ -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

View File

@ -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