Basic Examples
The Tiger OpenAPI C++ SDK provides methods for retrieving market data, subscribing to real-time updates, and trading. The examples below introduce each workflow.
Query Market Data
This example retrieves a stock quote snapshot.
#include "tigerapi/quote_client.h"
#include "tigerapi/client_config.h"
using namespace TIGER_API;
using namespace web::json;
int main() {
// Initialize configuration
ClientConfig config(false, U("your_config_directory_path"));
// Initialize quote client
QuoteClient quote_client(config);
// Query stock quote snapshot
value symbols = value::array();
symbols[0] = value::string(U("00700"));
value result = quote_client.get_brief(symbols);
// Print result
ucout << result.serialize() << std::endl;
return 0;
}Subscribe to Market Data
Tiger OpenAPI can also stream market data. This example subscribes to Apple and AMD quotes, prints each update, waits 30 seconds, and then unsubscribes and disconnects.
#include <iostream>
#include <thread>
#include <chrono>
#include "tigerapi/push_client.h"
#include "tigerapi/client_config.h"
using namespace TIGER_API;
int main() {
// Initialize configuration
ClientConfig config(false, U("your_config_directory_path"));
// Create push client
auto push_client = IPushClient::create_push_client(config);
// Set connected callback
push_client->set_connected_callback([]() {
std::cout << "Connected" << std::endl;
});
// Set disconnected callback
push_client->set_disconnected_callback([]() {
std::cout << "Disconnected" << std::endl;
});
// Set quote changed callback
push_client->set_quote_changed_callback([](const tigeropen::push::pb::QuoteBasicData& data) {
std::cout << "Symbol: " << data.symbol()
<< " Price: " << data.latestprice()
<< " Volume: " << data.volume()
<< std::endl;
});
// Connect
push_client->connect();
// Create a market data subscription
std::vector<std::string> symbols = {"AAPL", "AMD"};
push_client->subscribe_quote(symbols);
// Wait for push data
std::this_thread::sleep_for(std::chrono::seconds(30));
// Unsubscribe
push_client->unsubscribe_quote(symbols);
// Disconnect
push_client->disconnect();
return 0;
}Place a Trade Order
This example places a US limit order for Tiger Brokers stock (TIGR):
#include "tigerapi/trade_client.h"
#include "tigerapi/client_config.h"
#include "tigerapi/contract_util.h"
#include "tigerapi/order_util.h"
using namespace TIGER_API;
int main() {
// Initialize configuration
ClientConfig config(false, U("your_config_directory_path"));
// Initialize trade client
TradeClient trade_client(config);
// Method 1: Build contract object locally
Contract contract = ContractUtil::stock_contract(U("TIGR"), U("USD"));
// Method 2: Query contract information via API
// value contract_json = trade_client.get_contract(U("TIGR"), U("STK"));
// Create limit order
Order order = OrderUtil::limit_order(
config.account, // Trading account
contract, // Contract object
U("BUY"), // Buy direction
100, // Quantity
5.0 // Limit price
);
// Submit order
value result = trade_client.place_order(order);
ucout << result.serialize() << std::endl;
// After successful placement, order.id is populated with the actual order number
std::cout << "Order ID: " << order.id << std::endl;
return 0;
}SDK model shape
The submitted and updated Order model includes id, account, contract, action, order_type, total_quantity, limit_price, and status. Inspect the returned order fields to determine the current state.
Additional Notes
For other supported order types, see Place Order.
Updated about 2 months ago
Did this page help you?
