Skip to main content

new_order/
new_order.rs

1//! Open a new BUY order.
2//!
3//! Usage:
4//!   cargo run --example open_order
5
6use ctrader_rs::{proto::common::ProtoOaTradeSide, Client, Config};
7use std::time::Duration;
8
9#[tokio::main]
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}