IB TWS Trading Platform in Python 8 outsideRTH
In this tutorial you will add a check box to your gui form window that will allow you to trade after hours and pre-market.Add this variable to hold the variable for the checkbox just above the create widgets function.
self.my_outsideRTH = False
Add this to the end of the create_widgets(self): function
# create a check button box for outside RTH
self.chkOutsideRTH = Checkbutton(f1, font=('', 10),
text='OutsideRTH', variable=varOutsideRTH)
self.chkOutsideRTH.grid(row=9, column=6)
Add highlighted text to the buy(self): function and the sell(self): function
def buy(self):
self.symbol = varSymbol.get()
self.quantity = varQuantity.get()
self.order_type = varOrderType.get()
self.limit_price = varLimitPrice.get()
self.my_outside = varOutsideRTH.get() # add here
the_outsideRTH = 0
if self.my_outside == True:
the_outsideRTH = 1
self.place_market_order(self.symbol, self.quantity, self.order_type,
True, self.limit_price, the_outsideRTH) # add here
def sell(self):
self.symbol = varSymbol.get()
self.quantity = varQuantity.get()
self.order_type = varOrderType.get()
self.limit_price = varLimitPrice.get()
self.my_outside = varOutsideRTH.get() # add here
the_outsideRTH = 0
if self.my_outside == True:
the_outsideRTH = 1
self.place_market_order(self.symbol, self.quantity, self.order_type,
False, self.limit_price, the_outsideRTH) # add here
Add the variable my_outsideRTH to the highlighted areas in the place_market_order() function as shown below.
def place_market_order(self, symbol, quantity, order_type, is_buy, limit_price, my_outsideRTH): # add here
print (symbol, quantity, order_type, is_buy, limit_price)
contract = self.create_contract(symbol,
'STK',
'SMART',
'NASDAQ',
'USD')
# tests if is buy or sell
buysell = 'BUY' if is_buy else 'SELL'
order = self.create_order(order_type, quantity, buysell, limit_price, my_outsideRTH)# add here
self.tws_conn.placeOrder(self.order_id, contract, order)
# increses the order id by one
self.order_id += 1
Add the variable outsideRTH to the highlighted areas in the create_order() function as shown below.
def create_order(self, order_type, quantity, action, limit_price, outside_Rth): # add outside_Rth
order = Order()
order.m_orderType = order_type
order.m_totalQuantity = quantity
order.m_action = action
order.m_lmtPrice = limit_price
order.m_outsideRth = outside_Rth # add order.m_outsideRth = outside_Rth
return order
Add this to the bottom where all the variables are for your window form widgets.
varOutsideRTH = StringVar(root, value=False)