-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbroker_get_accounts_example.cpp
More file actions
47 lines (39 loc) · 1.62 KB
/
broker_get_accounts_example.cpp
File metadata and controls
47 lines (39 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <cstdlib>
#include <iostream>
#include "alpaca/AlpacaBrokerAPI.h"
int main() {
const char* key = std::getenv("ALPACA_BROKER_API_KEY");
const char* secret = std::getenv("ALPACA_BROKER_API_SECRET");
const char* cc_id = std::getenv("ALPACA_BROKER_CLIENT_ID");
const char* cc_sec = std::getenv("ALPACA_BROKER_CLIENT_SECRET");
const bool use_cc = cc_id && cc_sec;
if (!use_cc && (!key || !secret)) {
std::cerr << "Set either:\n"
<< " ALPACA_BROKER_API_KEY + ALPACA_BROKER_API_SECRET (legacy)\n"
<< " ALPACA_BROKER_CLIENT_ID + ALPACA_BROKER_CLIENT_SECRET (client credentials)\n";
return 1;
}
try {
std::unique_ptr<alpaca::AlpacaBrokerAPI> broker_ptr;
if (use_cc) {
std::cout << "[auth] Using client-credentials flow\n";
broker_ptr = std::make_unique<alpaca::AlpacaBrokerAPI>(
cc_id, cc_sec,
alpaca::BrokerAPIEndpoint::SANDBOX,
alpaca::BrokerAuthMode::CLIENT_CREDENTIALS);
} else {
std::cout << "[auth] Using legacy key/secret flow\n";
broker_ptr = std::make_unique<alpaca::AlpacaBrokerAPI>(
key, secret, alpaca::BrokerAPIEndpoint::SANDBOX);
}
auto accounts = broker_ptr->get_accounts();
std::cout << "Total accounts: " << accounts.size() << "\n\n";
for (const auto& account : accounts) {
std::cout << account.to_string() << "\n";
}
return 0;
} catch (const std::exception& ex) {
std::cerr << "Error: " << ex.what() << "\n";
return 1;
}
}