天择量化公共接口文档
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

86 lines
3.4 KiB

#include <json/json.h>
#include <curl/curl.h>
// 函数:获取精度信息
std::map<std::string, std::string> getPrecisions(const std::string& platform) {
std::map<std::string, std::string> precisions;
CURL* curl;
CURLcode res;
std::string response;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if (curl) {
// 定义请求 URL
std::string url = "http://43.133.213.20:8001/api/v1/PrecisionInfo/platform";
// 定义请求数据
std::string query = "platform=" + platform;
// 设置 CURL 选项
curl_easy_setopt(curl, CURLOPT_URL, (url + "?" + query).c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 15L); // 设置超时时间为15秒
// 执行请求
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
std::cerr << "getPrecisions failed: " << curl_easy_strerror(res) << std::endl;
} else {
Json::Value jsonData;
Json::CharReaderBuilder readerBuilder;
std::string errs;
std::istringstream responseStream(response);
if (Json::parseFromStream(readerBuilder, responseStream, &jsonData, &errs)) {
for (const auto& key : jsonData.getMemberNames()) {
// std::cout << key << ": " << jsonData[key] << std::endl;
precisions[key] = jsonData[key].toStyledString();
}
} else {
std::cerr << "解析 JSON 失败: " << errs << std::endl;
}
}
// 清理
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return precisions;
}
// 调用示例
std::map<std::string, std::string> precisions = getPrecisions(platform);
// 打印解析后的结果(如果解析了的话)
for (const auto& kv : precisions) {
if (kv.first == GateSwapUSymbolLower) {
std::cout << kv.first << ": " << kv.second << std::endl;
Json::Value jsonData;
Json::CharReaderBuilder readerBuilder;
std::string errs;
std::istringstream responseStream(kv.second);
if (Json::parseFromStream(readerBuilder, responseStream, &jsonData, &errs)) {
double min_size = jsonData["min_size"].asDouble();
double min_value = jsonData["min_value"].asDouble();
double price_decimal = jsonData["price_decimal"].asDouble();
int price_precision = jsonData["price_precision"].asInt();
int real_size_precision = jsonData["real_size_precision"].asInt();
GateSwapUsize_decimal = jsonData["size_decimal"].asDouble();
int size_precision = jsonData["size_precision"].asInt();
std::cout << "min_size: " << min_size << std::endl;
std::cout << "min_value: " << min_value << std::endl;
std::cout << "price_decimal: " << price_decimal << std::endl;
std::cout << "price_precision: " << price_precision << std::endl;
std::cout << "real_size_precision: " << real_size_precision << std::endl;
std::cout << "GateSwapUsize_decimal: " << GateSwapUsize_decimal << std::endl;
std::cout << "size_precision: " << size_precision << std::endl;
} else {
std::cerr << "解析 JSON 失败: " << errs << std::endl;
}
}
}