1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309

use crate::client::Client;
use crate::config::Config;
use crate::models::*;
use reqwest::Error;

pub struct IG {
	client: Client
}

impl IG {
	/// Creates a new API instance with the production configuration.
	pub fn live(account_id: String, api_key: String, username: String, password: String) -> IG {
		IG::new(account_id, api_key, username, password, Config::live())
	}

	/// Creates a new API instnace with the demo configuration.
	pub fn demo(account_id: String, api_key: String, username: String, password: String) -> IG {
		IG::new(account_id, api_key, username, password, Config::demo())
	}

	/// Creates a new API instance with a config.
	pub fn new(account_id: String, api_key: String, username: String, password: String, config: Config) -> IG {
		let client = Client::new(account_id, api_key, username, password, config);
		IG { client }
	}

	/// GET /accounts
	/// Returns a list of accounts belonging to the logged-in client.
	pub fn get_accounts(&self) -> Result<Accounts, Error> {
		let endpoint: String = "/accounts".into();
		let data: Accounts = self.client.get_signed(&endpoint, 1, None::<()>)?;
		Ok(data)
	}

	/// GET /accounts/preferences
	/// Returns account preferences.
	pub fn get_preferences(&self) -> Result<Preferences, Error> {
		let endpoint: String = "/accounts/preferences".into();
		let data: Preferences = self.client.get_signed(&endpoint, 1, None::<()>)?;
		Ok(data)
	}

	/// PUT /accounts/preferences
	/// Updates account preferences.
	pub fn update_preferences(&self, preferences: &Preferences) -> Result<OkResponse, Error> {
		let endpoint: String = "/accounts/preferences".into();
		let data: OkResponse = self.client.put_signed(&endpoint, 1, Some(preferences))?;
		Ok(data)
	}

	/// GET /history/activity
	/// Returns the account activity history.
	pub fn get_activity_history(&self, query: &ActivityHistoryQuery) -> Result<ActivityHistory, Error> {
		let endpoint: String = "/history/activity".into();
		let data: ActivityHistory = self.client.get_signed(&endpoint, 3, Some(query))?;
		Ok(data)
	}

	/// GET /history/transactions
	/// Returns the transaction history.
	/// By default returns the minute prices within the last 10 minutes.
	pub fn get_transaction_history(&self, query: &TransactionHistoryQuery) -> Result<TransactionHistory, Error> {
		let endpoint: String = "/history/transactions".into();
		let data: TransactionHistory = self.client.get_signed(&endpoint, 2, Some(query))?;
		Ok(data)
	}

	/// GET /clientsentiment
	/// Returns the client sentiment for the given instrument's market.
	pub fn get_client_sentiments(&self, query: &SentimentQuery) -> Result<Sentiments, Error> {		
		let endpoint = "/clientsentiment".to_string();
		let data: Sentiments = self.client.get_signed(&endpoint, 1, Some(query))?;
		Ok(data)
	}

	/// GET /clientsentiment/{marketId}
	/// Returns the client sentiment for the given instrument's market.
	pub fn get_client_sentiment(&self, market_id: &String) -> Result<Sentiment, Error> {
		let endpoint = format!("/clientsentiment/{}", market_id);
		let data: Sentiment = self.client.get_signed(&endpoint, 1, None::<()>)?;
		Ok(data)
	}

	/// GET /clientsentiment/related/{marketId}
	/// Returns a list of related (what others have traded) client sentiment for the given instrument's market.
	pub fn get_related_client_sentiment(&self, market_id: &String) -> Result<Sentiments, Error> {
		let endpoint = format!("/clientsentiment/related/{}", market_id);
		let data: Sentiments = self.client.get_signed(&endpoint, 1, None::<()>)?;
		Ok(data)
	}

	/// GET /confirms/{dealReference}
	/// Returns a deal confirmation for the given deal reference.
	pub fn get_deal_confirmation(&self, deal_reference: &String) -> Result<DealConfirmation, Error> {
		let endpoint = format!("/confirms/{}", deal_reference);
		let data: DealConfirmation = self.client.get_signed(&endpoint, 1, None::<()>)?;
		Ok(data)
	}

	/// GET /positions
	/// Returns all open positions for the active account.
	pub fn get_positions(&self) -> Result<Positions, Error> {
		let endpoint = "/positions".to_string();
		let data: Positions = self.client.get_signed(&endpoint, 2, None::<()>)?;
		Ok(data)
	}

	/// GET /positions/{dealId}
	/// Returns an open position for the active account by deal identifier.
	pub fn get_position(&self, deal_id: &String) -> Result<Position, Error> {
		let endpoint = format!("/positions/{}", deal_id);
		let data: Position = self.client.get_signed(&endpoint, 2, None::<()>)?;
		Ok(data)
	}

	/// DELETE /positions/otc
	/// Closes one or more OTC positions.
	pub fn close_position(&self, req: &ClosePosition) -> Result<DealRef, Error> {
		let endpoint: String = "/positions/otc".into();
		let data: DealRef = self.client.delete_signed(&endpoint, 1, Some(req))?;
		Ok(data)
	}

	/// POST /positions/otc
	/// Creates an OTC position.
	pub fn create_position(&self, req: &CreatePosition) -> Result<DealRef, Error> {
		let endpoint: String = "/positions/otc".into();
		let data: DealRef = self.client.post_signed(&endpoint, 2, Some(req))?;
		Ok(data)
	}

	/// PUT /positions/otc/{dealId}
	/// Updates an OTC position.
	pub fn update_position(&self, deal_id: &String, req: &UpdatePosition) -> Result<DealRef, Error> {
		let endpoint = format!("/positions/otc/{}", deal_id);
		let data: DealRef = self.client.put_signed(&endpoint, 2, Some(req))?;
		Ok(data)
	}

	/// GET /positions/sprintmarkets
	/// A list of sprint market positions.
	pub fn get_sprint_market_positions(&self) -> Result<SprintMarketPositions, Error> {
		let endpoint = "/positions/sprintmarkets".to_string();
		let data: SprintMarketPositions = self.client.get_signed(&endpoint, 2, None::<()>)?;
		Ok(data)
	}

	/// POST /positions/sprintmarkets
	/// Creates a sprint market position.
	pub fn create_sprint_market_position(&self, req: &CreateSprintMarketPosition) -> Result<DealRef, Error> {
		let endpoint = "/positions/sprintmarkets".to_string();
		let data: DealRef = self.client.post_signed(&endpoint, 1, Some(req))?;
		Ok(data)
	}

	/// GET /workingorders
	/// Returns all open working orders for the active account.
	pub fn get_working_orders(&self) -> Result<WorkingOrders, Error> {
		let endpoint = "/workingorders".to_string();
		let data: WorkingOrders = self.client.get_signed(&endpoint, 2, None::<()>)?;
		Ok(data)
	}

	/// POST /workingorders/otc
	/// Creates an OTC working order.
	pub fn create_working_order(&self, req: &CreateWorkingOrder) -> Result<DealRef, Error> {
		let endpoint = "/workingorders/otc".to_string();
		let data: DealRef = self.client.post_signed(&endpoint, 2, Some(req))?;
		Ok(data)
	}

	/// DELETE /workingorders/otc/{dealId}
	/// Deletes an OTC working order.
	pub fn delete_working_order(&self, deal_id: &String) -> Result<DealRef, Error> {
		let endpoint = format!("/workingorders/otc/{}", deal_id);
		let data: DealRef = self.client.delete_signed(&endpoint, 2, None::<()>)?;
		Ok(data)
	}

	/// PUT /workingorders/otc/{dealId}
	/// Updates an OTC working order.
	pub fn update_working_order(&self, deal_id: &String, req: &UpdateWorkingOrder) -> Result<DealRef, Error> {
		let endpoint = format!("/workingorders/otc/{}", deal_id);
		let data: DealRef = self.client.put_signed(&endpoint, 2, Some(req))?;
		Ok(data)
	}

	/// GET /operations/application
	/// Returns a list of client owned applications.
	pub fn get_applications(&self) -> Result<Vec<Application>, Error> {
		let endpoint = "/operations/application".to_string();
		let data: Vec<Application> = self.client.get_signed(&endpoint, 1, None::<()>)?;
		Ok(data)
	}

	/// PUT /operations/application
	/// Alters the details of a given user application.
	pub fn update_application(&self, req: &UpdateApplication) -> Result<Application, Error> {
		let endpoint = "/operations/application".to_string();
		let data: Application = self.client.put_signed(&endpoint, 1, Some(req))?;
		Ok(data)
	}

	/// GET /session
	/// Returns the user's session details and optionally tokens.
	pub fn get_session(&self) -> Result<Session, Error> {
		let endpoint = "/session".to_string();
		let data: Session = self.client.get_signed(&endpoint, 1, None::<()>)?;
		Ok(data)
	}

	/// GET /marketnavigation
	/// Returns all top-level nodes (market categories) in the market navigation hierarchy.
	pub fn get_market_categories(&self) -> Result<MarketCategory, Error> {
		let endpoint: String = "/marketnavigation".into();
		let data: MarketCategory = self.client.get_signed(&endpoint, 1, None::<()>)?;
		Ok(data)
	}

	/// GET /marketnavigation/{nodeId}
	/// Returns all sub-nodes of the given node in the market navigation hierarchy.
	pub fn get_market_category(&self, node_id: &String) -> Result<MarketCategory, Error> {
		let endpoint = format!("/marketnavigation/{}", node_id);
		let data: MarketCategory = self.client.get_signed(&endpoint, 1, None::<()>)?;
		Ok(data)
	}

	/// GET /markets
	/// Returns the details of the given markets.
	pub fn get_markets(&self, query: &MarketsQuery) -> Result<Markets, Error> {
		let endpoint: String = "/markets".into();
		let data: Markets = self.client.get_signed(&endpoint, 2, Some(query))?;
		Ok(data)
	}

	/// GET /markets/{epic}
	/// Returns the details of the given market.
	pub fn get_market(&self, epic: &String) -> Result<Market, Error> {
		let endpoint = format!("/markets/{}", epic);
		let data: Market = self.client.get_signed(&endpoint, 3, None::<()>)?;
		Ok(data)
	}

	/// GET /markets?searchTerm={searchTerm}
	/// Returns all markets matching the search term.
	pub fn search_markets(&self, search_term: &String) -> Result<MarketSearch, Error> {
		let endpoint = format!("/markets?searchTerm={}", search_term);
		let data: MarketSearch = self.client.get_signed(&endpoint, 1, None::<()>)?;
		Ok(data)
	}

	/// GET /prices/{epic}
	/// Returns historical prices for a particular instrument.
	/// By default returns the minute prices within the last 10 minutes.
	pub fn get_prices(&self, epic: &String, query: &PricesQuery) -> Result<Prices, Error> {
		let endpoint = format!("/prices/{}", epic);
		let data: Prices = self.client.get_signed(&endpoint, 3, Some(query))?;
		Ok(data)
	}

	/// GET /watchlists
	/// Returns all watchlists belonging to the active account
	pub fn get_watchlists(&self) -> Result<Watchlists, Error> {
		let endpoint = "/watchlists".to_string();
		let data: Watchlists = self.client.get_signed(&endpoint, 1, None::<()>)?;
		Ok(data)
	}

	/// POST /watchlists
	/// Creates a watchlist.
	pub fn create_watchlist(&self, req: &CreateWatchlist) -> Result<CreateWatchlistResult, Error> {
		let endpoint = "/watchlists".to_string();
		let data: CreateWatchlistResult = self.client.post_signed(&endpoint, 1, Some(req))?;
		Ok(data)
	}

	/// DELETE /watchlists/{watchlistId}
	/// Deletes a watchlist.
	pub fn delete_watchlist(&self, watchlist_id: &String) -> Result<OkResponse, Error> {
		let endpoint = format!("/watchlists/{}", watchlist_id);
		let data: OkResponse = self.client.delete_signed(&endpoint, 1, None::<()>)?;
		Ok(data)
	}

	/// GET /watchlists/{watchlistId}
	/// Returns a watchlist.
	pub fn get_watchlist(&self, watchlist_id: &String) -> Result<MarketSearch, Error> {
		let endpoint = format!("/watchlists/{}", watchlist_id);
		let data: MarketSearch = self.client.get_signed(&endpoint, 1, None::<()>)?;
		Ok(data)
	}

	/// PUT /watchlists/{watchlistId}
	/// Add a market to watchlist.
	pub fn add_market_to_watchlist(&self, watchlist_id: &String, req: &AddToWatchlist) -> Result<OkResponse, Error> {
		let endpoint = format!("/watchlists/{}", watchlist_id);
		let data: OkResponse = self.client.put_signed(&endpoint, 1, Some(req))?;
		Ok(data)
	}

	/// DELETE /watchlists/{watchlistId}/{epic}
	/// Remove a market from a watchlist.
	pub fn remove_market_from_watchlist(&self, watchlist_id: &String, epic: &String) -> Result<OkResponse, Error> {
		let endpoint = format!("/watchlists/{}/{}", watchlist_id, epic);
		let data: OkResponse = self.client.delete_signed(&endpoint, 1, None::<()>)?;
		Ok(data)
	}
}