def test_oco(self): self.assertFalse(has_diff({ 'orderStrategyType': 'OCO', 'childOrderStrategies': [ {'session': 'NORMAL'}, {'duration': 'DAY'}, ] }, one_cancels_other( OrderBuilder().set_session(Session.NORMAL), OrderBuilder().set_duration(Duration.DAY)).build()))
def test_trigger(self): self.assertFalse(has_diff({ 'orderStrategyType': 'TRIGGER', 'session': 'NORMAL', 'childOrderStrategies': [ {'duration': 'DAY'}, ] }, first_triggers_second( OrderBuilder().set_session(Session.NORMAL), OrderBuilder().set_duration(Duration.DAY)).build()))
def one_cancels_other(order1, order2): ''' If one of the orders is executed, immediately cancel the other. ''' from tda.orders.generic import OrderBuilder return (OrderBuilder().set_order_strategy_type( OrderStrategyType.OCO).add_child_order_strategy( order1).add_child_order_strategy(order2))
def option_buy_to_close_market(symbol, quantity): ''' Returns a pre-filled :class:`~tda.orders.generic.OrderBuilder` for a buy-to-close market order. ''' from tda.orders.common import Duration, OptionInstruction, OrderType from tda.orders.common import OrderStrategyType, Session return (OrderBuilder().set_session(Session.NORMAL).set_duration( Duration.DAY).set_order_type(OrderType.MARKET).set_order_strategy_type( OrderStrategyType.SINGLE).add_option_leg( OptionInstruction.BUY_TO_CLOSE, symbol, quantity))
def equity_buy_to_cover_market(symbol, quantity): ''' Returns a pre-filled :class:`~tda.orders.generic.OrderBuilder` for an equity buy-to-cover market 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.MARKET).set_session( Session.NORMAL).set_duration(Duration.DAY).set_order_strategy_type( OrderStrategyType.SINGLE).add_equity_leg( EquityInstruction.BUY_TO_COVER, symbol, quantity))
def equity_sell_short_market(symbol, quantity): ''' Returns a pre-filled :class:`~tda.orders.generic.OrderBuilder` for an equity short sell market 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.MARKET).set_session( Session.NORMAL).set_duration(Duration.DAY).set_order_strategy_type( OrderStrategyType.SINGLE).add_equity_leg( EquityInstruction.SELL_SHORT, symbol, quantity))
def option_sell_to_open_limit(symbol, quantity, price): ''' Returns a pre-filled :class:`~tda.orders.generic.OrderBuilder` for a sell-to-open limit order. ''' from tda.orders.common import Duration, OptionInstruction, OrderType from tda.orders.common import OrderStrategyType, Session return (OrderBuilder().set_session(Session.NORMAL).set_duration( Duration.DAY).set_order_type( OrderType.LIMIT).set_price(price).set_order_strategy_type( OrderStrategyType.SINGLE).add_option_leg( OptionInstruction.SELL_TO_OPEN, symbol, quantity))
def equity_sell_limit(symbol, quantity, price): ''' Returns a pre-filled :class:`~tda.orders.generic.OrderBuilder` for an equity sell 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.LIMIT).set_price(price).set_session( Session.NORMAL).set_duration(Duration.DAY).set_order_strategy_type( OrderStrategyType.SINGLE).add_equity_leg( EquityInstruction.SELL, symbol, quantity))
def __base_builder(): from tda.orders.common import Duration, Session return (OrderBuilder().set_session(Session.NORMAL).set_duration( Duration.DAY))
import config # stored in parent directory for security token_path = 'token' redirect_uri = "https://localhost" DRIVER_PATH = "/home/hopper/chromedriver" driver = webdriver.Chrome(DRIVER_PATH) try: c = auth.client_from_token_file(token_path, config.api_key) except FileNotFoundError: c = auth.client_from_login_flow( driver, config.api_key, redirect_uri, token_path ) time.sleep(2) # print(c.place_order(config.tda_acct_num, equity_buy_market('SNDL', 1).set_duration(Duration.GOOD_TILL_CANCEL).set_session(Session.SEAMLESS).build())) builder = OrderBuilder('SNDL', 1) builder.set_instruction(OrderBuilder.instruction.BUY) builder.set_order_type(OrderBuilder.OrderType.MARKET) builder.set_duration(Duration.DAY) builder.set_session(Session.NORMAL) response = c.place_order(config.tda_acct_num, builder.build()) print(response) time.sleep(2) driver.quit()