I have been coding for 20+ years but never touched python. I know Perl / C / PHP but python is not like any language I have every deal with. I was wondering if someone out their who knows Python could help me out??
I do not know how to stop the errors at the start when this code runs. Also I do not know how to match the currency pairs since the values are in 2 different arrays.
What I would like is the following.
1. The Pairs to have 3 things printed out.
2. If possible have the data loop until the prices stop changing. I do not know how to accomplish this with out doing some kind of a while(1) and that should never be done in code because it will never stop.
3. Be able to insert this into a SQL database. Only way I know to do that is take this current script and feed it into perl or php and clean up the data and do the calc for mid price and do the sql in perl / php.
So if some one out their who is really good at python would like to clean up this code... I would really appreciate it alot...
This is what I have working so far....
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
# this example shows how to get quotes for multiple contracts at the
# same time and how to tell which is which by using a different
# tickerId for each one
from ib.ext.Contract import Contract
from ib.opt import ibConnection, message
from time import sleep
# print all messages from TWS
def watcher(msg):
if ( msg ):
print msg
#pass
# show Bid and Ask quotes
def my_BidAsk(msg):
#print "WE MADE IT TOO BIDASK"
if msg.field == 1:
print 'Ticker-ID: %s%s:bid: %s' % (contractDict[msg.tickerId][0],
contractDict[msg.tickerId][3], msg.price)
elif msg.field == 2:
print 'Ticker-ID: %s%s:ask: %s' % (contractDict[msg.tickerId][0],
contractDict[msg.tickerId][3], msg.price)
def makeStkContract(contractTuple):
newContract = Contract()
newContract.m_symbol = contractTuple[0]
newContract.m_secType = contractTuple[1]
newContract.m_exchange = contractTuple[2]
newContract.m_currency = contractTuple[3]
newContract.m_expiry = contractTuple[4]
newContract.m_strike = contractTuple[5]
newContract.m_right = contractTuple[6]
#print 'Contract Values:%s,%s,%s,%s,%s,%s,%s:' % contractTuple
return newContract
contractDict = {}
# a stock
contractDict[1] = ('AUD', 'CASH', 'IDEALPRO', 'CAD', '', 0.0, '')
contractDict[2] = ('AUD', 'CASH', 'IDEALPRO', 'CHF', '', 0.0, '')
contractDict[3] = ('AUD', 'CASH', 'IDEALPRO', 'JPY', '', 0.0, '')
contractDict[4] = ('AUD', 'CASH', 'IDEALPRO', 'NZD', '', 0.0, '')
contractDict[5] = ('AUD', 'CASH', 'IDEALPRO', 'USD', '', 0.0, '')
contractDict[6] = ('CAD', 'CASH', 'IDEALPRO', 'CHF', '', 0.0, '')
contractDict[7] = ('CAD', 'CASH', 'IDEALPRO', 'JPY', '', 0.0, '')
contractDict[8] = ('CHF', 'CASH', 'IDEALPRO', 'JPY', '', 0.0, '')
contractDict[9] = ('EUR', 'CASH', 'IDEALPRO', 'AUD', '', 0.0, '')
contractDict[10] = ('EUR', 'CASH', 'IDEALPRO', 'CAD', '', 0.0, '')
contractDict[11] = ('EUR', 'CASH', 'IDEALPRO', 'CHF', '', 0.0, '')
contractDict[12] = ('EUR', 'CASH', 'IDEALPRO', 'GBP', '', 0.0, '')
contractDict[13] = ('EUR', 'CASH', 'IDEALPRO', 'JPY', '', 0.0, '')
contractDict[14] = ('EUR', 'CASH', 'IDEALPRO', 'NZD', '', 0.0, '')
contractDict[15] = ('EUR', 'CASH', 'IDEALPRO', 'USD', '', 0.0, '')
contractDict[16] = ('GBP', 'CASH', 'IDEALPRO', 'AUD', '', 0.0, '')
contractDict[17] = ('GBP', 'CASH', 'IDEALPRO', 'CAD', '', 0.0, '')
contractDict[18] = ('GBP', 'CASH', 'IDEALPRO', 'CHF', '', 0.0, '')
contractDict[19] = ('GBP', 'CASH', 'IDEALPRO', 'JPY', '', 0.0, '')
contractDict[20] = ('GBP', 'CASH', 'IDEALPRO', 'NZD', '', 0.0, '')
contractDict[21] = ('GBP', 'CASH', 'IDEALPRO', 'USD', '', 0.0, '')
contractDict[22] = ('NZD', 'CASH', 'IDEALPRO', 'CAD', '', 0.0, '')
contractDict[23] = ('NZD', 'CASH', 'IDEALPRO', 'CHF', '', 0.0, '')
contractDict[24] = ('NZD', 'CASH', 'IDEALPRO', 'JPY', '', 0.0, '')
contractDict[25] = ('NZD', 'CASH', 'IDEALPRO', 'USD', '', 0.0, '')
contractDict[26] = ('USD', 'CASH', 'IDEALPRO', 'CAD', '', 0.0, '')
contractDict[27] = ('USD', 'CASH', 'IDEALPRO', 'CHF', '', 0.0, '')
contractDict[28] = ('USD', 'CASH', 'IDEALPRO', 'JPY', '', 0.0, '')
#contractDict[29] = ('', 'CASH', 'IDEALPRO', '', '', 0.0, '')
#contractDict[30] = ('', 'CASH', 'IDEALPRO', '', '', 0.0, '')
if __name__ == '__main__':
con = ibConnection('192.168.1.123', 4001, 1)
#con.registerAll(watcher)
con.registerAll(my_BidAsk)
con.unregister(watcher,(message.tickSize,),(message.tickPrice,),(message.tickString,),(message.tickOptionComputation,),(message.tickGeneric,))
#showBidAskOnly = True # set False to see the raw messages
#if showBidAskOnly:
#con.unregister(watcher,(message.tickSize,),(message.tickPrice,),(message.tickString,),(message.tickOptionComputation,),(message.tickGeneric,))
#con.register(my_BidAsk, (message.tickPrice,))
con.connect()
sleep(1)
print '* * * * REQUESTING MARKET DATA * * * *'
for tickId in range(1,29):
stkContract = makeStkContract(contractDict[tickId])
con.reqMktData(tickId, stkContract, '', False)
sleep(999)
print '* * * * CANCELING MARKET DATA * * * *'
for tickId in range(1,29):
con.cancelMktData(tickId)
sleep(1)
con.disconnect()
sleep(1)