Basic Function

About 3 min

The Tiger Open API SDK provides a rich set of interfaces to call Tiger's services. This section will demonstrate the core functions of the Tiger API, including querying quotes, subscribing to quotes, and calling the API for trading.

Get Quotes

The following is the simplest example of calling the Tiger API, which demonstrates how to call the Open API to actively query stock quotes. The next examples demonstrate how to call Open API to trade and subscribe to quotes respectively.

In addition to the basic functionality described above, the Open API supports querying and trading different underlying instruments from multiple markets, as well as other complex requests. For other interfaces and requests supported by the Open API, please read the body of the documentation after the Quick Start for a list and how to use them, and refer to the Quick Start and the examples in the documentation for calls.

#include "tigerapi/tiger_client.h"
#include "tigerapi/quote_client.h"
#include "tigerapi/trade_client.h"
#include "tigerapi/contract_util.h"
#include "tigerapi/order_util.h"

using namespace std;
using namespace web;
using namespace web::json;
using namespace TIGER_API;


/**
 * Calling the quotation interface
 */

class TestQuoteClient {
public:
    static void test_grab_quote_permission(std::shared_ptr<QuoteClient> quote_client) {
        value perms = quote_client->grab_quote_permission();
        cout << "Quote perms: " << perms << endl;
    }

    static void test_get_symbols(std::shared_ptr<QuoteClient> quote_client) {
        value result = quote_client->get_symbols();
        cout << "result: " << result << endl;
    }

    static void test_get_symbols_names(std::shared_ptr<QuoteClient> quote_client) {
//        value result = quote_client->get_all_symbol_names("HK");
        value result = quote_client->get_all_symbol_names(Market::HK);
        cout << "result: " << result << endl;
    }

    static void test_get_kline(std::shared_ptr<QuoteClient> quote_client) {
        value symbols = value::array();
        symbols[0] = value::string("AAPL");
        symbols[1] = value::string("JD");
        cout << "symbols " << symbols << endl;
        value result = quote_client->get_kline(symbols);
        cout << "result: " << result << endl;
    }

    static void test_get_quote_stock_trade(std::shared_ptr<QuoteClient> quote_client) {
        value symbols = value::array();
        symbols[0] = value::string("AAPL");
        symbols[1] = value::string("JD");
        value result = quote_client->get_quote_stock_trade(symbols);
        cout << "Result: " << result << endl;
    }

    static void test_quote(std::shared_ptr<QuoteClient> quote_client) {
        TestQuoteClient::test_get_symbols_names(quote_client);
    }
};


/**
 * Directly use TigerApi
 */
class TestTigerApi {
public:
    static void test_grab_quote_permission(std::shared_ptr<TigerClient> tigerapi) {
        value obj = value::object(true);
        value perms = tigerapi->post(GRAB_QUOTE_PERMISSION, obj);
        cout << "quote perms: " << perms << endl;
    }

    static void test_get_market_status(std::shared_ptr<TigerClient>  tigerapi) {
        value obj = value::object(true);
        obj[U("market")] = value::string("US");
        tigerapi->post(MARKET_STATE, obj);
    }
};

int main(int argc, char *args[]) {
    cout << "Tiger Api main" << endl;
    /************************** set config **********************/
    ClientConfig config = ClientConfig(true);

    config.private_key = "-----BEGIN RSA PRIVATE KEY-----\n"
                         "xxxxxx your private key content xxxxxxxx"
                         "-----END RSA PRIVATE KEY-----";
    config.tiger_id = "tiger id";
    config.account = "account id";


    /**
     * Use QuoteClient
     */
    std::shared_ptr<QuoteClient> quote_client = std::make_shared<QuoteClient>(config);
    quote_client->grab_quote_permission();
    TestQuoteClient::test_quote(quote_client);

    /**
     * Use TigerApi
     */
    std::shared_ptr<TigerClient> tigerapi = std::make_shared<TigerClient>(config);
    TestTigerApi::test_grab_quote_permission(tigerapi);



    return 0;
}

Subscribe Quotes

In addition to the active query option, Open API also supports the Subscribe - Receive Push method to receive quotes and other information, see the following example. This example implements the process of subscribing to Apple and AMD stock quotes, outputting a snapshot of the quotes to the console, unsubscribing after 30 seconds, and disconnecting from the server.

It is important to note that the requests related to subscription push are processed asynchronously, so user-defined callback functions are required to bind with the intermediate functions. When an event occurs or the latest information is pushed by the server, the program will automatically call the user-defined callback function and pass in the data returned by the return interface.


Trade Orders

Trading is another major feature of the Open API. This example shows how to use Open API to place a market order on the US stock Tiger Securities TIGR.

#include "tigerapi/tiger_client.h"
#include "tigerapi/quote_client.h"
#include "tigerapi/trade_client.h"
#include "tigerapi/contract_util.h"
#include "tigerapi/order_util.h"

using namespace std;
using namespace web;
using namespace web::json;
using namespace TIGER_API;

/**
 * Calling trade interface
 */
class TestTradeClient {
public:
    static void test_get_prime_asset(const std::shared_ptr<TradeClient>& trade_client) {
        value res = trade_client->get_prime_asset();
        cout << "asset: " << res << endl;
    }

    static void test_get_prime_portfolio(const std::shared_ptr<TradeClient>& trade_client) {
        PortfolioAccount res = trade_client->get_prime_portfolio();
        cout << "portfolio: " << res.to_string() << endl;
    }

    static void test_get_asset(const std::shared_ptr<TradeClient>& trade_client) {
        value res = trade_client->get_asset();
        cout << "asset: " << res << endl;
    }

    static void test_get_position(const std::shared_ptr<TradeClient>& trade_client) {
        value res = trade_client->get_positions();
        cout << "position: " << res << endl;
    }

    static void test_get_position_list(const std::shared_ptr<TradeClient>& trade_client) {
        vector<Position> res = trade_client->get_position_list();
        cout << "position size: " << res.size()  << " , first item: " << res[0].to_string() << endl;
    }

    static void test_get_orders(const std::shared_ptr<TradeClient>& trade_client) {
        value res = trade_client->get_orders();
        cout << "orders: " << res << endl;
    }

    static void test_get_active_orders(const std::shared_ptr<TradeClient>& trade_client) {
        value res = trade_client->get_active_orders();
        cout << "active orders: " << res << endl;
    }

    static void test_get_contract(const std::shared_ptr<TradeClient>& trade_client) {
        value res = trade_client->get_contract("AAPL");
        cout << "contract: " << res << endl;
    }

    static void test_place_order(const std::shared_ptr<TradeClient>& trade_client) {
        Contract contract = stock_contract("AAPL", "USD");
        Order order = limit_order(contract, "BUY", 1, 100.0);
        value res = trade_client->place_order(order);
        long id = res["id"].as_integer();
        cout << "order id: " << id << endl;
        cout << "place order result: " << res << endl;
    }

    static void test_get_order(const std::shared_ptr<TradeClient>& trade_client) {
//        Contract contract = stock_contract("AAPL", "USD");
//        Order order = limit_order(contract, "BUY", 1, 100.0);
//        trade_client->place_order(order);
        Order my_order = trade_client->get_order(29270263515317248);
        cout << "order : " << my_order.to_string() << endl;
    }

    static void test_cancel_order(const std::shared_ptr<TradeClient>& trade_client) {
        value res = trade_client->cancel_order(29270263515317248);
        cout << "cancel order : " << res << endl;
    }

    static void test_modify_order(const std::shared_ptr<TradeClient>& trade_client) {
        Contract contract = stock_contract("AAPL", "USD");
        Order order = limit_order(contract, "BUY", 1, 100.0);
        long id = (long) trade_client->place_order(order)["id"].as_number().to_uint64();
        value res = trade_client->modify_order(order, 105);
        cout << "modify order res: " << res << endl;
        Order mod_order = trade_client->get_order(id);
        cout << "modified order: " << mod_order.to_string() << endl;
    }


    static void test_trade(const std::shared_ptr<TradeClient>& trade_client) {
        TestTradeClient::test_get_orders(trade_client);
    }
};

/**
 * Use TigerApi
 */
class TestTigerApi {
public:

    static void test_get_positions(std::shared_ptr<TigerClient>  tigerapi) {
        value obj = value::object(true);
        obj[U("market")] = value::string("US");
        obj[P_ACCOUNT] = value::string("402901");
        tigerapi->post(POSITIONS, obj);
    }
};

int main(int argc, char *args[]) {
    cout << "Tiger Api main" << endl;
    /************************** set config **********************/
    ClientConfig config = ClientConfig(true);

    config.private_key = "-----BEGIN RSA PRIVATE KEY-----\n"
                         "xxxxxx your private key content xxxxxxxx"
                         "-----END RSA PRIVATE KEY-----";
    config.tiger_id = "tiger id";
    config.account = "account id";


    /**
     * Use TradeClient
     */
    std::shared_ptr<TradeClient> trade_client = std::make_shared<TradeClient>(config);
    TestTradeClient::test_trade(trade_client);

    /**
     * Use TigerApi
     */
    std::shared_ptr<TigerClient> tigerapi = std::make_shared<TigerClient>(config);
    TestTigerApi::test_get_positions(tigerapi);



    return 0;
}

Last update: