fix bug and new field running to help to test

This commit is contained in:
chiahung.lin 2024-01-18 15:39:56 +08:00
parent 465206afba
commit 1b33308450
5 changed files with 30 additions and 9 deletions

View File

@ -201,7 +201,6 @@ func (e *GeneralOrderExecutor) Bind() {
})
e.tradeCollector.OnPositionUpdate(func(position *types.Position) {
log.Infof("position changed: %s", position)
Notify(position)
})
}

View File

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/exchange/retry"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
@ -32,6 +33,18 @@ func (s *Strategy) placeOpenPositionOrders(ctx context.Context) error {
s.debugOrders(createdOrders)
// Running is false means this is new bot (no matter it has trades or not) in persistence
if !s.ProfitStats.Running {
for _, createdOrder := range createdOrders {
if s.ProfitStats.FromOrderID == 0 || s.ProfitStats.FromOrderID > createdOrder.OrderID {
s.ProfitStats.FromOrderID = createdOrder.OrderID
}
}
s.ProfitStats.Running = true
bbgo.Sync(ctx, s)
}
return nil
}

View File

@ -21,6 +21,11 @@ type ProfitStats struct {
TotalProfit fixedpoint.Value `json:"totalProfit,omitempty"`
TotalFee map[string]fixedpoint.Value `json:"totalFee,omitempty"`
// Running is used for testing by the same account
// Running is true -> this bot is still working, we need to use the values in persistence
// Running is false -> this bot is closed, we can reset all the values in persistence
Running bool `json:"running,omitempty"`
types.PersistenceTTL
}

View File

@ -86,6 +86,7 @@ func (s *Strategy) runState(ctx context.Context) {
if nextState != validNextState {
s.logger.Warnf("[DCA] %d is not valid next state of curreny state %d", nextState, s.state)
continue
}
// move to next state

View File

@ -133,11 +133,11 @@ func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
instanceID := s.InstanceID()
s.Session = session
if s.ProfitStats == nil {
if s.ProfitStats == nil || !s.ProfitStats.Running {
s.ProfitStats = newProfitStats(s.Market, s.QuoteInvestment)
}
if s.Position == nil {
if s.Position == nil || !s.ProfitStats.Running {
s.Position = types.NewPositionFromMarket(s.Market)
}
@ -161,6 +161,7 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
// order executor
s.OrderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) {
s.logger.Infof("[DCA] POSITION CHANGE: %s", position.String())
s.logger.Infof("[DCA] POSITION UPDATE: %s", s.Position.String())
bbgo.Sync(ctx, s)
@ -202,21 +203,21 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
s.logger.Info("[DCA] user data stream authenticated")
time.AfterFunc(3*time.Second, func() {
if isInitialize := s.initializeNextStateC(); !isInitialize {
if s.RecoverWhenStart {
if s.RecoverWhenStart && s.ProfitStats.Running {
// recover
if err := s.recover(ctx); err != nil {
s.logger.WithError(err).Error("[DCA] something wrong when state recovering")
return
}
s.logger.Infof("[DCA] state: %d", s.state)
s.logger.Infof("[DCA] position %s", s.Position.String())
s.logger.Infof("[DCA] profit stats %s", s.ProfitStats.String())
s.logger.Infof("[DCA] startTimeOfNextRound %s", s.startTimeOfNextRound)
} else {
s.state = WaitToOpenPosition
}
s.logger.Infof("[DCA] state: %d", s.state)
s.logger.Infof("[DCA] position %s", s.Position.String())
s.logger.Infof("[DCA] profit stats %s", s.ProfitStats.String())
s.logger.Infof("[DCA] startTimeOfNextRound %s", s.startTimeOfNextRound)
s.updateTakeProfitPrice()
// store persistence
@ -263,6 +264,8 @@ func (s *Strategy) Close(ctx context.Context) error {
s.logger.WithError(err).Errorf("[DCA] there are errors when cancelling orders at close")
}
s.ProfitStats.Running = false
bbgo.Sync(ctx, s)
return err
}