2021-08-26 03:31:36 +00:00
|
|
|
package etf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/bbgo"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
const ID = "etf"
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
bbgo.RegisterStrategy(ID, &Strategy{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type Strategy struct {
|
|
|
|
Market types.Market
|
|
|
|
|
|
|
|
Notifiability *bbgo.Notifiability
|
|
|
|
|
|
|
|
|
|
|
|
TotalAmount fixedpoint.Value `json:"totalAmount,omitempty"`
|
|
|
|
|
|
|
|
// Interval is the period that you want to submit order
|
|
|
|
Duration types.Duration `json:"duration"`
|
|
|
|
|
|
|
|
Index map[string]fixedpoint.Value `json:"index"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) ID() string {
|
|
|
|
return ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) Validate() error {
|
2022-02-04 03:56:49 +00:00
|
|
|
if s.TotalAmount.IsZero() {
|
2021-08-26 03:31:36 +00:00
|
|
|
return errors.New("amount can not be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
|
|
|
|
go func() {
|
|
|
|
ticker := time.NewTicker(s.Duration.Duration())
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
s.Notifiability.Notify("ETF orders will be executed every %s", s.Duration.Duration().String())
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
|
|
|
|
case <-ticker.C:
|
|
|
|
totalAmount := s.TotalAmount
|
|
|
|
for symbol, ratio := range s.Index {
|
|
|
|
amount := totalAmount.Mul(ratio)
|
|
|
|
|
|
|
|
ticker, err := session.Exchange.QueryTicker(ctx, symbol)
|
|
|
|
if err != nil {
|
2021-08-26 03:57:17 +00:00
|
|
|
s.Notifiability.Notify("query ticker error: %s", err.Error())
|
2021-08-26 03:31:36 +00:00
|
|
|
log.WithError(err).Error("query ticker error")
|
2021-08-26 03:57:17 +00:00
|
|
|
break
|
2021-08-26 03:31:36 +00:00
|
|
|
}
|
|
|
|
|
2022-02-04 03:56:49 +00:00
|
|
|
askPrice := ticker.Sell
|
2021-08-26 03:31:36 +00:00
|
|
|
quantity := askPrice.Div(amount)
|
|
|
|
|
|
|
|
// execute orders
|
|
|
|
quoteBalance, ok := session.Account.Balance(s.Market.QuoteCurrency)
|
|
|
|
if !ok {
|
2021-08-26 03:57:17 +00:00
|
|
|
break
|
2021-08-26 03:31:36 +00:00
|
|
|
}
|
2022-02-04 03:56:49 +00:00
|
|
|
if quoteBalance.Available.Compare(amount) < 0 {
|
|
|
|
s.Notifiability.Notify("Quote balance %s is not enough: %s < %s", s.Market.QuoteCurrency, quoteBalance.Available.String(), amount.String())
|
2021-08-26 03:57:17 +00:00
|
|
|
break
|
2021-08-26 03:31:36 +00:00
|
|
|
}
|
|
|
|
|
2022-02-04 03:56:49 +00:00
|
|
|
s.Notifiability.Notify("Submitting etf order %s quantity %s at price %s (index ratio %s)",
|
2021-08-26 03:31:36 +00:00
|
|
|
symbol,
|
2022-02-04 03:56:49 +00:00
|
|
|
quantity.String(),
|
|
|
|
askPrice.String(),
|
|
|
|
ratio.Percentage())
|
2021-08-26 03:31:36 +00:00
|
|
|
_, err = orderExecutor.SubmitOrders(ctx, types.SubmitOrder{
|
|
|
|
Symbol: symbol,
|
|
|
|
Side: types.SideTypeBuy,
|
|
|
|
Type: types.OrderTypeMarket,
|
2022-02-04 03:56:49 +00:00
|
|
|
Quantity: quantity,
|
2021-08-26 03:31:36 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("submit order error")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|