Merge pull request #1621 from c9s/kbearXD/dca2/emit-position

FEATURE: [dca2] add position callback
This commit is contained in:
kbearXD 2024-04-22 12:54:12 +08:00 committed by GitHub
commit 489889d1e6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 1 deletions

View File

@ -100,7 +100,8 @@ type Strategy struct {
// callbacks
common.StatusCallbacks
profitCallbacks []func(*ProfitStats)
profitCallbacks []func(*ProfitStats)
positionUpdateCallbacks []func(*types.Position)
}
func (s *Strategy) ID() string {
@ -229,6 +230,9 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
// update take profit price here
s.updateTakeProfitPrice()
// emit position update
s.EmitPositionUpdate(position)
})
s.OrderExecutor.ActiveMakerOrders().OnFilled(func(o types.Order) {

View File

@ -2,6 +2,10 @@
package dca2
import (
"github.com/c9s/bbgo/pkg/types"
)
func (s *Strategy) OnProfit(cb func(*ProfitStats)) {
s.profitCallbacks = append(s.profitCallbacks, cb)
}
@ -11,3 +15,13 @@ func (s *Strategy) EmitProfit(profitStats *ProfitStats) {
cb(profitStats)
}
}
func (s *Strategy) OnPositionUpdate(cb func(*types.Position)) {
s.positionUpdateCallbacks = append(s.positionUpdateCallbacks, cb)
}
func (s *Strategy) EmitPositionUpdate(position *types.Position) {
for _, cb := range s.positionUpdateCallbacks {
cb(position)
}
}