2020-10-16 02:14:36 +00:00
|
|
|
package bbgo
|
|
|
|
|
2020-10-18 04:23:00 +00:00
|
|
|
import (
|
|
|
|
"github.com/c9s/bbgo/pkg/store"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
2020-10-16 02:14:36 +00:00
|
|
|
|
|
|
|
// ExchangeSession presents the exchange connection session
|
|
|
|
// It also maintains and collects the data returned from the stream.
|
|
|
|
type ExchangeSession struct {
|
|
|
|
// Exchange session name
|
|
|
|
Name string
|
|
|
|
|
|
|
|
// The exchange account states
|
2020-10-18 03:30:37 +00:00
|
|
|
Account *types.Account
|
2020-10-16 02:14:36 +00:00
|
|
|
|
|
|
|
// Stream is the connection stream of the exchange
|
|
|
|
Stream types.Stream
|
|
|
|
|
|
|
|
Subscriptions map[types.Subscription]types.Subscription
|
|
|
|
|
|
|
|
Exchange types.Exchange
|
|
|
|
|
2020-10-18 04:30:13 +00:00
|
|
|
// markets defines market configuration of a symbol
|
|
|
|
markets map[string]types.Market
|
2020-10-16 02:14:36 +00:00
|
|
|
|
2020-10-18 04:29:38 +00:00
|
|
|
lastPrices map[string]float64
|
2020-10-16 02:14:36 +00:00
|
|
|
|
|
|
|
// Trades collects the executed trades from the exchange
|
|
|
|
// map: symbol -> []trade
|
|
|
|
Trades map[string][]types.Trade
|
|
|
|
|
2020-10-18 12:24:16 +00:00
|
|
|
marketDataStores map[string]*store.MarketDataStore
|
2020-10-17 15:51:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewExchangeSession(name string, exchange types.Exchange) *ExchangeSession {
|
|
|
|
return &ExchangeSession{
|
2020-10-18 12:24:16 +00:00
|
|
|
Name: name,
|
|
|
|
Exchange: exchange,
|
2020-10-21 09:42:37 +00:00
|
|
|
Stream: exchange.NewStream(),
|
|
|
|
Account: &types.Account{},
|
2020-10-18 12:24:16 +00:00
|
|
|
Subscriptions: make(map[types.Subscription]types.Subscription),
|
|
|
|
markets: make(map[string]types.Market),
|
|
|
|
Trades: make(map[string][]types.Trade),
|
|
|
|
lastPrices: make(map[string]float64),
|
|
|
|
marketDataStores: make(map[string]*store.MarketDataStore),
|
2020-10-17 15:51:44 +00:00
|
|
|
}
|
2020-10-16 02:14:36 +00:00
|
|
|
}
|
|
|
|
|
2020-10-18 12:41:17 +00:00
|
|
|
// MarketDataStore returns the market data store of a symbol
|
2020-10-18 12:24:16 +00:00
|
|
|
func (session *ExchangeSession) MarketDataStore(symbol string) (s *store.MarketDataStore, ok bool) {
|
|
|
|
s, ok = session.marketDataStores[symbol]
|
2020-10-18 04:27:11 +00:00
|
|
|
return s, ok
|
|
|
|
}
|
|
|
|
|
2020-10-18 04:29:38 +00:00
|
|
|
func (session *ExchangeSession) LastPrice(symbol string) (price float64, ok bool) {
|
|
|
|
price, ok = session.lastPrices[symbol]
|
|
|
|
return price, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (session *ExchangeSession) Market(symbol string) (market types.Market, ok bool) {
|
2020-10-18 04:30:13 +00:00
|
|
|
market, ok = session.markets[symbol]
|
2020-10-18 04:29:38 +00:00
|
|
|
return market, ok
|
|
|
|
}
|
|
|
|
|
2020-10-16 02:14:36 +00:00
|
|
|
// Subscribe save the subscription info, later it will be assigned to the stream
|
|
|
|
func (session *ExchangeSession) Subscribe(channel types.Channel, symbol string, options types.SubscribeOptions) *ExchangeSession {
|
|
|
|
sub := types.Subscription{
|
|
|
|
Channel: channel,
|
|
|
|
Symbol: symbol,
|
|
|
|
Options: options,
|
|
|
|
}
|
|
|
|
|
|
|
|
session.Subscriptions[sub] = sub
|
|
|
|
return session
|
|
|
|
}
|