mirror of
https://github.com/c9s/bbgo.git
synced 2024-09-20 00:01:09 +00:00
fix
This commit is contained in:
parent
d3bc37f45e
commit
6e661c805a
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
18
pkg/types/persistence_ttl.go
Normal file
18
pkg/types/persistence_ttl.go
Normal file
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue
Block a user