Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added stop and trailing stop (limit) order templates with validation #322

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/order-templates.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ Sell orders

.. autofunction:: tda.orders.equities.equity_sell_market
.. autofunction:: tda.orders.equities.equity_sell_limit
.. autofunction:: tda.orders.equities.equity_sell_stop
.. autofunction:: tda.orders.equities.equity_sell_stop_limit
.. autofunction:: tda.orders.equities.equity_sell_trailing_stop
.. autofunction:: tda.orders.equities.equity_sell_trailing_stop_limit

+++++++++++++++++
Sell short orders
Expand Down
122 changes: 121 additions & 1 deletion tda/orders/equities.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
from enum import Enum

from tda.orders.common import Duration, Session
from tda.orders.common import Duration, EquityInstruction
from tda.utils import EnumEnforcer
from tda.orders.common import (OrderStrategyType, OrderType, Session, StopType,
StopPriceLinkType, StopPriceLinkBasis)
from tda.orders.generic import OrderBuilder

enforcer = EnumEnforcer(False)


##########################################################################

STOP_TYPES = ['MARK', 'LAST', 'BID', 'ASK', 'STANDARD']
STOP_PRICE_LINK_BASIS = ['MANUAL', 'BASE', 'TRIGGER', 'LAST', 'BID', 'ASK', 'ASK_BID', 'MARK', 'AVERAGE']
STOP_PRICE_LINK_TYPE = ['VALUE', 'PERCENT', 'TICK']

##########################################################################
# Buy orders
Expand Down Expand Up @@ -79,6 +91,114 @@ def equity_sell_limit(symbol, quantity, price):
.set_order_strategy_type(OrderStrategyType.SINGLE)
.add_equity_leg(EquityInstruction.SELL, symbol, quantity))


def equity_sell_stop(symbol, quantity, stop_price, stop_type='MARK'):
"""
Returns a pre-filled :class:`~tda.orders.generic.OrderBuilder` for an equity
sell stop order
"""

stop_type = enforcer.convert_enum(stop_type, StopType)
assert stop_type in STOP_TYPES, f'Stop Type must be one of {STOP_TYPES}'

return (OrderBuilder()
.set_order_type(OrderType.STOP)
.set_quantity(int(quantity))
.set_session(Session.NORMAL)
.set_stop_price(round(float(stop_price), 2))
.set_stop_type(stop_type)
.set_duration(Duration.DAY)
.set_order_strategy_type(OrderStrategyType.SINGLE)
.add_equity_leg(EquityInstruction.SELL, symbol, quantity))


def equity_sell_stop_limit(symbol, quantity, limit_price, stop_price,
stop_type='MARK'):
"""
Returns a pre-filled :class:`~tda.orders.generic.OrderBuilder` for an equity
sell stop limit order
"""

stop_type = enforcer.convert_enum(stop_type, StopType)
assert stop_type in STOP_TYPES, f'Stop Type must be one of {STOP_TYPES}'

return (OrderBuilder()
.set_order_type(OrderType.STOP_LIMIT)
.set_quantity(int(quantity))
.set_session(Session.NORMAL)
.set_price(round(float(limit_price), 2))
.set_stop_price(round(float(stop_price), 2))
.set_stop_type(stop_type)
.set_duration(Duration.DAY)
.set_order_strategy_type(OrderStrategyType.SINGLE)
.add_equity_leg(EquityInstruction.SELL, symbol, quantity))


def equity_sell_trailing_stop(symbol, quantity, trail_offset, trail_offset_type='PERCENT',
stop_type='MARK', stop_price_link_basis='MARK'):
"""
Returns a pre-filled :class:`~tda.orders.generic.OrderBuilder` for an equity
sell trailing stop order
"""
trail_offset_type = enforcer.convert_enum(trail_offset_type, StopPriceLinkType)
stop_type = enforcer.convert_enum(stop_type, StopType)
stop_price_link_basis = enforcer.convert_enum(stop_price_link_basis, StopType)

assert trail_offset_type in STOP_PRICE_LINK_TYPE, \
f'offset type must be one of {STOP_PRICE_LINK_TYPE}'
assert stop_type in STOP_TYPES, f'offset type must be one of {STOP_TYPES}'
assert stop_price_link_basis in STOP_PRICE_LINK_BASIS, \
f'offset type must be one of {STOP_PRICE_LINK_BASIS}'

if trail_offset_type == 'PERCENT' and (float(trail_offset) < 1 or (float(trail_offset)) > 99):
raise ValueError('When using percent, trailing offset must be >=1 and <=99')

return (OrderBuilder()
.set_order_type(OrderType.TRAILING_STOP)
.set_quantity(int(quantity))
.set_session(Session.NORMAL)
.set_stop_type(stop_type)
.set_duration(Duration.DAY)
.set_stop_price_offset(round(float(trail_offset), 2))
.set_stop_price_link_basis(stop_price_link_basis)
.set_stop_price_link_type(trail_offset_type)
.set_order_strategy_type(OrderStrategyType.SINGLE)
.add_equity_leg(EquityInstruction.SELL, symbol, quantity))


def equity_sell_trailing_stop_limit(symbol, quantity, trail_offset, limit_price,
trail_offset_type='PERCENT', stop_type='MARK',
stop_price_link_basis='MARK'):
"""
Returns a pre-filled :class:`~tda.orders.generic.OrderBuilder` for an equity
sell trailing stop limit order
"""
trail_offset_type = enforcer.convert_enum(trail_offset_type, StopPriceLinkType)
stop_type = enforcer.convert_enum(stop_type, StopType)
stop_price_link_basis = enforcer.convert_enum(stop_price_link_basis, StopType)

assert trail_offset_type in STOP_PRICE_LINK_TYPE, \
f'offset type must be one of {STOP_PRICE_LINK_TYPE}'
assert stop_type in STOP_TYPES, f'offset type must be one of {STOP_TYPES}'
assert stop_price_link_basis in STOP_PRICE_LINK_BASIS,\
f'offset type must be one of {STOP_PRICE_LINK_BASIS}'

if trail_offset_type == 'PERCENT' and (float(trail_offset) < 1 or float(trail_offset) > 99):
raise ValueError('When using percent, trailing offset must be >=1 and <=99')

return (OrderBuilder()
.set_order_type(OrderType.TRAILING_STOP)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.set_order_type(OrderType.TRAILING_STOP)
.set_order_type(OrderType.TRAILING_STOP_LIMIT)

.set_quantity(int(quantity))
.set_price(round(float(limit_price), 2))
.set_session(Session.NORMAL)
.set_stop_type(stop_type)
.set_duration(Duration.DAY)
.set_stop_price_offset(round(float(trail_offset), 2))
.set_stop_price_link_basis(stop_price_link_basis)
.set_stop_price_link_type(trail_offset_type)
.set_order_strategy_type(OrderStrategyType.SINGLE)
.add_equity_leg(EquityInstruction.SELL, symbol, quantity))

##########################################################################
# Short sell orders

Expand Down