Skip to content

Commit

Permalink
exchange: upgrade UpdatePair method (thrasher-corp#991)
Browse files Browse the repository at this point in the history
* exchange: upgrade update pair

* exchanges: Add enabled string matching and format handling if discrepency is found.

* linter: fixes

* bithumb: fix tests

* BTSE: api change fix ordering

* huobi: fix tests

* gloriousnits: stage 1

* gloriousnits: stage 2

* currency: more nits

* bitmex: add spot and process pairs before currency package call.

* bitmex: finished correct orderbook matching and other implementations

* linter: fix issue

* currency: Fix linter

* currency: segregate and protect pair store, update tests

* currency/manager: clean code, rm log output

* currency: Add store method and make sure formatting stays nil if not stored.

* gct: check errors

* engine/websocketroutineman: fix tests

* bybit: fix duplication bug

* huobi: fix test

* btse: fix tests?

* ob/buffer: fix tests

* Update currency/manager.go

Co-authored-by: Scott <[email protected]>

* glorious: nits

* glorious: nits strikes again.

* exchange: add bypassConfigFormatUpgrades to stop formatting

* GLORIOUS LINTER

* Update exchanges/bithumb/bithumb_wrapper.go

Co-authored-by: Scott <[email protected]>

* glorious: nits

* exchange: fix pair upgrade issue when duplications are in both avail and enabled pairs

* linter: fix shadow dec

* config: fix test

* Update currency/pair_test.go

Co-authored-by: Scott <[email protected]>

Co-authored-by: Ryan O'Hara-Reid <[email protected]>
Co-authored-by: Scott <[email protected]>
  • Loading branch information
3 people authored Sep 15, 2022
1 parent ecc3b10 commit f843b7d
Show file tree
Hide file tree
Showing 55 changed files with 1,819 additions and 672 deletions.
2 changes: 1 addition & 1 deletion backtester/engine/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func NewFromConfig(cfg *config.Config, templatePath, output string, verbose bool
if err != nil {
return nil, fmt.Errorf("could not get pair format %v, %w", curr, err)
}
curr = curr.Format(requestFormat.Delimiter, requestFormat.Uppercase)
curr = curr.Format(requestFormat)
var avail, enabled currency.Pairs
avail, err = exch.GetAvailablePairs(a)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,14 @@ func sortSignals(d []data.Handler) (map[currency.Pair]cashCarrySignals, error) {
a := l.GetAssetType()
switch {
case a == asset.Spot:
entry := response[l.Pair().Format("", false)]
entry := response[l.Pair().Format(currency.EMPTYFORMAT)]
entry.spotSignal = d[i]
response[l.Pair().Format("", false)] = entry
response[l.Pair().Format(currency.EMPTYFORMAT)] = entry
case a.IsFutures():
u := l.GetUnderlyingPair()
entry := response[u.Format("", false)]
entry := response[u.Format(currency.EMPTYFORMAT)]
entry.futureSignal = d[i]
response[u.Format("", false)] = entry
response[u.Format(currency.EMPTYFORMAT)] = entry
default:
return nil, errFuturesOnly
}
Expand Down
5 changes: 3 additions & 2 deletions backtester/report/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,12 @@ func createFuturesSpotDiffChart(items map[string]map[asset.Item]map[currency.Pai
AxisType: "linear",
}

upperFormat := currency.PairFormat{Uppercase: true}
for _, assetMap := range items {
for item, pairMap := range assetMap {
for pair, result := range pairMap {
if item.IsFutures() {
p := result.UnderlyingPair.Format("", true)
p := result.UnderlyingPair.Format(upperFormat)
diff, ok := currs[p]
if !ok {
diff = linkCurrencyDiff{}
Expand All @@ -151,7 +152,7 @@ func createFuturesSpotDiffChart(items map[string]map[asset.Item]map[currency.Pai
diff.FuturesEvents = result.Events
currs[p] = diff
} else {
p := pair.Format("", true)
p := pair.Format(upperFormat)
diff, ok := currs[p]
if !ok {
diff = linkCurrencyDiff{}
Expand Down
1 change: 1 addition & 0 deletions cmd/exchange_template/exchange_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ func makeExchange(exchangeDirectory string, configTestFile *config.Config, exch
},
ConfigFormat: &currency.PairFormat{
Uppercase: true,
Delimiter: currency.DashDelimiter,
},
}

Expand Down
28 changes: 14 additions & 14 deletions cmd/exchange_wrapper_issues/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,31 +299,31 @@ func testWrappers(e exchange.IBotExchange, base *exchange.Base, config *Config)
for i := range assetTypes {
var msg string
log.Printf("%v %v", base.GetName(), assetTypes[i])
if _, ok := base.Config.CurrencyPairs.Pairs[assetTypes[i]]; !ok {
storedPairs, ok := base.Config.CurrencyPairs.Pairs[assetTypes[i]]
if !ok {
continue
}

var p currency.Pair
var err error
switch {
case currencyPairOverride != "":
var err error
p, err = currency.NewPairFromString(currencyPairOverride)
if err != nil {
log.Printf("%v Encountered error: '%v'", base.GetName(), err)
continue
}
case len(base.Config.CurrencyPairs.Pairs[assetTypes[i]].Enabled) == 0:
if len(base.Config.CurrencyPairs.Pairs[assetTypes[i]].Available) == 0 {
log.Printf("%v has no enabled or available currencies. Skipping",
base.GetName())
continue
case len(storedPairs.Enabled) == 0:
if len(storedPairs.Available) == 0 {
err = fmt.Errorf("%v has no enabled or available currencies. Skipping", base.GetName())
break
}
p = base.Config.CurrencyPairs.Pairs[assetTypes[i]].Available.GetRandomPair()
p, err = storedPairs.Available.GetRandomPair()
default:
p = base.Config.CurrencyPairs.Pairs[assetTypes[i]].Enabled.GetRandomPair()
p, err = storedPairs.Enabled.GetRandomPair()
}

if err != nil {
log.Printf("%v Encountered error: '%v'", base.GetName(), err)
continue
}

var err error
p, err = disruptFormatting(p)
if err != nil {
log.Println("failed to disrupt currency pair formatting:", err)
Expand Down
83 changes: 48 additions & 35 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ import (
"github.com/thrasher-corp/gocryptotrader/portfolio/banking"
)

// errExchangeConfigIsNil defines an error when the config is nil
var errExchangeConfigIsNil = errors.New("exchange config is nil")
var (
// errExchangeConfigIsNil defines an error when the config is nil
errExchangeConfigIsNil = errors.New("exchange config is nil")
errPairsManagerIsNil = errors.New("currency pairs manager is nil")
)

// GetCurrencyConfig returns currency configurations
func (c *Config) GetCurrencyConfig() currency.Config {
Expand Down Expand Up @@ -346,7 +349,7 @@ func (c *Config) GetExchangeAssetTypes(exchName string) (asset.Items, error) {
}

if exchCfg.CurrencyPairs == nil {
return nil, fmt.Errorf("exchange %s currency pairs is nil", exchName)
return nil, fmt.Errorf("%s %w", exchName, errPairsManagerIsNil)
}

return exchCfg.CurrencyPairs.GetAssetTypes(false), nil
Expand All @@ -360,7 +363,7 @@ func (c *Config) SupportsExchangeAssetType(exchName string, assetType asset.Item
}

if exchCfg.CurrencyPairs == nil {
return fmt.Errorf("exchange %s currency pairs is nil", exchName)
return fmt.Errorf("%s %w", exchName, errPairsManagerIsNil)
}

if !assetType.IsValid() {
Expand Down Expand Up @@ -389,8 +392,7 @@ func (c *Config) SetPairs(exchName string, assetType asset.Item, enabled bool, p
return err
}

exchCfg.CurrencyPairs.StorePairs(assetType, pairs, enabled)
return nil
return exchCfg.CurrencyPairs.StorePairs(assetType, pairs, enabled)
}

// GetCurrencyPairConfig returns currency pair config for the desired exchange and asset type
Expand Down Expand Up @@ -507,10 +509,13 @@ func (c *Config) CheckPairConsistency(exchName string) error {
return err
}

err = c.SetPairs(exchName,
assetTypes[x],
true,
currency.Pairs{availPairs.GetRandomPair()})
var rPair currency.Pair
rPair, err = availPairs.GetRandomPair()
if err != nil {
return err
}

err = c.SetPairs(exchName, assetTypes[x], true, currency.Pairs{rPair})
if err != nil {
return err
}
Expand Down Expand Up @@ -565,10 +570,13 @@ func (c *Config) CheckPairConsistency(exchName string) error {
continue
}

err = c.SetPairs(exchName,
assetTypes[x],
true,
currency.Pairs{availPairs.GetRandomPair()})
var rPair currency.Pair
rPair, err = availPairs.GetRandomPair()
if err != nil {
return err
}

err = c.SetPairs(exchName, assetTypes[x], true, currency.Pairs{rPair})
if err != nil {
return err
}
Expand All @@ -583,16 +591,24 @@ func (c *Config) CheckPairConsistency(exchName string) error {
return err
}

newPair := avail.GetRandomPair()
err = c.SetPairs(exchName, assetTypes[0], true, currency.Pairs{newPair})
if len(avail) == 0 {
return nil
}

rPair, err := avail.GetRandomPair()
if err != nil {
return err
}

err = c.SetPairs(exchName, assetTypes[0], true, currency.Pairs{rPair})
if err != nil {
return err
}
log.Warnf(log.ConfigMgr,
"Exchange %s: [%v] No enabled pairs found in available pairs list, randomly added %v pair.\n",
exchName,
assetTypes[0],
newPair)
rPair)
}
return nil
}
Expand All @@ -611,12 +627,12 @@ func (c *Config) SupportsPair(exchName string, p currency.Pair, assetType asset.
func (c *Config) GetPairFormat(exchName string, assetType asset.Item) (currency.PairFormat, error) {
exchCfg, err := c.GetExchangeConfig(exchName)
if err != nil {
return currency.PairFormat{}, err
return currency.EMPTYFORMAT, err
}

err = c.SupportsExchangeAssetType(exchName, assetType)
if err != nil {
return currency.PairFormat{}, err
return currency.EMPTYFORMAT, err
}

if exchCfg.CurrencyPairs.UseGlobalFormat {
Expand All @@ -625,18 +641,18 @@ func (c *Config) GetPairFormat(exchName string, assetType asset.Item) (currency.

p, err := exchCfg.CurrencyPairs.Get(assetType)
if err != nil {
return currency.PairFormat{}, err
return currency.EMPTYFORMAT, err
}

if p == nil {
return currency.PairFormat{},
return currency.EMPTYFORMAT,
fmt.Errorf("exchange %s pair store for asset type %s is nil",
exchName,
assetType)
}

if p.ConfigFormat == nil {
return currency.PairFormat{},
return currency.EMPTYFORMAT,
fmt.Errorf("exchange %s pair config format for asset type %s is nil",
exchName,
assetType)
Expand Down Expand Up @@ -666,8 +682,7 @@ func (c *Config) GetAvailablePairs(exchName string, assetType asset.Item) (curre
return nil, nil
}

return pairs.Format(pairFormat.Delimiter, pairFormat.Index,
pairFormat.Uppercase), nil
return pairs.Format(pairFormat), nil
}

// GetEnabledPairs returns a list of currency pairs for a specifc exchange
Expand All @@ -691,10 +706,7 @@ func (c *Config) GetEnabledPairs(exchName string, assetType asset.Item) (currenc
return nil, nil
}

return pairs.Format(pairFormat.Delimiter,
pairFormat.Index,
pairFormat.Uppercase),
nil
return pairs.Format(pairFormat), nil
}

// GetEnabledExchanges returns a list of enabled exchanges
Expand Down Expand Up @@ -853,13 +865,14 @@ func (c *Config) CheckExchangeConfigValues() error {
}

c.Exchanges[i].CurrencyPairs.UseGlobalFormat = true
c.Exchanges[i].CurrencyPairs.Store(asset.Spot,
currency.PairStore{
AssetEnabled: convert.BoolPtr(true),
Available: availPairs,
Enabled: enabledPairs,
},
)
err := c.Exchanges[i].CurrencyPairs.Store(asset.Spot, &currency.PairStore{
AssetEnabled: convert.BoolPtr(true),
Available: availPairs,
Enabled: enabledPairs,
})
if err != nil {
return err
}

// flush old values
c.Exchanges[i].PairsLastUpdated = nil
Expand Down
Loading

0 comments on commit f843b7d

Please sign in to comment.