From 5f40de830071ee139b0b30e523a97bbe0a0e96fa Mon Sep 17 00:00:00 2001 From: Adrian Gallagher Date: Fri, 15 May 2015 15:42:41 +1000 Subject: [PATCH] Added fiat support to stats handler. --- anxhttp.go | 2 +- bitfinexhttp.go | 2 +- bitstamphttp.go | 2 +- btcchinahttp.go | 5 +++-- btcehttp.go | 2 +- btcmarkets.go | 3 ++- coinbasehttp.go | 4 ++-- cryptsyhttp.go | 2 +- huobihttp.go | 3 ++- itbithttp.go | 2 +- kraken.go | 2 +- lakebtchttp.go | 4 ++-- okcoinhttp.go | 7 ++++--- stats.go | 37 +++++++++++++++++++++---------------- 14 files changed, 43 insertions(+), 34 deletions(-) diff --git a/anxhttp.go b/anxhttp.go index d5a079dc..397ee11c 100644 --- a/anxhttp.go +++ b/anxhttp.go @@ -149,7 +149,7 @@ func (a *ANX) Run() { go func() { ticker := a.GetTicker(currency) log.Printf("ANX %s: Last %f High %f Low %f Volume %f\n", currency, ticker.Data.Last.Value, ticker.Data.High.Value, ticker.Data.Low.Value, ticker.Data.Vol.Value) - AddExchangeInfo(a.GetName(), currency, ticker.Data.Last.Value, ticker.Data.Vol.Value) + AddExchangeInfo(a.GetName(), currency[0:3], currency[3:], ticker.Data.Last.Value, ticker.Data.Vol.Value) }() } time.Sleep(time.Second * a.RESTPollingDelay) diff --git a/bitfinexhttp.go b/bitfinexhttp.go index e8c31969..16eb6e47 100644 --- a/bitfinexhttp.go +++ b/bitfinexhttp.go @@ -216,7 +216,7 @@ func (b *Bitfinex) Run() { go func() { ticker := b.GetTicker(currency) log.Printf("Bitfinex %s Last %f High %f Low %f Volume %f\n", currency, ticker.Last, ticker.High, ticker.Low, ticker.Volume) - AddExchangeInfo(b.GetName(), currency, ticker.Last, ticker.Volume) + AddExchangeInfo(b.GetName(), currency[0:3], currency[3:], ticker.Last, ticker.Volume) }() } time.Sleep(time.Second * b.RESTPollingDelay) diff --git a/bitstamphttp.go b/bitstamphttp.go index c6f4927b..f7aa2cf8 100644 --- a/bitstamphttp.go +++ b/bitstamphttp.go @@ -132,7 +132,7 @@ func (b *Bitstamp) Run() { go func() { ticker := b.GetTicker() log.Printf("Bitstamp %s: Last %f High %f Low %f Volume %f\n", currency, ticker.Last, ticker.High, ticker.Low, ticker.Volume) - AddExchangeInfo(b.GetName(), currency, ticker.Last, ticker.Volume) + AddExchangeInfo(b.GetName(), currency[0:3], currency[3:], ticker.Last, ticker.Volume) }() } time.Sleep(time.Second * b.RESTPollingDelay) diff --git a/btcchinahttp.go b/btcchinahttp.go index 4e869662..44fa5844 100644 --- a/btcchinahttp.go +++ b/btcchinahttp.go @@ -223,10 +223,11 @@ func (b *BTCChina) Run() { tickerHighUSD, _ := ConvertCurrency(ticker.High, "CNY", "USD") tickerLowUSD, _ := ConvertCurrency(ticker.Low, "CNY", "USD") log.Printf("BTCChina %s: Last %f (%f) High %f (%f) Low %f (%f) Volume %f\n", currency, tickerLastUSD, ticker.Last, tickerHighUSD, ticker.High, tickerLowUSD, ticker.Low, ticker.Vol) - AddExchangeInfo(b.GetName(), currency, tickerLastUSD, ticker.Vol) + AddExchangeInfo(b.GetName(), StringToUpper(currency[0:3]), StringToUpper(currency[3:]), ticker.Last, ticker.Vol) + AddExchangeInfo(b.GetName(), StringToUpper(currency[0:3]), "USD", tickerLastUSD, ticker.Vol) } else { log.Printf("BTCChina %s: Last %f High %f Low %f Volume %f\n", currency, ticker.Last, ticker.High, ticker.Low, ticker.Vol) - AddExchangeInfo(b.GetName(), currency, ticker.Last, ticker.Vol) + AddExchangeInfo(b.GetName(), StringToUpper(currency[0:3]), StringToUpper(currency[3:]), ticker.Last, ticker.Vol) } }() } diff --git a/btcehttp.go b/btcehttp.go index e34dadcc..9c3d32c5 100644 --- a/btcehttp.go +++ b/btcehttp.go @@ -122,7 +122,7 @@ func (b *BTCE) Run() { x = StringToUpper(x[0:3] + x[4:]) log.Printf("BTC-e %s: Last %f High %f Low %f Volume %f\n", x, y.Last, y.High, y.Low, y.Vol_cur) b.Ticker[x] = y - AddExchangeInfo(b.GetName(), x, y.Last, y.Vol_cur) + AddExchangeInfo(b.GetName(), StringToUpper(x[0:3]), StringToUpper(x[4:]), y.Last, y.Vol_cur) } }() time.Sleep(time.Second * b.RESTPollingDelay) diff --git a/btcmarkets.go b/btcmarkets.go index 507b1689..a25d7408 100644 --- a/btcmarkets.go +++ b/btcmarkets.go @@ -146,7 +146,8 @@ func (b *BTCMarkets) Run() { BTCMarketsBestBidUSD, _ := ConvertCurrency(ticker.BestBID, "AUD", "USD") BTCMarketsBestAskUSD, _ := ConvertCurrency(ticker.BestAsk, "AUD", "USD") log.Printf("BTC Markets %s: Last %f (%f) Bid %f (%f) Ask %f (%f)\n", currency, BTCMarketsLastUSD, ticker.LastPrice, BTCMarketsBestBidUSD, ticker.BestBID, BTCMarketsBestAskUSD, ticker.BestAsk) - AddExchangeInfo(b.GetName(), currency, BTCMarketsLastUSD, 0) + AddExchangeInfo(b.GetName(), currency[0:3], currency[3:], ticker.LastPrice, 0) + AddExchangeInfo(b.GetName(), currency[0:3], "USD", BTCMarketsLastUSD, 0) }() } time.Sleep(time.Second * b.RESTPollingDelay) diff --git a/coinbasehttp.go b/coinbasehttp.go index 5a440c22..66822ce1 100644 --- a/coinbasehttp.go +++ b/coinbasehttp.go @@ -154,7 +154,7 @@ func (c *Coinbase) Run() { if err != nil { log.Println(err) } else { - log.Printf("%s Updating available pairs. Changed %s.\n", c.Name, diff) + log.Printf("%s Updating available pairs. Difference: %s.\n", c.Name, diff) exch.AvailablePairs = JoinStrings(currencies, ",") UpdateExchangeConfig(exch) } @@ -168,7 +168,7 @@ func (c *Coinbase) Run() { stats := c.GetStats(currency) ticker := c.GetTicker(currency) log.Printf("Coinbase %s: Last %f High %f Low %f Volume %f\n", currency, ticker.Price, stats.High, stats.Low, stats.Volume) - AddExchangeInfo(c.GetName(), currency, ticker.Price, stats.Volume) + AddExchangeInfo(c.GetName(), currency[0:3], currency[4:], ticker.Price, stats.Volume) }() } time.Sleep(time.Second * c.RESTPollingDelay) diff --git a/cryptsyhttp.go b/cryptsyhttp.go index 3e20e6bf..574649fc 100644 --- a/cryptsyhttp.go +++ b/cryptsyhttp.go @@ -208,7 +208,7 @@ func (c *Cryptsy) Run() { market := c.Market[x] if market.ID != "" { log.Printf("Cryptsy %s: Last %f High %f Low %f Volume %f\n", x, market.LastTrade.Price, market.DayStats.PriceHigh, market.DayStats.PriceLow, market.DayStats.Volume) - AddExchangeInfo(c.GetName(), x, market.LastTrade.Price, market.DayStats.Volume) + AddExchangeInfo(c.GetName(), x[0:3], x[3:], market.LastTrade.Price, market.DayStats.Volume) } } } diff --git a/huobihttp.go b/huobihttp.go index f1ea9ba2..4fcefd54 100644 --- a/huobihttp.go +++ b/huobihttp.go @@ -92,7 +92,8 @@ func (h *HUOBI) Run() { 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.Vol) - AddExchangeInfo(h.GetName(), StringToUpper(currency), HuobiLastUSD, ticker.Vol) + AddExchangeInfo(h.GetName(), StringToUpper(currency[0:3]), StringToUpper(currency[3:]), ticker.Last, ticker.Vol) + AddExchangeInfo(h.GetName(), StringToUpper(currency[0:3]), "USD", HuobiLastUSD, ticker.Vol) }() } time.Sleep(time.Second * h.RESTPollingDelay) diff --git a/itbithttp.go b/itbithttp.go index b5af68fa..9515875e 100644 --- a/itbithttp.go +++ b/itbithttp.go @@ -96,7 +96,7 @@ func (i *ItBit) Run() { go func() { ticker := i.GetTicker(currency) log.Printf("ItBit %s: Last %f High %f Low %f Volume %f\n", currency, ticker.LastPrice, ticker.High24h, ticker.Low24h, ticker.Volume24h) - AddExchangeInfo(i.GetName(), currency, ticker.LastPrice, ticker.Volume24h) + AddExchangeInfo(i.GetName(), currency[0:3], currency[3:], ticker.LastPrice, ticker.Volume24h) }() } time.Sleep(time.Second * i.RESTPollingDelay) diff --git a/kraken.go b/kraken.go index ac8c03c9..c7de428c 100644 --- a/kraken.go +++ b/kraken.go @@ -101,7 +101,7 @@ func (k *Kraken) Run() { for _, x := range k.EnabledPairs { ticker := k.Ticker[x] log.Printf("Kraken %s Last %f High %f Low %f Volume %f\n", x, ticker.Last, ticker.High, ticker.Low, ticker.Volume) - AddExchangeInfo(k.GetName(), x, ticker.Last, ticker.Volume) + AddExchangeInfo(k.GetName(), x[0:3], x[3:], ticker.Last, ticker.Volume) } } time.Sleep(time.Second * k.RESTPollingDelay) diff --git a/lakebtchttp.go b/lakebtchttp.go index cbbf1561..ea499a21 100644 --- a/lakebtchttp.go +++ b/lakebtchttp.go @@ -108,10 +108,10 @@ func (l *LakeBTC) Run() { for _, x := range l.EnabledPairs { if x == "BTCUSD" { log.Printf("LakeBTC BTC USD: Last %f High %f Low %f Volume %f\n", ticker.USD.Last, ticker.USD.High, ticker.USD.Low, ticker.USD.Volume) - AddExchangeInfo(l.GetName(), "BTCUSD", ticker.USD.Last, ticker.USD.Volume) + AddExchangeInfo(l.GetName(), x[0:3], x[3:], ticker.USD.Last, ticker.USD.Volume) } else if x == "BTCCNY" { log.Printf("LakeBTC BTC CNY: Last %f High %f Low %f Volume %f\n", ticker.CNY.Last, ticker.CNY.High, ticker.CNY.Low, ticker.CNY.Volume) - AddExchangeInfo(l.GetName(), "BTCCNY", ticker.CNY.Last, ticker.CNY.Volume) + AddExchangeInfo(l.GetName(), x[0:3], x[3:], ticker.CNY.Last, ticker.CNY.Volume) } } time.Sleep(time.Second * l.RESTPollingDelay) diff --git a/okcoinhttp.go b/okcoinhttp.go index 30ec14b0..d5b6c806 100644 --- a/okcoinhttp.go +++ b/okcoinhttp.go @@ -217,13 +217,13 @@ func (o *OKCoin) Run() { go func() { ticker := o.GetFuturesTicker(currency, futuresValue) log.Printf("OKCoin Intl Futures %s (%s): Last %f High %f Low %f Volume %f\n", currency, futuresValue, ticker.Last, ticker.High, ticker.Low, ticker.Vol) - AddExchangeInfo(o.GetName(), currency, ticker.Last, ticker.Vol) + AddExchangeInfo(o.GetName(), StringToUpper(currency[0:3]), StringToUpper(currency[4:]), ticker.Last, ticker.Vol) }() } go func() { ticker := o.GetTicker(currency) log.Printf("OKCoin Intl Spot %s: Last %f High %f Low %f Volume %f\n", currency, ticker.Last, ticker.High, ticker.Low, ticker.Vol) - AddExchangeInfo(o.GetName(), currency, ticker.Last, ticker.Vol) + AddExchangeInfo(o.GetName(), StringToUpper(currency[0:3]), StringToUpper(currency[4:]), ticker.Last, ticker.Vol) }() } else { go func() { @@ -232,7 +232,8 @@ func (o *OKCoin) Run() { tickerHighUSD, _ := ConvertCurrency(ticker.High, "CNY", "USD") tickerLowUSD, _ := ConvertCurrency(ticker.Low, "CNY", "USD") log.Printf("OKCoin China %s: Last %f (%f) High %f (%f) Low %f (%f) Volume %f\n", currency, tickerLastUSD, ticker.Last, tickerHighUSD, ticker.High, tickerLowUSD, ticker.Low, ticker.Vol) - AddExchangeInfo(o.GetName(), currency, ticker.Last, ticker.Vol) + AddExchangeInfo(o.GetName(), StringToUpper(currency[0:3]), StringToUpper(currency[4:]), ticker.Last, ticker.Vol) + AddExchangeInfo(o.GetName(), StringToUpper(currency[0:3]), "USD", tickerLastUSD, ticker.Vol) }() } } diff --git a/stats.go b/stats.go index 9cb8520e..f639be44 100644 --- a/stats.go +++ b/stats.go @@ -5,10 +5,11 @@ import ( ) type ExchangeInfo struct { - Exchange string - Currency string - Price float64 - Volume float64 + Exchange string + CryptoCurrency string + FiatCurrency string + Price float64 + Volume float64 } var ExchInfo []ExchangeInfo @@ -41,30 +42,34 @@ func (this ByVolume) Swap(i, j int) { this[i], this[j] = this[j], this[i] } -func AddExchangeInfo(exchange, currency string, price, volume float64) { +func AddExchangeInfo(exchange, crypto, fiat string, price, volume float64) { + if !IsFiatCurrency(fiat) { + return + } if len(ExchInfo) == 0 { - AppendExchangeInfo(exchange, currency, price, volume) + AppendExchangeInfo(exchange, crypto, fiat, price, volume) } else { - if ExchangeInfoAlreadyExists(exchange, currency, price, volume) { + if ExchangeInfoAlreadyExists(exchange, crypto, fiat, price, volume) { return } else { - AppendExchangeInfo(exchange, currency, price, volume) + AppendExchangeInfo(exchange, crypto, fiat, price, volume) } } } -func AppendExchangeInfo(exchange, currency string, price, volume float64) { +func AppendExchangeInfo(exchange, crypto, fiat string, price, volume float64) { exch := ExchangeInfo{} exch.Exchange = exchange - exch.Currency = currency + exch.CryptoCurrency = crypto + exch.FiatCurrency = fiat exch.Price = price exch.Volume = volume ExchInfo = append(ExchInfo, exch) } -func ExchangeInfoAlreadyExists(exchange, currency string, price, volume float64) (bool) { +func ExchangeInfoAlreadyExists(exchange, crypto, fiat string, price, volume float64) bool { for i, _ := range ExchInfo { - if ExchInfo[i].Exchange == exchange && ExchInfo[i].Currency == currency { + if ExchInfo[i].Exchange == exchange && ExchInfo[i].CryptoCurrency == crypto && ExchInfo[i].FiatCurrency == fiat { ExchInfo[i].Price, ExchInfo[i].Volume = price, volume return true } @@ -72,11 +77,11 @@ func ExchangeInfoAlreadyExists(exchange, currency string, price, volume float64) return false } -func SortExchangesByVolume(currency string, reverse bool) []ExchangeInfo { +func SortExchangesByVolume(crypto, fiat string, reverse bool) []ExchangeInfo { info := []ExchangeInfo{} for _, x := range ExchInfo { - if x.Currency == currency { + if x.CryptoCurrency == crypto && x.FiatCurrency == fiat { info = append(info, x) } } @@ -89,11 +94,11 @@ func SortExchangesByVolume(currency string, reverse bool) []ExchangeInfo { return info } -func SortExchangesByPrice(currency string, reverse bool) []ExchangeInfo { +func SortExchangesByPrice(crypto, fiat string, reverse bool) []ExchangeInfo { info := []ExchangeInfo{} for _, x := range ExchInfo { - if x.Currency == currency { + if x.CryptoCurrency == crypto && x.FiatCurrency == fiat { info = append(info, x) } }