• C#
  • Java
  • VB
  • C++
  • Python
Contact us
Historical Time and Sales Data

High Resolution Historical Data

The highest granularity of historical data from IB's database can be retrieved using the API function IBApi::EClient::reqHistoricalTicks. This corresponds to the TWS Historical Time & Sales Window. TWS build 968+ and API version 973.04+ is required.

  • Historical Tick-By-Tick data is not available for combos
  • Data will not be returned from multiple trading sessions in a single request; Multiple requests must be used
  • To complete a full second, more ticks may be returned than requested
  • Note: the historical Time&Sales feature in Python API is available starting in API v973.06+.
  • client.reqHistoricalTicks(18001, ContractSamples.USStockAtSmart(), "20170712 21:39:33", null, 10, "TRADES", 1, true, null);
    client.reqHistoricalTicks(18002, ContractSamples.USStockAtSmart(), "20170712 21:39:33", null, 10, "BID_ASK", 1, true, null);
    client.reqHistoricalTicks(18003, ContractSamples.USStockAtSmart(), "20170712 21:39:33", null, 10, "MIDPOINT", 1, true, null);
  • client.reqHistoricalTicks(18001, ContractSamples.USStockAtSmart(), "20170712 21:39:33", null, 10, "TRADES", 1, true, null);
    client.reqHistoricalTicks(18002, ContractSamples.USStockAtSmart(), "20170712 21:39:33", null, 10, "BID_ASK", 1, true, null);
    client.reqHistoricalTicks(18003, ContractSamples.USStockAtSmart(), "20170712 21:39:33", null, 10, "MIDPOINT", 1, true, null);
  • client.reqHistoricalTicks(18001, ContractSamples.USStockAtSmart(), "20170712 21:39:33", Nothing, 10, "TRADES", 1, True, Nothing)
    client.reqHistoricalTicks(18002, ContractSamples.USStockAtSmart(), "20170712 21:39:33", Nothing, 10, "BID_ASK", 1, True, Nothing)
    client.reqHistoricalTicks(18003, ContractSamples.USStockAtSmart(), "20170712 21:39:33", Nothing, 10, "MIDPOINT", 1, True, Nothing)
  • m_pClient->reqHistoricalTicks(19001, ContractSamples::IBMUSStockAtSmart(), "20170621 09:38:33", "", 10, "BID_ASK", 1, true, TagValueListSPtr());
    m_pClient->reqHistoricalTicks(19002, ContractSamples::IBMUSStockAtSmart(), "20170621 09:38:33", "", 10, "MIDPOINT", 1, true, TagValueListSPtr());
    m_pClient->reqHistoricalTicks(19003, ContractSamples::IBMUSStockAtSmart(), "20170621 09:38:33", "", 10, "TRADES", 1, true, TagValueListSPtr());
  • 1  self.reqHistoricalTicks(18001, ContractSamples.USStockAtSmart(),
    2  "20170712 21:39:33", "", 10, "TRADES", 1, True, [])
    3  self.reqHistoricalTicks(18002, ContractSamples.USStockAtSmart(),
    4  "20170712 21:39:33", "", 10, "BID_ASK", 1, True, [])
    5  self.reqHistoricalTicks(18003, ContractSamples.USStockAtSmart(),
    6  "20170712 21:39:33", "", 10, "MIDPOINT", 1, True, [])
  • requestId, id of the request
  • contract, Contract object that is subject of query
  • startDateTime, i.e. "20170701 12:01:00". Uses TWS timezone specified at login.
  • endDateTime, i.e. "20170701 13:01:00". In TWS timezone. Exactly one of start time and end time has to be defined.
  • numberOfTicks, Number of distinct data points. Max currently 1000 per request.
  • whatToShow, (Bid_Ask, Midpoint, Trades) Type of data requested.
  • useRth, Data from regular trading hours (1), or all available hours (0)
  • ignoreSize, Specifies to not return changes only in the size of orders source price is Bid_Ask
  • miscOptions should be defined as null, reserved for internal use

Data is returned to the functions IBApi.EWrapper.historicalTicks (for whatToShow=MIDPOINT), IBApi.EWrapper.historicalTicksBidAsk (for whatToShow=BID_ASK), and IBApi.EWrapper.historicalTicksLast for (for whatToShow=TRADES), depending on the type of data requested.

  • public void historicalTicks(int reqId, HistoricalTick[] ticks, bool done)
    {
    foreach (var tick in ticks)
    {
    Console.WriteLine("Historical Tick. Request Id: {0}, Time: {1}, Price: {2}, Size: {3}", reqId, Util.UnixSecondsToString(tick.Time, "yyyyMMdd-HH:mm:ss zzz"), tick.Price, tick.Size);
    }
    }
    ...
    public void historicalTicksBidAsk(int reqId, HistoricalTickBidAsk[] ticks, bool done)
    {
    foreach (var tick in ticks)
    {
    Console.WriteLine("Historical Tick Bid/Ask. Request Id: {0}, Time: {1}, Mask: {2} Price Bid: {3}, Price Ask {4}, Size Bid: {5}, Size Ask {6}",
    reqId, Util.UnixSecondsToString(tick.Time, "yyyyMMdd-HH:mm:ss zzz"), tick.Mask, tick.PriceBid, tick.PriceAsk, tick.SizeBid, tick.SizeAsk);
    }
    }
    ...
    public void historicalTicksLast(int reqId, HistoricalTickLast[] ticks, bool done)
    {
    foreach (var tick in ticks)
    {
    Console.WriteLine("Historical Tick Last. Request Id: {0}, Time: {1}, Mask: {2}, Price: {3}, Size: {4}, Exchange: {5}, Special Conditions: {6}",
    reqId, Util.UnixSecondsToString(tick.Time, "yyyyMMdd-HH:mm:ss zzz"), tick.Mask, tick.Price, tick.Size, tick.Exchange, tick.SpecialConditions);
    }
    }
  • @Override
    public void historicalTicks(int reqId, List<HistoricalTick> ticks, boolean done) {
    for (HistoricalTick tick : ticks) {
    System.out.println(EWrapperMsgGenerator.historicalTick(reqId, tick.time(), tick.price(), tick.size()));
    }
    }
    ...
    @Override
    public void historicalTicksBidAsk(int reqId, List<HistoricalTickBidAsk> ticks, boolean done) {
    for (HistoricalTickBidAsk tick : ticks) {
    System.out.println(EWrapperMsgGenerator.historicalTickBidAsk(reqId, tick.time(), tick.mask(), tick.priceBid(), tick.priceAsk(), tick.sizeBid(),
    tick.sizeAsk()));
    }
    }
    ...
    public void historicalTicksLast(int reqId, List<HistoricalTickLast> ticks, boolean done) {
    for (HistoricalTickLast tick : ticks) {
    System.out.println(EWrapperMsgGenerator.historicalTickLast(reqId, tick.time(), tick.mask(), tick.price(), tick.size(), tick.exchange(),
    tick.specialConditions()));
    }
    }
  • Public Sub historicalTick(reqId As Integer, ticks As HistoricalTick(), done As Boolean) Implements EWrapper.historicalTicks
    For Each tick In ticks
    Console.WriteLine("Historical Tick. Request Id: {0}, Time: {1}, Price: {2}, Size: {3}", reqId, Util.UnixSecondsToString(tick.Time, "yyyyMMdd-HH:mm:ss zzz"), tick.Price, tick.Size)
    Next
    End Sub
    ...
    Public Sub historicalTickBidAsk(reqId As Integer, ticks As HistoricalTickBidAsk(), done As Boolean) Implements EWrapper.historicalTicksBidAsk
    For Each tick In ticks
    Console.WriteLine("Historical Tick Bid/Ask. Request Id: {0}, Time: {1}, Mask: {2} Price Bid: {3}, Price Ask {4}, Size Bid: {5}, Size Ask {6}",
    reqId, Util.UnixSecondsToString(tick.Time, "yyyyMMdd-HH:mm:ss zzz"), tick.Mask, tick.PriceBid, tick.PriceAsk, tick.SizeBid, tick.SizeAsk)
    Next
    End Sub
    ...
    Public Sub historicalTickLast(reqId As Integer, ticks As HistoricalTickLast(), done As Boolean) Implements EWrapper.historicalTicksLast
    For Each tick In ticks
    Console.WriteLine("Historical Tick Last. Request Id: {0}, Time: {1}, Mask: {2}, Price: {3}, Size: {4}, Exchange: {5}, Special Conditions: {6}",
    reqId, Util.UnixSecondsToString(tick.Time, "yyyyMMdd-HH:mm:ss zzz"), tick.Mask, tick.Price, tick.Size, tick.Exchange, tick.SpecialConditions)
    Next
    End Sub
  • void TestCppClient::historicalTicks(int reqId, const std::vector<HistoricalTick>& ticks, bool done) {
    for (HistoricalTick tick : ticks) {
    std::time_t t = tick.time;
    std::cout << "Historical tick. ReqId: " << reqId << ", time: " << ctime(&t) << ", price: "<< tick.price << ", size: " << tick.size << std::endl;
    }
    }
    ...
    void TestCppClient::historicalTicksBidAsk(int reqId, const std::vector<HistoricalTickBidAsk>& ticks, bool done) {
    for (HistoricalTickBidAsk tick : ticks) {
    std::time_t t = tick.time;
    std::cout << "Historical tick bid/ask. ReqId: " << reqId << ", time: " << ctime(&t) << ", mask: " << tick.mask << ", price bid: "<< tick.priceBid <<
    ", price ask: "<< tick.priceAsk << ", size bid: " << tick.sizeBid << ", size ask: " << tick.sizeAsk << std::endl;
    }
    }
    ...
    void TestCppClient::historicalTicksLast(int reqId, const std::vector<HistoricalTickLast>& ticks, bool done) {
    for (HistoricalTickLast tick : ticks) {
    std::time_t t = tick.time;
    std::cout << "Historical tick last. ReqId: " << reqId << ", time: " << ctime(&t) << ", mask: " << tick.mask << ", price: "<< tick.price <<
    ", size: " << tick.size << ", exchange: " << tick.exchange << ", special conditions: " << tick.specialConditions << std::endl;
    }
    }
  • 1  def historicalTicks(self, reqId: int, ticks: ListOfHistoricalTick, done: bool):
    2  for tick in ticks:
    3  print("Historical Tick. Req Id: ", reqId, ", time: ", tick.time,
    4  ", price: ", tick.price, ", size: ", tick.size)
    ...
    1  def historicalTicksBidAsk(self, reqId: int, ticks: ListOfHistoricalTickBidAsk,
    2  done: bool):
    3  for tick in ticks:
    4  print("Historical Tick Bid/Ask. Req Id: ", reqId, ", time: ", tick.time,
    5  ", bid price: ", tick.priceBid, ", ask price: ", tick.priceAsk,
    6  ", bid size: ", tick.sizeBid, ", ask size: ", tick.sizeAsk)
    ...
    1  def historicalTicksLast(self, reqId: int, ticks: ListOfHistoricalTickLast,
    2  done: bool):
    3  for tick in ticks:
    4  print("Historical Tick Last. Req Id: ", reqId, ", time: ", tick.time,
    5  ", price: ", tick.price, ", size: ", tick.size, ", exchange: ", tick.exchange,
    6  ", special conditions:", tick.specialConditions)