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

132 lines
3.9 KiB
Go

package bitstamp
import (
"log"
"time"
"github.com/thrasher-/gocryptotrader/common"
"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 *Bitstamp) Start() {
go b.Run()
}
func (b *Bitstamp) Run() {
if b.Verbose {
log.Printf("%s Websocket: %s.", b.GetName(), common.IsEnabled(b.Websocket))
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)
}
if b.Websocket {
go b.PusherClient()
}
for b.Enabled {
for _, x := range b.EnabledPairs {
currency := x
go func() {
ticker, err := b.GetTickerPrice(currency)
if err != nil {
log.Println(err)
return
}
log.Printf("Bitstamp %s: Last %f High %f Low %f Volume %f\n", currency, ticker.Last, ticker.High, ticker.Low, ticker.Volume)
stats.AddExchangeInfo(b.GetName(), currency[0:3], currency[3:], ticker.Last, ticker.Volume)
}()
}
time.Sleep(time.Second * b.RESTPollingDelay)
}
}
func (b *Bitstamp) GetTickerPrice(currency string) (ticker.TickerPrice, error) {
tickerNew, err := ticker.GetTicker(b.GetName(), currency[0:3], currency[3:])
if err == nil {
return tickerNew, nil
}
var tickerPrice ticker.TickerPrice
tick, err := b.GetTicker(currency, false)
if err != nil {
return tickerPrice, err
}
tickerPrice.Ask = tick.Ask
tickerPrice.Bid = tick.Bid
tickerPrice.FirstCurrency = currency[0:3]
tickerPrice.SecondCurrency = currency[3:]
tickerPrice.Low = tick.Low
tickerPrice.Last = tick.Last
tickerPrice.Volume = tick.Volume
tickerPrice.High = tick.High
ticker.ProcessTicker(b.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice)
return tickerPrice, nil
}
func (b *Bitstamp) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) {
ob, err := orderbook.GetOrderbook(b.GetName(), currency[0:3], currency[3:])
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.Amount, Price: data.Price})
}
for x, _ := range orderbookNew.Asks {
data := orderbookNew.Asks[x]
orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: data.Amount, Price: data.Price})
}
orderBook.FirstCurrency = currency[0:3]
orderBook.SecondCurrency = currency[3:]
orderbook.ProcessOrderbook(b.GetName(), orderBook.FirstCurrency, orderBook.SecondCurrency, orderBook)
return orderBook, nil
}
//GetExchangeAccountInfo : Retrieves balances for all enabled currencies for the Bitstamp exchange
func (e *Bitstamp) GetExchangeAccountInfo() (exchange.ExchangeAccountInfo, error) {
var response exchange.ExchangeAccountInfo
response.ExchangeName = e.GetName()
accountBalance, err := e.GetBalance()
if err != nil {
return response, err
}
response.Currencies = append(response.Currencies, exchange.ExchangeAccountCurrencyInfo{
CurrencyName: "BTC",
TotalValue: accountBalance.BTCAvailable,
Hold: accountBalance.BTCReserved,
})
response.Currencies = append(response.Currencies, exchange.ExchangeAccountCurrencyInfo{
CurrencyName: "XRP",
TotalValue: accountBalance.XRPAvailable,
Hold: accountBalance.XRPReserved,
})
response.Currencies = append(response.Currencies, exchange.ExchangeAccountCurrencyInfo{
CurrencyName: "USD",
TotalValue: accountBalance.USDAvailable,
Hold: accountBalance.USDReserved,
})
response.Currencies = append(response.Currencies, exchange.ExchangeAccountCurrencyInfo{
CurrencyName: "EUR",
TotalValue: accountBalance.EURAvailable,
Hold: accountBalance.EURReserved,
})
return response, nil
}