@@ -103,6 +103,50 @@ func (c *HedgeMarketConfig) Defaults() error {
103103 return nil
104104}
105105
106+ // ResolveQuotingDepthInQuote returns the quoting depth in quote currency.
107+ // Priority:
108+ // 1. If QuotingDepthInQuote is configured (> 0), return it directly.
109+ // 2. Else if QuotingDepth (base) is configured and latestPrice > 0, convert
110+ // base depth to quote via base * latestPrice.
111+ // 3. Otherwise return zero.
112+ func (c * HedgeMarketConfig ) ResolveQuotingDepthInQuote (latestPrice fixedpoint.Value ) fixedpoint.Value {
113+ if c == nil {
114+ return fixedpoint .Zero
115+ }
116+
117+ if c .QuotingDepthInQuote .Sign () > 0 {
118+ return c .QuotingDepthInQuote
119+ }
120+
121+ if c .QuotingDepth .Sign () > 0 && latestPrice .Sign () > 0 {
122+ return c .QuotingDepth .Mul (latestPrice )
123+ }
124+
125+ return fixedpoint .Zero
126+ }
127+
128+ // ResolveQuotingDepthInBase returns the quoting depth in base currency.
129+ // Priority:
130+ // 1. If QuotingDepth (base) is configured (> 0), return it directly.
131+ // 2. Else if QuotingDepthInQuote is configured and latestPrice > 0, convert
132+ // quote depth to base via quote / latestPrice.
133+ // 3. Otherwise return zero.
134+ func (c * HedgeMarketConfig ) ResolveQuotingDepthInBase (latestPrice fixedpoint.Value ) fixedpoint.Value {
135+ if c == nil {
136+ return fixedpoint .Zero
137+ }
138+
139+ if c .QuotingDepth .Sign () > 0 {
140+ return c .QuotingDepth
141+ }
142+
143+ if c .QuotingDepthInQuote .Sign () > 0 && latestPrice .Sign () > 0 {
144+ return c .QuotingDepthInQuote .Div (latestPrice )
145+ }
146+
147+ return fixedpoint .Zero
148+ }
149+
106150func InitializeHedgeMarketFromConfig (
107151 c * HedgeMarketConfig ,
108152 sessions map [string ]* bbgo.ExchangeSession ,
@@ -459,9 +503,44 @@ func (m *HedgeMarket) GetQuotePriceBySessionBalances() (bid, ask fixedpoint.Valu
459503 // fetch available balances once
460504 baseAvail , quoteAvail := m .GetBaseQuoteAvailableBalances ()
461505
506+ // determine a latest price for converting depths between base and quote
507+ bestBid := m .bidPricer (0 , fixedpoint .Zero )
508+ bestAsk := m .askPricer (0 , fixedpoint .Zero )
509+ latest := fixedpoint .Zero
510+ if bestBid .Sign () > 0 && bestAsk .Sign () > 0 {
511+ latest = bestBid .Add (bestAsk ).Div (fixedpoint .NewFromFloat (2 ))
512+ } else if bestBid .Sign () > 0 {
513+ latest = bestBid
514+ } else if bestAsk .Sign () > 0 {
515+ latest = bestAsk
516+ }
517+
518+ // resolve configured quoting depths in both currencies
519+ cfgQuoteDepth := m .HedgeMarketConfig .ResolveQuotingDepthInQuote (latest )
520+ cfgBaseDepth := m .HedgeMarketConfig .ResolveQuotingDepthInBase (latest )
521+
522+ // use min(balance, configuredDepth) per requirement
523+ baseDepthToUse := baseAvail
524+ if cfgBaseDepth .Sign () > 0 && baseAvail .Compare (cfgBaseDepth ) > 0 {
525+ baseDepthToUse = cfgBaseDepth
526+ }
527+
528+ quoteDepthToUse := quoteAvail
529+ if cfgQuoteDepth .Sign () > 0 && quoteAvail .Compare (cfgQuoteDepth ) > 0 {
530+ quoteDepthToUse = cfgQuoteDepth
531+ }
532+
462533 // compute raw prices using side-specific depth definitions
463- rawBid := m .depthBook .PriceAtDepth (types .SideTypeBuy , baseAvail )
464- rawAsk := m .depthBook .PriceAtQuoteDepth (types .SideTypeSell , quoteAvail )
534+ rawBid := m .depthBook .PriceAtDepth (types .SideTypeBuy , baseDepthToUse )
535+ rawAsk := m .depthBook .PriceAtQuoteDepth (types .SideTypeSell , quoteDepthToUse )
536+
537+ // fallback to best prices if depth-based prices are not available
538+ if rawBid .IsZero () {
539+ rawBid = bestBid
540+ }
541+ if rawAsk .IsZero () {
542+ rawAsk = bestAsk
543+ }
465544
466545 // Apply fee according to PriceFeeMode
467546 feeRate := m .priceFeeRate ()
@@ -472,8 +551,12 @@ func (m *HedgeMarket) GetQuotePriceBySessionBalances() (bid, ask fixedpoint.Valu
472551 if bid .IsZero () || ask .IsZero () {
473552 bids := m .book .SideBook (types .SideTypeBuy )
474553 asks := m .book .SideBook (types .SideTypeSell )
475- m .logger .Warnf ("no valid bid/ask price from session balances for %s, baseAvail=%s, quoteAvail=%s, bids: %v, asks: %v" ,
476- m .SymbolSelector , baseAvail .String (), quoteAvail .String (), bids , asks )
554+ m .logger .Warnf ("no valid bid/ask price from session balances for %s, baseAvail=%s, quoteAvail=%s, cfgBaseDepth=%s, cfgQuoteDepth=%s, usedBaseDepth=%s, usedQuoteDepth=%s, bids: %v, asks: %v" ,
555+ m .SymbolSelector ,
556+ baseAvail .String (), quoteAvail .String (),
557+ cfgBaseDepth .String (), cfgQuoteDepth .String (),
558+ baseDepthToUse .String (), quoteDepthToUse .String (),
559+ bids , asks )
477560 }
478561
479562 // store prices as snapshot
0 commit comments