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 US/Eastern", null, 10, "TRADES", 1, true, null);
client.reqHistoricalTicks(18002, ContractSamples.USStockAtSmart(), "20170712 21:39:33 US/Eastern", null, 10, "BID_ASK", 1, true, null);
client.reqHistoricalTicks(18003, ContractSamples.USStockAtSmart(), "20170712 21:39:33 US/Eastern", null, 10, "MIDPOINT", 1, true, null);
-
client.reqHistoricalTicks(18001, ContractSamples.USStockAtSmart(), "20220808 10:00:00 US/Eastern", null, 10, "TRADES", 1, true, null);
client.reqHistoricalTicks(18002, ContractSamples.USStockAtSmart(), "20220808 10:00:00 US/Eastern", null, 10, "BID_ASK", 1, true, null);
client.reqHistoricalTicks(18003, ContractSamples.USStockAtSmart(), "20220808 10:00:00 US/Eastern", null, 10, "MIDPOINT", 1, true, null);
-
client.reqHistoricalTicks(18001, ContractSamples.USStockAtSmart(), "20170712 21:39:33 US/Eastern", Nothing, 10, "TRADES", 1, True, Nothing)
client.reqHistoricalTicks(18002, ContractSamples.USStockAtSmart(), "20170712 21:39:33 US/Eastern", Nothing, 10, "BID_ASK", 1, True, Nothing)
client.reqHistoricalTicks(18003, ContractSamples.USStockAtSmart(), "20170712 21:39:33 US/Eastern", Nothing, 10, "MIDPOINT", 1, True, Nothing)
-
m_pClient->reqHistoricalTicks(19001, ContractSamples::IBMUSStockAtSmart(), "20170621 09:38:33 US/Eastern", "", 10, "BID_ASK", 1, true, TagValueListSPtr());
m_pClient->reqHistoricalTicks(19002, ContractSamples::IBMUSStockAtSmart(), "20170621 09:38:33 US/Eastern", "", 10, "MIDPOINT", 1, true, TagValueListSPtr());
m_pClient->reqHistoricalTicks(19003, ContractSamples::IBMUSStockAtSmart(), "20170621 09:38:33 US/Eastern", "", 10, "TRADES", 1, true, TagValueListSPtr());
-
1 self.reqHistoricalTicks(18001, ContractSamples.USStockAtSmart(),
2 "20170712 21:39:33 US/Eastern",
"", 10,
"TRADES", 1,
True, [])
3 self.reqHistoricalTicks(18002, ContractSamples.USStockAtSmart(),
4 "20170712 21:39:33 US/Eastern",
"", 10,
"BID_ASK", 1,
True, [])
5 self.reqHistoricalTicks(18003, ContractSamples.USStockAtSmart(),
6 "20170712 21:39:33 US/Eastern",
"", 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 startDateTime or endDateTime must be defined.
- numberOfTicks, Number of distinct data points. Max is 1000 per request.
- whatToShow, (Bid_Ask, Midpoint, or Trades) Type of data requested.
- useRth, Data from regular trading hours (1), or all available hours (0).
- ignoreSize, Omit updates that reflect only changes in size, and not price. Applicable to Bid_Ask data requests.
- 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"), Util.DoubleMaxString(tick.Price), Util.DecimalMaxString(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}, Price Bid: {2}, Price Ask: {3}, Size Bid: {4}, Size Ask: {5}, Bid/Ask Tick Attribs: {6} ",
reqId, Util.UnixSecondsToString(tick.Time, "yyyyMMdd-HH:mm:ss"), Util.DoubleMaxString(tick.PriceBid), Util.DoubleMaxString(tick.PriceAsk),
Util.DecimalMaxString(tick.SizeBid), Util.DecimalMaxString(tick.SizeAsk), tick.TickAttribBidAsk);
}
}
... public void historicalTicksLast(int reqId, HistoricalTickLast[] ticks, bool done)
{
foreach (var tick in ticks)
{
Console.WriteLine("Historical Tick Last. Request Id: {0}, Time: {1}, Price: {2}, Size: {3}, Exchange: {4}, Special Conditions: {5}, Last Tick Attribs: {6} ",
reqId, Util.UnixSecondsToString(tick.Time, "yyyyMMdd-HH:mm:ss"), Util.DoubleMaxString(tick.Price), Util.DecimalMaxString(tick.Size), tick.Exchange, tick.SpecialConditions, tick.TickAttribLast);
}
}
-
@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.tickAttribBidAsk(), 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.tickAttribLast(), 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"),
Util.DoubleMaxString(tick.Price), Util.DecimalMaxString(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}, Price Bid: {2}, Price Ask: {3}, Size Bid: {4}, Size Ask: {5}, Bid/Ask Tick Attribs: {6}",
reqId, Util.UnixSecondsToString(tick.Time, "yyyyMMdd-HH:mm:ss"), Util.DoubleMaxString(tick.PriceBid), Util.DoubleMaxString(tick.PriceAsk),
Util.DecimalMaxString(tick.SizeBid), Util.DecimalMaxString(tick.SizeAsk), tick.TickAttribBidAsk.ToString())
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}, Price: {2}, Size: {3}, Exchange: {4}, Special Conditions: {5}, Last Tick Attribs: {6}",
reqId, Util.UnixSecondsToString(tick.Time, "yyyyMMdd-HH:mm:ss"), Util.DoubleMaxString(tick.Price), Util.DecimalMaxString(tick.Size), tick.Exchange,
tick.SpecialConditions, tick.TickAttribLast.ToString())
Next
End Sub
-
void TestCppClient::historicalTicks(int reqId, const std::vector<HistoricalTick>& ticks, bool done) {
for (const HistoricalTick& tick : ticks) {
std::time_t t = tick.time;
char timeStr[80];
ctime_s(timeStr, sizeof(timeStr), &t);
std::cout << "Historical tick. ReqId: " << reqId << ", time: " << timeStr << ", price: "<< Utils::doubleMaxString(tick.price).c_str() << ", size: " << DecimalFunctions::decimalStringToDisplay(tick.size).c_str() << std::endl;
}
}
... void TestCppClient::historicalTicksBidAsk(int reqId, const std::vector<HistoricalTickBidAsk>& ticks, bool done) {
for (const HistoricalTickBidAsk& tick : ticks) {
std::time_t t = tick.time;
char timeStr[80];
ctime_s(timeStr, sizeof(timeStr), &t);
std::cout << "Historical tick bid/ask. ReqId: " << reqId << ", time: " << timeStr << ", price bid: "<< Utils::doubleMaxString(tick.priceBid).c_str() <<
", price ask: "<< Utils::doubleMaxString(tick.priceAsk).c_str() << ", size bid: " << DecimalFunctions::decimalStringToDisplay(tick.sizeBid).c_str() << ", size ask: " << DecimalFunctions::decimalStringToDisplay(tick.sizeAsk).c_str() <<
", bidPastLow: " << tick.tickAttribBidAsk.bidPastLow << ", askPastHigh: " << tick.tickAttribBidAsk.askPastHigh << std::endl;
}
}
... void TestCppClient::historicalTicksLast(int reqId, const std::vector<HistoricalTickLast>& ticks, bool done) {
for (HistoricalTickLast tick : ticks) {
std::time_t t = tick.time;
char timeStr[80];
ctime_s(timeStr, sizeof(timeStr), &t);
std::cout << "Historical tick last. ReqId: " << reqId << ", time: " << timeStr << ", price: "<< Utils::doubleMaxString(tick.price).c_str() <<
", size: " << DecimalFunctions::decimalStringToDisplay(tick.size).c_str() << ", exchange: " << tick.exchange << ", special conditions: " << tick.specialConditions <<
", unreported: " << tick.tickAttribLast.unreported << ", pastLimit: " << tick.tickAttribLast.pastLimit << std::endl;
}
}
-
1 def historicalTicks(self, reqId: int, ticks: ListOfHistoricalTick, done: bool):
3 print(
"HistoricalTick. ReqId:", reqId, tick)
... 1 def historicalTicksBidAsk(self, reqId: int, ticks: ListOfHistoricalTickBidAsk,
4 print(
"HistoricalTickBidAsk. ReqId:", reqId, tick)
... 1 def historicalTicksLast(self, reqId: int, ticks: ListOfHistoricalTickLast,
4 print(
"HistoricalTickLast. ReqId:", reqId, tick)
Historical Halted and Unhalted ticks
Effective with TWS 10.15+ and TWS API 10.15+ a new Tick-By-Tick attribute has been introduced. The tick attribute pastLimit is also returned with streaming Tick-By-Tick responses. Check Halted and Unhalted ticks section.
- If tick has zero price, zero size and pastLimit flag is set - this is "Halted" tick.
- If tick has zero price, zero size and followed immediately after "Halted" tick - this is "Unhalted" tick.