bbgo_origin/pkg/bbgo/session.go

144 lines
3.8 KiB
Go
Raw Normal View History

2020-10-16 02:14:36 +00:00
package bbgo
2020-10-18 04:23:00 +00:00
import (
2020-10-28 01:13:57 +00:00
"github.com/c9s/bbgo/pkg/indicator"
2020-10-18 04:23:00 +00:00
"github.com/c9s/bbgo/pkg/store"
"github.com/c9s/bbgo/pkg/types"
)
2020-10-16 02:14:36 +00:00
2020-10-28 01:13:57 +00:00
type IntervalWindow struct {
// The interval of kline
2020-10-28 01:43:19 +00:00
Interval types.Interval
2020-10-28 01:13:57 +00:00
// The windows size of EWMA and SMA
Window int
}
type StandardIndicatorSet struct {
Symbol string
// Standard indicators
// interval -> window
2020-10-28 01:43:19 +00:00
SMA map[IntervalWindow]*indicator.SMA
EWMA map[IntervalWindow]*indicator.EWMA
2020-10-28 01:13:57 +00:00
}
func NewStandardIndicatorSet(symbol string) *StandardIndicatorSet {
set := &StandardIndicatorSet{
Symbol: symbol,
SMA: make(map[IntervalWindow]*indicator.SMA),
2020-10-28 01:43:19 +00:00
EWMA: make(map[IntervalWindow]*indicator.EWMA),
2020-10-28 01:13:57 +00:00
}
// let us pre-defined commonly used intervals
2020-10-28 01:43:19 +00:00
for interval := range types.SupportedIntervals {
2020-10-28 01:13:57 +00:00
for _, window := range []int{7, 25, 99} {
set.SMA[IntervalWindow{interval, window}] = &indicator.SMA{
Interval: interval,
2020-10-28 01:43:19 +00:00
Window: window,
2020-10-28 01:13:57 +00:00
}
2020-10-28 01:43:19 +00:00
set.EWMA[IntervalWindow{interval, window}] = &indicator.EWMA{
2020-10-28 01:13:57 +00:00
Interval: interval,
2020-10-28 01:43:19 +00:00
Window: window,
2020-10-28 01:13:57 +00:00
}
}
}
return set
}
func (set *StandardIndicatorSet) BindMarketDataStore(store *store.MarketDataStore) {
for _, inc := range set.SMA {
2020-10-28 01:43:19 +00:00
inc.BindMarketDataStore(store)
2020-10-28 01:13:57 +00:00
}
2020-10-28 01:43:19 +00:00
for _, inc := range set.EWMA {
inc.BindMarketDataStore(store)
2020-10-28 01:13:57 +00:00
}
}
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
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-28 01:13:57 +00:00
// marketDataStores contains the market data store of each market
marketDataStores map[string]*store.MarketDataStore
2020-10-22 02:54:03 +00:00
2020-10-28 01:13:57 +00:00
// standard indicators of each market
standardIndicatorSets map[string]*StandardIndicatorSet
2020-10-22 02:54:03 +00:00
tradeReporter *TradeReporter
2020-10-17 15:51:44 +00:00
}
func NewExchangeSession(name string, exchange types.Exchange) *ExchangeSession {
return &ExchangeSession{
Name: name,
Exchange: exchange,
Stream: exchange.NewStream(),
Account: &types.Account{},
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-28 01:13:57 +00:00
func (session *ExchangeSession) StandardIndicatorSet(symbol string) (*StandardIndicatorSet, bool) {
set, ok := session.standardIndicatorSets[symbol]
return set, ok
}
// MarketDataStore returns the market data store of a symbol
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
}
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]
return market, ok
}
2020-10-22 02:54:03 +00:00
func (session *ExchangeSession) ReportTrade(notifier Notifier) *TradeReporter {
session.tradeReporter = NewTradeReporter(notifier)
return session.tradeReporter
}
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
}