Sunday, 9 October 2016

IB TWS Historical Data in Python 2

 


from time import sleep, strftime, localtime, time
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

class Application(Frame):
    """ a gui application"""

    def __init__(self, master):
        """ Initialize the Frame"""
        ttk.Frame.__init__(self, master)

        self.port=7496
        self.client_id = 97 # this number can be any number
        self.symbol_id, self.symbol = 5, 'MSFT'
        self.account_code = None
        self.strData = ""
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        """ create the window layout. """
        now = strftime('%Y%m%d %H:%M:%S', localtime(int(time())))
        myfont=('Arial', 12)
        myFont = ('Lucida Grande',12)
        
        # create a connect to tws button
        self.btnConnect = ttk.Button(self, text = "Connect", command=self.connect_to_tws)
        self.btnConnect.grid(row=0, column=1, sticky=W)

        # create a button to request the data
        self.btnGetData = ttk.Button(self, text = "Get Data", command=self.contract_creation)
        self.btnGetData.grid(row=0, column=2, sticky=W)

        # create a label for Symbol
        self.label_symbol = Label(root, text='Symbol').grid(row=1, column=0)
        
        # create a combobox for security symbol
        self.cbSymbol = ttk.Combobox(root, font=myfont, textvariable=varSymbol).grid(row=2, column=0)
        
        # create label for the endDateTime
        self.label_datetime = Label(root, font=myfont, text='End Date').grid(row=3, column=0)

        # create label for the duration
        self.label_duration = Label(root, font=myfont, text='Duration').grid(row=3, column=1)

        # create label for the barSizeSetting
        self.label_bar_size = Label(root, font=myfont, text='Bar Size').grid(row=3, column=2)

        # create Entry box (textbox) for the endDateTime
        self.tbDateTime = Entry(root, font=myfont, textvariable=varDateTime).grid(row=4, column=0)
        
        # create Combo box (textbox) for the duration 
        self.cbDuration = ttk.Combobox(root, font=myfont, textvariable=varDuration)
        self.cbDuration['values'] = ('1 Y', '1 M', '6 M', '1 D', '2 D')
        self.cbDuration.grid(row=4, column=1, sticky=W)

        # create Combo box (textbox) for the barSizeSetting
        self.cbBarSize = ttk.Combobox(root, font=myfont, textvariable=varBarSize)
        self.cbBarSize['values'] = ('1 day', '1 min', '2 mins', '5 mins')
        self.cbBarSize.grid(row=4, column=2, sticky=W)
        
        varDateTime.set(now)

        # create a listbox for the data make it 2 lines or the
        # listbox1.insert will not work you get an error
        # damn that is frustrating
        self.listbox1 = Listbox(root, font= ('', 12), width=75, height=30)
        self.listbox1.grid(row=6, column=0, columnspan=5, padx=5, pady=5, sticky='w')
            
    def connect_to_tws(self):
        self.tws_conn = Connection.create(port=7496, clientId=5)
        self.tws_conn.connect()

    def contract_creation(self):
        pass

root = Tk()

root.title("Historical data from IB TWS in Python please donate")
root.geometry("600x670")
root.attributes("-topmost", True)

varDateTime = StringVar()
varDuration = StringVar(root, value='1 D')
varBarSize = StringVar(root, value='5 mins')
varSymbol = StringVar(root, value='MSFT')

app = Application(root)

root.mainloop()



Pages: 1 2 3 4 5 6


Save

No comments:

Post a Comment