Skip to main content

ctrader_rs/
position.rs

1///
2///
3///
4///
5///
6///
7///
8///
9///
10///
11///
12///
13///
14///
15///
16///
17use crate::{client::Client, error::Error, payload, proto::common::*};
18
19impl Client {
20    /// Close an open position, either fully or partially.
21    ///
22    ///
23    ///
24    ///
25    ///
26    ///
27    ///
28    ///
29    ///
30    ///
31    ///
32    ///
33    /// To close fully, pass the full position volume.
34    pub async fn close_position(
35        &self,
36        ctid_trader_account_id: i64,
37        position_id: i64,
38        volume: i64,
39    ) -> Result<ProtoOaExecutionEvent, Error> {
40        let req = ProtoOaClosePositionReq {
41            payload_type: Some(payload::OA_CLOSE_POSITION_REQ as i32),
42            ctid_trader_account_id,
43            position_id,
44            volume,
45        };
46
47        self.command(
48            payload::OA_CLOSE_POSITION_REQ,
49            req,
50            payload::OA_EXECUTION_EVENT,
51        )
52        .await
53    }
54
55    /// Amend the Stop Loss and/or Take Profit of an open position.
56    ///
57    ///
58    ///
59    ///
60    ///
61    ///
62    ///
63    ///
64    ///
65    ///
66    ///
67    ///
68    /// `stop_loss` and `take_profit` are absolute price levels.
69    pub async fn amend_position_sltp(
70        &self,
71        ctid_trader_account_id: i64,
72        position_id: i64,
73        stop_loss: Option<f64>,
74        take_profit: Option<f64>,
75        trailing_stop_loss: Option<bool>,
76    ) -> Result<ProtoOaExecutionEvent, Error> {
77        let req = ProtoOaAmendPositionSltpReq {
78            payload_type: Some(ProtoOaPayloadType::ProtoOaAmendPositionSltpReq as i32),
79            ctid_trader_account_id,
80            position_id,
81            stop_loss,
82            take_profit,
83            trailing_stop_loss,
84            ..Default::default()
85        };
86
87        self.command(
88            payload::OA_AMEND_POSITION_SLTP_REQ,
89            req,
90            payload::OA_EXECUTION_EVENT,
91        )
92        .await
93    }
94
95    /// Get unrealized P&L for all open positions.
96    ///
97    ///
98    ///
99    ///
100    ///
101    ///
102    ///
103    ///
104    ///
105    ///
106    ///
107    ///
108    ///
109    /// Note: monetary values in the response are scaled by `10^moneyDigits`.
110    pub async fn position_unrealized_pnl(
111        &self,
112        ctid_trader_account_id: i64,
113    ) -> Result<ProtoOaGetPositionUnrealizedPnLRes, Error> {
114        let req = ProtoOaGetPositionUnrealizedPnLReq {
115            payload_type: Some(payload::OA_GET_POSITION_UNREALIZED_PNL_REQ as i32),
116            ctid_trader_account_id,
117        };
118
119        self.command(
120            payload::OA_GET_POSITION_UNREALIZED_PNL_REQ,
121            req,
122            payload::OA_GET_POSITION_UNREALIZED_PNL_RES,
123        )
124        .await
125    }
126}
127
128#[cfg(test)]
129mod tests {
130
131    #[tokio::test]
132    async fn test() {}
133}