all: move trade store and order store into pkg/core

This commit is contained in:
c9s 2023-07-04 21:42:24 +08:00
parent 1f98731636
commit f1828beac8
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
18 changed files with 54 additions and 39 deletions

View File

@ -9,6 +9,7 @@ import (
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/core"
"github.com/c9s/bbgo/pkg/sigchan"
"github.com/c9s/bbgo/pkg/types"
)
@ -16,6 +17,7 @@ import (
const CancelOrderWaitTime = 20 * time.Millisecond
// ActiveOrderBook manages the local active order books.
//
//go:generate callbackgen -type ActiveOrderBook
type ActiveOrderBook struct {
Symbol string
@ -209,7 +211,7 @@ func (b *ActiveOrderBook) GracefulCancel(ctx context.Context, ex types.Exchange,
continue
}
openOrderStore := NewOrderStore(symbol)
openOrderStore := core.NewOrderStore(symbol)
openOrderStore.Add(openOrders...)
for _, o := range orders {
// if it's not on the order book (open orders), we should remove it from our local side

View File

@ -10,6 +10,7 @@ import (
log "github.com/sirupsen/logrus"
"go.uber.org/multierr"
"github.com/c9s/bbgo/pkg/core"
"github.com/c9s/bbgo/pkg/exchange/retry"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
@ -33,7 +34,7 @@ type GeneralOrderExecutor struct {
strategyInstanceID string
position *types.Position
activeMakerOrders *ActiveOrderBook
orderStore *OrderStore
orderStore *core.OrderStore
tradeCollector *TradeCollector
logger log.FieldLogger
@ -49,7 +50,7 @@ func NewGeneralOrderExecutor(session *ExchangeSession, symbol, strategy, strateg
position.Strategy = strategy
position.StrategyInstanceID = strategyInstanceID
orderStore := NewOrderStore(symbol)
orderStore := core.NewOrderStore(symbol)
executor := &GeneralOrderExecutor{
session: session,
@ -121,7 +122,7 @@ func (e *GeneralOrderExecutor) marginAssetMaxBorrowableUpdater(ctx context.Conte
}
}
func (e *GeneralOrderExecutor) OrderStore() *OrderStore {
func (e *GeneralOrderExecutor) OrderStore() *core.OrderStore {
return e.orderStore
}

View File

@ -15,6 +15,7 @@ import (
"github.com/spf13/viper"
"github.com/c9s/bbgo/pkg/cache"
"github.com/c9s/bbgo/pkg/core"
"github.com/c9s/bbgo/pkg/util/templateutil"
exchange2 "github.com/c9s/bbgo/pkg/exchange"
@ -110,7 +111,7 @@ type ExchangeSession struct {
// indicators is the v2 api indicators
indicators map[string]*IndicatorSet
orderStores map[string]*OrderStore
orderStores map[string]*core.OrderStore
usedSymbols map[string]struct{}
initializedSymbols map[string]struct{}
@ -140,7 +141,7 @@ func NewExchangeSession(name string, exchange types.Exchange) *ExchangeSession {
marketDataStores: make(map[string]*MarketDataStore),
standardIndicatorSets: make(map[string]*StandardIndicatorSet),
indicators: make(map[string]*IndicatorSet),
orderStores: make(map[string]*OrderStore),
orderStores: make(map[string]*core.OrderStore),
usedSymbols: make(map[string]struct{}),
initializedSymbols: make(map[string]struct{}),
logger: log.WithField("session", name),
@ -398,7 +399,7 @@ func (session *ExchangeSession) initSymbol(ctx context.Context, environ *Environ
position.BindStream(session.UserDataStream)
session.positions[symbol] = position
orderStore := NewOrderStore(symbol)
orderStore := core.NewOrderStore(symbol)
orderStore.AddOrderUpdate = true
orderStore.BindStream(session.UserDataStream)
@ -615,12 +616,12 @@ func (session *ExchangeSession) Markets() map[string]types.Market {
return session.markets
}
func (session *ExchangeSession) OrderStore(symbol string) (store *OrderStore, ok bool) {
func (session *ExchangeSession) OrderStore(symbol string) (store *core.OrderStore, ok bool) {
store, ok = session.orderStores[symbol]
return store, ok
}
func (session *ExchangeSession) OrderStores() map[string]*OrderStore {
func (session *ExchangeSession) OrderStores() map[string]*core.OrderStore {
return session.orderStores
}
@ -809,7 +810,7 @@ func (session *ExchangeSession) InitExchange(name string, ex types.Exchange) err
session.positions = make(map[string]*types.Position)
session.standardIndicatorSets = make(map[string]*StandardIndicatorSet)
session.indicators = make(map[string]*IndicatorSet)
session.orderStores = make(map[string]*OrderStore)
session.orderStores = make(map[string]*core.OrderStore)
session.OrderExecutor = &ExchangeOrderExecutor{
// copy the notification system so that we can route
Session: session,

View File

@ -7,6 +7,7 @@ import (
log "github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/core"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/sigchan"
"github.com/c9s/bbgo/pkg/types"
@ -17,10 +18,10 @@ type TradeCollector struct {
Symbol string
orderSig sigchan.Chan
tradeStore *TradeStore
tradeStore *core.TradeStore
tradeC chan types.Trade
position *types.Position
orderStore *OrderStore
orderStore *core.OrderStore
doneTrades map[types.TradeKey]struct{}
mu sync.Mutex
@ -33,13 +34,13 @@ type TradeCollector struct {
profitCallbacks []func(trade types.Trade, profit *types.Profit)
}
func NewTradeCollector(symbol string, position *types.Position, orderStore *OrderStore) *TradeCollector {
func NewTradeCollector(symbol string, position *types.Position, orderStore *core.OrderStore) *TradeCollector {
return &TradeCollector{
Symbol: symbol,
orderSig: sigchan.New(1),
tradeC: make(chan types.Trade, 100),
tradeStore: NewTradeStore(),
tradeStore: core.NewTradeStore(),
doneTrades: make(map[types.TradeKey]struct{}),
position: position,
orderStore: orderStore,
@ -47,7 +48,7 @@ func NewTradeCollector(symbol string, position *types.Position, orderStore *Orde
}
// OrderStore returns the order store used by the trade collector
func (c *TradeCollector) OrderStore() *OrderStore {
func (c *TradeCollector) OrderStore() *core.OrderStore {
return c.orderStore
}
@ -56,7 +57,7 @@ func (c *TradeCollector) Position() *types.Position {
return c.position
}
func (c *TradeCollector) TradeStore() *TradeStore {
func (c *TradeCollector) TradeStore() *core.TradeStore {
return c.tradeStore
}

View File

@ -5,6 +5,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/c9s/bbgo/pkg/core"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
@ -12,7 +13,7 @@ import (
func TestTradeCollector_ShouldNotCountDuplicatedTrade(t *testing.T) {
symbol := "BTCUSDT"
position := types.NewPosition(symbol, "BTC", "USDT")
orderStore := NewOrderStore(symbol)
orderStore := core.NewOrderStore(symbol)
collector := NewTradeCollector(symbol, position, orderStore)
assert.NotNil(t, collector)

View File

@ -10,6 +10,7 @@ import (
log "github.com/sirupsen/logrus"
"golang.org/x/time/rate"
"github.com/c9s/bbgo/pkg/core"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
@ -37,7 +38,7 @@ type TwapExecution struct {
activePosition fixedpoint.Value
activeMakerOrders *ActiveOrderBook
orderStore *OrderStore
orderStore *core.OrderStore
position *types.Position
executionCtx context.Context
@ -406,7 +407,7 @@ func (e *TwapExecution) Run(parentCtx context.Context) error {
QuoteCurrency: e.market.QuoteCurrency,
}
e.orderStore = NewOrderStore(e.Symbol)
e.orderStore = core.NewOrderStore(e.Symbol)
e.orderStore.BindStream(e.userDataStream)
e.activeMakerOrders = NewActiveOrderBook(e.Symbol)
e.activeMakerOrders.OnFilled(e.handleFilledOrder)

View File

@ -14,6 +14,7 @@ import (
"github.com/google/uuid"
"github.com/c9s/bbgo/pkg/cmd/cmdutil"
"github.com/c9s/bbgo/pkg/core"
"github.com/c9s/bbgo/pkg/data/tsv"
"github.com/c9s/bbgo/pkg/util"
@ -314,7 +315,7 @@ var BacktestCmd = &cobra.Command{
for usedSymbol := range exSource.Session.Positions() {
market, _ := exSource.Session.Market(usedSymbol)
position := types.NewPositionFromMarket(market)
orderStore := bbgo.NewOrderStore(usedSymbol)
orderStore := core.NewOrderStore(usedSymbol)
orderStore.AddOrderUpdate = true
tradeCollector := bbgo.NewTradeCollector(usedSymbol, position, orderStore)

View File

@ -1,4 +1,4 @@
package bbgo
package core
import (
"sync"

View File

@ -1,4 +1,4 @@
package bbgo
package core
import (
"sync"

View File

@ -1,4 +1,4 @@
package bbgo
package core
import (
"testing"

View File

@ -8,6 +8,7 @@ import (
"github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/core"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/indicator"
"github.com/c9s/bbgo/pkg/types"
@ -71,7 +72,7 @@ type Strategy struct {
profitOrders *bbgo.ActiveOrderBook
orders *bbgo.OrderStore
orders *core.OrderStore
// boll is the BOLLINGER indicator we used for predicting the price.
boll *indicator.BOLL
@ -330,7 +331,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
Window: 21,
}, 2.0)
s.orders = bbgo.NewOrderStore(s.Symbol)
s.orders = core.NewOrderStore(s.Symbol)
s.orders.BindStream(session.UserDataStream)
// we don't persist orders so that we can not clear the previous orders for now. just need time to support this.

View File

@ -10,6 +10,7 @@ import (
"gonum.org/v1/gonum/floats"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/core"
floats2 "github.com/c9s/bbgo/pkg/datatype/floats"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
@ -47,7 +48,7 @@ type Strategy struct {
activeMakerOrders *bbgo.ActiveOrderBook
// closePositionOrders *bbgo.LocalActiveOrderBook
orderStore *bbgo.OrderStore
orderStore *core.OrderStore
tradeCollector *bbgo.TradeCollector
session *bbgo.ExchangeSession
@ -158,7 +159,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
// s.closePositionOrders = bbgo.NewLocalActiveOrderBook(s.Symbol)
// s.closePositionOrders.BindStream(session.UserDataStream)
s.orderStore = bbgo.NewOrderStore(s.Symbol)
s.orderStore = core.NewOrderStore(s.Symbol)
s.orderStore.BindStream(session.UserDataStream)
if s.Position == nil {

View File

@ -9,6 +9,7 @@ import (
"github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/core"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/service"
"github.com/c9s/bbgo/pkg/types"
@ -89,7 +90,7 @@ type Strategy struct {
ProfitStats *types.ProfitStats `persistence:"profit_stats"`
// orderStore is used to store all the created orders, so that we can filter the trades.
orderStore *bbgo.OrderStore
orderStore *core.OrderStore
// activeOrders is the locally maintained active order book of the maker orders.
activeOrders *bbgo.ActiveOrderBook
@ -562,7 +563,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
bbgo.Notify("grid %s position", s.Symbol, s.State.Position)
s.orderStore = bbgo.NewOrderStore(s.Symbol)
s.orderStore = core.NewOrderStore(s.Symbol)
s.orderStore.BindStream(session.UserDataStream)
// we don't persist orders so that we can not clear the previous orders for now. just need time to support this.

View File

@ -18,6 +18,7 @@ import (
"go.uber.org/multierr"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/core"
"github.com/c9s/bbgo/pkg/exchange/retry"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
@ -186,7 +187,7 @@ type Strategy struct {
orderQueryService types.ExchangeOrderQueryService
orderExecutor OrderExecutor
historicalTrades *bbgo.TradeStore
historicalTrades *core.TradeStore
logger *logrus.Entry
@ -1858,7 +1859,7 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
}
}
s.historicalTrades = bbgo.NewTradeStore()
s.historicalTrades = core.NewTradeStore()
s.historicalTrades.EnablePrune = true
s.historicalTrades.BindStream(session.UserDataStream)

View File

@ -11,7 +11,7 @@ import (
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/core"
"github.com/c9s/bbgo/pkg/fixedpoint"
gridmocks "github.com/c9s/bbgo/pkg/strategy/grid2/mocks"
"github.com/c9s/bbgo/pkg/types"
@ -588,7 +588,7 @@ func newTestStrategy() *Strategy {
UpperPrice: number(20_000),
LowerPrice: number(10_000),
GridNum: 11,
historicalTrades: bbgo.NewTradeStore(),
historicalTrades: core.NewTradeStore(),
filledOrderIDMap: types.NewSyncOrderMap(),

View File

@ -6,6 +6,7 @@ import (
"sync"
"time"
"github.com/c9s/bbgo/pkg/core"
"github.com/c9s/bbgo/pkg/util"
"github.com/pkg/errors"
@ -65,7 +66,7 @@ type Strategy struct {
activeAdjustmentOrders *bbgo.ActiveOrderBook
activeWallOrders *bbgo.ActiveOrderBook
orderStore *bbgo.OrderStore
orderStore *core.OrderStore
tradeCollector *bbgo.TradeCollector
groupID uint32
@ -273,7 +274,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
s.activeAdjustmentOrders = bbgo.NewActiveOrderBook(s.Symbol)
s.activeAdjustmentOrders.BindStream(session.UserDataStream)
s.orderStore = bbgo.NewOrderStore(s.Symbol)
s.orderStore = core.NewOrderStore(s.Symbol)
s.orderStore.BindStream(session.UserDataStream)
s.tradeCollector = bbgo.NewTradeCollector(s.Symbol, s.Position, s.orderStore)

View File

@ -11,6 +11,7 @@ import (
"github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/core"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
@ -50,7 +51,7 @@ type Strategy struct {
sessions map[string]*bbgo.ExchangeSession
orderBooks map[string]*bbgo.ActiveOrderBook
orderStore *bbgo.OrderStore
orderStore *core.OrderStore
}
func (s *Strategy) ID() string {
@ -242,7 +243,7 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se
s.sessions = make(map[string]*bbgo.ExchangeSession)
s.orderBooks = make(map[string]*bbgo.ActiveOrderBook)
s.orderStore = bbgo.NewOrderStore("")
s.orderStore = core.NewOrderStore("")
for _, sessionName := range s.PreferredSessions {
session, ok := sessions[sessionName]

View File

@ -11,6 +11,7 @@ import (
"golang.org/x/time/rate"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/core"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/indicator"
"github.com/c9s/bbgo/pkg/types"
@ -103,7 +104,7 @@ type Strategy struct {
hedgeErrorLimiter *rate.Limiter
hedgeErrorRateReservation *rate.Reservation
orderStore *bbgo.OrderStore
orderStore *core.OrderStore
tradeCollector *bbgo.TradeCollector
askPriceHeartBeat, bidPriceHeartBeat types.PriceHeartBeat
@ -732,7 +733,7 @@ func (s *Strategy) CrossRun(ctx context.Context, orderExecutionRouter bbgo.Order
s.activeMakerOrders = bbgo.NewActiveOrderBook(s.Symbol)
s.activeMakerOrders.BindStream(s.makerSession.UserDataStream)
s.orderStore = bbgo.NewOrderStore(s.Symbol)
s.orderStore = core.NewOrderStore(s.Symbol)
s.orderStore.BindStream(s.sourceSession.UserDataStream)
s.orderStore.BindStream(s.makerSession.UserDataStream)