Trying to find out how to handle crypto custom data with extremely low prices, like $0.0000000001 and whatnot. I'm getting Error executing margin models: Value was either too large or too small for a Decimal.
I have made sure to round orders to a few significant digits and make sure our holdings never have more than a few significant digits, but to no avail. Even if I just use 2 significant digits, I still get that error.
-
-
-
-
-
Here are some logs:
20250305 15:02:17.315 TRACE:: Debug: ORDER TICKET ISSUE DETECTED: Sell order for CHILI
OrderId: 8736
Status: New
Symbol: CHILI
Quantity: -170000000000000
Order Direction: SELL
Order Value: $19,433.07
Security Price: 0.0000000001143122
-- HOLDINGS INFORMATION --
Current Holdings: 490000000000000
Holdings Value: $56,012.98
Average Cost: 0.0000000000784707949178835979
Unrealized Profit: $17,562.29
-- ORDER DETAILS --
AverageFillPrice: 0
QuantityFilled: 0
Time: 3/2/2024 1:00:00 AM
HasOrder: True
20250305 15:02:17.315 TRACE:: Debug: ORDER EVENTS:
20250305 15:02:17.315 TRACE:: Debug: Event:
OrderId: 8736
Id: 1
Status: New
FillPrice: 0
FillQuantity: 0
Message: Error executing margin models
Quantity: -170000000000000
-
-
-
-
-
-
-
Full error:
20250305 15:02:16.277 ERROR:: BrokerageTransactionHandler.HandleSubmitOrderRequest(): System.OverflowException: Value was either too large
or too small for a Decimal.
at System.Number.ThrowOverflowException(TypeCode type)
at System.Decimal.DecCalc.ScaleResult(Buf24* bufRes, UInt32 hiRes, Int32 scale)
at System.Decimal.DecCalc.VarDecMul(DecCalc& d1, DecCalc& d2)
at QuantConnect.Securities.BuyingPowerModel.HasSufficientBuyingPowerForOrder(HasSufficientBuyingPowerForOrderParameters parameters, OrderTicket
ticket, Nullable`1 freeMarginToUse, Nullable`1 initialMarginRequired) in Common/Securities/BuyingPowerModel.cs:line 323
at QuantConnect.Securities.BuyingPowerModel.HasSufficientBuyingPowerForOrder(HasSufficientBuyingPowerForOrderParameters parameters) in
Common/Securities/BuyingPowerModel.cs:line 292
at
QuantConnect.Securities.Positions.SecurityPositionGroupBuyingPowerModel.HasSufficientBuyingPowerForOrder(HasSufficientPositionGroupBuyingPowerForOrder
Parameters parameters) in Common/Securities/Positions/SecurityPositionGroupBuyingPowerModel.cs:line 156
at QuantConnect.Securities.SecurityPortfolioManager.HasSufficientBuyingPowerForOrder(List`1 orders) in
Common/Securities/SecurityPortfolioManager.cs:line 911
at QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.HandleSubmitOrderRequest(SubmitOrderRequest request) in
Engine/TransactionHandlers/BrokerageTransactionHandler.cs:line 843
Mia Alissi
The error you're experiencing with handling very low Decimal values in crypto custom data is common when dealing with extremely small or large values that exceed the precision of
Decimal
during arithmetic operations. Here are some strategies to address this issue:Strategies:
Constrain Precision Globally: Set a consistent decimal precision across your algorithm when dealing with security price and order-related calculations to maintain stability.
Rounding of Prices: Ensure your price computations round to a pre-set number of decimal places before performing any arithmetic operations that involve these values.
Significant Digits Management: Choose a precision level up to which decimals will be rounded consistently before processing - this helps to avoid overflow exceptions by maintaining significant figures in calculations.
Use of Scaling: Implement custom scaling before processing financial calculations and reverse scaling afterward. Multiplying or dividing by a power of ten counteracts difficulties posed by very small values.
Numerical Adjustments: Before executing a calculation or passing an order, check for values below the precision threshold and log-transform small values when possible to manage precision indirectly.
Custom Buying Power Models: If the built-in buying power checks are leading to errors due to the scale of transactions, consider implementing or extending a custom buying power model:
HasSufficientBuyingPowerForOrder
: Modify the logic to handle precision limits and ensure calculations within your specified precision bounds.Understanding the Framework:
BuyingPower Models: Ensure your models have robust error-checking mechanisms for significant digits or scale adjustments that could avoid the overflow seen in your log.
Simulated Orders: Use simulation runs that closely mimic anticipated transaction loads to identify setups where such precision errors manifest prior to live trading.
These strategic adjustments should help you handle extremely low prices in your QuantConnect algorithms while ensuring stable calculations and avoiding decimals that exceed allowed ranges. If you continue to face issues, collaborating with the QuantConnect community or seeking further support might offer additional insights.
Sources: Order Errors - QuantConnect.com, Debugging - QuantConnect.com, Bybit Crypto Futures Regression Algorithm
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Kevin Wheeler
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!