bbgo_origin/pkg/strategy/ktrade/minute.go

115 lines
3.9 KiB
Go
Raw Normal View History

2022-08-30 01:16:53 +00:00
package ktrade
import (
"context"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
type Minute struct {
Symbol string
Market types.Market `json:"-"`
types.IntervalWindow
// MarketOrder is the option to enable market order short.
MarketOrder bool `json:"marketOrder"`
Quantity fixedpoint.Value `json:"quantity"`
orderExecutor *bbgo.GeneralOrderExecutor
session *bbgo.ExchangeSession
activeOrders *bbgo.ActiveOrderBook
StreamBook *types.StreamOrderBook
midPrice fixedpoint.Value
bbgo.QuantityOrAmount
}
func (s *Minute) Bind(session *bbgo.ExchangeSession, orderExecutor *bbgo.GeneralOrderExecutor) {
s.session = session
s.orderExecutor = orderExecutor
position := orderExecutor.Position()
symbol := position.Symbol
s.StreamBook = types.NewStreamBook(symbol)
s.StreamBook.BindStream(session.MarketDataStream)
//store, _ := session.MarketDataStore(symbol)
session.MarketDataStream.OnMarketTrade(func(trade types.Trade) {
log.Infof("%s trade @ %f", trade.Side, trade.Price.Float64())
2022-08-30 09:49:01 +00:00
bestBid, bestAsk, hasPrice := s.StreamBook.BestBidAndAsk()
if !hasPrice {
return
}
2022-08-30 04:30:08 +00:00
s.midPrice = bestBid.Price.Add(bestAsk.Price).Div(fixedpoint.NewFromInt(2))
2022-08-30 09:49:01 +00:00
ctx := context.Background()
2022-08-30 01:16:53 +00:00
2022-08-30 09:49:01 +00:00
if trade.Side == types.SideTypeBuy && trade.Price.Div(bestAsk.Price).Compare(fixedpoint.NewFromFloat(1.00005)) > 0 {
canceled := s.orderExecutor.GracefulCancel(ctx)
if canceled != nil {
_ = s.orderExecutor.GracefulCancel(ctx)
}
2022-08-30 01:16:53 +00:00
// update ask price
2022-08-30 09:49:01 +00:00
//bestAsk, _ = s.StreamBook.BestAsk()
newAskPrice := bestAsk.Price.Mul(fixedpoint.NewFromFloat(1.001)).Mul(fixedpoint.NewFromFloat(0.25)).Add(trade.Price.Mul(fixedpoint.NewFromFloat(0.75)))
2022-08-30 01:16:53 +00:00
log.Infof("short @ %f", newAskPrice.Float64())
2022-08-30 09:49:01 +00:00
err := s.placeOrder(context.Background(), types.SideTypeSell, s.Quantity, newAskPrice.Round(2, 0), symbol)
if err != nil {
newAskPrice = bestAsk.Price.Mul(fixedpoint.NewFromFloat(1.002)).Mul(fixedpoint.NewFromFloat(0.25)).Add(trade.Price.Mul(fixedpoint.NewFromFloat(0.75)))
log.Infof("short again @ %f", newAskPrice.Float64())
_ = s.placeOrder(context.Background(), types.SideTypeSell, s.Quantity, newAskPrice.Round(2, 0), symbol)
}
} else if trade.Side == types.SideTypeSell && trade.Price.Div(bestBid.Price).Compare(fixedpoint.NewFromFloat(0.99995)) < 0 {
canceled := s.orderExecutor.GracefulCancel(ctx)
if canceled != nil {
_ = s.orderExecutor.GracefulCancel(ctx)
}
2022-08-30 01:16:53 +00:00
// update bid price
2022-08-30 09:49:01 +00:00
//bestBid, _ = s.StreamBook.BestBid()
newBidPrice := bestBid.Price.Mul(fixedpoint.NewFromFloat(0.999)).Mul(fixedpoint.NewFromFloat(0.25)).Add(trade.Price.Mul(fixedpoint.NewFromFloat(0.75)))
2022-08-30 01:16:53 +00:00
log.Infof("long @ %f", newBidPrice.Float64())
2022-08-30 09:49:01 +00:00
err := s.placeOrder(context.Background(), types.SideTypeBuy, s.Quantity, newBidPrice.Round(2, 2), symbol)
if err != nil {
newBidPrice = bestBid.Price.Mul(fixedpoint.NewFromFloat(0.998)).Mul(fixedpoint.NewFromFloat(0.25)).Add(trade.Price.Mul(fixedpoint.NewFromFloat(0.75)))
log.Infof("long again @ %f", newBidPrice.Float64())
_ = s.placeOrder(context.Background(), types.SideTypeBuy, s.Quantity, newBidPrice.Round(2, 2), symbol)
}
2022-08-30 01:16:53 +00:00
}
})
session.MarketDataStream.OnKLineClosed(types.KLineWith(symbol, s.Interval, func(kline types.KLine) {
log.Info(kline.NumberOfTrades)
}))
if !bbgo.IsBackTesting {
session.MarketDataStream.OnMarketTrade(func(trade types.Trade) {
})
}
}
2022-08-30 09:49:01 +00:00
func (s *Minute) placeOrder(ctx context.Context, side types.SideType, quantity fixedpoint.Value, price fixedpoint.Value, symbol string) error {
2022-08-30 01:16:53 +00:00
market, _ := s.session.Market(symbol)
_, err := s.orderExecutor.SubmitOrders(ctx, types.SubmitOrder{
Symbol: symbol,
Market: market,
Side: side,
Type: types.OrderTypeLimitMaker,
Quantity: quantity,
Price: price,
//TimeInForce: types.TimeInForceGTC,
Tag: "ktrade",
})
if err != nil {
log.WithError(err).Errorf("can not place order")
}
2022-08-30 09:49:01 +00:00
return err
2022-08-30 01:16:53 +00:00
}