Average entry price of coin-margined crypto futures is arithmetic instead of harmonic
メンテナーはふだん 1 日以内に返信
関連するプルリクエストがすでにマージされています。
- #10173 @quantconnect-kronos による — マージ済み
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 25/100
- issue の種類
- バグ
- 明瞭さ
- おおむね明確
- 活発さ
- 活発
- 技術スタック
- csharp
調査の方向性
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.
索引モデルが issue の本文から書いたものです。
説明
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.
- 主要言語
- C#
- スター
- 22.6k
- フォーク
- 5.4k
- 平均マージ
- 11時間 39分
- マージ済み PR(30日)
- 104
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile または Docker Compose ファイルあり
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
QuantConnect/Lean のほかの issue
-
priority:p2
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
QuantConnect/Lean#10216 ·
メンテナーはふだん 1 日以内に返信
-
priority:p2
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
QuantConnect/Lean#10206 ·
メンテナーはふだん 1 日以内に返信
-
priority:p2
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
QuantConnect/Lean#10199 ·
メンテナーはふだん 1 日以内に返信
-
priority:p2
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
QuantConnect/Lean#10198 ·
メンテナーはふだん 1 日以内に返信
-
priority:p2
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
QuantConnect/Lean#10197 ·
メンテナーはふだん 1 日以内に返信
QuantConnect/Lean の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
dotnet-outdated/dotnet-outdated#871 ·
メンテナーはふだん 1 日以内に返信
-
triage/untriaged
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
unoplatform/uno#26676 ·
メンテナーはふだん 1 日以内に返信
-
.NET bug
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
jhaizhou-ops/S2Arm#2177 · コメント 3 件 ·
メンテナーはふだん 1 日以内に返信
-
area-CodeGen-coreclr untriaged
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
dotnet/runtime#138181 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信