mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 00:35:15 +00:00
bollgrid: check balance before submit reverse order
This commit is contained in:
parent
9f97947717
commit
f1309c46fc
|
@ -3,6 +3,7 @@ package bollgrid
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
@ -275,24 +276,30 @@ func (s *Strategy) updateOrders(orderExecutor bbgo.OrderExecutor, session *bbgo.
|
||||||
s.activeOrders.Print()
|
s.activeOrders.Print()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Strategy) submitReverseOrder(order types.Order) {
|
func (s *Strategy) submitReverseOrder(order types.Order, session *bbgo.ExchangeSession) {
|
||||||
|
balances := session.Account.Balances()
|
||||||
|
|
||||||
var side = order.Side.Reverse()
|
var side = order.Side.Reverse()
|
||||||
var price = order.Price
|
var price = order.Price
|
||||||
|
var quantity = order.Quantity
|
||||||
|
|
||||||
switch side {
|
switch side {
|
||||||
case types.SideTypeSell:
|
case types.SideTypeSell:
|
||||||
price += s.ProfitSpread.Float64()
|
price += s.ProfitSpread.Float64()
|
||||||
|
maxQuantity := balances[s.Market.BaseCurrency].Available.Float64()
|
||||||
|
quantity = math.Min(quantity, maxQuantity)
|
||||||
|
|
||||||
case types.SideTypeBuy:
|
case types.SideTypeBuy:
|
||||||
price -= s.ProfitSpread.Float64()
|
price -= s.ProfitSpread.Float64()
|
||||||
|
maxQuantity := balances[s.Market.QuoteCurrency].Available.Float64() / price
|
||||||
|
quantity = math.Min(quantity, maxQuantity)
|
||||||
}
|
}
|
||||||
|
|
||||||
submitOrder := types.SubmitOrder{
|
submitOrder := types.SubmitOrder{
|
||||||
Symbol: s.Symbol,
|
Symbol: s.Symbol,
|
||||||
Side: side,
|
Side: side,
|
||||||
Type: types.OrderTypeLimit,
|
Type: types.OrderTypeLimit,
|
||||||
Quantity: order.Quantity,
|
Quantity: quantity,
|
||||||
Price: price,
|
Price: price,
|
||||||
TimeInForce: "GTC",
|
TimeInForce: "GTC",
|
||||||
}
|
}
|
||||||
|
@ -325,7 +332,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
||||||
// we don't persist orders so that we can not clear the previous orders for now. just need time to support this.
|
// we don't persist orders so that we can not clear the previous orders for now. just need time to support this.
|
||||||
s.activeOrders = bbgo.NewLocalActiveOrderBook()
|
s.activeOrders = bbgo.NewLocalActiveOrderBook()
|
||||||
s.activeOrders.OnFilled(func(o types.Order) {
|
s.activeOrders.OnFilled(func(o types.Order) {
|
||||||
s.submitReverseOrder(o)
|
s.submitReverseOrder(o, session)
|
||||||
})
|
})
|
||||||
s.activeOrders.BindStream(session.Stream)
|
s.activeOrders.BindStream(session.Stream)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user