Slick Sim 0.10.1
Exchange simulator that shadows real venues for risk-free strategy testing
Loading...
Searching...
No Matches
hyperliquid_exchange.hpp
Go to the documentation of this file.
1#pragma once
2
6#include <utils/price.hpp>
7#include <slick/logger.hpp>
8#include <nlohmann/json.hpp>
9#include <array>
10#include <unordered_map>
11#include <unordered_set>
12#include <vector>
13
14namespace slick::sim::exch {
15
17 : public Exchange
19{
20public:
21 HyperliquidExchange(const nlohmann::json &config);
22 ~HyperliquidExchange() override = default;
23
24 // HyperliquidFeedCallbacks — called from the hyperliquid WS thread
25 void onL2BookUpdate(const nlohmann::json& msg) override;
26 void onTradesUpdate(const nlohmann::json& msg) override;
27 void onHyperliquidDisconnected() override;
28
29protected:
30 void handleMdSubscription(const Request &request) override;
31 void handleMdUnsubscription(const Request &request) override;
32 void poll() override;
33 void drainEventQueue();
34
35private:
36 Symbol* addSymbol(std::string_view coin);
37 void processL2BookEvent(const nlohmann::json& data);
38 void processL2Snapshot(const nlohmann::json& data);
39 void processL2Diff(const nlohmann::json& data);
40 void applyPhantomLevelUpdate(Symbol* symbol, Side side, price_t price,
41 qty_t target_qty, uint64_t event_time_ns);
42 void finalizeL2Event(Symbol* symbol);
43 void processTradesEvent(const nlohmann::json& data);
44
45 // Feed management
46 std::unordered_map<std::string, std::shared_ptr<md_feed::MDFeed>> map_coin_feed_;
47
48 // Per-channel pending subscriptions (symbols waiting for first snapshot)
49 std::array<std::unordered_set<Symbol*>,
50 static_cast<size_t>(HyperliquidChannel::__COUNT__)> pending_md_subscription_;
51
52 // coin's Symbol* -> (bid, ask) ordered price mirror — reflects the
53 // upstream exchange's OWN l2 book ordering (real-market only),
54 // independent of user orders resting in our simulated book. Used solely
55 // to resolve "r" (removed-by-index) diff entries back to a price.
56 std::unordered_map<Symbol*, std::array<std::vector<price_t>, 2>> l2_ref_levels_;
57
58 bool use_live_feed_ = false;
59 std::string live_ws_base_url_{"https://api.hyperliquid.xyz"};
60
61 // Cross-thread event queue: WS callbacks push here, exchange thread drains
62 struct FeedEvent {
63 enum class Type : uint8_t { L2_BOOK, TRADES };
64 Type type;
65 nlohmann::json data;
66 };
67 std::vector<FeedEvent> event_queue_;
68
69 std::vector<MDLevel> level_update_buffer_;
70};
71
72} // end namespace slick::sim::exch
Exchange(Venue venue, const nlohmann::json &config)
Definition exchange.cpp:21
void onTradesUpdate(const nlohmann::json &msg) override
void onL2BookUpdate(const nlohmann::json &msg) override
void handleMdSubscription(const Request &request) override
void poll() override
One iteration of the exchange thread's loop, called continuously between start() and stop().
HyperliquidExchange(const nlohmann::json &config)
void handleMdUnsubscription(const Request &request) override
Helpers for a venue whose one config entry describes several independent exchange instances,...
Definition exchange.hpp:30
int_fast64_t qty_t
Definition types.hpp:22
int_fast64_t price_t
Definition types.hpp:21