Trading and Orders

Order Errors


If an error occurs with one of your orders, LEAN logs the error message and stops executing.

Types of Order Errors

Errors can occur when you place orders or after you place them.

Errors at Order Submission

The following errors can occur when you submit an order:

  • AlgorithmWarmingUp
  • BrokerageFailedToSubmitOrder
  • BrokerageModelRefusedToSubmitOrder
  • ConversionRateZero
  • ExceededMaximumOrders
  • ExceedsShortableQuantity
  • ExchangeNotOpen
  • ForexBaseAndQuoteCurrenciesRequired
  • ForexConversionRateZero
  • InsufficientBuyingPower
  • MarketOnCloseOrderTooLate
  • MissingSecurity
  • NonExercisableSecurity
  • NonTradableSecurity
  • OrderAlreadyExisits
  • OrderQuantityLessThanLoteSize
  • OrderQuantityZero
  • PreOrderChecksError
  • QuoteCurrencyRequired
  • SecurityHasNoData
  • UnsupportedRequestType

Errors After Order Submission

The following errors can occur for active orders:

  • BrokerageFailedToUpdateOrder
  • BrokerageModelRefusedToUpdateOrder
  • InvalidNewOrderStatus
  • InvalidOrderStatus
  • InvalidRequest
  • RequestCanceled
  • UnableToFindOrder

Common Errors

There are a few common order errors you may experience.

Why is my order converted to a market on open order?

If you place a market order when the market is closed, LEAN automatically converts the order into market on open order. This most commonly happens when you use daily or hourly data, which your algorithm can receive when the market is closed. Your algorithm receives daily bars at midnight and receives the last hourly bar of each day at 4 PM Eastern Time (ET). To avoid LEAN from converting your market order to market on open orders, submit your market orders when the market is open. To check if the market is open, call the IsMarketOpen method.

if (IsMarketOpen(_symbol))
    MarketOrder(symbol, quantity);
if self.IsMarketOpen(self.symbol):
    self.MarketOrder(self.symbol, quantity)

Why am I seeing the "stale price" warning?

Stale fills occur when you fill an order with price data that is timestamped an hour or more into the past. Stale fills usually only occur if you trade illiquid assets or if your algorithm uses daily data but you trade intraday with Scheduled Events. If your order is filled with stale data, the fill price may not be realistic. The pre-built fill models can only fill market orders with stale data. To adjust the length of time that needs to pass before an order is considered stale, set the StalePriceTimeSpan setting.

Settings.StalePriceTimeSpan = TimeSpan.FromMinutes(10);
self.Settings.StalePriceTimeSpan = timedelta(minutes=10)

Why do I get "Backtest Handled Error: Order Error: id: XXX, Insufficient buying power to complete order" calling the SetHoldings method?

This error usually occurs when you place a market on open order with daily data. If you place the order with SetHoldings or use CalculateOrderQuantity to determine the order quantity, LEAN calculates the order quantity based on the market close price. If the open price on the following day makes your order more expensive, then you may have insufficient buying power. To avoid issues, use intraday data and place trades when the market is open or adjust your buying power buffer.

Why can I only place 10,000 orders in a backtest?

The number of orders you can place in a single backtest depends on the tier of your organization. The following table shows the number of orders you can place on each tier:

TierOrders Quota
Quant Researcher10M
Trading FirmUnlimited

To place more orders in a backtest, upgrade your organization.

Why do I get "Backtest Handled Error: The security with symbol '/ES' is marked as non-tradable"?

This error occurs when you place an order for a continuous Futures contract, which isn't a tradable security. To fix the issue, place the order for a specific Futures contract. To access the currently selected contract in the continuous contract series, use the Mapped property of the Future object.

Error Code Reference

The OrderError enumeration has the following members:

Order Response Error Reference

The OrderResponseErrorCode enumeration has the following members:

You can also see our Videos. You can also get in touch with us via Discord.

Did you find this page helpful?

Contribute to the documentation: