This is one way to place an order through IB's API using
ibPy and Python 2.7.
It places a 'SELL' order for 2 'YM' contracts with expiry '20120921'. Also, 'YM' is on the 'ECBOT' exchange, so, if you want to trade a different future check to see what its exchange is. You can't just change 'YM' to 'ES', for instance. You also have to change 'ECBOT' to 'GLOBEX'.
Also, remember to update your orderID each time you send an order. If you put this into a program you can have it auto-increment after each order. In this simple script, you'll have to do it by hand.
And, if you want to put in a limit order you can change 'MKT' to 'LMT' and make sure you put in a m_lmtPrice, i.e. a limit price. I have it commented out with '#' right now. But it's an easy adjustment.
from ib.ext.Contract import Contract
from ib.ext.Order import Order
from ib.opt import ibConnection, message
sym = 'YM'
shares = 2
action = 'SELL'
orderID = 3
def makeStkContract(sym):
contract = Contract()
contract.m_symbol = 'YM'
contract.m_secType = 'FUT'
contract.m_expiry = '20120921'
contract.m_exchange = 'ECBOT'
contract.m_currency = 'USD'
return contract
def makeStkOrder(shares,action):
order = Order()
order.m_minQty = shares
# order.m_lmtPrice = limit_price
order.m_orderType = 'MKT'
order.m_totalQuantity = shares
order.m_action = str(action).upper()
return order
con = ibConnection()
#con.registerAll(watcher)
con.connect()
stkContract = makeStkContract(sym)
stkOrder = makeStkOrder(shares, action)
con.placeOrder(orderID, stkContract, stkOrder)
con.disconnect()