Saturday, 17 September 2016

IB TWS Trading Platform in Python 5

create a variable to hold the next valid id ( nextValidId )
self.order_id = 555
create button for cancel all orders
 # create button for cancel all
        self.btnCancelAll = Button(f1, font= ('Lucida Grande', 10), text= 'Cancel All',
                                   width=8, bg="blue", fg="white", command=self.cancel_all)
        self.btnCancelAll.grid(row=7, column=2)
click events for text box (Entry) Bid, Ask, and Last prices
    def tbBid_Click(self, event):
        LimitPrice = varBid.get()
        varLimitPrice.set(LimitPrice)

    def tbAsk_Click(self, event):
        LimitPrice = varAsk.get()
        varLimitPrice.set(LimitPrice)    

    def tbLast_Click(self, event):
        LimitPrice = varLast.get()
        varLimitPrice.set(LimitPrice)
Function for Cancel All
 def cancel_all(self):
            self.tws_conn.reqGlobalCancel()
buy and sell click events
    def buy(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)
     def sell(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

    def place_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
create_order function

    def create_order(self, order_type, quantity, action, limit_price):  
        order = Order()
        order.m_orderType = order_type
        order.m_totalQuantity = quantity
        order.m_action = action
        order.m_lmtPrice = limit_price
        return order


Pages: 1 2 3 4 5 6 7 8 9


No comments:

Post a Comment