Added structs for Ticker information for various exchanges.

This commit is contained in:
Adrian Gallagher
2014-11-10 20:03:37 +11:00
parent 0e37811cd0
commit 9327a31ba5
8 changed files with 135 additions and 30 deletions

View File

@@ -4,6 +4,7 @@ import (
"net/http"
"io/ioutil"
"fmt"
"log"
"encoding/json"
"encoding/hex"
"crypto/hmac"
@@ -84,14 +85,14 @@ func (b *Bitfinex) SendAuthenticatedHTTPRequest(path string, params map[string]i
}
PayloadJson, err := json.Marshal(request)
fmt.Printf("Request JSON: %s\n", PayloadJson)
log.Printf("Request JSON: %s\n", PayloadJson)
if err != nil {
return errors.New("SendAuthenticatedHTTPRequest: Unable to JSON request")
}
PayloadBase64 := base64.StdEncoding.EncodeToString(PayloadJson)
fmt.Printf("Base64: %s\n", PayloadBase64)
log.Printf("Base64: %s\n", PayloadBase64)
hmac := hmac.New(sha512.New384, []byte(b.APISecret))
hmac.Write([]byte(PayloadBase64))
@@ -115,18 +116,18 @@ func (b *Bitfinex) SendAuthenticatedHTTPRequest(path string, params map[string]i
}
contents, _ := ioutil.ReadAll(resp.Body)
fmt.Printf("Recieved raw: %s\n", string(contents))
log.Printf("Recieved raw: %s\n", string(contents))
resp.Body.Close()
return nil
}
func (b *Bitfinex) GetTicker(symbol string) (bool) {
func (b *Bitfinex) GetTicker(symbol string) (interface{}) {
err := SendHTTPRequest(BITFINEX_API_URL + BITFINEX_TICKER + symbol, true, &b.Ticker)
if err != nil {
fmt.Println(err)
return false
return nil
}
return true
return b.Ticker
}
func (b *Bitfinex) GetStats(symbol string) (bool) {
@@ -320,12 +321,3 @@ func (b *Bitfinex) GetTradeHistory(symbol string, timestamp time.Time, limit int
}
}
func JsonDecode(data string, result interface{}) (err error) {
r := json.NewDecoder(strings.NewReader(data))
err = r.Decode(result)
if err != nil {
return err
}
return
}

View File

@@ -5,6 +5,7 @@ import (
"net/url"
"io/ioutil"
"fmt"
"log"
"encoding/hex"
"crypto/hmac"
"crypto/sha256"
@@ -62,13 +63,15 @@ type ConversionRate struct {
Sell string
}
func (b *Bitstamp) GetTicker() {
func (b *Bitstamp) GetTicker() (interface{}) {
err := SendHTTPRequest(BITSTAMP_API_URL + BITSTAMP_API_TICKER, true, &b.Ticker)
if err != nil {
fmt.Println(err)
return
return nil
}
return b.Ticker
}
func (b *Bitstamp) GetOrderbook() {
@@ -149,7 +152,7 @@ func (b *Bitstamp) PlaceOrder(price float64, amount float64, Type int) {
orderType = BITSTAMP_API_SELL
}
fmt.Printf("Placing %s order at price %f for %f amount.\n", orderType, price, amount)
log.Printf("Placing %s order at price %f for %f amount.\n", orderType, price, amount)
err := b.SendAuthenticatedHTTPRequest(orderType, req)

View File

@@ -24,14 +24,32 @@ type BTCChina struct {
APISecret, APIKey string
}
func (b *BTCChina) GetTicker(symbol string) (bool) {
func (b *BTCChina) GetTicker(symbol string) (interface{}) {
type Ticker struct {
High string
Low string
Buy string
Sell string
Last string
Vol string
Date int64
Vwap string
Prev_close string
Open string
}
type Response struct {
Ticker Ticker
}
resp := Response{}
req := fmt.Sprintf("%sdata/ticker?market=%s", BTCCHINA_API_URL, symbol)
err := SendHTTPRequest(req, true, nil)
err := SendHTTPRequest(req, true, &resp)
if err != nil {
fmt.Println(err)
return false
return nil
}
return true
return resp
}
func (b *BTCChina) GetTradesLast24h(symbol string) (bool) {
@@ -131,7 +149,7 @@ func (b *BTCChina) SendAuthenticatedHTTPRequest(method string, params []string)
return errors.New("Unable to JSON POST data")
}
fmt.Printf("Sending POST request to %s calling method %s with params %s\n", "https://api.btcchina.com/api_trade_v1.php", method, data)
log.Printf("Sending POST request to %s calling method %s with params %s\n", "https://api.btcchina.com/api_trade_v1.php", method, data)
reqBody := strings.NewReader(string(data))
b64 := base64.StdEncoding.EncodeToString([]byte(b.APIKey + ":" + hash))
@@ -154,7 +172,7 @@ func (b *BTCChina) SendAuthenticatedHTTPRequest(method string, params []string)
}
contents, _ := ioutil.ReadAll(resp.Body)
fmt.Printf("Recv'd :%s", string(contents))
log.Printf("Recv'd :%s", string(contents))
resp.Body.Close()
return nil

View File

@@ -28,6 +28,34 @@ type BTCE struct {
APIKey, APISecret string
}
func (b *BTCE) GetTicker(symbol string) (interface{}) {
type Ticker struct {
High float64
Low float64
Avg float64
Vol float64
Vol_cur float64
Last float64
Buy float64
Sell float64
Updated int64
Server_time int64
}
type Response struct {
Ticker Ticker
}
response := Response{}
req := fmt.Sprintf("https://btc-e.com/api/2/%s/ticker", symbol)
err := SendHTTPRequest(req, true, &response)
if err != nil {
fmt.Println(err)
return nil
}
return response
}
func (b *BTCE) GetInfo() {
err := b.SendAuthenticatedHTTPRequest(BTCE_GET_INFO, url.Values{})

View File

@@ -3,6 +3,8 @@ package main
import (
"net/http"
"fmt"
"strings"
"encoding/json"
"io/ioutil"
"errors"
)
@@ -35,3 +37,13 @@ func SendHTTPRequest(url string, jsonDecode bool, result interface{}) (err error
}
return
}
func JsonDecode(data string, result interface{}) (err error) {
r := json.NewDecoder(strings.NewReader(data))
err = r.Decode(result)
if err != nil {
return err
}
return
}

View File

@@ -12,6 +12,7 @@ import (
"time"
"encoding/json"
"io/ioutil"
"log"
"fmt"
)
@@ -204,7 +205,7 @@ func (i *ItBit) SendAuthenticatedHTTPRequest(method string, path string, params
}
PayloadJson, err := json.Marshal(request)
fmt.Printf("Request JSON: %s\n", PayloadJson)
log.Printf("Request JSON: %s\n", PayloadJson)
if err != nil {
return errors.New("SendAuthenticatedHTTPRequest: Unable to JSON request")
@@ -229,7 +230,7 @@ func (i *ItBit) SendAuthenticatedHTTPRequest(method string, path string, params
}
contents, _ := ioutil.ReadAll(resp.Body)
fmt.Printf("Recieved raw: %s\n", string(contents))
log.Printf("Recieved raw: %s\n", string(contents))
resp.Body.Close()
return nil
}

36
main.go Normal file
View File

@@ -0,0 +1,36 @@
package main
import (
"log"
"time"
)
type Exchange struct {
btcchina BTCChina
bitstamp Bitstamp
bitfinex Bitfinex
btce BTCE
lakeBTC LakeBTC
okcoin OKCoin
itbit ItBit
}
func main() {
log.Println("Bot started")
exchange := Exchange{}
for {
log.Println("BTC Pricing:")
log.Printf("BTC China: %v\n", exchange.btcchina.GetTicker("btccny"))
log.Printf("Bitfinex: %v\n", exchange.bitfinex.GetTicker("btcusd"))
log.Printf("BTC-e BTC: %v\n", exchange.btce.GetTicker("btc_usd"))
log.Printf("OKCoin: %v\n", exchange.okcoin.GetTicker("btc_usd"))
log.Println("LTC Pricing:")
log.Printf("BTC China: %v\n", exchange.btcchina.GetTicker("ltccny"))
log.Printf("Bitfinex: %v\n", exchange.bitfinex.GetTicker("ltcusd"))
log.Printf("BTC-e: %v\n", exchange.btce.GetTicker("ltc_usd"))
log.Printf("OKCoin: %v\n", exchange.okcoin.GetTicker("ltc_usd"))
time.Sleep(time.Second * 15)
}
}

View File

@@ -21,15 +21,30 @@ type OKCoin struct {
PartnerID, SecretKey string
}
func (o *OKCoin) GetTicker(symbol string) (bool) {
path := "ticker.do?symbol=" + symbol
err := SendHTTPRequest(OKCOIN_API_URL + path, true, nil)
type OKCoinTicker struct {
Buy string
High string
Last string
Low string
Sell string
Vol string
}
type OKCoinTickerResponse struct {
Date string
Ticker OKCoinTicker
}
func (o *OKCoin) GetTicker(symbol string) (interface{}) {
resp := OKCoinTickerResponse{}
path := fmt.Sprintf("ticker.do?symbol=%s&ok=1", symbol)
err := SendHTTPRequest(OKCOIN_API_URL + path, true, &resp)
if err != nil {
fmt.Println(err)
return false
}
return true
return resp
}
func (o *OKCoin) GetOrderBook(symbol string) (bool) {