Config: OrderManager upgrade (#1737)

* Config: Allow missing Versions

This allows easier development of non-stacked version upgrades.
Though the PRs still need to be merged sequentially, or renumbered right
before merging

* Config: Move OrderManager upgrade to Version management
This commit is contained in:
Gareth Kirwan
2025-03-19 12:11:28 +07:00
committed by GitHub
parent f5faca2eb2
commit ade3ac5c59
10 changed files with 180 additions and 74 deletions

View File

@@ -165,7 +165,7 @@ func validateSettings(b *Engine, s *Settings, flagSet FlagSet) {
b.Settings = *s
flagSet.WithBool("coinmarketcap", &b.Settings.EnableCoinmarketcapAnalysis, b.Config.Currency.CryptocurrencyProvider.Enabled)
flagSet.WithBool("ordermanager", &b.Settings.EnableOrderManager, b.Config.OrderManager.Enabled != nil && *b.Config.OrderManager.Enabled)
flagSet.WithBool("ordermanager", &b.Settings.EnableOrderManager, b.Config.OrderManager.Enabled)
flagSet.WithBool("currencyconverter", &b.Settings.EnableCurrencyConverter, b.Config.Currency.ForexProviders.IsEnabled("currencyconverter"))

View File

@@ -39,15 +39,15 @@ func SetupOrderManager(exchangeManager iExchangeManager, communicationsManager i
if cfg == nil {
return nil, fmt.Errorf("%w OrderManager", errNilConfig)
}
var respectOrderHistoryLimits bool
if cfg.RespectOrderHistoryLimits != nil {
respectOrderHistoryLimits = *cfg.RespectOrderHistoryLimits
if cfg.ActivelyTrackFuturesPositions && cfg.FuturesTrackingSeekDuration <= 0 {
return nil, errInvalidFuturesTrackingSeekDuration
}
om := &OrderManager{
shutdown: make(chan struct{}),
activelyTrackFuturesPositions: cfg.ActivelyTrackFuturesPositions,
respectOrderHistoryLimits: respectOrderHistoryLimits,
futuresPositionSeekDuration: cfg.FuturesTrackingSeekDuration,
respectOrderHistoryLimits: cfg.RespectOrderHistoryLimits,
orderStore: store{
Orders: make(map[string][]*order.Detail),
exchangeManager: exchangeManager,
@@ -60,15 +60,6 @@ func SetupOrderManager(exchangeManager iExchangeManager, communicationsManager i
CancelOrdersOnShutdown: cfg.CancelOrdersOnShutdown,
},
}
if cfg.ActivelyTrackFuturesPositions {
if cfg.FuturesTrackingSeekDuration > 0 {
cfg.FuturesTrackingSeekDuration *= -1
}
if cfg.FuturesTrackingSeekDuration == 0 {
cfg.FuturesTrackingSeekDuration = defaultOrderSeekTime
}
om.futuresPositionSeekDuration = cfg.FuturesTrackingSeekDuration
}
return om, nil
}
@@ -728,7 +719,7 @@ func (m *OrderManager) processOrders() {
return
}
if sd.IsZero() {
sd = time.Now().Add(m.futuresPositionSeekDuration)
sd = time.Now().Add(-m.futuresPositionSeekDuration)
}
positions, err = exchanges[x].GetFuturesPositionOrders(context.TODO(), &futures.PositionsRequest{
Asset: enabledAssets[y],

View File

@@ -13,7 +13,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/common/convert"
"github.com/thrasher-corp/gocryptotrader/config"
"github.com/thrasher-corp/gocryptotrader/currency"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
@@ -162,30 +161,23 @@ func (f omfExchange) GetFuturesPositions(_ context.Context, req *futures.Positio
func TestSetupOrderManager(t *testing.T) {
_, err := SetupOrderManager(nil, nil, nil, nil)
if !errors.Is(err, errNilExchangeManager) {
t.Errorf("error '%v', expected '%v'", err, errNilExchangeManager)
}
assert.ErrorIs(t, err, errNilExchangeManager)
_, err = SetupOrderManager(NewExchangeManager(), nil, nil, nil)
if !errors.Is(err, errNilCommunicationsManager) {
t.Errorf("error '%v', expected '%v'", err, errNilCommunicationsManager)
}
assert.ErrorIs(t, err, errNilCommunicationsManager)
_, err = SetupOrderManager(NewExchangeManager(), &CommunicationManager{}, nil, &config.OrderManager{})
if !errors.Is(err, errNilWaitGroup) {
t.Errorf("error '%v', expected '%v'", err, errNilWaitGroup)
}
assert.ErrorIs(t, err, errNilWaitGroup)
var wg sync.WaitGroup
_, err = SetupOrderManager(NewExchangeManager(), &CommunicationManager{}, &wg, &config.OrderManager{})
if !errors.Is(err, nil) {
t.Errorf("error '%v', expected '%v'", err, nil)
}
_, err = SetupOrderManager(NewExchangeManager(), &CommunicationManager{}, &wg, &config.OrderManager{ActivelyTrackFuturesPositions: true, FuturesTrackingSeekDuration: 0})
if !errors.Is(err, nil) {
t.Errorf("error '%v', expected '%v'", err, nil)
}
require.NoError(t, err)
_, err = SetupOrderManager(NewExchangeManager(), &CommunicationManager{}, &wg, &config.OrderManager{ActivelyTrackFuturesPositions: true})
require.ErrorIs(t, err, errInvalidFuturesTrackingSeekDuration)
_, err = SetupOrderManager(NewExchangeManager(), &CommunicationManager{}, &wg, &config.OrderManager{ActivelyTrackFuturesPositions: true, FuturesTrackingSeekDuration: time.Hour})
if !errors.Is(err, nil) {
t.Errorf("error '%v', expected '%v'", err, nil)
}
require.NoError(t, err)
}
func TestOrderManagerStart(t *testing.T) {
@@ -1546,7 +1538,7 @@ func TestGetOpenFuturesPosition(t *testing.T) {
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
}
o, err = SetupOrderManager(em, &CommunicationManager{}, wg, &config.OrderManager{
Enabled: convert.BoolPtr(true),
Enabled: true,
FuturesTrackingSeekDuration: time.Hour,
Verbose: true,
ActivelyTrackFuturesPositions: true,

View File

@@ -13,20 +13,20 @@ import (
// OrderManagerName is an exported subsystem name
const OrderManagerName = "orders"
// vars for the fund manager package
// Public Errors
var (
// ErrOrdersAlreadyExists occurs when the order already exists in the manager
ErrOrdersAlreadyExists = errors.New("order already exists")
// ErrOrderIDCannotBeEmpty occurs when an order does not have an ID
ErrOrdersAlreadyExists = errors.New("order already exists")
ErrOrderIDCannotBeEmpty = errors.New("orderID cannot be empty")
// ErrOrderNotFound occurs when an order is not found in the orderstore
ErrOrderNotFound = errors.New("order does not exist")
ErrOrderNotFound = errors.New("order does not exist")
)
var (
errNilCommunicationsManager = errors.New("cannot start with nil communications manager")
errNilOrder = errors.New("nil order received")
errFuturesTrackingDisabled = errors.New("tracking futures positions disabled. enable it via config under orderManager activelyTrackFuturesPositions")
orderManagerInterval = time.Second * 10
defaultOrderSeekTime = -time.Hour * 24 * 365
errInvalidFuturesTrackingSeekDuration = errors.New("invalid config value for futuresTrackingSeekDuration")
)
type orderManagerConfig struct {