grid2: calculate TotalFee

This commit is contained in:
c9s 2022-12-06 10:05:43 +08:00
parent 7a8ae7cd01
commit e29f3c50e8
4 changed files with 69 additions and 41 deletions

View File

@ -21,6 +21,7 @@ backtest:
symbols:
- BTCUSDT
sessions: [binance]
feeMode: token
accounts:
binance:
balances:
@ -32,7 +33,7 @@ exchangeStrategies:
- on: binance
grid2:
symbol: BTCUSDT
upperPrice: 60_000.0
upperPrice: 50_000.0
lowerPrice: 28_000.0
gridNumber: 1000

View File

@ -13,42 +13,3 @@ type GridProfit struct {
Time time.Time `json:"time"`
Order types.Order `json:"order"`
}
type GridProfitStats struct {
Symbol string `json:"symbol"`
TotalBaseProfit fixedpoint.Value `json:"totalBaseProfit,omitempty"`
TotalQuoteProfit fixedpoint.Value `json:"totalQuoteProfit,omitempty"`
FloatProfit fixedpoint.Value `json:"floatProfit,omitempty"`
GridProfit fixedpoint.Value `json:"gridProfit,omitempty"`
ArbitrageCount int `json:"arbitrageCount,omitempty"`
TotalFee fixedpoint.Value `json:"totalFee,omitempty"`
Volume fixedpoint.Value `json:"volume,omitempty"`
Market types.Market `json:"market,omitempty"`
ProfitEntries []*GridProfit `json:"profitEntries,omitempty"`
}
func newGridProfitStats(market types.Market) *GridProfitStats {
return &GridProfitStats{
Symbol: market.Symbol,
TotalBaseProfit: fixedpoint.Zero,
TotalQuoteProfit: fixedpoint.Zero,
FloatProfit: fixedpoint.Zero,
GridProfit: fixedpoint.Zero,
ArbitrageCount: 0,
TotalFee: fixedpoint.Zero,
Volume: fixedpoint.Zero,
Market: market,
ProfitEntries: nil,
}
}
func (s *GridProfitStats) AddProfit(profit *GridProfit) {
switch profit.Currency {
case s.Market.QuoteCurrency:
s.TotalQuoteProfit = s.TotalQuoteProfit.Add(profit.Profit)
case s.Market.BaseCurrency:
s.TotalBaseProfit = s.TotalBaseProfit.Add(profit.Profit)
}
s.ProfitEntries = append(s.ProfitEntries, profit)
}

View File

@ -0,0 +1,60 @@
package grid2
import (
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
type GridProfitStats struct {
Symbol string `json:"symbol"`
TotalBaseProfit fixedpoint.Value `json:"totalBaseProfit,omitempty"`
TotalQuoteProfit fixedpoint.Value `json:"totalQuoteProfit,omitempty"`
FloatProfit fixedpoint.Value `json:"floatProfit,omitempty"`
GridProfit fixedpoint.Value `json:"gridProfit,omitempty"`
ArbitrageCount int `json:"arbitrageCount,omitempty"`
TotalFee map[string]fixedpoint.Value `json:"totalFee,omitempty"`
Volume fixedpoint.Value `json:"volume,omitempty"`
Market types.Market `json:"market,omitempty"`
ProfitEntries []*GridProfit `json:"profitEntries,omitempty"`
}
func newGridProfitStats(market types.Market) *GridProfitStats {
return &GridProfitStats{
Symbol: market.Symbol,
TotalBaseProfit: fixedpoint.Zero,
TotalQuoteProfit: fixedpoint.Zero,
FloatProfit: fixedpoint.Zero,
GridProfit: fixedpoint.Zero,
ArbitrageCount: 0,
TotalFee: make(map[string]fixedpoint.Value),
Volume: fixedpoint.Zero,
Market: market,
ProfitEntries: nil,
}
}
func (s *GridProfitStats) AddTrade(trade types.Trade) {
if s.TotalFee == nil {
s.TotalFee = make(map[string]fixedpoint.Value)
}
if fee, ok := s.TotalFee[trade.FeeCurrency]; ok {
s.TotalFee[trade.FeeCurrency] = fee.Add(trade.Fee)
} else {
s.TotalFee[trade.FeeCurrency] = trade.Fee
}
}
func (s *GridProfitStats) AddProfit(profit *GridProfit) {
// increase arbitrage count per profit round
s.ArbitrageCount++
switch profit.Currency {
case s.Market.QuoteCurrency:
s.TotalQuoteProfit = s.TotalQuoteProfit.Add(profit.Profit)
case s.Market.BaseCurrency:
s.TotalBaseProfit = s.TotalBaseProfit.Add(profit.Profit)
}
s.ProfitEntries = append(s.ProfitEntries, profit)
}

View File

@ -261,7 +261,7 @@ func (s *Strategy) handleOrderFilled(o types.Order) {
if o.Side == types.SideTypeBuy {
orderTrades := s.historicalTrades.GetOrderTrades(o)
if len(orderTrades) > 0 {
s.logger.Infof("FOUND FILLED ORDER TRADES: %+v", orderTrades)
s.logger.Infof("found filled order trades: %+v", orderTrades)
}
if !s.verifyOrderTrades(o, orderTrades) {
@ -276,6 +276,7 @@ func (s *Strategy) handleOrderFilled(o types.Order) {
if err != nil {
s.logger.WithError(err).Errorf("query order trades error")
} else {
s.logger.Infof("fetch api trades: %+v", apiOrderTrades)
orderTrades = apiOrderTrades
}
}
@ -959,9 +960,14 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
s.orderExecutor.BindEnvironment(s.Environment)
s.orderExecutor.BindProfitStats(s.ProfitStats)
s.orderExecutor.Bind()
s.orderExecutor.TradeCollector().OnTrade(func(trade types.Trade, _, _ fixedpoint.Value) {
s.GridProfitStats.AddTrade(trade)
})
s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) {
bbgo.Sync(ctx, s)
})
s.orderExecutor.ActiveMakerOrders().OnFilled(s.handleOrderFilled)
// TODO: detect if there are previous grid orders on the order book