Streaming L2 Market Depth
Ten seconds of the market depth of AAPL, five rows a side.
What this shows
reqMktDepthwith the smart depth, andcancelMktDepth.updateMktDepthL2: the row, the operation (insert, update, delete), the side, the price and the size.
Run it
cmake -S cpp -B build/cpp -DCMAKE_BUILD_TYPE=Release
cmake --build build/cpp --target hello_l2
IB_USERNAME=... IB_PASSWORD=... build/cpp/examples/hello_l2
Source
// Ten seconds of the market depth of AAPL, five rows a side.
//
// IB_USERNAME=... IB_PASSWORD=... hello_l2
#include "session.h"
struct App : Session {
int updates = 0;
void updateMktDepthL2(int, int position, const std::string& marketMaker, int operation, int side, double price, Decimal size, bool) override
{
++updates;
std::printf("%s row=%d op=%d %-8s %.2f x %s\n", side ? "bid" : "ask", position, operation, marketMaker.c_str(), price,
DecimalFunctions::decimalStringToDisplay(size).c_str());
}
void updateMktDepth(int, int position, int operation, int side, double price, Decimal size) override
{
++updates;
std::printf("%s row=%d op=%d %.2f x %s\n", side ? "bid" : "ask", position, operation, price,
DecimalFunctions::decimalStringToDisplay(size).c_str());
}
};
int main()
{
App app;
if (!app.connect())
return 1;
app.client.reqMktDepth(1, stock("AAPL", 265598), 5, true, TagValueListSPtr());
app.pump(10);
app.client.cancelMktDepth(1, true);
std::printf("updates: %d\n", app.updates);
app.client.eDisconnect();
return 0;
}
The session every example uses:
// What the examples share: a session with its client, its signal and its
// reader, and a loop that gives the callbacks on the calling thread.
#pragma once
#ifndef IBX_EXAMPLES_SESSION_H
#define IBX_EXAMPLES_SESSION_H
#include <chrono>
#include <cstdio>
#include <functional>
#include <memory>
#include <string>
#include "DefaultEWrapper.h"
#include "EClientSocket.h"
#include "EReader.h"
#include "EReaderOSSignal.h"
class Session : public DefaultEWrapper {
public:
Session() : signal(100), client(this, &signal) {}
// Host and port name a local gateway for the official client; here the
// login goes to the servers with the credentials of the environment
// (IB_USERNAME, IB_PASSWORD), on the paper system.
bool connect(int clientId = 0)
{
if (!client.eConnect("127.0.0.1", 4002, clientId))
return false;
reader.reset(new EReader(&client, &signal));
reader->start();
// The session starts with the accounts and the next valid order id.
return pump(30, [this] { return nextId >= 0; });
}
// Give the callbacks until `done` says so or the time has passed.
bool pump(double seconds, std::function<bool()> done = [] { return false; })
{
auto end = std::chrono::steady_clock::now() + std::chrono::milliseconds(static_cast<long long>(seconds * 1000));
while (client.isConnected() && std::chrono::steady_clock::now() < end) {
signal.waitForSignal();
reader->processMsgs();
if (done())
return true;
}
return done();
}
// Orders go to a paper account only.
bool paper() const
{
if (account.compare(0, 2, "DU") == 0)
return true;
std::printf("not a paper account: no order is sent\n");
return false;
}
void nextValidId(int orderId) override { nextId = orderId; }
void managedAccounts(const std::string& accountsList) override { account = accountsList.substr(0, accountsList.find(',')); }
void error(int id, long long, int errorCode, const std::string& errorString, const std::string&) override
{
// The notices of the data connections are not errors.
if (errorCode < 2100 || errorCode > 2199)
std::printf("[error] id=%d code=%d %s\n", id, errorCode, errorString.c_str());
}
EReaderOSSignal signal;
EClientSocket client;
std::unique_ptr<EReader> reader;
int nextId = -1;
std::string account;
};
inline Contract stock(const char* symbol, int conId = 0)
{
Contract c;
c.conId = conId;
c.symbol = symbol;
c.secType = "STK";
c.exchange = "SMART";
c.currency = "USD";
return c;
}
#endif