2020-10-16 02:14:36 +00:00
|
|
|
package bbgo
|
|
|
|
|
2020-10-18 04:23:00 +00:00
|
|
|
import (
|
2020-12-21 05:47:40 +00:00
|
|
|
"fmt"
|
|
|
|
|
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
|
2020-11-30 05:06:35 +00:00
|
|
|
sma map[types.IntervalWindow]*indicator.SMA
|
|
|
|
ewma map[types.IntervalWindow]*indicator.EWMA
|
|
|
|
boll map[types.IntervalWindow]*indicator.BOLL
|
2020-10-28 23:40:02 +00:00
|
|
|
|
|
|
|
store *MarketDataStore
|
2020-10-28 01:13:57 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 23:40:02 +00:00
|
|
|
func NewStandardIndicatorSet(symbol string, store *MarketDataStore) *StandardIndicatorSet {
|
2020-10-28 01:13:57 +00:00
|
|
|
set := &StandardIndicatorSet{
|
|
|
|
Symbol: symbol,
|
2020-11-30 05:06:35 +00:00
|
|
|
sma: make(map[types.IntervalWindow]*indicator.SMA),
|
|
|
|
ewma: make(map[types.IntervalWindow]*indicator.EWMA),
|
|
|
|
boll: make(map[types.IntervalWindow]*indicator.BOLL),
|
2020-10-28 23:40:02 +00:00
|
|
|
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}
|
2020-11-30 05:06:35 +00:00
|
|
|
set.sma[iw] = &indicator.SMA{IntervalWindow: iw}
|
|
|
|
set.sma[iw].Bind(store)
|
2020-10-28 23:40:02 +00:00
|
|
|
|
2020-11-30 05:06:35 +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
|
|
|
|
2020-11-30 05:06:35 +00:00
|
|
|
// setup boll indicator, we may refactor boll indicator by subscribing SMA indicator,
|
2020-10-29 09:51:20 +00:00
|
|
|
// however, since general used BOLLINGER band use window 21, which is not in the existing SMA indicator sets.
|
2020-11-30 05:06:35 +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}
|
2020-11-30 05:06:35 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-11-30 05:06:35 +00:00
|
|
|
// BOLL returns the bollinger band indicator of the given interval and the window,
|
2020-10-29 10:02:19 +00:00
|
|
|
// Please note that the K for std dev is fixed and defaults to 2.0
|
2020-11-30 05:06:35 +00:00
|
|
|
func (set *StandardIndicatorSet) BOLL(iw types.IntervalWindow, bandWidth float64) *indicator.BOLL {
|
|
|
|
inc, ok := set.boll[iw]
|
2020-10-29 10:02:19 +00:00
|
|
|
if !ok {
|
2020-11-11 15:18:53 +00:00
|
|
|
inc := &indicator.BOLL{IntervalWindow: iw, K: bandWidth}
|
2020-10-29 10:02:19 +00:00
|
|
|
inc.Bind(set.store)
|
2020-11-30 05:06:35 +00:00
|
|
|
set.boll[iw] = inc
|
2020-10-29 10:02:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return inc
|
|
|
|
}
|
|
|
|
|
2020-11-30 05:06:35 +00:00
|
|
|
// SMA returns the simple moving average indicator of the given interval and the window size.
|
|
|
|
func (set *StandardIndicatorSet) SMA(iw types.IntervalWindow) *indicator.SMA {
|
|
|
|
inc, ok := set.sma[iw]
|
2020-10-28 23:40:02 +00:00
|
|
|
if !ok {
|
2020-10-28 23:51:23 +00:00
|
|
|
inc := &indicator.SMA{IntervalWindow: iw}
|
2020-10-28 23:40:02 +00:00
|
|
|
inc.Bind(set.store)
|
2020-11-30 05:06:35 +00:00
|
|
|
set.sma[iw] = inc
|
2020-10-28 01:13:57 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 23:40:02 +00:00
|
|
|
return inc
|
2020-10-28 01:13:57 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 23:44:22 +00:00
|
|
|
// GetEWMA returns the exponential weighed moving average indicator of the given interval and the window size.
|
2020-11-30 05:06:35 +00:00
|
|
|
func (set *StandardIndicatorSet) EWMA(iw types.IntervalWindow) *indicator.EWMA {
|
|
|
|
inc, ok := set.ewma[iw]
|
2020-10-28 23:44:22 +00:00
|
|
|
if !ok {
|
2020-10-28 23:51:23 +00:00
|
|
|
inc := &indicator.EWMA{IntervalWindow: iw}
|
2020-10-28 23:44:22 +00:00
|
|
|
inc.Bind(set.store)
|
2020-11-30 05:06:35 +00:00
|
|
|
set.ewma[iw] = inc
|
2020-10-28 23:44:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return inc
|
|
|
|
}
|
|
|
|
|
2020-11-17 00:19:22 +00:00
|
|
|
// ExchangeSession presents the exchange connection Session
|
2020-10-16 02:14:36 +00:00
|
|
|
// It also maintains and collects the data returned from the stream.
|
|
|
|
type ExchangeSession struct {
|
2020-11-17 00:19:22 +00:00
|
|
|
// exchange Session based notification system
|
2020-10-30 20:36:45 +00:00
|
|
|
// we make it as a value field so that we can configure it separately
|
2021-01-24 10:42:36 +00:00
|
|
|
Notifiability `json:"-"`
|
2020-10-29 15:06:36 +00:00
|
|
|
|
2020-11-17 00:19:22 +00:00
|
|
|
// Exchange Session name
|
2021-01-24 10:42:36 +00:00
|
|
|
Name string `json:"name"`
|
2020-10-16 02:14:36 +00:00
|
|
|
|
|
|
|
// The exchange account states
|
2021-01-24 10:42:36 +00:00
|
|
|
Account *types.Account `json:"account"`
|
2020-10-16 02:14:36 +00:00
|
|
|
|
|
|
|
// Stream is the connection stream of the exchange
|
2021-01-24 10:42:36 +00:00
|
|
|
Stream types.Stream `json:"-"`
|
2020-10-16 02:14:36 +00:00
|
|
|
|
2021-01-24 10:42:36 +00:00
|
|
|
Subscriptions map[types.Subscription]types.Subscription `json:"-"`
|
2020-10-16 02:14:36 +00:00
|
|
|
|
2021-01-24 10:42:36 +00:00
|
|
|
Exchange types.Exchange `json:"-"`
|
2020-10-16 02:14:36 +00:00
|
|
|
|
2020-10-18 04:30:13 +00:00
|
|
|
// markets defines market configuration of a symbol
|
2021-01-25 06:32:46 +00:00
|
|
|
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
|
2021-01-25 06:32:46 +00:00
|
|
|
startPrices map[string]float64
|
2020-11-10 11:06:20 +00:00
|
|
|
|
2021-01-25 06:32:46 +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
|
2021-01-24 10:42:36 +00:00
|
|
|
Trades map[string]*types.TradeSlice `json:"-"`
|
2020-10-16 02:14:36 +00:00
|
|
|
|
2020-10-28 01:13:57 +00:00
|
|
|
// marketDataStores contains the market data store of each market
|
2021-01-25 06:32:46 +00:00
|
|
|
marketDataStores map[string]*MarketDataStore
|
2020-10-22 02:54:03 +00:00
|
|
|
|
2021-01-25 06:32:46 +00:00
|
|
|
positions map[string]*Position
|
2021-01-20 08:28:27 +00:00
|
|
|
|
2020-10-28 01:13:57 +00:00
|
|
|
// standard indicators of each market
|
2021-01-25 06:32:46 +00:00
|
|
|
standardIndicatorSets map[string]*StandardIndicatorSet
|
2020-10-28 01:13:57 +00:00
|
|
|
|
2021-01-25 06:32:46 +00:00
|
|
|
orderStores map[string]*OrderStore
|
2021-01-21 06:51:37 +00:00
|
|
|
|
2021-01-25 06:32:46 +00:00
|
|
|
loadedSymbols map[string]struct{}
|
2021-01-19 17:46:17 +00:00
|
|
|
|
2021-01-24 10:42:36 +00:00
|
|
|
IsMargin bool `json:"isMargin"`
|
2021-01-19 17:46:17 +00:00
|
|
|
|
2021-01-24 10:42:36 +00:00
|
|
|
IsIsolatedMargin bool `json:"isIsolatedMargin,omitempty"`
|
2021-01-19 17:46:17 +00:00
|
|
|
|
2021-01-24 10:42:36 +00:00
|
|
|
IsolatedMarginSymbol string `json:"isolatedMarginSymbol,omitempty"`
|
2020-10-17 15:51:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewExchangeSession(name string, exchange types.Exchange) *ExchangeSession {
|
|
|
|
return &ExchangeSession{
|
2020-10-29 15:06:36 +00:00
|
|
|
Notifiability: Notifiability{
|
|
|
|
SymbolChannelRouter: NewPatternChannelRouter(nil),
|
|
|
|
SessionChannelRouter: NewPatternChannelRouter(nil),
|
|
|
|
ObjectChannelRouter: NewObjectChannelRouter(),
|
|
|
|
},
|
|
|
|
|
2020-10-28 08:27:25 +00:00
|
|
|
Name: name,
|
|
|
|
Exchange: exchange,
|
|
|
|
Stream: exchange.NewStream(),
|
|
|
|
Subscriptions: make(map[types.Subscription]types.Subscription),
|
|
|
|
Account: &types.Account{},
|
2021-01-21 07:10:40 +00:00
|
|
|
Trades: make(map[string]*types.TradeSlice),
|
2020-10-28 08:27:25 +00:00
|
|
|
|
|
|
|
markets: make(map[string]types.Market),
|
2020-11-10 11:06:20 +00:00
|
|
|
startPrices: make(map[string]float64),
|
2020-10-28 08:27:25 +00:00
|
|
|
lastPrices: make(map[string]float64),
|
2021-01-20 08:28:27 +00:00
|
|
|
positions: make(map[string]*Position),
|
2020-10-28 08:27:25 +00:00
|
|
|
marketDataStores: make(map[string]*MarketDataStore),
|
|
|
|
standardIndicatorSets: make(map[string]*StandardIndicatorSet),
|
2021-01-21 06:51:37 +00:00
|
|
|
orderStores: make(map[string]*OrderStore),
|
2020-10-28 08:27:25 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-01-20 08:29:15 +00:00
|
|
|
func (session *ExchangeSession) Position(symbol string) (pos *Position, ok bool) {
|
|
|
|
pos, ok = session.positions[symbol]
|
|
|
|
return pos, ok
|
|
|
|
}
|
|
|
|
|
2020-10-18 12:41:17 +00:00
|
|
|
// MarketDataStore returns the market data store of a symbol
|
2020-10-28 08:27:25 +00:00
|
|
|
func (session *ExchangeSession) MarketDataStore(symbol string) (s *MarketDataStore, ok bool) {
|
2020-10-18 12:24:16 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-01-24 11:08:12 +00:00
|
|
|
func (session *ExchangeSession) OrderStore(symbol string) (store *OrderStore, ok bool) {
|
|
|
|
store, ok = session.orderStores[symbol]
|
|
|
|
return store, 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,
|
|
|
|
}
|
|
|
|
|
2020-10-28 08:27:25 +00:00
|
|
|
// add to the loaded symbol table
|
|
|
|
session.loadedSymbols[symbol] = struct{}{}
|
2020-10-16 02:14:36 +00:00
|
|
|
session.Subscriptions[sub] = sub
|
|
|
|
return session
|
|
|
|
}
|
2020-12-21 05:47:40 +00:00
|
|
|
|
|
|
|
func (session *ExchangeSession) FormatOrder(order types.SubmitOrder) (types.SubmitOrder, error) {
|
|
|
|
market, ok := session.Market(order.Symbol)
|
|
|
|
if !ok {
|
|
|
|
return order, fmt.Errorf("market is not defined: %s", order.Symbol)
|
|
|
|
}
|
|
|
|
|
|
|
|
order.Market = market
|
|
|
|
|
|
|
|
switch order.Type {
|
|
|
|
case types.OrderTypeStopMarket, types.OrderTypeStopLimit:
|
|
|
|
order.StopPriceString = market.FormatPrice(order.StopPrice)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
switch order.Type {
|
|
|
|
case types.OrderTypeMarket, types.OrderTypeStopMarket:
|
|
|
|
order.Price = 0.0
|
|
|
|
order.PriceString = ""
|
|
|
|
|
|
|
|
default:
|
|
|
|
order.PriceString = market.FormatPrice(order.Price)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
order.QuantityString = market.FormatQuantity(order.Quantity)
|
|
|
|
return order, nil
|
|
|
|
}
|