Order want Strategy Type but no examples have it #207
Replies: 1 comment 1 reply
-
I got it. It was the child that needed the type Then I need to change from BUY to BUY_TO_OPEN since it is an option. The order finally went through BUT my child trailing stop sell did not fire. Any ideas on that one? The buy shows up in mt TOS but not the trail stop sell. I tried SELL and SELL_TO_CLOSE since it is an option. here is the response back that seems to have the trailing stop in there thanks! |
Beta Was this translation helpful? Give feedback.
-
Had to take out all the # since it turns it bold for some reason.
I am stuck and can't find any info on it. I have been using the rest of the API fine but cannot get an order to go.
I am testing buying a SPY option with a child trailing stop order at the same time.
I get this error
YOU HAVE 1 ORDER(S) IN THE ORDER LEGS COLLECTION.
YOU HAVE 1 ORDER(S) IN THE ORDER LEGS COLLECTION.
YOU HAVE 1 CHILD ORDER STRATEGIES IN THE CHILD ORDER STRATEGIES COLLECTION.
Traceback (most recent call last):
File "C:\Users\Doug\Documents\Stocks\Python\Option Order.py", line 161, in
orderResponse = TDSession.place_order(account = accountNum, order = new_order)
File "C:\Users\Doug\AppData\Local\Programs\Python\Python39\lib\site-packages\td\client.py", line 1925, in place_order
return self._make_request(method='post', endpoint=endpoint, mode='json', json=order, order_details=True)
File "C:\Users\Doug\AppData\Local\Programs\Python\Python39\lib\site-packages\td\client.py", line 617, in _make_request
raise NotNulError(message=response.text)
td.exceptions.NotNulError: {
"error" : "Enter a valid order strategy type"
}
I tried both of these
#"orderStrategyType": "SINGLE"
#new_order.order_strategy_type(order_strategy_type=STRATEGY.SINGLE)
new_order.order_strategy_type(orderStrategyType = 'SINGLE')
but get
Traceback (most recent call last):
File "C:\Users\Doug\Documents\Stocks\Python\Option Order.py", line 34, in
new_order.order_strategy_type(order_strategy_type=STRATEGY.SINGLE)
NameError: name 'STRATEGY' is not defined
Remote Interpreter Reinitialized
Traceback (most recent call last):
File "C:\Users\Doug\Documents\Stocks\Python\Option Order.py", line 34, in
new_order.order_strategy_type(orderStrategyType = 'SINGLE')
TypeError: order_strategy_type() got an unexpected keyword argument 'orderStrategyType'
How can I get this parameter correct?
Here is the full code that I started from the example. thanks for any help
`from td.client import TDClient
from td.orders import Order, OrderLeg
from td.enums import ORDER_SESSION, DURATION, ORDER_INSTRUCTIONS, ORDER_ASSET_TYPE, ORDER_TYPE
accountNum = "XXXXXXX"
stock = "SPY"
optionDate = "081121"
optionBase = stock + "_" + optionDate
optionSymbol = "SPY_081121P442"
-comment SPY option sample "SPY_071221P435"
-comment Initalize a new Order Object.
new_order = Order()
-comment Define the SESSION of the Order - ENUM EXAMPLE.
new_order.order_session(session=ORDER_SESSION.NORMAL)
-comment Define the SESSION of the Order - STRING EXAMPLE.
-comment new_order.order_session(session = 'NORMAL')
-comment Define the DURATION of the Order - ENUM EXAMPLE.
new_order.order_duration(duration=DURATION.DAY)
-comment Define the DURATION of the Order - STRING EXAMPLE.
-comment new_saved_order.order_duration(duration = 'GOOD_TILL_CANCEL')
new_order.order_type(order_type=ORDER_TYPE.MARKET)
-comment"orderStrategyType": "SINGLE"
new_order.order_strategy_type(orderStrategyType = 'SINGLE')
-commentnew_order.order_strategy_type(order_strategy_type=STRATEGY.SINGLE)
-comment Define a new OrderLeg Object.
new_order_leg = OrderLeg()
-comment Define the ORDER INSTRUCTION - ENUM EXAMPLE.
new_order_leg.order_leg_instruction(instruction=ORDER_INSTRUCTIONS.BUY)
-comment Define the ORDER INSTRUCTION - STRING EXAMPLE.
-comment new_order_leg.order_leg_instruction(instruction = 'SELL')
-comment Define the PRICE - CAN ONLY BE A FLOAT.
-commentnew_order_leg.order_leg_price(price=112.50)
-comment Define the QUANTITY - CAN ONLY BE A INTEGER.
new_order_leg.order_leg_quantity(quantity=1)
-comment Define the ASSET to be traded - ENUM EXAMPLE -- SYMBOL MUST ALWAYS BE A STRING.
new_order_leg.order_leg_asset(asset_type=ORDER_ASSET_TYPE.OPTION, symbol=optionSymbol)
-comment Define the ASSET to be traded - STRING EXAMPLE -- SYMBOL MUST ALWAYS BE A STRING.
-commentnew_order_leg.order_leg_asset(asset_type='EQUITY', symbol='MSFT')
-comment Order Legs can be copied so you have a template to build off of.
copied_order_leg = new_order_leg.copy()
-comment Once we have built our order leg, we can add it to our OrderObject.
new_order.add_order_leg(order_leg=new_order_leg)
-comment Print it out so you can see it, normally this method is called by the TD API when using the td.client object.
print("YOU HAVE {} ORDER(S) IN THE ORDER LEGS COLLECTION.".format(
new_order.order_legs_count)
)
print('-'*80)
-comment We could also add a copied order to our order leg collection.
-commentnew_order.add_order_leg(order_leg=copied_order_leg)
-comment Print it out so we can see both now.
-comment print("YOU HAVE {} ORDER(S) IN THE ORDER LEGS COLLECTION.".format(
-comment new_order.order_legs_count)
-comment )
-comment print('-'*80)
-comment
-comment SOME ORDERS, USUALLY THE MORE COMPLEX ONES, HAVE A CHILD ORDER.
-comment A CHILD ORDER IS SIMPLY A REGULAR ORDER OBJECT BUT IS A DESCENDANT OF THE
-comment MAIN PARENT ORDER OBJECT. CHILD ORDERS NORMALLY EMPHASIZE CONDITIONS IN
-comment TRADING.
-comment
-comment Let's take our Order Object and create a new child order using the
create_child_order_strategy
method.child_order = new_order.create_child_order_strategy()
-comment Define the SESSION of our CHILD ORDER - ENUM EXAMPLE.
child_order.order_session(session=ORDER_SESSION.NORMAL)
-comment Define the SESSION of our CHILD ORDER - STRING EXAMPLE.
-comment child_order.order_session(session = 'PM')
-comment Define the DURATION of our CHILD ORDER - ENUM EXAMPLE.
child_order.order_duration(duration=DURATION.DAY)
-commentnew_order.order_type(order_type=ORDER_TYPE.MARKET)
child_order.order_type(order_type=ORDER_TYPE.TRAILING_STOP)
child_order.stop_price_offset(stop_price_offset=.03)
-comment Define the DURATION of our CHILD ORDER - STRING EXAMPLE.
-commentchild_order.order_duration(duration='FILL_OR_KILL')
-comment Child Orders, because they're an Order can also have their own OrderLeg Object. Let's create one.
child_order_leg = OrderLeg()
-comment Define the ORDER LEG INSTRUCTION for the CHILD ORDER LEG - ENUM EXAMPLE.
child_order_leg.order_leg_instruction(instruction=ORDER_INSTRUCTIONS.SELL)
-comment Define the ORDER LEG INSTRUCTION for the CHILD ORDER LEG - STRING EXAMPLE.
-comment child_order_leg.order_leg_instruction(instruction = 'SELL')
-comment Define the PRICE of the CHILD ORDER LEG - MUST BE FLOAT.
-commentchild_order_leg.order_leg_price(price=300.50)
-comment Define the QUANTITY of the CHILD ORDER LEG - MUST BE INTEGER.
child_order_leg.order_leg_quantity(quantity=1)
-comment Define the ASSET to be traded - ENUM EXAMPLE -- SYMBOL MUST ALWAYS BE A STRING.
child_order_leg.order_leg_asset(
asset_type=ORDER_ASSET_TYPE.OPTION,
symbol=optionSymbol
)
-comment Define the ASSET to be traded - STRING EXAMPLE -- SYMBOL MUST ALWAYS BE A STRING.
-commentchild_order_leg.order_leg_asset(asset_type='EQUITY', symbol='AMZN')
-comment Take the CHILD ORDER LEG and it to the CHILD ORDER STRATEGY'S ORDER LEG COLLECTION.
child_order.add_order_leg(order_leg=child_order_leg)
-comment Display the count.
print("YOU HAVE {} ORDER(S) IN THE ORDER LEGS COLLECTION.".format(
child_order.order_legs_count)
)
print('-'*80)
-comment Add the Child Order Strategy to the PARENT ORDER or in other words the order we created originally.
new_order.add_child_order_strategy(child_order_strategy=child_order)
-comment Print it so the user can see all the info at once.
print("YOU HAVE {} CHILD ORDER STRATEGIES IN THE CHILD ORDER STRATEGIES COLLECTION.".format(
new_order.child_order_count)
)
-comment submit the order
-comment Create a new session, credentials path is required.
TDSession = TDClient(
client_id=accountNum,
redirect_uri='http://localhost',
credentials_path= r'C:\Users\Documents\Stocks\Python\td_state.json',
auth_flow='flask'
)
orderResponse = TDSession.place_order(account = accountNum, order = new_order)
print(orderResponse)`
Beta Was this translation helpful? Give feedback.
All reactions