I create a database in the following method
def websocket_con(tickers):
global db
db = sqlite3.connect("C:/ibkr_api/tick_data/ticks.db")
c=db.cursor()
for i in range(len(tickers)):
c.execute("CREATE TABLE IF NOT EXISTS TICKER{} (time datetime primary key, bidPrice real, askPrice real, bidSize integer, askSize integer, midPrice real, volume integer)".format(i))
try:
db.commit()
except:
db.rollback()
app.run()
I then stream data using the following methods and try and insert it into the database.
def streamData(req_num,contract):
"""stream tick leve data"""
app.reqTickByTickData(reqId=req_num,
contract=contract,
tickType="BidAsk",
numberOfTicks=0,
ignoreSize=True)
def tickByTickBidAsk(self, reqId, time, bidPrice, askPrice,bidSize, askSize, tickAttribBidAsk):
super().tickByTickBidAsk(reqId, time, bidPrice, askPrice, bidSize,askSize, tickAttribBidAsk)
c=db.cursor()
for ms in range(100):
try:
midPrice = (bidPrice+askPrice)/2
volume = bidSize + askSize
print(" ReqId:", reqId, "Time:", (dt.datetime.fromtimestamp(time)+dt.timedelta(milliseconds=ms)).strftime("%Y%m%d %H:%M:%S.%f"), "Bidprice:", bidPrice, "Askprice:", askPrice, "Bidsize:", bidSize, "Asksize:", askSize, "Midprice:", midPrice, "Volume:", volume)
vals = [(dt.datetime.fromtimestamp(time)+dt.timedelta(milliseconds=ms)).strftime("%Y%m%d %H:%M:%S.%f"),bidPrice, askPrice, bidSize, askSize, midPrice, volume]
query = "INSERT INTO TICKER{}(time, bidPrice, askPrice, bidSize, askSize, midPrice, volume) VALUES (?,?,?,?,?,?,?)".format(reqId)
c.execute(query,vals)
break
except Exception as e:
print(e)
try:
db.commit()
except:
db.rollback()
Everything streams fine and the datatypes should match but I can't insert it into the database and cannot figure out why.