From 8e0d7eed0926aea61007af960728784bcd03d312 Mon Sep 17 00:00:00 2001 From: Adrian Gallagher Date: Sat, 22 Nov 2014 20:20:11 +1100 Subject: [PATCH] Added support for Huobi exchange. --- huobihttp.go | 200 +++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 27 ++++--- 2 files changed, 219 insertions(+), 8 deletions(-) create mode 100644 huobihttp.go diff --git a/huobihttp.go b/huobihttp.go new file mode 100644 index 00000000..985aa1e5 --- /dev/null +++ b/huobihttp.go @@ -0,0 +1,200 @@ +package main + +import ( + "net/http" + "net/url" + "crypto/md5" + "errors" + "strings" + "encoding/hex" + "io/ioutil" + "strconv" + "time" + "fmt" +) + +const ( + HUOBI_API_URL = "https://api.huobi.com/apiv2.php" +) + +type HUOBI struct { + AccessKey, SecretKey string +} + +type HuobiTicker struct { + High string + Low string + Last string + Vol float64 + Buy string + Sell string +} + +type HuobiTickerResponse struct { + Time int64 + Ticker HuobiTicker +} + +func (h *HUOBI) GetTicker(symbol string) (HuobiTicker) { + resp := HuobiTickerResponse{} + path := fmt.Sprintf("http://market.huobi.com/staticmarket/ticker_%s_json.js", symbol) + err := SendHTTPRequest(path, true, &resp) + + if err != nil { + fmt.Println(err) + return HuobiTicker{} + } + return resp.Ticker +} + +func (h *HUOBI) GetOrderBook(symbol string) (bool) { + path := fmt.Sprintf("http://market.huobi.com/staticmarket/depth_%s_json.js", symbol) + err := SendHTTPRequest(path, true, nil) + if err != nil { + fmt.Println(err) + return false + } + return true +} + +func (h *HUOBI) GetAccountInfo() { + err := h.SendAuthenticatedRequest("get_account_info", url.Values{}) + + if err != nil { + fmt.Println(err) + } +} + + +func (h *HUOBI) GetOrders(coinType int) { + values := url.Values{} + values.Set("coin_type", strconv.Itoa(coinType)) + err := h.SendAuthenticatedRequest("get_orders", values) + + if err != nil { + fmt.Println(err) + } +} + +func (h *HUOBI) GetOrderInfo(orderID, coinType int) { + values := url.Values{} + values.Set("id", strconv.Itoa(orderID)) + values.Set("coin_type", strconv.Itoa(coinType)) + err := h.SendAuthenticatedRequest("order_info", values) + + if err != nil { + fmt.Println(err) + } +} + +func (h *HUOBI) Trade(orderType string, coinType int, price, amount float64) { + values := url.Values{} + if orderType != "buy" { + orderType = "sell" + } + values.Set("coin_type", strconv.Itoa(coinType)) + values.Set("amount", strconv.FormatFloat(amount, 'f', 8, 64)) + values.Set("price", strconv.FormatFloat(price, 'f', 8, 64)) + err := h.SendAuthenticatedRequest(orderType, values) + + if err != nil { + fmt.Println(err) + } +} + +func (h *HUOBI) MarketTrade(orderType string, coinType int, price, amount float64) { + values := url.Values{} + if orderType != "buy_market" { + orderType = "sell_market" + } + values.Set("coin_type", strconv.Itoa(coinType)) + values.Set("amount", strconv.FormatFloat(amount, 'f', 8, 64)) + values.Set("price", strconv.FormatFloat(price, 'f', 8, 64)) + err := h.SendAuthenticatedRequest(orderType, values) + + if err != nil { + fmt.Println(err) + } +} + +func (h *HUOBI) CancelOrder(orderID, coinType int) { + values := url.Values{} + values.Set("coin_type", strconv.Itoa(coinType)) + values.Set("id", strconv.Itoa(orderID)) + err := h.SendAuthenticatedRequest("cancel_order", values) + + if err != nil { + fmt.Println(err) + } +} + +func (h *HUOBI) ModifyOrder(orderType string, coinType, orderID int, price, amount float64) { + values := url.Values{} + values.Set("coin_type", strconv.Itoa(coinType)) + values.Set("id", strconv.Itoa(orderID)) + values.Set("amount", strconv.FormatFloat(amount, 'f', 8, 64)) + values.Set("price", strconv.FormatFloat(price, 'f', 8, 64)) + err := h.SendAuthenticatedRequest("modify_order", values) + + if err != nil { + fmt.Println(err) + } +} + +func (h *HUOBI) GetNewDealOrders(coinType int) { + values := url.Values{} + values.Set("coin_type", strconv.Itoa(coinType)) + err := h.SendAuthenticatedRequest("get_new_deal_orders", values) + + if err != nil { + fmt.Println(err) + } +} + +func (h *HUOBI) GetOrderIDByTradeID(coinType, orderID int) { + values := url.Values{} + values.Set("coin_type", strconv.Itoa(coinType)) + values.Set("trade_id", strconv.Itoa(orderID)) + err := h.SendAuthenticatedRequest("get_order_id_by_trade_id", values) + + if err != nil { + fmt.Println(err) + } +} + +func (h *HUOBI) SendAuthenticatedRequest(method string, v url.Values) (error) { + v.Set("access_key", h.AccessKey) + v.Set("created", strconv.FormatInt(time.Now().Unix(), 10)) + v.Set("method", method) + + hasher := md5.New() + hasher.Write([]byte(v.Encode() + "&secret_key=" + h.SecretKey)) + signature := strings.ToUpper(hex.EncodeToString(hasher.Sum(nil))) + v.Set("sign", signature) + + + encoded := v.Encode() + fmt.Printf("Signature: %s\n", signature) + fmt.Printf("Sending POST request to %s with params %s\n", HUOBI_API_URL, encoded) + + reqBody := strings.NewReader(encoded) + req, err := http.NewRequest("POST", HUOBI_API_URL, reqBody) + + if err != nil { + return err + } + + req.Header.Add("Content-Type", "application/x-www-form-urlencoded") + + client := &http.Client{} + resp, err := client.Do(req) + + if err != nil { + return errors.New("PostRequest: Unable to send request") + } + + contents, _ := ioutil.ReadAll(resp.Body) + fmt.Printf("Recieved raw: %s\n", string(contents)) + resp.Body.Close() + return nil +} \ No newline at end of file diff --git a/main.go b/main.go index 8c74f66a..04850673 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,7 @@ type Exchange struct { btce BTCE okcoinChina OKCoin okcoinIntl OKCoin + huobi HUOBI } func main() { @@ -29,6 +30,11 @@ func main() { log.Printf("BTCChina BTC: Last %s High %s Low %s Volume %s\n", BTCChinaBTC.Last, BTCChinaBTC.High, BTCChinaBTC.Low, BTCChinaBTC.Vol) }() + go func() { + HuobiBTC := exchange.huobi.GetTicker("btc") + log.Printf("Huobi BTC: Last %s High %s Low %s Volume %f\n", HuobiBTC.Last, HuobiBTC.High, HuobiBTC.Low, HuobiBTC.Vol) + }() + go func() { BitstampBTC := exchange.bitstamp.GetTicker() log.Printf("Bitstamp BTC: Last %s High %s Low %s Volume %s\n", BitstampBTC.Last, BitstampBTC.High, BitstampBTC.Low, BitstampBTC.Volume) @@ -55,23 +61,23 @@ func main() { }() go func() { - BTCChinaBTC := exchange.btcchina.GetTicker("ltccny") - log.Printf("BTCChina LTC: Last %s High %s Low %s Volume %s\n", BTCChinaBTC.Last, BTCChinaBTC.High, BTCChinaBTC.Low, BTCChinaBTC.Vol) + BTCChinaLTC := exchange.btcchina.GetTicker("ltccny") + log.Printf("BTCChina LTC: Last %s High %s Low %s Volume %s\n", BTCChinaLTC.Last, BTCChinaLTC.High, BTCChinaLTC.Low, BTCChinaLTC.Vol) }() go func() { - BitfinexBTC := exchange.bitfinex.GetTicker("ltcusd") - log.Printf("Bitfinex LTC: Last %s High %s Low %s Volume %s\n", BitfinexBTC.Last_price, BitfinexBTC.High, BitfinexBTC.Low, BitfinexBTC.Volume) + BitfinexLTC := exchange.bitfinex.GetTicker("ltcusd") + log.Printf("Bitfinex LTC: Last %s High %s Low %s Volume %s\n", BitfinexLTC.Last_price, BitfinexLTC.High, BitfinexLTC.Low, BitfinexLTC.Volume) }() go func() { - BTCeBTC := exchange.btce.GetTicker("ltc_usd") - log.Printf("BTC-e LTC: Last %f High %f Low %f Volume %f\n", BTCeBTC.Last, BTCeBTC.High, BTCeBTC.Low, BTCeBTC.Vol_cur) + BTCeLTC := exchange.btce.GetTicker("ltc_usd") + log.Printf("BTC-e LTC: Last %f High %f Low %f Volume %f\n", BTCeLTC.Last, BTCeLTC.High, BTCeLTC.Low, BTCeLTC.Vol_cur) }() go func() { - OKCoinChinaBTC := exchange.okcoinChina.GetTicker("ltc_cny") - log.Printf("OKCoin China LTC: Last %s High %s Low %s Volume %s\n", OKCoinChinaBTC.Last, OKCoinChinaBTC.High, OKCoinChinaBTC.Low, OKCoinChinaBTC.Vol) + OKCoinChinaLTC := exchange.okcoinChina.GetTicker("ltc_cny") + log.Printf("OKCoin China LTC: Last %s High %s Low %s Volume %s\n", OKCoinChinaLTC.Last, OKCoinChinaLTC.High, OKCoinChinaLTC.Low, OKCoinChinaLTC.Vol) }() go func() { @@ -79,6 +85,11 @@ func main() { log.Printf("OKCoin Intl LTC: Last %s High %s Low %s Volume %s\n", OKCoinChinaIntlLTC.Last, OKCoinChinaIntlLTC.High, OKCoinChinaIntlLTC.Low, OKCoinChinaIntlLTC.Vol) }() + go func() { + HuobiLTC := exchange.huobi.GetTicker("ltc") + log.Printf("Huobi LTC: Last %s High %s Low %s Volume %f\n", HuobiLTC.Last, HuobiLTC.High, HuobiLTC.Low, HuobiLTC.Vol) + }() + time.Sleep(time.Second * 15) cmd := exec.Command("cmd", "/c", "cls") cmd.Stdout = os.Stdout