Skip to content

Commit

Permalink
feat(gateio): add GetTicker
Browse files Browse the repository at this point in the history
  • Loading branch information
Akagi201 committed Dec 20, 2017
1 parent 439175c commit 08fbb84
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
11 changes: 10 additions & 1 deletion cmd/trader/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ func main() {
log.Infof("gate.io pairs: %+v", pairs)
}

{
if false {
marketInfo, err := c.GetMarketInfo(ctx)
if err != nil {
log.Fatalf("gate.io get market_info failed, err: %v", err)
Expand All @@ -444,5 +444,14 @@ func main() {
log.Infof("gate.io market_info: %+v", marketInfo)
}

{
ethTicker, err := c.GetTicker(ctx, "eth", "btc")
if err != nil {
log.Fatalf("gate.io get ticker failed, err: %v", err)
}

log.Infof("gate.io get ticker eth-btc: %+v", ethTicker)
}

}
}
33 changes: 33 additions & 0 deletions gateio/gateio.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"net/url"
"path"
"strings"

"github.com/Akagi201/cryptotrader/model"
"github.com/pkg/errors"
Expand Down Expand Up @@ -136,3 +137,35 @@ func (c *Client) GetMarketInfo(ctx context.Context) ([]model.MarketInfo, error)

return marketInfos, nil
}

// GetTicker 返回最新, 最高, 最低 交易行情和交易量, 每 10 秒钟更新, for http://data.gate.io/api2/1/ticker/[quote]_[base]
func (c *Client) GetTicker(ctx context.Context, quote string, base string) (*model.Ticker, error) {
req, err := c.newRequest(ctx, "GET", "ticker/"+strings.ToUpper(quote)+"_"+strings.ToUpper(base), nil, nil)
if err != nil {
return nil, err
}

body, err := c.getResponse(req)
if err != nil {
return nil, err
}

log.Debugf("Response body: %v", string(body))

buy := gjson.GetBytes(body, "highestBid").Float()
sell := gjson.GetBytes(body, "lowestAsk").Float()
last := gjson.GetBytes(body, "last").Float()
low := gjson.GetBytes(body, "low24hr").Float()
high := gjson.GetBytes(body, "high24hr").Float()
vol := gjson.GetBytes(body, "baseVolume").Float()

return &model.Ticker{
Buy: buy,
Sell: sell,
Last: last,
Low: low,
High: high,
Vol: vol,
Raw: string(body),
}, nil
}
1 change: 0 additions & 1 deletion okex/okex.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ func (c *Client) getResponse(req *http.Request) ([]byte, error) {

// GetTicker 获取OKEx最新币币行情数据, for Get /api/v1/ticker.do
func (c *Client) GetTicker(ctx context.Context, quote string, base string) (*model.Ticker, error) {

v := url.Values{}
v.Set("symbol", strings.ToUpper(quote)+"_"+strings.ToUpper(base))

Expand Down

0 comments on commit 08fbb84

Please sign in to comment.