mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 23:16:45 +00:00
Add method for obtaining orderbook info
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"log"
|
||||
|
||||
"github.com/thrasher-/gocryptotrader/exchanges"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/orderbook"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
|
||||
)
|
||||
|
||||
@@ -39,3 +40,31 @@ func (a *Alphapoint) GetTickerPrice(currency string) ticker.TickerPrice {
|
||||
|
||||
return tickerPrice
|
||||
}
|
||||
|
||||
func (a *Alphapoint) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) {
|
||||
ob, err := orderbook.GetOrderbook(a.GetName(), currency[0:3], currency[3:])
|
||||
if err == nil {
|
||||
return ob, nil
|
||||
}
|
||||
|
||||
var orderBook orderbook.OrderbookBase
|
||||
orderbookNew, err := a.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.Quantity, Price: data.Price})
|
||||
}
|
||||
|
||||
for x, _ := range orderbookNew.Asks {
|
||||
data := orderbookNew.Asks[x]
|
||||
orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: data.Quantity, Price: data.Price})
|
||||
}
|
||||
|
||||
orderBook.FirstCurrency = currency[0:3]
|
||||
orderBook.SecondCurrency = currency[3:]
|
||||
orderbook.ProcessOrderbook(a.GetName(), orderBook.FirstCurrency, orderBook.SecondCurrency, orderBook)
|
||||
return orderBook, nil
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-/gocryptotrader/exchanges"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/orderbook"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/stats"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
|
||||
)
|
||||
@@ -60,6 +61,10 @@ func (a *ANX) GetTickerPrice(currency string) (ticker.TickerPrice, error) {
|
||||
return tickerPrice, nil
|
||||
}
|
||||
|
||||
func (e *ANX) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) {
|
||||
return orderbook.OrderbookBase{}, nil
|
||||
}
|
||||
|
||||
//GetExchangeAccountInfo : Retrieves balances for all enabled currencies for the ANX exchange
|
||||
func (e *ANX) GetExchangeAccountInfo() (exchange.ExchangeAccountInfo, error) {
|
||||
var response exchange.ExchangeAccountInfo
|
||||
|
||||
@@ -2,10 +2,12 @@ package bitfinex
|
||||
|
||||
import (
|
||||
"log"
|
||||
"strconv"
|
||||
"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"
|
||||
)
|
||||
@@ -74,6 +76,36 @@ func (b *Bitfinex) GetTickerPrice(currency string) (ticker.TickerPrice, error) {
|
||||
return tickerPrice, nil
|
||||
}
|
||||
|
||||
func (b *Bitfinex) 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, nil)
|
||||
if err != nil {
|
||||
return orderBook, err
|
||||
}
|
||||
|
||||
for x, _ := range orderbookNew.Asks {
|
||||
price, _ := strconv.ParseFloat(orderbookNew.Asks[x].Price, 64)
|
||||
amount, _ := strconv.ParseFloat(orderbookNew.Asks[x].Amount, 64)
|
||||
orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Price: price, Amount: amount})
|
||||
}
|
||||
|
||||
for x, _ := range orderbookNew.Bids {
|
||||
price, _ := strconv.ParseFloat(orderbookNew.Bids[x].Price, 64)
|
||||
amount, _ := strconv.ParseFloat(orderbookNew.Bids[x].Amount, 64)
|
||||
orderBook.Bids = append(orderBook.Bids, orderbook.OrderbookItem{Price: price, Amount: amount})
|
||||
}
|
||||
|
||||
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 Bitfinex exchange
|
||||
func (e *Bitfinex) GetExchangeAccountInfo() (exchange.ExchangeAccountInfo, error) {
|
||||
var response exchange.ExchangeAccountInfo
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"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"
|
||||
)
|
||||
@@ -66,6 +67,34 @@ func (b *Bitstamp) GetTickerPrice(currency string) (ticker.TickerPrice, error) {
|
||||
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
|
||||
|
||||
@@ -123,14 +123,15 @@ func (b *BTCC) GetTradeHistory(symbol string, limit, sinceTid int64, time time.T
|
||||
return true
|
||||
}
|
||||
|
||||
func (b *BTCC) GetOrderBook(symbol string, limit int) bool {
|
||||
func (b *BTCC) GetOrderBook(symbol string, limit int) (BTCCOrderbook, error) {
|
||||
result := BTCCOrderbook{}
|
||||
req := fmt.Sprintf("%sdata/orderbook?market=%s&limit=%d", BTCC_API_URL, symbol, limit)
|
||||
err := common.SendHTTPGetRequest(req, true, nil)
|
||||
err := common.SendHTTPGetRequest(req, true, &result)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return false
|
||||
return BTCCOrderbook{}, err
|
||||
}
|
||||
return true
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (b *BTCC) GetAccountInfo(infoType string) {
|
||||
|
||||
@@ -29,6 +29,12 @@ type BTCCProfile struct {
|
||||
APIKeyPermission int64 `json:"api_key_permission"`
|
||||
}
|
||||
|
||||
type BTCCOrderbook struct {
|
||||
Bids [][]float64 `json:"bids"`
|
||||
Asks [][]float64 `json:"asks"`
|
||||
Date int64 `json:"date"`
|
||||
}
|
||||
|
||||
type BTCCCurrencyGeneric struct {
|
||||
Currency string
|
||||
Symbol string
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"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"
|
||||
)
|
||||
@@ -62,6 +63,34 @@ func (b *BTCC) GetTickerPrice(currency string) (ticker.TickerPrice, error) {
|
||||
return tickerPrice, nil
|
||||
}
|
||||
|
||||
func (b *BTCC) 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, 100)
|
||||
if err != nil {
|
||||
return orderBook, err
|
||||
}
|
||||
|
||||
for x, _ := range orderbookNew.Bids {
|
||||
data := orderbookNew.Bids[x]
|
||||
orderBook.Bids = append(ob.Bids, orderbook.OrderbookItem{Price: data[0], Amount: data[1]})
|
||||
}
|
||||
|
||||
for x, _ := range orderbookNew.Asks {
|
||||
data := orderbookNew.Asks[x]
|
||||
orderBook.Asks = append(ob.Asks, orderbook.OrderbookItem{Price: data[0], Amount: data[1]})
|
||||
}
|
||||
|
||||
orderBook.FirstCurrency = currency[0:3]
|
||||
orderBook.SecondCurrency = currency[3:]
|
||||
orderbook.ProcessOrderbook(b.GetName(), orderBook.FirstCurrency, orderBook.SecondCurrency, orderBook)
|
||||
return orderBook, nil
|
||||
}
|
||||
|
||||
//TODO: Retrieve BTCC info
|
||||
//GetExchangeAccountInfo : Retrieves balances for all enabled currencies for the Kraken exchange
|
||||
func (e *BTCC) GetExchangeAccountInfo() (exchange.ExchangeAccountInfo, error) {
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"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"
|
||||
)
|
||||
@@ -55,8 +56,8 @@ func (b *BTCE) GetTickerPrice(currency string) (ticker.TickerPrice, error) {
|
||||
}
|
||||
tickerPrice.Ask = tick.Buy
|
||||
tickerPrice.Bid = tick.Sell
|
||||
tickerPrice.FirstCurrency = currency[0:3]
|
||||
tickerPrice.SecondCurrency = currency[3:]
|
||||
tickerPrice.FirstCurrency = common.StringToUpper(currency[0:3])
|
||||
tickerPrice.SecondCurrency = common.StringToUpper(currency[4:])
|
||||
tickerPrice.Low = tick.Low
|
||||
tickerPrice.Last = tick.Last
|
||||
tickerPrice.Volume = tick.Vol_cur
|
||||
@@ -65,6 +66,34 @@ func (b *BTCE) GetTickerPrice(currency string) (ticker.TickerPrice, error) {
|
||||
return tickerPrice, nil
|
||||
}
|
||||
|
||||
func (b *BTCE) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) {
|
||||
ob, err := orderbook.GetOrderbook(b.GetName(), common.StringToUpper(currency[0:3]), common.StringToUpper(currency[4:]))
|
||||
if err == nil {
|
||||
return ob, nil
|
||||
}
|
||||
|
||||
var orderBook orderbook.OrderbookBase
|
||||
orderbookNew, err := b.GetDepth(currency)
|
||||
if err != nil {
|
||||
return orderBook, err
|
||||
}
|
||||
|
||||
for x, _ := range orderbookNew.Bids {
|
||||
data := orderbookNew.Bids[x]
|
||||
orderBook.Bids = append(ob.Bids, orderbook.OrderbookItem{Price: data[0], Amount: data[1]})
|
||||
}
|
||||
|
||||
for x, _ := range orderbookNew.Asks {
|
||||
data := orderbookNew.Asks[x]
|
||||
orderBook.Asks = append(ob.Asks, orderbook.OrderbookItem{Price: data[0], Amount: data[1]})
|
||||
}
|
||||
|
||||
orderBook.FirstCurrency = common.StringToUpper(currency[0:3])
|
||||
orderBook.SecondCurrency = common.StringToUpper(currency[4:])
|
||||
orderbook.ProcessOrderbook(b.GetName(), orderBook.FirstCurrency, orderBook.SecondCurrency, orderBook)
|
||||
return orderBook, nil
|
||||
}
|
||||
|
||||
//GetExchangeAccountInfo : Retrieves balances for all enabled currencies for the BTCE exchange
|
||||
func (e *BTCE) GetExchangeAccountInfo() (exchange.ExchangeAccountInfo, error) {
|
||||
var response exchange.ExchangeAccountInfo
|
||||
|
||||
@@ -45,7 +45,7 @@ func (b *BTCMarkets) Setup(exch config.ExchangeConfig) {
|
||||
if !exch.Enabled {
|
||||
b.SetEnabled(false)
|
||||
} else {
|
||||
b.Enabled = false
|
||||
b.Enabled = true
|
||||
b.AuthenticatedAPISupport = exch.AuthenticatedAPISupport
|
||||
b.SetAPIKeys(exch.APIKey, exch.APISecret, "", true)
|
||||
b.RESTPollingDelay = exch.RESTPollingDelay
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"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"
|
||||
)
|
||||
@@ -41,7 +42,7 @@ func (b *BTCMarkets) Run() {
|
||||
}
|
||||
|
||||
func (b *BTCMarkets) GetTickerPrice(currency string) (ticker.TickerPrice, error) {
|
||||
tickerNew, err := ticker.GetTicker(b.GetName(), currency[0:3], currency[3:])
|
||||
tickerNew, err := ticker.GetTicker(b.GetName(), currency[0:3], "AUD")
|
||||
if err == nil {
|
||||
return tickerNew, nil
|
||||
}
|
||||
@@ -54,12 +55,40 @@ func (b *BTCMarkets) GetTickerPrice(currency string) (ticker.TickerPrice, error)
|
||||
tickerPrice.Ask = tick.BestAsk
|
||||
tickerPrice.Bid = tick.BestBID
|
||||
tickerPrice.FirstCurrency = currency[0:3]
|
||||
tickerPrice.SecondCurrency = currency[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
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"github.com/thrasher-/gocryptotrader/common"
|
||||
"github.com/thrasher-/gocryptotrader/config"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/orderbook"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
|
||||
)
|
||||
|
||||
@@ -51,6 +52,7 @@ type IBotExchange interface {
|
||||
GetName() string
|
||||
IsEnabled() bool
|
||||
GetTickerPrice(currency string) (ticker.TickerPrice, error)
|
||||
GetOrderbookEx(currency string) (orderbook.OrderbookBase, error)
|
||||
GetEnabledCurrencies() []string
|
||||
GetExchangeAccountInfo() (ExchangeAccountInfo, error)
|
||||
}
|
||||
|
||||
@@ -111,11 +111,7 @@ func (g *GDAX) GetOrderbook(symbol string, level int) (interface{}, error) {
|
||||
continue
|
||||
}
|
||||
|
||||
order := make([]GDAXOrderL3, 1)
|
||||
order[0].Price = price
|
||||
order[0].Amount = amount
|
||||
order[0].OrderID = x[2].(string)
|
||||
ob.Asks = append(ob.Asks, order)
|
||||
ob.Asks = append(ob.Asks, GDAXOrderL3{Price: price, Amount: amount, OrderID: x[2].(string)})
|
||||
}
|
||||
for _, x := range orderbook.Bids {
|
||||
price, err := strconv.ParseFloat((x[0].(string)), 64)
|
||||
@@ -127,11 +123,7 @@ func (g *GDAX) GetOrderbook(symbol string, level int) (interface{}, error) {
|
||||
continue
|
||||
}
|
||||
|
||||
order := make([]GDAXOrderL3, 1)
|
||||
order[0].Price = price
|
||||
order[0].Amount = amount
|
||||
order[0].OrderID = x[2].(string)
|
||||
ob.Bids = append(ob.Bids, order)
|
||||
ob.Bids = append(ob.Bids, GDAXOrderL3{Price: price, Amount: amount, OrderID: x[2].(string)})
|
||||
}
|
||||
return ob, nil
|
||||
} else {
|
||||
@@ -147,11 +139,7 @@ func (g *GDAX) GetOrderbook(symbol string, level int) (interface{}, error) {
|
||||
continue
|
||||
}
|
||||
|
||||
order := make([]GDAXOrderL1L2, 1)
|
||||
order[0].Price = price
|
||||
order[0].Amount = amount
|
||||
order[0].NumOrders = x[2].(float64)
|
||||
ob.Asks = append(ob.Asks, order)
|
||||
ob.Asks = append(ob.Asks, GDAXOrderL1L2{Price: price, Amount: amount, NumOrders: x[2].(float64)})
|
||||
}
|
||||
for _, x := range orderbook.Bids {
|
||||
price, err := strconv.ParseFloat((x[0].(string)), 64)
|
||||
@@ -163,11 +151,7 @@ func (g *GDAX) GetOrderbook(symbol string, level int) (interface{}, error) {
|
||||
continue
|
||||
}
|
||||
|
||||
order := make([]GDAXOrderL1L2, 1)
|
||||
order[0].Price = price
|
||||
order[0].Amount = amount
|
||||
order[0].NumOrders = x[2].(float64)
|
||||
ob.Bids = append(ob.Bids, order)
|
||||
ob.Bids = append(ob.Bids, GDAXOrderL1L2{Price: price, Amount: amount, NumOrders: x[2].(float64)})
|
||||
}
|
||||
return ob, nil
|
||||
}
|
||||
|
||||
@@ -30,15 +30,15 @@ type GDAXOrderL3 struct {
|
||||
}
|
||||
|
||||
type GDAXOrderbookL1L2 struct {
|
||||
Sequence int64 `json:"sequence"`
|
||||
Bids [][]GDAXOrderL1L2 `json:"asks"`
|
||||
Asks [][]GDAXOrderL1L2 `json:"asks"`
|
||||
Sequence int64 `json:"sequence"`
|
||||
Bids []GDAXOrderL1L2 `json:"asks"`
|
||||
Asks []GDAXOrderL1L2 `json:"asks"`
|
||||
}
|
||||
|
||||
type GDAXOrderbookL3 struct {
|
||||
Sequence int64 `json:"sequence"`
|
||||
Bids [][]GDAXOrderL3 `json:"asks"`
|
||||
Asks [][]GDAXOrderL3 `json:"asks"`
|
||||
Sequence int64 `json:"sequence"`
|
||||
Bids []GDAXOrderL3 `json:"asks"`
|
||||
Asks []GDAXOrderL3 `json:"asks"`
|
||||
}
|
||||
|
||||
type GDAXOrderbookResponse struct {
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"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"
|
||||
)
|
||||
@@ -79,7 +80,7 @@ func (e *GDAX) GetExchangeAccountInfo() (exchange.ExchangeAccountInfo, error) {
|
||||
}
|
||||
|
||||
func (g *GDAX) GetTickerPrice(currency string) (ticker.TickerPrice, error) {
|
||||
tickerNew, err := ticker.GetTicker(g.GetName(), currency[0:3], currency[3:])
|
||||
tickerNew, err := ticker.GetTicker(g.GetName(), currency[0:3], currency[4:])
|
||||
if err == nil {
|
||||
return tickerNew, nil
|
||||
}
|
||||
@@ -105,3 +106,30 @@ func (g *GDAX) GetTickerPrice(currency string) (ticker.TickerPrice, error) {
|
||||
ticker.ProcessTicker(g.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice)
|
||||
return tickerPrice, nil
|
||||
}
|
||||
|
||||
func (g *GDAX) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) {
|
||||
ob, err := orderbook.GetOrderbook(g.GetName(), currency[0:3], currency[4:])
|
||||
if err == nil {
|
||||
return ob, nil
|
||||
}
|
||||
|
||||
var orderBook orderbook.OrderbookBase
|
||||
orderbookNew, err := g.GetOrderbook(currency, 2)
|
||||
if err != nil {
|
||||
return orderBook, err
|
||||
}
|
||||
|
||||
obNew := orderbookNew.(GDAXOrderbookL1L2)
|
||||
|
||||
for x, _ := range obNew.Bids {
|
||||
orderBook.Bids = append(orderBook.Bids, orderbook.OrderbookItem{Amount: obNew.Bids[x].Amount, Price: obNew.Bids[x].Price})
|
||||
}
|
||||
|
||||
for x, _ := range obNew.Asks {
|
||||
orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: obNew.Bids[x].Amount, Price: obNew.Bids[x].Price})
|
||||
}
|
||||
orderBook.FirstCurrency = currency[0:3]
|
||||
orderBook.SecondCurrency = currency[4:]
|
||||
orderbook.ProcessOrderbook(g.GetName(), orderBook.FirstCurrency, orderBook.SecondCurrency, orderBook)
|
||||
return orderBook, nil
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package gemini
|
||||
|
||||
type GeminiOrderbookEntry struct {
|
||||
Price float64 `json:"price,string"`
|
||||
Quantity float64 `json:"quantity,string"`
|
||||
Price float64 `json:"price,string"`
|
||||
Amount float64 `json:"amount,string"`
|
||||
}
|
||||
|
||||
type GeminiOrderbook struct {
|
||||
|
||||
@@ -2,9 +2,11 @@ package gemini
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-/gocryptotrader/exchanges"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/orderbook"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/stats"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
|
||||
)
|
||||
@@ -85,3 +87,29 @@ func (g *Gemini) GetTickerPrice(currency string) (ticker.TickerPrice, error) {
|
||||
ticker.ProcessTicker(g.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice)
|
||||
return tickerPrice, nil
|
||||
}
|
||||
|
||||
func (g *Gemini) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) {
|
||||
ob, err := orderbook.GetOrderbook(g.GetName(), currency[0:3], currency[3:])
|
||||
if err == nil {
|
||||
return ob, nil
|
||||
}
|
||||
|
||||
var orderBook orderbook.OrderbookBase
|
||||
orderbookNew, err := g.GetOrderbook(currency, url.Values{})
|
||||
if err != nil {
|
||||
return orderBook, err
|
||||
}
|
||||
|
||||
for x, _ := range orderbookNew.Bids {
|
||||
orderBook.Bids = append(orderBook.Bids, orderbook.OrderbookItem{Amount: orderbookNew.Bids[x].Amount, Price: orderbookNew.Bids[x].Price})
|
||||
}
|
||||
|
||||
for x, _ := range orderbookNew.Asks {
|
||||
orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: orderbookNew.Asks[x].Amount, Price: orderbookNew.Asks[x].Price})
|
||||
}
|
||||
|
||||
orderBook.FirstCurrency = currency[0:3]
|
||||
orderBook.SecondCurrency = currency[3:]
|
||||
orderbook.ProcessOrderbook(g.GetName(), orderBook.FirstCurrency, orderBook.SecondCurrency, orderBook)
|
||||
return orderBook, nil
|
||||
}
|
||||
|
||||
@@ -62,14 +62,14 @@ func (h *HUOBI) GetTicker(symbol string) (HuobiTicker, error) {
|
||||
return resp.Ticker, nil
|
||||
}
|
||||
|
||||
func (h *HUOBI) GetOrderBook(symbol string) bool {
|
||||
func (h *HUOBI) GetOrderBook(symbol string) (HuobiOrderbook, error) {
|
||||
path := fmt.Sprintf("http://api.huobi.com/staticmarket/depth_%s_json.js", symbol)
|
||||
err := common.SendHTTPGetRequest(path, true, nil)
|
||||
resp := HuobiOrderbook{}
|
||||
err := common.SendHTTPGetRequest(path, true, &resp)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return false
|
||||
return resp, err
|
||||
}
|
||||
return true
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (h *HUOBI) GetAccountInfo() {
|
||||
|
||||
@@ -13,3 +13,11 @@ type HuobiTickerResponse struct {
|
||||
Time string
|
||||
Ticker HuobiTicker
|
||||
}
|
||||
|
||||
type HuobiOrderbook struct {
|
||||
ID float64
|
||||
TS float64
|
||||
Bids [][]float64 `json:"bids"`
|
||||
Asks [][]float64 `json:"asks"`
|
||||
Symbol string `json:"string"`
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/thrasher-/gocryptotrader/common"
|
||||
"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"
|
||||
)
|
||||
@@ -70,6 +71,33 @@ func (h *HUOBI) GetTickerPrice(currency string) (ticker.TickerPrice, error) {
|
||||
return tickerPrice, nil
|
||||
}
|
||||
|
||||
func (h *HUOBI) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) {
|
||||
ob, err := orderbook.GetOrderbook(h.GetName(), common.StringToUpper(currency[0:3]), common.StringToUpper(currency[3:]))
|
||||
if err == nil {
|
||||
return ob, nil
|
||||
}
|
||||
|
||||
var orderBook orderbook.OrderbookBase
|
||||
orderbookNew, err := h.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 = common.StringToUpper(currency[0:3])
|
||||
orderBook.SecondCurrency = common.StringToUpper(currency[3:])
|
||||
orderbook.ProcessOrderbook(h.GetName(), orderBook.FirstCurrency, orderBook.SecondCurrency, orderBook)
|
||||
return orderBook, nil
|
||||
}
|
||||
|
||||
//TODO: retrieve HUOBI balance info
|
||||
//GetExchangeAccountInfo : Retrieves balances for all enabled currencies for the HUOBI exchange
|
||||
func (e *HUOBI) GetExchangeAccountInfo() (exchange.ExchangeAccountInfo, error) {
|
||||
|
||||
@@ -20,15 +20,7 @@ type ItBitTicker struct {
|
||||
ServertimeUTC string
|
||||
}
|
||||
|
||||
type ItbitOrderbookEntry struct {
|
||||
Quantitiy float64 `json:"quantity,string"`
|
||||
Price float64 `json:"price,string"`
|
||||
}
|
||||
|
||||
type ItBitOrderbookResponse struct {
|
||||
ServerTimeUTC string `json:"serverTimeUTC"`
|
||||
LastUpdatedTimeUTC string `json:"lastUpdatedTimeUTC"`
|
||||
Ticker string `json:"ticker"`
|
||||
Bids []ItbitOrderbookEntry `json:"bids"`
|
||||
Asks []ItbitOrderbookEntry `json:"asks"`
|
||||
Bids [][]string `json:"bids"`
|
||||
Asks [][]string `json:"asks"`
|
||||
}
|
||||
|
||||
@@ -2,9 +2,11 @@ package itbit
|
||||
|
||||
import (
|
||||
"log"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-/gocryptotrader/exchanges"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/orderbook"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/stats"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
|
||||
)
|
||||
@@ -59,6 +61,49 @@ func (i *ItBit) GetTickerPrice(currency string) (ticker.TickerPrice, error) {
|
||||
return tickerPrice, nil
|
||||
}
|
||||
|
||||
func (i *ItBit) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) {
|
||||
ob, err := orderbook.GetOrderbook(i.GetName(), currency[0:3], currency[3:])
|
||||
if err == nil {
|
||||
return ob, nil
|
||||
}
|
||||
|
||||
var orderBook orderbook.OrderbookBase
|
||||
orderbookNew, err := i.GetOrderbook(currency)
|
||||
if err != nil {
|
||||
return orderBook, err
|
||||
}
|
||||
|
||||
for x, _ := range orderbookNew.Bids {
|
||||
data := orderbookNew.Bids[x]
|
||||
price, err := strconv.ParseFloat(data[0], 64)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
amount, err := strconv.ParseFloat(data[1], 64)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
orderBook.Bids = append(orderBook.Bids, orderbook.OrderbookItem{Amount: amount, Price: price})
|
||||
}
|
||||
|
||||
for x, _ := range orderbookNew.Asks {
|
||||
data := orderbookNew.Asks[x]
|
||||
price, err := strconv.ParseFloat(data[0], 64)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
amount, err := strconv.ParseFloat(data[1], 64)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: amount, Price: price})
|
||||
}
|
||||
orderBook.FirstCurrency = currency[0:3]
|
||||
orderBook.SecondCurrency = currency[3:]
|
||||
orderbook.ProcessOrderbook(i.GetName(), orderBook.FirstCurrency, orderBook.SecondCurrency, orderBook)
|
||||
return orderBook, nil
|
||||
}
|
||||
|
||||
//TODO Get current holdings from ItBit
|
||||
//GetExchangeAccountInfo : Retrieves balances for all enabled currencies for the ItBit exchange
|
||||
func (e *ItBit) GetExchangeAccountInfo() (exchange.ExchangeAccountInfo, error) {
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"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"
|
||||
)
|
||||
@@ -64,6 +65,10 @@ func (k *Kraken) GetTickerPrice(currency string) (ticker.TickerPrice, error) {
|
||||
return tickerPrice, nil
|
||||
}
|
||||
|
||||
func (k *Kraken) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) {
|
||||
return orderbook.OrderbookBase{}, nil
|
||||
}
|
||||
|
||||
//TODO: Retrieve Kraken info
|
||||
//GetExchangeAccountInfo : Retrieves balances for all enabled currencies for the Kraken exchange
|
||||
func (e *Kraken) GetExchangeAccountInfo() (exchange.ExchangeAccountInfo, error) {
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"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"
|
||||
)
|
||||
@@ -63,6 +64,32 @@ func (l *LakeBTC) GetTickerPrice(currency string) (ticker.TickerPrice, error) {
|
||||
return tickerPrice, nil
|
||||
}
|
||||
|
||||
func (l *LakeBTC) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) {
|
||||
ob, err := orderbook.GetOrderbook(l.GetName(), currency[0:3], currency[3:])
|
||||
if err == nil {
|
||||
return ob, nil
|
||||
}
|
||||
|
||||
var orderBook orderbook.OrderbookBase
|
||||
orderbookNew, err := l.GetOrderBook(currency)
|
||||
if err != nil {
|
||||
return orderBook, err
|
||||
}
|
||||
|
||||
for x, _ := range orderbookNew.Bids {
|
||||
orderBook.Bids = append(orderBook.Bids, orderbook.OrderbookItem{Amount: orderbookNew.Bids[x].Amount, Price: orderbookNew.Bids[x].Price})
|
||||
}
|
||||
|
||||
for x, _ := range orderbookNew.Asks {
|
||||
orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: orderbookNew.Asks[x].Amount, Price: orderbookNew.Asks[x].Price})
|
||||
}
|
||||
|
||||
orderBook.FirstCurrency = currency[0:3]
|
||||
orderBook.SecondCurrency = currency[3:]
|
||||
orderbook.ProcessOrderbook(l.GetName(), orderBook.FirstCurrency, orderBook.SecondCurrency, orderBook)
|
||||
return orderBook, nil
|
||||
}
|
||||
|
||||
func (l *LakeBTC) GetExchangeAccountInfo() (exchange.ExchangeAccountInfo, error) {
|
||||
var response exchange.ExchangeAccountInfo
|
||||
response.ExchangeName = l.GetName()
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"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"
|
||||
)
|
||||
@@ -79,6 +80,34 @@ func (l *Liqui) GetTickerPrice(currency string) (ticker.TickerPrice, error) {
|
||||
return tickerPrice, nil
|
||||
}
|
||||
|
||||
func (l *Liqui) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) {
|
||||
currencies := common.SplitStrings(currency, "_")
|
||||
ob, err := orderbook.GetOrderbook(l.GetName(), currencies[0], currencies[1])
|
||||
if err == nil {
|
||||
return ob, nil
|
||||
}
|
||||
|
||||
var orderBook orderbook.OrderbookBase
|
||||
orderbookNew, err := l.GetDepth(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 = currencies[0]
|
||||
orderBook.SecondCurrency = currencies[1]
|
||||
orderbook.ProcessOrderbook(l.GetName(), orderBook.FirstCurrency, orderBook.SecondCurrency, orderBook)
|
||||
return orderBook, nil
|
||||
}
|
||||
|
||||
//GetExchangeAccountInfo : Retrieves balances for all enabled currencies for the Liqui exchange
|
||||
func (e *Liqui) GetExchangeAccountInfo() (exchange.ExchangeAccountInfo, error) {
|
||||
var response exchange.ExchangeAccountInfo
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-/gocryptotrader/exchanges"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/orderbook"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/stats"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
|
||||
)
|
||||
@@ -58,6 +59,10 @@ func (l *LocalBitcoins) GetTickerPrice(currency string) (ticker.TickerPrice, err
|
||||
return tickerPrice, nil
|
||||
}
|
||||
|
||||
func (l *LocalBitcoins) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) {
|
||||
return orderbook.OrderbookBase{}, nil
|
||||
}
|
||||
|
||||
//GetExchangeAccountInfo : Retrieves balances for all enabled currencies for the LocalBitcoins exchange
|
||||
func (e *LocalBitcoins) GetExchangeAccountInfo() (exchange.ExchangeAccountInfo, error) {
|
||||
var response exchange.ExchangeAccountInfo
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/thrasher-/gocryptotrader/common"
|
||||
"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"
|
||||
)
|
||||
@@ -72,7 +73,7 @@ func (o *OKCoin) Run() {
|
||||
}
|
||||
|
||||
func (o *OKCoin) GetTickerPrice(currency string) (ticker.TickerPrice, error) {
|
||||
tickerNew, err := ticker.GetTicker(o.GetName(), currency[0:3], currency[3:])
|
||||
tickerNew, err := ticker.GetTicker(o.GetName(), common.StringToUpper(currency[0:3]), common.StringToUpper(currency[4:]))
|
||||
if err == nil {
|
||||
return tickerNew, nil
|
||||
}
|
||||
@@ -94,6 +95,33 @@ func (o *OKCoin) GetTickerPrice(currency string) (ticker.TickerPrice, error) {
|
||||
return tickerPrice, nil
|
||||
}
|
||||
|
||||
func (o *OKCoin) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) {
|
||||
ob, err := orderbook.GetOrderbook(o.GetName(), common.StringToUpper(currency[0:3]), common.StringToUpper(currency[4:]))
|
||||
if err == nil {
|
||||
return ob, nil
|
||||
}
|
||||
|
||||
var orderBook orderbook.OrderbookBase
|
||||
orderbookNew, err := o.GetOrderBook(currency, 200, false)
|
||||
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 = common.StringToUpper(currency[0:3])
|
||||
orderBook.SecondCurrency = common.StringToUpper(currency[4:])
|
||||
orderbook.ProcessOrderbook(o.GetName(), orderBook.FirstCurrency, orderBook.SecondCurrency, orderBook)
|
||||
return orderBook, nil
|
||||
}
|
||||
|
||||
func (e *OKCoin) GetExchangeAccountInfo() (exchange.ExchangeAccountInfo, error) {
|
||||
var response exchange.ExchangeAccountInfo
|
||||
response.ExchangeName = e.GetName()
|
||||
|
||||
146
exchanges/orderbook/orderbook.go
Normal file
146
exchanges/orderbook/orderbook.go
Normal file
@@ -0,0 +1,146 @@
|
||||
package orderbook
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrOrderbookForExchangeNotFound = "Ticker for exchange does not exist."
|
||||
ErrPrimaryCurrencyNotFound = "Error primary currency for orderbook not found."
|
||||
ErrSecondaryCurrencyNotFound = "Error secondary currency for orderbook not found."
|
||||
|
||||
Orderbooks []Orderbook
|
||||
)
|
||||
|
||||
type OrderbookItem struct {
|
||||
Amount float64
|
||||
Price float64
|
||||
}
|
||||
|
||||
type OrderbookBase struct {
|
||||
FirstCurrency string `json:"first_currency"`
|
||||
SecondCurrency string `json:"second_currency"`
|
||||
CurrencyPair string `json:"currency_pair"`
|
||||
Bids []OrderbookItem `json:"bids"`
|
||||
Asks []OrderbookItem `json:"asks"`
|
||||
LastUpdated time.Time `json:"last_updated"`
|
||||
}
|
||||
|
||||
type Orderbook struct {
|
||||
Orderbook map[string]map[string]OrderbookBase
|
||||
ExchangeName string
|
||||
}
|
||||
|
||||
func (o *OrderbookBase) CalculateTotalBids() (float64, float64) {
|
||||
amountCollated := float64(0)
|
||||
total := float64(0)
|
||||
for _, x := range o.Bids {
|
||||
amountCollated += x.Amount
|
||||
total += x.Amount * x.Price
|
||||
}
|
||||
return amountCollated, total
|
||||
}
|
||||
|
||||
func (o *OrderbookBase) CalculateTotalAsks() (float64, float64) {
|
||||
amountCollated := float64(0)
|
||||
total := float64(0)
|
||||
for _, x := range o.Asks {
|
||||
amountCollated += x.Amount
|
||||
total += x.Amount * x.Price
|
||||
}
|
||||
return amountCollated, total
|
||||
}
|
||||
|
||||
func (o *OrderbookBase) Update(Bids, Asks []OrderbookItem) {
|
||||
o.Bids = Bids
|
||||
o.Asks = Asks
|
||||
o.LastUpdated = time.Now()
|
||||
}
|
||||
|
||||
func GetOrderbook(exchange, firstCurrency, secondCurrency string) (OrderbookBase, error) {
|
||||
orderbook, err := GetOrderbookByExchange(exchange)
|
||||
if err != nil {
|
||||
return OrderbookBase{}, err
|
||||
}
|
||||
|
||||
if !FirstCurrencyExists(exchange, firstCurrency) {
|
||||
return OrderbookBase{}, errors.New(ErrPrimaryCurrencyNotFound)
|
||||
}
|
||||
|
||||
if !SecondCurrencyExists(exchange, firstCurrency, secondCurrency) {
|
||||
return OrderbookBase{}, errors.New(ErrSecondaryCurrencyNotFound)
|
||||
}
|
||||
|
||||
return orderbook.Orderbook[firstCurrency][secondCurrency], nil
|
||||
}
|
||||
|
||||
func GetOrderbookByExchange(exchange string) (*Orderbook, error) {
|
||||
for _, y := range Orderbooks {
|
||||
if y.ExchangeName == exchange {
|
||||
return &y, nil
|
||||
}
|
||||
}
|
||||
return nil, errors.New(ErrOrderbookForExchangeNotFound)
|
||||
}
|
||||
|
||||
func FirstCurrencyExists(exchange, currency string) bool {
|
||||
for _, y := range Orderbooks {
|
||||
if y.ExchangeName == exchange {
|
||||
if _, ok := y.Orderbook[currency]; ok {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func SecondCurrencyExists(exchange, primary, secondary string) bool {
|
||||
for _, y := range Orderbooks {
|
||||
if y.ExchangeName == exchange {
|
||||
if _, ok := y.Orderbook[primary]; ok {
|
||||
if _, ok := y.Orderbook[primary][secondary]; ok {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func CreateNewOrderbook(exchangeName string, firstCurrency, secondCurrency string, orderbookNew OrderbookBase) Orderbook {
|
||||
orderbook := Orderbook{}
|
||||
orderbook.ExchangeName = exchangeName
|
||||
orderbook.Orderbook = make(map[string]map[string]OrderbookBase)
|
||||
sMap := make(map[string]OrderbookBase)
|
||||
sMap[secondCurrency] = orderbookNew
|
||||
orderbook.Orderbook[firstCurrency] = sMap
|
||||
return orderbook
|
||||
}
|
||||
|
||||
func ProcessOrderbook(exchangeName string, firstCurrency, secondCurrency string, orderbookNew OrderbookBase) {
|
||||
orderbookNew.CurrencyPair = orderbookNew.FirstCurrency + orderbookNew.SecondCurrency
|
||||
|
||||
if len(Orderbooks) == 0 {
|
||||
CreateNewOrderbook(exchangeName, firstCurrency, secondCurrency, orderbookNew)
|
||||
return
|
||||
} else {
|
||||
orderbook, err := GetOrderbookByExchange(exchangeName)
|
||||
if err != nil {
|
||||
CreateNewOrderbook(exchangeName, firstCurrency, secondCurrency, orderbookNew)
|
||||
return
|
||||
}
|
||||
|
||||
if FirstCurrencyExists(exchangeName, firstCurrency) {
|
||||
if !SecondCurrencyExists(exchangeName, firstCurrency, secondCurrency) {
|
||||
second := orderbook.Orderbook[firstCurrency]
|
||||
second[secondCurrency] = orderbookNew
|
||||
orderbook.Orderbook[firstCurrency] = second
|
||||
return
|
||||
}
|
||||
}
|
||||
sMap := make(map[string]OrderbookBase)
|
||||
sMap[secondCurrency] = orderbookNew
|
||||
orderbook.Orderbook[firstCurrency] = sMap
|
||||
}
|
||||
}
|
||||
@@ -104,12 +104,7 @@ func (p *Poloniex) GetVolume() (interface{}, error) {
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
//TO-DO: add support for individual pair depth fetching
|
||||
func (p *Poloniex) GetOrderbook(currencyPair string, depth int) (map[string]PoloniexOrderbook, error) {
|
||||
type Response struct {
|
||||
Data map[string]PoloniexOrderbook
|
||||
}
|
||||
|
||||
func (p *Poloniex) GetOrderbook(currencyPair string, depth int) (PoloniexOrderbook, error) {
|
||||
vals := url.Values{}
|
||||
vals.Set("currencyPair", currencyPair)
|
||||
|
||||
@@ -117,14 +112,29 @@ func (p *Poloniex) GetOrderbook(currencyPair string, depth int) (map[string]Polo
|
||||
vals.Set("depth", strconv.Itoa(depth))
|
||||
}
|
||||
|
||||
resp := Response{}
|
||||
resp := PoloniexOrderbookResponse{}
|
||||
path := fmt.Sprintf("%s/public?command=returnOrderBook&%s", POLONIEX_API_URL, vals.Encode())
|
||||
err := common.SendHTTPGetRequest(path, true, &resp.Data)
|
||||
err := common.SendHTTPGetRequest(path, true, &resp)
|
||||
|
||||
if err != nil {
|
||||
return resp.Data, err
|
||||
return PoloniexOrderbook{}, err
|
||||
}
|
||||
return resp.Data, nil
|
||||
|
||||
ob := PoloniexOrderbook{}
|
||||
for x, _ := range resp.Asks {
|
||||
data := resp.Asks[x]
|
||||
price, _ := strconv.ParseFloat(data[0].(string), 64)
|
||||
amount := data[1].(float64)
|
||||
ob.Asks = append(ob.Asks, PoloniexOrderbookItem{Price: price, Amount: amount})
|
||||
}
|
||||
|
||||
for x, _ := range resp.Bids {
|
||||
data := resp.Asks[x]
|
||||
price, _ := strconv.ParseFloat(data[0].(string), 64)
|
||||
amount := data[1].(float64)
|
||||
ob.Bids = append(ob.Bids, PoloniexOrderbookItem{Price: price, Amount: amount})
|
||||
}
|
||||
return ob, nil
|
||||
}
|
||||
|
||||
func (p *Poloniex) GetTradeHistory(currencyPair, start, end string) ([]PoloniexTradeHistory, error) {
|
||||
|
||||
@@ -16,12 +16,22 @@ type PoloniexTicker struct {
|
||||
Low24Hr float64 `json:"low24hr,string"`
|
||||
}
|
||||
|
||||
type PoloniexOrderbook struct {
|
||||
type PoloniexOrderbookResponse struct {
|
||||
Asks [][]interface{} `json:"asks"`
|
||||
Bids [][]interface{} `json:"bids"`
|
||||
IsFrozen string `json:"isFrozen"`
|
||||
}
|
||||
|
||||
type PoloniexOrderbookItem struct {
|
||||
Price float64
|
||||
Amount float64
|
||||
}
|
||||
|
||||
type PoloniexOrderbook struct {
|
||||
Asks []PoloniexOrderbookItem `json:"asks"`
|
||||
Bids []PoloniexOrderbookItem `json:"bids"`
|
||||
}
|
||||
|
||||
type PoloniexTradeHistory struct {
|
||||
GlobalTradeID int64 `json:"globalTradeID"`
|
||||
TradeID int64 `json:"tradeID"`
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"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"
|
||||
)
|
||||
@@ -44,7 +45,8 @@ func (p *Poloniex) Run() {
|
||||
}
|
||||
|
||||
func (p *Poloniex) GetTickerPrice(currency string) (ticker.TickerPrice, error) {
|
||||
tickerNew, err := ticker.GetTicker(p.GetName(), currency[0:3], currency[3:])
|
||||
currencyPair := common.SplitStrings(currency, "_")
|
||||
tickerNew, err := ticker.GetTicker(p.GetName(), currencyPair[0], currencyPair[1])
|
||||
if err == nil {
|
||||
return tickerNew, nil
|
||||
}
|
||||
@@ -55,7 +57,6 @@ func (p *Poloniex) GetTickerPrice(currency string) (ticker.TickerPrice, error) {
|
||||
return tickerPrice, err
|
||||
}
|
||||
|
||||
currencyPair := common.SplitStrings(currency, "_")
|
||||
tickerPrice.FirstCurrency = currencyPair[0]
|
||||
tickerPrice.SecondCurrency = currencyPair[1]
|
||||
tickerPrice.Ask = tick[currency].Last
|
||||
@@ -68,6 +69,34 @@ func (p *Poloniex) GetTickerPrice(currency string) (ticker.TickerPrice, error) {
|
||||
return tickerPrice, nil
|
||||
}
|
||||
|
||||
func (p *Poloniex) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) {
|
||||
currencyPair := common.SplitStrings(currency, "_")
|
||||
ob, err := orderbook.GetOrderbook(p.GetName(), currencyPair[0], currencyPair[1])
|
||||
if err == nil {
|
||||
return ob, nil
|
||||
}
|
||||
|
||||
var orderBook orderbook.OrderbookBase
|
||||
orderbookNew, err := p.GetOrderbook(currency, 1000)
|
||||
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 = currencyPair[0]
|
||||
orderBook.SecondCurrency = currencyPair[1]
|
||||
orderbook.ProcessOrderbook(p.GetName(), orderBook.FirstCurrency, orderBook.SecondCurrency, orderBook)
|
||||
return orderBook, nil
|
||||
}
|
||||
|
||||
//GetExchangeAccountInfo : Retrieves balances for all enabled currencies for the Poloniex exchange
|
||||
func (e *Poloniex) GetExchangeAccountInfo() (exchange.ExchangeAccountInfo, error) {
|
||||
var response exchange.ExchangeAccountInfo
|
||||
|
||||
Reference in New Issue
Block a user