service.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #ifndef SERVICE_H
  2. #define SERVICE_H
  3. #include <boost/asio.hpp>
  4. #include <boost/beast.hpp>
  5. #include <thread>
  6. #include <memory>
  7. #include <iostream>
  8. #include <string>
  9. #include <map>
  10. #include <vector>
  11. #include <chrono>
  12. #include <mutex>
  13. namespace beast = boost::beast;
  14. namespace http = beast::http;
  15. namespace net = boost::asio;
  16. using tcp = boost::asio::ip::tcp;
  17. struct PlatformData {
  18. double price;
  19. double deviationCompensation;
  20. bool isOpenLong;
  21. bool isOpenShort;
  22. std::vector<double> pricesHistory; // 用于存储历史价格以计算平均值
  23. };
  24. class MyService {
  25. public:
  26. MyService(unsigned short port);
  27. void start();
  28. private:
  29. void doHttpSession(tcp::socket socket);
  30. void doWebSocketSession(tcp::socket socket);
  31. void onHttpRequest(http::request<http::string_body> req);
  32. void onWebSocketMessage(const std::string& message);
  33. void updatePrice(const std::string &platform, double price);
  34. void adjustDecimalPoints();
  35. void checkDeviationCompensation();
  36. void calculateAveragePrices();
  37. void calculateDeviationPercentage();
  38. void determinePosition();
  39. unsigned short port_;
  40. std::map<std::string, PlatformData> platforms_; // 存储每个平台的数据
  41. std::chrono::time_point<std::chrono::steady_clock> lastCompensationTime_;
  42. std::mutex dataMutex_;
  43. const int compensationIntervalMinutes = 120; // 默认2小时
  44. const int averagePeriodMinutes = 15; // 默认15分钟
  45. const double entryThresholdPercentage = 0.01; // 示例开仓条件百分比
  46. };
  47. #endif // SERVICE_H