ig_client/presentation/
account.rs

1use crate::presentation::instrument::InstrumentType;
2use crate::presentation::market::MarketState;
3use crate::presentation::order::{Direction, OrderType, Status, TimeInForce};
4use crate::presentation::serialization::string_as_float_opt;
5use lightstreamer_rs::subscription::ItemUpdate;
6use pretty_simple_display::{DebugPretty, DisplaySimple};
7use serde::{Deserialize, Serialize};
8use std::collections::HashMap;
9use std::ops::Add;
10
11/// Account information
12#[derive(DebugPretty, DisplaySimple, Clone, Deserialize, Serialize)]
13pub struct AccountInfo {
14    /// List of accounts owned by the user
15    pub accounts: Vec<Account>,
16}
17
18/// Details of a specific account
19#[derive(DebugPretty, DisplaySimple, Clone, Deserialize, Serialize)]
20pub struct Account {
21    /// Unique identifier for the account
22    #[serde(rename = "accountId")]
23    pub account_id: String,
24    /// Name of the account
25    #[serde(rename = "accountName")]
26    pub account_name: String,
27    /// Type of the account (e.g., CFD, Spread bet)
28    #[serde(rename = "accountType")]
29    pub account_type: String,
30    /// Balance information for the account
31    pub balance: AccountBalance,
32    /// Base currency of the account
33    pub currency: String,
34    /// Current status of the account
35    pub status: String,
36    /// Whether this is the preferred account
37    pub preferred: bool,
38}
39
40/// Account balance information
41#[derive(DebugPretty, DisplaySimple, Clone, Deserialize, Serialize)]
42pub struct AccountBalance {
43    /// Total balance of the account
44    pub balance: f64,
45    /// Deposit amount
46    pub deposit: f64,
47    /// Current profit or loss
48    #[serde(rename = "profitLoss")]
49    pub profit_loss: f64,
50    /// Available funds for trading
51    pub available: f64,
52}
53
54/// Metadata for activity pagination
55#[derive(DebugPretty, DisplaySimple, Clone, Deserialize, Serialize)]
56pub struct ActivityMetadata {
57    /// Paging information
58    pub paging: Option<ActivityPaging>,
59}
60
61/// Paging information for activities
62#[derive(DebugPretty, DisplaySimple, Clone, Deserialize, Serialize)]
63pub struct ActivityPaging {
64    /// Number of items per page
65    pub size: Option<i32>,
66    /// URL for the next page of results
67    pub next: Option<String>,
68}
69
70#[derive(Debug, Copy, Clone, DisplaySimple, Deserialize, Serialize)]
71/// Type of account activity
72#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
73pub enum ActivityType {
74    /// Activity related to editing stop and limit orders
75    EditStopAndLimit,
76    /// Activity related to positions
77    Position,
78    /// System-generated activity
79    System,
80    /// Activity related to working orders
81    WorkingOrder,
82}
83
84/// Individual activity record
85#[derive(Debug, Clone, DisplaySimple, Deserialize, Serialize)]
86pub struct Activity {
87    /// Date and time of the activity
88    pub date: String,
89    /// Unique identifier for the deal
90    #[serde(rename = "dealId", default)]
91    pub deal_id: Option<String>,
92    /// Instrument EPIC identifier
93    #[serde(default)]
94    pub epic: Option<String>,
95    /// Time period of the activity
96    #[serde(default)]
97    pub period: Option<String>,
98    /// Client-generated reference for the deal
99    #[serde(rename = "dealReference", default)]
100    pub deal_reference: Option<String>,
101    /// Type of activity
102    #[serde(rename = "type")]
103    pub activity_type: ActivityType,
104    /// Status of the activity
105    #[serde(default)]
106    pub status: Option<Status>,
107    /// Description of the activity
108    #[serde(default)]
109    pub description: Option<String>,
110    /// Additional details about the activity
111    /// This is a string when detailed=false, and an object when detailed=true
112    #[serde(default)]
113    pub details: Option<ActivityDetails>,
114    /// Channel the activity occurred on (e.g., "WEB" or "Mobile")
115    #[serde(default)]
116    pub channel: Option<String>,
117    /// The currency, e.g., a pound symbol
118    #[serde(default)]
119    pub currency: Option<String>,
120    /// Price level
121    #[serde(default)]
122    pub level: Option<String>,
123}
124
125/// Detailed information about an activity
126/// Only available when using the detailed=true parameter
127#[derive(Debug, Clone, DisplaySimple, Deserialize, Serialize)]
128pub struct ActivityDetails {
129    /// Client-generated reference for the deal
130    #[serde(rename = "dealReference", default)]
131    pub deal_reference: Option<String>,
132    /// List of actions associated with this activity
133    #[serde(default)]
134    pub actions: Vec<ActivityAction>,
135    /// Name of the market
136    #[serde(rename = "marketName", default)]
137    pub market_name: Option<String>,
138    /// Date until which the order is valid
139    #[serde(rename = "goodTillDate", default)]
140    pub good_till_date: Option<String>,
141    /// Currency of the transaction
142    #[serde(default)]
143    pub currency: Option<String>,
144    /// Size/quantity of the transaction
145    #[serde(default)]
146    pub size: Option<f64>,
147    /// Direction of the transaction (BUY or SELL)
148    #[serde(default)]
149    pub direction: Option<Direction>,
150    /// Price level
151    #[serde(default)]
152    pub level: Option<f64>,
153    /// Stop level price
154    #[serde(rename = "stopLevel", default)]
155    pub stop_level: Option<f64>,
156    /// Distance for the stop
157    #[serde(rename = "stopDistance", default)]
158    pub stop_distance: Option<f64>,
159    /// Whether the stop is guaranteed
160    #[serde(rename = "guaranteedStop", default)]
161    pub guaranteed_stop: Option<bool>,
162    /// Distance for the trailing stop
163    #[serde(rename = "trailingStopDistance", default)]
164    pub trailing_stop_distance: Option<f64>,
165    /// Step size for the trailing stop
166    #[serde(rename = "trailingStep", default)]
167    pub trailing_step: Option<f64>,
168    /// Limit level price
169    #[serde(rename = "limitLevel", default)]
170    pub limit_level: Option<f64>,
171    /// Distance for the limit
172    #[serde(rename = "limitDistance", default)]
173    pub limit_distance: Option<f64>,
174}
175
176/// Types of actions that can be performed on an activity
177#[derive(Debug, Copy, Clone, DisplaySimple, Deserialize, Serialize)]
178#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
179pub enum ActionType {
180    /// A limit order was deleted
181    LimitOrderDeleted,
182    /// A limit order was filled
183    LimitOrderFilled,
184    /// A limit order was opened
185    LimitOrderOpened,
186    /// A limit order was rolled
187    LimitOrderRolled,
188    /// A position was closed
189    PositionClosed,
190    /// A position was deleted
191    PositionDeleted,
192    /// A position was opened
193    PositionOpened,
194    /// A position was partially closed
195    PositionPartiallyClosed,
196    /// A position was rolled
197    PositionRolled,
198    /// A stop/limit was amended
199    StopLimitAmended,
200    /// A stop order was amended
201    StopOrderAmended,
202    /// A stop order was deleted
203    StopOrderDeleted,
204    /// A stop order was filled
205    StopOrderFilled,
206    /// A stop order was opened
207    StopOrderOpened,
208    /// A stop order was rolled
209    StopOrderRolled,
210    /// Unknown action type
211    Unknown,
212    /// A working order was deleted
213    WorkingOrderDeleted,
214}
215
216/// Action associated with an activity
217#[derive(DebugPretty, DisplaySimple, Clone, Deserialize, Serialize)]
218#[serde(rename_all = "camelCase")]
219pub struct ActivityAction {
220    /// Type of action
221    pub action_type: ActionType,
222    /// Deal ID affected by this action
223    pub affected_deal_id: Option<String>,
224}
225
226/// Individual position
227#[derive(DebugPretty, Clone, DisplaySimple, Serialize, Deserialize)]
228pub struct Position {
229    /// Details of the position
230    pub position: PositionDetails,
231    /// Market information for the position
232    pub market: PositionMarket,
233    /// Profit and loss for the position
234    pub pnl: Option<f64>,
235}
236
237impl Position {
238    /// Calculates the profit and loss (PnL) for the current position
239    /// of a trader.
240    ///
241    /// The method determines PnL based on whether it is already cached
242    /// (`self.pnl`) or needs to be calculated from the position and
243    /// market details.
244    ///
245    /// # Returns
246    ///
247    /// A floating-point value that represents the PnL for the position.
248    /// Positive values indicate a profit, and negative values indicate a loss.
249    ///
250    /// # Logic
251    ///
252    /// - If `self.pnl` is available, it directly returns the cached value.
253    /// - If not, the PnL is calculated based on the direction of the position:
254    ///   - For a Buy position:
255    ///     - The PnL is calculated as the difference between the `current_value`
256    ///       (based on the `market.bid` price or fallback value) and the original
257    ///       `value` (based on the position's size and level).
258    ///   - For a Sell position:
259    ///     - The PnL is calculated as the difference between the original
260    ///       `value` and the `current_value` (based on the `market.offer`
261    ///       price or fallback value).
262    ///
263    /// # Assumptions
264    /// - The `market.bid` and `market.offer` values are optional, so fallback
265    ///   to the original position value is used if they are unavailable.
266    /// - `self.position.direction` must be either `Direction::Buy` or
267    ///   `Direction::Sell`.
268    ///
269    pub fn pnl(&self) -> f64 {
270        if let Some(pnl) = self.pnl {
271            pnl
272        } else {
273            match self.position.direction {
274                Direction::Buy => {
275                    let value = self.position.size * self.position.level;
276                    let current_value = self.position.size * self.market.bid.unwrap_or(value);
277                    current_value - value
278                }
279                Direction::Sell => {
280                    let value = self.position.size * self.position.level;
281                    let current_value = self.position.size * self.market.offer.unwrap_or(value);
282                    value - current_value
283                }
284            }
285        }
286    }
287
288    /// Updates the profit and loss (PnL) for the current position in the market.
289    ///
290    /// The method calculates the PnL based on the position's direction (Buy or Sell),
291    /// size, level (entry price), and the current bid or offer price from the market data.
292    /// The result is stored in the `pnl` field.
293    ///
294    /// # Calculation:
295    /// - If the position is a Buy:
296    ///     - Calculate the initial value of the position as `size * level`.
297    ///     - Calculate the current value of the position using the current `bid` price from the market,
298    ///       or use the initial value if the `bid` price is not available.
299    ///     - PnL is the difference between the current value and the initial value.
300    /// - If the position is a Sell:
301    ///     - Calculate the initial value of the position as `size * level`.
302    ///     - Calculate the current value of the position using the current `offer` price from the market,
303    ///       or use the initial value if the `offer` price is not available.
304    ///     - PnL is the difference between the initial value and the current value.
305    ///
306    /// # Fields Updated:
307    /// - `self.pnl`: The calculated profit or loss is updated in this field. If no valid market price
308    ///   (bid/offer) is available, `pnl` will be calculated based on the initial value.
309    ///
310    /// # Panics:
311    /// This function does not explicitly panic but relies on the `unwrap_or` method to handle cases
312    /// where the `bid` or `offer` is unavailable. It assumes that the market or position data are initialized correctly.
313    ///
314    pub fn update_pnl(&mut self) {
315        let pnl = match self.position.direction {
316            Direction::Buy => {
317                let value = self.position.size * self.position.level;
318                let current_value = self.position.size * self.market.bid.unwrap_or(value);
319                current_value - value
320            }
321            Direction::Sell => {
322                let value = self.position.size * self.position.level;
323                let current_value = self.position.size * self.market.offer.unwrap_or(value);
324                value - current_value
325            }
326        };
327        self.pnl = Some(pnl);
328    }
329}
330
331impl Add for Position {
332    type Output = Position;
333
334    fn add(self, other: Position) -> Position {
335        if self.market.epic != other.market.epic {
336            panic!("Cannot add positions from different markets");
337        }
338        Position {
339            position: self.position + other.position,
340            market: self.market,
341            pnl: match (self.pnl, other.pnl) {
342                (Some(a), Some(b)) => Some(a + b),
343                (Some(a), None) => Some(a),
344                (None, Some(b)) => Some(b),
345                (None, None) => None,
346            },
347        }
348    }
349}
350
351/// Details of a position
352#[derive(DebugPretty, DisplaySimple, Clone, Deserialize, Serialize)]
353pub struct PositionDetails {
354    /// Size of one contract
355    #[serde(rename = "contractSize")]
356    pub contract_size: f64,
357    /// Date and time when the position was created
358    #[serde(rename = "createdDate")]
359    pub created_date: String,
360    /// UTC date and time when the position was created
361    #[serde(rename = "createdDateUTC")]
362    pub created_date_utc: String,
363    /// Unique identifier for the deal
364    #[serde(rename = "dealId")]
365    pub deal_id: String,
366    /// Client-generated reference for the deal
367    #[serde(rename = "dealReference")]
368    pub deal_reference: String,
369    /// Direction of the position (buy or sell)
370    pub direction: Direction,
371    /// Price level for take profit
372    #[serde(rename = "limitLevel")]
373    pub limit_level: Option<f64>,
374    /// Opening price level of the position
375    pub level: f64,
376    /// Size/quantity of the position
377    pub size: f64,
378    /// Price level for stop loss
379    #[serde(rename = "stopLevel")]
380    pub stop_level: Option<f64>,
381    /// Step size for trailing stop
382    #[serde(rename = "trailingStep")]
383    pub trailing_step: Option<f64>,
384    /// Distance for trailing stop
385    #[serde(rename = "trailingStopDistance")]
386    pub trailing_stop_distance: Option<f64>,
387    /// Currency of the position
388    pub currency: String,
389    /// Whether the position has controlled risk
390    #[serde(rename = "controlledRisk")]
391    pub controlled_risk: bool,
392    /// Premium paid for limited risk
393    #[serde(rename = "limitedRiskPremium")]
394    pub limited_risk_premium: Option<f64>,
395}
396
397impl Add for PositionDetails {
398    type Output = PositionDetails;
399
400    fn add(self, other: PositionDetails) -> PositionDetails {
401        let (contract_size, size) = if self.direction != other.direction {
402            (
403                (self.contract_size - other.contract_size).abs(),
404                (self.size - other.size).abs(),
405            )
406        } else {
407            (
408                self.contract_size + other.contract_size,
409                self.size + other.size,
410            )
411        };
412
413        PositionDetails {
414            contract_size,
415            created_date: self.created_date,
416            created_date_utc: self.created_date_utc,
417            deal_id: self.deal_id,
418            deal_reference: self.deal_reference,
419            direction: self.direction,
420            limit_level: other.limit_level.or(self.limit_level),
421            level: (self.level + other.level) / 2.0, // Average level
422            size,
423            stop_level: other.stop_level.or(self.stop_level),
424            trailing_step: other.trailing_step.or(self.trailing_step),
425            trailing_stop_distance: other.trailing_stop_distance.or(self.trailing_stop_distance),
426            currency: self.currency.clone(),
427            controlled_risk: self.controlled_risk || other.controlled_risk,
428            limited_risk_premium: other.limited_risk_premium.or(self.limited_risk_premium),
429        }
430    }
431}
432
433/// Market information for a position
434#[derive(DebugPretty, DisplaySimple, Clone, Deserialize, Serialize)]
435pub struct PositionMarket {
436    /// Human-readable name of the instrument
437    #[serde(rename = "instrumentName")]
438    pub instrument_name: String,
439    /// Expiry date of the instrument
440    pub expiry: String,
441    /// Unique identifier for the market
442    pub epic: String,
443    /// Type of the instrument
444    #[serde(rename = "instrumentType")]
445    pub instrument_type: String,
446    /// Size of one lot
447    #[serde(rename = "lotSize")]
448    pub lot_size: f64,
449    /// Highest price of the current trading session
450    pub high: Option<f64>,
451    /// Lowest price of the current trading session
452    pub low: Option<f64>,
453    /// Percentage change in price since previous close
454    #[serde(rename = "percentageChange")]
455    pub percentage_change: f64,
456    /// Net change in price since previous close
457    #[serde(rename = "netChange")]
458    pub net_change: f64,
459    /// Current bid price
460    pub bid: Option<f64>,
461    /// Current offer/ask price
462    pub offer: Option<f64>,
463    /// Time of the last price update
464    #[serde(rename = "updateTime")]
465    pub update_time: String,
466    /// UTC time of the last price update
467    #[serde(rename = "updateTimeUTC")]
468    pub update_time_utc: String,
469    /// Delay time in milliseconds for market data
470    #[serde(rename = "delayTime")]
471    pub delay_time: i64,
472    /// Whether streaming prices are available for this market
473    #[serde(rename = "streamingPricesAvailable")]
474    pub streaming_prices_available: bool,
475    /// Current status of the market (e.g., "OPEN", "CLOSED")
476    #[serde(rename = "marketStatus")]
477    pub market_status: String,
478    /// Factor for scaling prices
479    #[serde(rename = "scalingFactor")]
480    pub scaling_factor: i64,
481}
482
483/// Working order
484#[derive(DebugPretty, Clone, DisplaySimple, Deserialize, Serialize)]
485pub struct WorkingOrder {
486    /// Details of the working order
487    #[serde(rename = "workingOrderData")]
488    pub working_order_data: WorkingOrderData,
489    /// Market information for the working order
490    #[serde(rename = "marketData")]
491    pub market_data: AccountMarketData,
492}
493
494/// Details of a working order
495#[derive(DebugPretty, Clone, DisplaySimple, Deserialize, Serialize)]
496pub struct WorkingOrderData {
497    /// Unique identifier for the deal
498    #[serde(rename = "dealId")]
499    pub deal_id: String,
500    /// Direction of the order (buy or sell)
501    pub direction: Direction,
502    /// Instrument EPIC identifier
503    pub epic: String,
504    /// Size/quantity of the order
505    #[serde(rename = "orderSize")]
506    pub order_size: f64,
507    /// Price level for the order
508    #[serde(rename = "orderLevel")]
509    pub order_level: f64,
510    /// Time in force for the order
511    #[serde(rename = "timeInForce")]
512    pub time_in_force: TimeInForce,
513    /// Expiry date for GTD orders
514    #[serde(rename = "goodTillDate")]
515    pub good_till_date: Option<String>,
516    /// ISO formatted expiry date for GTD orders
517    #[serde(rename = "goodTillDateISO")]
518    pub good_till_date_iso: Option<String>,
519    /// Date and time when the order was created
520    #[serde(rename = "createdDate")]
521    pub created_date: String,
522    /// UTC date and time when the order was created
523    #[serde(rename = "createdDateUTC")]
524    pub created_date_utc: String,
525    /// Whether the order has a guaranteed stop
526    #[serde(rename = "guaranteedStop")]
527    pub guaranteed_stop: bool,
528    /// Type of the order
529    #[serde(rename = "orderType")]
530    pub order_type: OrderType,
531    /// Distance for stop loss
532    #[serde(rename = "stopDistance")]
533    pub stop_distance: Option<f64>,
534    /// Distance for take profit
535    #[serde(rename = "limitDistance")]
536    pub limit_distance: Option<f64>,
537    /// Currency code for the order
538    #[serde(rename = "currencyCode")]
539    pub currency_code: String,
540    /// Whether direct market access is enabled
541    pub dma: bool,
542    /// Premium for limited risk
543    #[serde(rename = "limitedRiskPremium")]
544    pub limited_risk_premium: Option<f64>,
545    /// Price level for take profit
546    #[serde(rename = "limitLevel", default)]
547    pub limit_level: Option<f64>,
548    /// Price level for stop loss
549    #[serde(rename = "stopLevel", default)]
550    pub stop_level: Option<f64>,
551    /// Client-generated reference for the deal
552    #[serde(rename = "dealReference", default)]
553    pub deal_reference: Option<String>,
554}
555
556/// Market data for a working order
557#[derive(DebugPretty, Clone, DisplaySimple, Deserialize, Serialize)]
558pub struct AccountMarketData {
559    /// Human-readable name of the instrument
560    #[serde(rename = "instrumentName")]
561    pub instrument_name: String,
562    /// Exchange identifier
563    #[serde(rename = "exchangeId")]
564    pub exchange_id: String,
565    /// Expiry date of the instrument
566    pub expiry: String,
567    /// Current status of the market
568    #[serde(rename = "marketStatus")]
569    pub market_status: MarketState,
570    /// Unique identifier for the market
571    pub epic: String,
572    /// Type of the instrument
573    #[serde(rename = "instrumentType")]
574    pub instrument_type: InstrumentType,
575    /// Size of one lot
576    #[serde(rename = "lotSize")]
577    pub lot_size: f64,
578    /// Highest price of the current trading session
579    pub high: Option<f64>,
580    /// Lowest price of the current trading session
581    pub low: Option<f64>,
582    /// Percentage change in price since previous close
583    #[serde(rename = "percentageChange")]
584    pub percentage_change: f64,
585    /// Net change in price since previous close
586    #[serde(rename = "netChange")]
587    pub net_change: f64,
588    /// Current bid price
589    pub bid: Option<f64>,
590    /// Current offer/ask price
591    pub offer: Option<f64>,
592    /// Time of the last price update
593    #[serde(rename = "updateTime")]
594    pub update_time: String,
595    /// UTC time of the last price update
596    #[serde(rename = "updateTimeUTC")]
597    pub update_time_utc: String,
598    /// Delay time in milliseconds for market data
599    #[serde(rename = "delayTime")]
600    pub delay_time: i64,
601    /// Whether streaming prices are available for this market
602    #[serde(rename = "streamingPricesAvailable")]
603    pub streaming_prices_available: bool,
604    /// Factor for scaling prices
605    #[serde(rename = "scalingFactor")]
606    pub scaling_factor: i64,
607}
608
609/// Transaction metadata
610#[derive(DebugPretty, Clone, DisplaySimple, Deserialize, Serialize)]
611pub struct TransactionMetadata {
612    /// Pagination information
613    #[serde(rename = "pageData")]
614    pub page_data: PageData,
615    /// Total number of transactions
616    pub size: i32,
617}
618
619/// Pagination information
620#[derive(DebugPretty, Clone, DisplaySimple, Deserialize, Serialize)]
621pub struct PageData {
622    /// Current page number
623    #[serde(rename = "pageNumber")]
624    pub page_number: i32,
625    /// Number of items per page
626    #[serde(rename = "pageSize")]
627    pub page_size: i32,
628    /// Total number of pages
629    #[serde(rename = "totalPages")]
630    pub total_pages: i32,
631}
632
633/// Individual transaction
634#[derive(DebugPretty, DisplaySimple, Clone, Deserialize, Serialize)]
635pub struct AccountTransaction {
636    /// Date and time of the transaction
637    pub date: String,
638    /// UTC date and time of the transaction
639    #[serde(rename = "dateUtc")]
640    pub date_utc: String,
641    /// Represents the date and time in UTC when an event or entity was opened or initiated.
642    #[serde(rename = "openDateUtc")]
643    pub open_date_utc: String,
644    /// Name of the instrument
645    #[serde(rename = "instrumentName")]
646    pub instrument_name: String,
647    /// Time period of the transaction
648    pub period: String,
649    /// Profit or loss amount
650    #[serde(rename = "profitAndLoss")]
651    pub profit_and_loss: String,
652    /// Type of transaction
653    #[serde(rename = "transactionType")]
654    pub transaction_type: String,
655    /// Reference identifier for the transaction
656    pub reference: String,
657    /// Opening price level
658    #[serde(rename = "openLevel")]
659    pub open_level: String,
660    /// Closing price level
661    #[serde(rename = "closeLevel")]
662    pub close_level: String,
663    /// Size/quantity of the transaction
664    pub size: String,
665    /// Currency of the transaction
666    pub currency: String,
667    /// Whether this is a cash transaction
668    #[serde(rename = "cashTransaction")]
669    pub cash_transaction: bool,
670}
671
672/// Representation of account data received from the IG Markets streaming API
673#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize, Default)]
674pub struct AccountData {
675    /// Name of the item this data belongs to
676    pub item_name: String,
677    /// Position of the item in the subscription
678    pub item_pos: i32,
679    /// All account fields
680    pub fields: AccountFields,
681    /// Fields that have changed in this update
682    pub changed_fields: AccountFields,
683    /// Whether this is a snapshot or an update
684    pub is_snapshot: bool,
685}
686
687/// Fields containing account financial information
688#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize, Default)]
689pub struct AccountFields {
690    #[serde(rename = "PNL")]
691    #[serde(with = "string_as_float_opt")]
692    #[serde(skip_serializing_if = "Option::is_none")]
693    pnl: Option<f64>,
694
695    #[serde(rename = "DEPOSIT")]
696    #[serde(with = "string_as_float_opt")]
697    #[serde(skip_serializing_if = "Option::is_none")]
698    deposit: Option<f64>,
699
700    #[serde(rename = "AVAILABLE_CASH")]
701    #[serde(with = "string_as_float_opt")]
702    #[serde(skip_serializing_if = "Option::is_none")]
703    available_cash: Option<f64>,
704
705    #[serde(rename = "PNL_LR")]
706    #[serde(with = "string_as_float_opt")]
707    #[serde(skip_serializing_if = "Option::is_none")]
708    pnl_lr: Option<f64>,
709
710    #[serde(rename = "PNL_NLR")]
711    #[serde(with = "string_as_float_opt")]
712    #[serde(skip_serializing_if = "Option::is_none")]
713    pnl_nlr: Option<f64>,
714
715    #[serde(rename = "FUNDS")]
716    #[serde(with = "string_as_float_opt")]
717    #[serde(skip_serializing_if = "Option::is_none")]
718    funds: Option<f64>,
719
720    #[serde(rename = "MARGIN")]
721    #[serde(with = "string_as_float_opt")]
722    #[serde(skip_serializing_if = "Option::is_none")]
723    margin: Option<f64>,
724
725    #[serde(rename = "MARGIN_LR")]
726    #[serde(with = "string_as_float_opt")]
727    #[serde(skip_serializing_if = "Option::is_none")]
728    margin_lr: Option<f64>,
729
730    #[serde(rename = "MARGIN_NLR")]
731    #[serde(with = "string_as_float_opt")]
732    #[serde(skip_serializing_if = "Option::is_none")]
733    margin_nlr: Option<f64>,
734
735    #[serde(rename = "AVAILABLE_TO_DEAL")]
736    #[serde(with = "string_as_float_opt")]
737    #[serde(skip_serializing_if = "Option::is_none")]
738    available_to_deal: Option<f64>,
739
740    #[serde(rename = "EQUITY")]
741    #[serde(with = "string_as_float_opt")]
742    #[serde(skip_serializing_if = "Option::is_none")]
743    equity: Option<f64>,
744
745    #[serde(rename = "EQUITY_USED")]
746    #[serde(with = "string_as_float_opt")]
747    #[serde(skip_serializing_if = "Option::is_none")]
748    equity_used: Option<f64>,
749}
750
751impl AccountData {
752    /// Converts an ItemUpdate from the Lightstreamer API to an AccountData object
753    ///
754    /// # Arguments
755    /// * `item_update` - The ItemUpdate received from the Lightstreamer API
756    ///
757    /// # Returns
758    /// * `Result<Self, String>` - The converted AccountData or an error message
759    pub fn from_item_update(item_update: &ItemUpdate) -> Result<Self, String> {
760        // Extract the item_name, defaulting to an empty string if None
761        let item_name = item_update.item_name.clone().unwrap_or_default();
762
763        // Convert item_pos from usize to i32
764        let item_pos = item_update.item_pos as i32;
765
766        // Extract is_snapshot
767        let is_snapshot = item_update.is_snapshot;
768
769        // Convert fields
770        let fields = Self::create_account_fields(&item_update.fields)?;
771
772        // Convert changed_fields by first creating a HashMap<String, Option<String>>
773        let mut changed_fields_map: HashMap<String, Option<String>> = HashMap::new();
774        for (key, value) in &item_update.changed_fields {
775            changed_fields_map.insert(key.clone(), Some(value.clone()));
776        }
777        let changed_fields = Self::create_account_fields(&changed_fields_map)?;
778
779        Ok(AccountData {
780            item_name,
781            item_pos,
782            fields,
783            changed_fields,
784            is_snapshot,
785        })
786    }
787
788    /// Helper method to create AccountFields from a HashMap of field values
789    ///
790    /// # Arguments
791    /// * `fields_map` - HashMap containing field names and their string values
792    ///
793    /// # Returns
794    /// * `Result<AccountFields, String>` - The parsed AccountFields or an error message
795    fn create_account_fields(
796        fields_map: &HashMap<String, Option<String>>,
797    ) -> Result<AccountFields, String> {
798        // Helper function to safely get a field value
799        let get_field = |key: &str| -> Option<String> { fields_map.get(key).cloned().flatten() };
800
801        // Helper function to parse float values
802        let parse_float = |key: &str| -> Result<Option<f64>, String> {
803            match get_field(key) {
804                Some(val) if !val.is_empty() => val
805                    .parse::<f64>()
806                    .map(Some)
807                    .map_err(|_| format!("Failed to parse {key} as float: {val}")),
808                _ => Ok(None),
809            }
810        };
811
812        Ok(AccountFields {
813            pnl: parse_float("PNL")?,
814            deposit: parse_float("DEPOSIT")?,
815            available_cash: parse_float("AVAILABLE_CASH")?,
816            pnl_lr: parse_float("PNL_LR")?,
817            pnl_nlr: parse_float("PNL_NLR")?,
818            funds: parse_float("FUNDS")?,
819            margin: parse_float("MARGIN")?,
820            margin_lr: parse_float("MARGIN_LR")?,
821            margin_nlr: parse_float("MARGIN_NLR")?,
822            available_to_deal: parse_float("AVAILABLE_TO_DEAL")?,
823            equity: parse_float("EQUITY")?,
824            equity_used: parse_float("EQUITY_USED")?,
825        })
826    }
827}
828
829impl From<&ItemUpdate> for AccountData {
830    fn from(item_update: &ItemUpdate) -> Self {
831        Self::from_item_update(item_update).unwrap_or_else(|_| AccountData::default())
832    }
833}
834
835#[cfg(test)]
836mod tests {
837    use super::*;
838    use crate::presentation::order::Direction;
839
840    fn sample_position_details(direction: Direction, level: f64, size: f64) -> PositionDetails {
841        PositionDetails {
842            contract_size: 1.0,
843            created_date: "2025/10/30 18:13:53:000".to_string(),
844            created_date_utc: "2025-10-30T17:13:53".to_string(),
845            deal_id: "DIAAAAVJNQPWZAG".to_string(),
846            deal_reference: "RZ0RQ1K8V1S1JN2".to_string(),
847            direction,
848            limit_level: None,
849            level,
850            size,
851            stop_level: None,
852            trailing_step: None,
853            trailing_stop_distance: None,
854            currency: "USD".to_string(),
855            controlled_risk: false,
856            limited_risk_premium: None,
857        }
858    }
859
860    fn sample_market(bid: Option<f64>, offer: Option<f64>) -> PositionMarket {
861        PositionMarket {
862            instrument_name: "US 500 6910 PUT ($1)".to_string(),
863            expiry: "DEC-25".to_string(),
864            epic: "OP.D.OTCSPX3.6910P.IP".to_string(),
865            instrument_type: "UNKNOWN".to_string(),
866            lot_size: 1.0,
867            high: Some(153.43),
868            low: Some(147.42),
869            percentage_change: 0.61,
870            net_change: 6895.38,
871            bid,
872            offer,
873            update_time: "05:55:59".to_string(),
874            update_time_utc: "05:55:59".to_string(),
875            delay_time: 0,
876            streaming_prices_available: true,
877            market_status: "TRADEABLE".to_string(),
878            scaling_factor: 1,
879        }
880    }
881
882    #[test]
883    fn pnl_sell_uses_offer_and_matches_sample_data() {
884        // Given the provided sample data (SELL):
885        // size = 1.0, level = 155.14, offer = 152.82
886        // value = 155.14, current_value = 152.82 => pnl = 155.14 - 152.82 = 2.32
887        let details = sample_position_details(Direction::Sell, 155.14, 1.0);
888        let market = sample_market(Some(151.32), Some(152.82));
889        let position = Position {
890            position: details,
891            market,
892            pnl: None,
893        };
894
895        let pnl = position.pnl();
896        assert!((pnl - 2.32).abs() < 1e-9, "expected 2.32, got {}", pnl);
897    }
898
899    #[test]
900    fn pnl_buy_uses_bid_and_computes_difference() {
901        // For BUY: pnl = current_value - value
902        // Using size = 1.0, level = 155.14, bid = 151.32 => pnl = 151.32 - 155.14 = -3.82
903        let details = sample_position_details(Direction::Buy, 155.14, 1.0);
904        let market = sample_market(Some(151.32), Some(152.82));
905        let position = Position {
906            position: details,
907            market,
908            pnl: None,
909        };
910
911        let pnl = position.pnl();
912        assert!((pnl + 3.82).abs() < 1e-9, "expected -3.82, got {}", pnl);
913    }
914
915    #[test]
916    fn pnl_field_overrides_calculation_when_present() {
917        let details = sample_position_details(Direction::Sell, 155.14, 1.0);
918        let market = sample_market(Some(151.32), Some(152.82));
919        // Set explicit pnl different from calculated (which would be 2.32)
920        let position = Position {
921            position: details,
922            market,
923            pnl: Some(10.0),
924        };
925        assert_eq!(position.pnl(), 10.0);
926    }
927
928    #[test]
929    fn pnl_sell_is_zero_when_offer_missing() {
930        // When offer is missing for SELL, unwrap_or(value) makes current_value == value => pnl = 0
931        let details = sample_position_details(Direction::Sell, 155.14, 1.0);
932        let market = sample_market(Some(151.32), None);
933        let position = Position {
934            position: details,
935            market,
936            pnl: None,
937        };
938        assert!((position.pnl() - 0.0).abs() < 1e-12);
939    }
940
941    #[test]
942    fn pnl_buy_is_zero_when_bid_missing() {
943        // When bid is missing for BUY, unwrap_or(value) makes current_value == value => pnl = 0
944        let details = sample_position_details(Direction::Buy, 155.14, 1.0);
945        let market = sample_market(None, Some(152.82));
946        let position = Position {
947            position: details,
948            market,
949            pnl: None,
950        };
951        assert!((position.pnl() - 0.0).abs() < 1e-12);
952    }
953}