You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

54 lines
1.3 KiB

package exchange
import (
"context"
"fmt"
"sig-pub/pkg/types"
"sync"
)
// 交易所行情数据订阅
type ExchangeSubscriber interface {
// 交易所类型
ExhcangeType() types.Exchange
// 消费k线行情数据
ConsumerKline() <-chan *types.ChannelKline
// 订阅产品k线行情
SubscribeKline(instIds ...string) (err error)
// 取消订阅产品k线行情
UnsubscribeKline(instIds ...string) (err error)
}
// 交易所行情数据请求
type ExchangeFetcher interface {
// 交易所类型
ExhcangeType() types.Exchange
// 获取区间内历史k线数据
FetchHistoryKlines(ctx context.Context, instId string, interval types.Interval, after, before int64) (klines []*types.Kline, err error)
}
// 交易所交互接口
type Exchange struct {
ExType types.Exchange
Fetcher ExchangeFetcher
Subscriber ExchangeSubscriber
Insts map[string]*types.TradeInstance // <ExchangeInstId, Inst>
sync.RWMutex
}
func NewExchange(fetcher ExchangeFetcher, subscriber ExchangeSubscriber) *Exchange {
exType := fetcher.ExhcangeType()
if exType != subscriber.ExhcangeType() {
panic(fmt.Errorf("exchange type not match: %#v, %#v", exType, subscriber.ExhcangeType()))
}
return &Exchange{
ExType: exType,
Fetcher: fetcher,
Subscriber: subscriber,
Insts: make(map[string]*types.TradeInstance),
}
}