From 4987e6be70f54cd5cb12827cfe7974f6a5530e5a Mon Sep 17 00:00:00 2001 From: Jkorf Date: Fri, 21 Aug 2026 09:52:54 +0200 Subject: [PATCH] Added WithCalculatedQuantities to SharedQuantity to retrieve a copy with calculated quantities --- .../SharedApis/SharedQuantity.cs | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/CryptoExchange.Net/SharedApis/SharedQuantity.cs b/CryptoExchange.Net/SharedApis/SharedQuantity.cs index dcddd06a..e4b1dee7 100644 --- a/CryptoExchange.Net/SharedApis/SharedQuantity.cs +++ b/CryptoExchange.Net/SharedApis/SharedQuantity.cs @@ -176,6 +176,42 @@ namespace CryptoExchange.Net.SharedApis return null; } + /// + /// Get a copy of this quantities record with calculated quantities for data not returned by the API based on the provided price and contract size + /// + /// The relevant price + /// The contract size of the symbol + /// A copy of this SharedOrderQuantity with calculated quantities + public SharedOrderQuantity WithCalculatedQuantities(decimal? price, decimal? contractSize) + { + var copy = this with {}; + if (copy.QuantityInBaseAsset == null) + { + if (copy.QuantityInQuoteAsset != null && price > 0) + copy.QuantityInBaseAsset = copy.QuantityInQuoteAsset / price; + else if (copy.QuantityInContracts != null && contractSize != null) + copy.QuantityInBaseAsset = copy.QuantityInContracts * contractSize; + } + + if (copy.QuantityInContracts == null) + { + if (copy.QuantityInBaseAsset != null && contractSize > 0) + copy.QuantityInContracts = copy.QuantityInBaseAsset / contractSize; + else if (copy.QuantityInQuoteAsset != null && price > 0 && contractSize > 0) + copy.QuantityInContracts = copy.QuantityInQuoteAsset / price / contractSize; + } + + if (copy.QuantityInQuoteAsset == null) + { + if (copy.QuantityInBaseAsset != null && price != null) + copy.QuantityInQuoteAsset = copy.QuantityInBaseAsset * price; + else if (copy.QuantityInContracts != null && price != null && contractSize != null) + copy.QuantityInQuoteAsset = copy.QuantityInContracts * contractSize * price; + } + + return copy; + } + /// public override string ToString() => base.ToString(); }