I am trying to request real-time tick by tick data, but data arrives with timestamp missing milliseconds. Here is my code:
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.ticktype import TickTypeEnum
import time
from datetime import datetime
import threading
?
?
class TestApp(EWrapper, EClient):
? ? def __init__(self):
? ? ? ? EClient.__init__(self, self)
? ? ? ? self.last_price_list = []
?
? ? def error(self, reqId, errorCode, errorString):
? ? ? ? print("Error: ", reqId, " ", errorCode, " ", errorString)
?
? ? def tickByTickAllLast(self, reqId, tickType, time, price, size, tickAtrribLast, exchange, specialConditions):
? ? ? ? print("Tick Price. Ticker Id:", reqId, "tickType:", TickTypeEnum.to_str(tickType),?
? ? ? ? ? ? ? "Price:", price, "\n",
? ? ? ? ? ? ? "market_time_unix:", time, "\n",
? ? ? ? ? ? ? "market_time:", datetime.fromtimestamp(time).strftime("%Y-%m-%d %H:%M:%S.%f'"), "\n",
? ? ? ? ? ? ? "serve_time :", datetime.now())
? ? ? ? print("_______________________")
? ??
?
? ? app = TestApp()
?
app.connect("127.0.0.1", 4002, 0)
? ??
? ? # allow time to connect to server
time.sleep(1)
? ??
contract = Contract()
contract.symbol = "SPY"
contract.secType = "STK"
contract.exchange = "SMART"
contract.currency = "USD"
contract.primaryExchange = "ARCA"
?
#app.reqMarketDataType(4)? # switch to delayed-frozen data if live is not available
app.reqTickByTickData(1 ,contract, "Last", 0, True)
#app.reqMktData(1, contract, "", False, False, [])
?
api_thread = threading.Thread(target=app.run)
api_thread.start()
Here is output:
Error:? -1? ?2104? ?Market data farm connection is OK:usfarm
Error:? -1? ?2106? ?HMDS data farm connection is OK:ushmds
Error:? -1? ?2158? ?Sec-def data farm connection is OK:secdefil
Tick Price. Ticker Id: 1 tickType: BID Price: 414.37?
?market_time_unix: 1659545243?
?market_time: 2022-08-03 16:47:23.000000'?
?server_time : 2022-08-03 16:47:23.880814
_______________________
Tick Price. Ticker Id: 1 tickType: BID Price: 414.37?
?market_time_unix: 1659545243?
?market_time: 2022-08-03 16:47:23.000000'?
?server_time : 2022-08-03 16:47:23.881391
_______________________
Tick Price. Ticker Id: 1 tickType: BID Price: 414.37?
?market_time_unix: 1659545243?
?market_time: 2022-08-03 16:47:23.000000'?
?server_time : 2022-08-03 16:47:23.881610
Is there way to get milliseconds/microseconds?