oanda-rs 0.1.0

Async Rust SDK for the OANDA v20 REST and streaming API
Documentation
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
//! Trade endpoints: listing, closing, and modifying open trades.

use serde::{Deserialize, Serialize};

use crate::client::Client;
use crate::error::Error;
use crate::models::transaction::{
    MarketOrderRejectTransaction, MarketOrderTransaction, OrderCancelRejectTransaction,
    OrderCancelTransaction, OrderFillTransaction, StopLossOrderRejectTransaction,
    StopLossOrderTransaction, TakeProfitOrderRejectTransaction, TakeProfitOrderTransaction,
    TradeClientExtensionsModifyRejectTransaction, TradeClientExtensionsModifyTransaction,
    TrailingStopLossOrderRejectTransaction, TrailingStopLossOrderTransaction,
};
use crate::models::{
    AccountId, ClientExtensions, InstrumentName, StopLossDetails, TakeProfitDetails, Trade,
    TradeId, TradeSpecifier, TradeStateFilter, TrailingStopLossDetails, TransactionId,
};

impl Client {
    /// Get a list of trades for an account.
    ///
    /// `GET /v3/accounts/{accountID}/trades`
    pub fn list_trades(&self, account_id: impl Into<AccountId>) -> ListTradesRequest {
        ListTradesRequest {
            client: self.clone(),
            account_id: account_id.into(),
            params: Vec::new(),
        }
    }

    /// Get the list of open trades for an account.
    ///
    /// `GET /v3/accounts/{accountID}/openTrades`
    pub async fn list_open_trades(
        &self,
        account_id: impl Into<AccountId>,
    ) -> Result<ListTradesResponse, Error> {
        let account_id = account_id.into();
        self.execute(self.get(&["accounts", account_id.as_str(), "openTrades"]))
            .await
    }

    /// Get the details of a specific trade in an account.
    ///
    /// `GET /v3/accounts/{accountID}/trades/{tradeSpecifier}`
    pub async fn trade(
        &self,
        account_id: impl Into<AccountId>,
        trade: impl Into<TradeSpecifier>,
    ) -> Result<TradeResponse, Error> {
        let account_id = account_id.into();
        let trade = trade.into();
        self.execute(self.get(&["accounts", account_id.as_str(), "trades", trade.as_str()]))
            .await
    }

    /// Close (partially or fully) a specific open trade in an account.
    ///
    /// `PUT /v3/accounts/{accountID}/trades/{tradeSpecifier}/close`
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn run() -> Result<(), oanda_rs::Error> {
    /// # let client = oanda_rs::Client::new(oanda_rs::Environment::Practice, "token");
    /// // Close a trade fully (the default) ...
    /// client.close_trade("101-004-1234567-001", "6543").send().await?;
    /// // ... or partially:
    /// client
    ///     .close_trade("101-004-1234567-001", "6543")
    ///     .units("50")
    ///     .send()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn close_trade(
        &self,
        account_id: impl Into<AccountId>,
        trade: impl Into<TradeSpecifier>,
    ) -> CloseTradeRequest {
        CloseTradeRequest {
            client: self.clone(),
            account_id: account_id.into(),
            trade: trade.into(),
            units: None,
        }
    }

    /// Update the client extensions for a trade.
    ///
    /// **Do not add, update, or delete the client extensions if your
    /// account is associated with MT4.**
    ///
    /// `PUT /v3/accounts/{accountID}/trades/{tradeSpecifier}/clientExtensions`
    pub async fn set_trade_client_extensions(
        &self,
        account_id: impl Into<AccountId>,
        trade: impl Into<TradeSpecifier>,
        extensions: ClientExtensions,
    ) -> Result<SetTradeClientExtensionsResponse, Error> {
        let account_id = account_id.into();
        let trade = trade.into();
        let request = self
            .put(&[
                "accounts",
                account_id.as_str(),
                "trades",
                trade.as_str(),
                "clientExtensions",
            ])
            .json(&SetTradeClientExtensionsBody {
                client_extensions: extensions,
            });
        self.execute(request).await
    }

    /// Create, replace and cancel the dependent orders (take-profit,
    /// stop-loss and trailing stop-loss) of a trade.
    ///
    /// Setting a detail replaces the existing dependent order (or creates
    /// one); setting it to "cancel" removes it. Details not set are left
    /// unmodified.
    ///
    /// `PUT /v3/accounts/{accountID}/trades/{tradeSpecifier}/orders`
    pub fn set_trade_dependent_orders(
        &self,
        account_id: impl Into<AccountId>,
        trade: impl Into<TradeSpecifier>,
    ) -> SetTradeDependentOrdersRequest {
        SetTradeDependentOrdersRequest {
            client: self.clone(),
            account_id: account_id.into(),
            trade: trade.into(),
            body: SetTradeDependentOrdersBody {
                take_profit: None,
                stop_loss: None,
                trailing_stop_loss: None,
            },
        }
    }
}

/// Builder for [`Client::list_trades`].
#[derive(Debug)]
pub struct ListTradesRequest {
    client: Client,
    account_id: AccountId,
    params: Vec<(&'static str, String)>,
}

impl ListTradesRequest {
    /// Restricts the response to the given trade IDs.
    pub fn ids<I>(mut self, ids: I) -> Self
    where
        I: IntoIterator,
        I::Item: Into<TradeId>,
    {
        let joined = ids
            .into_iter()
            .map(|id| id.into().0)
            .collect::<Vec<_>>()
            .join(",");
        self.params.push(("ids", joined));
        self
    }

    /// The state to filter the requested trades by (default `OPEN`).
    pub fn state(mut self, state: TradeStateFilter) -> Self {
        self.params.push(("state", state.to_string()));
        self
    }

    /// The instrument to filter the requested trades by.
    pub fn instrument(mut self, instrument: impl Into<InstrumentName>) -> Self {
        self.params
            .push(("instrument", instrument.into().as_str().to_owned()));
        self
    }

    /// The maximum number of trades to return (default 50, maximum 500).
    pub fn count(mut self, count: u32) -> Self {
        self.params.push(("count", count.to_string()));
        self
    }

    /// The maximum trade ID to return: only trades with IDs at or below
    /// this are returned.
    pub fn before_id(mut self, before_id: impl Into<TradeId>) -> Self {
        self.params.push(("beforeID", before_id.into().0));
        self
    }

    /// Performs the request.
    pub async fn send(self) -> Result<ListTradesResponse, Error> {
        let request = self
            .client
            .get(&["accounts", self.account_id.as_str(), "trades"])
            .query(&self.params);
        self.client.execute(request).await
    }
}

/// Response of [`Client::list_trades`] and [`Client::list_open_trades`].
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ListTradesResponse {
    /// The list of trades satisfying the request.
    #[serde(rename = "trades", default, skip_serializing_if = "Vec::is_empty")]
    pub trades: Vec<Trade>,
    /// The ID of the most recent transaction created for the account.
    #[serde(rename = "lastTransactionID", skip_serializing_if = "Option::is_none")]
    pub last_transaction_id: Option<TransactionId>,
}

/// Response of [`Client::trade`].
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct TradeResponse {
    /// The details of the requested trade.
    #[serde(rename = "trade")]
    pub trade: Trade,
    /// The ID of the most recent transaction created for the account.
    #[serde(rename = "lastTransactionID", skip_serializing_if = "Option::is_none")]
    pub last_transaction_id: Option<TransactionId>,
}

/// Builder for [`Client::close_trade`].
#[derive(Debug)]
pub struct CloseTradeRequest {
    client: Client,
    account_id: AccountId,
    trade: TradeSpecifier,
    units: Option<String>,
}

#[derive(Debug, Serialize)]
struct CloseTradeBody {
    #[serde(skip_serializing_if = "Option::is_none")]
    units: Option<String>,
}

impl CloseTradeRequest {
    /// How much of the trade to close: either `"ALL"` (the default) or a
    /// number of units as a decimal string (e.g. `"50"`).
    pub fn units(mut self, units: impl Into<String>) -> Self {
        self.units = Some(units.into());
        self
    }

    /// Performs the request.
    pub async fn send(self) -> Result<CloseTradeResponse, Error> {
        let request = self
            .client
            .put(&[
                "accounts",
                self.account_id.as_str(),
                "trades",
                self.trade.as_str(),
                "close",
            ])
            .json(&CloseTradeBody { units: self.units });
        self.client.execute(request).await
    }
}

/// Response of [`Client::close_trade`].
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct CloseTradeResponse {
    /// The market order created to close the trade.
    #[serde(
        rename = "orderCreateTransaction",
        skip_serializing_if = "Option::is_none"
    )]
    pub order_create_transaction: Option<MarketOrderTransaction>,
    /// The fill of the close-trade market order.
    #[serde(
        rename = "orderFillTransaction",
        skip_serializing_if = "Option::is_none"
    )]
    pub order_fill_transaction: Option<OrderFillTransaction>,
    /// The cancellation of the close-trade market order (when it could not
    /// be filled).
    #[serde(
        rename = "orderCancelTransaction",
        skip_serializing_if = "Option::is_none"
    )]
    pub order_cancel_transaction: Option<OrderCancelTransaction>,
    /// The IDs of all transactions created while satisfying the request.
    #[serde(
        rename = "relatedTransactionIDs",
        default,
        skip_serializing_if = "Vec::is_empty"
    )]
    pub related_transaction_ids: Vec<TransactionId>,
    /// The ID of the most recent transaction created for the account.
    #[serde(rename = "lastTransactionID", skip_serializing_if = "Option::is_none")]
    pub last_transaction_id: Option<TransactionId>,
}

/// Typed view of the error body when [`Client::close_trade`] is rejected
/// (HTTP 400/404). Recover it via
/// [`ApiErrorBody::details`](crate::ApiErrorBody::details).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct CloseTradeRejectBody {
    /// The market order reject transaction.
    #[serde(
        rename = "orderRejectTransaction",
        skip_serializing_if = "Option::is_none"
    )]
    pub order_reject_transaction: Option<MarketOrderRejectTransaction>,
    /// The IDs of all transactions created while satisfying the request.
    #[serde(
        rename = "relatedTransactionIDs",
        default,
        skip_serializing_if = "Vec::is_empty"
    )]
    pub related_transaction_ids: Vec<TransactionId>,
    /// The ID of the most recent transaction created for the account.
    #[serde(rename = "lastTransactionID", skip_serializing_if = "Option::is_none")]
    pub last_transaction_id: Option<TransactionId>,
}

#[derive(Debug, Serialize)]
struct SetTradeClientExtensionsBody {
    #[serde(rename = "clientExtensions")]
    client_extensions: ClientExtensions,
}

/// Response of [`Client::set_trade_client_extensions`].
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct SetTradeClientExtensionsResponse {
    /// The transaction that updated the trade's client extensions.
    #[serde(
        rename = "tradeClientExtensionsModifyTransaction",
        skip_serializing_if = "Option::is_none"
    )]
    pub trade_client_extensions_modify_transaction: Option<TradeClientExtensionsModifyTransaction>,
    /// The IDs of all transactions created while satisfying the request.
    #[serde(
        rename = "relatedTransactionIDs",
        default,
        skip_serializing_if = "Vec::is_empty"
    )]
    pub related_transaction_ids: Vec<TransactionId>,
    /// The ID of the most recent transaction created for the account.
    #[serde(rename = "lastTransactionID", skip_serializing_if = "Option::is_none")]
    pub last_transaction_id: Option<TransactionId>,
}

/// Typed view of the error body when
/// [`Client::set_trade_client_extensions`] is rejected (HTTP 400/404).
/// Recover it via [`ApiErrorBody::details`](crate::ApiErrorBody::details).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct SetTradeClientExtensionsRejectBody {
    /// The transaction that rejected the modification.
    #[serde(
        rename = "tradeClientExtensionsModifyRejectTransaction",
        skip_serializing_if = "Option::is_none"
    )]
    pub trade_client_extensions_modify_reject_transaction:
        Option<TradeClientExtensionsModifyRejectTransaction>,
    /// The IDs of all transactions created while satisfying the request.
    #[serde(
        rename = "relatedTransactionIDs",
        default,
        skip_serializing_if = "Vec::is_empty"
    )]
    pub related_transaction_ids: Vec<TransactionId>,
    /// The ID of the most recent transaction created for the account.
    #[serde(rename = "lastTransactionID", skip_serializing_if = "Option::is_none")]
    pub last_transaction_id: Option<TransactionId>,
}

/// Builder for [`Client::set_trade_dependent_orders`].
#[derive(Debug)]
pub struct SetTradeDependentOrdersRequest {
    client: Client,
    account_id: AccountId,
    trade: TradeSpecifier,
    body: SetTradeDependentOrdersBody,
}

#[derive(Debug, Serialize)]
struct SetTradeDependentOrdersBody {
    #[serde(rename = "takeProfit", skip_serializing_if = "Option::is_none")]
    take_profit: Option<Option<TakeProfitDetails>>,
    #[serde(rename = "stopLoss", skip_serializing_if = "Option::is_none")]
    stop_loss: Option<Option<StopLossDetails>>,
    #[serde(rename = "trailingStopLoss", skip_serializing_if = "Option::is_none")]
    trailing_stop_loss: Option<Option<TrailingStopLossDetails>>,
}

impl SetTradeDependentOrdersRequest {
    /// Creates or replaces the trade's take-profit order.
    pub fn take_profit(mut self, details: TakeProfitDetails) -> Self {
        self.body.take_profit = Some(Some(details));
        self
    }

    /// Cancels the trade's take-profit order (sends `null`).
    pub fn cancel_take_profit(mut self) -> Self {
        self.body.take_profit = Some(None);
        self
    }

    /// Creates or replaces the trade's stop-loss order.
    pub fn stop_loss(mut self, details: StopLossDetails) -> Self {
        self.body.stop_loss = Some(Some(details));
        self
    }

    /// Cancels the trade's stop-loss order (sends `null`).
    pub fn cancel_stop_loss(mut self) -> Self {
        self.body.stop_loss = Some(None);
        self
    }

    /// Creates or replaces the trade's trailing stop-loss order.
    pub fn trailing_stop_loss(mut self, details: TrailingStopLossDetails) -> Self {
        self.body.trailing_stop_loss = Some(Some(details));
        self
    }

    /// Cancels the trade's trailing stop-loss order (sends `null`).
    pub fn cancel_trailing_stop_loss(mut self) -> Self {
        self.body.trailing_stop_loss = Some(None);
        self
    }

    /// Performs the request.
    pub async fn send(self) -> Result<SetTradeDependentOrdersResponse, Error> {
        let request = self
            .client
            .put(&[
                "accounts",
                self.account_id.as_str(),
                "trades",
                self.trade.as_str(),
                "orders",
            ])
            .json(&self.body);
        self.client.execute(request).await
    }
}

/// Response of [`Client::set_trade_dependent_orders`].
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct SetTradeDependentOrdersResponse {
    /// The transaction cancelling the trade's previous take-profit order.
    #[serde(
        rename = "takeProfitOrderCancelTransaction",
        skip_serializing_if = "Option::is_none"
    )]
    pub take_profit_order_cancel_transaction: Option<OrderCancelTransaction>,
    /// The transaction creating the trade's new take-profit order.
    #[serde(
        rename = "takeProfitOrderTransaction",
        skip_serializing_if = "Option::is_none"
    )]
    pub take_profit_order_transaction: Option<TakeProfitOrderTransaction>,
    /// The fill of the new take-profit order (only when it was immediately
    /// filled).
    #[serde(
        rename = "takeProfitOrderFillTransaction",
        skip_serializing_if = "Option::is_none"
    )]
    pub take_profit_order_fill_transaction: Option<OrderFillTransaction>,
    /// The cancellation of the new take-profit order (only when it was
    /// immediately cancelled).
    #[serde(
        rename = "takeProfitOrderCreatedCancelTransaction",
        skip_serializing_if = "Option::is_none"
    )]
    pub take_profit_order_created_cancel_transaction: Option<OrderCancelTransaction>,
    /// The transaction cancelling the trade's previous stop-loss order.
    #[serde(
        rename = "stopLossOrderCancelTransaction",
        skip_serializing_if = "Option::is_none"
    )]
    pub stop_loss_order_cancel_transaction: Option<OrderCancelTransaction>,
    /// The transaction creating the trade's new stop-loss order.
    #[serde(
        rename = "stopLossOrderTransaction",
        skip_serializing_if = "Option::is_none"
    )]
    pub stop_loss_order_transaction: Option<StopLossOrderTransaction>,
    /// The fill of the new stop-loss order (only when it was immediately
    /// filled).
    #[serde(
        rename = "stopLossOrderFillTransaction",
        skip_serializing_if = "Option::is_none"
    )]
    pub stop_loss_order_fill_transaction: Option<OrderFillTransaction>,
    /// The cancellation of the new stop-loss order (only when it was
    /// immediately cancelled).
    #[serde(
        rename = "stopLossOrderCreatedCancelTransaction",
        skip_serializing_if = "Option::is_none"
    )]
    pub stop_loss_order_created_cancel_transaction: Option<OrderCancelTransaction>,
    /// The transaction cancelling the trade's previous trailing stop-loss
    /// order.
    #[serde(
        rename = "trailingStopLossOrderCancelTransaction",
        skip_serializing_if = "Option::is_none"
    )]
    pub trailing_stop_loss_order_cancel_transaction: Option<OrderCancelTransaction>,
    /// The transaction creating the trade's new trailing stop-loss order.
    #[serde(
        rename = "trailingStopLossOrderTransaction",
        skip_serializing_if = "Option::is_none"
    )]
    pub trailing_stop_loss_order_transaction: Option<TrailingStopLossOrderTransaction>,
    /// The IDs of all transactions created while satisfying the request.
    #[serde(
        rename = "relatedTransactionIDs",
        default,
        skip_serializing_if = "Vec::is_empty"
    )]
    pub related_transaction_ids: Vec<TransactionId>,
    /// The ID of the most recent transaction created for the account.
    #[serde(rename = "lastTransactionID", skip_serializing_if = "Option::is_none")]
    pub last_transaction_id: Option<TransactionId>,
}

/// Typed view of the error body when
/// [`Client::set_trade_dependent_orders`] is rejected (HTTP 400). Recover
/// it via [`ApiErrorBody::details`](crate::ApiErrorBody::details).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct SetTradeDependentOrdersRejectBody {
    /// The rejection of the cancellation of the trade's take-profit order.
    #[serde(
        rename = "takeProfitOrderCancelRejectTransaction",
        skip_serializing_if = "Option::is_none"
    )]
    pub take_profit_order_cancel_reject_transaction: Option<OrderCancelRejectTransaction>,
    /// The rejection of the creation of a new take-profit order.
    #[serde(
        rename = "takeProfitOrderRejectTransaction",
        skip_serializing_if = "Option::is_none"
    )]
    pub take_profit_order_reject_transaction: Option<TakeProfitOrderRejectTransaction>,
    /// The rejection of the cancellation of the trade's stop-loss order.
    #[serde(
        rename = "stopLossOrderCancelRejectTransaction",
        skip_serializing_if = "Option::is_none"
    )]
    pub stop_loss_order_cancel_reject_transaction: Option<OrderCancelRejectTransaction>,
    /// The rejection of the creation of a new stop-loss order.
    #[serde(
        rename = "stopLossOrderRejectTransaction",
        skip_serializing_if = "Option::is_none"
    )]
    pub stop_loss_order_reject_transaction: Option<StopLossOrderRejectTransaction>,
    /// The rejection of the cancellation of the trade's trailing stop-loss
    /// order.
    #[serde(
        rename = "trailingStopLossOrderCancelRejectTransaction",
        skip_serializing_if = "Option::is_none"
    )]
    pub trailing_stop_loss_order_cancel_reject_transaction: Option<OrderCancelRejectTransaction>,
    /// The rejection of the creation of a new trailing stop-loss order.
    #[serde(
        rename = "trailingStopLossOrderRejectTransaction",
        skip_serializing_if = "Option::is_none"
    )]
    pub trailing_stop_loss_order_reject_transaction: Option<TrailingStopLossOrderRejectTransaction>,
    /// The ID of the most recent transaction created for the account.
    #[serde(rename = "lastTransactionID", skip_serializing_if = "Option::is_none")]
    pub last_transaction_id: Option<TransactionId>,
}