C++ API call example

Author: , Created: 2018-08-31 10:18:43, Updated:

C++ API call example www.fmz.com

Support C++ 11, compatible with various platforms (Windows/Linux/Mac). Cloud compile.

void main() {
    if (!Test("c++")) {
        Panic("Please download the latest version of the docker");
    }
    // json: https://github.com/nlohmann/json
    // All objects returned with Valid to determine whether it is valid
    LogProfitReset();
    LogReset();
    Log(_N(9.12345, 2));
    Log("use _C", _C(exchange.GetTicker), _C(exchange.GetAccount));

    // Test asynchronous concurrency
    auto routineTicker = exchange.Go("GetTicker");
    auto routineDepth = exchange.Go("GetDepth");
    Ticker asyncTicker;
    Depth asyncDepth;
    
    // No timeout, wait until Ticker returns
    if (routineTicker.wait(asyncTicker)) {
        Log("Wait Ticker OK", asyncTicker.Valid, asyncTicker, asyncTicker.Last);
    } else {
        Log("Wait Ticker fail");
    }
    // Wait for 300ms with timeout, if specified as 0, return immediately
    if (routineDepth.wait(asyncDepth, 200)) {
        // In the waiting time Api returns the call, but does not indicate that the depth is valid, asyncDepth.Valid represents whether it is valid
        Log("Wait Depth OK", asyncDepth.Valid, asyncDepth);
    } else {
        Log("Wait Depth timeout");
    }
    
    auto records = _C(exchange.GetRecords);
    Log("The last line of the K line", records[records.size()-1].Time, _D(records[records.size()-1].Time), records[records.size()-1]);
    // Test TA
    auto ema = TA.EMA(records, 20);
    Log("The last few groups of EMA", ema[ema.size()-1], ema[ema.size()-2], ema[ema.size()-3]);
    
    auto macd = talib.MACD(records);
    Log("The last few groups of MACD", macd[0][macd[0].size()-1], macd[1][macd[1].size()-1], macd[2][macd[2].size()-1]);
        
    //SetErrorFilter("timeout");
    Log(GetOS(), GetPid(), GetLastError(), "MD5", MD5("hello"));
    Log("Hash", Hash("md5", "hex", "hello"), Hash("sha512", "base64", "hello"));
    Log("HMAC", HMAC("sha512", "base64", "hello", "pass"));
    Log(Version(), Unix(), UnixNano());
    Log("Start Test Chart");
    Chart c = Chart(R"EOF({"chart":{"type":"line"},"title":{"text":"Simple chart"},"xAxis":{"title":{"text":"Date"}},"yAxis":{"title":{"text":"Number"}},"series":[{"name":"number","data":[]}]})EOF");
    c.reset();
    for (size_t i = 0; i < 10; i++) {
        c.add(0, {(Unix() + i)*1000, rand() % 100});
    }
    Log(exchange.GetName(), exchange.GetLabel(), exchanges.size());
    auto acc = exchange.GetAccount();
    if (acc.Valid) {
        Log(acc);
    }
    
    // Use LogStatus to combine json to draw a table. The built-in json library is very powerful. See https://github.com/nlohmann/json
    json tbl = R"({"type" : "table", "title" : "AAA", "cols" : ["Head1", "Head2"], "rows": []})"_json;
    tbl["rows"].push_back({"111", "222"});
    tbl["rows"].push_back({"col2", "col22"});
    LogStatus("`"+tbl.dump()+"`");
    
    auto ticker = exchange.GetTicker();
    if (ticker.Valid) {
        Log(ticker);
        Log(ticker.Info); // Info structure is directly json object
    }
    
    auto d = exchange.GetDepth();
    if (d.Valid) {
        Log(d.Asks[0], d.Bids[0]);
    }
    // Test futures
    if (exchange.GetName() == "Futures_OKCoin") {
        exchange.SetContractType("this_week");
        exchange.SetMarginLevel(20);
        exchange.SetDirection("closebuy");
            
        auto positions = exchange.GetPosition();
        if (positions.Valid) {
            Log(positions);
        }
    }
    // Test other libraries
    Log("HttpQuery", HttpQuery("http://www.baidu.com/404").size());
    auto obj = json::parse(HttpQuery("http://www.baidu.com/404", "", "", "", true));
    string body = obj["Body"];
    Log("HttpQuery", body.size(), obj["Header"].dump());
    Log(Mail("smtp://smtp.163.com", "test@163.com", "password", "admin@163.com", "title", "test c++ email"));
    // Test Dial
    auto client = Dial("tcp://www.baidu.com:80");
    if (client.Valid) {
        client.write("GET / HTTP/1.1\nHost: www.baidu.com\nConnection: Close\n\n");
        while (true) {
            string buf = client.read();
            if (buf == "") {
                break;
            }
            Log("Dial receive", buf.size());
        }
        client.close();
    }

    _G("OK","xxx");
    Log(_G("OK"));
    _G("OK","yyyyyy");
    Log(_G("OK"));
    _G("OK",NULL);
    Log(_G("OK"));
    
    // Testing commodity futures 
    if (exchange.GetName() == "Futures_CTP") {
        // C++ IO returns a json object
        exchange.IO("mode", 0);
        vector<string> Symbols = {"MA888", "rb888"};
        for (auto &s : Symbols) {
            // C++ SetContractType returns a json object
            Log(s, _C(exchange.SetContractType, s)["InstrumentName"]);
        }
        // Exit after receiving 100 ticks
        for (size_t i = 0; i < 100; i++) {
            Log(exchange.IO("wait_any"));
        }
    }
}

More