Real time and historical data functionality is combined through the IBApi.EClient.reqRealTimeBars request. reqRealTimeBars will create an active subscription that will return a single bar in real time every five seconds that has the OHLC values over that period. reqRealTimeBars can only be used with a bar size of 5 seconds.
Important: real time bars subscriptions combine the limitations of both, top and historical market data. Make sure you observe Market Data Lines and Pacing Violations for Small Bars (30 secs or less). For example, no more than 60 *new* requests for real time bars can be made in 10 minutes, and the total number of active active subscriptions of all types cannot exceed the maximum allowed market data lines for the user.
Requesting
-
client.reqRealTimeBars(3001, ContractSamples.EurGbpFx(), 5, "MIDPOINT", true, null);
-
client.reqRealTimeBars(3001, ContractSamples.EurGbpFx(), 5, "MIDPOINT", true, null);
-
client.reqRealTimeBars(3001, ContractSamples.EurGbpFx(), 5, "MIDPOINT", True, Nothing)
-
m_pClient->reqRealTimeBars(3001, ContractSamples::EurGbpFx(), 5, "MIDPOINT", true, TagValueListSPtr());
-
1 self.reqRealTimeBars(3001, ContractSamples.EurGbpFx(), 5,
"MIDPOINT",
True, [])
- To receive volume, VWAP, or trade count data, it is necessary to specify whatToShow as TRADES.
- It may be necessary to remake real time bars subscriptions after the IB server reset or between trading sessions.
- If bust event happens, then bust event error (code=10225) is reported to all API client versions connected to TWS version 978+.
Receiving
Once invoked, historical data bars of five seconds will start being delivered through the IBApi.EWrapper.realtimeBar callback:
-
public class EWrapperImpl : EWrapper
{
... public virtual void realtimeBar(int reqId, long time, double open, double high, double low, double close, decimal volume, decimal WAP, int count)
{
Console.WriteLine("RealTimeBars. " + reqId + " - Time: " + Util.LongMaxString(time) + ", Open: " + Util.DoubleMaxString(open) + ", High: " + Util.DoubleMaxString(high) +
", Low: " + Util.DoubleMaxString(low) + ", Close: " + Util.DoubleMaxString(close) + ", Volume: " + Util.DecimalMaxString(volume) + ", Count: " + Util.IntMaxString(count) +
", WAP: " + Util.DecimalMaxString(WAP));
}
-
public class EWrapperImpl implements EWrapper {
... @Override
public void realtimeBar(int reqId, long time, double open, double high, double low, double close, Decimal volume, Decimal wap, int count) {
System.out.println("RealTimeBar: " + EWrapperMsgGenerator.realtimeBar(reqId, time, open, high, low, close, volume, wap, count));
}
-
Public Class EWrapperImpl
Implements EWrapper
... Public Sub realtimeBar(reqId As Integer, time As Long, open As Double, high As Double, low As Double, close As Double, volume As Decimal, WAP As Decimal, count As Integer) Implements IBApi.EWrapper.realtimeBar
Console.WriteLine("RealTimeBars. " & reqId & " - Time: " & Util.LongMaxString(time) & ", Open: " & Util.DoubleMaxString(open) & ", High: " & Util.DoubleMaxString(high) &
", Low: " & Util.DoubleMaxString(low) & ", Close: " & Util.DoubleMaxString(close) & ", Volume: " & Util.DecimalMaxString(volume) & ", Count: " &
Util.IntMaxString(count) & ", WAP: " & Util.DecimalMaxString(WAP))
End Sub
-
class TestCppClient : public EWrapper
{
... void TestCppClient::realtimeBar(TickerId reqId, long time, double open, double high, double low, double close,
Decimal volume, Decimal wap, int count) {
printf( "RealTimeBars. %ld - Time: %s, Open: %s, High: %s, Low: %s, Close: %s, Volume: %s, Count: %s, WAP: %s\n", reqId, Utils::longMaxString(time).c_str(),
Utils::doubleMaxString(open).c_str(), Utils::doubleMaxString(high).c_str(), Utils::doubleMaxString(low).c_str(), Utils::doubleMaxString(close).c_str(),
DecimalFunctions::decimalStringToDisplay(volume).c_str(), Utils::intMaxString(count).c_str(), DecimalFunctions::decimalStringToDisplay(wap).c_str());
}
-
1 class TestWrapper(wrapper.EWrapper):
... 1 def realtimeBar(self, reqId: TickerId, time:int, open_: float, high: float, low: float, close: float,
2 volume: Decimal, wap: Decimal, count: int):
3 super().realtimeBar(reqId, time, open_, high, low, close, volume, wap, count)
4 print(
"RealTimeBar. TickerId:", reqId, RealTimeBar(time, -1, open_, high, low, close, volume, wap, count))
-Volume for US stocks has a multiplier of 100
Canceling
To cancel an active request simply invoke IBApi.EClient.cancelRealTimeBars
-
client.cancelRealTimeBars(3001);
-
client.cancelRealTimeBars(3001);
-
client.cancelRealTimeBars(3001)
-
m_pClient->cancelRealTimeBars(3001);
-
1 self.cancelRealTimeBars(3001)