I use below Python code to collect tick-by-tick daily trades for SPY (using IBRK). Code works fine without thread locking, but if I introduce thread locking I start missing many ticks. My guess is that thread locking is very time consuming and server buffer is really small, so some data gets dumped into nowhere.
I am trying to think of a way to fix this problem, but do not know how to avoid locking thread. Locking thread ensures that I can safely put data into list (or whatever collection structure), before accessing it for analysis/decision making routine.
I ran it on Google compute instance with 2 cores and 8 GB ram which also has IBRK TWS hosted
Any suggestions?
P.S. sorry for cross-posting:
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.ticktype import TickTypeEnum
import time
import threading
class TestApp(EWrapper, EClient):
def __init__(self):
EClient.__init__(self, self)
self.last_exchange = []
self.last_market_time = []
self.last_price = []
self.last_size = []
self.lock_last = threading.Lock()
def error(self, reqId, errorCode, errorString):
print("Error: ", reqId, " ", errorCode, " ", errorString)
def tickByTickAllLast(self, reqId, tickType, time, price, size, tickAtrribLast,
exchange, specialConditions):
super().tickByTickAllLast(reqId, tickType, time, price, size, tickAtrribLast,
exchange, specialConditions)
collectDate(self, exchange, time, price, size)
def collectData(self, exchange, time, price, size):
self.lock_last.acquire()
self.last_exchange = self.last_exchange + [exchange]
self.last_market_time = self.last_market_time + [time]
self.last_price = self.last_price + [price]
self.last_size = self.last_size + [size]
self.lock_last.release()
app = TestApp()
contract = Contract()
contract.symbol = "SPY"
contract.secType = "STK"
contract.exchange = "SMART"
contract.currency = "USD"
contract.primaryExchange = "ARCA"
app.connect("127.0.0.1", 4002, 0)
time.sleep(1)
app.reqTickByTickData(1 ,contract, "Last", 0, False)
api_thread = threading.Thread(target=app.run)
api_thread.start()
I am trying to think of a way to fix this problem, but do not know how to avoid locking thread. Locking thread ensures that I can safely put data into list (or whatever collection structure), before accessing it for analysis/decision making routine.
I ran it on Google compute instance with 2 cores and 8 GB ram which also has IBRK TWS hosted
Any suggestions?
P.S. sorry for cross-posting:
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.ticktype import TickTypeEnum
import time
import threading
class TestApp(EWrapper, EClient):
def __init__(self):
EClient.__init__(self, self)
self.last_exchange = []
self.last_market_time = []
self.last_price = []
self.last_size = []
self.lock_last = threading.Lock()
def error(self, reqId, errorCode, errorString):
print("Error: ", reqId, " ", errorCode, " ", errorString)
def tickByTickAllLast(self, reqId, tickType, time, price, size, tickAtrribLast,
exchange, specialConditions):
super().tickByTickAllLast(reqId, tickType, time, price, size, tickAtrribLast,
exchange, specialConditions)
collectDate(self, exchange, time, price, size)
def collectData(self, exchange, time, price, size):
self.lock_last.acquire()
self.last_exchange = self.last_exchange + [exchange]
self.last_market_time = self.last_market_time + [time]
self.last_price = self.last_price + [price]
self.last_size = self.last_size + [size]
self.lock_last.release()
app = TestApp()
contract = Contract()
contract.symbol = "SPY"
contract.secType = "STK"
contract.exchange = "SMART"
contract.currency = "USD"
contract.primaryExchange = "ARCA"
app.connect("127.0.0.1", 4002, 0)
time.sleep(1)
app.reqTickByTickData(1 ,contract, "Last", 0, False)
api_thread = threading.Thread(target=app.run)
api_thread.start()
