基本功能示例

大约 4 分钟

老虎Open API SDK提供了丰富的接口来调用老虎的服务,本章节将对老虎API的核心功能进行一一演示:包括查询行情,订阅行情,以及调用API进行交易

查询行情

以下为一个最简单的调用老虎API的示例,演示了如何调用Open API来主动查询股票行情。接下来的例子分别演示了如何调用Open API来进行交易与订阅行情。

除上述基础功能外,Open API还支持查询、交易多个市场的不同标的,以及其他复杂请求。对于其他Open API支持的接口和请求,请在快速入门后阅读文档正文获取列表及使用方法,并参考快速入门以及文档中的例子进行调用

#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;


/**
 * 调用行情接口
 */

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);
    }
};


/**
 * 直接使用 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();

    config.private_key = "xxxxxx your private key string xxxxxxxx";
    config.tiger_id = "your tiger id";
    config.account = "your account";



    /**
     * 使用封装后的行情接口 QuoteClient
     */
    std::shared_ptr<QuoteClient> quote_client = std::make_shared<QuoteClient>(config);
    quote_client->grab_quote_permission();
    TestQuoteClient::test_quote(quote_client);

    /**
     * 直接使用未封装的 TigerApi
     */
    std::shared_ptr<TigerClient> tigerapi = std::make_shared<TigerClient>(config);
    TestTigerApi::test_grab_quote_permission(tigerapi);



    return 0;
}

订阅行情

除了选择主动查询的方式,Open API还支持订阅-接受推送的方式来接收行情等信息,具体请见下例。此示例实现了订阅苹果与AMD股票行情,将行情快照输出在console,持续30秒后取消订阅,并且断开与服务器的连接的过程。

需要注意的是,订阅推送相关的请求均为异步处理,故需要用户自定义回调函数,与中间函数进行绑定。某个事件发生,或有最新信息更新被服务器推送时,程序会自动调用用户自定义的回调函数并传入返回接口返回的数据,由用户自定义的回调函数来处理数据。

交易下单

交易是Open API的另一个主要功能。此例展示了如何使用Open API对美股老虎证券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;

/**
 * 调用交易接口
 */
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);
    }
};

/**
 * 直接使用 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();

    config.private_key = "xxxxxx your private key string xxxxxxxx";
    config.tiger_id = "your tiger id";
    config.account = "your account";



    /**
     * 使用封装后的交易接口 TradeClient
     */
    std::shared_ptr<TradeClient> trade_client = std::make_shared<TradeClient>(config);
    TestTradeClient::test_trade(trade_client);

    /**
     * 直接使用未封装的 TigerApi
     */
    std::shared_ptr<TigerClient> tigerapi = std::make_shared<TigerClient>(config);
    TestTigerApi::test_get_positions(tigerapi);



    return 0;
}

上次编辑于: