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
483impl PositionMarket {
484    /// Checks if the current financial instrument is a call option.
485    ///
486    /// A call option is a financial derivative that gives the holder the right (but not the obligation)
487    /// to buy an underlying asset at a specified price within a specified time period. This method checks
488    /// whether the instrument represented by this instance is a call option by inspecting the `instrument_name`
489    /// field.
490    ///
491    /// # Returns
492    ///
493    /// * `true` if the instrument's name contains the substring `"CALL"`, indicating it is a call option.
494    /// * `false` otherwise.
495    ///
496    pub fn is_call(&self) -> bool {
497        self.instrument_name.contains("CALL")
498    }
499
500    /// Checks if the financial instrument is a "PUT" option.
501    ///
502    /// This method examines the `instrument_name` field of the struct to determine
503    /// if it contains the substring "PUT". If the substring is found, the method
504    /// returns `true`, indicating that the instrument is categorized as a "PUT" option.
505    /// Otherwise, it returns `false`.
506    ///
507    /// # Returns
508    /// * `true` - If `instrument_name` contains the substring "PUT".
509    /// * `false` - If `instrument_name` does not contain the substring "PUT".
510    ///
511    pub fn is_put(&self) -> bool {
512        self.instrument_name.contains("PUT")
513    }
514}
515
516/// Working order
517#[derive(DebugPretty, Clone, DisplaySimple, Deserialize, Serialize)]
518pub struct WorkingOrder {
519    /// Details of the working order
520    #[serde(rename = "workingOrderData")]
521    pub working_order_data: WorkingOrderData,
522    /// Market information for the working order
523    #[serde(rename = "marketData")]
524    pub market_data: AccountMarketData,
525}
526
527/// Details of a working order
528#[derive(DebugPretty, Clone, DisplaySimple, Deserialize, Serialize)]
529pub struct WorkingOrderData {
530    /// Unique identifier for the deal
531    #[serde(rename = "dealId")]
532    pub deal_id: String,
533    /// Direction of the order (buy or sell)
534    pub direction: Direction,
535    /// Instrument EPIC identifier
536    pub epic: String,
537    /// Size/quantity of the order
538    #[serde(rename = "orderSize")]
539    pub order_size: f64,
540    /// Price level for the order
541    #[serde(rename = "orderLevel")]
542    pub order_level: f64,
543    /// Time in force for the order
544    #[serde(rename = "timeInForce")]
545    pub time_in_force: TimeInForce,
546    /// Expiry date for GTD orders
547    #[serde(rename = "goodTillDate")]
548    pub good_till_date: Option<String>,
549    /// ISO formatted expiry date for GTD orders
550    #[serde(rename = "goodTillDateISO")]
551    pub good_till_date_iso: Option<String>,
552    /// Date and time when the order was created
553    #[serde(rename = "createdDate")]
554    pub created_date: String,
555    /// UTC date and time when the order was created
556    #[serde(rename = "createdDateUTC")]
557    pub created_date_utc: String,
558    /// Whether the order has a guaranteed stop
559    #[serde(rename = "guaranteedStop")]
560    pub guaranteed_stop: bool,
561    /// Type of the order
562    #[serde(rename = "orderType")]
563    pub order_type: OrderType,
564    /// Distance for stop loss
565    #[serde(rename = "stopDistance")]
566    pub stop_distance: Option<f64>,
567    /// Distance for take profit
568    #[serde(rename = "limitDistance")]
569    pub limit_distance: Option<f64>,
570    /// Currency code for the order
571    #[serde(rename = "currencyCode")]
572    pub currency_code: String,
573    /// Whether direct market access is enabled
574    pub dma: bool,
575    /// Premium for limited risk
576    #[serde(rename = "limitedRiskPremium")]
577    pub limited_risk_premium: Option<f64>,
578    /// Price level for take profit
579    #[serde(rename = "limitLevel", default)]
580    pub limit_level: Option<f64>,
581    /// Price level for stop loss
582    #[serde(rename = "stopLevel", default)]
583    pub stop_level: Option<f64>,
584    /// Client-generated reference for the deal
585    #[serde(rename = "dealReference", default)]
586    pub deal_reference: Option<String>,
587}
588
589/// Market data for a working order
590#[derive(DebugPretty, Clone, DisplaySimple, Deserialize, Serialize)]
591pub struct AccountMarketData {
592    /// Human-readable name of the instrument
593    #[serde(rename = "instrumentName")]
594    pub instrument_name: String,
595    /// Exchange identifier
596    #[serde(rename = "exchangeId")]
597    pub exchange_id: String,
598    /// Expiry date of the instrument
599    pub expiry: String,
600    /// Current status of the market
601    #[serde(rename = "marketStatus")]
602    pub market_status: MarketState,
603    /// Unique identifier for the market
604    pub epic: String,
605    /// Type of the instrument
606    #[serde(rename = "instrumentType")]
607    pub instrument_type: InstrumentType,
608    /// Size of one lot
609    #[serde(rename = "lotSize")]
610    pub lot_size: f64,
611    /// Highest price of the current trading session
612    pub high: Option<f64>,
613    /// Lowest price of the current trading session
614    pub low: Option<f64>,
615    /// Percentage change in price since previous close
616    #[serde(rename = "percentageChange")]
617    pub percentage_change: f64,
618    /// Net change in price since previous close
619    #[serde(rename = "netChange")]
620    pub net_change: f64,
621    /// Current bid price
622    pub bid: Option<f64>,
623    /// Current offer/ask price
624    pub offer: Option<f64>,
625    /// Time of the last price update
626    #[serde(rename = "updateTime")]
627    pub update_time: String,
628    /// UTC time of the last price update
629    #[serde(rename = "updateTimeUTC")]
630    pub update_time_utc: String,
631    /// Delay time in milliseconds for market data
632    #[serde(rename = "delayTime")]
633    pub delay_time: i64,
634    /// Whether streaming prices are available for this market
635    #[serde(rename = "streamingPricesAvailable")]
636    pub streaming_prices_available: bool,
637    /// Factor for scaling prices
638    #[serde(rename = "scalingFactor")]
639    pub scaling_factor: i64,
640}
641
642impl AccountMarketData {
643    /// Checks if the current financial instrument is a call option.
644    ///
645    /// A call option is a financial derivative that gives the holder the right (but not the obligation)
646    /// to buy an underlying asset at a specified price within a specified time period. This method checks
647    /// whether the instrument represented by this instance is a call option by inspecting the `instrument_name`
648    /// field.
649    ///
650    /// # Returns
651    ///
652    /// * `true` if the instrument's name contains the substring `"CALL"`, indicating it is a call option.
653    /// * `false` otherwise.
654    ///
655    pub fn is_call(&self) -> bool {
656        self.instrument_name.contains("CALL")
657    }
658
659    /// Checks if the financial instrument is a "PUT" option.
660    ///
661    /// This method examines the `instrument_name` field of the struct to determine
662    /// if it contains the substring "PUT". If the substring is found, the method
663    /// returns `true`, indicating that the instrument is categorized as a "PUT" option.
664    /// Otherwise, it returns `false`.
665    ///
666    /// # Returns
667    /// * `true` - If `instrument_name` contains the substring "PUT".
668    /// * `false` - If `instrument_name` does not contain the substring "PUT".
669    ///
670    pub fn is_put(&self) -> bool {
671        self.instrument_name.contains("PUT")
672    }
673}
674
675/// Transaction metadata
676#[derive(DebugPretty, Clone, DisplaySimple, Deserialize, Serialize)]
677pub struct TransactionMetadata {
678    /// Pagination information
679    #[serde(rename = "pageData")]
680    pub page_data: PageData,
681    /// Total number of transactions
682    pub size: i32,
683}
684
685/// Pagination information
686#[derive(DebugPretty, Clone, DisplaySimple, Deserialize, Serialize)]
687pub struct PageData {
688    /// Current page number
689    #[serde(rename = "pageNumber")]
690    pub page_number: i32,
691    /// Number of items per page
692    #[serde(rename = "pageSize")]
693    pub page_size: i32,
694    /// Total number of pages
695    #[serde(rename = "totalPages")]
696    pub total_pages: i32,
697}
698
699/// Individual transaction
700#[derive(DebugPretty, DisplaySimple, Clone, Deserialize, Serialize)]
701pub struct AccountTransaction {
702    /// Date and time of the transaction
703    pub date: String,
704    /// UTC date and time of the transaction
705    #[serde(rename = "dateUtc")]
706    pub date_utc: String,
707    /// Represents the date and time in UTC when an event or entity was opened or initiated.
708    #[serde(rename = "openDateUtc")]
709    pub open_date_utc: String,
710    /// Name of the instrument
711    #[serde(rename = "instrumentName")]
712    pub instrument_name: String,
713    /// Time period of the transaction
714    pub period: String,
715    /// Profit or loss amount
716    #[serde(rename = "profitAndLoss")]
717    pub profit_and_loss: String,
718    /// Type of transaction
719    #[serde(rename = "transactionType")]
720    pub transaction_type: String,
721    /// Reference identifier for the transaction
722    pub reference: String,
723    /// Opening price level
724    #[serde(rename = "openLevel")]
725    pub open_level: String,
726    /// Closing price level
727    #[serde(rename = "closeLevel")]
728    pub close_level: String,
729    /// Size/quantity of the transaction
730    pub size: String,
731    /// Currency of the transaction
732    pub currency: String,
733    /// Whether this is a cash transaction
734    #[serde(rename = "cashTransaction")]
735    pub cash_transaction: bool,
736}
737
738impl AccountTransaction {
739    /// Checks if the current financial instrument is a call option.
740    ///
741    /// A call option is a financial derivative that gives the holder the right (but not the obligation)
742    /// to buy an underlying asset at a specified price within a specified time period. This method checks
743    /// whether the instrument represented by this instance is a call option by inspecting the `instrument_name`
744    /// field.
745    ///
746    /// # Returns
747    ///
748    /// * `true` if the instrument's name contains the substring `"CALL"`, indicating it is a call option.
749    /// * `false` otherwise.
750    ///
751    pub fn is_call(&self) -> bool {
752        self.instrument_name.contains("CALL")
753    }
754
755    /// Checks if the financial instrument is a "PUT" option.
756    ///
757    /// This method examines the `instrument_name` field of the struct to determine
758    /// if it contains the substring "PUT". If the substring is found, the method
759    /// returns `true`, indicating that the instrument is categorized as a "PUT" option.
760    /// Otherwise, it returns `false`.
761    ///
762    /// # Returns
763    /// * `true` - If `instrument_name` contains the substring "PUT".
764    /// * `false` - If `instrument_name` does not contain the substring "PUT".
765    ///
766    pub fn is_put(&self) -> bool {
767        self.instrument_name.contains("PUT")
768    }
769}
770
771/// Representation of account data received from the IG Markets streaming API
772#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize, Default)]
773pub struct AccountData {
774    /// Name of the item this data belongs to
775    pub item_name: String,
776    /// Position of the item in the subscription
777    pub item_pos: i32,
778    /// All account fields
779    pub fields: AccountFields,
780    /// Fields that have changed in this update
781    pub changed_fields: AccountFields,
782    /// Whether this is a snapshot or an update
783    pub is_snapshot: bool,
784}
785
786/// Fields containing account financial information
787#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize, Default)]
788pub struct AccountFields {
789    #[serde(rename = "PNL")]
790    #[serde(with = "string_as_float_opt")]
791    #[serde(skip_serializing_if = "Option::is_none")]
792    pnl: Option<f64>,
793
794    #[serde(rename = "DEPOSIT")]
795    #[serde(with = "string_as_float_opt")]
796    #[serde(skip_serializing_if = "Option::is_none")]
797    deposit: Option<f64>,
798
799    #[serde(rename = "AVAILABLE_CASH")]
800    #[serde(with = "string_as_float_opt")]
801    #[serde(skip_serializing_if = "Option::is_none")]
802    available_cash: Option<f64>,
803
804    #[serde(rename = "PNL_LR")]
805    #[serde(with = "string_as_float_opt")]
806    #[serde(skip_serializing_if = "Option::is_none")]
807    pnl_lr: Option<f64>,
808
809    #[serde(rename = "PNL_NLR")]
810    #[serde(with = "string_as_float_opt")]
811    #[serde(skip_serializing_if = "Option::is_none")]
812    pnl_nlr: Option<f64>,
813
814    #[serde(rename = "FUNDS")]
815    #[serde(with = "string_as_float_opt")]
816    #[serde(skip_serializing_if = "Option::is_none")]
817    funds: Option<f64>,
818
819    #[serde(rename = "MARGIN")]
820    #[serde(with = "string_as_float_opt")]
821    #[serde(skip_serializing_if = "Option::is_none")]
822    margin: Option<f64>,
823
824    #[serde(rename = "MARGIN_LR")]
825    #[serde(with = "string_as_float_opt")]
826    #[serde(skip_serializing_if = "Option::is_none")]
827    margin_lr: Option<f64>,
828
829    #[serde(rename = "MARGIN_NLR")]
830    #[serde(with = "string_as_float_opt")]
831    #[serde(skip_serializing_if = "Option::is_none")]
832    margin_nlr: Option<f64>,
833
834    #[serde(rename = "AVAILABLE_TO_DEAL")]
835    #[serde(with = "string_as_float_opt")]
836    #[serde(skip_serializing_if = "Option::is_none")]
837    available_to_deal: Option<f64>,
838
839    #[serde(rename = "EQUITY")]
840    #[serde(with = "string_as_float_opt")]
841    #[serde(skip_serializing_if = "Option::is_none")]
842    equity: Option<f64>,
843
844    #[serde(rename = "EQUITY_USED")]
845    #[serde(with = "string_as_float_opt")]
846    #[serde(skip_serializing_if = "Option::is_none")]
847    equity_used: Option<f64>,
848}
849
850impl AccountData {
851    /// Converts an ItemUpdate from the Lightstreamer API to an AccountData object
852    ///
853    /// # Arguments
854    /// * `item_update` - The ItemUpdate received from the Lightstreamer API
855    ///
856    /// # Returns
857    /// * `Result<Self, String>` - The converted AccountData or an error message
858    pub fn from_item_update(item_update: &ItemUpdate) -> Result<Self, String> {
859        // Extract the item_name, defaulting to an empty string if None
860        let item_name = item_update.item_name.clone().unwrap_or_default();
861
862        // Convert item_pos from usize to i32
863        let item_pos = item_update.item_pos as i32;
864
865        // Extract is_snapshot
866        let is_snapshot = item_update.is_snapshot;
867
868        // Convert fields
869        let fields = Self::create_account_fields(&item_update.fields)?;
870
871        // Convert changed_fields by first creating a HashMap<String, Option<String>>
872        let mut changed_fields_map: HashMap<String, Option<String>> = HashMap::new();
873        for (key, value) in &item_update.changed_fields {
874            changed_fields_map.insert(key.clone(), Some(value.clone()));
875        }
876        let changed_fields = Self::create_account_fields(&changed_fields_map)?;
877
878        Ok(AccountData {
879            item_name,
880            item_pos,
881            fields,
882            changed_fields,
883            is_snapshot,
884        })
885    }
886
887    /// Helper method to create AccountFields from a HashMap of field values
888    ///
889    /// # Arguments
890    /// * `fields_map` - HashMap containing field names and their string values
891    ///
892    /// # Returns
893    /// * `Result<AccountFields, String>` - The parsed AccountFields or an error message
894    fn create_account_fields(
895        fields_map: &HashMap<String, Option<String>>,
896    ) -> Result<AccountFields, String> {
897        // Helper function to safely get a field value
898        let get_field = |key: &str| -> Option<String> { fields_map.get(key).cloned().flatten() };
899
900        // Helper function to parse float values
901        let parse_float = |key: &str| -> Result<Option<f64>, String> {
902            match get_field(key) {
903                Some(val) if !val.is_empty() => val
904                    .parse::<f64>()
905                    .map(Some)
906                    .map_err(|_| format!("Failed to parse {key} as float: {val}")),
907                _ => Ok(None),
908            }
909        };
910
911        Ok(AccountFields {
912            pnl: parse_float("PNL")?,
913            deposit: parse_float("DEPOSIT")?,
914            available_cash: parse_float("AVAILABLE_CASH")?,
915            pnl_lr: parse_float("PNL_LR")?,
916            pnl_nlr: parse_float("PNL_NLR")?,
917            funds: parse_float("FUNDS")?,
918            margin: parse_float("MARGIN")?,
919            margin_lr: parse_float("MARGIN_LR")?,
920            margin_nlr: parse_float("MARGIN_NLR")?,
921            available_to_deal: parse_float("AVAILABLE_TO_DEAL")?,
922            equity: parse_float("EQUITY")?,
923            equity_used: parse_float("EQUITY_USED")?,
924        })
925    }
926}
927
928impl From<&ItemUpdate> for AccountData {
929    fn from(item_update: &ItemUpdate) -> Self {
930        Self::from_item_update(item_update).unwrap_or_else(|_| AccountData::default())
931    }
932}
933
934#[cfg(test)]
935mod tests {
936    use super::*;
937    use crate::presentation::order::Direction;
938
939    fn sample_position_details(direction: Direction, level: f64, size: f64) -> PositionDetails {
940        PositionDetails {
941            contract_size: 1.0,
942            created_date: "2025/10/30 18:13:53:000".to_string(),
943            created_date_utc: "2025-10-30T17:13:53".to_string(),
944            deal_id: "DIAAAAVJNQPWZAG".to_string(),
945            deal_reference: "RZ0RQ1K8V1S1JN2".to_string(),
946            direction,
947            limit_level: None,
948            level,
949            size,
950            stop_level: None,
951            trailing_step: None,
952            trailing_stop_distance: None,
953            currency: "USD".to_string(),
954            controlled_risk: false,
955            limited_risk_premium: None,
956        }
957    }
958
959    fn sample_market(bid: Option<f64>, offer: Option<f64>) -> PositionMarket {
960        PositionMarket {
961            instrument_name: "US 500 6910 PUT ($1)".to_string(),
962            expiry: "DEC-25".to_string(),
963            epic: "OP.D.OTCSPX3.6910P.IP".to_string(),
964            instrument_type: "UNKNOWN".to_string(),
965            lot_size: 1.0,
966            high: Some(153.43),
967            low: Some(147.42),
968            percentage_change: 0.61,
969            net_change: 6895.38,
970            bid,
971            offer,
972            update_time: "05:55:59".to_string(),
973            update_time_utc: "05:55:59".to_string(),
974            delay_time: 0,
975            streaming_prices_available: true,
976            market_status: "TRADEABLE".to_string(),
977            scaling_factor: 1,
978        }
979    }
980
981    #[test]
982    fn pnl_sell_uses_offer_and_matches_sample_data() {
983        // Given the provided sample data (SELL):
984        // size = 1.0, level = 155.14, offer = 152.82
985        // value = 155.14, current_value = 152.82 => pnl = 155.14 - 152.82 = 2.32
986        let details = sample_position_details(Direction::Sell, 155.14, 1.0);
987        let market = sample_market(Some(151.32), Some(152.82));
988        let position = Position {
989            position: details,
990            market,
991            pnl: None,
992        };
993
994        let pnl = position.pnl();
995        assert!((pnl - 2.32).abs() < 1e-9, "expected 2.32, got {}", pnl);
996    }
997
998    #[test]
999    fn pnl_buy_uses_bid_and_computes_difference() {
1000        // For BUY: pnl = current_value - value
1001        // Using size = 1.0, level = 155.14, bid = 151.32 => pnl = 151.32 - 155.14 = -3.82
1002        let details = sample_position_details(Direction::Buy, 155.14, 1.0);
1003        let market = sample_market(Some(151.32), Some(152.82));
1004        let position = Position {
1005            position: details,
1006            market,
1007            pnl: None,
1008        };
1009
1010        let pnl = position.pnl();
1011        assert!((pnl + 3.82).abs() < 1e-9, "expected -3.82, got {}", pnl);
1012    }
1013
1014    #[test]
1015    fn pnl_field_overrides_calculation_when_present() {
1016        let details = sample_position_details(Direction::Sell, 155.14, 1.0);
1017        let market = sample_market(Some(151.32), Some(152.82));
1018        // Set explicit pnl different from calculated (which would be 2.32)
1019        let position = Position {
1020            position: details,
1021            market,
1022            pnl: Some(10.0),
1023        };
1024        assert_eq!(position.pnl(), 10.0);
1025    }
1026
1027    #[test]
1028    fn pnl_sell_is_zero_when_offer_missing() {
1029        // When offer is missing for SELL, unwrap_or(value) makes current_value == value => pnl = 0
1030        let details = sample_position_details(Direction::Sell, 155.14, 1.0);
1031        let market = sample_market(Some(151.32), None);
1032        let position = Position {
1033            position: details,
1034            market,
1035            pnl: None,
1036        };
1037        assert!((position.pnl() - 0.0).abs() < 1e-12);
1038    }
1039
1040    #[test]
1041    fn pnl_buy_is_zero_when_bid_missing() {
1042        // When bid is missing for BUY, unwrap_or(value) makes current_value == value => pnl = 0
1043        let details = sample_position_details(Direction::Buy, 155.14, 1.0);
1044        let market = sample_market(None, Some(152.82));
1045        let position = Position {
1046            position: details,
1047            market,
1048            pnl: None,
1049        };
1050        assert!((position.pnl() - 0.0).abs() < 1e-12);
1051    }
1052}