forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Binance websocket (thrasher-corp#143)
* optimize dockerfile to not invalidate layers * added binance websocket * added binance websocket types * loading exchanges from the codebase * Setting Binance websocket to Yes * revert import naming * binance websocket was missing * added gorilla websocket
- Loading branch information
Showing
6 changed files
with
184 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
FROM golang:1.9.4 as build | ||
WORKDIR /go/src/github.com/thrasher-/gocryptotrader | ||
RUN curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh | ||
WORKDIR /go/src/gocryptotrader | ||
COPY Gopkg.* ./ | ||
RUN dep ensure -vendor-only | ||
COPY . . | ||
RUN mv -vn config_example.json config.json \ | ||
&& go get -v -d \ | ||
&& GOARCH=386 GOOS=linux CGO_ENABLED=0 go install -v \ | ||
&& mv /go/bin/linux_386 /go/bin/gocryptotrader | ||
|
||
FROM alpine:latest | ||
RUN apk update && apk add --no-cache ca-certificates | ||
COPY --from=build /go/bin/gocryptotrader /app/ | ||
COPY --from=build /go/src/github.com/thrasher-/gocryptotrader/config.json /app/ | ||
COPY --from=build /go/src/gocryptotrader/config.json /app/ | ||
EXPOSE 9050 | ||
CMD ["/app/gocryptotrader"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package binance | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"strings" | ||
"time" | ||
|
||
"github.com/gorilla/websocket" | ||
"github.com/thrasher-/gocryptotrader/common" | ||
) | ||
|
||
const ( | ||
binanceDefaultWebsocketURL = "wss://stream.binance.com:9443" | ||
binancePingPeriod = 20 * time.Second | ||
) | ||
|
||
func (b *Binance) WebsocketClient() { | ||
for b.Enabled && b.Websocket { | ||
var Dialer websocket.Dialer | ||
var err error | ||
// myenabledPairs := strings.ToLower(strings.Replace(strings.Join(b.EnabledPairs, "@ticker/"), "-", "", -1)) + "@trade" | ||
|
||
myenabledPairsTicker := strings.ToLower(strings.Replace(strings.Join(b.EnabledPairs, "@ticker/"), "-", "", -1)) + "@ticker" | ||
myenabledPairsTrade := strings.ToLower(strings.Replace(strings.Join(b.EnabledPairs, "@trade/"), "-", "", -1)) + "@trade" | ||
myenabledPairsKline := strings.ToLower(strings.Replace(strings.Join(b.EnabledPairs, "@kline_1m/"), "-", "", -1)) + "@kline_1m" | ||
wsurl := b.WebsocketURL + "/stream?streams=" + myenabledPairsTicker + "/" + myenabledPairsTrade + "/" + myenabledPairsKline | ||
|
||
// b.WebsocketConn, _, err = Dialer.Dial(binanceDefaultWebsocketURL+myenabledPairs, http.Header{}) | ||
b.WebsocketConn, _, err = Dialer.Dial(wsurl, http.Header{}) | ||
|
||
if err != nil { | ||
log.Printf("%s Unable to connect to Websocket. Error: %s\n", b.Name, err) | ||
continue | ||
} | ||
|
||
if b.Verbose { | ||
log.Printf("%s Connected to Websocket.\n", b.Name) | ||
} | ||
|
||
for b.Enabled && b.Websocket { | ||
msgType, resp, err := b.WebsocketConn.ReadMessage() | ||
if err != nil { | ||
log.Println(err) | ||
break | ||
} | ||
|
||
switch msgType { | ||
case websocket.TextMessage: | ||
multiStreamData := MultiStreamData{} | ||
|
||
err := common.JSONDecode(resp, &multiStreamData) | ||
|
||
if err != nil { | ||
log.Println("Could not load multi stream data.", string(resp)) | ||
continue | ||
} | ||
|
||
if strings.Contains(multiStreamData.Stream, "trade") { | ||
trade := TradeStream{} | ||
err := common.JSONDecode(multiStreamData.Data, &trade) | ||
|
||
if err != nil { | ||
log.Println("Could not convert to a TradeStream structure") | ||
continue | ||
} | ||
log.Println("Trade received", trade.Symbol, trade.TimeStamp, trade.TradeID, trade.Price, trade.Quantity) | ||
} else if strings.Contains(multiStreamData.Stream, "ticker") { | ||
ticker := TickerStream{} | ||
|
||
err := common.JSONDecode(multiStreamData.Data, &ticker) | ||
if err != nil { | ||
log.Println("Could not convert to a TickerStream structure") | ||
continue | ||
} | ||
|
||
log.Println("Ticker received", ticker.Symbol, ticker.EventTime, ticker.TotalTradedVolume, ticker.LastTradeID) | ||
} else if strings.Contains(multiStreamData.Stream, "kline") { | ||
kline := KlineStream{} | ||
|
||
err := common.JSONDecode(multiStreamData.Data, &kline) | ||
if err != nil { | ||
log.Println("Could not convert to a KlineStream structure") | ||
continue | ||
} | ||
|
||
log.Println("Kline received", kline.Symbol, kline.EventTime, kline.Kline.HighPrice, kline.Kline.LowPrice) | ||
} | ||
type MsgType struct { | ||
MessageType string `json:"messageType"` | ||
} | ||
} | ||
} | ||
b.WebsocketConn.Close() | ||
log.Printf("%s Websocket client disconnected.", b.Name) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters