rename kline store to market data store back

This commit is contained in:
c9s 2020-10-18 20:24:16 +08:00
parent b7a450327c
commit 75115774f6
6 changed files with 41 additions and 41 deletions

View File

@ -102,7 +102,7 @@ func (environ *Environment) Init(ctx context.Context) (err error) {
session.lastPrices[symbol] = currentPrice
session.klineStores[symbol] = store.NewMarketDataStore(symbol)
session.marketDataStores[symbol] = store.NewMarketDataStore(symbol)
}
log.Infof("querying balances...")
@ -121,7 +121,7 @@ func (environ *Environment) Init(ctx context.Context) (err error) {
// update last prices
stream.OnKLineClosed(func(kline types.KLine) {
session.lastPrices[kline.Symbol] = kline.Close
session.klineStores[kline.Symbol].AddKLine(kline)
session.marketDataStores[kline.Symbol].AddKLine(kline)
})
stream.OnTrade(func(trade *types.Trade) {

View File

@ -30,23 +30,23 @@ type ExchangeSession struct {
// map: symbol -> []trade
Trades map[string][]types.Trade
klineStores map[string]*store.KLineStore
marketDataStores map[string]*store.MarketDataStore
}
func NewExchangeSession(name string, exchange types.Exchange) *ExchangeSession {
return &ExchangeSession{
Name: name,
Exchange: exchange,
Subscriptions: make(map[types.Subscription]types.Subscription),
markets: make(map[string]types.Market),
Trades: make(map[string][]types.Trade),
lastPrices: make(map[string]float64),
klineStores: make(map[string]*store.KLineStore),
Name: name,
Exchange: exchange,
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),
}
}
func (session *ExchangeSession) KLineStore(symbol string) (s *store.KLineStore, ok bool) {
s, ok = session.klineStores[symbol]
func (session *ExchangeSession) MarketDataStore(symbol string) (s *store.MarketDataStore, ok bool) {
s, ok = session.marketDataStores[symbol]
return s, ok
}

View File

@ -9,7 +9,7 @@ import (
)
type SMA struct {
store *store.KLineStore
store *store.MarketDataStore
Window int
}
@ -49,9 +49,9 @@ func calculateMovingAverage(klines types.KLineWindow, period int) (values []Indi
return values
}
func (i *SMA) SubscribeStore(store *store.KLineStore) {
func (i *SMA) SubscribeStore(store *store.MarketDataStore) {
i.store = store
// register kline update callback
store.OnUpdate(i.handleUpdate)
// store.OnUpdate(i.handleUpdate)
}

View File

@ -1,17 +0,0 @@
// Code generated by "callbackgen -type KLineStore"; DO NOT EDIT.
package store
import (
"github.com/c9s/bbgo/pkg/types"
)
func (store *KLineStore) OnUpdate(cb func(kline types.KLine)) {
store.updateCallbacks = append(store.updateCallbacks, cb)
}
func (store *KLineStore) EmitUpdate(kline types.KLine) {
for _, cb := range store.updateCallbacks {
cb(kline)
}
}

View File

@ -4,8 +4,8 @@ import (
"github.com/c9s/bbgo/pkg/types"
)
//go:generate callbackgen -type KLineStore
type KLineStore struct {
//go:generate callbackgen -type MarketDataStore
type MarketDataStore struct {
Symbol string
// KLineWindows stores all loaded klines per interval
@ -13,11 +13,11 @@ type KLineStore struct {
LastKLine types.KLine
updateCallbacks []func(kline types.KLine)
kLineUpdateCallbacks []func(kline types.KLine)
}
func NewMarketDataStore(symbol string) *KLineStore {
return &KLineStore{
func NewMarketDataStore(symbol string) *MarketDataStore {
return &MarketDataStore{
Symbol: symbol,
// KLineWindows stores all loaded klines per interval
@ -25,22 +25,22 @@ func NewMarketDataStore(symbol string) *KLineStore {
}
}
func (store *KLineStore) BindStream(stream types.Stream) {
func (store *MarketDataStore) BindStream(stream types.Stream) {
stream.OnKLineClosed(store.handleKLineClosed)
}
func (store *KLineStore) handleKLineClosed(kline types.KLine) {
func (store *MarketDataStore) handleKLineClosed(kline types.KLine) {
if kline.Symbol == store.Symbol {
store.AddKLine(kline)
}
}
func (store *KLineStore) AddKLine(kline types.KLine) {
func (store *MarketDataStore) AddKLine(kline types.KLine) {
var interval = types.Interval(kline.Interval)
var window = store.KLineWindows[interval]
window.Add(kline)
store.LastKLine = kline
store.EmitUpdate(kline)
store.EmitKLineUpdate(kline)
}

View File

@ -0,0 +1,17 @@
// Code generated by "callbackgen -type MarketDataStore"; DO NOT EDIT.
package store
import (
"github.com/c9s/bbgo/pkg/types"
)
func (store *MarketDataStore) OnKLineUpdate(cb func(kline types.KLine)) {
store.kLineUpdateCallbacks = append(store.kLineUpdateCallbacks, cb)
}
func (store *MarketDataStore) EmitKLineUpdate(kline types.KLine) {
for _, cb := range store.kLineUpdateCallbacks {
cb(kline)
}
}