ctrader_connector/open_api/
account.rs1use rust_decimal::Decimal;
19
20use crate::common::errors::{ConnectorError, ValueError};
21use crate::common::ids::{AccountId, AssetId};
22use crate::common::time::Timestamp;
23use crate::open_api::client::OpenApiClient;
24use crate::open_api::messages::{
25 ProtoOaAccessRights, ProtoOaAccountType, ProtoOaTrader, ProtoOaTraderReq,
26};
27use crate::open_api::symbols::Asset;
28use crate::open_api::trading::{DEFAULT_MONEY_DIGITS, money};
29
30const LEVERAGE_SCALE: u32 = 2;
31
32const ACCOUNT_TYPE: &str = "account type";
33const ACCESS_RIGHTS: &str = "access rights";
34const DEPOSIT_ASSET: &str = "deposit asset";
35
36#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
38#[non_exhaustive]
39pub enum AccountType {
40 Hedged,
42 Netted,
44 SpreadBetting,
46}
47
48impl AccountType {
49 fn from_wire(raw: Option<i32>) -> Result<Self, ValueError> {
50 let Some(raw) = raw else {
51 return Ok(Self::Hedged);
52 };
53 match ProtoOaAccountType::try_from(raw) {
54 Ok(ProtoOaAccountType::Hedged) => Ok(Self::Hedged),
55 Ok(ProtoOaAccountType::Netted) => Ok(Self::Netted),
56 Ok(ProtoOaAccountType::SpreadBetting) => Ok(Self::SpreadBetting),
57 Err(_) => Err(ValueError::OutOfRange { kind: ACCOUNT_TYPE }),
58 }
59 }
60}
61
62#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
64#[non_exhaustive]
65pub enum AccessRights {
66 FullAccess,
68 CloseOnly,
70 NoTrading,
72 NoLogin,
74}
75
76impl AccessRights {
77 fn from_wire(raw: Option<i32>) -> Result<Self, ValueError> {
78 let Some(raw) = raw else {
79 return Ok(Self::FullAccess);
80 };
81 match ProtoOaAccessRights::try_from(raw) {
82 Ok(ProtoOaAccessRights::FullAccess) => Ok(Self::FullAccess),
83 Ok(ProtoOaAccessRights::CloseOnly) => Ok(Self::CloseOnly),
84 Ok(ProtoOaAccessRights::NoTrading) => Ok(Self::NoTrading),
85 Ok(ProtoOaAccessRights::NoLogin) => Ok(Self::NoLogin),
86 Err(_) => Err(ValueError::OutOfRange {
87 kind: ACCESS_RIGHTS,
88 }),
89 }
90 }
91}
92
93#[derive(Debug, Clone, PartialEq, Eq, Hash)]
97#[non_exhaustive]
98pub struct Trader {
99 pub account: AccountId,
101 pub balance: Decimal,
103 pub money_digits: u32,
105 pub deposit_asset: AssetId,
107 pub currency: String,
109 pub leverage: Option<Decimal>,
111 pub max_leverage: Option<u32>,
113 pub account_type: AccountType,
115 pub access_rights: AccessRights,
117 pub swap_free: bool,
119 pub limited_risk: bool,
122 pub manager_bonus: Option<Decimal>,
124 pub ib_bonus: Option<Decimal>,
126 pub non_withdrawable_bonus: Option<Decimal>,
128 pub broker_name: Option<String>,
130 pub registered: Option<Timestamp>,
132}
133
134impl Trader {
135 fn parse(trader: ProtoOaTrader, assets: &[Asset]) -> Result<Self, ValueError> {
138 let digits = trader.money_digits;
139 let amount = |raw: Option<i64>| raw.map(|raw| money(raw, digits)).transpose();
140 let deposit_asset = AssetId::new(trader.deposit_asset_id)?;
141 let currency = assets
142 .iter()
143 .find(|asset| asset.id == deposit_asset)
144 .map(|asset| asset.name.clone())
145 .ok_or(ValueError::OutOfRange {
146 kind: DEPOSIT_ASSET,
147 })?;
148 Ok(Self {
149 account: AccountId::new(trader.ctid_trader_account_id)?,
150 balance: money(trader.balance, digits)?,
151 money_digits: digits.unwrap_or(DEFAULT_MONEY_DIGITS),
152 deposit_asset,
153 currency,
154 leverage: trader
155 .leverage_in_cents
156 .map(|cents| Decimal::new(i64::from(cents), LEVERAGE_SCALE).normalize()),
157 max_leverage: trader.max_leverage,
158 account_type: AccountType::from_wire(trader.account_type)?,
159 access_rights: AccessRights::from_wire(trader.access_rights)?,
160 swap_free: trader.swap_free.unwrap_or(false),
161 limited_risk: trader.is_limited_risk.unwrap_or(false),
162 manager_bonus: amount(trader.manager_bonus)?,
163 ib_bonus: amount(trader.ib_bonus)?,
164 non_withdrawable_bonus: amount(trader.non_withdrawable_bonus)?,
165 broker_name: trader.broker_name,
166 registered: trader
167 .registration_timestamp
168 .map(Timestamp::from_millis)
169 .transpose()?,
170 })
171 }
172}
173
174impl OpenApiClient {
175 pub async fn trader(&self, account: AccountId) -> Result<Trader, ConnectorError> {
188 let request = ProtoOaTraderReq {
189 payload_type: None,
190 ctid_trader_account_id: account.get(),
191 };
192 let trader = self.connection().request(&request).await?.trader;
193 let assets = self.assets(account).await?;
194 Ok(Trader::parse(trader, &assets)?)
195 }
196}