pythonpython-3.xibpy

trying to send order outside regular trading hours (rth) using ibpy


I am new to Python and ibpy, but I´m able to test run my strategy using both. My issue is that I cannot send orders outside the regular trading hours (RTH), when the market is open but ib doesnt consider them a 'regular trading hour'.

below is how I am sending these orders.

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

import time

port = 7496
client = 1
account = "XXXXX"

newContract = Contract()
newContract.m_symbol = "CL"
newContract.m_secType = "FUT"
newContract.m_exchange = "NYMEX"
newContract.m_currency = "USD"
newContract.m_expiry = "201806"

newOrder = Order()
newOrder.m_orderType = "LMT"
newOrder.m_totalQuantity = 1
newOrder.m_action = "BUY"
newOrder.m_lmtPrice = "65"

tws_conn = Connection.create(port=port, clientId=client)
print("------ Connecting.... --------")
tws_conn.connect()
time.sleep(1)
#to simplify I am using a number, but this needs to be updated every new order.
orderId = 1
tws_conn.placeOrder(orderId, newContract, newOrder)

There order goes in nicely during the RTH, but I get this message when we are outside RTH,:

07:45:45:392 -> 4-2-1-399-Order Message:
BUY 1 CL JUN'18 (CLM8) 
Warning: your order will not be placed at the exchange until 2018-05-14 09:30:00 US/Eastern-

Is there a way to enable RTH orders?


Solution

  • Read the docs for order there is plenty of useful information. http://interactivebrokers.github.io/tws-api/classIBApi_1_1Order.html#a60dcca6e3c3ae6ae6e0c5f3fdffc5a4a

    bool OutsideRth [get, set] If set to true, allows orders to also trigger or fill outside of regular trading hours.

    That looks like C# docs but the same is true in ibpy. If you check the source code, Order has a field m_outsideRth = bool(). Just set it to true.