Basic Examples
The Tiger Open API C++ SDK provides a rich set of interfaces for calling Tiger's services. This section demonstrates the core functionality of the Tiger API, including querying market data, subscribing to market data, and placing trades via the API.
Query Market Data
The following is the simplest example of calling the Tiger API, demonstrating how to query stock market data using the Open API.
#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("/path/to/your/properties/"));
// 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;
}Return Example
{
"symbol": "00700",
"askPrice": 326.4,
"askSize": 15300,
"bidPrice": 326.2,
"bidSize": 26100,
"preClose": 321.80,
"latestPrice": 326.4,
"volume": 2593802,
"open": 325.00,
"high": 326.80,
"low": 323.20,
"status": "NORMAL"
}Subscribe to Market Data
In addition to active querying, the Open API also supports a subscribe-push model to receive market data and other information. This example subscribes to Apple and AMD stock quotes, prints the quote snapshots to the console, unsubscribes after 30 seconds, and disconnects from the server.
#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("/path/to/your/properties/"));
// 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();
// Subscribe to quotes
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
Trading is another major function of the Open API. This example demonstrates how to place a limit order for Tiger Brokers stock (TIGR) on the US market:
#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("/path/to/your/properties/"));
// 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;
}Return Example
{
"id": 14275856193552384,
"orderId": 0,
"account": "164644",
"symbol": "TIGR",
"secType": "STK",
"action": "BUY",
"orderType": "LMT",
"totalQuantity": 100,
"limitPrice": 5.0,
"status": "NEW"
}Additional Notes
For other supported order types, please refer to the Trade section - Place Order documentation.
Updated 1 day ago
