service.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include "service.h"
  2. // 构造函数
  3. MyService::MyService(unsigned short port) : port_(port) {
  4. // 初始化各个平台数据
  5. platforms_["BN"] = {0.0, 0.0, false, false, {}};
  6. platforms_["BG"] = {0.0, 0.0, false, false, {}};
  7. platforms_["HOT"] = {0.0, 0.0, false, false, {}};
  8. }
  9. void MyService::start() {
  10. net::io_context io_context;
  11. tcp::acceptor acceptor(io_context, tcp::endpoint(tcp::v4(), port_));
  12. std::cout << "Server running on port " << port_ << std::endl;
  13. while (true) {
  14. tcp::socket socket(io_context);
  15. acceptor.accept(socket);
  16. std::thread(&MyService::doHttpSession, this, std::move(socket)).detach();
  17. // 在这里可以启动 WebSocket 会话
  18. }
  19. }
  20. void MyService::doHttpSession(tcp::socket socket) {
  21. try {
  22. beast::flat_buffer buffer;
  23. http::request<http::string_body> req;
  24. http::read(socket, buffer, req);
  25. onHttpRequest(req);
  26. http::response<http::string_body> res{http::status::ok, req.version()};
  27. res.set(http::field::content_type, "text/plain");
  28. res.body() = "Hello from MyService!";
  29. res.prepare_payload();
  30. http::write(socket, res);
  31. } catch (std::exception& e) {
  32. std::cerr << "HTTP Session error: " << e.what() << std::endl;
  33. }
  34. }
  35. void MyService::onHttpRequest(http::request<http::string_body> req) {
  36. // 解析前端传递的参数,例如价格更新或校正小数点位数
  37. // 假设消息格式为:{"platform": "BN", "price": 100.5}
  38. // 这里进行简单处理,只是演示
  39. std::string platformId = "BN"; // 这应从请求中读取
  40. double newPrice = 100.5; // 这应从请求中读取
  41. updatePrice(platformId, newPrice);
  42. }
  43. void MyService::updatePrice(const std::string &platform, double price) {
  44. std::lock_guard<std::mutex> lock(dataMutex_);
  45. if (platforms_.find(platform) != platforms_.end()) {
  46. auto &data = platforms_[platform];
  47. // 更新价格并记录历史
  48. data.price = price;
  49. data.pricesHistory.push_back(price);
  50. // 调整小数点位数(可根据实际需求调整)
  51. adjustDecimalPoints();
  52. // 检查偏差补偿
  53. checkDeviationCompensation();
  54. // 计算偏离度百分比
  55. calculateDeviationPercentage();
  56. // 判断开仓平仓
  57. determinePosition();
  58. }
  59. }
  60. void MyService::adjustDecimalPoints() {
  61. std::lock_guard<std::mutex> lock(dataMutex_);
  62. // 实现小数点位数的校正逻辑
  63. // 例如,可以根据前端给定的参数来统一各平台的小数点位数
  64. }
  65. void MyService::checkDeviationCompensation() {
  66. auto now = std::chrono::steady_clock::now();
  67. std::chrono::duration<double, std::ratio<60>> elapsed = now - lastCompensationTime_;
  68. if (elapsed.count() > compensationIntervalMinutes) {
  69. calculateAveragePrices();
  70. lastCompensationTime_ = now; // 更新最后偏差补偿时间
  71. }
  72. }
  73. void MyService::calculateAveragePrices() {
  74. std::lock_guard<std::mutex> lock(dataMutex_);
  75. double bnSum = 0.0;
  76. int bnCount = 0;
  77. for (const auto &price : platforms_["BN"].pricesHistory) {
  78. bnSum += price;
  79. bnCount++;
  80. }
  81. double bnAverage = (bnCount > 0) ? (bnSum / bnCount) : 0.0;
  82. // 对于其他平台(BG 和 HOT)进行类似的处理
  83. for (const auto &platformName : {"BG", "HOT"}) {
  84. double avg = bnAverage - platforms_[platformName].price; // 偏差补偿
  85. platforms_[platformName].deviationCompensation = avg;
  86. }
  87. }
  88. void MyService::calculateDeviationPercentage() {
  89. std::lock_guard<std::mutex> lock(dataMutex_);
  90. for (auto &entry : platforms_) {
  91. auto &data = entry.second;
  92. double deviationPercentage = (platforms_["BN"].price - (data.deviationCompensation + data.price)) / platforms_["BN"].price;
  93. std::cout << "Platform: " << entry.first << ", Deviation Percentage: " << deviationPercentage << std::endl;
  94. }
  95. }
  96. void MyService::determinePosition() {
  97. std::lock_guard<std::mutex> lock(dataMutex_);
  98. for (auto &entry : platforms_) {
  99. auto &data = entry.second;
  100. double deviationPercentage = (platforms_["BN"].price - (data.deviationCompensation + data.price)) / platforms_["BN"].price;
  101. // 开多
  102. if (!data.isOpenLong && deviationPercentage > entryThresholdPercentage) {
  103. data.isOpenLong = true;
  104. std::cout << entry.first << " opened long position." << std::endl;
  105. }
  106. // 平仓
  107. else if (data.isOpenLong && deviationPercentage < entryThresholdPercentage) {
  108. data.isOpenLong = false;
  109. std::cout << entry.first << " closed long position." << std::endl;
  110. }
  111. // 开空
  112. if (!data.isOpenShort && deviationPercentage < -entryThresholdPercentage) {
  113. data.isOpenShort = true;
  114. std::cout << entry.first << " opened short position." << std::endl;
  115. }
  116. // 平仓
  117. else if (data.isOpenShort && deviationPercentage > -entryThresholdPercentage) {
  118. data.isOpenShort = false;
  119. std::cout << entry.first << " closed short position." << std::endl;
  120. }
  121. }
  122. }