Skip to content

Commit 03a827a

Browse files
committed
Begin to rewrite web socket streaming code in C++ to see if we can receive market events with improved latency
1 parent 2c15137 commit 03a827a

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

tradier_stream_connect.cpp

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <curl/curl.h>
4+
#include <thread>
5+
#include <unistd.h>
6+
#include <cstdlib>
7+
8+
#include <nlohmann/json.hpp>
9+
10+
using json = nlohmann::json;
11+
12+
13+
//
14+
// Callback function - handles data returned from curl
15+
//
16+
17+
size_t callback(const char* in, size_t size, size_t num, std::string* out) {
18+
const size_t totalBytes(size*num);
19+
out->append(in, totalBytes);
20+
return totalBytes;
21+
}
22+
23+
24+
//
25+
// Initialize + Processs HTTP Request
26+
//
27+
28+
void tradier_http_stream() {
29+
CURL* curl = curl_easy_init();
30+
if (curl) {
31+
std::string response_string;
32+
std::string header_string;
33+
curl_easy_setopt(curl, CURLOPT_URL, "https://api.tradier.com/v1/markets/events/session");
34+
35+
// Authorization, headers
36+
// std::string auth_header = "Authorization: Bearer " + std::string(getenv("TRADIER_TOKEN_LIVE"));
37+
std::string auth_header = "Authorization: Bearer ABC123"; // replace ABC123 with actual authorization token
38+
// std::string auth_header = "Authorization: Bearer " + std::string(getenv("tradier_token_live"));
39+
struct curl_slist* headers = nullptr;
40+
headers = curl_slist_append(headers, auth_header.c_str());
41+
headers = curl_slist_append(headers, "Accept: application/json");
42+
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
43+
44+
// Setup the POST request
45+
curl_easy_setopt(curl, CURLOPT_POST, 1L);
46+
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 0L);
47+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback);
48+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_string);
49+
50+
// Make the CURL request
51+
CURLcode res = curl_easy_perform(curl);
52+
if (res == CURLE_OK) {
53+
long http_code = 0;
54+
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
55+
if (http_code == 200) {
56+
// Parse JSON
57+
json result = json::parse(response_string);
58+
auto stream_url = result["stream"]["url"].get<std::string>();
59+
auto session_id = result["stream"]["sessionid"].get<std::string>();
60+
61+
std::cout << "Connect to stream : " << stream_url << " with session ID " << session_id << "\n\n";
62+
sleep(240);
63+
} else {
64+
std::cout << "Failed to connect.\nHTTP Status Code: " << http_code << "\n";
65+
}
66+
} else {
67+
std::cout << "CURL Error: " << curl_easy_strerror(res) << "\n";
68+
}
69+
70+
curl_easy_cleanup(curl);
71+
curl_slist_free_all(headers);
72+
}
73+
}
74+
75+
76+
int main() {
77+
const char* env_var = std::getenv("tradier_token_live");
78+
std::cout << "env var: " << env_var << std::endl;
79+
std::cout << "hello, world!" << std::endl;
80+
std::thread t(tradier_http_stream);
81+
t.join();
82+
return 0;
83+
}
84+
85+
86+
87+
88+
//
89+
// OUTPUT
90+
//
91+
92+
// thammons@toms-MacBook-Air t_bot % g++ -std=c++17 -o tradier_stream_connect tradier_stream_connect.cpp -I/opt/homebrew/Cellar/nlohmann-json/3.11.3/include -lcurl -lpthread
93+
// thammons@toms-MacBook-Air t_bot % ./tradier_stream_connect
94+
// hello, world!
95+
// Connect to stream : https://stream.tradier.com/v1/markets/events with session ID a7344a1f-cb26-4528-9c76-03224c3ed368

0 commit comments

Comments
 (0)