Komnomnomnom / swigibpy

Third party Interactive Brokers Python API generated from TWS C++ API using SWIG.
http://github.com/Komnomnomnom/swigibpy/
Other
157 stars 35 forks source link

Data for multiple tickers #22

Closed pavankumar4uin closed 10 years ago

pavankumar4uin commented 10 years ago

I am trying to get data for multiple tickers using swigibpy. Following is my code.

from swigibpy import EWrapper, EPosixClientSocket, Contract, Order, TagValue, TagValueList, LAST, LAST_SIZE, CLOSE
from time import sleep
class getTickData(EWrapper):
    def openOrderEnd(self):
        '''Not relevant for our example'''
        pass

    def execDetails(self, id, contract, execution):
        '''Not relevant for our example'''
        pass

    def managedAccounts(self, openOrderEnd):
        '''Not relevant for our example'''
        pass

    def nextValidId(self, validOrderId):
        '''Capture the next order id'''
        global orderId
        orderId = validOrderId

    def orderStatus(self, id, status, filled, remaining, avgFillPrice, permId,
                    parentId, lastFilledPrice, clientId, whyHeld):

        print(("Order #%s - %s (filled %d, remaining %d, avgFillPrice %f,"
               "last fill price %f)") % (
                  id, status, filled, remaining, avgFillPrice, lastFilledPrice))

    def tickString(self, tickerId, tickType, value):
        pass

    def tickOptionComputation(self, tickerId, field, impliedVolatility, delta):
        pass

    def tickSize(self, tickerId, field, size):
        global lastSize, tickData
        tickData[field] = size
        if field == LAST_SIZE:
            lastSize = size

    def tickGeneric(self,tickerId,tickType,value):
        pass

    def tickPrice(self,tickerId,field, price, canAutoExecute):
        global lastPrice, tickData
        tickData[field] = price
        if field == CLOSE:
            lastPrice = price

    def tickSnapshotEnd(self, req_id):
        pass
def make_contract(symbol,sectype,exchange,currency,expiry,strike,right):
    cont = Contract()
    cont.symbol = symbol
    cont.secType = sectype
    cont.exchange = exchange
    cont.currency = currency
    cont.expiry = expiry
    cont.strike = strike
    cont.right = right
    return cont
symbols = ['MSFT','C']#,'INTC','GOOG']
if __name__ == '__main__':
    callback = getTickData()
    tws = EPosixClientSocket(callback)
    tws.eConnect("",7496,20)
    req_id=0
    for symbol in symbols:
        lastPrice = lastSize = None
        tickData = {}
        symbol_contract = make_contract(symbol, 'STK', 'SMART', 'USD', '', 0.0, '')
        tws.reqMktData(req_id,symbol_contract,'',True)
        while lastPrice is None:
            print 'For symbol %s lastPrice is still None' % symbol
            sleep(1)
        print tickData
        tws.cancelMktData(req_id)
        req_id += 1

tws.eDisconnect()

When I run the above code for some tickers I am not getting the tickPrice for CLOSE field hence. Same code works fine some times. Please look into this issue and let me know if I am doing some thing wrong.

Komnomnomnom commented 10 years ago

The code you provided works for me. Please re-open if you can reproduce and provide further information (exact tickers, the output you see etc).

pavankumar4uin commented 10 years ago

Hi,

I am still seeing the issue. As I mentioned, code works for once but later it fails when I try running multiple times.

Regards Pavan

On Thu, Nov 14, 2013 at 5:42 AM, Komnomnomnom notifications@github.comwrote:

The code you provided works for me. Please re-open if you can reproduce and provide further information (exact tickers, the output you see etc).

— Reply to this email directly or view it on GitHubhttps://github.com/Komnomnomnom/swigibpy/issues/22#issuecomment-28447689 .

pavankumar4uin commented 10 years ago

Hi

Can you please looking into this?

Regards Pavan