Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

24hr for all markets #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added binance/.DS_Store
Binary file not shown.
58 changes: 58 additions & 0 deletions binance/examples/ticker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
//"sync"
//"strings"
"encoding/json"
"fmt"
"log"

"github.com/gorilla/websocket"
)

const (
MaxQueue = 100 // Size of message queue
)

type ChangeStats struct {
Symbol string `json:"s"`
PriceChange string `json:"p"`
PriceChangePercent string `json:"P"`
WeightedAvgPrice string `json:"w"`
PrevClosePrice string `json:"x"`
BidPrice string `json:"b"`
AskPrice string `json:"a"`
HighPrice string `json:"h"`
Volume string `json:"v"`
}

func main() {

address := fmt.Sprintf("wss://stream.binance.com:9443/ws/!ticker@arr")

var wsDialer websocket.Dialer
wsConn, _, err := wsDialer.Dial(address, nil)
if err != nil {
panic(err)
}
defer wsConn.Close()
log.Println("Dialed:", address)

for {
_, message, err := wsConn.ReadMessage()

if err != nil {
log.Println("[ERROR] ReadMessage:", err)
}

var msg []ChangeStats
fmt.Println("Received message !")

err = json.Unmarshal(message, &msg)
if err != nil {
log.Println("[ERROR] Parsing:", err)
continue
}
fmt.Println(msg)
}
}
9 changes: 9 additions & 0 deletions binance/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ func (b *Binance) Get24Hr(q SymbolQuery) (changeStats ChangeStats, err error) {
return
}

// 24 hour price for all statistics
func (b *Binance) GetAll24Hr(q SymbolQuery) (changeStats []ChangeStats, err error) {

reqUrl := fmt.Sprintf("api/v1/ticker/24hr")
_, err = b.client.do("GET", reqUrl, "", false, &changeStats)

return
}

// Latest price for all symbols.
func (b *Binance) GetAllPrices() (prices []TickerPrice, err error) {

Expand Down
1 change: 1 addition & 0 deletions binance/market_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func (o *Order) UnmarshalJSON(b []byte) error {

// Result from: GET /api/v1/ticker/24hr
type ChangeStats struct {
Symbol string `json:"symbol, string"`
PriceChange float64 `json:"priceChange,string"`
PriceChangePercent float64 `json:"priceChangePercent,string"`
WeightedAvgPrice float64 `json:"weightedAvgPrice,string"`
Expand Down