Average entry price of coin-margined crypto futures is arithmetic instead of harmonic
Maintainer thường phản hồi trong vòng 1 ngày
Một pull request liên quan đã được merge.
- #10173 của @quantconnect-kronos — đã merge
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 25/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- csharp
- Lĩnh vực
- fintech-quant
Hướng nghiên cứu
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.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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.
- Ngôn ngữ chính
- C#
- Star
- 22.6k
- Fork
- 5.4k
- Merge trung bình
- 11 giờ 39 phút
- Pull request đã merge (30 ngày)
- 104
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Có Dockerfile hoặc tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của QuantConnect/Lean
-
priority:p2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
QuantConnect/Lean#10216 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
priority:p2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
QuantConnect/Lean#10206 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
priority:p2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
QuantConnect/Lean#10199 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
priority:p2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
QuantConnect/Lean#10198 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
priority:p2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
QuantConnect/Lean#10197 ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của QuantConnect/Lean
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 66/100
dotnet-outdated/dotnet-outdated#871 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
triage/untriaged
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
unoplatform/uno#26676 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
.NET bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
Maintainer thường phản hồi trong vòng 1 ngày
-
[问题] 导出文件时,提示后面还有1243390911步Đang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
jhaizhou-ops/S2Arm#2177 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
area-CodeGen-coreclr untriaged
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
dotnet/runtime#138181 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày