From 6e661c805ac62ec70d9852173d7109f33261822f Mon Sep 17 00:00:00 2001 From: "chiahung.lin" Date: Wed, 10 Jan 2024 14:37:07 +0800 Subject: [PATCH] fix --- config/dca2.yaml | 3 ++- pkg/{types => strategy/common}/callbacks.go | 17 +++++++++-------- pkg/strategy/dca2/profit_stats.go | 18 +----------------- pkg/strategy/dca2/strategy.go | 15 ++++++++++++++- pkg/types/persistence_ttl.go | 18 ++++++++++++++++++ 5 files changed, 44 insertions(+), 27 deletions(-) rename pkg/{types => strategy/common}/callbacks.go (51%) create mode 100644 pkg/types/persistence_ttl.go diff --git a/config/dca2.yaml b/config/dca2.yaml index 894afc634..e14c2c963 100644 --- a/config/dca2.yaml +++ b/config/dca2.yaml @@ -22,9 +22,10 @@ exchangeStrategies: - on: max dca2: symbol: ETHUSDT - short: false quoteInvestment: "200" maxOrderCount: 5 priceDeviation: "0.01" takeProfitRatio: "0.002" coolDownInterval: 180 + recoverWhenStart: true + keepOrdersWhenShutdown: true diff --git a/pkg/types/callbacks.go b/pkg/strategy/common/callbacks.go similarity index 51% rename from pkg/types/callbacks.go rename to pkg/strategy/common/callbacks.go index 01a4af82b..3df6c875f 100644 --- a/pkg/types/callbacks.go +++ b/pkg/strategy/common/callbacks.go @@ -1,36 +1,37 @@ -package types +package common -type CommonCallback struct { +//go:generate callbackgen -type StatusCallbacks +type StatusCallbacks struct { readyCallbacks []func() closedCallbacks []func() errorCallbacks []func(error) } -func (c *CommonCallback) OnReady(cb func()) { +func (c *StatusCallbacks) OnReady(cb func()) { c.readyCallbacks = append(c.readyCallbacks, cb) } -func (c *CommonCallback) EmitReady() { +func (c *StatusCallbacks) EmitReady() { for _, cb := range c.readyCallbacks { cb() } } -func (c *CommonCallback) OnClosed(cb func()) { +func (c *StatusCallbacks) OnClosed(cb func()) { c.closedCallbacks = append(c.closedCallbacks, cb) } -func (c *CommonCallback) EmitClosed() { +func (c *StatusCallbacks) EmitClosed() { for _, cb := range c.closedCallbacks { cb() } } -func (c *CommonCallback) OnError(cb func(err error)) { +func (c *StatusCallbacks) OnError(cb func(err error)) { c.errorCallbacks = append(c.errorCallbacks, cb) } -func (c *CommonCallback) EmitError(err error) { +func (c *StatusCallbacks) EmitError(err error) { for _, cb := range c.errorCallbacks { cb(err) } diff --git a/pkg/strategy/dca2/profit_stats.go b/pkg/strategy/dca2/profit_stats.go index a468e65cd..2bde24197 100644 --- a/pkg/strategy/dca2/profit_stats.go +++ b/pkg/strategy/dca2/profit_stats.go @@ -3,27 +3,11 @@ package dca2 import ( "fmt" "strings" - "time" "github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/types" ) -type PersistenceTTL struct { - ttl time.Duration -} - -func (p *PersistenceTTL) SetTTL(ttl time.Duration) { - if ttl.Nanoseconds() <= 0 { - return - } - p.ttl = ttl -} - -func (p *PersistenceTTL) Expiration() time.Duration { - return p.ttl -} - type ProfitStats struct { Symbol string `json:"symbol"` Market types.Market `json:"market,omitempty"` @@ -37,7 +21,7 @@ type ProfitStats struct { TotalProfit fixedpoint.Value `json:"totalProfit,omitempty"` TotalFee map[string]fixedpoint.Value `json:"totalFee,omitempty"` - PersistenceTTL + types.PersistenceTTL } func newProfitStats(market types.Market, quoteInvestment fixedpoint.Value) *ProfitStats { diff --git a/pkg/strategy/dca2/strategy.go b/pkg/strategy/dca2/strategy.go index d355b5f1f..1717a1e5b 100644 --- a/pkg/strategy/dca2/strategy.go +++ b/pkg/strategy/dca2/strategy.go @@ -10,6 +10,7 @@ import ( "github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/fixedpoint" + "github.com/c9s/bbgo/pkg/strategy/common" "github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/util" "github.com/prometheus/client_golang/prometheus" @@ -69,7 +70,7 @@ type Strategy struct { state State // callbacks - types.CommonCallback + common.StatusCallbacks positionCallbacks []func(*types.Position) profitCallbacks []func(*ProfitStats) } @@ -297,11 +298,22 @@ func (s *Strategy) CalculateProfitOfCurrentRound(ctx context.Context) error { // query the trades of this round for _, order := range orders { + if order.OrderID > s.ProfitStats.FromOrderID { + s.ProfitStats.FromOrderID = order.OrderID + } + + // skip not this strategy order + if order.GroupID != s.OrderGroupID { + continue + } + if order.ExecutedQuantity.Sign() == 0 { // skip no trade orders continue } + s.logger.Infof("[DCA] calculate profit stats from order: %s", order.String()) + trades, err := queryService.QueryOrderTrades(ctx, types.OrderQuery{ Symbol: order.Symbol, OrderID: strconv.FormatUint(order.OrderID, 10), @@ -312,6 +324,7 @@ func (s *Strategy) CalculateProfitOfCurrentRound(ctx context.Context) error { } for _, trade := range trades { + s.logger.Infof("[DCA] calculate profit stats from trade: %s", trade.String()) s.ProfitStats.AddTrade(trade) } } diff --git a/pkg/types/persistence_ttl.go b/pkg/types/persistence_ttl.go new file mode 100644 index 000000000..1b056ca71 --- /dev/null +++ b/pkg/types/persistence_ttl.go @@ -0,0 +1,18 @@ +package types + +import "time" + +type PersistenceTTL struct { + ttl time.Duration +} + +func (p *PersistenceTTL) SetTTL(ttl time.Duration) { + if ttl.Nanoseconds() <= 0 { + return + } + p.ttl = ttl +} + +func (p *PersistenceTTL) Expiration() time.Duration { + return p.ttl +}