To place a trade, convert an instrument into a 'leg' using the build_leg(quantity, order_action) method. A trade (such as a spread or strangle) is constructed by passing a list of these legs to a LimitOrder.
Key Details:
build_leg parameters: Requires quantity (int) and order_action (from tastytrade.order.OrderAction).- Pricing: The
price in a LimitOrder is per quantity, not the total order price. For example, if you have two legs with a quantity of 2 each and a price of Decimal('1.25'), the total credit/debit is calculated based on that per-unit price. - Execution: Use
account.place_order(session, order, dry_run=False) to execute the trade.
from tastytrade.instruments import get_future_option_chain
from tastytrade.order import LimitOrder, OrderAction
from datetime import date
from decimal import Decimal
# 1. Get the chain
chain = await get_future_option_chain(session, '/MCL')
# 2. Select specific instruments (e.g., a strangle)
put = chain[date(2024, 3, 15)][286]
call = chain[date(2024, 3, 15)][187]
# 3. Build the order
order = LimitOrder(
legs=[
put.build_leg(1, OrderAction.SELL_TO_OPEN),
call.build_leg(1, OrderAction.SELL_TO_OPEN)
],
price=Decimal('1.25') # This is the price per unit
)
# 4. Place the order
await account.place_order(session, order, dry_run=False)