2022-06-18 08:31:53 +00:00
|
|
|
package bbgo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-06-27 10:17:57 +00:00
|
|
|
"strings"
|
2022-06-18 08:31:53 +00:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type NotifyFunc func(obj interface{}, args ...interface{})
|
|
|
|
|
|
|
|
// GeneralOrderExecutor implements the general order executor for strategy
|
|
|
|
type GeneralOrderExecutor struct {
|
|
|
|
session *ExchangeSession
|
|
|
|
symbol string
|
|
|
|
strategy string
|
|
|
|
strategyInstanceID string
|
|
|
|
position *types.Position
|
|
|
|
activeMakerOrders *ActiveOrderBook
|
|
|
|
orderStore *OrderStore
|
|
|
|
tradeCollector *TradeCollector
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewGeneralOrderExecutor(session *ExchangeSession, symbol, strategy, strategyInstanceID string, position *types.Position) *GeneralOrderExecutor {
|
|
|
|
// Always update the position fields
|
|
|
|
position.Strategy = strategy
|
|
|
|
position.StrategyInstanceID = strategyInstanceID
|
|
|
|
|
|
|
|
orderStore := NewOrderStore(symbol)
|
|
|
|
return &GeneralOrderExecutor{
|
|
|
|
session: session,
|
|
|
|
symbol: symbol,
|
|
|
|
strategy: strategy,
|
|
|
|
strategyInstanceID: strategyInstanceID,
|
|
|
|
position: position,
|
|
|
|
activeMakerOrders: NewActiveOrderBook(symbol),
|
|
|
|
orderStore: orderStore,
|
|
|
|
tradeCollector: NewTradeCollector(symbol, position, orderStore),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *GeneralOrderExecutor) BindEnvironment(environ *Environment) {
|
|
|
|
e.tradeCollector.OnProfit(func(trade types.Trade, profit *types.Profit) {
|
|
|
|
environ.RecordPosition(e.position, trade, profit)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *GeneralOrderExecutor) BindTradeStats(tradeStats *types.TradeStats) {
|
|
|
|
e.tradeCollector.OnProfit(func(trade types.Trade, profit *types.Profit) {
|
|
|
|
if profit == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tradeStats.Add(profit.Profit)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-06-19 04:29:36 +00:00
|
|
|
func (e *GeneralOrderExecutor) BindProfitStats(profitStats *types.ProfitStats) {
|
2022-06-18 08:31:53 +00:00
|
|
|
e.tradeCollector.OnProfit(func(trade types.Trade, profit *types.Profit) {
|
|
|
|
profitStats.AddTrade(trade)
|
|
|
|
if profit == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
profitStats.AddProfit(*profit)
|
2022-07-01 09:32:40 +00:00
|
|
|
Notify(profitStats)
|
2022-06-18 08:31:53 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-06-19 05:01:22 +00:00
|
|
|
func (e *GeneralOrderExecutor) Bind() {
|
2022-06-18 08:31:53 +00:00
|
|
|
e.activeMakerOrders.BindStream(e.session.UserDataStream)
|
|
|
|
e.orderStore.BindStream(e.session.UserDataStream)
|
|
|
|
|
|
|
|
// trade notify
|
|
|
|
e.tradeCollector.OnTrade(func(trade types.Trade, profit, netProfit fixedpoint.Value) {
|
2022-06-19 05:01:22 +00:00
|
|
|
Notify(trade)
|
2022-06-18 08:31:53 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
e.tradeCollector.OnPositionUpdate(func(position *types.Position) {
|
|
|
|
log.Infof("position changed: %s", position)
|
2022-06-19 05:01:22 +00:00
|
|
|
Notify(position)
|
2022-06-18 08:31:53 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
e.tradeCollector.BindStream(e.session.UserDataStream)
|
|
|
|
}
|
|
|
|
|
2022-06-30 16:57:19 +00:00
|
|
|
// CancelOrders cancels the given order objects directly
|
2022-06-26 08:13:58 +00:00
|
|
|
func (e *GeneralOrderExecutor) CancelOrders(ctx context.Context, orders ...types.Order) error {
|
2022-06-30 16:57:19 +00:00
|
|
|
return e.session.Exchange.CancelOrders(ctx, orders...)
|
2022-06-26 08:13:58 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 07:57:59 +00:00
|
|
|
func (e *GeneralOrderExecutor) SubmitOrders(ctx context.Context, submitOrders ...types.SubmitOrder) (types.OrderSlice, error) {
|
2022-06-18 08:31:53 +00:00
|
|
|
formattedOrders, err := e.session.FormatOrders(submitOrders)
|
|
|
|
if err != nil {
|
2022-06-19 07:57:59 +00:00
|
|
|
return nil, err
|
2022-06-18 08:31:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
createdOrders, err := e.session.Exchange.SubmitOrders(ctx, formattedOrders...)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Errorf("can not place orders")
|
|
|
|
}
|
|
|
|
|
|
|
|
e.orderStore.Add(createdOrders...)
|
|
|
|
e.activeMakerOrders.Add(createdOrders...)
|
|
|
|
e.tradeCollector.Process()
|
2022-06-19 07:57:59 +00:00
|
|
|
return createdOrders, err
|
2022-06-18 08:31:53 +00:00
|
|
|
}
|
|
|
|
|
2022-06-30 16:57:19 +00:00
|
|
|
// GracefulCancelActiveOrderBook cancels the orders from the active orderbook.
|
|
|
|
func (e *GeneralOrderExecutor) GracefulCancelActiveOrderBook(ctx context.Context, activeOrders *ActiveOrderBook) error {
|
|
|
|
if err := activeOrders.GracefulCancel(ctx, e.session.Exchange); err != nil {
|
2022-06-18 08:31:53 +00:00
|
|
|
log.WithError(err).Errorf("graceful cancel order error")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-06-19 05:40:10 +00:00
|
|
|
e.tradeCollector.Process()
|
2022-06-18 08:31:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-30 16:57:19 +00:00
|
|
|
// GracefulCancel cancels all active maker orders
|
|
|
|
func (e *GeneralOrderExecutor) GracefulCancel(ctx context.Context) error {
|
|
|
|
return e.GracefulCancelActiveOrderBook(ctx, e.activeMakerOrders)
|
|
|
|
}
|
|
|
|
|
2022-06-27 10:17:57 +00:00
|
|
|
func (e *GeneralOrderExecutor) ClosePosition(ctx context.Context, percentage fixedpoint.Value, tags ...string) error {
|
2022-06-19 03:20:29 +00:00
|
|
|
submitOrder := e.position.NewMarketCloseOrder(percentage)
|
2022-06-18 08:31:53 +00:00
|
|
|
if submitOrder == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-27 10:17:57 +00:00
|
|
|
submitOrder.Tag = strings.Join(tags, ",")
|
2022-06-19 07:57:59 +00:00
|
|
|
_, err := e.SubmitOrders(ctx, *submitOrder)
|
|
|
|
return err
|
2022-06-18 08:31:53 +00:00
|
|
|
}
|
2022-06-19 05:40:10 +00:00
|
|
|
|
|
|
|
func (e *GeneralOrderExecutor) TradeCollector() *TradeCollector {
|
|
|
|
return e.tradeCollector
|
|
|
|
}
|
2022-06-26 08:13:58 +00:00
|
|
|
|
|
|
|
func (e *GeneralOrderExecutor) Session() *ExchangeSession {
|
|
|
|
return e.session
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *GeneralOrderExecutor) Position() *types.Position {
|
|
|
|
return e.position
|
|
|
|
}
|