add order store to exchange session

This commit is contained in:
c9s 2021-01-21 14:51:37 +08:00
parent 568250f4bb
commit 84b6982033
6 changed files with 24 additions and 5 deletions

View File

@ -231,6 +231,10 @@ func (environ *Environment) Init(ctx context.Context) (err error) {
session.Trades[symbol] = trades session.Trades[symbol] = trades
session.lastPrices[symbol] = 0.0 session.lastPrices[symbol] = 0.0
orderStore := NewOrderStore(symbol)
orderStore.BindStream(session.Stream)
session.orderStores[symbol] = orderStore
marketDataStore := NewMarketDataStore(symbol) marketDataStore := NewMarketDataStore(symbol)
marketDataStore.BindStream(session.Stream) marketDataStore.BindStream(session.Stream)
session.marketDataStores[symbol] = marketDataStore session.marketDataStores[symbol] = marketDataStore

View File

@ -11,11 +11,13 @@ type OrderStore struct {
mu sync.Mutex mu sync.Mutex
orders map[uint64]types.Order orders map[uint64]types.Order
Symbol string
RemoveCancelled bool RemoveCancelled bool
} }
func NewOrderStore() *OrderStore { func NewOrderStore(symbol string) *OrderStore {
return &OrderStore{ return &OrderStore{
Symbol: symbol,
orders: make(map[uint64]types.Order), orders: make(map[uint64]types.Order),
} }
} }
@ -56,7 +58,17 @@ func (s *OrderStore) Update(o types.Order) bool {
} }
func (s *OrderStore) BindStream(stream types.Stream) { func (s *OrderStore) BindStream(stream types.Stream) {
stream.OnOrderUpdate(s.handleOrderUpdate) hasSymbol := s.Symbol != ""
stream.OnOrderUpdate(func(order types.Order) {
if hasSymbol {
if order.Symbol != s.Symbol {
return
}
s.handleOrderUpdate(order)
} else {
s.handleOrderUpdate(order)
}
})
} }
func (s *OrderStore) handleOrderUpdate(order types.Order) { func (s *OrderStore) handleOrderUpdate(order types.Order) {

View File

@ -126,6 +126,8 @@ type ExchangeSession struct {
// standard indicators of each market // standard indicators of each market
standardIndicatorSets map[string]*StandardIndicatorSet standardIndicatorSets map[string]*StandardIndicatorSet
orderStores map[string]*OrderStore
loadedSymbols map[string]struct{} loadedSymbols map[string]struct{}
IsMargin bool IsMargin bool
@ -156,6 +158,7 @@ func NewExchangeSession(name string, exchange types.Exchange) *ExchangeSession {
positions: make(map[string]*Position), positions: make(map[string]*Position),
marketDataStores: make(map[string]*MarketDataStore), marketDataStores: make(map[string]*MarketDataStore),
standardIndicatorSets: make(map[string]*StandardIndicatorSet), standardIndicatorSets: make(map[string]*StandardIndicatorSet),
orderStores: make(map[string]*OrderStore),
loadedSymbols: make(map[string]struct{}), loadedSymbols: make(map[string]struct{}),
} }

View File

@ -315,7 +315,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
Window: 21, Window: 21,
}, 2.0) }, 2.0)
s.orders = bbgo.NewOrderStore() s.orders = bbgo.NewOrderStore(s.Symbol)
s.orders.BindStream(session.Stream) s.orders.BindStream(session.Stream)
// we don't persist orders so that we can not clear the previous orders for now. just need time to support this. // we don't persist orders so that we can not clear the previous orders for now. just need time to support this.

View File

@ -208,7 +208,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
return fmt.Errorf("upper price (%f) should not be less than lower price (%f)", s.UpperPrice.Float64(), s.LowerPrice.Float64()) return fmt.Errorf("upper price (%f) should not be less than lower price (%f)", s.UpperPrice.Float64(), s.LowerPrice.Float64())
} }
s.orderStore = bbgo.NewOrderStore() s.orderStore = bbgo.NewOrderStore(s.Symbol)
s.orderStore.BindStream(session.Stream) s.orderStore.BindStream(session.Stream)
// we don't persist orders so that we can not clear the previous orders for now. just need time to support this. // we don't persist orders so that we can not clear the previous orders for now. just need time to support this.

View File

@ -257,7 +257,7 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se
s.activeMakerOrders = bbgo.NewLocalActiveOrderBook() s.activeMakerOrders = bbgo.NewLocalActiveOrderBook()
s.activeMakerOrders.BindStream(s.makerSession.Stream) s.activeMakerOrders.BindStream(s.makerSession.Stream)
s.orderStore = bbgo.NewOrderStore() s.orderStore = bbgo.NewOrderStore(s.Symbol)
s.orderStore.BindStream(s.makerSession.Stream) s.orderStore.BindStream(s.makerSession.Stream)
s.stopC = make(chan struct{}) s.stopC = make(chan struct{})