FEATURE: [dca2] add position callback

This commit is contained in:
kbearXD 2024-04-19 16:24:40 +08:00
parent fe53319817
commit 547e9ece8f
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)
positionCallbacks []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.EmitPosition(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) OnPosition(cb func(*types.Position)) {
s.positionCallbacks = append(s.positionCallbacks, cb)
}
func (s *Strategy) EmitPosition(position *types.Position) {
for _, cb := range s.positionCallbacks {
cb(position)
}
}