Skip to main content

Config

Struct Config 

Source
pub struct Config {
    pub client_id: String,
    pub client_secret: String,
    pub live: bool,
    pub deadline: Duration,
}
Expand description

Configuration for the cTrader client.

Fields§

§client_id: String

clientId from openapi.ctrader.com → your app → Credentials.

§client_secret: String

clientSecret from the same location.

§live: bool

Use live servers (live.ctraderapi.com) when true, demo otherwise.

§deadline: Duration

Per-request deadline. Defaults to 5 seconds.

Implementations§

Source§

impl Config

Source

pub fn new( client_id: impl Into<String>, client_secret: impl Into<String>, ) -> Self

Examples found in repository?
examples/version.rs (line 22)
11async fn main() -> Result<(), Box<dyn std::error::Error>> {
12    dotenvy::dotenv().ok();
13
14    tracing_subscriber::fmt()
15        .with_env_filter("version=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
22    let config = Config::new(client_id, secret).deadline(Duration::from_secs(5));
23
24    tracing::debug!("Connecting to demo.ctraderapi.com:5035 …");
25    let client = Client::start(config).await?;
26    tracing::debug!("✓ Connected and application authenticated");
27
28    // Fetch all accounts linked to the token
29    let res = client.version().await?;
30
31    tracing::debug!("Version: {}", res.version);
32
33    Ok(())
34}
More examples
Hide additional examples
examples/get_trader.rs (line 22)
10async fn main() -> Result<(), Box<dyn std::error::Error>> {
11    dotenvy::dotenv().ok();
12
13    tracing_subscriber::fmt()
14        .with_env_filter("new_order=debug,ctrader_rs=debug")
15        .with_line_number(true)
16        .init();
17
18    let client_id = std::env::var("CTRADER_CLIENT_ID")?;
19    let secret = std::env::var("CTRADER_SECRET")?;
20    let account_id: i64 = std::env::var("CTRADER_ACCOUNT_ID")?.parse()?;
21
22    let config = Config::new(client_id, secret).deadline(Duration::from_secs(5));
23
24    tracing::debug!("Connecting to demo.ctraderapi.com:5035 …");
25    let client = Client::start(config).await?;
26    tracing::debug!("✓ Connected and application authenticated");
27
28    let res = client.get_trader(account_id).await?;
29
30    tracing::info!("Trader: {:?}", res);
31
32    Ok(())
33}
examples/get_position_unrealized_pnl.rs (line 22)
10async fn main() -> Result<(), Box<dyn std::error::Error>> {
11    dotenvy::dotenv().ok();
12
13    tracing_subscriber::fmt()
14        .with_env_filter("new_order=debug,ctrader_rs=debug")
15        .with_line_number(true)
16        .init();
17
18    let client_id = std::env::var("CTRADER_CLIENT_ID")?;
19    let secret = std::env::var("CTRADER_SECRET")?;
20    let account_id: i64 = std::env::var("CTRADER_ACCOUNT_ID")?.parse()?;
21
22    let config = Config::new(client_id, secret).deadline(Duration::from_secs(5));
23
24    tracing::debug!("Connecting to demo.ctraderapi.com:5035 …");
25    let client = Client::start(config).await?;
26    tracing::debug!("✓ Connected and application authenticated");
27
28    let res = client.position_unrealized_pnl(account_id).await?;
29
30    tracing::info!("Position Unrealized PnL: {:?}", res);
31
32    Ok(())
33}
examples/subscribe.rs (line 23)
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 account_id = std::env::var("CTRADER_ACCOUNT_ID")?.parse::<i64>()?;
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.subscribe_symbol(vec![61], account_id).await?;
31
32    println!("{:?}", res);
33
34    Ok(())
35}
examples/new_order.rs (line 23)
10async fn main() -> Result<(), Box<dyn std::error::Error>> {
11    dotenvy::dotenv().ok();
12
13    tracing_subscriber::fmt()
14        .with_env_filter("new_order=debug,ctrader_rs=debug")
15        .with_line_number(true)
16        .init();
17
18    let client_id = std::env::var("CTRADER_CLIENT_ID")?;
19    let secret = std::env::var("CTRADER_SECRET")?;
20    let token = std::env::var("CTRADER_TOKEN")?;
21    let account_id: i64 = std::env::var("CTRADER_ACCOUNT_ID")?.parse()?;
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    // Authenticate the account
30    let auth_res = client.account_auth(account_id, &token).await?;
31    assert_eq!(auth_res.ctid_trader_account_id, account_id);
32    tracing::debug!("✓ Account {account_id} authenticated");
33
34    let res = client
35        .new_market_order(account_id, 41, ProtoOaTradeSide::Buy, 100000)
36        .await?;
37
38    tracing::debug!("{:?}", res);
39
40    // let res = client
41    //     .new_limit_order(account_id, 41, ProtoOaTradeSide::Buy, 1000, 4800.00)
42    //     .await?;
43
44    // tracing::debug!("{:?}", res);
45
46    // let res = client
47    //     .new_stop_loss_take_profit_order(account_id, 41, ProtoOaTradeSide::Buy, 100_000, 500, 1000)
48    //     .await?;
49
50    // tracing::debug!("{:?}", res);
51
52    // let res = client
53    //     .new_market_range_order(account_id, 41, ProtoOaTradeSide::Buy, 1000)
54    //     .await?;
55
56    // tracing::debug!("{:?}", res);
57
58    loop {}
59}
examples/get_accounts.rs (line 23)
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}
Source

pub fn live(self) -> Self

Source

pub fn deadline(self, d: Duration) -> Self

Examples found in repository?
examples/version.rs (line 22)
11async fn main() -> Result<(), Box<dyn std::error::Error>> {
12    dotenvy::dotenv().ok();
13
14    tracing_subscriber::fmt()
15        .with_env_filter("version=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
22    let config = Config::new(client_id, secret).deadline(Duration::from_secs(5));
23
24    tracing::debug!("Connecting to demo.ctraderapi.com:5035 …");
25    let client = Client::start(config).await?;
26    tracing::debug!("✓ Connected and application authenticated");
27
28    // Fetch all accounts linked to the token
29    let res = client.version().await?;
30
31    tracing::debug!("Version: {}", res.version);
32
33    Ok(())
34}
More examples
Hide additional examples
examples/get_trader.rs (line 22)
10async fn main() -> Result<(), Box<dyn std::error::Error>> {
11    dotenvy::dotenv().ok();
12
13    tracing_subscriber::fmt()
14        .with_env_filter("new_order=debug,ctrader_rs=debug")
15        .with_line_number(true)
16        .init();
17
18    let client_id = std::env::var("CTRADER_CLIENT_ID")?;
19    let secret = std::env::var("CTRADER_SECRET")?;
20    let account_id: i64 = std::env::var("CTRADER_ACCOUNT_ID")?.parse()?;
21
22    let config = Config::new(client_id, secret).deadline(Duration::from_secs(5));
23
24    tracing::debug!("Connecting to demo.ctraderapi.com:5035 …");
25    let client = Client::start(config).await?;
26    tracing::debug!("✓ Connected and application authenticated");
27
28    let res = client.get_trader(account_id).await?;
29
30    tracing::info!("Trader: {:?}", res);
31
32    Ok(())
33}
examples/get_position_unrealized_pnl.rs (line 22)
10async fn main() -> Result<(), Box<dyn std::error::Error>> {
11    dotenvy::dotenv().ok();
12
13    tracing_subscriber::fmt()
14        .with_env_filter("new_order=debug,ctrader_rs=debug")
15        .with_line_number(true)
16        .init();
17
18    let client_id = std::env::var("CTRADER_CLIENT_ID")?;
19    let secret = std::env::var("CTRADER_SECRET")?;
20    let account_id: i64 = std::env::var("CTRADER_ACCOUNT_ID")?.parse()?;
21
22    let config = Config::new(client_id, secret).deadline(Duration::from_secs(5));
23
24    tracing::debug!("Connecting to demo.ctraderapi.com:5035 …");
25    let client = Client::start(config).await?;
26    tracing::debug!("✓ Connected and application authenticated");
27
28    let res = client.position_unrealized_pnl(account_id).await?;
29
30    tracing::info!("Position Unrealized PnL: {:?}", res);
31
32    Ok(())
33}
examples/subscribe.rs (line 23)
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 account_id = std::env::var("CTRADER_ACCOUNT_ID")?.parse::<i64>()?;
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.subscribe_symbol(vec![61], account_id).await?;
31
32    println!("{:?}", res);
33
34    Ok(())
35}
examples/new_order.rs (line 23)
10async fn main() -> Result<(), Box<dyn std::error::Error>> {
11    dotenvy::dotenv().ok();
12
13    tracing_subscriber::fmt()
14        .with_env_filter("new_order=debug,ctrader_rs=debug")
15        .with_line_number(true)
16        .init();
17
18    let client_id = std::env::var("CTRADER_CLIENT_ID")?;
19    let secret = std::env::var("CTRADER_SECRET")?;
20    let token = std::env::var("CTRADER_TOKEN")?;
21    let account_id: i64 = std::env::var("CTRADER_ACCOUNT_ID")?.parse()?;
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    // Authenticate the account
30    let auth_res = client.account_auth(account_id, &token).await?;
31    assert_eq!(auth_res.ctid_trader_account_id, account_id);
32    tracing::debug!("✓ Account {account_id} authenticated");
33
34    let res = client
35        .new_market_order(account_id, 41, ProtoOaTradeSide::Buy, 100000)
36        .await?;
37
38    tracing::debug!("{:?}", res);
39
40    // let res = client
41    //     .new_limit_order(account_id, 41, ProtoOaTradeSide::Buy, 1000, 4800.00)
42    //     .await?;
43
44    // tracing::debug!("{:?}", res);
45
46    // let res = client
47    //     .new_stop_loss_take_profit_order(account_id, 41, ProtoOaTradeSide::Buy, 100_000, 500, 1000)
48    //     .await?;
49
50    // tracing::debug!("{:?}", res);
51
52    // let res = client
53    //     .new_market_range_order(account_id, 41, ProtoOaTradeSide::Buy, 1000)
54    //     .await?;
55
56    // tracing::debug!("{:?}", res);
57
58    loop {}
59}
examples/get_accounts.rs (line 23)
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}

Trait Implementations§

Source§

impl Clone for Config

Source§

fn clone(&self) -> Config

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Config

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more