Reputation: 11
I am new to python and programming but I am learning. I am trying to send an order through python for options to TWS. I keep getting this error message
"Server Error: Server Response: error, Traceback (most recent call last):"
I tried to put an order for stock and it went through but not for options
def create_contract(a,b,c,d):
contract = Contract()
contract.symbol = '%a'
contract.secType = "OPT"
contract.exchange = "SMART"
contract.order_exchange = "SMART"
contract.currency = "USD"
contract.ContractMonth = b
contract.strike = c
contract.right = '%d'
contract.multiplier = "100"
return contract
def create_order(order_type, quantity, action):
order = Order()
order.m_orderType = order_type
order.m_totalQuantity = quantity
order.m_action = action
return order
if __name__ == "__main__":
tws_conn = Connection.create("127.0.0.1", port=7495)
tws_conn.connect()
tws_conn.register(error_handler, 'Error')
tws_conn.registerAll(reply_handler)
order_id = 1
goog_contract = create_contract(tic,hup.cell(2,3).value,hup.cell(2,4).value,hup.cell(2,13).value)
goog_order = create_order('MKT', 1, 'BUY')
tws_conn.placeOrder(order_id, goog_contract, goog_order)
I want to see the option orders goes through TWS. I also would love to see some proper example on how the placeorder parameter should have.
Upvotes: 0
Views: 2103
Reputation:
from ib_insync import *
util.startLoop()
ib=IB()
ib.connect("127.0.0.1", 7497, clientId=13)
contract=Option("SPY","20191016", 280,"P","SMART")
ib.qualifyContracts(contract)
order=MarketOrder("Buy",2)
trade=ib.placeOrder(contract,order)
trade
this code will allow. you to buy 2 puts on the "spy" at a 280 strike expiring October 19 2019
Upvotes: 1
Reputation: 816
It looks like you're using IBPy or a third party wrapper of an IB API? You may want to consider instead using the native Python API from IB, or ib_insync.
If you can place an order for a stock but not an option, the problem is likely in the way the option contract is defined (assuming your paper account has the necessary permissions). There are examples of defining options in the native IB Python API at:
http://interactivebrokers.github.io/tws-api/basic_contracts.html#opt
To say which parameter is incorrect you would have to show the values of a,b,c,d
.
You may want to also enable API logging in TWS as described at:
http://interactivebrokers.github.io/tws-api/support.html#tws_logs
for troubleshooting purposes.
Upvotes: 0