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 equity stop order templates #323

Open
wants to merge 3 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
83 changes: 82 additions & 1 deletion tda/orders/equities.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
from enum import Enum

from tda.orders.common import Duration, Session
from tda.orders.common import (
Duration,
StopType,
Session,
StopType,
StopPriceLinkBasis,
StopPriceLinkType,
)


##########################################################################
Expand Down Expand Up @@ -154,3 +161,77 @@ def equity_buy_to_cover_limit(symbol, quantity, price):
.set_duration(Duration.DAY)
.set_order_strategy_type(OrderStrategyType.SINGLE)
.add_equity_leg(EquityInstruction.BUY_TO_COVER, symbol, quantity))


##########################################################################
# Stop orders


def equity_sell_stop_market(
symbol, quantity, stop_price, stop_type=StopType.MARK):
"""
Returns a pre-filled :class:`~tda.orders.generic.OrderBuilder` for an equity
sell stop order.
"""
from tda.orders.common import Duration, EquityInstruction
from tda.orders.common import OrderStrategyType, OrderType, Session
from tda.orders.generic import OrderBuilder

return (OrderBuilder()
.set_order_type(OrderType.STOP)
.set_quantity(quantity)
.set_session(Session.NORMAL)
.set_stop_price(stop_price)
.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=StopType.MARK):
"""
Returns a pre-filled :class:`~tda.orders.generic.OrderBuilder` for an equity
sell stop limit order.
"""

from tda.orders.common import Duration, EquityInstruction
from tda.orders.common import OrderStrategyType, OrderType, Session
from tda.orders.generic import OrderBuilder

return (OrderBuilder()
.set_order_type(OrderType.STOP_LIMIT)
.set_quantity(quantity)
.set_session(Session.NORMAL)
.set_price(limit_price)
.set_stop_price(stop_price)
.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_market(
symbol, quantity, trail_offset,
trail_offset_type=StopPriceLinkType.PERCENT, stop_type=StopType.MARK,
stop_price_link_basis=StopPriceLinkBasis.MARK):
"""
Returns a pre-filled :class:`~tda.orders.generic.OrderBuilder` for an equity
sell trailing stop order.
"""

from tda.orders.common import Duration, EquityInstruction
from tda.orders.common import OrderStrategyType, OrderType, Session
from tda.orders.generic import OrderBuilder

return (OrderBuilder()
.set_order_type(OrderType.TRAILING_STOP)
.set_quantity(quantity)
.set_session(Session.NORMAL)
.set_duration(Duration.DAY)
.set_stop_type(stop_type)
.set_stop_price_offset(trail_offset)
.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))
60 changes: 60 additions & 0 deletions tests/orders_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,63 @@ def test_equity_buy_to_cover_limit(self):
}
}]
}, equity_buy_to_cover_limit('GOOG', 10, 199.99).build()))

def test_equity_sell_stop_market(self):
self.assertFalse(has_diff({
'orderType': 'STOP',
'quantity': 10,
'session': 'NORMAL',
'duration': 'DAY',
'stopPrice': '199.99',
'stopType': 'MARK',
'orderStrategyType': 'SINGLE',
'orderLegCollection': [{
'instruction': 'SELL',
'quantity': 10,
'instrument': {
'symbol': 'GOOG',
'assetType': 'EQUITY',
}
}]
}, equity_sell_stop_market('GOOG', 10, 199.99).build()))

def test_equity_sell_stop_limit(self):
self.assertFalse(has_diff({
'orderType': 'STOP_LIMIT',
'quantity': 10,
'session': 'NORMAL',
'duration': 'DAY',
'price': '88.88',
'stopPrice': '199.99',
'stopType': 'MARK',
'orderStrategyType': 'SINGLE',
'orderLegCollection': [{
'instruction': 'SELL',
'quantity': 10,
'instrument': {
'symbol': 'GOOG',
'assetType': 'EQUITY',
}
}]
}, equity_sell_stop_limit('GOOG', 10, 88.88, 199.99).build()))

def test_equity_sell_trailing_stop_market(self):
self.assertFalse(has_diff({
'orderType': 'TRAILING_STOP',
'quantity': 10,
'session': 'NORMAL',
'duration': 'DAY',
'stopType': 'MARK',
'stopPriceOffset': 5,
'stopPriceLinkBasis': 'MARK',
'stopPriceLinkType': 'PERCENT',
'orderStrategyType': 'SINGLE',
'orderLegCollection': [{
'instruction': 'SELL',
'quantity': 10,
'instrument': {
'symbol': 'GOOG',
'assetType': 'EQUITY',
}
}]
}, equity_sell_trailing_stop_market('GOOG', 10, 5).build()))