Merge pull request #1703 from c9s/c9s/core/position-metrics

FEATURE: [core] add position metrics
This commit is contained in:
c9s 2024-08-22 13:32:39 +08:00 committed by GitHub
commit bb06a6a046
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 111 additions and 47 deletions

View File

@ -2,9 +2,10 @@ package binance
import ( import (
"fmt" "fmt"
"github.com/c9s/bbgo/pkg/exchange/binance/binanceapi"
"time" "time"
"github.com/c9s/bbgo/pkg/exchange/binance/binanceapi"
"github.com/adshao/go-binance/v2/futures" "github.com/adshao/go-binance/v2/futures"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -42,11 +43,10 @@ func toGlobalFuturesPositions(futuresPositions []*binanceapi.FuturesAccountPosit
retFuturesPositions := make(types.FuturesPositionMap) retFuturesPositions := make(types.FuturesPositionMap)
for _, futuresPosition := range futuresPositions { for _, futuresPosition := range futuresPositions {
retFuturesPositions[futuresPosition.Symbol] = types.FuturesPosition{ // TODO: types.FuturesPosition retFuturesPositions[futuresPosition.Symbol] = types.FuturesPosition{ // TODO: types.FuturesPosition
Isolated: futuresPosition.Isolated, Isolated: futuresPosition.Isolated,
AverageCost: fixedpoint.MustNewFromString(futuresPosition.EntryPrice), AverageCost: fixedpoint.MustNewFromString(futuresPosition.EntryPrice),
ApproximateAverageCost: fixedpoint.MustNewFromString(futuresPosition.EntryPrice), Base: fixedpoint.MustNewFromString(futuresPosition.PositionAmt),
Base: fixedpoint.MustNewFromString(futuresPosition.PositionAmt), Quote: fixedpoint.MustNewFromString(futuresPosition.Notional),
Quote: fixedpoint.MustNewFromString(futuresPosition.Notional),
PositionRisk: &types.PositionRisk{ PositionRisk: &types.PositionRisk{
Leverage: fixedpoint.MustNewFromString(futuresPosition.Leverage), Leverage: fixedpoint.MustNewFromString(futuresPosition.Leverage),

View File

@ -854,12 +854,12 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
s.highestPrice = 0 s.highestPrice = 0
s.lowestPrice = 0 s.lowestPrice = 0
} else if s.Position.IsLong() { } else if s.Position.IsLong() {
s.buyPrice = s.Position.ApproximateAverageCost.Float64() s.buyPrice = s.Position.AverageCost.Float64()
s.sellPrice = 0 s.sellPrice = 0
s.highestPrice = math.Max(s.buyPrice, s.highestPrice) s.highestPrice = math.Max(s.buyPrice, s.highestPrice)
s.lowestPrice = s.buyPrice s.lowestPrice = s.buyPrice
} else if s.Position.IsShort() { } else if s.Position.IsShort() {
s.sellPrice = s.Position.ApproximateAverageCost.Float64() s.sellPrice = s.Position.AverageCost.Float64()
s.buyPrice = 0 s.buyPrice = 0
s.highestPrice = s.sellPrice s.highestPrice = s.sellPrice
if s.lowestPrice == 0 { if s.lowestPrice == 0 {

View File

@ -354,12 +354,12 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
s.highestPrice = 0 s.highestPrice = 0
s.lowestPrice = 0 s.lowestPrice = 0
} else if s.Position.IsLong() { } else if s.Position.IsLong() {
s.buyPrice = s.Position.ApproximateAverageCost.Float64() s.buyPrice = s.Position.AverageCost.Float64()
s.sellPrice = 0 s.sellPrice = 0
s.highestPrice = math.Max(s.buyPrice, s.highestPrice) s.highestPrice = math.Max(s.buyPrice, s.highestPrice)
s.lowestPrice = 0 s.lowestPrice = 0
} else { } else {
s.sellPrice = s.Position.ApproximateAverageCost.Float64() s.sellPrice = s.Position.AverageCost.Float64()
s.buyPrice = 0 s.buyPrice = 0
s.highestPrice = 0 s.highestPrice = 0
if s.lowestPrice == 0 { if s.lowestPrice == 0 {

View File

@ -5,6 +5,7 @@ import (
"sync" "sync"
"time" "time"
"github.com/prometheus/client_golang/prometheus"
"github.com/slack-go/slack" "github.com/slack-go/slack"
"github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/fixedpoint"
@ -43,16 +44,16 @@ type Position struct {
Quote fixedpoint.Value `json:"quote" db:"quote"` Quote fixedpoint.Value `json:"quote" db:"quote"`
AverageCost fixedpoint.Value `json:"averageCost" db:"average_cost"` AverageCost fixedpoint.Value `json:"averageCost" db:"average_cost"`
// ApproximateAverageCost adds the computed fee in quote in the average cost
// This is used for calculating net profit
ApproximateAverageCost fixedpoint.Value `json:"approximateAverageCost"`
FeeRate *ExchangeFee `json:"feeRate,omitempty"` FeeRate *ExchangeFee `json:"feeRate,omitempty"`
ExchangeFeeRates map[ExchangeName]ExchangeFee `json:"exchangeFeeRates"` ExchangeFeeRates map[ExchangeName]ExchangeFee `json:"exchangeFeeRates"`
// TotalFee stores the fee currency -> total fee quantity // TotalFee stores the fee currency -> total fee quantity
TotalFee map[string]fixedpoint.Value `json:"totalFee" db:"-"` TotalFee map[string]fixedpoint.Value `json:"totalFee" db:"-"`
// FeeAverageCosts stores the fee currency -> average cost of the fee
// e.g. BNB -> 341.0
FeeAverageCosts map[string]fixedpoint.Value `json:"feeAverageCosts" db:"-"`
OpenedAt time.Time `json:"openedAt,omitempty" db:"-"` OpenedAt time.Time `json:"openedAt,omitempty" db:"-"`
ChangedAt time.Time `json:"changedAt,omitempty" db:"changed_at"` ChangedAt time.Time `json:"changedAt,omitempty" db:"changed_at"`
@ -277,10 +278,6 @@ type FuturesPosition struct {
Quote fixedpoint.Value `json:"quote"` Quote fixedpoint.Value `json:"quote"`
AverageCost fixedpoint.Value `json:"averageCost"` AverageCost fixedpoint.Value `json:"averageCost"`
// ApproximateAverageCost adds the computed fee in quote in the average cost
// This is used for calculating net profit
ApproximateAverageCost fixedpoint.Value `json:"approximateAverageCost"`
FeeRate *ExchangeFee `json:"feeRate,omitempty"` FeeRate *ExchangeFee `json:"feeRate,omitempty"`
ExchangeFeeRates map[ExchangeName]ExchangeFee `json:"exchangeFeeRates"` ExchangeFeeRates map[ExchangeName]ExchangeFee `json:"exchangeFeeRates"`
@ -306,7 +303,10 @@ func NewPositionFromMarket(market Market) *Position {
BaseCurrency: market.BaseCurrency, BaseCurrency: market.BaseCurrency,
QuoteCurrency: market.QuoteCurrency, QuoteCurrency: market.QuoteCurrency,
Market: market, Market: market,
TotalFee: make(map[string]fixedpoint.Value),
FeeAverageCosts: make(map[string]fixedpoint.Value),
TotalFee: make(map[string]fixedpoint.Value),
ExchangeFeeRates: make(map[ExchangeName]ExchangeFee),
} }
} }
@ -315,7 +315,10 @@ func NewPosition(symbol, base, quote string) *Position {
Symbol: symbol, Symbol: symbol,
BaseCurrency: base, BaseCurrency: base,
QuoteCurrency: quote, QuoteCurrency: quote,
TotalFee: make(map[string]fixedpoint.Value),
TotalFee: make(map[string]fixedpoint.Value),
FeeAverageCosts: make(map[string]fixedpoint.Value),
ExchangeFeeRates: make(map[ExchangeName]ExchangeFee),
} }
} }
@ -345,6 +348,10 @@ func (p *Position) SetExchangeFeeRate(ex ExchangeName, exchangeFee ExchangeFee)
p.ExchangeFeeRates[ex] = exchangeFee p.ExchangeFeeRates[ex] = exchangeFee
} }
func (p *Position) SetFeeAverageCost(currency string, cost fixedpoint.Value) {
p.FeeAverageCosts[currency] = cost
}
func (p *Position) IsShort() bool { func (p *Position) IsShort() bool {
return p.Base.Sign() < 0 return p.Base.Sign() < 0
} }
@ -489,6 +496,34 @@ func (p *Position) AddTrades(trades []Trade) (fixedpoint.Value, fixedpoint.Value
return totalProfitAmount, totalNetProfit, !totalProfitAmount.IsZero() return totalProfitAmount, totalNetProfit, !totalProfitAmount.IsZero()
} }
func (p *Position) calculateFeeInQuote(td Trade) fixedpoint.Value {
var quoteQuantity = td.QuoteQuantity
if cost, ok := p.FeeAverageCosts[td.FeeCurrency]; ok {
return td.Fee.Mul(cost)
}
if p.ExchangeFeeRates != nil {
if exchangeFee, ok := p.ExchangeFeeRates[td.Exchange]; ok {
if td.IsMaker {
return exchangeFee.MakerFeeRate.Mul(quoteQuantity)
} else {
return exchangeFee.TakerFeeRate.Mul(quoteQuantity)
}
}
}
if p.FeeRate != nil {
if td.IsMaker {
return p.FeeRate.MakerFeeRate.Mul(quoteQuantity)
} else {
return p.FeeRate.TakerFeeRate.Mul(quoteQuantity)
}
}
return fixedpoint.Zero
}
func (p *Position) AddTrade(td Trade) (profit fixedpoint.Value, netProfit fixedpoint.Value, madeProfit bool) { func (p *Position) AddTrade(td Trade) (profit fixedpoint.Value, netProfit fixedpoint.Value, madeProfit bool) {
price := td.Price price := td.Price
quantity := td.Quantity quantity := td.Quantity
@ -502,6 +537,7 @@ func (p *Position) AddTrade(td Trade) (profit fixedpoint.Value, netProfit fixedp
switch td.FeeCurrency { switch td.FeeCurrency {
case p.BaseCurrency: case p.BaseCurrency:
// USD-M futures use the quote currency as the fee currency.
if !td.IsFutures { if !td.IsFutures {
quantity = quantity.Sub(fee) quantity = quantity.Sub(fee)
} }
@ -513,27 +549,15 @@ func (p *Position) AddTrade(td Trade) (profit fixedpoint.Value, netProfit fixedp
default: default:
if !td.Fee.IsZero() { if !td.Fee.IsZero() {
if p.ExchangeFeeRates != nil { feeInQuote = p.calculateFeeInQuote(td)
if exchangeFee, ok := p.ExchangeFeeRates[td.Exchange]; ok {
if td.IsMaker {
feeInQuote = feeInQuote.Add(exchangeFee.MakerFeeRate.Mul(quoteQuantity))
} else {
feeInQuote = feeInQuote.Add(exchangeFee.TakerFeeRate.Mul(quoteQuantity))
}
}
} else if p.FeeRate != nil {
if td.IsMaker {
feeInQuote = feeInQuote.Add(p.FeeRate.MakerFeeRate.Mul(quoteQuantity))
} else {
feeInQuote = feeInQuote.Add(p.FeeRate.TakerFeeRate.Mul(quoteQuantity))
}
}
} }
} }
p.Lock() p.Lock()
defer p.Unlock() defer p.Unlock()
defer p.updateMetrics()
// update changedAt field before we unlock in the defer func // update changedAt field before we unlock in the defer func
defer func() { defer func() {
p.ChangedAt = td.Time.Time() p.ChangedAt = td.Time.Time()
@ -551,11 +575,10 @@ func (p *Position) AddTrade(td Trade) (profit fixedpoint.Value, netProfit fixedp
// convert short position to long position // convert short position to long position
if p.Base.Add(quantity).Sign() > 0 { if p.Base.Add(quantity).Sign() > 0 {
profit = p.AverageCost.Sub(price).Mul(p.Base.Neg()) profit = p.AverageCost.Sub(price).Mul(p.Base.Neg())
netProfit = p.ApproximateAverageCost.Sub(price).Mul(p.Base.Neg()).Sub(feeInQuote) netProfit = p.AverageCost.Sub(price).Mul(p.Base.Neg()).Sub(feeInQuote)
p.Base = p.Base.Add(quantity) p.Base = p.Base.Add(quantity)
p.Quote = p.Quote.Sub(quoteQuantity) p.Quote = p.Quote.Sub(quoteQuantity)
p.AverageCost = price p.AverageCost = price
p.ApproximateAverageCost = price
p.AccumulatedProfit = p.AccumulatedProfit.Add(profit) p.AccumulatedProfit = p.AccumulatedProfit.Add(profit)
p.OpenedAt = td.Time.Time() p.OpenedAt = td.Time.Time()
return profit, netProfit, true return profit, netProfit, true
@ -564,7 +587,7 @@ func (p *Position) AddTrade(td Trade) (profit fixedpoint.Value, netProfit fixedp
p.Base = p.Base.Add(quantity) p.Base = p.Base.Add(quantity)
p.Quote = p.Quote.Sub(quoteQuantity) p.Quote = p.Quote.Sub(quoteQuantity)
profit = p.AverageCost.Sub(price).Mul(quantity) profit = p.AverageCost.Sub(price).Mul(quantity)
netProfit = p.ApproximateAverageCost.Sub(price).Mul(quantity).Sub(feeInQuote) netProfit = p.AverageCost.Sub(price).Mul(quantity).Sub(feeInQuote)
p.AccumulatedProfit = p.AccumulatedProfit.Add(profit) p.AccumulatedProfit = p.AccumulatedProfit.Add(profit)
return profit, netProfit, true return profit, netProfit, true
} }
@ -578,11 +601,12 @@ func (p *Position) AddTrade(td Trade) (profit fixedpoint.Value, netProfit fixedp
// here the case is: base == 0 or base > 0 // here the case is: base == 0 or base > 0
divisor := p.Base.Add(quantity) divisor := p.Base.Add(quantity)
p.ApproximateAverageCost = p.ApproximateAverageCost.Mul(p.Base).
p.AverageCost = p.AverageCost.Mul(p.Base).
Add(quoteQuantity). Add(quoteQuantity).
Add(feeInQuote). Add(feeInQuote).
Div(divisor) Div(divisor)
p.AverageCost = p.AverageCost.Mul(p.Base).Add(quoteQuantity).Div(divisor)
p.Base = p.Base.Add(quantity) p.Base = p.Base.Add(quantity)
p.Quote = p.Quote.Sub(quoteQuantity) p.Quote = p.Quote.Sub(quoteQuantity)
return fixedpoint.Zero, fixedpoint.Zero, false return fixedpoint.Zero, fixedpoint.Zero, false
@ -593,11 +617,10 @@ func (p *Position) AddTrade(td Trade) (profit fixedpoint.Value, netProfit fixedp
// convert long position to short position // convert long position to short position
if p.Base.Compare(quantity) < 0 { if p.Base.Compare(quantity) < 0 {
profit = price.Sub(p.AverageCost).Mul(p.Base) profit = price.Sub(p.AverageCost).Mul(p.Base)
netProfit = price.Sub(p.ApproximateAverageCost).Mul(p.Base).Sub(feeInQuote) netProfit = price.Sub(p.AverageCost).Mul(p.Base).Sub(feeInQuote)
p.Base = p.Base.Sub(quantity) p.Base = p.Base.Sub(quantity)
p.Quote = p.Quote.Add(quoteQuantity) p.Quote = p.Quote.Add(quoteQuantity)
p.AverageCost = price p.AverageCost = price
p.ApproximateAverageCost = price
p.AccumulatedProfit = p.AccumulatedProfit.Add(profit) p.AccumulatedProfit = p.AccumulatedProfit.Add(profit)
p.OpenedAt = td.Time.Time() p.OpenedAt = td.Time.Time()
return profit, netProfit, true return profit, netProfit, true
@ -605,7 +628,7 @@ func (p *Position) AddTrade(td Trade) (profit fixedpoint.Value, netProfit fixedp
p.Base = p.Base.Sub(quantity) p.Base = p.Base.Sub(quantity)
p.Quote = p.Quote.Add(quoteQuantity) p.Quote = p.Quote.Add(quoteQuantity)
profit = price.Sub(p.AverageCost).Mul(quantity) profit = price.Sub(p.AverageCost).Mul(quantity)
netProfit = price.Sub(p.ApproximateAverageCost).Mul(quantity).Sub(feeInQuote) netProfit = price.Sub(p.AverageCost).Mul(quantity).Sub(feeInQuote)
p.AccumulatedProfit = p.AccumulatedProfit.Add(profit) p.AccumulatedProfit = p.AccumulatedProfit.Add(profit)
return profit, netProfit, true return profit, netProfit, true
} }
@ -619,13 +642,10 @@ func (p *Position) AddTrade(td Trade) (profit fixedpoint.Value, netProfit fixedp
// handling short position, since Base here is negative we need to reverse the sign // handling short position, since Base here is negative we need to reverse the sign
divisor := quantity.Sub(p.Base) divisor := quantity.Sub(p.Base)
p.ApproximateAverageCost = p.ApproximateAverageCost.Mul(p.Base.Neg()).
Add(quoteQuantity).
Sub(feeInQuote).
Div(divisor)
p.AverageCost = p.AverageCost.Mul(p.Base.Neg()). p.AverageCost = p.AverageCost.Mul(p.Base.Neg()).
Add(quoteQuantity). Add(quoteQuantity).
Sub(feeInQuote).
Div(divisor) Div(divisor)
p.Base = p.Base.Sub(quantity) p.Base = p.Base.Sub(quantity)
p.Quote = p.Quote.Add(quoteQuantity) p.Quote = p.Quote.Add(quoteQuantity)
@ -635,3 +655,18 @@ func (p *Position) AddTrade(td Trade) (profit fixedpoint.Value, netProfit fixedp
return fixedpoint.Zero, fixedpoint.Zero, false return fixedpoint.Zero, fixedpoint.Zero, false
} }
func (p *Position) updateMetrics() {
// update the position metrics only if the position defines the strategy ID
if p.StrategyInstanceID == "" || p.Strategy == "" {
return
}
labels := prometheus.Labels{
"strategy_id": p.StrategyInstanceID,
"strategy_type": p.Strategy,
}
positionAverageCostMetrics.With(labels).Set(p.AverageCost.Float64())
positionBaseQuantityMetrics.With(labels).Set(p.Base.Float64())
positionQuoteQuantityMetrics.With(labels).Set(p.Quote.Float64())
}

View File

@ -0,0 +1,29 @@
package types
import "github.com/prometheus/client_golang/prometheus"
var positionAverageCostMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "bbgo_position_avg_cost",
Help: "bbgo position average cost metrics",
}, []string{"strategy_id", "strategy_type", "symbol"})
var positionBaseQuantityMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "bbgo_position_base_qty",
Help: "bbgo position base quantity metrics",
}, []string{"strategy_id", "strategy_type", "symbol"})
var positionQuoteQuantityMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "bbgo_position_quote_qty",
Help: "bbgo position quote quantity metrics",
}, []string{"strategy_id", "strategy_type", "symbol"})
func init() {
prometheus.MustRegister(
positionAverageCostMetrics,
positionBaseQuantityMetrics,
positionQuoteQuantityMetrics,
)
}