mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 15:09:42 +00:00
Added fetching currency exchange rate and conversion support.
This commit is contained in:
@@ -38,11 +38,18 @@ const (
|
||||
|
||||
type BitfinexStats struct {
|
||||
Period int64
|
||||
Volume string
|
||||
Volume float64 `json:",string"`
|
||||
}
|
||||
|
||||
type BitfinexTicker struct {
|
||||
Mid, Bid, Ask, Last_price, Low, High, Volume, Timestamp string
|
||||
Mid float64 `json:",string"`
|
||||
Bid float64 `json:",string"`
|
||||
Ask float64 `json:",string"`
|
||||
Last float64 `json:"Last_price,string"`
|
||||
Low float64 `json:",string"`
|
||||
High float64 `json:",string"`
|
||||
Volume float64 `json:",string"`
|
||||
Timestamp string
|
||||
}
|
||||
|
||||
type BookStructure struct {
|
||||
|
||||
@@ -44,7 +44,13 @@ type Bitstamp struct {
|
||||
}
|
||||
|
||||
type BitstampTicker struct {
|
||||
Last, High, Low, Vwap, Volume, Bid, Ask string
|
||||
Last float64 `json:",string"`
|
||||
High float64 `json:",string"`
|
||||
Low float64 `json:",string"`
|
||||
Vwap float64 `json:",string"`
|
||||
Volume float64 `json:",string"`
|
||||
Bid float64 `json:",string"`
|
||||
Ask float64 `json:",string"`
|
||||
}
|
||||
|
||||
type Orderbook struct {
|
||||
|
||||
@@ -26,16 +26,16 @@ type BTCChina struct {
|
||||
}
|
||||
|
||||
type BTCChinaTicker struct {
|
||||
High string
|
||||
Low string
|
||||
Buy string
|
||||
Sell string
|
||||
Last string
|
||||
Vol string
|
||||
High float64 `json:",string"`
|
||||
Low float64 `json:",string"`
|
||||
Buy float64 `json:",string"`
|
||||
Sell float64 `json:",string"`
|
||||
Last float64 `json:",string"`
|
||||
Vol float64 `json:",string"`
|
||||
Date int64
|
||||
Vwap string
|
||||
Prev_close string
|
||||
Open string
|
||||
Vwap float64 `json:",string"`
|
||||
Prev_close float64 `json:",string"`
|
||||
Open float64 `json:",string"`
|
||||
}
|
||||
|
||||
func (b *BTCChina) GetTicker(symbol string) (BTCChinaTicker) {
|
||||
|
||||
116
currency.go
Normal file
116
currency.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"time"
|
||||
"errors"
|
||||
"log"
|
||||
)
|
||||
|
||||
type Rate struct {
|
||||
Id string `json:"id"`
|
||||
Name string `json:"Name"`
|
||||
Rate float64 `json:",string"`
|
||||
Date string `json:"Date"`
|
||||
Time string `json:"Time"`
|
||||
Ask float64 `json:",string"`
|
||||
Bid float64 `json:",string"`
|
||||
}
|
||||
|
||||
type YahooJSONResponseInfo struct {
|
||||
Count int `json:"count"`
|
||||
Created time.Time `json:"created"`
|
||||
Lang string `json:"lang"`
|
||||
}
|
||||
|
||||
type YahooJSONResponse struct {
|
||||
Query struct {
|
||||
YahooJSONResponseInfo
|
||||
Results struct {
|
||||
Rate []Rate `json:"rate"`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
YAHOO_YQL_URL = "http://query.yahooapis.com/v1/public/yql"
|
||||
YAHOO_DATABASE = "store://datatables.org/alltableswithkeys"
|
||||
CURRENCIES = "USD,CNY,AUD,EUR"
|
||||
|
||||
)
|
||||
|
||||
var (
|
||||
CurrencyStore YahooJSONResponse
|
||||
ErrCurrencyDataNotFetched = errors.New("Yahoo currency data has not been fetched yet.")
|
||||
ErrCurrencyNotFound = errors.New("Unable to find specified currency.")
|
||||
)
|
||||
|
||||
func MakeCurrencyPairs() (string) {
|
||||
currencies := strings.Split(CURRENCIES, ",")
|
||||
pairs := ""
|
||||
count := len(currencies)
|
||||
for i := 0; i < count; i++ {
|
||||
currency := currencies[i]
|
||||
for j := 0; j < count; j++ {
|
||||
if currency != currencies[j] {
|
||||
pairs += currency + currencies[j] + ","
|
||||
}
|
||||
}
|
||||
}
|
||||
return pairs[0:len(pairs)-1]
|
||||
}
|
||||
|
||||
func ConvertCurrency(amount float64, from, to string) (float64, error) {
|
||||
if CurrencyStore.Query.YahooJSONResponseInfo.Count == 0 {
|
||||
return 0, ErrCurrencyDataNotFetched
|
||||
}
|
||||
|
||||
currency := strings.ToUpper(from + to)
|
||||
for i := 0; i < CurrencyStore.Query.YahooJSONResponseInfo.Count; i++ {
|
||||
if CurrencyStore.Query.Results.Rate[i].Id == currency {
|
||||
return amount * CurrencyStore.Query.Results.Rate[i].Rate, nil
|
||||
}
|
||||
}
|
||||
return 0, ErrCurrencyNotFound
|
||||
}
|
||||
|
||||
func QueryYahooCurrencyValues() (error) {
|
||||
currencyPairs := MakeCurrencyPairs()
|
||||
log.Printf("Supported currency pairs: %s\n", currencyPairs)
|
||||
|
||||
values := url.Values{}
|
||||
values.Set("q", fmt.Sprintf("SELECT * from yahoo.finance.xchange WHERE pair in (\"%s\")", currencyPairs))
|
||||
values.Set("format", "json")
|
||||
values.Set("env", YAHOO_DATABASE)
|
||||
path := YAHOO_YQL_URL+"?"+values.Encode()
|
||||
log.Println("Sending request to: " + path)
|
||||
req, err := http.NewRequest("GET", path, strings.NewReader(""))
|
||||
|
||||
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 err
|
||||
}
|
||||
|
||||
contents, _ := ioutil.ReadAll(resp.Body)
|
||||
err = json.Unmarshal(contents, &CurrencyStore)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resp.Body.Close()
|
||||
return nil
|
||||
}
|
||||
10
huobihttp.go
10
huobihttp.go
@@ -22,12 +22,12 @@ type HUOBI struct {
|
||||
}
|
||||
|
||||
type HuobiTicker struct {
|
||||
High string
|
||||
Low string
|
||||
Last string
|
||||
High float64 `json:",string"`
|
||||
Low float64 `json:",string"`
|
||||
Last float64 `json:",string"`
|
||||
Vol float64
|
||||
Buy string
|
||||
Sell string
|
||||
Buy float64 `json:",string"`
|
||||
Sell float64 `json:",string"`
|
||||
}
|
||||
|
||||
type HuobiTickerResponse struct {
|
||||
|
||||
29
itbithttp.go
29
itbithttp.go
@@ -24,14 +24,35 @@ type ItBit struct {
|
||||
ClientKey, APISecret, UserID string
|
||||
}
|
||||
|
||||
func (i *ItBit) GetTicker(currency string) (bool) {
|
||||
type ItBitTicker struct {
|
||||
Pair string
|
||||
Bid float64 `json:",string"`
|
||||
BidAmt float64 `json:",string"`
|
||||
Ask float64 `json:",string"`
|
||||
AskAmt float64 `json:",string"`
|
||||
LastPrice float64 `json:",string"`
|
||||
LastAmt float64 `json:",string"`
|
||||
Volume24h float64 `json:",string"`
|
||||
VolumeToday float64 `json:",string"`
|
||||
High24h float64 `json:",string"`
|
||||
Low24h float64 `json:",string"`
|
||||
HighToday float64 `json:",string"`
|
||||
LowToday float64 `json:",string"`
|
||||
OpenToday float64 `json:",string"`
|
||||
VwapToday float64 `json:",string"`
|
||||
Vwap24h float64 `json:",string"`
|
||||
ServertimeUTC string
|
||||
}
|
||||
|
||||
func (i *ItBit) GetTicker(currency string) (ItBitTicker) {
|
||||
path := ITBIT_API_URL + "/markets/" + currency + "/ticker"
|
||||
err := SendHTTPRequest(path, true, nil)
|
||||
var itbitTicker ItBitTicker
|
||||
err := SendHTTPRequest(path, true, &itbitTicker)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return false
|
||||
return ItBitTicker{}
|
||||
}
|
||||
return true
|
||||
return itbitTicker
|
||||
}
|
||||
|
||||
func (i *ItBit) GetOrderbook(currency string) (bool) {
|
||||
|
||||
94
main.go
94
main.go
@@ -14,6 +14,7 @@ type Exchange struct {
|
||||
btce BTCE
|
||||
okcoinChina OKCoin
|
||||
okcoinIntl OKCoin
|
||||
itbit ItBit
|
||||
huobi HUOBI
|
||||
}
|
||||
|
||||
@@ -22,27 +23,65 @@ func main() {
|
||||
exchange := Exchange{}
|
||||
exchange.okcoinChina.SetURL(OKCOIN_API_URL_CHINA)
|
||||
exchange.okcoinIntl.SetURL(OKCOIN_API_URL)
|
||||
err := QueryYahooCurrencyValues()
|
||||
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
return
|
||||
}
|
||||
|
||||
//temp until proper asynchronous method of getting pricing/order books is coded
|
||||
for {
|
||||
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)
|
||||
BTCChinaBTCLastUSD, _ := ConvertCurrency(BTCChinaBTC.Last, "CNY", "USD")
|
||||
BTCChinaBTCHighUSD, _ := ConvertCurrency(BTCChinaBTC.High, "CNY", "USD")
|
||||
BTCChinaBTCLowUSD, _ := ConvertCurrency(BTCChinaBTC.Low, "CNY", "USD")
|
||||
log.Printf("BTCChina BTC: Last %f (%f) High %f (%f) Low %f (%f) Volume %f\n", BTCChinaBTCLastUSD, BTCChinaBTC.Last, BTCChinaBTCHighUSD, BTCChinaBTC.High, BTCChinaBTCLowUSD, BTCChinaBTC.Low, BTCChinaBTC.Vol)
|
||||
}()
|
||||
|
||||
go func() {
|
||||
BTCChinaLTC := exchange.btcchina.GetTicker("ltccny")
|
||||
BTCChinaLTCLastUSD, _ := ConvertCurrency(BTCChinaLTC.Last, "CNY", "USD")
|
||||
BTCChinaLTCHighUSD, _ := ConvertCurrency(BTCChinaLTC.High, "CNY", "USD")
|
||||
BTCChinaLTCLowUSD, _ := ConvertCurrency(BTCChinaLTC.Low, "CNY", "USD")
|
||||
log.Printf("BTCChina LTC: Last %f (%f) High %f (%f) Low %f (%f) Volume %f\n", BTCChinaLTCLastUSD, BTCChinaLTC.Last, BTCChinaLTCHighUSD, BTCChinaLTC.High, BTCChinaLTCLowUSD, BTCChinaLTC.Low, BTCChinaLTC.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)
|
||||
HuobiBTCLastUSD, _ := ConvertCurrency(HuobiBTC.Last, "CNY", "USD")
|
||||
HuobiBTCHighUSD, _ := ConvertCurrency(HuobiBTC.High, "CNY", "USD")
|
||||
HuobiBTCLowUSD, _ := ConvertCurrency(HuobiBTC.Low, "CNY", "USD")
|
||||
log.Printf("Huobi BTC: Last %f (%f) High %f (%f) Low %f (%f) Volume %f\n", HuobiBTCLastUSD, HuobiBTC.Last, HuobiBTCHighUSD, HuobiBTC.High, HuobiBTCLowUSD, HuobiBTC.Low, HuobiBTC.Vol)
|
||||
}()
|
||||
|
||||
go func() {
|
||||
HuobiLTC := exchange.huobi.GetTicker("btc")
|
||||
HuobiLTCLastUSD, _ := ConvertCurrency(HuobiLTC.Last, "CNY", "USD")
|
||||
HuobiLTCHighUSD, _ := ConvertCurrency(HuobiLTC.High, "CNY", "USD")
|
||||
HuobiLTCLowUSD, _ := ConvertCurrency(HuobiLTC.Low, "CNY", "USD")
|
||||
log.Printf("Huobi BTC: Last %f (%f) High %f (%f) Low %f (%f) Volume %f\n", HuobiLTCLastUSD, HuobiLTC.Last, HuobiLTCHighUSD, HuobiLTC.High, HuobiLTCLowUSD, HuobiLTC.Low, HuobiLTC.Vol)
|
||||
}()
|
||||
|
||||
go func() {
|
||||
ItbitBTC := exchange.itbit.GetTicker("XBTUSD")
|
||||
log.Printf("ItBit BTC: Last %f High %f Low %f Volume %f\n", ItbitBTC.LastPrice, ItbitBTC.High24h, ItbitBTC.Low24h, ItbitBTC.Volume24h)
|
||||
}()
|
||||
|
||||
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)
|
||||
log.Printf("Bitstamp BTC: Last %f High %f Low %f Volume %f\n", BitstampBTC.Last, BitstampBTC.High, BitstampBTC.Low, BitstampBTC.Volume)
|
||||
}()
|
||||
|
||||
go func() {
|
||||
BitfinexLTC := exchange.bitfinex.GetTicker("ltcusd")
|
||||
log.Printf("Bitfinex LTC: Last %f High %f Low %f Volume %f\n", BitfinexLTC.Last, BitfinexLTC.High, BitfinexLTC.Low, BitfinexLTC.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)
|
||||
log.Printf("Bitfinex BTC: Last %f High %f Low %f Volume %f\n", BitfinexBTC.Last, BitfinexBTC.High, BitfinexBTC.Low, BitfinexBTC.Volume)
|
||||
}()
|
||||
|
||||
go func() {
|
||||
@@ -50,49 +89,40 @@ func main() {
|
||||
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() {
|
||||
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() {
|
||||
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() {
|
||||
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("btc_cny")
|
||||
OKCoinChinaBTCLastUSD, _ := ConvertCurrency(OKCoinChinaBTC.Last, "CNY", "USD")
|
||||
OKCoinChinaBTCHighUSD, _ := ConvertCurrency(OKCoinChinaBTC.High, "CNY", "USD")
|
||||
OKCoinChinaBTCLowUSD, _ := ConvertCurrency(OKCoinChinaBTC.Low, "CNY", "USD")
|
||||
log.Printf("OKCoin China: Last %f (%f) High %f (%f) Low %f (%f) Volume %f\n", OKCoinChinaBTCLastUSD, OKCoinChinaBTC.Last, OKCoinChinaBTCHighUSD, OKCoinChinaBTC.High, OKCoinChinaBTCLowUSD, OKCoinChinaBTC.Low, OKCoinChinaBTC.Vol)
|
||||
}()
|
||||
|
||||
go func() {
|
||||
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)
|
||||
OKCoinChinaLTCLastUSD, _ := ConvertCurrency(OKCoinChinaLTC.Last, "CNY", "USD")
|
||||
OKCoinChinaLTCHighUSD, _ := ConvertCurrency(OKCoinChinaLTC.High, "CNY", "USD")
|
||||
OKCoinChinaLTCLowUSD, _ := ConvertCurrency(OKCoinChinaLTC.Low, "CNY", "USD")
|
||||
log.Printf("OKCoin China: Last %f (%f) High %f (%f) Low %f (%f) Volume %f\n", OKCoinChinaLTCLastUSD, OKCoinChinaLTC.Last, OKCoinChinaLTCHighUSD, OKCoinChinaLTC.High, OKCoinChinaLTCLowUSD, OKCoinChinaLTC.Low, OKCoinChinaLTC.Vol)
|
||||
}()
|
||||
|
||||
go func() {
|
||||
OKCoinChinaIntlBTC := exchange.okcoinIntl.GetTicker("btc_usd")
|
||||
log.Printf("OKCoin Intl BTC: Last %f High %f Low %f Volume %f\n", OKCoinChinaIntlBTC.Last, OKCoinChinaIntlBTC.High, OKCoinChinaIntlBTC.Low, OKCoinChinaIntlBTC.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)
|
||||
}()
|
||||
|
||||
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)
|
||||
log.Printf("OKCoin Intl LTC: Last %f High %f Low %f Volume %f\n", OKCoinChinaIntlLTC.Last, OKCoinChinaIntlLTC.High, OKCoinChinaIntlLTC.Low, OKCoinChinaIntlLTC.Vol)
|
||||
}()
|
||||
|
||||
time.Sleep(time.Second * 15)
|
||||
cmd := exec.Command("cmd", "/c", "cls")
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Run()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,12 +22,12 @@ type OKCoin struct {
|
||||
}
|
||||
|
||||
type OKCoinTicker struct {
|
||||
Buy string
|
||||
High string
|
||||
Last string
|
||||
Low string
|
||||
Sell string
|
||||
Vol string
|
||||
Buy float64 `json:",string"`
|
||||
High float64 `json:",string"`
|
||||
Last float64 `json:",string"`
|
||||
Low float64 `json:",string"`
|
||||
Sell float64 `json:",string"`
|
||||
Vol float64 `json:",string"`
|
||||
}
|
||||
|
||||
type OKCoinTickerResponse struct {
|
||||
|
||||
Reference in New Issue
Block a user