Retrieve and display ticker pricing for various exchanges.

This commit is contained in:
Adrian Gallagher
2014-11-15 22:11:19 +11:00
parent 7dea37cc32
commit 091db41e85
7 changed files with 114 additions and 71 deletions

View File

@@ -121,11 +121,11 @@ func (b *Bitfinex) SendAuthenticatedHTTPRequest(path string, params map[string]i
return nil
}
func (b *Bitfinex) GetTicker(symbol string) (interface{}) {
func (b *Bitfinex) GetTicker(symbol string) (BitfinexTicker) {
err := SendHTTPRequest(BITFINEX_API_URL + BITFINEX_TICKER + symbol, true, &b.Ticker)
if err != nil {
fmt.Println(err)
return nil
return BitfinexTicker{}
}
return b.Ticker
}

View File

@@ -63,12 +63,12 @@ type ConversionRate struct {
Sell string
}
func (b *Bitstamp) GetTicker() (interface{}) {
func (b *Bitstamp) GetTicker() (BitstampTicker) {
err := SendHTTPRequest(BITSTAMP_API_URL + BITSTAMP_API_TICKER, true, &b.Ticker)
if err != nil {
fmt.Println(err)
return nil
return BitstampTicker{}
}
return b.Ticker

View File

@@ -25,22 +25,22 @@ type BTCChina struct {
APISecret, APIKey string
}
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 BTCChinaTicker struct {
High string
Low string
Buy string
Sell string
Last string
Vol string
Date int64
Vwap string
Prev_close string
Open string
}
func (b *BTCChina) GetTicker(symbol string) (BTCChinaTicker) {
type Response struct {
Ticker Ticker
Ticker BTCChinaTicker
}
resp := Response{}
@@ -48,9 +48,9 @@ func (b *BTCChina) GetTicker(symbol string) (interface{}) {
err := SendHTTPRequest(req, true, &resp)
if err != nil {
fmt.Println(err)
return nil
return BTCChinaTicker{}
}
return resp
return resp.Ticker
}
func (b *BTCChina) GetTradesLast24h(symbol string) (bool) {

View File

@@ -28,22 +28,22 @@ 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 BTCeTicker struct {
High float64
Low float64
Avg float64
Vol float64
Vol_cur float64
Last float64
Buy float64
Sell float64
Updated int64
Server_time int64
}
func (b *BTCE) GetTicker(symbol string) (BTCeTicker) {
type Response struct {
Ticker Ticker
Ticker BTCeTicker
}
response := Response{}
@@ -51,9 +51,9 @@ func (b *BTCE) GetTicker(symbol string) (interface{}) {
err := SendHTTPRequest(req, true, &response)
if err != nil {
fmt.Println(err)
return nil
return BTCeTicker{}
}
return response
return response.Ticker
}
func (b *BTCE) GetInfo() {

View File

@@ -3,7 +3,6 @@ package main
import (
"net/http"
"fmt"
"strings"
"encoding/json"
"io/ioutil"
"errors"
@@ -11,7 +10,6 @@ import (
func SendHTTPRequest(url string, jsonDecode bool, result interface{}) (err error) {
res, err := http.Get(url)
fmt.Println("Attempting connection to: " + url)
if err != nil {
fmt.Println(err)
@@ -24,10 +22,10 @@ func SendHTTPRequest(url string, jsonDecode bool, result interface{}) (err error
}
contents, _ := ioutil.ReadAll(res.Body)
fmt.Printf("Recieved raw: %s\n", string(contents))
//fmt.Printf("Recieved raw: %s\n", string(contents))
if jsonDecode {
err = JsonDecode(string(contents), result)
err := json.Unmarshal(contents, &result)
if err != nil {
return errors.New("Unable to JSON decode body.")
@@ -36,14 +34,4 @@ func SendHTTPRequest(url string, jsonDecode bool, result interface{}) (err error
result = contents
}
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
}

77
main.go
View File

@@ -3,6 +3,8 @@ package main
import (
"log"
"time"
"os"
"os/exec"
)
type Exchange struct {
@@ -10,27 +12,76 @@ type Exchange struct {
bitstamp Bitstamp
bitfinex Bitfinex
btce BTCE
lakeBTC LakeBTC
okcoin OKCoin
itbit ItBit
okcoinChina OKCoin
okcoinIntl OKCoin
}
func main() {
log.Println("Bot started")
exchange := Exchange{}
exchange.okcoinChina.SetURL(OKCOIN_API_URL_CHINA)
exchange.okcoinIntl.SetURL(OKCOIN_API_URL)
//temp until proper asynchronous method of getting pricing/order books is coded
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"))
go func() {
BTCChinaBTC := exchange.btcchina.GetTicker("btccny")
log.Printf("BTCChina BTC: Last %s High %s Low %s Volume %s\n", BTCChinaBTC.Last, BTCChinaBTC.High, BTCChinaBTC.Low, BTCChinaBTC.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)
}()
go func() {
BitfinexBTC := exchange.bitfinex.GetTicker("btcusd")
log.Printf("Bitfinex BTC: Last %s High %s Low %s Volume %s\n", BitfinexBTC.Last_price, BitfinexBTC.High, BitfinexBTC.Low, BitfinexBTC.Volume)
}()
go func() {
BTCeBTC := exchange.btce.GetTicker("btc_usd")
log.Printf("BTC-e BTC: Last %f High %f Low %f Volume %f\n", BTCeBTC.Last, BTCeBTC.High, BTCeBTC.Low, BTCeBTC.Vol_cur)
}()
go func() {
OKCoinChinaBTC := exchange.okcoinChina.GetTicker("btc_cny")
log.Printf("OKCoin China BTC: Last %s High %s Low %s Volume %s\n", OKCoinChinaBTC.Last, OKCoinChinaBTC.High, OKCoinChinaBTC.Low, OKCoinChinaBTC.Vol)
}()
go func() {
OKCoinChinaIntlBTC := exchange.okcoinIntl.GetTicker("btc_usd")
log.Printf("OKCoin Intl BTC: Last %s High %s Low %s Volume %s\n", OKCoinChinaIntlBTC.Last, OKCoinChinaIntlBTC.High, OKCoinChinaIntlBTC.Low, OKCoinChinaIntlBTC.Vol)
}()
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)
}()
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)
}()
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)
}()
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)
}()
go func() {
OKCoinChinaIntlLTC := exchange.okcoinIntl.GetTicker("ltc_usd")
log.Printf("OKCoin Intl LTC: Last %s High %s Low %s Volume %s\n", OKCoinChinaIntlLTC.Last, OKCoinChinaIntlLTC.High, OKCoinChinaIntlLTC.Low, OKCoinChinaIntlLTC.Vol)
}()
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)
cmd := exec.Command("cmd", "/c", "cls")
cmd.Stdout = os.Stdout
cmd.Run()
}
}

View File

@@ -18,7 +18,7 @@ const (
)
type OKCoin struct {
PartnerID, SecretKey string
APIUrl, PartnerID, SecretKey string
}
type OKCoinTicker struct {
@@ -35,21 +35,25 @@ type OKCoinTickerResponse struct {
Ticker OKCoinTicker
}
func (o *OKCoin) GetTicker(symbol string) (interface{}) {
func (o *OKCoin) SetURL(url string) {
o.APIUrl = url
}
func (o *OKCoin) GetTicker(symbol string) (OKCoinTicker) {
resp := OKCoinTickerResponse{}
path := fmt.Sprintf("ticker.do?symbol=%s&ok=1", symbol)
err := SendHTTPRequest(OKCOIN_API_URL + path, true, &resp)
err := SendHTTPRequest(o.APIUrl + path, true, &resp)
if err != nil {
fmt.Println(err)
return false
return OKCoinTicker{}
}
return resp
return resp.Ticker
}
func (o *OKCoin) GetOrderBook(symbol string) (bool) {
path := "depth.do?symbol=" + symbol
err := SendHTTPRequest(OKCOIN_API_URL + path, true, nil)
err := SendHTTPRequest(o.APIUrl + path, true, nil)
if err != nil {
fmt.Println(err)
return false
@@ -59,7 +63,7 @@ func (o *OKCoin) GetOrderBook(symbol string) (bool) {
func (o *OKCoin) GetTradeHistory(symbol string) (bool) {
path := "trades.do?symbol=" + symbol
err := SendHTTPRequest(OKCOIN_API_URL + path, true, nil)
err := SendHTTPRequest(o.APIUrl + path, true, nil)
if err != nil {
fmt.Println(err)
return false
@@ -172,7 +176,7 @@ func (o *OKCoin) SendAuthenticatedHTTPRequest(method string, v url.Values) (err
encoded := v.Encode() + "&partner=" + o.PartnerID
fmt.Printf("Signature: %s\n", signature)
path := OKCOIN_API_URL + method
path := o.APIUrl + method
fmt.Printf("Sending POST request to %s with params %s\n", path, encoded)
reqBody := strings.NewReader(encoded)