xmaker: improve hedge account credit calculation

This commit is contained in:
c9s 2024-09-01 00:58:50 +08:00
parent cff7103ece
commit d85da78e17
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -503,6 +503,10 @@ func (s *Strategy) updateQuote(ctx context.Context) {
!hedgeAccount.MarginLevel.IsZero() { !hedgeAccount.MarginLevel.IsZero() {
if hedgeAccount.MarginLevel.Compare(s.MinMarginLevel) < 0 { if hedgeAccount.MarginLevel.Compare(s.MinMarginLevel) < 0 {
s.logger.Infof("hedge account margin level %s is less then the min margin level %s, calculating the borrowed positions",
hedgeAccount.MarginLevel.String(),
s.MinMarginLevel.String())
if quote, ok := hedgeAccount.Balance(s.sourceMarket.QuoteCurrency); ok { if quote, ok := hedgeAccount.Balance(s.sourceMarket.QuoteCurrency); ok {
quoteDebt := quote.Debt() quoteDebt := quote.Debt()
if quoteDebt.Sign() > 0 { if quoteDebt.Sign() > 0 {
@ -517,22 +521,41 @@ func (s *Strategy) updateQuote(ctx context.Context) {
} }
} }
} else { } else {
// credit buffer s.logger.Infof("hedge account margin level %s is greater than the min margin level %s, calculating the net value",
creditBufferRatio := fixedpoint.NewFromFloat(1.2) hedgeAccount.MarginLevel.String(),
s.MinMarginLevel.String())
netValueInUsd, calcErr := s.accountValueCalculator.NetValue(ctx)
if calcErr != nil {
s.logger.WithError(calcErr).Errorf("unable to calculate the net value")
} else {
// calculate credit buffer
s.logger.Infof("hedge account net value in usd: %f", netValueInUsd.Float64())
if quote, ok := hedgeAccount.Balance(s.sourceMarket.QuoteCurrency); ok { if quote, ok := hedgeAccount.Balance(s.sourceMarket.QuoteCurrency); ok {
netQuote := quote.Net() debt := quote.Debt()
if netQuote.Sign() > 0 { quota := netValueInUsd.Sub(debt)
hedgeQuota.QuoteAsset.Add(netQuote.Mul(creditBufferRatio))
} s.logger.Infof("hedge account quote balance: %s, debt: %s, quota: %s",
quote.String(),
debt.String(),
quota.String())
hedgeQuota.QuoteAsset.Add(quota)
} }
if base, ok := hedgeAccount.Balance(s.sourceMarket.BaseCurrency); ok { if base, ok := hedgeAccount.Balance(s.sourceMarket.BaseCurrency); ok {
netBase := base.Net() debt := base.Debt()
if netBase.Sign() > 0 { quota := netValueInUsd.Div(bestAsk.Price).Sub(debt)
hedgeQuota.BaseAsset.Add(netBase.Mul(creditBufferRatio))
s.logger.Infof("hedge account base balance: %s, debt: %s, quota: %s",
base.String(),
debt.String(),
quota.String())
hedgeQuota.BaseAsset.Add(quota)
} }
} }
// netValueInUsd, err := s.accountValueCalculator.NetValue(ctx)
} }
} else { } else {