mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 08:45:16 +00:00
merge connect and read method
This commit is contained in:
parent
db5b203e9d
commit
2a168031f3
|
@ -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,
|
||||
})
|
||||
|
||||
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
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package types
|
|||
|
||||
import "github.com/slack-go/slack"
|
||||
|
||||
type KLine interface {
|
||||
type KLineOrWindow interface {
|
||||
GetTrend() int
|
||||
GetChange() float64
|
||||
GetMaxChange() float64
|
||||
|
|
Loading…
Reference in New Issue
Block a user