Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: recover active orders with open orders periodically #1328

Merged
merged 14 commits into from Oct 17, 2023

Conversation

kbearXD
Copy link
Collaborator

@kbearXD kbearXD commented Sep 28, 2023

No description provided.

@bbgokarma-bot
Copy link

Hi @kbearXD,

This pull request may get 322 BBG.

To receive BBG token, please left your polygon address as an issue comment in this pull request with the following format, e.g.,

polygon:0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B

Once this pull request is merged, your BBG token will be sent to your wallet.

@codecov
Copy link

codecov bot commented Sep 28, 2023

Codecov Report

Merging #1328 (ccb7308) into main (fb110a1) will increase coverage by 0.11%.
Report is 3 commits behind head on main.
The diff coverage is 41.60%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1328      +/-   ##
==========================================
+ Coverage   20.68%   20.79%   +0.11%     
==========================================
  Files         560      561       +1     
  Lines       40014    40105      +91     
==========================================
+ Hits         8276     8339      +63     
- Misses      31138    31160      +22     
- Partials      600      606       +6     
Files Coverage Δ
pkg/strategy/grid2/metrics.go 87.93% <100.00%> (+0.89%) ⬆️
pkg/strategy/grid2/strategy.go 33.40% <0.00%> (+0.74%) ⬆️
pkg/strategy/grid2/active_order_recover.go 39.21% <39.21%> (ø)

... and 5 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fb110a1...ccb7308. Read the comment docs.

@bbgokarma-bot
Copy link

Re-estimated karma: this pull request may get 317 BBG

}
}

ticker := time.NewTicker(util.MillisecondsJitter(40*time.Minute, 30*60*1000))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how did you decide the interval? does it mean 40minutes + 0~30minute jitter?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, because I think we don't need to check it very frequently, so I wanna choose interval around 1 hour. To avoid DDOS, I use 0-30 min jitter.

for _, activeOrder := range activeOrders {
if _, exist := openOrdersMap[activeOrder.OrderID]; !exist {
s.logger.Infof("find active order (%d) not in open orders, updating...", activeOrder.OrderID)
delete(openOrdersMap, activeOrder.OrderID)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OrderID looks not existing in the map? So the delete is redundant?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change to delete when exist


s.recoverActiveOrders(ctx, session)
})
*/
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove unused code?


s.recoverActiveOrders(ctx, session)
})
go s.recoverActiveOrdersWithOpenOrdersPeriodically(ctx)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

every "OnAuth" event will launch a new goroutine for this?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you might need to check if recovered := atomic.LoadInt32(&s.recovered) then we start the goroutine for this?

or maybe you can move this to OnStart (which only trigger one time)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I should move it to OnStart

@bbgokarma-bot
Copy link

Re-estimated karma: this pull request may get 395 BBG

@kbearXD kbearXD requested review from gx578007 and c9s October 6, 2023 10:05
}
}

func (s *Strategy) queryOpenOrdersThenRecoverActiveOrders(ctx context.Context) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about just syncOpenOrders

// update active orders not in open orders
for _, activeOrder := range activeOrders {
if _, exist := openOrdersMap[activeOrder.OrderID]; !exist {
s.logger.Infof("find active order (%d) not in open orders, updating...", activeOrder.OrderID)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use #%d format for the orderID

})

if err != nil {
s.logger.WithError(err).Errorf("[ActiveOrderRecover] unable to query order (%d)", activeOrder.OrderID)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use #%d format for the orderID

@bbgokarma-bot
Copy link

Re-estimated karma: this pull request may get 437 BBG

@bbgokarma-bot
Copy link

Re-estimated karma: this pull request may get 442 BBG

@bbgokarma-bot
Copy link

Re-estimated karma: this pull request may get 446 BBG

@bbgokarma-bot
Copy link

Re-estimated karma: this pull request may get 471 BBG

@kbearXD kbearXD requested a review from c9s October 11, 2023 09:16
var openOrdersExpired bool = false
for _, activeOrder := range activeOrders {
if _, exist := openOrdersMap[activeOrder.OrderID]; exist {
if openOrdersExpired || time.Now().After(openOrdersExpiredTime) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pull out time.Now() to variable now?

@bbgokarma-bot
Copy link

Re-estimated karma: this pull request may get 508 BBG

}

// update active orders not in open orders
var openOrdersExpired bool = false
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

explain how this variable is used in the comment?

@bbgokarma-bot
Copy link

Re-estimated karma: this pull request may get 518 BBG

@kbearXD kbearXD requested a review from c9s October 12, 2023 08:24
@kbearXD kbearXD force-pushed the feature/grid2/recover-active-order-periodically branch from 5633d92 to c544937 Compare October 13, 2023 08:51
@bbgokarma-bot
Copy link

Re-estimated karma: this pull request may get 712 BBG

s.recoverActiveOrders(ctx, session)
})
}
time.AfterFunc(util.MillisecondsJitter(5*time.Second, 1000*10), func() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to check if it's in backtest here, you need to run backtest

@bbgokarma-bot
Copy link

Re-estimated karma: this pull request may get 764 BBG

@bbgokarma-bot
Copy link

Re-estimated karma: this pull request may get 782 BBG

s.mu.Lock()
defer s.mu.Unlock()

alreadyInitialize := false
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isInitialized is much simpler word

alreadyInitialize := false

if s.activeOrdersRecoverCh == nil {
s.logger.Info("initialize recover channel")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use word initializing

s.logger.Info("initialize recover channel")
s.activeOrdersRecoverCh = make(chan struct{}, 1)
} else {
s.logger.Info("already initialize recover channel, trigger active orders recover")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

recover channel is already already initialized

} else {
s.logger.Info("already initialize recover channel, trigger active orders recover")
alreadyInitialize = true
s.activeOrdersRecoverCh <- struct{}{}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use select + default case to skip it if the channel is full?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK!

log.WithError(err).Errorf("unable to sync active orders")
}

case <-s.activeOrdersRecoverCh:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we usually end the channel var with C instead of Ch

@bbgokarma-bot
Copy link

Re-estimated karma: this pull request may get 839 BBG

@bbgokarma-bot
Copy link

Re-estimated karma: this pull request may get 883 BBG

@kbearXD kbearXD merged commit 3bc03ff into main Oct 17, 2023
4 checks passed
@kbearXD kbearXD deleted the feature/grid2/recover-active-order-periodically branch October 17, 2023 09:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants