defbuy(self):
self.symbol = varSymbol.get() # gets the symbol string from the symbol combo box
self.quantity = varQuantity.get() # gets the share size from the quantity spinbox
self.order_type = varOrderType.get() # gets the order type for the order type combobox
self.limit_price = varLimitPrice.get() # gets the limit price from the limit price spinbox# calls the function place_market order passes variables# symbol, quantity, order type, buy or sell represeted by true or false, and limit price
self.place_market_order(self.symbol, self.quantity, self.order_type, True, self.limit_price)
defsell(self):
self.symbol = varSymbol.get()
self.quantity = varQuantity.get()
self.order_type = varOrderType.get()
self.limit_price = varLimitPrice.get()
self.place_market_order(self.symbol, self.quantity, self.order_type, False, self.limit_price)
Place order function
defplace_market_order(self, symbol, quantity, order_type, is_buy, limit_price):
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)
self.tws_conn.placeOrder(self.order_id, contract, order)
# increses the order id by one
self.order_id += 1 # add number to order id so it is ready for the next order# must be increase higher than the previous number
add some methods or functions for requesting streaming data
add events to the combobox
change text from lowercase to upper case
select all text in combobox
add items to the drop down list
Uncomment out these 2 lines of code in the widgets function under the combo box widget
the one that is called cbSymbol combobox symbol
self.cbSymbol.bind('<Return>', self.cbSymbol_onEnter) #when the enter key is press an event happens
self.cbSymbol.bind('<<ComboboxSelected>>',self.cbSymbol_onEnter)
add all these functions to your Python program. Added some extra code that was not covered in the YouTube video that is highlighted in yellow.
defcbSymbol_onEnter(self, event):
# cancels Account updates
self.tws_conn.reqAccountUpdates(False, self.account_code)
# changes characters to upper case
varSymbol.set(varSymbol.get().upper())
# gets the value of the text from the combobox. cbSymbol# and adds it to the variable mytext
mytext = varSymbol.get()
# gets list of values from dropdwn list of# cbSymbol combobox
vals = self.cbSymbol.cget('values')
# selects all in the combobox. cbSymbol
self.cbSymbol.select_range(0, END)
# checks if symbol exists in the combobox if not it adds it# to the dropdown listif not vals:
self.cbSymbol.configure(values = (mytext, ))
elif mytext not in vals:
self.cbSymbol.configure(values = vals + (mytext, ))
mySymbol = varSymbol.get()
self.symbol = mySymbol
# calls the cancel_market_data() method
self.cancel_market_data()
# sets the text boxes for position and average price to zero#varPosition.set('0')#varAvgPrice.set('0.00')# calls the method to request streaming data
self.request_market_data(self.symbol_id, self.symbol)
# calls method to request account updates
self.request_account_updates(self.account_code)
# sets bid and ask price to zero
varBid.set('0.00')
varAsk.set('0.00')
defrequest_account_updates(self, account_code):
self.tws_conn.reqAccountUpdates(True, self.account_code)
defcancel_market_data(self):
self.tws_conn.cancelMktData(self.symbol_id)
defrequest_market_data(self, symbol_id, symbol):
contract = self.create_contract(symbol,
'STK',
'SMART',
'NASDAQ',
'USD')
self.tws_conn.reqMktData(symbol_id, contract, '', False)
#time.sleep(1) deftick_event(self, msg):
if msg.tickerId == 0: # added this to the code not shown in video ********* if msg.field == 1: # 1 is for the bid price
self.bid_price = msg.price
elif msg.field == 2: # 2 is for the ask price
self.ask_price = msg.price
elif msg.field == 4: # 4 represents the last price
self.last_prices = msg.price
self.monitor_position(msg)
defcreate_contract(self, 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
defregister_callback_functions(self):
# Assign server messages handling function.
self.tws_conn.registerAll(self.server_handler)
# Assign error handling function.
self.tws_conn.register(self.error_handler, 'Error')
# Register market data events.
self.tws_conn.register(self.tick_event,
message.tickPrice,
message.tickSize)
defserver_handler(self, msg):
if msg.typeName == "nextValidId":
self.order_id = msg.orderId
elif msg.typeName == "managedAccounts":
self.account_code = msg.accountsList
elif msg.typeName == "updatePortfolio" \
and msg.contract.m_symbol == self.symbol \
and msg.contract.m_secType == 'STK': # added this to my code not shown in video ******
self.unrealized_pnl = msg.unrealizedPNL
self.realized_pnl = msg.realizedPNL
self.position = msg.position
self.average_price = msg.averageCost
elif msg.typeName == "error"and msg.id != -1:
returndeferror_handler(self, msg):
if msg.typeName == 'error'and msg.id != -1:
print ('Server Error:', msg)
defmonitor_position(self, msg): #*print ('Last Price = %s' % (self.last_prices))
varLast.set(self.last_prices)
varBid.set(self.bid_price)
varAsk.set(self.ask_price)
Create a trading platform using Python programming language.
Trading platform how to video in python part 3 creating the form or window.
Tutorial adding the different tools, or widgets on a form as it relates to the stock market, using the Python programming language, and tkinter.
always be aware of your indents in your code
self.btnDisconnect = ttk.Button(self, text = "Disconnect", command=self.disconnect_it).grid(row=0, column=1, sticky=W)
#notebook
n = ttk.Notebook(root, width=550, height=350)
f1 = ttk.Frame(n) # first page, which would get widgets gridded into it
f2 = ttk.Frame(n) # second page
n.add(f1, text='One')
n.add(f2, text='Two')
n.grid(row=3, column=0, padx=5, pady=5, sticky=W)
#create listbox
self.listbox1 = Listbox(f1, font=('Lucida Grande',9), width=7)
#self.listbox1.bind('', self.OnDoubleClick_listbox)
self.listbox1.insert(1, 'NFLX')
self.listbox1.insert(2, 'AAPL')
self.listbox1.insert(3, 'FB')
self.listbox1.grid(row=0, rowspan=5, column=0, padx=5)
#create Label Symbol
self.label4 = Label(f1, font=myFont, text="Symbol").grid(row=0, column =1)
#create Label Quantity
self.label5 = Label(f1, font=myFont, text="Quantity").grid(row=0, column =2)
#create Label Limit Price
self.label6 = Label(f1, font=myFont, text="Limit Price").grid(row=0, column =3)
#create Label Market
self.label7 = Label(f1, font=myFont, text="Market").grid(row=0, column =4)
#create combo box for the Symbol
self.cbSymbol = ttk.Combobox(f1, font=myFont, width=6, textvariable = varSymbol)
#self.cbSymbol.bind("", self.cbSymbol_onEnter) #when the enter key is press an event happens#self.cbSymbol.bind('<>',self.cbSymbol_onEnter)
self.cbSymbol['values'] = ('AAPL','FB','NFLX')
self.cbSymbol.grid(row=1, column =1,sticky = W)
#create spinbox (numericUpDown) for Limit Price
self.spinQuantity = Spinbox(f1, font=myFont, increment=100, from_=0, to=10000, width=7,
textvariable=varQuantity).grid(row=1, column=2)
#create spinbox (numericUpDown) for Limit Price ****** note: make sure there is an underscore at the end of the word from
self.spinLimitPrice = Spinbox(f1, font=myFont, format='%8.2f', increment=.01, from_=0.0, to=1000.0, width=7,
textvariable=varLimitPrice)
# when control and up or down arrow are pressed call spenLimitDime()#self.spinLimitPrice.bind('', self.spinLimitDime)# when Alt and up or down arrow are pressed call spenLimitPenny()#self.spinLimitPrice.bind('', self.spinLimitPenny)
self.spinLimitPrice.grid(row=1, column=3)
#create textbox(Entry box) for the Market
self.cbMarket = ttk.Combobox(f1, font=myFont, width=7, textvariable=varMarket).grid(row=1, column=4, sticky = W)
#create Label OrderType ********-3-****
self.label8 = Label(f1, font=myFont, text="OrderType").grid(row=2, column =1, sticky=W)
#create Label Visible
self.label9 = Label(f1, font=myFont, text="Visible").grid(row=2, column =2)
#create Label Primary Exchange
self.labe20 = Label(f1, font=myFont, text="Primary Ex.").grid(row=2, column =3)
#create Label Time in Force
self.labe21 = Label(f1, font=myFont, text="TIF").grid(row=2, column =4)
#create textbox(Entry box) for the Order Type ****4****
self.cbOrderType = ttk.Combobox(f1, font=myFont, width=6, textvariable=varOrderType)
self.cbOrderType['values'] = ('LMT','MKT','STP', 'STP LMT', 'TRAIL', 'MOC','LOC')
self.cbOrderType.grid(row=3, column =1,sticky = W)
#create textbox(Entry box) for the Primary Exchange
self.tbPrimaryEx = Entry(f1, font=myFont, width=8, textvariable=varPrimaryEx).grid(row=3, column =3,sticky = W)
#create textbox(Entry box) for the Time in Force
self.cbTIF = ttk.Combobox(f1, font=myFont, width=7, textvariable=varTIF)
self.cbTIF['values'] = ('DAY','GTC')
self.cbTIF.grid(row=3, column =4,sticky = W)
#create Bid Label
self.label2 = Label(f1, font=myFont, text="Bid", width=7).grid(row=4, column=2)
#create Ask Label
self.label3 = Label(f1, font=myFont, text="Ask", width=7).grid(row=4, column=3)
#create textbox(Entry box) for the Bid price
self.tbBid = Entry(f1, font=myFont, width=7, textvariable = varBid)
#self.tbBid.bind("", self.tbBid_Click)
self.tbBid.grid(row=5, column =2, sticky=E)
#create textbox(Entry box) for the Ask price
self.tbAsk = Entry(f1, font=myFont, width=7, textvariable = varAsk)
#self.tbAsk.bind("", self.tbAsk_Click)
self.tbAsk.grid(row=5, column=3)
#create a sell button ***
self.btnSell = Button(f1, font=('Lucida Grande',10,'bold'), text="SELL", width=9, bg="red", fg="white")
self.btnSell.grid(row=5, column=1, sticky=W)
#create a buy button ***
self.btnBuy = Button(f1, font=('Lucida Grande',10,'bold'), text="BUY", width=9, bg="green", fg="white")
self.btnBuy.grid(row=5, column=4, sticky=E)
#create Label
self.label1 = Label(f1, font=myFont, width=8, text="Last").grid(row=6, column =1)
#create textbox(Entry box) for the last price
self.tbLast = Entry(f1, font=myFont, width=8, textvariable = varLast).grid(row=6, column =2,sticky = W)
[showhide type="links3" more_text=" * Show code for the disconnect method" less_text=" * Hide code for the disconnect method"]
Create a trading platform using Python programming language.
connect to tws
from ib.ext.Contract import Contract
from ib.ext.Order import Order
from ib.opt import Connection, message
from tkinter import *
from tkinter import ttk
import time
class Application(Frame):
def__init__(self, master):
"""Initialize the Frame"""
ttk.Frame.__init__(self, master)
self.port=7496
self.client_id = 86 # this can be any number
self.grid()
self.create_widgets()
defcreate_widgets(self):
""" create the window layout. """# create connect button widget
self.btnConnect = ttk.Button(self, text='Connect', command=self.connect_to_tws)
self.btnConnect.grid(row=0, column=0)
defconnect_to_tws(self):
self.tws_conn = Connection.create(port=self.port,
clientId=self.client_id)
self.tws_conn.connect()
root = Tk()
root.title("Connect to IB TWS with Python")
root.geometry('600x480')
root.attributes('-topmost', True)
app = Application(root)
root.mainloop()
Change spinbox increment value programmatically from 10 cents to 1 cent, using control click arrow up or down.
Format for 8 characters and 2 decimal places, this should be include otherwise you can get different numbers.
Please comment below if you have a better way of doing the same basic thing.
please see link below for other example using a combobox
# Craig Hammond from tkinter import *
from tkinter import ttk
class Application(Frame):
"""A GUI application """def__init__(self, master):
""" Initialize the Frame"""
ttk.Frame.__init__(self, master)
self.grid()
self.create_widgets()
self.increment_switch = True# used as a switch
defcreate_widgets(self):
""" create the window layout. """
myFont = ('Lucida Grande', 12)
#create Label Limit Price
self.label2 = Label(self, font=myFont, text="Limit Price").grid(row=0, column=0)
# create a Spinbox
self.spinLimitPrice = Spinbox(self, font=myFont, format='%8.2f',
increment=.01, from_=0.0, to=1000.0, width=7,
textvariable=varLimitPrice)
# when control and up or down arrow are pressed call spinLimitDime()
self.spinLimitPrice.bind('<Control-Button-1>', self.spinLimitDime)
self.spinLimitPrice.grid(row=2, column=3)
def spinLimitIncrement(self, event):
if self.increment_switch == True:
self.spinLimitPrice.configure(increment=0.10)
self.increment_switch = Falseelse:
self.spinLimitPrice.configure(increment=0.01)
self.increment_switch = True
root = Tk()
root.title("Spinbox")
root.geometry("600x480")
varLimitPrice = StringVar()
app = Application(root)
root.mainloop()
on Enter event, when the enter key is pressed.
changes text to uppercase.
adds item to list if not in the drop down list.
highlights, selects all text, this allows you to keep typing a new symbol without placing your mouse in the box and erasing the previous symbol so you can type a new one.
# Craig Hammond 2016from tkinter import *
from tkinter import ttk
classApplication(Frame):
"""A GUI application """def__init__(self, master):
""" Initialize the Frame"""
ttk.Frame.__init__(self, master)
self.grid()
self.create_widgets()
defcreate_widgets(self):
""" create the window layout. """
myFont = ('Lucida Grande', 12)
#create Label Symbol
self.label2 = Label(self, font=myFont, text="Symbol").grid(row=0, column=0)
# create combobox for the symbol
self.cbSymbol = ttk.Combobox(self, font=myFont, width=7, textvariable = varSymbol)
self.cbSymbol.bind("<Return>", self.cbSymbol_onEnter) #when the enter key is press an event happens
self.cbSymbol.bind('<<ComboboxSelected>>',self.cbSymbol_onEnter)
self.cbSymbol['values'] = ('AAPL','FB','NFLX')
self.cbSymbol.grid(row=0, column =1,sticky = W)
defcbSymbol_onEnter(self, event):
# changes characters to upper case
varSymbol.set(varSymbol.get().upper())
# gets the value of the text in the combobox. cbSymbol
mytext = varSymbol.get()
# gets list of values from dropdwn list of
# cbSymbol combobox
vals = self.cbSymbol.cget('values')
# selects all in the combobox. cbSymbol
self.cbSymbol.select_range(0, END)
print(mytext)
# checks if symbol exists in the combobox if not it adds it
# to the dropdown listif not vals:
self.cbSymbol.configure(values = (mytext, ))
elif mytext not in vals:
self.cbSymbol.configure(values = vals + (mytext, ))
return'break'
root = Tk()
root.title("Combobox")
root.geometry("600x480")
varSymbol = StringVar(root, value='NFLX')
app = Application(root)
root.mainloop()