QuantConnect / Lean.Brokerages.Binance

Binance Brokerage Plugin
Apache License 2.0
3 stars 17 forks source link

Margin Account Should Not Use Cached Holdings #24

Closed AlexCatarino closed 12 months ago

AlexCatarino commented 1 year ago

Expected Behavior

If we deploy to a margin account, we should never read the cached holdings.

Actual Behavior

When we deploy the algorithm to a cash account, ApiClient.GetAccountHoldings(); always returns an empty list, so we should use the cashed holdings: base.GetAccountHoldings(_job?.BrokerageData, _algorithm.Securities.Values);. If we deploy to a margin account, and get an empty list from Binance, we use cached values too.

Potential Solution

If we are trading a margin account, GetAccountHoldings should never use cached values.

Reproducing the Problem

The problem below is from a Cash case. it's correct, but the UI doesn't let us remove the cached position.

Buy BTCUSDT, then stop the algorithm. Redeploy. Verify the BTCUSDT position is loaded. Stop the algorithm. Close the project, deploy it, and verify that there is no BTCUSDT position. So the same account returns two different states.

2023-06-30T21:28:03.0585556Z TRACE:: BrokerageSetupHandler.Setup(): Setting BTC cash to 0.00126482
2023-06-30T21:28:03.0585999Z TRACE:: BrokerageSetupHandler.Setup(): Setting TUSD cash to 661.13740660
2023-06-30T21:28:03.0646670Z TRACE:: BrokerageSetupHandler.Setup(): Fetching open orders from brokerage...
2023-06-30T21:28:03.2394701Z TRACE:: BrokerageSetupHandler.Setup(): Fetching holdings from brokerage...
2023-06-30T21:28:03.2458444Z TRACE:: Brokerage.GetAccountHoldings(): sourcing holdings from provided brokerage data, found 1 entries
2023-06-30T21:28:03.2476051Z TRACE:: BrokerageSetupHandler.Setup(): Has existing holding: BTCTUSD: -0.002 @ $30548.01 - Market: $0 - Conversion: 
2023-06-30T21:26:38.1517871Z TRACE:: BrokerageSetupHandler.Setup(): Setting BTC cash to 0.00126482
2023-06-30T21:26:38.1518244Z TRACE:: BrokerageSetupHandler.Setup(): Setting TUSD cash to 661.13740660
2023-06-30T21:26:38.1566469Z TRACE:: BrokerageSetupHandler.Setup(): Fetching open orders from brokerage...
2023-06-30T21:26:38.3464017Z TRACE:: BrokerageSetupHandler.Setup(): Fetching holdings from brokerage...
2023-06-30T21:26:38.3474590Z TRACE:: Brokerage.GetAccountHoldings(): sourcing holdings from provided brokerage data, found 0 entries

Checklist

AlexCatarino commented 12 months ago

Binance doesn't report holdings for Margin (Cross 3x) accounts.