check balances

This commit is contained in:
narumi 2023-10-07 13:01:45 +08:00
parent a0efa2769d
commit 81ea074b4f
2 changed files with 65 additions and 20 deletions

View File

@ -3,7 +3,10 @@ exchangeStrategies:
- on: max - on: max
randomtrader: randomtrader:
symbol: USDCUSDT symbol: USDCUSDT
cronExpression: "@every 8h" # https://pkg.go.dev/github.com/robfig/cron#hdr-Predefined_schedules # https://pkg.go.dev/github.com/robfig/cron#hdr-Predefined_schedules
cronExpression: "@every 1m"
quantity: 8 quantity: 8
# adjust quantity by minimal notional and minimal quantity
adjustQuantity: true
onStart: true onStart: true
dryRun: true dryRun: true

View File

@ -32,8 +32,11 @@ type Strategy struct {
Symbol string `json:"symbol"` Symbol string `json:"symbol"`
CronExpression string `json:"cronExpression"` CronExpression string `json:"cronExpression"`
Quantity fixedpoint.Value `json:"quantity"` Quantity fixedpoint.Value `json:"quantity"`
AdjustQuantity bool `json:"adjustQuantity"`
OnStart bool `json:"onStart"` OnStart bool `json:"onStart"`
DryRun bool `json:"dryRun"` DryRun bool `json:"dryRun"`
cron *cron.Cron
} }
func (s *Strategy) Defaults() error { func (s *Strategy) Defaults() error {
@ -67,7 +70,7 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
session.UserDataStream.OnStart(func() { session.UserDataStream.OnStart(func() {
if s.OnStart { if s.OnStart {
s.trade(ctx) s.placeOrder()
} }
}) })
@ -77,39 +80,78 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
_ = s.OrderExecutor.GracefulCancel(ctx) _ = s.OrderExecutor.GracefulCancel(ctx)
}) })
cron := cron.New() s.cron = cron.New()
cron.AddFunc(s.CronExpression, func() { s.cron.AddFunc(s.CronExpression, s.placeOrder)
s.trade(ctx) s.cron.Start()
})
cron.Start()
return nil return nil
} }
func (s *Strategy) trade(ctx context.Context) { func (s *Strategy) placeOrder() {
orderForm := []types.SubmitOrder{ ctx := context.Background()
{
Symbol: s.Symbol, baseBalance, ok := s.Session.GetAccount().Balance(s.Market.BaseCurrency)
Side: types.SideTypeBuy, if !ok {
Type: types.OrderTypeMarket, log.Errorf("base balance not found")
Quantity: s.Quantity, return
}, { }
quoteBalance, ok := s.Session.GetAccount().Balance(s.Market.QuoteCurrency)
if !ok {
log.Errorf("quote balance not found")
return
}
ticker, err := s.Session.Exchange.QueryTicker(ctx, s.Symbol)
if err != nil {
log.WithError(err).Error("query ticker error")
return
}
sellQuantity := s.Quantity
buyQuantity := s.Quantity
if s.AdjustQuantity {
sellQuantity = s.Market.AdjustQuantityByMinNotional(s.Quantity, ticker.Sell)
buyQuantity = fixedpoint.Max(s.Quantity, s.Market.MinQuantity)
}
orderForm := []types.SubmitOrder{}
if baseBalance.Available.Compare(sellQuantity) > 0 {
orderForm = append(orderForm, types.SubmitOrder{
Symbol: s.Symbol, Symbol: s.Symbol,
Side: types.SideTypeSell, Side: types.SideTypeSell,
Type: types.OrderTypeMarket, Type: types.OrderTypeMarket,
Quantity: s.Quantity, Quantity: sellQuantity,
}, })
} else {
log.Infof("base balance: %s is not enough", baseBalance.Available.String())
} }
submitOrder := orderForm[rand.Intn(2)] if quoteBalance.Available.Div(ticker.Buy).Compare(buyQuantity) > 0 {
log.Infof("submit order: %s", submitOrder.String()) orderForm = append(orderForm, types.SubmitOrder{
Symbol: s.Symbol,
Side: types.SideTypeBuy,
Type: types.OrderTypeMarket,
Quantity: buyQuantity,
})
} else {
log.Infof("quote balance: %s is not enough", quoteBalance.Available.String())
}
var order types.SubmitOrder
if len(orderForm) == 0 {
log.Infof("both base and quote balance are not enough, skip submit order")
return
} else {
order = orderForm[rand.Intn(len(orderForm))]
}
log.Infof("submit order: %s", order.String())
if s.DryRun { if s.DryRun {
log.Infof("dry run, skip submit order") log.Infof("dry run, skip submit order")
return return
} }
_, err := s.OrderExecutor.SubmitOrders(ctx, submitOrder) _, err = s.OrderExecutor.SubmitOrders(ctx, order)
if err != nil { if err != nil {
log.WithError(err).Error("submit order error") log.WithError(err).Error("submit order error")
return return