2020-10-24 10:22:23 +00:00
|
|
|
package xpuremaker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/bbgo"
|
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
bbgo.RegisterExchangeStrategy("xpuremaker", &Strategy{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type Strategy struct {
|
|
|
|
Symbol string `json:"symbol"`
|
|
|
|
Side string `json:"side"`
|
|
|
|
NumOrders int `json:"numOrders"`
|
|
|
|
BehindVolume fixedpoint.Value `json:"behindVolume"`
|
|
|
|
PriceTick fixedpoint.Value `json:"priceTick"`
|
|
|
|
BaseQuantity fixedpoint.Value `json:"baseQuantity"`
|
|
|
|
BuySellRatio float64 `json:"buySellRatio"`
|
|
|
|
|
2020-10-25 16:26:17 +00:00
|
|
|
book *types.StreamOrderBook
|
|
|
|
activeOrders map[string]types.Order
|
2020-10-24 10:22:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(symbol string) *Strategy {
|
|
|
|
return &Strategy{
|
|
|
|
Symbol: symbol,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-25 16:26:17 +00:00
|
|
|
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
|
2020-10-24 10:22:23 +00:00
|
|
|
session.Subscribe(types.BookChannel, s.Symbol, types.SubscribeOptions{})
|
|
|
|
|
|
|
|
s.book = types.NewStreamBook(s.Symbol)
|
|
|
|
s.book.BindStream(session.Stream)
|
|
|
|
|
2020-10-25 16:26:17 +00:00
|
|
|
s.activeOrders = make(map[string]types.Order)
|
|
|
|
|
2020-10-24 10:22:23 +00:00
|
|
|
// We can move the go routine to the parent level.
|
|
|
|
go func() {
|
|
|
|
ticker := time.NewTicker(1 * time.Minute)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
2020-10-25 10:26:10 +00:00
|
|
|
s.update(orderExecutor)
|
|
|
|
|
2020-10-24 10:22:23 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
|
|
|
|
case <-s.book.C:
|
2020-10-25 10:26:10 +00:00
|
|
|
s.update(orderExecutor)
|
2020-10-24 10:22:23 +00:00
|
|
|
|
|
|
|
case <-ticker.C:
|
2020-10-25 10:26:10 +00:00
|
|
|
s.update(orderExecutor)
|
2020-10-24 10:22:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
/*
|
|
|
|
session.Stream.OnKLineClosed(func(kline types.KLine) {
|
|
|
|
market, ok := session.Market(s.Symbol)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
quoteBalance, ok := session.Account.Balance(market.QuoteCurrency)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = quoteBalance
|
|
|
|
|
|
|
|
err := orderExecutor.SubmitOrder(ctx, types.SubmitOrder{
|
|
|
|
Symbol: kline.Symbol,
|
|
|
|
Side: types.SideTypeBuy,
|
|
|
|
Type: types.OrderTypeMarket,
|
|
|
|
Quantity: 0.01,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("submit order error")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
*/
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-25 16:26:17 +00:00
|
|
|
func (s *Strategy) cancelOrders(orderExecutor bbgo.OrderExecutor) {
|
|
|
|
var deletedIDs []string
|
|
|
|
for clientOrderID, o := range s.activeOrders {
|
|
|
|
log.Infof("canceling order: %+v", o)
|
|
|
|
|
|
|
|
if err := orderExecutor.Session().Exchange.CancelOrders(context.Background(), o); err != nil {
|
|
|
|
log.WithError(err).Error("cancel order error")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
deletedIDs = append(deletedIDs, clientOrderID)
|
|
|
|
}
|
|
|
|
s.book.C.Drain(1*time.Second, 3*time.Second)
|
|
|
|
|
|
|
|
for _, id := range deletedIDs {
|
|
|
|
delete(s.activeOrders, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) update(orderExecutor bbgo.OrderExecutor) {
|
|
|
|
s.cancelOrders(orderExecutor)
|
|
|
|
|
2020-10-24 10:22:23 +00:00
|
|
|
switch s.Side {
|
|
|
|
case "buy":
|
2020-10-25 10:26:10 +00:00
|
|
|
s.updateOrders(orderExecutor, types.SideTypeBuy)
|
2020-10-24 10:22:23 +00:00
|
|
|
case "sell":
|
2020-10-25 10:26:10 +00:00
|
|
|
s.updateOrders(orderExecutor, types.SideTypeSell)
|
2020-10-24 10:22:23 +00:00
|
|
|
case "both":
|
2020-10-25 10:26:10 +00:00
|
|
|
s.updateOrders(orderExecutor, types.SideTypeBuy)
|
|
|
|
s.updateOrders(orderExecutor, types.SideTypeSell)
|
|
|
|
|
|
|
|
default:
|
|
|
|
log.Panicf("undefined side: %s", s.Side)
|
2020-10-24 10:22:23 +00:00
|
|
|
}
|
2020-10-25 16:26:17 +00:00
|
|
|
|
|
|
|
s.book.C.Drain(1*time.Second, 3*time.Second)
|
2020-10-24 10:22:23 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 16:26:17 +00:00
|
|
|
func (s *Strategy) updateOrders(orderExecutor bbgo.OrderExecutor, side types.SideType) {
|
2020-10-24 10:22:23 +00:00
|
|
|
book := s.book.Copy()
|
|
|
|
|
|
|
|
var pvs types.PriceVolumeSlice
|
|
|
|
|
|
|
|
switch side {
|
|
|
|
case types.SideTypeBuy:
|
|
|
|
pvs = book.Bids
|
|
|
|
case types.SideTypeSell:
|
|
|
|
pvs = book.Asks
|
|
|
|
}
|
|
|
|
|
|
|
|
if pvs == nil || len(pvs) == 0 {
|
|
|
|
log.Warn("empty bids or asks")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-25 10:26:10 +00:00
|
|
|
log.Infof("placing order behind volume: %f", s.BehindVolume.Float64())
|
|
|
|
|
2020-10-24 10:22:23 +00:00
|
|
|
index := pvs.IndexByVolumeDepth(s.BehindVolume)
|
|
|
|
if index == -1 {
|
|
|
|
// do not place orders
|
|
|
|
log.Warn("depth is not enough")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var price = pvs[index].Price
|
|
|
|
var orders = s.generateOrders(s.Symbol, side, price, s.PriceTick, s.BaseQuantity, s.NumOrders)
|
|
|
|
if len(orders) == 0 {
|
|
|
|
log.Warn("empty orders")
|
|
|
|
return
|
|
|
|
}
|
2020-10-25 16:26:17 +00:00
|
|
|
|
2020-10-24 10:22:23 +00:00
|
|
|
log.Infof("submitting %d orders", len(orders))
|
2020-10-25 16:26:17 +00:00
|
|
|
createdOrders, err := orderExecutor.SubmitOrders(context.Background(), orders...)
|
|
|
|
if err != nil {
|
2020-10-25 10:26:10 +00:00
|
|
|
log.WithError(err).Errorf("order submit error")
|
|
|
|
return
|
|
|
|
}
|
2020-10-25 16:26:17 +00:00
|
|
|
|
|
|
|
// add created orders to the list
|
|
|
|
for i, o := range createdOrders {
|
|
|
|
log.Infof("adding order: %s => %+v", o.ClientOrderID, o)
|
|
|
|
s.activeOrders[o.ClientOrderID] = createdOrders[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("active orders: %+v", s.activeOrders)
|
2020-10-24 10:22:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) generateOrders(symbol string, side types.SideType, price, priceTick, baseVolume fixedpoint.Value, numOrders int) (orders []types.SubmitOrder) {
|
|
|
|
var expBase = fixedpoint.NewFromFloat(0.0)
|
|
|
|
|
|
|
|
switch side {
|
|
|
|
case types.SideTypeBuy:
|
|
|
|
if priceTick > 0 {
|
|
|
|
priceTick = -priceTick
|
|
|
|
}
|
|
|
|
|
|
|
|
case types.SideTypeSell:
|
|
|
|
if priceTick < 0 {
|
|
|
|
priceTick = -priceTick
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < numOrders; i++ {
|
|
|
|
volume := math.Exp(expBase.Float64()) * baseVolume.Float64()
|
|
|
|
|
|
|
|
// skip order less than 10usd
|
|
|
|
if volume*price.Float64() < 10.0 {
|
|
|
|
log.Warnf("amount too small (< 10usd). price=%f volume=%f amount=%f", price.Float64(), volume, volume*price.Float64())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
orders = append(orders, types.SubmitOrder{
|
|
|
|
Symbol: symbol,
|
|
|
|
Side: side,
|
|
|
|
Type: types.OrderTypeLimit,
|
|
|
|
Price: price.Float64(),
|
|
|
|
Quantity: volume,
|
|
|
|
})
|
|
|
|
|
2020-10-25 10:26:10 +00:00
|
|
|
log.Infof("%s order: %.2f @ %f", side, volume, price.Float64())
|
2020-10-24 10:22:23 +00:00
|
|
|
|
|
|
|
if len(orders) >= numOrders {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
price = price + priceTick
|
|
|
|
declog := math.Log10(math.Abs(priceTick.Float64()))
|
|
|
|
expBase += fixedpoint.NewFromFloat(math.Pow10(-int(declog)) * math.Abs(priceTick.Float64()))
|
2020-10-25 10:26:10 +00:00
|
|
|
// log.Infof("expBase: %f", expBase.Float64())
|
2020-10-24 10:22:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return orders
|
|
|
|
}
|