Skip to content

Commit ce3210b

Browse files
authored
fix: preserve BNFCR in zero-balance filter and emit MiCA warning (#74)
* fix: use availableBalance for BNFCR in FutureBalance For EU/EEA accounts under Binance MiCA Credits Trading Mode, BNFCR walletBalance can be negative (fees and PnL are charged in BNFCR). Binance stores the total cross-margin available balance — aggregating all stablecoins minus used margin — in availableBalance on the BNFCR asset entry. Using availableBalance as Amount for BNFCR ensures the BinanceCryptoFutureMarginModel receives the correct collateral figure and the > 0 guard passes for accounts with sufficient collateral. * refactor: preserve BNFCR in zero-balance filter, revert availableBalance - Add BNFCR exception to the zero-balance filter in BinanceBaseRestApiClient so BNFCR always passes through even when walletBalance=0 - Remove manual BNFCR injection in BinanceFuturesRestApiClient (no longer needed) - Revert FutureBalance.Amount to use walletBalance only (availableBalance causes double-counting with Lean's own PnL/margin tracking) * feat: emit BrokerageMessageEvent warning when BNFCR detected in account balances * fix: update MiCA warning message to reflect all collateral assets
1 parent e1d3bd9 commit ce3210b

3 files changed

Lines changed: 9 additions & 7 deletions

File tree

QuantConnect.BinanceBrokerage/BinanceBaseRestApiClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ protected BalanceEntry[] GetCashBalance(string apiPrefix)
190190
return JsonConvert
191191
.DeserializeObject<AccountInformation>(response.Content, CreateAccountConverter())
192192
.Balances
193-
.Where(s => s.Amount != 0)
193+
.Where(s => s.Amount != 0 || s.Asset.Equals("BNFCR", StringComparison.InvariantCultureIgnoreCase))
194194
.ToArray();
195195
}
196196

QuantConnect.BinanceBrokerage/BinanceBrokerage.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,13 @@ public override List<CashAmount> GetCashBalance()
235235
if (balances == null || !balances.Any())
236236
return new List<CashAmount>();
237237

238+
if (balances.Any(b => b.Asset.Equals("BNFCR", StringComparison.InvariantCultureIgnoreCase)))
239+
{
240+
OnMessage(new BrokerageMessageEvent(BrokerageMessageType.Warning, "BinanceFuturesCreditsTradingMode",
241+
"Binance Futures Credits Trading Mode detected. " +
242+
"Margin will be calculated using all available collateral assets in the account."));
243+
}
244+
238245
return balances
239246
.Select(b => new CashAmount(b.Amount, b.Asset.LazyToUpper()))
240247
.ToList();

QuantConnect.BinanceBrokerage/BinanceFuturesRestApiClient.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,7 @@ public override List<Holding> GetAccountHoldings()
100100

101101
public override BalanceEntry[] GetCashBalance()
102102
{
103-
var balances = GetCashBalance(_prefixV2) ?? [];
104-
if (!balances.Any(x => x.Asset.Equals("BNFCR", StringComparison.InvariantCultureIgnoreCase)))
105-
{
106-
balances = balances.Concat([new FutureBalance() { Asset = "BNFCR", WalletBalance = 0 }]).ToArray();
107-
}
108-
return balances;
103+
return GetCashBalance(_prefixV2) ?? [];
109104
}
110105

111106
/// <summary>

0 commit comments

Comments
 (0)