Runner Bean
Runner Bean

Reputation: 5195

IbPy cant send order - must specify an account

Im trying to use the following code to try an send an order to TWS to place on google shares. I do not understand why it keeps on asking for an account, I have TWS open and have checked enable ActiveX etc. Ive also checked the socket numbers and client ID are correct.

from ib.ext.Contract import Contract
from ib.ext.Order import Order
from ib.opt import Connection, message
import time

def error_handler(msg):
    print "Server Error: %s" % msg

def reply_handler(msg):
    print "Server Response: %s, %s" % (msg.typeName, msg)

def create_contract(symbol, sec_type, exch, prim_exch, curr):
    contract = Contract()
    contract.m_symbol = symbol
    contract.m_secType = sec_type
    contract.m_exchange = exch
    contract.m_primaryExch = prim_exch
    contract.m_currency = curr
    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(port=7496, clientId=100)
    tws_conn.connect()

    tws_conn.register(error_handler, 'Error')

    tws_conn.registerAll(reply_handler)

    order_id = 200

    goog_contract = create_contract('GOOG', 'STK', 'SMART', 'SMART', 'USD')

    goog_order = create_order('MKT', 5, 'BUY')

    tws_conn.placeOrder(order_id, goog_contract, goog_order)

    time.sleep(1)

    tws_conn.disconnect()

I get the following error

Server Response: error, <error id=200, errorCode=321, errorMsg=Error validating request:-'ie' : cause - You must specify an account.

If I run the code in a demo IB account the order gets placed and filled, so everything works! But when I run the same code in my paper trading account, that is when I get the above error message.

Does anyone know to to "specify an account" i.e. enter my account number somewhere?

Upvotes: 2

Views: 2067

Answers (1)

Runner Bean
Runner Bean

Reputation: 5195

I have working code now, thanks to Brian, see comments on original questions above. I thought I would post the whole script in case anyone else ever had the same problem when trying to implement code from the article "USING PYTHON, IBPY AND THE INTERACTIVE BROKERS API TO AUTOMATE TRADES" from the Quantstart website, which btw is a great website.

So it just came down to putting in a command to give the order an account number by adding an extra argument to the order definition. This argument allows the account number to be specified when submitting the order. In the below script I have not provided my real account number but just wrote 'DUxxxxxx' where the 'x's are numeric numbers, for myself this account number is clearly visible in the top right corner of the TWS GUI.

from ib.ext.Contract import Contract
from ib.ext.Order import Order
from ib.opt import Connection, message
import time

def error_handler(msg):
    print "Server Error: %s" % msg

def reply_handler(msg):
    print "Server Response: %s, %s" % (msg.typeName, msg)

def create_contract(symbol, sec_type, exch, prim_exch, curr):
    contract = Contract()
    contract.m_symbol = symbol
    contract.m_secType = sec_type
    contract.m_exchange = exch
    contract.m_primaryExch = prim_exch
    contract.m_currency = curr
    return contract

def create_order(order_type, quantity, action, account):
    order = Order()
    order.m_orderType = order_type
    order.m_totalQuantity = quantity
    order.m_action = action
    order.m_account = account
    return order

if __name__ == "__main__":
    tws_conn = Connection.create(port=7496, clientId=100)
    tws_conn.connect()

    tws_conn.register(error_handler, 'Error')

    tws_conn.registerAll(reply_handler)

    order_id = 200

    goog_contract = create_contract('GOOG', 'STK', 'SMART', 'SMART', 'USD')

    goog_order = create_order('MKT', 5, 'BUY', 'DUxxxxxx')

    tws_conn.placeOrder(order_id, goog_contract, goog_order)

    time.sleep(1)

    tws_conn.disconnect()

Also one more thing to note which may be obvoius to some but was not to me ta first was that the order_id has to change each time. Each order is unique and reference by the order_id, so it needs to be manually changed if using the above code or something added to increment it.

Happy trading!

Upvotes: 2

Related Questions