Skip to main content

get_accounts/
get_accounts.rs

1//! Get all broker account IDs linked to your access token.
2//!
3//! Usage:
4//!   cargo run --example get_accounts
5
6use std::time::Duration;
7
8use ctrader_rs::{Client, Config};
9
10#[tokio::main]
11async fn main() -> Result<(), Box<dyn std::error::Error>> {
12    dotenvy::dotenv().ok();
13
14    tracing_subscriber::fmt()
15        .with_env_filter("get_accounts=debug,ctrader_rs=debug")
16        .with_line_number(true)
17        .init();
18
19    let client_id = std::env::var("CTRADER_CLIENT_ID")?;
20    let secret = std::env::var("CTRADER_SECRET")?;
21    let token = std::env::var("CTRADER_TOKEN")?;
22
23    let config = Config::new(client_id, secret).deadline(Duration::from_secs(5));
24
25    tracing::debug!("Connecting to demo.ctraderapi.com:5035 …");
26    let client = Client::start(config).await?;
27    tracing::debug!("✓ Connected and application authenticated");
28
29    // Fetch all accounts linked to the token
30    let res = client.get_accounts_by_access_token(&token).await?;
31
32    if res.ctid_trader_account.is_empty() {
33        tracing::debug!("No accounts found for this access token.");
34    } else {
35        tracing::debug!("\nFound {} account(s):\n", res.ctid_trader_account.len());
36        tracing::debug!(
37            "{:<25} {:<8} {:<15} {}",
38            "ctidTradingAccountId",
39            "Type",
40            "TraderLogin",
41            "Broker"
42        );
43        tracing::debug!("{}", "-".repeat(65));
44        for acc in &res.ctid_trader_account {
45            tracing::debug!(
46                "{:<25} {:<8} {:<15} {}",
47                acc.ctid_trader_account_id,
48                if acc.is_live.unwrap_or(false) {
49                    "live"
50                } else {
51                    "demo"
52                },
53                acc.trader_login.unwrap_or(0),
54                acc.broker_title_short.as_deref().unwrap_or(""),
55            );
56        }
57    }
58
59    Ok(())
60}