mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-06 07:26:47 +00:00
Kraken: bugfix - don't use internal ticker map
It's up to the calling application to handle concurrency Addresses https://github.com/thrasher-/gocryptotrader/issues/167
This commit is contained in:
@@ -50,7 +50,6 @@ const (
|
||||
type Kraken struct {
|
||||
exchange.Base
|
||||
CryptoFee, FiatFee float64
|
||||
Ticker map[string]Ticker
|
||||
}
|
||||
|
||||
// SetDefaults sets current default settings
|
||||
@@ -61,7 +60,6 @@ func (k *Kraken) SetDefaults() {
|
||||
k.CryptoFee = 0.10
|
||||
k.Verbose = false
|
||||
k.RESTPollingDelay = 10
|
||||
k.Ticker = make(map[string]Ticker)
|
||||
k.RequestCurrencyPairFormat.Delimiter = ""
|
||||
k.RequestCurrencyPairFormat.Uppercase = true
|
||||
k.RequestCurrencyPairFormat.Separator = ","
|
||||
@@ -202,6 +200,48 @@ func (k *Kraken) GetTicker(symbol string) (Ticker, error) {
|
||||
return ticker, nil
|
||||
}
|
||||
|
||||
// GetTickers supports fetching multiple tickers from Kraken
|
||||
// pairList must be in the format pairs separated by commas
|
||||
// ("LTCUSD,ETCUSD")
|
||||
func (k *Kraken) GetTickers(pairList string) (Tickers, error) {
|
||||
values := url.Values{}
|
||||
values.Set("pair", pairList)
|
||||
|
||||
type Response struct {
|
||||
Error []interface{} `json:"error"`
|
||||
Data map[string]TickerResponse `json:"result"`
|
||||
}
|
||||
|
||||
resp := Response{}
|
||||
path := fmt.Sprintf("%s/%s/public/%s?%s", krakenAPIURL, krakenAPIVersion, krakenTicker, values.Encode())
|
||||
|
||||
err := k.SendHTTPRequest(path, &resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(resp.Error) > 0 {
|
||||
return nil, fmt.Errorf("Kraken error: %s", resp.Error)
|
||||
}
|
||||
|
||||
tickers := make(Tickers)
|
||||
|
||||
for x, y := range resp.Data {
|
||||
ticker := Ticker{}
|
||||
ticker.Ask, _ = strconv.ParseFloat(y.Ask[0], 64)
|
||||
ticker.Bid, _ = strconv.ParseFloat(y.Bid[0], 64)
|
||||
ticker.Last, _ = strconv.ParseFloat(y.Last[0], 64)
|
||||
ticker.Volume, _ = strconv.ParseFloat(y.Volume[1], 64)
|
||||
ticker.VWAP, _ = strconv.ParseFloat(y.VWAP[1], 64)
|
||||
ticker.Trades = y.Trades[1]
|
||||
ticker.Low, _ = strconv.ParseFloat(y.Low[1], 64)
|
||||
ticker.High, _ = strconv.ParseFloat(y.High[1], 64)
|
||||
ticker.Open, _ = strconv.ParseFloat(y.Open, 64)
|
||||
tickers[x] = ticker
|
||||
}
|
||||
return tickers, nil
|
||||
}
|
||||
|
||||
// GetOHLC returns an array of open high low close values of a currency pair
|
||||
func (k *Kraken) GetOHLC(symbol string) ([]OpenHighLowClose, error) {
|
||||
values := url.Values{}
|
||||
|
||||
@@ -69,6 +69,14 @@ func TestGetTicker(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTickers(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, err := k.GetTickers("LTCUSD,ETCUSD")
|
||||
if err != nil {
|
||||
t.Error("Test failed - GetTickers() error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetOHLC(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, err := k.GetOHLC("BCHEUR")
|
||||
|
||||
@@ -49,6 +49,9 @@ type Ticker struct {
|
||||
Open float64
|
||||
}
|
||||
|
||||
// Tickers stores a map of tickers
|
||||
type Tickers map[string]Ticker
|
||||
|
||||
// TickerResponse holds ticker information before its put into the Ticker struct
|
||||
type TickerResponse struct {
|
||||
Ask []string `json:"a"`
|
||||
|
||||
@@ -2,10 +2,7 @@ package kraken
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"sync"
|
||||
|
||||
"github.com/thrasher-/gocryptotrader/common"
|
||||
@@ -80,13 +77,13 @@ func (k *Kraken) UpdateTicker(p pair.CurrencyPair, assetType string) (ticker.Pri
|
||||
if err != nil {
|
||||
return tickerPrice, err
|
||||
}
|
||||
err = k.SetTicker(pairsCollated.String())
|
||||
tickers, err := k.GetTickers(pairsCollated.String())
|
||||
if err != nil {
|
||||
return tickerPrice, err
|
||||
}
|
||||
|
||||
for _, x := range pairs {
|
||||
for y, z := range k.Ticker {
|
||||
for y, z := range tickers {
|
||||
if common.StringContains(y, x.FirstCurrency.Upper().String()) && common.StringContains(y, x.SecondCurrency.Upper().String()) {
|
||||
var tp ticker.Price
|
||||
tp.Pair = x
|
||||
@@ -103,44 +100,6 @@ func (k *Kraken) UpdateTicker(p pair.CurrencyPair, assetType string) (ticker.Pri
|
||||
return ticker.GetTicker(k.GetName(), p, assetType)
|
||||
}
|
||||
|
||||
// SetTicker sets ticker information from kraken
|
||||
func (k *Kraken) SetTicker(symbol string) error {
|
||||
values := url.Values{}
|
||||
values.Set("pair", symbol)
|
||||
|
||||
type Response struct {
|
||||
Error []interface{} `json:"error"`
|
||||
Data map[string]TickerResponse `json:"result"`
|
||||
}
|
||||
|
||||
resp := Response{}
|
||||
path := fmt.Sprintf("%s/%s/public/%s?%s", krakenAPIURL, krakenAPIVersion, krakenTicker, values.Encode())
|
||||
|
||||
err := k.SendHTTPRequest(path, &resp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(resp.Error) > 0 {
|
||||
return fmt.Errorf("Kraken error: %s", resp.Error)
|
||||
}
|
||||
|
||||
for x, y := range resp.Data {
|
||||
ticker := Ticker{}
|
||||
ticker.Ask, _ = strconv.ParseFloat(y.Ask[0], 64)
|
||||
ticker.Bid, _ = strconv.ParseFloat(y.Bid[0], 64)
|
||||
ticker.Last, _ = strconv.ParseFloat(y.Last[0], 64)
|
||||
ticker.Volume, _ = strconv.ParseFloat(y.Volume[1], 64)
|
||||
ticker.VWAP, _ = strconv.ParseFloat(y.VWAP[1], 64)
|
||||
ticker.Trades = y.Trades[1]
|
||||
ticker.Low, _ = strconv.ParseFloat(y.Low[1], 64)
|
||||
ticker.High, _ = strconv.ParseFloat(y.High[1], 64)
|
||||
ticker.Open, _ = strconv.ParseFloat(y.Open, 64)
|
||||
k.Ticker[x] = ticker
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetTickerPrice returns the ticker for a currency pair
|
||||
func (k *Kraken) GetTickerPrice(p pair.CurrencyPair, assetType string) (ticker.Price, error) {
|
||||
tickerNew, err := ticker.GetTicker(k.GetName(), p, assetType)
|
||||
|
||||
Reference in New Issue
Block a user