# Order Types

## Option Exercise Orders

### Introduction

If you buy an Option contract, you can exercise your right to buy or sell the underlying shares at the strike price. However, you don't need to exercise it. You can sell the Option or let it expire worthless if it's out of the money. If you hold a long position in an Option that expires in the money, LEAN automatically exercises it at the expiration date. If you sell an Option contract and the buyer exercises their right to buy or sell the underlying shares, you are assigned the Option and must trade with the buyer at the strike price.

### Brokerage Support

Each brokerage has a set of assets and order types they support. To avoid issues with Option exercise orders, set the brokerage model to a brokerage that supports them.

SetBrokerageModel(BrokerageName.QuantConnectBrokerage);
self.SetBrokerageModel(BrokerageName.QuantConnectBrokerage)

To check if your brokerage has any special requirements for Option exercise orders, see the Orders section of the brokerage integration documentation.

### Place Orders

The US Equity Option dataset contains American style Options, so you can exercise contracts before they expire. To exercise an Option, call the ExerciseOption method with the Option contract Symbol and a quantity. If you do not have sufficient capital for the order, it's rejected. By default, Option exercise orders are synchronous and fill immediately.

var ticket = ExerciseOption(contractSymbol, quantity);
ticket = self.ExerciseOption(contract_symbol, quantity)

You can provide a tag and order properties to the ExerciseOption method.

ExerciseOption(symbol, quantity, tag: tag, orderProperties: orderProperties);
self.ExerciseOption(symbol, quantity, tag=tag, orderProperties=order_properties)

### Monitor Order Fills

To monitor the fills of your order, save a reference to the order ticket.

var ticket = ExerciseOption(contractSymbol, quantity);
Debug($"Quantity filled: {ticket.QuantityFilled}; Fill price: {ticket.AverageFillPrice}"); ticket = self.ExerciseOption(contract_symbol, quantity) self.Debug(f"Quantity filled: {ticket.QuantityFilled}; Fill price: {ticket.AverageFillPrice}") For more information about how LEAN models Option exercise orders in backtests, see the Exercise Option model. ### Synchronous Timeouts Option exercise orders are synchronous by default, so your algorithm waits for the order to fill before moving to the next line of code. If your order takes longer than five seconds to fill, your algorithm continues executing even if the trade isn't filled. To adjust the timeout period, set the Transactions.MarketOrderFillTimeout property. // Adjust the market fill-timeout to 30 seconds. Transactions.MarketOrderFillTimeout = TimeSpan.FromSeconds(30);   # Adjust the market fill-timeout to 30 seconds. self.Transactions.MarketOrderFillTimeout = timedelta(seconds=30) ### Place Asynchronous Orders When you trade a large portfolio of assets, you may want to send orders in batches and not wait for the response of each one. To send asynchronous orders, set the asynchronous argument to Truetrue. var ticket = ExerciseOption(contractSymbol, quantity, true); ticket = self.ExerciseOption(contract_symbol, quantity, True) ### Option Assignments If you sell an Option in a backtest, LEAN can simulate an Option exercise order on behalf of the buyer. Every 2 hours, the BasicOptionAssignmentSimulation scans your portfolio. It considers exercising American-style Options if they are within 4 days of their expiration and it considers exercising European-style Options on their day of expiration. If you have sold an Option that's 5% in-the-money and the Option exercise order is profitable after the cost of fees, LEAN exercises the Option. To view the source code of the Option assignment logic, see the BasicOptionAssignmentSimulation in the LEAN GitHub repository. ### Example The following backtest verifies the ExerciseOption behavior. The following table shows the first three trades in the backtest: TimeSymbolPriceQuantityTypeStatusValueTag 2021-07-01T09:31:00ZSPY 221216C00085000345.301Buy MarketFilled34530.00 2021-07-01T09:31:00ZSPY 221216C000850000.00-1Sell Option ExerciseFilled0.00Automatic Exercise 2021-07-01T09:31:00ZSPY85.00100Sell Option ExerciseFilled8500.00Option Exercise The algorithm first brought a deep ITM option based on the set option selection conditions by$345.30, then exercise it actively, so 100 shares of SPY (in which 1 contract represents) was brought at its strike price at \$85.00, with the option discarded from the portfolio.

To reproduce these results, backtest the following algorithm:

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