ig_client/application/interfaces/market.rs
1use crate::error::AppError;
2use crate::model::requests::RecentPricesRequest;
3use crate::model::responses::{
4 CategoriesResponse, CategoryInstrumentsResponse, DBEntryResponse, HistoricalPricesResponse,
5 MarketNavigationResponse, MarketSearchResponse, MultipleMarketDetailsResponse,
6};
7use crate::presentation::market::{MarketData, MarketDetails};
8use async_trait::async_trait;
9
10/// Interface for the market service
11#[async_trait]
12pub trait MarketService: Send + Sync {
13 /// Searches markets by search term
14 async fn search_markets(&self, search_term: &str) -> Result<MarketSearchResponse, AppError>;
15
16 /// Gets details of a specific market by its EPIC
17 async fn get_market_details(&self, epic: &str) -> Result<MarketDetails, AppError>;
18
19 /// Gets details of multiple markets by their EPICs in a single request
20 ///
21 /// This method accepts a vector of EPICs and returns a vector of market details.
22 /// The EPICs are sent as a comma-separated list in a single API request.
23 ///
24 /// # Arguments
25 /// * `session` - The active IG session
26 /// * `epics` - A slice of EPICs to get details for
27 ///
28 /// # Returns
29 /// A vector of market details in the same order as the input EPICs
30 async fn get_multiple_market_details(
31 &self,
32 epics: &[String],
33 ) -> Result<MultipleMarketDetailsResponse, AppError>;
34
35 /// Gets historical prices for a market
36 async fn get_historical_prices(
37 &self,
38 epic: &str,
39 resolution: &str,
40 from: &str,
41 to: &str,
42 ) -> Result<HistoricalPricesResponse, AppError>;
43
44 /// Gets historical prices for a market using path parameters (API v2)
45 ///
46 /// # Arguments
47 /// * `epic` - Instrument epic
48 /// * `resolution` - Price resolution (SECOND, MINUTE, MINUTE_2, MINUTE_3, MINUTE_5, MINUTE_10, MINUTE_15, MINUTE_30, HOUR, HOUR_2, HOUR_3, HOUR_4, DAY, WEEK, MONTH)
49 /// * `start_date` - Start date (yyyy-MM-dd HH:mm:ss)
50 /// * `end_date` - End date (yyyy-MM-dd HH:mm:ss). Must be later than the start date
51 async fn get_historical_prices_by_date_range(
52 &self,
53 epic: &str,
54 resolution: &str,
55 start_date: &str,
56 end_date: &str,
57 ) -> Result<HistoricalPricesResponse, AppError>;
58
59 /// Gets recent historical prices with custom parameters
60 ///
61 /// # Arguments
62 /// * `params` - Request parameters including epic, resolution, and date range
63 ///
64 /// # Returns
65 /// Historical price data for the specified parameters
66 async fn get_recent_prices(
67 &self,
68 params: &RecentPricesRequest<'_>,
69 ) -> Result<HistoricalPricesResponse, AppError>;
70
71 /// Gets historical prices by number of data points (API v1)
72 ///
73 /// # Arguments
74 /// * `epic` - Instrument epic
75 /// * `resolution` - Price resolution
76 /// * `num_points` - Number of data points required
77 async fn get_historical_prices_by_count_v1(
78 &self,
79
80 epic: &str,
81 resolution: &str,
82 num_points: i32,
83 ) -> Result<HistoricalPricesResponse, AppError>;
84
85 /// Gets historical prices by number of data points (API v2)
86 ///
87 /// # Arguments
88 /// * `epic` - Instrument epic
89 /// * `resolution` - Price resolution
90 /// * `num_points` - Number of data points required
91 async fn get_historical_prices_by_count_v2(
92 &self,
93 epic: &str,
94 resolution: &str,
95 num_points: i32,
96 ) -> Result<HistoricalPricesResponse, AppError>;
97
98 /// Gets the top-level market navigation nodes
99 ///
100 /// This method returns the root nodes of the market hierarchy, which can be used
101 /// to navigate through the available markets.
102 async fn get_market_navigation(&self) -> Result<MarketNavigationResponse, AppError>;
103
104 /// Gets the market navigation node with the specified ID
105 ///
106 /// This method returns the child nodes and markets under the specified node ID.
107 ///
108 /// # Arguments
109 /// * `node_id` - The ID of the navigation node to retrieve
110 async fn get_market_navigation_node(
111 &self,
112 node_id: &str,
113 ) -> Result<MarketNavigationResponse, AppError>;
114
115 /// Navigates through all levels of the market hierarchy and collects all MarketData
116 ///
117 /// This method performs a comprehensive traversal of the IG Markets hierarchy,
118 /// starting from the root navigation and going through multiple levels to collect
119 /// all available market instruments.
120 ///
121 /// # Arguments
122 /// * `session` - The authenticated IG session
123 /// * `max_levels` - Maximum depth to traverse (default: 5 levels)
124 ///
125 /// # Returns
126 /// * `Result<Vec<MarketData>, AppError>` - Vector containing all found market instruments
127 async fn get_all_markets(&self) -> Result<Vec<MarketData>, AppError>;
128
129 /// Gets all markets converted to database entries format
130 ///
131 /// This method retrieves all available markets and converts them to a standardized
132 /// database entry format for storage or further processing.
133 ///
134 /// # Arguments
135 /// * `session` - The authenticated IG session
136 ///
137 /// # Returns
138 /// * `Result<Vec<DBEntry>, AppError>` - Vector of database entries representing all markets
139 async fn get_vec_db_entries(&self) -> Result<Vec<DBEntryResponse>, AppError>;
140
141 /// Gets all categories of instruments enabled for the IG account
142 ///
143 /// This method returns a list of all categories of instruments that are
144 /// available for trading on the account.
145 ///
146 /// # Returns
147 /// * `Result<CategoriesResponse, AppError>` - List of available categories
148 async fn get_categories(&self) -> Result<CategoriesResponse, AppError>;
149
150 /// Gets all instruments for a specific category
151 ///
152 /// This method returns all instruments belonging to the specified category,
153 /// with optional pagination support.
154 ///
155 /// # Arguments
156 /// * `category_id` - The identifier of the category
157 /// * `page_number` - Optional page number (default: 0)
158 /// * `page_size` - Optional page size (default: 150, max: 1000)
159 ///
160 /// # Returns
161 /// * `Result<CategoryInstrumentsResponse, AppError>` - List of instruments in the category
162 async fn get_category_instruments(
163 &self,
164 category_id: &str,
165 page_number: Option<i32>,
166 page_size: Option<i32>,
167 ) -> Result<CategoryInstrumentsResponse, AppError>;
168}