tastytrade/tastytrade/order.py

33 lines
1.7 KiB
Python

from .instrument_type import EQUITY_OPTION
from .order_action import BUY_TO_CLOSE, BUY_TO_OPEN, SELL_TO_CLOSE, SELL_TO_OPEN
from .order_type import LIMIT, STOP_LIMIT
from .price_effect import CREDIT, DEBIT
from .time_in_force import GTC
def create_leg(instrument_type: str, symbol: str, action: str, quantity: int):
return {'instrument-type': instrument_type, 'symbol': symbol, 'action': action, 'quantity': quantity}
def create_order(order_type: str, time_in_force: str, price: float, price_effect: str, legs: list,
stop_trigger: float = None):
order = {
'order-type': order_type,
'time-in-force': time_in_force,
'price': price,
'price-effect': price_effect,
'legs': legs
}
if stop_trigger:
order['stop-trigger'] = stop_trigger
return order
def create_credit_spread(short_contract: str, long_contract: str, price: float, quantity: int,
instrument_type: str = EQUITY_OPTION, time_in_force: str = GTC):
short_leg = create_leg(instrument_type, short_contract, SELL_TO_OPEN, quantity)
long_leg = create_leg(instrument_type, long_contract, BUY_TO_OPEN, quantity)
return create_order(LIMIT, time_in_force, price, CREDIT, [short_leg, long_leg])
def create_stop_limit_order(short_contract: str, long_contract: str, stop_trigger: float, limit_price: float,
quantity: int, instrument_type: str = EQUITY_OPTION, time_in_force: str = GTC):
short_leg = create_leg(instrument_type, short_contract, BUY_TO_CLOSE, quantity)
long_leg = create_leg(instrument_type, long_contract, SELL_TO_CLOSE, quantity)
return create_order(STOP_LIMIT, time_in_force, limit_price, DEBIT, [short_leg, long_leg], stop_trigger)