Slick Trader 0.11.0
Multi-broker algorithmic trading engine with plugin strategies and a live web dashboard
Loading...
Searching...
No Matches
hyperliquid_md_connector.hpp
Go to the documentation of this file.
1#pragma once
2
5#include <slick/stream_buffer_multiplexer.hpp>
7#include <nlohmann/json.hpp>
8#include <atomic>
9#include <condition_variable>
10#include <memory>
11#include <mutex>
12#include <string>
13#include <thread>
14#include <vector>
15#include <set>
16#include <unordered_map>
17
18namespace hyperliquid { class Info; }
19
20namespace slick::trader {
21
22using json = nlohmann::json;
23
24using producer_buffer = slick::stream_buffer_multiplexer::producer_buffer;
25
26// The definitions of Hyperliquid's perpetuals, from the metadata Info loads: coin -> asset index
27// (perps below 10000) and asset -> size decimals. In coin order, each naming its key in
28// `coin_to_asset`, which must outlive them. Tick 0: a perp's tick depends on its price.
29std::vector<MdDefinition> hyperliquid_definitions(const std::unordered_map<std::string, int>& coin_to_asset,
30 const std::unordered_map<int, int>& asset_to_sz_decimals);
31
33public:
35 const json& config,
36 slick::stream_buffer_multiplexer &mux,
37 slick::queue<uint8_t>& control_queue,
38 slick::queue<uint8_t>& request_queue
39 );
40 ~HyperliquidMDConnector() override;
41
42 void register_md_connector() override;
43
44 bool connect() override;
45 void disconnect() override;
46 bool is_connected() const override { return connected_.load(std::memory_order_relaxed); }
47
48 bool subscribe(const MDSubscription& request) override;
49 bool unsubscribe(const MDSubscription& request) override;
50
51 void start() override;
52 void stop() override;
53 bool is_running() const override { return running_.load(std::memory_order_relaxed); }
54
55private:
56 void run();
57 void process_md_requests();
58 // Publishes a definition of each perp in the loaded metadata, for ladders to list them.
59 void publish_definitions();
60 void subscribe_all();
61 void on_trades(const nlohmann::json& msg);
62 void on_bbo(const nlohmann::json& msg);
63 void on_l2(const nlohmann::json& msg);
64 void on_l2book(const nlohmann::json& msg);
65
66 std::atomic_bool running_{false};
67 std::atomic_bool connected_{false};
68 std::thread worker_thread_;
69
70 bool enabled_{true};
71 std::string shm_name_;
72
73 std::unique_ptr<hyperliquid::Info> info_;
74
75 std::shared_ptr<producer_buffer> event_buffer_;
76 slick::queue<uint8_t>& control_queue_;
77 slick::queue<uint8_t>& request_queue_;
78 uint64_t request_cursor_;
79
80 std::string api_url_{"https://api.hyperliquid.xyz"};
81
82 std::vector<std::string> trade_instruments_;
83 std::vector<std::string> quote_instruments_;
84 std::vector<std::string> level2_instruments_;
85 int level2_depth_{10};
86
87 // subscription_ids returned by info_->subscribe(), for cleanup on reconnect
88 std::vector<std::pair<std::string, int>> sub_ids_; // channel_json_str → id
89
90 std::unordered_map<std::string, std::string> coin_to_name_;
91
92 std::set<price_t, std::greater<price_t>> bid_prices_;
93 std::set<price_t> ask_prices_;
94};
95
96} // namespace slick::trader
HyperliquidMDConnector(const json &config, slick::stream_buffer_multiplexer &mux, slick::queue< uint8_t > &control_queue, slick::queue< uint8_t > &request_queue)
bool unsubscribe(const MDSubscription &request) override
bool subscribe(const MDSubscription &request) override
std::vector< MdDefinition > hyperliquid_definitions(const std::unordered_map< std::string, int > &coin_to_asset, const std::unordered_map< int, int > &asset_to_sz_decimals)
nlohmann::json json
slick::stream_buffer_multiplexer::producer_buffer producer_buffer