TWS API not placing orders

1,156 views
Skip to first unread message

ELIAS ABSAWI

unread,
Aug 20, 2020, 1:54:01 PM8/20/20
to IbPy Discuss
Greetings everyone, hope you're doing amazing

I have a test algorithm just to test my API connection and orders.
I run the python module and make a connection (I receive a notification from IBKR to accept incoming connection and I accept) but the problem is the order doesn't show up nor come through.
Any suggestions on what should I do?

Here is the test algorithm if it might help: (Client Id & Port were hidden on purpose)

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

def make_contract(symbol, sec_type, exch, prim_exch, curr):
    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 make_order(order_type, action, quantity):
 
    order = Order()
    order.m_orderType = order_type
    order.m_totalQuantity = quantity
    order.m_action = action
    return order

         


def main():
    conn = Connection.create(port=**** , clientId=***)
    conn.connect()

    oid = 200

    cont = make_contract('TSLA', 'STK' , 'SMART' , 'SMART' , 'USD')
    offer = make_order('MKT' , 100 , 'BUY')

    conn.placeOrder (oid , cont , offer)

    conn.disconnect()


main()

Derek Tishler

unread,
Aug 20, 2020, 4:23:45 PM8/20/20
to IbPy Discuss
Your contract will need to be formed, inside make_contract, like:
contract = Contract()
Currently it is using the imported object instead.

Perhaps you will have luck with error messages:
https://www.quantstart.com/articles/Using-Python-IBPy-and-the-Interactive-Brokers-API-to-Automate-Trades/

Also you can monitor open orders for a next step:
https://stackoverflow.com/questions/40186531/ibpy-getting-order-status-update
https://stackoverflow.com/questions/37746433/how-do-i-retrieve-the-status-of-open-orders-using-ibpy/37755904#37755904

ELIAS ABSAWI

unread,
Aug 21, 2020, 5:37:21 AM8/21/20
to IbPy Discuss
Thank you so so much, I really appreciate your answer.

Sending you the best,
Elias.
Message has been deleted

ELIAS ABSAWI

unread,
Aug 21, 2020, 10:12:20 AM8/21/20
to IbPy Discuss
I edited the code, unfortunately it did not work :/ 
I'll try looking into the links above.

Many thanks,
Elias. 

Derek Tishler

unread,
Aug 21, 2020, 10:36:25 AM8/21/20
to IbPy Discuss
Does your new make_contract function resemble:
Source: https://blog.quantinsti.com/ibpy-tutorial-implement-python-interactive-brokers-api/

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

Also, for me, instead of connection the preferred client interface is called ibConnection. Which version of ibpy are you using?:
from ib.opt import ibConnection, message
https://github.com/blampe/IbPy

Try the error registration to avoid silent failures, but to give it time to reutrn errors or even submit the order perhaps you also want to try a short sleep after placeOrder in order to ensure the program is not disconnecting before receiving and printing any error messages.

ELIAS ABSAWI

unread,
Aug 21, 2020, 2:21:12 PM8/21/20
to IbPy Discuss
Thank you so much, I'll do the edits right now and update you...
I really appreciate your help.

ELIAS ABSAWI

unread,
Aug 21, 2020, 2:56:47 PM8/21/20
to IbPy Discuss
Tried everything...still not working.
I think I might change to the native API (IBapi).
I'll update you if it works out for me.
I really appreciate your help, thank you so so much.

Have a wonderful weekend, 
Elias.

Derek Tishler

unread,
Aug 21, 2020, 5:53:40 PM8/21/20
to IbPy Discuss
I am sorry to hear that, it should not be so tricky with ibpy...I can review the code again if posted, or there could be an issue with TWS settings or something making the issue external to ibpy(can try confirming connection via ibpy or in tws or other things to figure out the issue as it may also occur in the native api).

Perhaps issue in tws settings or how the connection is being made.(In top right of TWS, it says Data, can click on that to confirm connected clients vs using code; but avoid disconnecting quickly, or sleep long enough, so you can inspect before disconnecting in your code):
https://stackoverflow.com/questions/32927186/connecting-to-ib-tws-using-ibpy

Can get ideas from Remi's great example: (note his use of Connection vs ibConnection, but it required .crate())
https://groups.google.com/d/msg/ibpy-discuss/PrTQf2kH1Cc/xK8OptGKBQAJ

However, if you are keen to switch to the newer, native api(could be a good idea for new features) then perhaps these walkthroughs can be of use:
https://algotrading101.com/learn/interactive-brokers-python-api-native-guide/
https://www.youtube.com/watch?v=DDbYsm5mMRM

Good luck!

ELIAS ABSAWI

unread,
Aug 22, 2020, 5:06:58 AM8/22/20
to IbPy Discuss
I can't thank you enough for the help.
I'll try what you suggested to check in TWS before switching to the native.
If problems continued to occur, I'll just switch and follow the walkthroughs above.

All my best,
Elias.

ELIAS ABSAWI

unread,
Aug 22, 2020, 9:52:49 AM8/22/20
to IbPy Discuss
I ran the code, connected and then checked the "data" in top right corner.
Seems that the problem is right here but I don't know what to do.
https://imgur.com/a/DuknYZi

Jake Zimmerman

unread,
Aug 22, 2020, 9:56:40 AM8/22/20
to ibpy-d...@googlegroups.com
Elias - make sure that your order id's are valid. If the next valid id is 201 and you are sending the order with an id of 200, it will not go through. And also, I would switch to ibpythonic. This is the newer version of ibpy, and works basically the same way. Ibpy should work either way though.

--
You received this message because you are subscribed to the Google Groups "IbPy Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ibpy-discuss...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ibpy-discuss/ad1939f1-38f5-40f2-bfcb-23c8cfba9bf9n%40googlegroups.com.

Jake Zimmerman

unread,
Aug 22, 2020, 9:59:21 AM8/22/20
to ibpy-d...@googlegroups.com
You can reset the order id sequence from within TWS, or you can call reqIds(1) and use the nextValidId() callback function to get the current order id.

ELIAS ABSAWI

unread,
Aug 22, 2020, 10:13:19 AM8/22/20
to IbPy Discuss
Thank you so much Jake, 
I think my order IDs are valid since every time I run the code I change it to a higher number...+10 every run (just to make sure).
And if you suggest so then I will change to IBpythonic since it's a newer version.

Many many thanks,
Elias.

Jake Zimmerman

unread,
Aug 22, 2020, 10:38:35 AM8/22/20
to ibpy-d...@googlegroups.com
Hmm. It should work basically the same way on IbPy. I've attached a working script for sending an order with IbPythonic along with what my api settings look like and my console. Hopefully this helps.

pythonicOrder.py
Order Output.png
api settings.png

Derek Tishler

unread,
Aug 22, 2020, 10:40:03 AM8/22/20
to IbPy Discuss
Registering for errors or all messages after you connect will show messages such as 'order id invalid' to pinpoint the issue explicitly and avoid silent failures. You can also reset order id's in TWS api settings; but that is not a fix just a note.

Your issue will probably occur on any python interface to TWS as they all, essentially, act the same. The issue is not a bug or related to ibpy I believe. (But don't be discouraged from switching to a newer, more feature rich version like ibpythonic like Jack suggested).

Would you mind reposting your code again?

Jake Zimmerman

unread,
Aug 22, 2020, 10:46:56 AM8/22/20
to ibpy-d...@googlegroups.com
Make sure that socket ports line up. In this case I have it listening on 7497, so ignore the commented out line that shows port 7496. Also, those server error messages should show up- if they haven't, it means that your connection attempt was unsuccessful. I generally add sleep clauses to ensure that messages get sent through. This isn't the correct way to do this, but it seems to work fine. Generally, this is needed to prevent the client socket from closing before the message actually gets sent back from the server. Ideally, you want to wait for the response, however long it takes, rather than using an arbitrary sleep clause.


ELIAS ABSAWI

unread,
Aug 22, 2020, 11:41:01 AM8/22/20
to IbPy Discuss
Thank you so much for the script and screenshots, I'm currently trying to download IBpythonic and will run it right after I finish.

And this is the code I'm currently using: (IbPy)

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

def make_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 make_order(order_type, action, quantity):
 
    order = Order()
    order.m_orderType = order_type
    order.m_totalQuantity = quantity
    order.m_action = action
    return order

         


def main():
    conn = Connection.create(port=7497 , clientId=999)
    conn.connect()

    oid = 395

    cont = make_contract('TSLA', 'STK' , 'SMART' , 'SMART' , 'USD')
    offer = make_order('MKT' , 100 , 'BUY')

    conn.placeOrder (oid , cont , offer)

    time.sleep (10)

    conn.disconnect()


main()
 


ELIAS ABSAWI

unread,
Aug 22, 2020, 12:18:39 PM8/22/20
to IbPy Discuss
Might IB Gateway work better since it's only dedicated for the API usage?
Should I try?

Jake Zimmerman

unread,
Aug 22, 2020, 12:28:02 PM8/22/20
to ibpy-d...@googlegroups.com
Doesnt matter if you use gateway or tws. Tws would be better in this case because you can see the order in the brokerage platform. Frankly, the only real advantage i see with the gateway is that it doesnt automatically restart the way tws does sometimes.

ELIAS ABSAWI

unread,
Aug 22, 2020, 1:09:23 PM8/22/20
to IbPy Discuss
I see...
I'll stick with TWS then

Patrick Actor

unread,
Aug 22, 2020, 5:00:09 PM8/22/20
to ibpy-d...@googlegroups.com
Probably your problem is the call to make_order; the function requires the parameters be entered in the same sequence as constructed in the function.
offer = make_order('MKT' , 100 , 'BUY')
def make_order(order_type, action, quantity):
Just reverse 100 and 'BUY'.

ELIAS ABSAWI

unread,
Aug 23, 2020, 5:31:13 AM8/23/20
to IbPy Discuss
Makes so much sense, I'll test today on the market open and let you know.
Thank you so much

ELIAS ABSAWI

unread,
Aug 23, 2020, 9:39:00 AM8/23/20
to IbPy Discuss
UPDATE - An order just got placed.
Thank you so so so much. 
I really appreciate it.

ELIAS ABSAWI

unread,
Aug 23, 2020, 9:45:18 AM8/23/20
to IbPy Discuss
I would like to thank all of you. 
Really,  every single one of you gave me a point on what to fix and it worked out perfect.
I couldn't have done this without you, I really appreciate it.

Thank you from the bottom of my heart,
Elias Absawy.

Reply all
Reply to author
Forward
0 new messages