all: add trade converter to trade pnl fixer

This commit is contained in:
c9s 2024-08-12 15:02:02 +08:00
parent 473a6bc108
commit 1b0d4599e2
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
3 changed files with 10 additions and 1 deletions

View File

@ -134,7 +134,7 @@ func (c *TradeCollector) BindStreamForBackground(stream types.Stream) {
func (c *TradeCollector) BindStream(stream types.Stream) { func (c *TradeCollector) BindStream(stream types.Stream) {
stream.OnTradeUpdate(func(trade types.Trade) { stream.OnTradeUpdate(func(trade types.Trade) {
c.processTrade(trade) c.ProcessTrade(trade)
}) })
} }

View File

@ -10,6 +10,7 @@ import (
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
"github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/core"
"github.com/c9s/bbgo/pkg/exchange/batch" "github.com/c9s/bbgo/pkg/exchange/batch"
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
) )
@ -22,6 +23,8 @@ type ProfitFixerConfig struct {
// ProfitFixer implements a trade-history-based profit fixer // ProfitFixer implements a trade-history-based profit fixer
type ProfitFixer struct { type ProfitFixer struct {
sessions map[string]types.ExchangeTradeHistoryService sessions map[string]types.ExchangeTradeHistoryService
core.ConverterManager
} }
func NewProfitFixer() *ProfitFixer { func NewProfitFixer() *ProfitFixer {
@ -106,6 +109,8 @@ func (f *ProfitFixer) Fix(
func (f *ProfitFixer) FixFromTrades(allTrades []types.Trade, stats *types.ProfitStats, position *types.Position) error { func (f *ProfitFixer) FixFromTrades(allTrades []types.Trade, stats *types.ProfitStats, position *types.Position) error {
for _, trade := range allTrades { for _, trade := range allTrades {
trade = f.ConverterManager.ConvertTrade(trade)
profit, netProfit, madeProfit := position.AddTrade(trade) profit, netProfit, madeProfit := position.AddTrade(trade)
if madeProfit { if madeProfit {
p := position.NewProfit(trade, profit, netProfit) p := position.NewProfit(trade, profit, netProfit)

View File

@ -160,6 +160,7 @@ type Strategy struct {
Environment *bbgo.Environment Environment *bbgo.Environment
// Symbol is the maker exchange symbol
Symbol string `json:"symbol"` Symbol string `json:"symbol"`
// HedgeSymbol is the symbol for the hedge exchange // HedgeSymbol is the symbol for the hedge exchange
@ -262,6 +263,7 @@ func (s *Strategy) CrossSubscribe(sessions map[string]*bbgo.ExchangeSession) {
}) })
hedgeSession.Subscribe(types.KLineChannel, s.HedgeSymbol, types.SubscribeOptions{Interval: "1m"}) hedgeSession.Subscribe(types.KLineChannel, s.HedgeSymbol, types.SubscribeOptions{Interval: "1m"})
makerSession.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: "1m"}) makerSession.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: "1m"})
} }
@ -355,6 +357,8 @@ func (s *Strategy) CrossRun(
s.CrossExchangeMarketMakingStrategy.ProfitStats = types.NewProfitStats(makerMarket) s.CrossExchangeMarketMakingStrategy.ProfitStats = types.NewProfitStats(makerMarket)
fixer := common.NewProfitFixer() fixer := common.NewProfitFixer()
fixer.ConverterManager = s.ConverterManager
if ss, ok := makerSession.Exchange.(types.ExchangeTradeHistoryService); ok { if ss, ok := makerSession.Exchange.(types.ExchangeTradeHistoryService); ok {
log.Infof("adding makerSession %s to profitFixer", makerSession.Name) log.Infof("adding makerSession %s to profitFixer", makerSession.Name)
fixer.AddExchange(makerSession.Name, ss) fixer.AddExchange(makerSession.Name, ss)