Skip to content

Commit

Permalink
Add various helper functions for exchanges and currency pairs.
Browse files Browse the repository at this point in the history
Improve Kraken config/request currency handling.
Update config file to reflect changes.
  • Loading branch information
thrasher- committed Jan 15, 2018
1 parent 16629c6 commit 34eeed2
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 9 deletions.
9 changes: 5 additions & 4 deletions config_example.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
"AuthenticatedAPISupport": false,
"APIKey": "Key",
"APISecret": "Secret",
"AvailablePairs": "BTC-LTC,BTC-DOGE,BTC-VTC,BTC-PPC,BTC-FTC,BTC-RDD,BTC-NXT,BTC-DASH,BTC-POT,BTC-BLK,BTC-EMC2,BTC-XMY,BTC-AUR,BTC-EFL,BTC-GLD,BTC-SLR,BTC-PTC,BTC-GRS,BTC-NLG,BTC-RBY,BTC-XWC,BTC-MONA,BTC-THC,BTC-ENRG,BTC-ERC,BTC-VRC,BTC-CURE,BTC-XMR,BTC-CLOAK,BTC-START,BTC-KORE,BTC-XDN,BTC-TRUST,BTC-NAV,BTC-XST,BTC-BTCD,BTC-VIA,BTC-PINK,BTC-IOC,BTC-CANN,BTC-SYS,BTC-NEOS,BTC-DGB,BTC-BURST,BTC-EXCL,BTC-DOPE,BTC-BLOCK,BTC-ABY,BTC-BYC,BTC-XMG,BTC-BLITZ,BTC-BAY,BTC-FAIR,BTC-SPR,BTC-VTR,BTC-XRP,BTC-GAME,BTC-COVAL,BTC-NXS,BTC-XCP,BTC-BITB,BTC-GEO,BTC-FLDC,BTC-GRC,BTC-FLO,BTC-NBT,BTC-MUE,BTC-XEM,BTC-CLAM,BTC-DMD,BTC-GAM,BTC-SPHR,BTC-OK,BTC-SNRG,BTC-PKB,BTC-CPC,BTC-AEON,BTC-ETH,BTC-GCR,BTC-TX,BTC-BCY,BTC-EXP,BTC-INFX,BTC-OMNI,BTC-AMP,BTC-AGRS,BTC-XLM,USDT-BTC,BTC-CLUB,BTC-VOX,BTC-EMC,BTC-FCT,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ETH-MCO,BTC-ADT,ETH-ADT,BTC-FUN,ETH-FUN,BTC-PAY,ETH-PAY,BTC-STORJ,ETH-STORJ,BTC-ADX,ETH-ADX,ETH-DASH,ETH-SC,ETH-ZEC,USDT-ZEC,USDT-LTC,USDT-ETC,USDT-XRP,BTC-OMG,ETH-OMG,BTC-CVC,ETH-CVC,BTC-PART,BTC-QTUM,ETH-QTUM,ETH-XMR,ETH-XEM,ETH-XLM,ETH-NEO,USDT-XMR,USDT-DASH,ETH-BCC,USDT-BCC,BTC-BCC,BTC-DNT,ETH-DNT,USDT-NEO,ETH-WAVES,ETH-STRAT,ETH-DGB,ETH-FCT,USDT-OMG,BTC-ADA,BTC-MANA,ETH-MANA,BTC-SALT,ETH-SALT,BTC-TIX,ETH-TIX,BTC-RCN,ETH-RCN,BTC-VIB,ETH-VIB,BTC-MER,BTC-POWR,ETH-POWR,BTC-BTG,ETH-BTG,USDT-BTG,ETH-ADA,BTC-ENG,ETH-ENG,USDT-ADA,USDT-XVG,USDT-NXT,BTC-UKG,ETH-UKG",
"EnabledPairs": "USDT-BTC",
"BaseCurrencies": "USD",
"AssetTypes": "SPOT",
Expand Down Expand Up @@ -307,12 +307,13 @@
"AuthenticatedAPISupport": false,
"APIKey": "Key",
"APISecret": "Secret",
"AvailablePairs": "BCHEUR,REPEUR,XBTGBP,XBTUSD,ETHXBT,MLNXBT,ETCEUR,ETHGBP,ICNXBT,ZECEUR,EOSETH,GNOXBT,ETHCAD.D,ETHGBP.D,XRPEUR,BCHXBT,EOSXBT,LTCXBT,XBTEUR.D,XBTUSD.D,DASHUSD,GNOETH,ETHJPY,ETHUSD.D,REPETH,USDTUSD,ETHEUR,XLMXBT,BCHUSD,ETHCAD,XBTEUR,XMRUSD,ZECXBT,LTCUSD,XBTCAD,XMRXBT,ETHJPY.D,ICNETH,XBTCAD.D,XBTJPY,XRPUSD,ZECUSD,DASHEUR,ETCETH,ETCUSD,MLNETH,XMREUR,DASHXBT,ETHXBT.D,XDGXBT,XBTGBP.D,XRPXBT,XBTJPY.D,ETCXBT,ETHEUR.D,ETHUSD,LTCEUR,REPXBT",
"EnabledPairs": "ETCUSD,XBTUSD,ETHUSD",
"AvailablePairs": "DASH-USD,ETH-EUR,ETH-GBP,ZEC-EUR,BCH-EUR,BCH-XBT,REP-XBT,XBT-USD,XDG-XBT,REP-ETH,ZEC-XBT,REP-EUR,XMR-USD,ETC-ETH,EOS-ETH,LTC-EUR,EOS-XBT,LTC-XBT,ETH-XBT,LTC-USD,ETH-CAD,DASH-EUR,ETC-USD,GNO-XBT,ETC-XBT,ETC-EUR,ETH-USD,BCH-USD,DASH-XBT,XBT-CAD,XBT-JPY,ICN-ETH,MLN-XBT,GNO-ETH,XMR-XBT,USDT-USD,XBT-GBP,XLM-XBT,XRP-EUR,XRP-USD,ZEC-USD,MLN-ETH,XBT-EUR,XRP-XBT,XMR-EUR,ETH-JPY,ICN-XBT",
"EnabledPairs": "XBT-USD",
"BaseCurrencies": "EUR,USD,CAD,GBP,JPY",
"AssetTypes": "SPOT",
"ConfigCurrencyPairFormat": {
"Uppercase": true
"Uppercase": true,
"Delimiter": "-"
},
"RequestCurrencyPairFormat": {
"Uppercase": true,
Expand Down
11 changes: 11 additions & 0 deletions currency/pair/pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,14 @@ func NewCurrencyPairFromString(currency string) CurrencyPair {
}
return NewCurrencyPair(currency[0:3], currency[3:])
}

// Contains checks to see if a specified pair exists inside a currency pair
// array
func Contains(pairs []CurrencyPair, p CurrencyPair) bool {
for x := range pairs {
if pairs[x].Equal(p) {
return true
}
}
return false
}
17 changes: 17 additions & 0 deletions currency/pair/pair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,20 @@ func TestNewCurrencyPairFromString(t *testing.T) {
)
}
}

func TestContains(t *testing.T) {
pairOne := NewCurrencyPair("BTC", "USD")
pairTwo := NewCurrencyPair("LTC", "USD")

var pairs []CurrencyPair
pairs = append(pairs, pairOne)
pairs = append(pairs, pairTwo)

if !Contains(pairs, pairOne) {
t.Errorf("Test failed. TestContains: Expected pair was not found")
}

if Contains(pairs, NewCurrencyPair("ETH", "USD")) {
t.Errorf("Test failed. TestContains: Non-existant pair was found")
}
}
29 changes: 27 additions & 2 deletions exchanges/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type IBotExchange interface {
GetOrderbookEx(currency pair.CurrencyPair, assetType string) (orderbook.Base, error)
UpdateOrderbook(currency pair.CurrencyPair, assetType string) (orderbook.Base, error)
GetEnabledCurrencies() []pair.CurrencyPair
GetAvailableCurrencies() []pair.CurrencyPair
GetExchangeAccountInfo() (AccountInfo, error)
GetAuthenticatedAPISupport() bool
}
Expand Down Expand Up @@ -109,6 +110,18 @@ func GetExchangeAssetTypes(exchName string) ([]string, error) {
return common.SplitStrings(exch.AssetTypes, ","), nil
}

// CompareCurrencyPairFormats checks and returns whether or not the two supplied
// config currency pairs match
func CompareCurrencyPairFormats(pair1 config.CurrencyPairFormatConfig, pair2 *config.CurrencyPairFormatConfig) bool {
if pair1.Delimiter != pair2.Delimiter ||
pair1.Uppercase != pair2.Uppercase ||
pair1.Separator != pair2.Separator ||
pair1.Index != pair2.Index {
return false
}
return true
}

// SetCurrencyPairFormat checks the exchange request and config currency pair
// formats and sets it to a default setting if it doesn't exist
func (e *Base) SetCurrencyPairFormat() error {
Expand All @@ -128,7 +141,13 @@ func (e *Base) SetCurrencyPairFormat() error {
}
update = true
} else {
e.RequestCurrencyPairFormat = *exch.RequestCurrencyPairFormat
if CompareCurrencyPairFormats(e.RequestCurrencyPairFormat,
exch.RequestCurrencyPairFormat) {
e.RequestCurrencyPairFormat = *exch.RequestCurrencyPairFormat
} else {
*exch.RequestCurrencyPairFormat = e.ConfigCurrencyPairFormat
update = true
}
}

if exch.ConfigCurrencyPairFormat == nil {
Expand All @@ -140,7 +159,13 @@ func (e *Base) SetCurrencyPairFormat() error {
}
update = true
} else {
e.ConfigCurrencyPairFormat = *exch.ConfigCurrencyPairFormat
if CompareCurrencyPairFormats(e.ConfigCurrencyPairFormat,
exch.ConfigCurrencyPairFormat) {
e.ConfigCurrencyPairFormat = *exch.ConfigCurrencyPairFormat
} else {
*exch.ConfigCurrencyPairFormat = e.ConfigCurrencyPairFormat
update = true
}
}

if update {
Expand Down
25 changes: 25 additions & 0 deletions exchanges/exchange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,25 @@ func TestGetExchangeAssetTypes(t *testing.T) {
}
}

func TestCompareCurrencyPairFormats(t *testing.T) {
cfgOne := config.CurrencyPairFormatConfig{
Delimiter: "-",
Uppercase: true,
Index: "",
Separator: ",",
}

cfgTwo := cfgOne
if !CompareCurrencyPairFormats(cfgOne, &cfgTwo) {
t.Fatal("Test failed. CompareCurrencyPairFormats should be true")
}

cfgTwo.Delimiter = "~"
if CompareCurrencyPairFormats(cfgOne, &cfgTwo) {
t.Fatal("Test failed. CompareCurrencyPairFormats should not be true")
}
}

func TestSetCurrencyPairFormat(t *testing.T) {
cfg := config.GetConfig()
err := cfg.LoadConfig(config.ConfigTestFile)
Expand Down Expand Up @@ -142,6 +161,12 @@ func TestSetCurrencyPairFormat(t *testing.T) {
b.RequestCurrencyPairFormat.Uppercase {
t.Fatal("Test failed. TestSetCurrencyPairFormat RequestCurrencyPairFormat values are incorrect")
}

// if currency pairs are the same as the config, should load from config
err = b.SetCurrencyPairFormat()
if err != nil {
t.Fatalf("Test failed. TestSetCurrencyPairFormat. Error %s", err)
}
}

func TestGetAuthenticatedAPISupport(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion exchanges/kraken/kraken.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (k *Kraken) SetDefaults() {
k.RequestCurrencyPairFormat.Delimiter = ""
k.RequestCurrencyPairFormat.Uppercase = true
k.RequestCurrencyPairFormat.Separator = ","
k.ConfigCurrencyPairFormat.Delimiter = ""
k.ConfigCurrencyPairFormat.Delimiter = "-"
k.ConfigCurrencyPairFormat.Uppercase = true
k.AssetTypes = []string{ticker.Spot}
}
Expand Down
28 changes: 26 additions & 2 deletions exchanges/kraken/kraken_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,35 @@ func (k *Kraken) Run() {
if err != nil {
log.Printf("%s Failed to get available symbols.\n", k.GetName())
} else {
forceUpgrade := false
if !common.DataContains(k.EnabledPairs, "-") || !common.DataContains(k.AvailablePairs, "-") {
forceUpgrade = true
}

var exchangeProducts []string
for _, v := range assetPairs {
exchangeProducts = append(exchangeProducts, v.Altname)
if common.StringContains(v.Altname, ".d") {
continue
}
if v.Base[0] == 'X' {
v.Base = v.Base[1:]
}
if v.Quote[0] == 'Z' || v.Quote[0] == 'X' {
v.Quote = v.Quote[1:]
}
exchangeProducts = append(exchangeProducts, v.Base+"-"+v.Quote)
}

if forceUpgrade {
enabledPairs := []string{"XBT-USD"}
log.Println("WARNING: Available pairs for Kraken reset due to config upgrade, please enable the ones you would like again")

err = k.UpdateEnabledCurrencies(enabledPairs, true)
if err != nil {
log.Printf("%s Failed to get config.\n", k.GetName())
}
}
err = k.UpdateAvailableCurrencies(exchangeProducts, false)
err = k.UpdateAvailableCurrencies(exchangeProducts, forceUpgrade)
if err != nil {
log.Printf("%s Failed to get config.\n", k.GetName())
}
Expand Down
30 changes: 30 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,43 @@ import (
"errors"
"fmt"

"github.com/thrasher-/gocryptotrader/common"
"github.com/thrasher-/gocryptotrader/currency/pair"
"github.com/thrasher-/gocryptotrader/currency/translation"
exchange "github.com/thrasher-/gocryptotrader/exchanges"
"github.com/thrasher-/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-/gocryptotrader/exchanges/stats"
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
)

// GetRelatableCurrencies returns a list of currency pairs if it can find
// any relatable currencies (e.g BTCUSD -> BTC USDT -> XBT USD)
func GetRelatableCurrencies(p pair.CurrencyPair) []pair.CurrencyPair {
var pairs []pair.CurrencyPair
first, err := translation.GetTranslation(p.FirstCurrency)
if err == nil {
pairs = append(pairs, pair.NewCurrencyPair(first.String(),
p.SecondCurrency.String()))
}

second, err := translation.GetTranslation(p.SecondCurrency)
if err == nil {
pairs = append(pairs, pair.NewCurrencyPair(p.FirstCurrency.String(),
second.String()))
}
return pairs
}

// GetExchangeByName returns an exchange given an exchange name
func GetExchangeByName(exchName string) exchange.IBotExchange {
for x := range bot.exchanges {
if common.StringToLower(bot.exchanges[x].GetName()) == common.StringToLower(exchName) {
return bot.exchanges[x]
}
}
return nil
}

// GetSpecificOrderbook returns a specific orderbook given the currency,
// exchangeName and assetType
func GetSpecificOrderbook(currency, exchangeName, assetType string) (orderbook.Base, error) {
Expand Down

0 comments on commit 34eeed2

Please sign in to comment.