Skip to content

Commit

Permalink
Added fiat support to stats handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
thrasher- committed May 15, 2015
1 parent 13379ed commit 5f40de8
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 34 deletions.
2 changes: 1 addition & 1 deletion anxhttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion bitfinexhttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion bitstamphttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions btcchinahttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}()
}
Expand Down
2 changes: 1 addition & 1 deletion btcehttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion btcmarkets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions coinbasehttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cryptsyhttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion huobihttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion itbithttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion kraken.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions lakebtchttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions okcoinhttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)
}()
}
}
Expand Down
37 changes: 21 additions & 16 deletions stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -41,42 +42,46 @@ 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
}
}
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)
}
}
Expand All @@ -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)
}
}
Expand Down

0 comments on commit 5f40de8

Please sign in to comment.