mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-24 07:26:47 +00:00
127 lines
3.9 KiB
Go
127 lines
3.9 KiB
Go
package btcmarkets
|
|
|
|
import (
|
|
"log"
|
|
|
|
"github.com/thrasher-/gocryptotrader/common"
|
|
|
|
"github.com/thrasher-/gocryptotrader/currency/pair"
|
|
"github.com/thrasher-/gocryptotrader/exchanges"
|
|
"github.com/thrasher-/gocryptotrader/exchanges/orderbook"
|
|
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
|
|
)
|
|
|
|
// Start starts the BTC Markets go routine
|
|
func (b *BTCMarkets) Start() {
|
|
go b.Run()
|
|
}
|
|
|
|
// Run implements the BTC Markets wrapper
|
|
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)
|
|
}
|
|
|
|
if !common.DataContains(b.EnabledPairs, "AUD") || !common.DataContains(b.EnabledPairs, "AUD") {
|
|
enabledPairs := []string{}
|
|
for x := range b.EnabledPairs {
|
|
enabledPairs = append(enabledPairs, b.EnabledPairs[x]+"AUD")
|
|
}
|
|
|
|
availablePairs := []string{}
|
|
for x := range b.AvailablePairs {
|
|
availablePairs = append(availablePairs, b.AvailablePairs[x]+"AUD")
|
|
}
|
|
|
|
log.Println("BTCMarkets: Upgrading available and enabled pairs")
|
|
|
|
err := b.UpdateEnabledCurrencies(enabledPairs, true)
|
|
if err != nil {
|
|
log.Printf("%s Failed to get config.\n", b.GetName())
|
|
return
|
|
}
|
|
|
|
err = b.UpdateAvailableCurrencies(availablePairs, true)
|
|
if err != nil {
|
|
log.Printf("%s Failed to get config.\n", b.GetName())
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
// UpdateTicker updates and returns the ticker for a currency pair
|
|
func (b *BTCMarkets) UpdateTicker(p pair.CurrencyPair, assetType string) (ticker.Price, error) {
|
|
var tickerPrice ticker.Price
|
|
tick, err := b.GetTicker(p.GetFirstCurrency().String())
|
|
if err != nil {
|
|
return tickerPrice, err
|
|
}
|
|
tickerPrice.Pair = p
|
|
tickerPrice.Ask = tick.BestAsk
|
|
tickerPrice.Bid = tick.BestBID
|
|
tickerPrice.Last = tick.LastPrice
|
|
ticker.ProcessTicker(b.GetName(), p, tickerPrice, assetType)
|
|
return ticker.GetTicker(b.Name, p, assetType)
|
|
}
|
|
|
|
// GetTickerPrice returns the ticker for a currency pair
|
|
func (b *BTCMarkets) GetTickerPrice(p pair.CurrencyPair, assetType string) (ticker.Price, error) {
|
|
tickerNew, err := ticker.GetTicker(b.GetName(), p, assetType)
|
|
if err != nil {
|
|
return b.UpdateTicker(p, assetType)
|
|
}
|
|
return tickerNew, nil
|
|
}
|
|
|
|
// GetOrderbookEx returns orderbook base on the currency pair
|
|
func (b *BTCMarkets) GetOrderbookEx(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
|
|
ob, err := orderbook.GetOrderbook(b.GetName(), p, assetType)
|
|
if err != nil {
|
|
return b.UpdateOrderbook(p, assetType)
|
|
}
|
|
return ob, nil
|
|
}
|
|
|
|
// UpdateOrderbook updates and returns the orderbook for a currency pair
|
|
func (b *BTCMarkets) UpdateOrderbook(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
|
|
var orderBook orderbook.Base
|
|
orderbookNew, err := b.GetOrderbook(p.GetFirstCurrency().String())
|
|
if err != nil {
|
|
return orderBook, err
|
|
}
|
|
|
|
for x := range orderbookNew.Bids {
|
|
data := orderbookNew.Bids[x]
|
|
orderBook.Bids = append(orderBook.Bids, orderbook.Item{Amount: data[1], Price: data[0]})
|
|
}
|
|
|
|
for x := range orderbookNew.Asks {
|
|
data := orderbookNew.Asks[x]
|
|
orderBook.Asks = append(orderBook.Asks, orderbook.Item{Amount: data[1], Price: data[0]})
|
|
}
|
|
|
|
orderbook.ProcessOrderbook(b.GetName(), p, orderBook, assetType)
|
|
return orderbook.GetOrderbook(b.Name, p, assetType)
|
|
}
|
|
|
|
// GetExchangeAccountInfo retrieves balances for all enabled currencies for the
|
|
// BTCMarkets exchange
|
|
func (b *BTCMarkets) GetExchangeAccountInfo() (exchange.AccountInfo, error) {
|
|
var response exchange.AccountInfo
|
|
response.ExchangeName = b.GetName()
|
|
accountBalance, err := b.GetAccountBalance()
|
|
if err != nil {
|
|
return response, err
|
|
}
|
|
for i := 0; i < len(accountBalance); i++ {
|
|
var exchangeCurrency exchange.AccountCurrencyInfo
|
|
exchangeCurrency.CurrencyName = accountBalance[i].Currency
|
|
exchangeCurrency.TotalValue = accountBalance[i].Balance
|
|
exchangeCurrency.Hold = accountBalance[i].PendingFunds
|
|
|
|
response.Currencies = append(response.Currencies, exchangeCurrency)
|
|
}
|
|
return response, nil
|
|
}
|