Skip to content

Commit db0e423

Browse files
committed
📝 feat(position): fee conversion from quote to USD, add callback function
1 parent 897dcc9 commit db0e423

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

‎pkg/types/position.go‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ type Position struct {
8888

8989
// ttl is the ttl to keep in persistence
9090
ttl time.Duration
91+
92+
// quote to USD price map cache
93+
quotePriceInUsd QuotePrice
94+
}
95+
96+
type QuotePrice struct {
97+
Price fixedpoint.Value
98+
Time time.Time
9199
}
92100

93101
type PositionKey struct {
@@ -574,8 +582,14 @@ func (p *Position) AddTrades(trades []Trade) (fixedpoint.Value, fixedpoint.Value
574582
}
575583

576584
func (p *Position) calculateFeeInQuote(td Trade) fixedpoint.Value {
585+
// assume the fee is not charged either in base currency or quote currency
577586
var quoteQuantity = td.QuoteQuantity
578587

588+
// check if we have the quote price in USD
589+
if !p.quotePriceInUsd.Price.IsZero() && td.FeeCurrency == "USD" {
590+
return td.Fee.Div(p.quotePriceInUsd.Price)
591+
}
592+
579593
if cost, ok := p.FeeAverageCosts[td.FeeCurrency]; ok {
580594
return td.Fee.Mul(cost)
581595
}
@@ -758,3 +772,16 @@ func (p *Position) updateMetrics() {
758772
positionBaseQuantityMetrics.With(labels).Set(p.Base.Float64())
759773
positionQuoteQuantityMetrics.With(labels).Set(p.Quote.Float64())
760774
}
775+
776+
func (p *Position) UpdateQuteUsdPrice(kline KLine) {
777+
p.Lock()
778+
defer p.Unlock()
779+
780+
if p.quotePriceInUsd.Time.After(kline.EndTime.Time()) {
781+
return
782+
}
783+
p.quotePriceInUsd = QuotePrice{
784+
Price: kline.Close,
785+
Time: kline.EndTime.Time(),
786+
}
787+
}

0 commit comments

Comments
 (0)