Skip to content

Commit

Permalink
feat(bitflyer): add ticker
Browse files Browse the repository at this point in the history
  • Loading branch information
Akagi201 committed Sep 19, 2017
1 parent 7ba2cff commit 49387b2
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ A cryptocurrency trader for all famous exchanges.
- [x] allcoin
- [x] bitfinex
- [x] coincheck
- [x] bitflyer

## Data API
- [x] etherscan
Expand Down
66 changes: 66 additions & 0 deletions bitflyer/bitflyer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Package bitflyer bitflyer rest api
package bitflyer

import (
"io/ioutil"
"net/http"
"strings"

"github.com/Akagi201/cryptotrader/model"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
)

const (
JPAPI = "https://api.bitflyer.jp/v1/"
USAPI = "https://api.bitflyer.com/v1/"
)

// Bitflyer API data
type Bitflyer struct {
AccessKey string
SecretKey string
}

// New create new Bitflyer API data
func New(accessKey string, secretKey string) *Bitflyer {
return &Bitflyer{
AccessKey: accessKey,
SecretKey: secretKey,
}
}

// GetTicker 行情
func (bf *Bitflyer) GetTicker(base string, quote string) (*model.Ticker, error) {
url := JPAPI + "ticker?product_code=" + strings.ToUpper(quote) + "_" + strings.ToUpper(base)

log.Debugf("Request url: %v", url)

resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

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

buy := gjson.GetBytes(body, "best_bid").Float()
sell := gjson.GetBytes(body, "best_ask").Float()
last := gjson.GetBytes(body, "ltp").Float()
low := gjson.GetBytes(body, "best_bid").Float()
high := gjson.GetBytes(body, "best_ask").Float()
vol := gjson.GetBytes(body, "volume").Float()

return &model.Ticker{
Buy: buy,
Sell: sell,
Last: last,
Low: low,
High: high,
Vol: vol,
}, nil
}
15 changes: 14 additions & 1 deletion cmd/trader/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/Akagi201/cryptotrader/allcoin"
"github.com/Akagi201/cryptotrader/binance"
"github.com/Akagi201/cryptotrader/bitfinex"
"github.com/Akagi201/cryptotrader/bitflyer"
"github.com/Akagi201/cryptotrader/bittrex"
"github.com/Akagi201/cryptotrader/btc9"
"github.com/Akagi201/cryptotrader/chbtc"
Expand Down Expand Up @@ -264,7 +265,7 @@ func main() {
log.Infof("Get ticker: %+v", ticker)
}

{
if false {
// coincheck
api := coincheck.New("", "")

Expand All @@ -275,4 +276,16 @@ func main() {

log.Infof("Get ticker: %+v", ticker)
}

{
// bitflyer
api := bitflyer.New("", "")

ticker, err := api.GetTicker("jpy", "btc")
if err != nil {
log.Errorf("Get ticker failed, err: %v", err)
}

log.Infof("Get ticker: %+v", ticker)
}
}

0 comments on commit 49387b2

Please sign in to comment.