strategy/supertrend: add TradeStats

This commit is contained in:
Andy Cheng 2022-07-05 16:30:13 +08:00
parent 5b3ba03042
commit f0dc9d6147

View File

@ -3,6 +3,7 @@ package supertrend
import ( import (
"context" "context"
"fmt" "fmt"
"os"
"sync" "sync"
"github.com/c9s/bbgo/pkg/util" "github.com/c9s/bbgo/pkg/util"
@ -24,8 +25,6 @@ var log = logrus.WithField("strategy", ID)
// TODO: SL by fixed percentage // TODO: SL by fixed percentage
// TODO: limit order if possible // TODO: limit order if possible
// TODO: lingre as indicator
// TODO: types.TradeStats
func init() { func init() {
// Register the pointer of the strategy struct, // Register the pointer of the strategy struct,
@ -88,8 +87,9 @@ type Strategy struct {
Market types.Market Market types.Market
// persistence fields // persistence fields
Position *types.Position `json:"position,omitempty" persistence:"position"` Position *types.Position `persistence:"position"`
ProfitStats *types.ProfitStats `json:"profitStats,omitempty" persistence:"profit_stats"` ProfitStats *types.ProfitStats `persistence:"profit_stats"`
TradeStats *types.TradeStats `persistence:"trade_stats"`
// Order and trade // Order and trade
orderExecutor *bbgo.GeneralOrderExecutor orderExecutor *bbgo.GeneralOrderExecutor
@ -314,6 +314,16 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
s.Position.Strategy = ID s.Position.Strategy = ID
s.Position.StrategyInstanceID = s.InstanceID() s.Position.StrategyInstanceID = s.InstanceID()
// Profit stats
if s.ProfitStats == nil {
s.ProfitStats = types.NewProfitStats(s.Market)
}
// Trade stats
if s.TradeStats == nil {
s.TradeStats = &types.TradeStats{}
}
// Set fee rate // Set fee rate
if s.session.MakerFeeRate.Sign() > 0 || s.session.TakerFeeRate.Sign() > 0 { if s.session.MakerFeeRate.Sign() > 0 || s.session.TakerFeeRate.Sign() > 0 {
s.Position.SetExchangeFeeRate(s.session.ExchangeName, types.ExchangeFee{ s.Position.SetExchangeFeeRate(s.session.ExchangeName, types.ExchangeFee{
@ -322,15 +332,11 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
}) })
} }
// Profit
if s.ProfitStats == nil {
s.ProfitStats = types.NewProfitStats(s.Market)
}
// Setup order executor // Setup order executor
s.orderExecutor = bbgo.NewGeneralOrderExecutor(session, s.Symbol, ID, instanceID, s.Position) s.orderExecutor = bbgo.NewGeneralOrderExecutor(session, s.Symbol, ID, instanceID, s.Position)
s.orderExecutor.BindEnvironment(s.Environment) s.orderExecutor.BindEnvironment(s.Environment)
s.orderExecutor.BindProfitStats(s.ProfitStats) s.orderExecutor.BindProfitStats(s.ProfitStats)
s.orderExecutor.BindTradeStats(s.TradeStats)
s.orderExecutor.Bind() s.orderExecutor.Bind()
// Sync position to redis on trade // Sync position to redis on trade
@ -487,6 +493,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
close(s.stopC) close(s.stopC)
_ = s.orderExecutor.GracefulCancel(ctx) _ = s.orderExecutor.GracefulCancel(ctx)
_, _ = fmt.Fprintln(os.Stderr, s.TradeStats.String())
}) })
return nil return nil