merge connect and read method

This commit is contained in:
c9s 2020-07-11 12:51:02 +08:00
parent db5b203e9d
commit 2a168031f3
6 changed files with 27 additions and 17 deletions

View File

@ -58,7 +58,7 @@ func (s *PrivateStream) Subscribe(channel string, symbol string, options Subscri
}) })
} }
func (s *PrivateStream) Connect(ctx context.Context) error { func (s *PrivateStream) Connect(ctx context.Context, eventC chan interface{}) error {
url := "wss://stream.binance.com:9443/ws/" + s.ListenKey url := "wss://stream.binance.com:9443/ws/" + s.ListenKey
conn, _, err := websocket.DefaultDialer.Dial(url, nil) conn, _, err := websocket.DefaultDialer.Dial(url, nil)
if err != nil { if err != nil {
@ -80,10 +80,16 @@ func (s *PrivateStream) Connect(ctx context.Context) error {
ID: 1, ID: 1,
}) })
if err != nil {
return err return err
}
go s.read(ctx, eventC)
return nil
} }
func (s *PrivateStream) Read(ctx context.Context, eventC chan interface{}) { func (s *PrivateStream) read(ctx context.Context, eventC chan interface{}) {
defer close(eventC) defer close(eventC)
ticker := time.NewTicker(30 * time.Minute) ticker := time.NewTicker(30 * time.Minute)
@ -205,6 +211,8 @@ func (e *BinanceExchange) SubmitOrder(ctx context.Context, order *Order) error {
} }
func (e *BinanceExchange) QueryKLines(ctx context.Context, symbol, interval string, limit int) ([]KLine, error) { func (e *BinanceExchange) QueryKLines(ctx context.Context, symbol, interval string, limit int) ([]KLine, error) {
log.Infof("[binance] querying kline %s %s limit %d", symbol, interval, limit)
resp, err := e.Client.NewKlinesService().Symbol(symbol).Interval(interval).Limit(limit).Do(ctx) resp, err := e.Client.NewKlinesService().Symbol(symbol).Interval(interval).Limit(limit).Do(ctx)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -194,8 +194,6 @@ func (k KLine) SlackAttachment() slack.Attachment {
} }
} }
type KLineWindow []KLine type KLineWindow []KLine
func (k KLineWindow) Len() int { func (k KLineWindow) Len() int {
@ -259,7 +257,6 @@ func (k KLineWindow) AllRise() bool {
return true return true
} }
func (k KLineWindow) GetTrend() int { func (k KLineWindow) GetTrend() int {
o := k.GetOpen() o := k.GetOpen()
c := k.GetClose() c := k.GetClose()
@ -281,7 +278,6 @@ func (k KLineWindow) Color() string {
return "#f0f0f0" return "#f0f0f0"
} }
func (k KLineWindow) Mid() float64 { func (k KLineWindow) Mid() float64 {
return k.GetHigh() - k.GetLow()/2 return k.GetHigh() - k.GetLow()/2
} }
@ -312,7 +308,7 @@ func (k KLineWindow) Tail(size int) KLineWindow {
if len(k) <= size { if len(k) <= size {
return k[:] return k[:]
} }
return k[len(k) - size:] return k[len(k)-size:]
} }
func (k *KLineWindow) Truncate(size int) { func (k *KLineWindow) Truncate(size int) {

View File

@ -124,7 +124,7 @@ func (d *KLineDetector) String() string {
} }
func (d *KLineDetector) NewOrder(e *KLineEvent, tradingCtx *TradingContext) *Order { func (d *KLineDetector) NewOrder(e *KLineEvent, tradingCtx *TradingContext) *Order {
var kline types.KLine = e.KLine var kline types.KLineOrWindow = e.KLine
if d.EnableLookBack { if d.EnableLookBack {
klineWindow := tradingCtx.KLineWindows[e.KLine.Interval] klineWindow := tradingCtx.KLineWindows[e.KLine.Interval]
if len(klineWindow) >= d.LookBackFrames { if len(klineWindow) >= d.LookBackFrames {
@ -150,7 +150,7 @@ func (d *KLineDetector) NewOrder(e *KLineEvent, tradingCtx *TradingContext) *Ord
} }
} }
func (d *KLineDetector) Detect(e *KLineEvent, tradingCtx *TradingContext) (reason string, kline types.KLine, ok bool) { func (d *KLineDetector) Detect(e *KLineEvent, tradingCtx *TradingContext) (reason string, kline types.KLineOrWindow, ok bool) {
kline = e.KLine kline = e.KLine
// if the 3m trend is drop, do not buy, let 5m window handle it. // if the 3m trend is drop, do not buy, let 5m window handle it.

View File

@ -6,6 +6,8 @@ type Market struct {
Symbol string Symbol string
PricePrecision int PricePrecision int
VolumePrecision int VolumePrecision int
QuoteCurrency string
BaseCurrency string
MinQuantity float64 MinQuantity float64
MinAmount float64 MinAmount float64
} }
@ -22,6 +24,8 @@ func (m Market) FormatVolume(val float64) string {
var MarketBTCUSDT = Market{ var MarketBTCUSDT = Market{
Symbol: "BTCUSDT", Symbol: "BTCUSDT",
BaseCurrency: "BTC",
QuoteCurrency: "USDT",
PricePrecision: 2, PricePrecision: 2,
VolumePrecision: 6, VolumePrecision: 6,
MinQuantity: 0.00000100, MinQuantity: 0.00000100,
@ -30,6 +34,8 @@ var MarketBTCUSDT = Market{
var MarketBNBUSDT = Market{ var MarketBNBUSDT = Market{
Symbol: "BNBUSDT", Symbol: "BNBUSDT",
BaseCurrency: "BNB",
QuoteCurrency: "USDT",
PricePrecision: 4, PricePrecision: 4,
VolumePrecision: 2, VolumePrecision: 2,
MinQuantity: 0.01, MinQuantity: 0.01,

View File

@ -10,6 +10,6 @@ type Trade interface {
} }
type Exchange interface { type Exchange interface {
QueryKLines(interval string, startFrom time.Time, endTo time.Time) []KLine QueryKLines(interval string, startFrom time.Time, endTo time.Time) []KLineOrWindow
QueryTrades(symbol string, startFrom time.Time) []Trade QueryTrades(symbol string, startFrom time.Time) []Trade
} }

View File

@ -2,7 +2,7 @@ package types
import "github.com/slack-go/slack" import "github.com/slack-go/slack"
type KLine interface { type KLineOrWindow interface {
GetTrend() int GetTrend() int
GetChange() float64 GetChange() float64
GetMaxChange() float64 GetMaxChange() float64