bbgo_origin/pkg/bbgo/session.go

192 lines
5.7 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/types"
)
2020-10-16 02:14:36 +00:00
2020-10-28 01:13:57 +00:00
type StandardIndicatorSet struct {
Symbol string
// Standard indicators
// interval -> window
SMA map[types.IntervalWindow]*indicator.SMA
EWMA map[types.IntervalWindow]*indicator.EWMA
2020-10-29 09:51:20 +00:00
BOLL map[types.IntervalWindow]*indicator.BOLL
store *MarketDataStore
2020-10-28 01:13:57 +00:00
}
func NewStandardIndicatorSet(symbol string, store *MarketDataStore) *StandardIndicatorSet {
2020-10-28 01:13:57 +00:00
set := &StandardIndicatorSet{
Symbol: symbol,
SMA: make(map[types.IntervalWindow]*indicator.SMA),
EWMA: make(map[types.IntervalWindow]*indicator.EWMA),
2020-10-29 13:10:34 +00:00
BOLL: make(map[types.IntervalWindow]*indicator.BOLL),
store: store,
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} {
2020-10-29 05:08:33 +00:00
iw := types.IntervalWindow{Interval: interval, Window: window}
set.SMA[iw] = &indicator.SMA{IntervalWindow: iw}
set.SMA[iw].Bind(store)
2020-10-29 05:08:33 +00:00
set.EWMA[iw] = &indicator.EWMA{IntervalWindow: iw}
set.EWMA[iw].Bind(store)
2020-10-28 01:13:57 +00:00
}
2020-10-29 09:51:20 +00:00
// setup BOLL indicator, we may refactor BOLL indicator by subscribing SMA indicator,
// however, since general used BOLLINGER band use window 21, which is not in the existing SMA indicator sets.
2020-11-11 15:18:53 +00:00
// Pull out the bandwidth configuration as the BOLL Key
2020-10-29 09:51:20 +00:00
iw := types.IntervalWindow{Interval: interval, Window: 21}
set.BOLL[iw] = &indicator.BOLL{IntervalWindow: iw, K: 2.0}
set.BOLL[iw].Bind(store)
2020-10-28 01:13:57 +00:00
}
return set
}
// GetBOLL returns the bollinger band indicator of the given interval and the window,
// Please note that the K for std dev is fixed and defaults to 2.0
2020-11-11 15:18:53 +00:00
func (set *StandardIndicatorSet) GetBOLL(iw types.IntervalWindow, bandWidth float64) *indicator.BOLL {
inc, ok := set.BOLL[iw]
if !ok {
2020-11-11 15:18:53 +00:00
inc := &indicator.BOLL{IntervalWindow: iw, K: bandWidth}
inc.Bind(set.store)
set.BOLL[iw] = inc
}
return inc
}
// GetSMA returns the simple moving average indicator of the given interval and the window size.
func (set *StandardIndicatorSet) GetSMA(iw types.IntervalWindow) *indicator.SMA {
inc, ok := set.SMA[iw]
if !ok {
inc := &indicator.SMA{IntervalWindow: iw}
inc.Bind(set.store)
set.SMA[iw] = inc
2020-10-28 01:13:57 +00:00
}
return inc
2020-10-28 01:13:57 +00:00
}
// GetEWMA returns the exponential weighed moving average indicator of the given interval and the window size.
func (set *StandardIndicatorSet) GetEWMA(iw types.IntervalWindow) *indicator.EWMA {
inc, ok := set.EWMA[iw]
if !ok {
inc := &indicator.EWMA{IntervalWindow: iw}
inc.Bind(set.store)
set.EWMA[iw] = inc
}
return inc
}
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 based notification system
// we make it as a value field so that we can configure it separately
Notifiability
2020-10-16 02:14:36 +00:00
// 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-11-10 11:06:20 +00:00
// startPrices is used for backtest
startPrices map[string]float64
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]*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
loadedSymbols map[string]struct{}
2020-10-17 15:51:44 +00:00
}
func NewExchangeSession(name string, exchange types.Exchange) *ExchangeSession {
return &ExchangeSession{
Notifiability: Notifiability{
SymbolChannelRouter: NewPatternChannelRouter(nil),
SessionChannelRouter: NewPatternChannelRouter(nil),
ObjectChannelRouter: NewObjectChannelRouter(),
},
Name: name,
Exchange: exchange,
Stream: exchange.NewStream(),
Subscriptions: make(map[types.Subscription]types.Subscription),
Account: &types.Account{},
Trades: make(map[string][]types.Trade),
markets: make(map[string]types.Market),
2020-11-10 11:06:20 +00:00
startPrices: make(map[string]float64),
lastPrices: make(map[string]float64),
marketDataStores: make(map[string]*MarketDataStore),
standardIndicatorSets: make(map[string]*StandardIndicatorSet),
loadedSymbols: make(map[string]struct{}),
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 *MarketDataStore, ok bool) {
s, ok = session.marketDataStores[symbol]
2020-10-18 04:27:11 +00:00
return s, ok
}
2020-11-10 11:06:20 +00:00
func (session *ExchangeSession) StartPrice(symbol string) (price float64, ok bool) {
price, ok = session.startPrices[symbol]
return price, 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-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,
}
// add to the loaded symbol table
session.loadedSymbols[symbol] = struct{}{}
2020-10-16 02:14:36 +00:00
session.Subscriptions[sub] = sub
return session
}