Average entry price of coin-margined crypto futures is arithmetic instead of harmonic

クローズ
#10,134 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

関連するプルリクエストがすでにマージされています。

  • #10173 @quantconnect-kronos による — マージ済み

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
25/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
csharp
領域
fintech-quant

調査の方向性

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 の本文から書いたものです。

説明

priority:P1
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

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

QuantConnect/Lean のほかの issue

QuantConnect/Lean の issue をすべて見る

似ている issue

C# の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。