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
579
580
581
582
583
584
585
586
//! Instrument metadata, candlestick, and order/position book models.

use serde::{Deserialize, Serialize};

use super::macros::string_enum;
use super::{DateTime, DecimalNumber, GuaranteedStopLossOrderMode, InstrumentName, PriceValue};

/// Full specification of an Instrument.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Instrument {
    /// The name of the Instrument
    #[serde(rename = "name", skip_serializing_if = "Option::is_none")]
    pub name: Option<InstrumentName>,

    /// The `type` field.
    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
    pub r#type: Option<InstrumentType>,

    /// The display name of the Instrument
    #[serde(rename = "displayName", skip_serializing_if = "Option::is_none")]
    pub display_name: Option<String>,

    /// The location of the "pip" for this instrument. The decimal position of
    /// the pip in this Instrument's price can be found at 10 ^ pipLocation
    /// (e.g. -4 pipLocation results in a decimal pip position of 10 ^ -4 =
    /// 0.0001).
    #[serde(rename = "pipLocation", skip_serializing_if = "Option::is_none")]
    pub pip_location: Option<i64>,

    /// The number of decimal places that should be used to display prices for
    /// this instrument. (e.g. a displayPrecision of 5 would result in a price
    /// of "1" being displayed as "1.00000")
    #[serde(rename = "displayPrecision", skip_serializing_if = "Option::is_none")]
    pub display_precision: Option<i64>,

    /// The amount of decimal places that may be provided when specifying the
    /// number of units traded for this instrument.
    #[serde(
        rename = "tradeUnitsPrecision",
        skip_serializing_if = "Option::is_none"
    )]
    pub trade_units_precision: Option<i64>,

    /// The smallest number of units allowed to be traded for this instrument.
    #[serde(rename = "minimumTradeSize", skip_serializing_if = "Option::is_none")]
    pub minimum_trade_size: Option<DecimalNumber>,

    /// The maximum trailing stop distance allowed for a trailing stop loss
    /// created for this instrument. Specified in price units.
    #[serde(
        rename = "maximumTrailingStopDistance",
        skip_serializing_if = "Option::is_none"
    )]
    pub maximum_trailing_stop_distance: Option<DecimalNumber>,

    /// The minimum trailing stop distance allowed for a trailing stop loss
    /// created for this instrument. Specified in price units.
    #[serde(
        rename = "minimumTrailingStopDistance",
        skip_serializing_if = "Option::is_none"
    )]
    pub minimum_trailing_stop_distance: Option<DecimalNumber>,

    /// The maximum position size allowed for this instrument. Specified in
    /// units.
    #[serde(
        rename = "maximumPositionSize",
        skip_serializing_if = "Option::is_none"
    )]
    pub maximum_position_size: Option<DecimalNumber>,

    /// The maximum units allowed for an Order placed for this instrument.
    /// Specified in units.
    #[serde(rename = "maximumOrderUnits", skip_serializing_if = "Option::is_none")]
    pub maximum_order_units: Option<DecimalNumber>,

    /// The margin rate for this instrument.
    #[serde(rename = "marginRate", skip_serializing_if = "Option::is_none")]
    pub margin_rate: Option<DecimalNumber>,

    /// The `commission` field.
    #[serde(rename = "commission", skip_serializing_if = "Option::is_none")]
    pub commission: Option<InstrumentCommission>,

    /// The `guaranteedStopLossOrderMode` field.
    #[serde(
        rename = "guaranteedStopLossOrderMode",
        skip_serializing_if = "Option::is_none"
    )]
    pub guaranteed_stop_loss_order_mode: Option<GuaranteedStopLossOrderMode>,

    /// The minimum distance allowed between the Trade's fill price and the
    /// configured price for guaranteed Stop Loss Orders created for this
    /// instrument. Specified in price units.
    #[serde(
        rename = "minimumGuaranteedStopLossDistance",
        skip_serializing_if = "Option::is_none"
    )]
    pub minimum_guaranteed_stop_loss_distance: Option<DecimalNumber>,

    /// The amount that is charged to the account if a guaranteed Stop Loss
    /// Order is triggered and filled. The value is in price units and is
    /// charged for each unit of the Trade.
    #[serde(
        rename = "guaranteedStopLossOrderExecutionPremium",
        skip_serializing_if = "Option::is_none"
    )]
    pub guaranteed_stop_loss_order_execution_premium: Option<DecimalNumber>,

    /// The `guaranteedStopLossOrderLevelRestriction` field.
    #[serde(
        rename = "guaranteedStopLossOrderLevelRestriction",
        skip_serializing_if = "Option::is_none"
    )]
    pub guaranteed_stop_loss_order_level_restriction:
        Option<GuaranteedStopLossOrderLevelRestriction>,

    /// The `financing` field.
    #[serde(rename = "financing", skip_serializing_if = "Option::is_none")]
    pub financing: Option<InstrumentFinancing>,

    /// The tags associated with this instrument.
    #[serde(rename = "tags", default, skip_serializing_if = "Vec::is_empty")]
    pub tags: Vec<Tag>,
}

/// An InstrumentCommission represents an instrument-specific commission
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct InstrumentCommission {
    /// The commission amount (in the Account's home currency) charged per
    /// unitsTraded of the instrument
    #[serde(rename = "commission", skip_serializing_if = "Option::is_none")]
    pub commission: Option<DecimalNumber>,

    /// The number of units traded that the commission amount is based on.
    #[serde(rename = "unitsTraded", skip_serializing_if = "Option::is_none")]
    pub units_traded: Option<DecimalNumber>,

    /// The minimum commission amount (in the Account's home currency) that is
    /// charged when an Order is filled for this instrument.
    #[serde(rename = "minimumCommission", skip_serializing_if = "Option::is_none")]
    pub minimum_commission: Option<DecimalNumber>,
}

/// Financing data for the instrument.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct InstrumentFinancing {
    /// The financing rate to be used for a long position for the instrument.
    #[serde(rename = "longRate", skip_serializing_if = "Option::is_none")]
    pub long_rate: Option<DecimalNumber>,

    /// The financing rate to be used for a short position for the instrument.
    #[serde(rename = "shortRate", skip_serializing_if = "Option::is_none")]
    pub short_rate: Option<DecimalNumber>,

    /// The days of the week to debit or credit financing charges; the exact
    /// time of day at which to charge the financing is set in the
    /// DivisionTradingGroup for the client's account.
    #[serde(
        rename = "financingDaysOfWeek",
        default,
        skip_serializing_if = "Vec::is_empty"
    )]
    pub financing_days_of_week: Vec<FinancingDayOfWeek>,
}

/// A FinancingDayOfWeek message defines a day of the week when financing
/// charges are debited or credited.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct FinancingDayOfWeek {
    /// The `dayOfWeek` field.
    #[serde(rename = "dayOfWeek", skip_serializing_if = "Option::is_none")]
    pub day_of_week: Option<DayOfWeek>,

    /// The number of days worth of financing to be charged on dayOfWeek.
    #[serde(rename = "daysCharged", skip_serializing_if = "Option::is_none")]
    pub days_charged: Option<i64>,
}

string_enum! {
    /// The DayOfWeek provides a representation of the day of the week.
    pub enum DayOfWeek {
        Sunday => "SUNDAY",
        Monday => "MONDAY",
        Tuesday => "TUESDAY",
        Wednesday => "WEDNESDAY",
        Thursday => "THURSDAY",
        Friday => "FRIDAY",
        Saturday => "SATURDAY",
    }
}

string_enum! {
    /// The type of an Instrument.
    pub enum InstrumentType {
        Currency => "CURRENCY",
        Cfd => "CFD",
        Metal => "METAL",
    }
}

/// A tag associated with an entity.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Tag {
    /// The type of the tag.
    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
    pub r#type: Option<String>,

    /// The name of the tag.
    #[serde(rename = "name", skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
}

/// A GuaranteedStopLossOrderLevelRestriction represents the total position size
/// that can exist within a given price window for Trades with guaranteed Stop
/// Loss Orders attached for a specific Instrument.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct GuaranteedStopLossOrderLevelRestriction {
    /// Applies to Trades with a guaranteed Stop Loss Order attached for the
    /// specified Instrument. This is the total allowed Trade volume that can
    /// exist within the priceRange based on the trigger prices of the
    /// guaranteed Stop Loss Orders.
    #[serde(rename = "volume", skip_serializing_if = "Option::is_none")]
    pub volume: Option<DecimalNumber>,

    /// The price range the volume applies to. This value is in price units.
    #[serde(rename = "priceRange", skip_serializing_if = "Option::is_none")]
    pub price_range: Option<DecimalNumber>,
}

string_enum! {
    /// The granularity of a candlestick
    pub enum CandlestickGranularity {
        S5 => "S5",
        S10 => "S10",
        S15 => "S15",
        S30 => "S30",
        M1 => "M1",
        M2 => "M2",
        M4 => "M4",
        M5 => "M5",
        M10 => "M10",
        M15 => "M15",
        M30 => "M30",
        H1 => "H1",
        H2 => "H2",
        H3 => "H3",
        H4 => "H4",
        H6 => "H6",
        H8 => "H8",
        H12 => "H12",
        D => "D",
        W => "W",
        M => "M",
    }
}

string_enum! {
    /// The day of the week to use for candlestick granularities with weekly
    /// alignment.
    pub enum WeeklyAlignment {
        Monday => "Monday",
        Tuesday => "Tuesday",
        Wednesday => "Wednesday",
        Thursday => "Thursday",
        Friday => "Friday",
        Saturday => "Saturday",
        Sunday => "Sunday",
    }
}

/// The price data (open, high, low, close) for the Candlestick representation.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct CandlestickData {
    /// The first (open) price in the time-range represented by the candlestick.
    #[serde(rename = "o", skip_serializing_if = "Option::is_none")]
    pub o: Option<PriceValue>,

    /// The highest price in the time-range represented by the candlestick.
    #[serde(rename = "h", skip_serializing_if = "Option::is_none")]
    pub h: Option<PriceValue>,

    /// The lowest price in the time-range represented by the candlestick.
    #[serde(rename = "l", skip_serializing_if = "Option::is_none")]
    pub l: Option<PriceValue>,

    /// The last (closing) price in the time-range represented by the
    /// candlestick.
    #[serde(rename = "c", skip_serializing_if = "Option::is_none")]
    pub c: Option<PriceValue>,
}

/// The Candlestick representation
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Candlestick {
    /// The start time of the candlestick
    #[serde(rename = "time", skip_serializing_if = "Option::is_none")]
    pub time: Option<DateTime>,

    /// The `bid` field.
    #[serde(rename = "bid", skip_serializing_if = "Option::is_none")]
    pub bid: Option<CandlestickData>,

    /// The `ask` field.
    #[serde(rename = "ask", skip_serializing_if = "Option::is_none")]
    pub ask: Option<CandlestickData>,

    /// The `mid` field.
    #[serde(rename = "mid", skip_serializing_if = "Option::is_none")]
    pub mid: Option<CandlestickData>,

    /// The number of prices created during the time-range represented by the
    /// candlestick.
    #[serde(rename = "volume", skip_serializing_if = "Option::is_none")]
    pub volume: Option<i64>,

    /// A flag indicating if the candlestick is complete. A complete candlestick
    /// is one whose ending time is not in the future.
    #[serde(rename = "complete", skip_serializing_if = "Option::is_none")]
    pub complete: Option<bool>,
}

/// The LatestCandles representation.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct InstrumentCandles {
    /// The instrument whose Prices are represented by the candlesticks.
    #[serde(rename = "instrument", skip_serializing_if = "Option::is_none")]
    pub instrument: Option<InstrumentName>,

    /// The `granularity` field.
    #[serde(rename = "granularity", skip_serializing_if = "Option::is_none")]
    pub granularity: Option<CandlestickGranularity>,

    /// The list of candlesticks that satisfy the request.
    #[serde(rename = "candles", default, skip_serializing_if = "Vec::is_empty")]
    pub candles: Vec<Candlestick>,
}

/// The representation of an instrument's order book at a point in time
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct OrderBook {
    /// The order book's instrument
    #[serde(rename = "instrument", skip_serializing_if = "Option::is_none")]
    pub instrument: Option<InstrumentName>,

    /// The time when the order book snapshot was created.
    #[serde(rename = "time", skip_serializing_if = "Option::is_none")]
    pub time: Option<DateTime>,

    /// The price (midpoint) for the order book's instrument at the time of the
    /// order book snapshot
    #[serde(rename = "price", skip_serializing_if = "Option::is_none")]
    pub price: Option<PriceValue>,

    /// The price width for each bucket. Each bucket covers the price range from
    /// the bucket's price to the bucket's price + bucketWidth.
    #[serde(rename = "bucketWidth", skip_serializing_if = "Option::is_none")]
    pub bucket_width: Option<PriceValue>,

    /// The partitioned order book, divided into buckets using a default bucket
    /// width. These buckets are only provided for price ranges which actually
    /// contain order or position data.
    #[serde(rename = "buckets", default, skip_serializing_if = "Vec::is_empty")]
    pub buckets: Vec<OrderBookBucket>,

    /// The book's creation time as a Unix timestamp (seconds), string-encoded.
    /// This field is returned by the live v20 API but is not present in OANDA's
    /// official documentation.
    #[serde(rename = "unixTime", skip_serializing_if = "Option::is_none")]
    pub unix_time: Option<DateTime>,
}

/// The order book data for a partition of the instrument's prices.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct OrderBookBucket {
    /// The lowest price (inclusive) covered by the bucket. The bucket covers
    /// the price range from the price to price + the order book's bucketWidth.
    #[serde(rename = "price", skip_serializing_if = "Option::is_none")]
    pub price: Option<PriceValue>,

    /// The percentage of the total number of orders represented by the long
    /// orders found in this bucket.
    #[serde(rename = "longCountPercent", skip_serializing_if = "Option::is_none")]
    pub long_count_percent: Option<DecimalNumber>,

    /// The percentage of the total number of orders represented by the short
    /// orders found in this bucket.
    #[serde(rename = "shortCountPercent", skip_serializing_if = "Option::is_none")]
    pub short_count_percent: Option<DecimalNumber>,
}

/// The representation of an instrument's position book at a point in time
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct PositionBook {
    /// The position book's instrument
    #[serde(rename = "instrument", skip_serializing_if = "Option::is_none")]
    pub instrument: Option<InstrumentName>,

    /// The time when the position book snapshot was created
    #[serde(rename = "time", skip_serializing_if = "Option::is_none")]
    pub time: Option<DateTime>,

    /// The price (midpoint) for the position book's instrument at the time of
    /// the position book snapshot
    #[serde(rename = "price", skip_serializing_if = "Option::is_none")]
    pub price: Option<PriceValue>,

    /// The price width for each bucket. Each bucket covers the price range from
    /// the bucket's price to the bucket's price + bucketWidth.
    #[serde(rename = "bucketWidth", skip_serializing_if = "Option::is_none")]
    pub bucket_width: Option<PriceValue>,

    /// The partitioned position book, divided into buckets using a default
    /// bucket width. These buckets are only provided for price ranges which
    /// actually contain order or position data.
    #[serde(rename = "buckets", default, skip_serializing_if = "Vec::is_empty")]
    pub buckets: Vec<PositionBookBucket>,

    /// The book's creation time as a Unix timestamp (seconds), string-encoded.
    /// This field is returned by the live v20 API but is not present in OANDA's
    /// official documentation.
    #[serde(rename = "unixTime", skip_serializing_if = "Option::is_none")]
    pub unix_time: Option<DateTime>,
}

/// The position book data for a partition of the instrument's prices.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct PositionBookBucket {
    /// The lowest price (inclusive) covered by the bucket. The bucket covers
    /// the price range from the price to price + the position book's
    /// bucketWidth.
    #[serde(rename = "price", skip_serializing_if = "Option::is_none")]
    pub price: Option<PriceValue>,

    /// The percentage of the total number of positions represented by the long
    /// positions found in this bucket.
    #[serde(rename = "longCountPercent", skip_serializing_if = "Option::is_none")]
    pub long_count_percent: Option<DecimalNumber>,

    /// The percentage of the total number of positions represented by the short
    /// positions found in this bucket.
    #[serde(rename = "shortCountPercent", skip_serializing_if = "Option::is_none")]
    pub short_count_percent: Option<DecimalNumber>,
}

/// The price component(s) a candlestick request applies to: any combination
/// of bid (`B`), mid (`M`) and ask (`A`).
///
/// ```
/// use oanda_rs::models::PricingComponent;
///
/// assert_eq!(PricingComponent::MID.to_string(), "M");
/// assert_eq!(PricingComponent::MID.with_bid().with_ask().to_string(), "BMA");
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct PricingComponent {
    /// Include bid candles.
    pub bid: bool,
    /// Include mid candles.
    pub mid: bool,
    /// Include ask candles.
    pub ask: bool,
}

impl PricingComponent {
    /// Bid candles only (`B`).
    pub const BID: PricingComponent = PricingComponent {
        bid: true,
        mid: false,
        ask: false,
    };
    /// Mid candles only (`M`) — OANDA's default.
    pub const MID: PricingComponent = PricingComponent {
        bid: false,
        mid: true,
        ask: false,
    };
    /// Ask candles only (`A`).
    pub const ASK: PricingComponent = PricingComponent {
        bid: false,
        mid: false,
        ask: true,
    };

    /// Adds the bid component.
    pub fn with_bid(mut self) -> Self {
        self.bid = true;
        self
    }

    /// Adds the mid component.
    pub fn with_mid(mut self) -> Self {
        self.mid = true;
        self
    }

    /// Adds the ask component.
    pub fn with_ask(mut self) -> Self {
        self.ask = true;
        self
    }
}

impl std::fmt::Display for PricingComponent {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.bid {
            f.write_str("B")?;
        }
        if self.mid {
            f.write_str("M")?;
        }
        if self.ask {
            f.write_str("A")?;
        }
        Ok(())
    }
}

impl std::str::FromStr for PricingComponent {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut component = PricingComponent::default();
        for c in s.chars() {
            match c.to_ascii_uppercase() {
                'B' => component.bid = true,
                'M' => component.mid = true,
                'A' => component.ask = true,
                other => return Err(format!("invalid pricing component character: {other:?}")),
            }
        }
        Ok(component)
    }
}

/// An instrument name, a granularity, and a price component to get
/// candlestick data for, e.g. `EUR_USD:S10:BM`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CandleSpecification {
    /// The instrument to get candles for.
    pub instrument: InstrumentName,
    /// The candlestick granularity.
    pub granularity: CandlestickGranularity,
    /// The price component(s); OANDA defaults to mid when omitted.
    pub price: Option<PricingComponent>,
}

impl CandleSpecification {
    /// A specification for the given instrument and granularity (mid price).
    pub fn new(instrument: impl Into<InstrumentName>, granularity: CandlestickGranularity) -> Self {
        CandleSpecification {
            instrument: instrument.into(),
            granularity,
            price: None,
        }
    }

    /// Selects the price component(s) for this specification.
    pub fn price(mut self, price: PricingComponent) -> Self {
        self.price = Some(price);
        self
    }
}

impl std::fmt::Display for CandleSpecification {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}:{}", self.instrument, self.granularity)?;
        if let Some(price) = &self.price {
            write!(f, ":{price}")?;
        }
        Ok(())
    }
}