Files
gocryptotrader/exchanges/btcmarkets/btcmarkets_wrapper.go
2017-04-08 02:42:18 +10:00

110 lines
3.5 KiB
Go

package btcmarkets
import (
"log"
"time"
"github.com/thrasher-/gocryptotrader/currency"
"github.com/thrasher-/gocryptotrader/exchanges"
"github.com/thrasher-/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-/gocryptotrader/exchanges/stats"
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
)
func (b *BTCMarkets) Start() {
go b.Run()
}
func (b *BTCMarkets) Run() {
if b.Verbose {
log.Printf("%s polling delay: %ds.\n", b.GetName(), b.RESTPollingDelay)
log.Printf("%s %d currencies enabled: %s.\n", b.GetName(), len(b.EnabledPairs), b.EnabledPairs)
}
for b.Enabled {
for _, x := range b.EnabledPairs {
curr := x
go func() {
ticker, err := b.GetTickerPrice(curr)
if err != nil {
return
}
BTCMarketsLastUSD, _ := currency.ConvertCurrency(ticker.Last, "AUD", "USD")
BTCMarketsBestBidUSD, _ := currency.ConvertCurrency(ticker.Bid, "AUD", "USD")
BTCMarketsBestAskUSD, _ := currency.ConvertCurrency(ticker.Ask, "AUD", "USD")
log.Printf("BTC Markets %s: Last %f (%f) Bid %f (%f) Ask %f (%f)\n", curr, BTCMarketsLastUSD, ticker.Last, BTCMarketsBestBidUSD, ticker.Bid, BTCMarketsBestAskUSD, ticker.Ask)
stats.AddExchangeInfo(b.GetName(), curr[0:3], curr[3:], ticker.Last, 0)
stats.AddExchangeInfo(b.GetName(), curr[0:3], "USD", BTCMarketsLastUSD, 0)
}()
}
time.Sleep(time.Second * b.RESTPollingDelay)
}
}
func (b *BTCMarkets) GetTickerPrice(currency string) (ticker.TickerPrice, error) {
tickerNew, err := ticker.GetTicker(b.GetName(), currency[0:3], "AUD")
if err == nil {
return tickerNew, nil
}
var tickerPrice ticker.TickerPrice
tick, err := b.GetTicker(currency)
if err != nil {
return tickerPrice, err
}
tickerPrice.Ask = tick.BestAsk
tickerPrice.Bid = tick.BestBID
tickerPrice.FirstCurrency = currency[0:3]
tickerPrice.SecondCurrency = "AUD"
tickerPrice.Last = tick.LastPrice
ticker.ProcessTicker(b.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice)
return tickerPrice, nil
}
func (b *BTCMarkets) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) {
ob, err := orderbook.GetOrderbook(b.GetName(), currency[0:3], "AUD")
if err == nil {
return ob, nil
}
var orderBook orderbook.OrderbookBase
orderbookNew, err := b.GetOrderbook(currency)
if err != nil {
return orderBook, err
}
for x, _ := range orderbookNew.Bids {
data := orderbookNew.Bids[x]
orderBook.Bids = append(orderBook.Bids, orderbook.OrderbookItem{Amount: data[1], Price: data[0]})
}
for x, _ := range orderbookNew.Asks {
data := orderbookNew.Asks[x]
orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: data[1], Price: data[0]})
}
orderBook.FirstCurrency = currency[0:3]
orderBook.SecondCurrency = "AUD"
orderbook.ProcessOrderbook(b.GetName(), orderBook.FirstCurrency, orderBook.SecondCurrency, orderBook)
return orderBook, nil
}
//GetExchangeAccountInfo : Retrieves balances for all enabled currencies for the BTCMarkets exchange
func (e *BTCMarkets) GetExchangeAccountInfo() (exchange.ExchangeAccountInfo, error) {
var response exchange.ExchangeAccountInfo
response.ExchangeName = e.GetName()
accountBalance, err := e.GetAccountBalance()
if err != nil {
return response, err
}
for i := 0; i < len(accountBalance); i++ {
var exchangeCurrency exchange.ExchangeAccountCurrencyInfo
exchangeCurrency.CurrencyName = accountBalance[i].Currency
exchangeCurrency.TotalValue = accountBalance[i].Balance
exchangeCurrency.Hold = accountBalance[i].PendingFunds
response.Currencies = append(response.Currencies, exchangeCurrency)
}
return response, nil
}