Skip to content

Commit

Permalink
asset: bitmask type optimisation (thrasher-corp#922)
Browse files Browse the repository at this point in the history
* asset: basic optim. bitmask

* glorious: nits

* currency: forgot parralel in testttttt

* ticker/orderbook: test fixes

* engine/rpcserver: fix and expand tests
  • Loading branch information
shazbert authored Apr 27, 2022
1 parent b45fbb8 commit b87a079
Show file tree
Hide file tree
Showing 41 changed files with 501 additions and 223 deletions.
8 changes: 4 additions & 4 deletions backtester/backtest/backtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,19 +525,19 @@ func (bt *BackTest) setupExchangeSettings(cfg *config.Config) (exchange.Exchange
func (bt *BackTest) loadExchangePairAssetBase(exch, base, quote, ass string) (gctexchange.IBotExchange, currency.Pair, asset.Item, error) {
e, err := bt.exchangeManager.GetExchangeByName(exch)
if err != nil {
return nil, currency.EMPTYPAIR, "", err
return nil, currency.EMPTYPAIR, asset.Empty, err
}

var cp, fPair currency.Pair
cp, err = currency.NewPairFromStrings(base, quote)
if err != nil {
return nil, currency.EMPTYPAIR, "", err
return nil, currency.EMPTYPAIR, asset.Empty, err
}

var a asset.Item
a, err = asset.New(ass)
if err != nil {
return nil, currency.EMPTYPAIR, "", err
return nil, currency.EMPTYPAIR, asset.Empty, err
}

exchangeBase := e.GetBase()
Expand All @@ -547,7 +547,7 @@ func (bt *BackTest) loadExchangePairAssetBase(exch, base, quote, ass string) (gc

fPair, err = exchangeBase.FormatExchangeCurrency(cp, a)
if err != nil {
return nil, currency.EMPTYPAIR, "", err
return nil, currency.EMPTYPAIR, asset.Empty, err
}
return e, fPair, a, nil
}
Expand Down
2 changes: 1 addition & 1 deletion backtester/eventhandlers/exchange/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ func applySlippageToPrice(direction gctorder.Side, price, slippageRate decimal.D
// SetExchangeAssetCurrencySettings sets the settings for an exchange, asset, currency
func (e *Exchange) SetExchangeAssetCurrencySettings(exch string, a asset.Item, cp currency.Pair, c *Settings) {
if c.Exchange == "" ||
c.Asset == "" ||
c.Asset == asset.Empty ||
c.Pair.IsEmpty() {
return
}
Expand Down
6 changes: 3 additions & 3 deletions backtester/eventhandlers/exchange/exchange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestReset(t *testing.T) {
func TestSetCurrency(t *testing.T) {
t.Parallel()
e := Exchange{}
e.SetExchangeAssetCurrencySettings("", "", currency.EMPTYPAIR, &Settings{})
e.SetExchangeAssetCurrencySettings("", asset.Empty, currency.EMPTYPAIR, &Settings{})
if len(e.CurrencySettings) != 0 {
t.Error("expected 0")
}
Expand Down Expand Up @@ -265,7 +265,7 @@ func TestExecuteOrder(t *testing.T) {
Item: gctkline.Item{
Exchange: "",
Pair: currency.EMPTYPAIR,
Asset: "",
Asset: asset.Empty,
Interval: 0,
Candles: []gctkline.Candle{
{
Expand Down Expand Up @@ -386,7 +386,7 @@ func TestExecuteOrderBuySellSizeLimit(t *testing.T) {
Item: gctkline.Item{
Exchange: "",
Pair: currency.EMPTYPAIR,
Asset: "",
Asset: asset.Empty,
Interval: 0,
Candles: []gctkline.Candle{
{
Expand Down
2 changes: 1 addition & 1 deletion backtester/eventhandlers/portfolio/portfolio.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ func (p *Portfolio) SetupCurrencySettingsMap(settings *exchange.Settings) (*Sett
if settings.Exchange == "" {
return nil, errExchangeUnset
}
if settings.Asset == "" {
if settings.Asset == asset.Empty {
return nil, errAssetUnset
}
if settings.Pair.IsEmpty() {
Expand Down
4 changes: 2 additions & 2 deletions backtester/eventhandlers/portfolio/portfolio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ func TestUpdate(t *testing.T) {
func TestGetFee(t *testing.T) {
t.Parallel()
p := Portfolio{}
f := p.GetFee("", "", currency.EMPTYPAIR)
f := p.GetFee("", asset.Empty, currency.EMPTYPAIR)
if !f.IsZero() {
t.Error("expected 0")
}
Expand All @@ -323,7 +323,7 @@ func TestGetFee(t *testing.T) {
func TestGetComplianceManager(t *testing.T) {
t.Parallel()
p := Portfolio{}
_, err := p.GetComplianceManager("", "", currency.EMPTYPAIR)
_, err := p.GetComplianceManager("", asset.Empty, currency.EMPTYPAIR)
if !errors.Is(err, errNoPortfolioSettings) {
t.Errorf("received: %v, expected: %v", err, errNoPortfolioSettings)
}
Expand Down
13 changes: 1 addition & 12 deletions backtester/funding/funding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -852,18 +852,7 @@ func TestMatchesCurrency(t *testing.T) {
func TestCreateSnapshot(t *testing.T) {
f := FundManager{}
f.CreateSnapshot(time.Time{})
f.items = append(f.items, &Item{
exchange: "",
asset: "",
currency: currency.EMPTYCODE,
initialFunds: decimal.Decimal{},
available: decimal.Decimal{},
reserved: decimal.Decimal{},
transferFee: decimal.Decimal{},
pairedWith: nil,
usdTrackingCandles: nil,
snapshot: nil,
})
f.items = append(f.items, &Item{})
f.CreateSnapshot(time.Time{})

dfk := &kline.DataFromKline{
Expand Down
4 changes: 2 additions & 2 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ func TestSupportsExchangeAssetType(t *testing.T) {
t.Error(err)
}

err = c.SupportsExchangeAssetType(testFakeExchangeName, "asdf")
err = c.SupportsExchangeAssetType(testFakeExchangeName, asset.Empty)
if err == nil {
t.Error("Expected error from invalid asset item")
}
Expand Down Expand Up @@ -988,7 +988,7 @@ func TestGetPairFormat(t *testing.T) {
asset.Spot: new(currency.PairStore),
},
}
_, err = c.GetPairFormat(testFakeExchangeName, asset.Item("invalid"))
_, err = c.GetPairFormat(testFakeExchangeName, asset.Empty)
if err == nil {
t.Error("Expected error from non-existent asset item")
}
Expand Down
31 changes: 31 additions & 0 deletions currency/manager.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package currency

import (
"encoding/json"
"errors"
"fmt"

Expand Down Expand Up @@ -224,3 +225,33 @@ func (p *PairsManager) getPairStore(a asset.Item) (*PairStore, error) {

return c, nil
}

// UnmarshalJSON implements the unmarshal json interface so that the key can be
// correctly unmarshalled from a string into a uint.
func (fs *FullStore) UnmarshalJSON(d []byte) error {
var temp map[string]*PairStore
err := json.Unmarshal(d, &temp)
if err != nil {
return err
}

*fs = make(FullStore, len(temp))
for key, val := range temp {
ai, err := asset.New(key)
if err != nil {
return err
}
(*fs)[ai] = val
}
return nil
}

// MarshalJSON implements the marshal json interface so that the key can be
// correctly marshalled from a uint.
func (fs FullStore) MarshalJSON() ([]byte, error) {
temp := make(map[string]*PairStore, len(fs))
for key, val := range fs {
temp[key.String()] = val
}
return json.Marshal(temp)
}
41 changes: 40 additions & 1 deletion currency/manager_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package currency

import (
"encoding/json"
"errors"
"testing"

"github.com/thrasher-corp/gocryptotrader/common/convert"
Expand Down Expand Up @@ -158,7 +160,7 @@ func TestGetPairs(t *testing.T) {
t.Fatal("pairs should be populated")
}

pairs, err = p.GetPairs("blah", true)
pairs, err = p.GetPairs(asset.Empty, true)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -361,3 +363,40 @@ func TestIsAssetEnabled_SetAssetEnabled(t *testing.T) {
t.Error("unexpected result")
}
}

func TestUnmarshalMarshal(t *testing.T) {
t.Parallel()
var um = make(FullStore)
um[asset.Spot] = &PairStore{AssetEnabled: convert.BoolPtr(true)}

data, err := json.Marshal(um)
if err != nil {
t.Fatal(err)
}

if string(data) != `{"spot":{"assetEnabled":true,"enabled":"","available":""}}` {
t.Fatal("unexpected value")
}

var another FullStore
err = json.Unmarshal(data, &another)
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
}

if _, ok := another[asset.Spot]; !ok {
t.Fatal("expected values to be associated with spot")
}

data = []byte(`{123:{"assetEnabled":null,"enabled":"","available":""}}`)
err = json.Unmarshal(data, &another)
if errors.Is(err, nil) {
t.Fatalf("expected error")
}

data = []byte(`{"bro":{"assetEnabled":null,"enabled":"","available":""}}`)
err = json.Unmarshal(data, &another)
if !errors.Is(err, asset.ErrNotSupported) {
t.Fatalf("received: '%v' but expected: '%v'", err, asset.ErrNotSupported)
}
}
16 changes: 10 additions & 6 deletions currency/manager_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@ import (

// PairsManager manages asset pairs
type PairsManager struct {
BypassConfigFormatUpgrades bool `json:"bypassConfigFormatUpgrades"`
RequestFormat *PairFormat `json:"requestFormat,omitempty"`
ConfigFormat *PairFormat `json:"configFormat,omitempty"`
UseGlobalFormat bool `json:"useGlobalFormat,omitempty"`
LastUpdated int64 `json:"lastUpdated,omitempty"`
Pairs map[asset.Item]*PairStore `json:"pairs"`
BypassConfigFormatUpgrades bool `json:"bypassConfigFormatUpgrades"`
RequestFormat *PairFormat `json:"requestFormat,omitempty"`
ConfigFormat *PairFormat `json:"configFormat,omitempty"`
UseGlobalFormat bool `json:"useGlobalFormat,omitempty"`
LastUpdated int64 `json:"lastUpdated,omitempty"`
Pairs FullStore `json:"pairs"`
m sync.RWMutex
}

// FullStore holds all supported asset types with the enabled and available
// pairs for an exchange.
type FullStore map[asset.Item]*PairStore

// PairStore stores a currency pair store
type PairStore struct {
AssetEnabled *bool `json:"assetEnabled"`
Expand Down
Loading

0 comments on commit b87a079

Please sign in to comment.