Average entry price of coin-margined crypto futures is arithmetic instead of harmonic
Maintainers usually reply within 1 day
A pull request for this has already been merged.
- #10173 by @quantconnect-kronos — merged
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 25/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Active
- Tech stack
- csharp
- Domain
- fintech-quant
Research direction
Start in Common/Securities/SecurityPortfolioModel.cs, at ProcessFill where averageHoldingsPrice is updated when adding to a long or short, and compare with CryptoFutureHolding.GetQuantityValue. Reproduce with the steps in Tests/Common/Securities/CryptoFuture/CryptoFutureMarginModelTests.cs. Done when the BTC change for the 40,000/50,000/45,000 example is about 0.0055556, and linear and other security types are unchanged. The issue depends on #9940 and #10133, so check those first.
Written by the indexing model from the issue text.
Description
Expected Behavior
For a coin-margined (inverse) crypto future, adding to a position at a different price should give an average entry price that keeps the position's profit right, which for inverse contracts is the harmonic mean of the fill prices weighted by quantity (Binance COIN-M: average entry price = total quantity / Σ(quantity / fill price)).
Example, Binance BTCUSD (USD 100 per contract), zero fees: buy 100 at 40,000, buy 100 at 50,000, sell 200 at 45,000. The realized profit is
100 × 100 × (1/40000 − 1/45000) + 100 × 100 × (1/50000 − 1/45000) ≈ +0.0055556 BTC.
Actual Behavior
SecurityPortfolioModel.ProcessFill (Common/Securities/SecurityPortfolioModel.cs, the averageHoldingsPrice updates for adding to a long or short) averages fill prices arithmetically for every security type. For the example the average entry price is 45,000, so closing at 45,000 credits 0 BTC and books 0 profit instead of +0.0055556 BTC. Unrealized profit of such a position is off by the same amount.
This is independent of the profit sign fixed for #9940: with the correct sign, the average is still wrong.
Reproducing the Problem
In Tests/Common/Securities/CryptoFuture/CryptoFutureMarginModelTests.cs, with its GetAlgorithm() and SetPrice() helpers:
var algo = GetAlgorithm();
var cf = algo.AddCryptoFuture("BTCUSD");
cf.BaseCurrency.SetAmount(1);
cf.FeeModel = new ConstantFeeModel(0);
SetPrice(cf, 40000);
algo.Portfolio.ProcessFills(new List<OrderEvent> { new OrderEvent(1, cf.Symbol, algo.UtcTime, OrderStatus.Filled, OrderDirection.Buy, 40000, 100, OrderFee.Zero) });
SetPrice(cf, 50000);
algo.Portfolio.ProcessFills(new List<OrderEvent> { new OrderEvent(2, cf.Symbol, algo.UtcTime, OrderStatus.Filled, OrderDirection.Buy, 50000, 100, OrderFee.Zero) });
SetPrice(cf, 45000);
var before = cf.BaseCurrency.Amount;
algo.Portfolio.ProcessFills(new List<OrderEvent> { new OrderEvent(3, cf.Symbol, algo.UtcTime, OrderStatus.Filled, OrderDirection.Sell, 45000, -200, OrderFee.Zero) });
// BTC change: 0, expected ≈ 0.0055556
What a fix must achieve
Average entry price of coin-margined crypto futures (outside dYdX, matching CryptoFutureHolding.GetQuantityValue) follows the inverse-contract convention when a position is increased, so realized and unrealized profit match contracts × contract size × (1/entry − 1/exit) per fill. Linear contracts and other security types stay as they are. A suggestion, whoever fixes it picks the approach: let the holding compute the new average, as #10133 lets it compute profit.
System Information
Current master.
- Dominant language
- C#
- Stars
- 22.6k
- Forks
- 5.4k
- Avg merge
- 11h 39m
- Merged PRs (30d)
- 104
Getting set up
Starts the project's dev container in your browser, under your own GitHub account.
- Ships a Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from QuantConnect/Lean
-
priority:p2
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
QuantConnect/Lean#10216 ·
Maintainers usually reply within 1 day
-
priority:p2
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
QuantConnect/Lean#10206 ·
Maintainers usually reply within 1 day
-
priority:p2
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
QuantConnect/Lean#10199 ·
Maintainers usually reply within 1 day
-
priority:p2
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
QuantConnect/Lean#10198 ·
Maintainers usually reply within 1 day
-
priority:p2
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
QuantConnect/Lean#10197 ·
Maintainers usually reply within 1 day
All issues in QuantConnect/Lean
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
dotnet-outdated/dotnet-outdated#871 ·
Maintainers usually reply within 1 day
-
triage/untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
unoplatform/uno#26676 ·
Maintainers usually reply within 1 day
-
.NET bug
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
jhaizhou-ops/S2Arm#2177 · 3 comments ·
Maintainers usually reply within 1 day
-
area-CodeGen-coreclr untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
dotnet/runtime#138181 · 1 comment ·
Maintainers usually reply within 1 day