Merge pull request #208 from LarryLuTW/fix/submit-reverse

This commit is contained in:
Yo-An Lin 2021-05-03 16:27:47 +08:00 committed by GitHub
commit a634635999

View File

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