Skip to content

Instantly share code, notes, and snippets.

@pssolanki111
Last active August 26, 2022 19:12
Show Gist options
  • Save pssolanki111/11849045869cb3d1274340c4f03f9a9e to your computer and use it in GitHub Desktop.
Save pssolanki111/11849045869cb3d1274340c4f03f9a9e to your computer and use it in GitHub Desktop.
# =============================================================== #
from pprint import pprint
from tda.orders.generic import OrderBuilder
from typing import Union
import sys
from tda.orders.common import (one_cancels_other as oco,
first_triggers_second as trigger)
# =============================================================== #
# conditional Helper #
# =============================================================== #
# see line 126 and 132 for example
# Just use `oco` and `trigger` lol. They're both available in the same namespace.
# =============================================================== #
# Helpers #
# =============================================================== #
# MARKET Equity Order
def market_equity(symbol: str, qty: int, instr: str = 'BUY', session: str = 'NORMAL', duration: str = 'DAY'):
return OrderBuilder(enforce_enums=False).set_order_strategy_type('SINGLE') \
.set_session(session.upper()) \
.set_duration(duration.upper()) \
.set_order_type('MARKET') \
.add_equity_leg(instr.upper(), symbol.upper(), qty)
# LIMIT Equity Order
def limit_equity(symbol: str, qty: Union[int, str], limit_price: Union[str, int, float], instr: str = 'BUY',
session: str = 'NORMAL', duration: str = 'DAY'):
return OrderBuilder(enforce_enums=False).set_order_strategy_type('SINGLE') \
.set_session(session.upper()) \
.set_duration(duration.upper()) \
.set_order_type('LIMIT') \
.set_price(limit_price) \
.add_equity_leg(instr.upper(), symbol.upper(), qty)
# STOP Equity Order
def stop_equity(symbol: str, qty: Union[int, str], stop_price: Union[str, int, float], instr: str = 'SELL',
stop_type: str = 'MARK', session: str = 'NORMAL', duration: str = 'DAY'):
return OrderBuilder(enforce_enums=False) \
.set_order_strategy_type('SINGLE') \
.set_session(session.upper()) \
.set_duration(duration.upper()) \
.set_order_type('STOP') \
.set_stop_type(stop_type.upper()) \
.set_stop_price(stop_price) \
.add_equity_leg(instr.upper(), symbol.upper(), qty)
# STOP LIMIT Equity Order
def stop_limit_equity(symbol: str, qty: Union[int, str], stop_price: Union[str, int, float],
limit_price: Union[str, int, float], instr: str = 'SELL', session: str = 'NORMAL',
duration: str = 'DAY', stop_type: str = 'MARK'):
return OrderBuilder(enforce_enums=False) \
.set_order_strategy_type('SINGLE') \
.set_session(session.upper()) \
.set_duration(duration.upper()) \
.set_order_type('STOP_LIMIT') \
.set_stop_type(stop_type.upper()) \
.set_stop_price(stop_price) \
.set_price(limit_price) \
.add_equity_leg(instr.upper(), symbol.upper(), qty)
# TRAILING STOP Equity Order
def trailing_stop_equity(symbol: str, qty: Union[int, str], trailing_offset: Union[str, int, float], instr: str = 'SELL',
stop_price_link_type: str = 'PERCENT', stop_price_link_basis: str = 'MARK',
stop_type: str = 'MARK', session: str = 'NORMAL', duration: str = 'DAY'):
if stop_price_link_type == 'PERCENT' and (float(trailing_offset) < 1 or float(trailing_offset) > 99):
raise ValueError('For percent trail, the offset must be between 1 to 99')
return OrderBuilder(enforce_enums=False) \
.set_duration(duration.upper()) \
.set_order_strategy_type('SINGLE') \
.set_order_type('TRAILING_STOP') \
.set_quantity(qty) \
.set_session(session.upper()) \
.set_stop_price_link_basis(stop_price_link_basis.upper()) \
.set_stop_price_link_type(stop_price_link_type.upper()) \
.set_stop_price_offset(trailing_offset) \
.set_stop_type(stop_type.upper()) \
.add_equity_leg(instr.upper(), symbol.upper(), qty)
# TRAILING STOP LIMIT Equity Order
def trailing_stop_limit_equity(symbol: str, qty: Union[int, str], trailing_offset: Union[str, int, float],
limit_price: Union[str, int, float], instr: str = 'SELL',
stop_price_link_type: str = 'PERCENT', stop_price_link_basis: str = 'MARK',
stop_type: str = 'MARK', session: str = 'NORMAL', duration: str = 'DAY'):
if stop_price_link_type == 'PERCENT' and (float(trailing_offset) < 1 or float(trailing_offset) > 99):
raise ValueError('For percent trail, the offset must be between 1 to 99')
return OrderBuilder(enforce_enums=False) \
.set_duration(duration.upper()) \
.set_order_strategy_type('SINGLE') \
.set_order_type('TRAILING_STOP_LIMIT') \
.set_quantity(qty) \
.set_session(session.upper()) \
.set_stop_price_link_basis(stop_price_link_basis.upper()) \
.set_stop_price_link_type(stop_price_link_type.upper()) \
.set_stop_price_offset(trailing_offset) \
.set_stop_type(stop_type.upper()) \
.set_price(limit_price.upper()) \
.add_equity_leg(instr.upper(), symbol.upper(), qty)
# MARKET ON CLOSE equity Order
def market_on_close_equity(symbol: str, qty: int, instr: str = 'BUY'):
return OrderBuilder(enforce_enums=False).set_order_strategy_type('SINGLE') \
.set_order_type('MARKET_ON_CLOSE') \
.add_equity_leg(instr.upper(), symbol.upper(), qty)
# =============================================================== #
if __name__ == '__main__': # tests. Do NOT run the file directly.
a = market_equity('SNAP', 1)
b = trailing_stop_equity('GOOG', 1, 1, 'SELL')
c = trigger(a, b) # TRIGGER example
order1 = market_equity('SNAP', qty=1)
order2 = trailing_stop_equity('SNAP', 1, 1, instr='SELL')
oco(order1, order2) # OCO example
# =============================================================== #
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment