I have this bit of code that performs some algorithms but there's a couple of blatant errors which are difficult to solve, as I am new to Python and IBPy and connection issues are always specialist issues in any language anyways.
- The first problem is that while the class declaration is in essence correct, it stops working as soon as you have two instances of the class 'Downloader' as in the code below. I can easily see that the way to handle this is to either have an error check and not declare a new instance of connection, or simply check if a connection is open and not declare a new instance. An alternative is to not create a new object but in that case I cannot see how to perform the operations in the algorithm so you can understand my predicament here. Also note that in the main program, I initialise another connection but I wonder if this is necessary; so if you're an expert with IBPy, can you tell me if connecting to TWS once by opening a connection in the class 'Downloader' creates a connection direct to the API (which is what I am trying to achieve with the connection in the main program) and there is no more need to initialise a new connection in the main program? And I MUST EMPHASISE I am not a specialist of Python or IBYPy and I seriously need a hand from someone much more knowledgeable please.
- The other egregious issue is the 'requestID'. This is incremented in a first instantiation of the class, but reinitialised in a second instantiation. And again, the only solutions are error check then increment and no more initialisation, or check if non zero and increment, or find a way to not need an instance of a new object. (On an unrelated note, I think there's a sophisticated way to increment c_id and order_id but can't see how for now, so if you have ideas, they're welcome)
Here is the code
import ib
from ib.opt import Connection, message
from ib.ext.Contract import Contract
from ib.ext.Order import Order
class Downloader(object):
field4price = ''
def __init__(self):
self.tws = ibConnection('localhost', 4001, 0)
self.tws.register(self.tickPriceHandler, 'TickPrice')
self.tws.connect()
self._reqId = 1 # current request id
def tickPriceHandler(self,msg):
if msg.field == 4:
self.field4price = msg.price
#print '[debug]', msg
def requestData(self,contract):
self.tws.reqMktData(self._reqId, contract, '', 1)
self._reqId+=1
def make_contract(symbol, sec_type, exch, prim_exch, curr):
Contract.m_symbol = symbol
Contract.m_secType = sec_type
Contract.m_exchange = exch
Contract.m_primaryExch = prim_exch
Contract.m_currency = curr
return Contract
def make_order(action,quantity, price = None):
if price is not None:
order = Order()
order.m_orderType = 'LMT'
order.m_totalQuantity = quantity
order.m_action = action
order.m_lmtPrice = price
else:
order = Order()
order.m_orderType = 'MKT'
order.m_totalQuantity = quantity
order.m_action = action
return order
cid = 303
c_id = 304
while __name__ == "__main__":
d1 = downloader
d2 = downloader
conn = Connection.create(port=7496, clientId=999)
conn.connect()
orderId = cid
oid = c_id
3sigma = 3
sec1 = make_contract('TSLA', 'STK', 'SMART', 'SMART', 'USD')
sec2 = make_contract('AAPL', 'STK', 'SMART', 'SMART', 'USD')
d1.requestData(sec1)
d2.requestData(sec2)
if abs(float(d1.field4price) - float(d2.field4price)) < 3sigma
if float(d1.field4price) < float(d2.field4price)
offer = make_order('BUY', 1, 200)
conn.placeOrder(orderId, sec1, offer)
offer = make_order('SELL', 1, 200)
conn.placeOrder(orderId, sec2, offer)
else
offer = make_order('BUY', 1, 200)
conn.placeOrder(orderId, sec2, offer)
offer = make_order('SELL', 1, 200)
conn.placeOrder(orderId, sec1, offer)
conn.disconnect()
x = raw_input('enter to resend')
c_id += 1
cid +=1