types: add fee cost settter to the position

This commit is contained in:
c9s 2024-08-22 10:59:38 +08:00
parent 9136877207
commit e2d68f2a86
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 19 additions and 4 deletions

View File

@ -54,6 +54,10 @@ type Position struct {
// TotalFee stores the fee currency -> total fee quantity
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:"-"`
ChangedAt time.Time `json:"changedAt,omitempty" db:"changed_at"`
@ -307,7 +311,10 @@ func NewPositionFromMarket(market Market) *Position {
BaseCurrency: market.BaseCurrency,
QuoteCurrency: market.QuoteCurrency,
Market: market,
FeeAverageCosts: make(map[string]fixedpoint.Value),
TotalFee: make(map[string]fixedpoint.Value),
ExchangeFeeRates: make(map[ExchangeName]ExchangeFee),
}
}
@ -316,7 +323,10 @@ func NewPosition(symbol, base, quote string) *Position {
Symbol: symbol,
BaseCurrency: base,
QuoteCurrency: quote,
TotalFee: make(map[string]fixedpoint.Value),
FeeAverageCosts: make(map[string]fixedpoint.Value),
ExchangeFeeRates: make(map[ExchangeName]ExchangeFee),
}
}
@ -346,6 +356,10 @@ func (p *Position) SetExchangeFeeRate(ex ExchangeName, exchangeFee ExchangeFee)
p.ExchangeFeeRates[ex] = exchangeFee
}
func (p *Position) SetFeeAverageCost(currency string, cost fixedpoint.Value) {
p.FeeAverageCosts[currency] = cost
}
func (p *Position) IsShort() bool {
return p.Base.Sign() < 0
}
@ -640,6 +654,7 @@ func (p *Position) AddTrade(td Trade) (profit fixedpoint.Value, netProfit fixedp
}
func (p *Position) updateMetrics() {
// update the position metrics only if the position defines the strategy ID
if p.StrategyInstanceID == "" || p.Strategy == "" {
return
}

View File

@ -16,8 +16,8 @@ var positionBaseQuantityMetrics = prometheus.NewGaugeVec(
var positionQuoteQuantityMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "bbgo_position_base_qty",
Help: "bbgo position base quantity metrics",
Name: "bbgo_position_quote_qty",
Help: "bbgo position quote quantity metrics",
}, []string{"strategy_id", "strategy_type", "symbol"})
func init() {