Skip to content

Commit

Permalink
feat(allcoin): add ticker
Browse files Browse the repository at this point in the history
  • Loading branch information
Akagi201 committed Sep 18, 2017
1 parent 3c6a13a commit e760192
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ A cryptocurrency trader for all famous exchanges.
- [x] poloniex
- [x] lhang
- [x] jubi
- [x] allcoin

## Data API
- [x] etherscan
Expand All @@ -38,5 +39,5 @@ A cryptocurrency trader for all famous exchanges.
* NEO: AU1jyMccQCzEnns2BaFcnDPB3W8JqEDYyQ

## Join us
* 量投社: [Slack](https://join.slack.com/t/cryptotraderhub/shared_invite/MjM5MTU2MzY3MzQ2LTE1MDUxMzQ1MjYtYmI0NGNlZTQ4YQ) [Telegram](https://t.me/cryptotraderhub)
* 量投社: [Slack](https://join.slack.com/t/cryptotraderhub/shared_invite/MjM5MTU2MzY3MzQ2LTE1MDUxMzQ1MjYtYmI0NGNlZTQ4YQ) [Telegram Group](https://t.me/cryptotraderhub) [Telegram Channel](https://t.me/cryptotraderchannel)
* 量投社: 讨论海外项目投资, 量化交易, 搬砖套利, 挖矿, 区块链技术.
95 changes: 95 additions & 0 deletions allcoin/allcoin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Package allcoin allcoin rest api package
package allcoin

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

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

const (
API = "https://api.allcoin.com/api/v1/"
)

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

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

// GetTicker 行情
func (ac *Allcoin) GetTicker(base string, quote string) (*model.Ticker, error) {
url := API + "ticker?symbol=" + strings.ToLower(quote) + "_" + strings.ToLower(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))

buyRes := gjson.GetBytes(body, "ticker.buy").String()
buy, err := strconv.ParseFloat(buyRes, 64)
if err != nil {
return nil, err
}

sellRes := gjson.GetBytes(body, "ticker.sell").String()
sell, err := strconv.ParseFloat(sellRes, 64)
if err != nil {
return nil, err
}

lastRes := gjson.GetBytes(body, "ticker.last").String()
last, err := strconv.ParseFloat(lastRes, 64)
if err != nil {
return nil, err
}

lowRes := gjson.GetBytes(body, "ticker.low").String()
low, err := strconv.ParseFloat(lowRes, 64)
if err != nil {
return nil, err
}

highRes := gjson.GetBytes(body, "ticker.high").String()
high, err := strconv.ParseFloat(highRes, 64)
if err != nil {
return nil, err
}

volRes := gjson.GetBytes(body, "ticker.vol").String()
vol, err := strconv.ParseFloat(volRes, 64)
if err != nil {
return nil, err
}

return &model.Ticker{
Buy: buy,
Sell: sell,
Last: last,
Low: low,
High: high,
Vol: vol,
}, nil
}
17 changes: 15 additions & 2 deletions cmd/trader/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"github.com/Akagi201/cryptotrader/allcoin"
"github.com/Akagi201/cryptotrader/binance"
"github.com/Akagi201/cryptotrader/bittrex"
"github.com/Akagi201/cryptotrader/btc9"
Expand All @@ -22,7 +23,7 @@ import (
func main() {
if false {
// CHBTC
api := chbtc.New("c390fceb-cee2-44bd-980a-0662aed39142", "dfffc1e0-bab1-46ca-a947-49d814154836")
api := chbtc.New("", "")

ticker, err := api.GetTicker("cny", "eth")
if err != nil {
Expand Down Expand Up @@ -225,7 +226,7 @@ func main() {
log.Infof("Get ticker: %+v", ticker)
}

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

Expand All @@ -236,4 +237,16 @@ func main() {

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

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

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

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

0 comments on commit e760192

Please sign in to comment.