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
conn, _, err := websocket.DefaultDialer.Dial(url, nil)
if err != nil {
@ -80,10 +80,16 @@ func (s *PrivateStream) Connect(ctx context.Context) error {
ID: 1,
})
return err
if err != nil {
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)
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) {
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)
if err != nil {
return nil, err

View File

@ -27,9 +27,9 @@ type KLine struct {
Volume string `json:"V"` // taker buy base asset volume (like 10 BTC)
QuoteVolume string `json:"Q"` // taker buy quote asset volume (like 1000USDT)
LastTradeID int `json:"L"`
NumberOfTrades int64 `json:"n"`
Closed bool `json:"x"`
LastTradeID int `json:"L"`
NumberOfTrades int64 `json:"n"`
Closed bool `json:"x"`
}
func (k KLine) Mid() float64 {
@ -135,7 +135,7 @@ func (k KLine) Color() string {
func (k KLine) SlackAttachment() slack.Attachment {
return slack.Attachment{
Text: "KLine",
Text: "KLine",
Color: k.Color(),
Fields: []slack.AttachmentField{
{
@ -194,8 +194,6 @@ func (k KLine) SlackAttachment() slack.Attachment {
}
}
type KLineWindow []KLine
func (k KLineWindow) Len() int {
@ -259,7 +257,6 @@ func (k KLineWindow) AllRise() bool {
return true
}
func (k KLineWindow) GetTrend() int {
o := k.GetOpen()
c := k.GetClose()
@ -281,7 +278,6 @@ func (k KLineWindow) Color() string {
return "#f0f0f0"
}
func (k KLineWindow) Mid() float64 {
return k.GetHigh() - k.GetLow()/2
}
@ -312,7 +308,7 @@ func (k KLineWindow) Tail(size int) KLineWindow {
if len(k) <= size {
return k[:]
}
return k[len(k) - size:]
return k[len(k)-size:]
}
func (k *KLineWindow) Truncate(size int) {
@ -362,7 +358,7 @@ func (k KLineWindow) GetLowerShadowHeight() float64 {
func (k KLineWindow) SlackAttachment() slack.Attachment {
return slack.Attachment{
Text: "KLine",
Text: "KLine",
Color: k.Color(),
Fields: []slack.AttachmentField{
{

View File

@ -124,7 +124,7 @@ func (d *KLineDetector) String() string {
}
func (d *KLineDetector) NewOrder(e *KLineEvent, tradingCtx *TradingContext) *Order {
var kline types.KLine = e.KLine
var kline types.KLineOrWindow = e.KLine
if d.EnableLookBack {
klineWindow := tradingCtx.KLineWindows[e.KLine.Interval]
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
// 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
PricePrecision int
VolumePrecision int
QuoteCurrency string
BaseCurrency string
MinQuantity float64
MinAmount float64
}
@ -22,6 +24,8 @@ func (m Market) FormatVolume(val float64) string {
var MarketBTCUSDT = Market{
Symbol: "BTCUSDT",
BaseCurrency: "BTC",
QuoteCurrency: "USDT",
PricePrecision: 2,
VolumePrecision: 6,
MinQuantity: 0.00000100,
@ -30,6 +34,8 @@ var MarketBTCUSDT = Market{
var MarketBNBUSDT = Market{
Symbol: "BNBUSDT",
BaseCurrency: "BNB",
QuoteCurrency: "USDT",
PricePrecision: 4,
VolumePrecision: 2,
MinQuantity: 0.01,

View File

@ -10,6 +10,6 @@ type Trade 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
}

View File

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