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(),
if quote, ok := hedgeAccount.Balance(s.sourceMarket.QuoteCurrency); ok { s.MinMarginLevel.String())
netQuote := quote.Net()
if netQuote.Sign() > 0 {
hedgeQuota.QuoteAsset.Add(netQuote.Mul(creditBufferRatio))
}
}
if base, ok := hedgeAccount.Balance(s.sourceMarket.BaseCurrency); ok { netValueInUsd, calcErr := s.accountValueCalculator.NetValue(ctx)
netBase := base.Net() if calcErr != nil {
if netBase.Sign() > 0 { s.logger.WithError(calcErr).Errorf("unable to calculate the net value")
hedgeQuota.BaseAsset.Add(netBase.Mul(creditBufferRatio)) } 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 {
debt := quote.Debt()
quota := netValueInUsd.Sub(debt)
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 {
debt := base.Debt()
quota := netValueInUsd.Div(bestAsk.Price).Sub(debt)
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 {