Skip to content

Commit

Permalink
all: add trade converter to trade pnl fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Aug 12, 2024
1 parent 473a6bc commit 1b0d459
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/core/tradecollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (c *TradeCollector) BindStreamForBackground(stream types.Stream) {

func (c *TradeCollector) BindStream(stream types.Stream) {
stream.OnTradeUpdate(func(trade types.Trade) {
c.processTrade(trade)
c.ProcessTrade(trade)
})
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/strategy/common/profit_fixer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"golang.org/x/sync/errgroup"

"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/core"
"github.com/c9s/bbgo/pkg/exchange/batch"
"github.com/c9s/bbgo/pkg/types"
)
Expand All @@ -22,6 +23,8 @@ type ProfitFixerConfig struct {
// ProfitFixer implements a trade-history-based profit fixer
type ProfitFixer struct {
sessions map[string]types.ExchangeTradeHistoryService

core.ConverterManager
}

func NewProfitFixer() *ProfitFixer {
Expand Down Expand Up @@ -106,6 +109,8 @@ func (f *ProfitFixer) Fix(

func (f *ProfitFixer) FixFromTrades(allTrades []types.Trade, stats *types.ProfitStats, position *types.Position) error {
for _, trade := range allTrades {
trade = f.ConverterManager.ConvertTrade(trade)

profit, netProfit, madeProfit := position.AddTrade(trade)
if madeProfit {
p := position.NewProfit(trade, profit, netProfit)
Expand Down
4 changes: 4 additions & 0 deletions pkg/strategy/xdepthmaker/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ type Strategy struct {

Environment *bbgo.Environment

// Symbol is the maker exchange symbol
Symbol string `json:"symbol"`

// HedgeSymbol is the symbol for the hedge exchange
Expand Down Expand Up @@ -262,6 +263,7 @@ func (s *Strategy) CrossSubscribe(sessions map[string]*bbgo.ExchangeSession) {
})

hedgeSession.Subscribe(types.KLineChannel, s.HedgeSymbol, types.SubscribeOptions{Interval: "1m"})

makerSession.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: "1m"})
}

Expand Down Expand Up @@ -355,6 +357,8 @@ func (s *Strategy) CrossRun(
s.CrossExchangeMarketMakingStrategy.ProfitStats = types.NewProfitStats(makerMarket)

fixer := common.NewProfitFixer()
fixer.ConverterManager = s.ConverterManager

if ss, ok := makerSession.Exchange.(types.ExchangeTradeHistoryService); ok {
log.Infof("adding makerSession %s to profitFixer", makerSession.Name)
fixer.AddExchange(makerSession.Name, ss)
Expand Down

0 comments on commit 1b0d459

Please sign in to comment.