mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 15:09:42 +00:00
Added basic support for BTCMarkets exchange.
This commit is contained in:
125
btcmarkets.go
Normal file
125
btcmarkets.go
Normal file
@@ -0,0 +1,125 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"crypto/hmac"
|
||||
"crypto/sha512"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
"log"
|
||||
"io/ioutil"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
const (
|
||||
BTCMARKETS_API_URL = "https://api.btcmarkets.net"
|
||||
)
|
||||
|
||||
type BTCMarkets struct {
|
||||
Name string
|
||||
Enabled bool
|
||||
APIKey, APISecret string
|
||||
}
|
||||
|
||||
type BTCMarketsTicker struct {
|
||||
BestBID float64
|
||||
BestAsk float64
|
||||
LastPrice float64
|
||||
Currency string
|
||||
Instrument string
|
||||
Timestamp int64
|
||||
}
|
||||
|
||||
func (b *BTCMarkets) SetDefaults() {
|
||||
b.Name = "BTC Markets"
|
||||
b.Enabled = true
|
||||
}
|
||||
|
||||
func (b *BTCMarkets) GetName() (string) {
|
||||
return b.Name
|
||||
}
|
||||
|
||||
func (b *BTCMarkets) SetEnabled(enabled bool) {
|
||||
b.Enabled = enabled
|
||||
}
|
||||
|
||||
func (b *BTCMarkets) IsEnabled() (bool) {
|
||||
return b.Enabled
|
||||
}
|
||||
|
||||
func (b *BTCMarkets) GetTicker(symbol string) (BTCMarketsTicker) {
|
||||
ticker := BTCMarketsTicker{}
|
||||
path := fmt.Sprintf("/market/%s/AUD/tick", symbol)
|
||||
err := SendHTTPRequest(BTCMARKETS_API_URL + path, true, &ticker)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return BTCMarketsTicker{}
|
||||
}
|
||||
return ticker
|
||||
}
|
||||
|
||||
func (b *BTCMarkets) GetOrderbook(symbol string) {
|
||||
path := fmt.Sprintf("/market/%s/AUD/orderbook", symbol)
|
||||
err := SendHTTPRequest(BTCMARKETS_API_URL + path, true, nil)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (b *BTCMarkets) GetTrades(symbol, since string) {
|
||||
path := ""
|
||||
if len(since) > 0 {
|
||||
path = fmt.Sprintf("/market/%s/AUD/trades?since=%s", symbol, since)
|
||||
} else {
|
||||
path = fmt.Sprintf("/market/%s/AUD/trades", symbol)
|
||||
}
|
||||
err := SendHTTPRequest(BTCMARKETS_API_URL + path, true, nil)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (b *BTCMarkets) SendAuthenticatedRequest(reqType, path, data string) (error) {
|
||||
nonce := strconv.FormatInt(time.Now().Unix(), 10)
|
||||
request := ""
|
||||
|
||||
if len(data) > 0 {
|
||||
request = path + "\n" + nonce + "\n" + data
|
||||
} else {
|
||||
request = path + "\n" + nonce + "\n"
|
||||
}
|
||||
|
||||
hmac := hmac.New(sha512.New, []byte(b.APISecret))
|
||||
hmac.Write([]byte(request))
|
||||
log.Printf("Sending %s request to %s path %s with params %s\n", reqType, BTCMARKETS_API_URL + path, path, request)
|
||||
req, err := http.NewRequest(reqType, BTCMARKETS_API_URL + path, strings.NewReader(""))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
b64 := base64.StdEncoding.EncodeToString(hmac.Sum(nil))
|
||||
|
||||
req.Header.Add("Accept", "application/json")
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
req.Header.Add("User-Agent", "btc markets python client")
|
||||
req.Header.Add("Accept-Charset", "UTF-8")
|
||||
req.Header.Add("apikey", b.APIKey)
|
||||
req.Header.Add("timestamp", nonce)
|
||||
req.Header.Add("signature", b64)
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
|
||||
if err != nil {
|
||||
return errors.New("PostRequest: Unable to send request")
|
||||
}
|
||||
|
||||
contents, _ := ioutil.ReadAll(resp.Body)
|
||||
log.Printf("Recieved raw: %s\n", string(contents))
|
||||
resp.Body.Close()
|
||||
return nil
|
||||
}
|
||||
@@ -32,6 +32,14 @@
|
||||
"BaseCurrencies": "USD,RUB,EUR,CNY,GBP",
|
||||
"Enabled": true
|
||||
},
|
||||
{
|
||||
"Name": "BTC Markets",
|
||||
"Pairs": "LTCAUD,BTCAUD,LTCBTC",
|
||||
"APIKey": "Key",
|
||||
"APISecret": "Secret",
|
||||
"BaseCurrencies": "AUD",
|
||||
"Enabled": true
|
||||
},
|
||||
{
|
||||
"Name": "Huobi",
|
||||
"Pairs": "BTCCNY,LTCCNY,LTCBTC",
|
||||
|
||||
29
main.go
29
main.go
@@ -12,6 +12,7 @@ type Exchange struct {
|
||||
bitstamp Bitstamp
|
||||
bitfinex Bitfinex
|
||||
btce BTCE
|
||||
btcmarkets BTCMarkets
|
||||
okcoinChina OKCoin
|
||||
okcoinIntl OKCoin
|
||||
itbit ItBit
|
||||
@@ -51,6 +52,7 @@ func main() {
|
||||
exchange.bitstamp.SetDefaults()
|
||||
exchange.bitfinex.SetDefaults()
|
||||
exchange.btce.SetDefaults()
|
||||
exchange.btcmarkets.SetDefaults()
|
||||
exchange.okcoinChina.SetURL(OKCOIN_API_URL_CHINA)
|
||||
exchange.okcoinChina.SetDefaults()
|
||||
exchange.okcoinIntl.SetURL(OKCOIN_API_URL)
|
||||
@@ -88,6 +90,13 @@ func main() {
|
||||
} else {
|
||||
log.Printf("%s enabled.\n", exch.Name)
|
||||
}
|
||||
} else if exchange.btcmarkets.GetName() == exch.Name {
|
||||
if !exch.Enabled {
|
||||
exchange.btcmarkets.SetEnabled(false)
|
||||
log.Printf("%s disabled.\n", exch.Name)
|
||||
} else {
|
||||
log.Printf("%s enabled.\n", exch.Name)
|
||||
}
|
||||
} else if exchange.okcoinChina.GetName() == exch.Name {
|
||||
if !exch.Enabled {
|
||||
exchange.okcoinChina.SetEnabled(false)
|
||||
@@ -125,7 +134,7 @@ func main() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
err = RetrieveConfigCurrencyPairs(config)
|
||||
|
||||
if err != nil {
|
||||
@@ -218,6 +227,24 @@ func main() {
|
||||
}()
|
||||
}
|
||||
|
||||
if exchange.btcmarkets.IsEnabled() {
|
||||
go func() {
|
||||
BTCMarketsBTC := exchange.btcmarkets.GetTicker("BTC")
|
||||
BTCMarketsBTCLastUSD, _ := ConvertCurrency(BTCMarketsBTC.LastPrice, "AUD", "USD")
|
||||
BTCMarketsBTCBestBidUSD, _ := ConvertCurrency(BTCMarketsBTC.BestBID, "AUD", "USD")
|
||||
BTCMarketsBTCBestAskUSD, _ := ConvertCurrency(BTCMarketsBTC.BestAsk, "AUD", "USD")
|
||||
log.Printf("BTC Markets BTC: Last %f (%f) Bid %f (%f) Ask %f (%f)\n", BTCMarketsBTCLastUSD, BTCMarketsBTC.LastPrice, BTCMarketsBTCBestBidUSD, BTCMarketsBTC.BestBID, BTCMarketsBTCBestAskUSD, BTCMarketsBTC.BestAsk)
|
||||
}()
|
||||
|
||||
go func() {
|
||||
BTCMarketsLTC := exchange.btcmarkets.GetTicker("LTC")
|
||||
BTCMarketsLTCLastUSD, _ := ConvertCurrency(BTCMarketsLTC.LastPrice, "AUD", "USD")
|
||||
BTCMarketsLTCBestBidUSD, _ := ConvertCurrency(BTCMarketsLTC.BestBID, "AUD", "USD")
|
||||
BTCMarketsLTCBestAskUSD, _ := ConvertCurrency(BTCMarketsLTC.BestAsk, "AUD", "USD")
|
||||
log.Printf("BTC Markets LTC: Last %f (%f) Bid %f (%f) Ask %f (%f)", BTCMarketsLTCLastUSD, BTCMarketsLTC.LastPrice, BTCMarketsLTCBestBidUSD, BTCMarketsLTC.BestBID, BTCMarketsLTCBestAskUSD, BTCMarketsLTC.BestAsk)
|
||||
}()
|
||||
}
|
||||
|
||||
if exchange.okcoinChina.IsEnabled() {
|
||||
go func() {
|
||||
OKCoinChinaBTC := exchange.okcoinChina.GetTicker("btc_cny")
|
||||
|
||||
Reference in New Issue
Block a user