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: StringclientId from openapi.ctrader.com → your app → Credentials.
client_secret: StringclientSecret from the same location.
live: boolUse live servers (live.ctraderapi.com) when true, demo otherwise.
deadline: DurationPer-request deadline. Defaults to 5 seconds.
Implementations§
Source§impl Config
impl Config
Sourcepub fn new(
client_id: impl Into<String>,
client_secret: impl Into<String>,
) -> Self
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
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}Additional examples can be found in:
Sourcepub fn deadline(self, d: Duration) -> Self
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
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}Additional examples can be found in:
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Config
impl RefUnwindSafe for Config
impl Send for Config
impl Sync for Config
impl Unpin for Config
impl UnsafeUnpin for Config
impl UnwindSafe for Config
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more