mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-01 07:26:48 +00:00
Package exchanges
This commit is contained in:
15
exchanges/huobi/huobi_types.go
Normal file
15
exchanges/huobi/huobi_types.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package huobi
|
||||
|
||||
type HuobiTicker struct {
|
||||
High float64
|
||||
Low float64
|
||||
Last float64
|
||||
Vol float64
|
||||
Buy float64
|
||||
Sell float64
|
||||
}
|
||||
|
||||
type HuobiTickerResponse struct {
|
||||
Time string
|
||||
Ticker HuobiTicker
|
||||
}
|
||||
80
exchanges/huobi/huobi_wrapper.go
Normal file
80
exchanges/huobi/huobi_wrapper.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package huobi
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-/gocryptotrader/common"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
|
||||
)
|
||||
|
||||
func (h *HUOBI) Start() {
|
||||
go h.Run()
|
||||
}
|
||||
|
||||
func (h *HUOBI) Run() {
|
||||
if h.Verbose {
|
||||
log.Printf("%s Websocket: %s (url: %s).\n", h.GetName(), common.IsEnabled(h.Websocket), HUOBI_SOCKETIO_ADDRESS)
|
||||
log.Printf("%s polling delay: %ds.\n", h.GetName(), h.RESTPollingDelay)
|
||||
log.Printf("%s %d currencies enabled: %s.\n", h.GetName(), len(h.EnabledPairs), h.EnabledPairs)
|
||||
}
|
||||
|
||||
if h.Websocket {
|
||||
go h.WebsocketClient()
|
||||
}
|
||||
|
||||
for h.Enabled {
|
||||
for _, x := range h.EnabledPairs {
|
||||
currency := common.StringToLower(x[0:3])
|
||||
go func() {
|
||||
ticker, err := h.GetTickerPrice(currency)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
/*
|
||||
HuobiLastUSD, _ := ConvertCurrency(ticker.Last, "CNY", "USD")
|
||||
HuobiHighUSD, _ := ConvertCurrency(ticker.High, "CNY", "USD")
|
||||
HuobiLowUSD, _ := ConvertCurrency(ticker.Low, "CNY", "USD")
|
||||
log.Printf("Huobi %s: Last %f (%f) High %f (%f) Low %f (%f) Volume %f\n", currency, HuobiLastUSD, ticker.Last, HuobiHighUSD, ticker.High, HuobiLowUSD, ticker.Low, ticker.Volume)
|
||||
AddExchangeInfo(h.GetName(), common.StringToUpper(currency[0:3]), common.StringToUpper(currency[3:]), ticker.Last, ticker.Volume)
|
||||
AddExchangeInfo(h.GetName(), common.StringToUpper(currency[0:3]), "USD", HuobiLastUSD, ticker.Volume)
|
||||
*/
|
||||
log.Printf("Huobi %s: Last %f High %f Low %f Volume %f\n", currency, ticker.Last, ticker.High, ticker.Low, ticker.Volume)
|
||||
}()
|
||||
}
|
||||
time.Sleep(time.Second * h.RESTPollingDelay)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *HUOBI) GetTickerPrice(currency string) (ticker.TickerPrice, error) {
|
||||
tickerNew, err := ticker.GetTicker(h.GetName(), common.StringToUpper(currency[0:3]), common.StringToUpper(currency[3:]))
|
||||
if err == nil {
|
||||
return tickerNew, nil
|
||||
}
|
||||
|
||||
var tickerPrice ticker.TickerPrice
|
||||
tick, err := h.GetTicker(currency)
|
||||
if err != nil {
|
||||
return tickerPrice, err
|
||||
}
|
||||
tickerPrice.Ask = tick.Sell
|
||||
tickerPrice.Bid = tick.Buy
|
||||
tickerPrice.FirstCurrency = common.StringToUpper(currency[0:3])
|
||||
tickerPrice.SecondCurrency = common.StringToUpper(currency[3:])
|
||||
tickerPrice.Low = tick.Low
|
||||
tickerPrice.Last = tick.Last
|
||||
tickerPrice.Volume = tick.Vol
|
||||
tickerPrice.High = tick.High
|
||||
ticker.ProcessTicker(h.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice)
|
||||
return tickerPrice, nil
|
||||
}
|
||||
|
||||
//TODO: retrieve HUOBI balance info
|
||||
//GetExchangeAccountInfo : Retrieves balances for all enabled currencies for the HUOBI exchange
|
||||
func (e *HUOBI) GetExchangeAccountInfo() (exchange.ExchangeAccountInfo, error) {
|
||||
var response exchange.ExchangeAccountInfo
|
||||
response.ExchangeName = e.GetName()
|
||||
return response, nil
|
||||
}
|
||||
205
exchanges/huobi/huobihttp.go
Normal file
205
exchanges/huobi/huobihttp.go
Normal file
@@ -0,0 +1,205 @@
|
||||
package huobi
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-/gocryptotrader/common"
|
||||
"github.com/thrasher-/gocryptotrader/config"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges"
|
||||
)
|
||||
|
||||
const (
|
||||
HUOBI_API_URL = "https://api.huobi.com/apiv2.php"
|
||||
HUOBI_API_VERSION = "2"
|
||||
)
|
||||
|
||||
type HUOBI struct {
|
||||
exchange.ExchangeBase
|
||||
}
|
||||
|
||||
func (h *HUOBI) SetDefaults() {
|
||||
h.Name = "Huobi"
|
||||
h.Enabled = false
|
||||
h.Fee = 0
|
||||
h.Verbose = false
|
||||
h.Websocket = false
|
||||
h.RESTPollingDelay = 10
|
||||
}
|
||||
|
||||
func (h *HUOBI) Setup(exch config.ExchangeConfig) {
|
||||
if !exch.Enabled {
|
||||
h.SetEnabled(false)
|
||||
} else {
|
||||
h.Enabled = true
|
||||
h.AuthenticatedAPISupport = exch.AuthenticatedAPISupport
|
||||
h.SetAPIKeys(exch.APIKey, exch.APISecret, "", false)
|
||||
h.RESTPollingDelay = exch.RESTPollingDelay
|
||||
h.Verbose = exch.Verbose
|
||||
h.Websocket = exch.Websocket
|
||||
h.BaseCurrencies = common.SplitStrings(exch.BaseCurrencies, ",")
|
||||
h.AvailablePairs = common.SplitStrings(exch.AvailablePairs, ",")
|
||||
h.EnabledPairs = common.SplitStrings(exch.EnabledPairs, ",")
|
||||
}
|
||||
}
|
||||
|
||||
func (h *HUOBI) GetFee() float64 {
|
||||
return h.Fee
|
||||
}
|
||||
|
||||
func (h *HUOBI) GetTicker(symbol string) (HuobiTicker, error) {
|
||||
resp := HuobiTickerResponse{}
|
||||
path := fmt.Sprintf("http://api.huobi.com/staticmarket/ticker_%s_json.js", symbol)
|
||||
err := common.SendHTTPGetRequest(path, true, &resp)
|
||||
|
||||
if err != nil {
|
||||
return HuobiTicker{}, err
|
||||
}
|
||||
return resp.Ticker, nil
|
||||
}
|
||||
|
||||
func (h *HUOBI) GetOrderBook(symbol string) bool {
|
||||
path := fmt.Sprintf("http://api.huobi.com/staticmarket/depth_%s_json.js", symbol)
|
||||
err := common.SendHTTPGetRequest(path, true, nil)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (h *HUOBI) GetAccountInfo() {
|
||||
err := h.SendAuthenticatedRequest("get_account_info", url.Values{})
|
||||
|
||||
if err != nil {
|
||||
log.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 {
|
||||
log.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 {
|
||||
log.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', -1, 64))
|
||||
values.Set("price", strconv.FormatFloat(price, 'f', -1, 64))
|
||||
err := h.SendAuthenticatedRequest(orderType, values)
|
||||
|
||||
if err != nil {
|
||||
log.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', -1, 64))
|
||||
values.Set("price", strconv.FormatFloat(price, 'f', -1, 64))
|
||||
err := h.SendAuthenticatedRequest(orderType, values)
|
||||
|
||||
if err != nil {
|
||||
log.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 {
|
||||
log.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', -1, 64))
|
||||
values.Set("price", strconv.FormatFloat(price, 'f', -1, 64))
|
||||
err := h.SendAuthenticatedRequest("modify_order", values)
|
||||
|
||||
if err != nil {
|
||||
log.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 {
|
||||
log.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 {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *HUOBI) SendAuthenticatedRequest(method string, v url.Values) error {
|
||||
v.Set("access_key", h.APIKey)
|
||||
v.Set("created", strconv.FormatInt(time.Now().Unix(), 10))
|
||||
v.Set("method", method)
|
||||
hash := common.GetMD5([]byte(v.Encode() + "&secret_key=" + h.APISecret))
|
||||
v.Set("sign", common.StringToLower(common.HexEncodeToString(hash)))
|
||||
encoded := v.Encode()
|
||||
|
||||
if h.Verbose {
|
||||
log.Printf("Sending POST request to %s with params %s\n", HUOBI_API_URL, encoded)
|
||||
}
|
||||
|
||||
headers := make(map[string]string)
|
||||
headers["Content-Type"] = "application/x-www-form-urlencoded"
|
||||
|
||||
resp, err := common.SendHTTPRequest("POST", HUOBI_API_URL, headers, strings.NewReader(encoded))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if h.Verbose {
|
||||
log.Printf("Recieved raw: %s\n", resp)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
221
exchanges/huobi/huobiwebsocket.go
Normal file
221
exchanges/huobi/huobiwebsocket.go
Normal file
@@ -0,0 +1,221 @@
|
||||
package huobi
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/thrasher-/gocryptotrader/common"
|
||||
"github.com/thrasher-/socketio"
|
||||
)
|
||||
|
||||
const (
|
||||
HUOBI_SOCKETIO_ADDRESS = "https://hq.huobi.com:443"
|
||||
|
||||
//Service API
|
||||
HUOBI_SOCKET_REQ_SYMBOL_LIST = "reqSymbolList"
|
||||
HUOBI_SOCKET_REQ_SYMBOL_DETAIL = "reqSymbolDetail"
|
||||
HUOBI_SOCKET_REQ_SUBSCRIBE = "reqMsgSubscribe"
|
||||
HUOBI_SOCKET_REQ_UNSUBSCRIBE = "reqMsgUnsubscribe"
|
||||
|
||||
// Market data API
|
||||
HUOBI_SOCKET_MARKET_DETAIL = "marketDetail"
|
||||
HUOBI_SOCKET_TRADE_DETAIL = "tradeDetail"
|
||||
HUOBI_SOCKET_MARKET_DEPTH_TOP = "marketDepthTop"
|
||||
HUOBI_SOCKET_MARKET_DEPTH_TOP_SHORT = "marketDepthTopShort"
|
||||
HUOBI_SOCKET_MARKET_DEPTH = "marketDepth"
|
||||
HUOBI_SOCKET_MARKET_DEPTH_TOP_DIFF = "marketDepthTopDiff"
|
||||
HUOBI_SOCKET_MARKET_DEPTH_DIFF = "marketDepthDiff"
|
||||
HUOBI_SOCKET_MARKET_LAST_KLINE = "lastKLine"
|
||||
HUOBI_SOCKET_MARKET_LAST_TIMELINE = "lastTimeLine"
|
||||
HUOBI_SOCKET_MARKET_OVERVIEW = "marketOverview"
|
||||
HUOBI_SOCKET_MARKET_STATIC = "marketStatic"
|
||||
|
||||
// History data API
|
||||
HUOBI_SOCKET_REQ_TIMELINE = "reqTimeLine"
|
||||
HUOBI_SOCKET_REQ_KLINE = "reqKLine"
|
||||
HUOBI_SOCKET_REQ_DEPTH_TOP = "reqMarketDepthTop"
|
||||
HUOBI_SOCKET_REQ_DEPTH = "reqMarketDepth"
|
||||
HUOBI_SOCKET_REQ_TRADE_DETAIL_TOP = "reqTradeDetailTop"
|
||||
HUOBI_SOCKET_REQ_MARKET_DETAIL = "reqMarketDetail"
|
||||
)
|
||||
|
||||
var HuobiSocket *socketio.SocketIO
|
||||
|
||||
type HuobiDepth struct {
|
||||
SymbolID string `json:"symbolId"`
|
||||
Time float64 `json:"time"`
|
||||
Version float64 `json:"version"`
|
||||
BidName string `json:"bidName"`
|
||||
BidPrice []float64 `json:"bidPrice"`
|
||||
BidTotal []float64 `json:"bidTotal"`
|
||||
BidAmount []float64 `json:"bidAmount"`
|
||||
AskName string `json:"askName"`
|
||||
AskPrice []float64 `json:"askPrice"`
|
||||
AskTotal []float64 `json:"askTotal"`
|
||||
AskAmount []float64 `json:"askAmount"`
|
||||
}
|
||||
|
||||
type HuobiWebsocketTrade struct {
|
||||
Price []float64 `json:"price"`
|
||||
Level []float64 `json:"level"`
|
||||
Amount []float64 `json:"amount"`
|
||||
AccuAmount []float64 `json:"accuAmount"`
|
||||
}
|
||||
|
||||
type HuobiWebsocketTradeDetail struct {
|
||||
SymbolID string `json:"symbolId"`
|
||||
TradeID []int64 `json:"tradeId"`
|
||||
Price []float64 `json:"price"`
|
||||
Time []int64 `json:"time"`
|
||||
Amount []float64 `json:"amount"`
|
||||
TopBids []HuobiWebsocketTrade `json:"topBids"`
|
||||
TopAsks []HuobiWebsocketTrade `json:"topAsks"`
|
||||
}
|
||||
|
||||
type HuobiWebsocketMarketOverview struct {
|
||||
SymbolID string `json:"symbolId"`
|
||||
Last float64 `json:"priceNew"`
|
||||
Open float64 `json:"priceOpen"`
|
||||
High float64 `json:"priceHigh"`
|
||||
Low float64 `json:"priceLow"`
|
||||
Ask float64 `json:"priceAsk"`
|
||||
Bid float64 `json:"priceBid"`
|
||||
Volume float64 `json:"totalVolume"`
|
||||
TotalAmount float64 `json:"totalAmount"`
|
||||
}
|
||||
|
||||
type HuobiWebsocketLastTimeline struct {
|
||||
ID int64 `json:"_id"`
|
||||
SymbolID string `json:"symbolId"`
|
||||
Time int64 `json:"time"`
|
||||
LastPrice float64 `json:"priceLast"`
|
||||
Amount float64 `json:"amount"`
|
||||
Volume float64 `json:"volume"`
|
||||
Count int64 `json:"count"`
|
||||
}
|
||||
|
||||
type HuobiResponse struct {
|
||||
Version int `json:"version"`
|
||||
MsgType string `json:"msgType"`
|
||||
RequestIndex int64 `json:"requestIndex"`
|
||||
RetCode int64 `json:"retCode"`
|
||||
RetMessage string `json:"retMsg"`
|
||||
Payload map[string]interface{} `json:"payload"`
|
||||
}
|
||||
|
||||
func (h *HUOBI) BuildHuobiWebsocketRequest(msgType string, requestIndex int64, symbolRequest []string) map[string]interface{} {
|
||||
request := map[string]interface{}{}
|
||||
request["version"] = 1
|
||||
request["msgType"] = msgType
|
||||
|
||||
if requestIndex != 0 {
|
||||
request["requestIndex"] = requestIndex
|
||||
}
|
||||
|
||||
if len(symbolRequest) != 0 {
|
||||
request["symbolIdList"] = symbolRequest
|
||||
}
|
||||
|
||||
return request
|
||||
}
|
||||
|
||||
func (h *HUOBI) BuildHuobiWebsocketRequestExtra(msgType string, requestIndex int64, symbolIdList interface{}) interface{} {
|
||||
request := map[string]interface{}{}
|
||||
request["version"] = 1
|
||||
request["msgType"] = msgType
|
||||
|
||||
if requestIndex != 0 {
|
||||
request["requestIndex"] = requestIndex
|
||||
}
|
||||
|
||||
request["symbolList"] = symbolIdList
|
||||
return request
|
||||
}
|
||||
|
||||
func (h *HUOBI) BuildHuobiWebsocketParamsList(objectName, currency, pushType, period, count, from, to, percentage string) interface{} {
|
||||
list := map[string]interface{}{}
|
||||
list["symbolId"] = currency
|
||||
list["pushType"] = pushType
|
||||
|
||||
if period != "" {
|
||||
list["period"] = period
|
||||
}
|
||||
if percentage != "" {
|
||||
list["percent"] = percentage
|
||||
}
|
||||
if count != "" {
|
||||
list["count"] = count
|
||||
}
|
||||
if from != "" {
|
||||
list["from"] = from
|
||||
}
|
||||
if to != "" {
|
||||
list["to"] = to
|
||||
}
|
||||
|
||||
listArray := []map[string]interface{}{}
|
||||
listArray = append(listArray, list)
|
||||
|
||||
listCompleted := make(map[string][]map[string]interface{})
|
||||
listCompleted[objectName] = listArray
|
||||
return listCompleted
|
||||
}
|
||||
|
||||
func (h *HUOBI) OnConnect(output chan socketio.Message) {
|
||||
if h.Verbose {
|
||||
log.Printf("%s Connected to Websocket.", h.GetName())
|
||||
}
|
||||
|
||||
for _, x := range h.EnabledPairs {
|
||||
currency := common.StringToLower(x)
|
||||
msg := h.BuildHuobiWebsocketRequestExtra(HUOBI_SOCKET_REQ_SUBSCRIBE, 100, h.BuildHuobiWebsocketParamsList(HUOBI_SOCKET_MARKET_OVERVIEW, currency, "pushLong", "", "", "", "", ""))
|
||||
result, err := common.JSONEncode(msg)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
output <- socketio.CreateMessageEvent("request", string(result), nil, HuobiSocket.Version)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *HUOBI) OnDisconnect(output chan socketio.Message) {
|
||||
log.Printf("%s Disconnected from websocket server.. Reconnecting.\n", h.GetName())
|
||||
h.WebsocketClient()
|
||||
}
|
||||
|
||||
func (h *HUOBI) OnError() {
|
||||
log.Printf("%s Error with Websocket connection.. Reconnecting.\n", h.GetName())
|
||||
h.WebsocketClient()
|
||||
}
|
||||
|
||||
func (h *HUOBI) OnMessage(message []byte, output chan socketio.Message) {
|
||||
}
|
||||
|
||||
func (h *HUOBI) OnRequest(message []byte, output chan socketio.Message) {
|
||||
response := HuobiResponse{}
|
||||
err := common.JSONDecode(message, &response)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *HUOBI) WebsocketClient() {
|
||||
events := make(map[string]func(message []byte, output chan socketio.Message))
|
||||
events["request"] = h.OnRequest
|
||||
events["message"] = h.OnMessage
|
||||
|
||||
HuobiSocket = &socketio.SocketIO{
|
||||
Version: 0.9,
|
||||
OnConnect: h.OnConnect,
|
||||
OnEvent: events,
|
||||
OnError: h.OnError,
|
||||
OnDisconnect: h.OnDisconnect,
|
||||
}
|
||||
|
||||
for h.Enabled && h.Websocket {
|
||||
err := socketio.ConnectToSocket(HUOBI_SOCKETIO_ADDRESS, HuobiSocket)
|
||||
if err != nil {
|
||||
log.Printf("%s Unable to connect to Websocket. Err: %s\n", h.GetName(), err)
|
||||
continue
|
||||
}
|
||||
log.Printf("%s Disconnected from Websocket.\n", h.GetName())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user