天择量化公共接口文档
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.

85 lines
3.4 KiB

  1. #include <json/json.h>
  2. #include <curl/curl.h>
  3. // 函数:获取精度信息
  4. std::map<std::string, std::string> getPrecisions(const std::string& platform) {
  5. std::map<std::string, std::string> precisions;
  6. CURL* curl;
  7. CURLcode res;
  8. std::string response;
  9. curl_global_init(CURL_GLOBAL_DEFAULT);
  10. curl = curl_easy_init();
  11. if (curl) {
  12. // 定义请求 URL
  13. std::string url = "http://43.133.213.20:8001/api/v1/PrecisionInfo/platform";
  14. // 定义请求数据
  15. std::string query = "platform=" + platform;
  16. // 设置 CURL 选项
  17. curl_easy_setopt(curl, CURLOPT_URL, (url + "?" + query).c_str());
  18. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
  19. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
  20. curl_easy_setopt(curl, CURLOPT_TIMEOUT, 15L); // 设置超时时间为15秒
  21. // 执行请求
  22. res = curl_easy_perform(curl);
  23. if (res != CURLE_OK) {
  24. std::cerr << "getPrecisions failed: " << curl_easy_strerror(res) << std::endl;
  25. } else {
  26. Json::Value jsonData;
  27. Json::CharReaderBuilder readerBuilder;
  28. std::string errs;
  29. std::istringstream responseStream(response);
  30. if (Json::parseFromStream(readerBuilder, responseStream, &jsonData, &errs)) {
  31. for (const auto& key : jsonData.getMemberNames()) {
  32. // std::cout << key << ": " << jsonData[key] << std::endl;
  33. precisions[key] = jsonData[key].toStyledString();
  34. }
  35. } else {
  36. std::cerr << "解析 JSON 失败: " << errs << std::endl;
  37. }
  38. }
  39. // 清理
  40. curl_easy_cleanup(curl);
  41. }
  42. curl_global_cleanup();
  43. return precisions;
  44. }
  45. // 调用示例
  46. std::map<std::string, std::string> precisions = getPrecisions(platform);
  47. // 打印解析后的结果(如果解析了的话)
  48. for (const auto& kv : precisions) {
  49. if (kv.first == GateSwapUSymbolLower) {
  50. std::cout << kv.first << ": " << kv.second << std::endl;
  51. Json::Value jsonData;
  52. Json::CharReaderBuilder readerBuilder;
  53. std::string errs;
  54. std::istringstream responseStream(kv.second);
  55. if (Json::parseFromStream(readerBuilder, responseStream, &jsonData, &errs)) {
  56. double min_size = jsonData["min_size"].asDouble();
  57. double min_value = jsonData["min_value"].asDouble();
  58. double price_decimal = jsonData["price_decimal"].asDouble();
  59. int price_precision = jsonData["price_precision"].asInt();
  60. int real_size_precision = jsonData["real_size_precision"].asInt();
  61. GateSwapUsize_decimal = jsonData["size_decimal"].asDouble();
  62. int size_precision = jsonData["size_precision"].asInt();
  63. std::cout << "min_size: " << min_size << std::endl;
  64. std::cout << "min_value: " << min_value << std::endl;
  65. std::cout << "price_decimal: " << price_decimal << std::endl;
  66. std::cout << "price_precision: " << price_precision << std::endl;
  67. std::cout << "real_size_precision: " << real_size_precision << std::endl;
  68. std::cout << "GateSwapUsize_decimal: " << GateSwapUsize_decimal << std::endl;
  69. std::cout << "size_precision: " << size_precision << std::endl;
  70. } else {
  71. std::cerr << "解析 JSON 失败: " << errs << std::endl;
  72. }
  73. }
  74. }