oanda_client/models/instrument.rs
1//! OANDA v20 instrument definitions.
2use crate::ids::InstrumentName;
3use crate::timestamp::Timestamp;
4use rust_decimal::Decimal;
5use serde::{Deserialize, Serialize};
6
7provider_enum! {
8 /// Unrecognized spellings are retained and serialized unchanged for provider
9 /// compatibility; endpoint admission applies any operation-specific restrictions.
10 /// The granularity of a candlestick
11 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
12 #[non_exhaustive]
13 pub enum CandlestickGranularity {
14 /// 5 second candlesticks, minute alignment
15 S5 => "S5",
16 /// 10 second candlesticks, minute alignment
17 S10 => "S10",
18 /// 15 second candlesticks, minute alignment
19 S15 => "S15",
20 /// 30 second candlesticks, minute alignment
21 S30 => "S30",
22 /// 1 minute candlesticks, minute alignment
23 M1 => "M1",
24 /// 2 minute candlesticks, hour alignment
25 M2 => "M2",
26 /// 4 minute candlesticks, hour alignment
27 M4 => "M4",
28 /// 5 minute candlesticks, hour alignment
29 M5 => "M5",
30 /// 10 minute candlesticks, hour alignment
31 M10 => "M10",
32 /// 15 minute candlesticks, hour alignment
33 M15 => "M15",
34 /// 30 minute candlesticks, hour alignment
35 M30 => "M30",
36 /// 1 hour candlesticks, hour alignment
37 H1 => "H1",
38 /// 2 hour candlesticks, day alignment
39 H2 => "H2",
40 /// 3 hour candlesticks, day alignment
41 H3 => "H3",
42 /// 4 hour candlesticks, day alignment
43 H4 => "H4",
44 /// 6 hour candlesticks, day alignment
45 H6 => "H6",
46 /// 8 hour candlesticks, day alignment
47 H8 => "H8",
48 /// 12 hour candlesticks, day alignment
49 H12 => "H12",
50 /// 1 day candlesticks, day alignment
51 D => "D",
52 /// 1 week candlesticks, aligned to start of week
53 W => "W",
54 /// 1 month candlesticks, aligned to first day of the month
55 M => "M",
56 ;
57 /// An unrecognized provider value, preserved for forward compatibility.
58 Unknown(String),
59 }
60}
61
62provider_enum! {
63 /// Unrecognized spellings are retained and serialized unchanged for provider
64 /// compatibility; endpoint admission applies any operation-specific restrictions.
65 /// The day of the week to use for candlestick granularities with weekly alignment.
66 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
67 #[non_exhaustive]
68 pub enum WeeklyAlignment {
69 /// Monday
70 Monday => "Monday",
71 /// Tuesday
72 Tuesday => "Tuesday",
73 /// Wednesday
74 Wednesday => "Wednesday",
75 /// Thursday
76 Thursday => "Thursday",
77 /// Friday
78 Friday => "Friday",
79 /// Saturday
80 Saturday => "Saturday",
81 /// Sunday
82 Sunday => "Sunday",
83 ;
84 /// An unrecognized provider value, preserved for forward compatibility.
85 Unknown(String),
86 }
87}
88
89/// The Candlestick representation
90#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
91#[serde(rename_all = "camelCase")]
92pub struct Candlestick {
93 /// The start time of the candlestick
94 #[serde(rename = "time", default, skip_serializing_if = "Option::is_none")]
95 pub time: Option<Timestamp>,
96 /// The candlestick data based on bids. Only provided if bid-based candles were requested.
97 #[serde(rename = "bid", default, skip_serializing_if = "Option::is_none")]
98 pub bid: Option<CandlestickData>,
99 /// The candlestick data based on asks. Only provided if ask-based candles were requested.
100 #[serde(rename = "ask", default, skip_serializing_if = "Option::is_none")]
101 pub ask: Option<CandlestickData>,
102 /// The candlestick data based on midpoints. Only provided if midpoint-based candles were requested.
103 #[serde(rename = "mid", default, skip_serializing_if = "Option::is_none")]
104 pub mid: Option<CandlestickData>,
105 /// The number of prices created during the time-range represented by the candlestick.
106 #[serde(rename = "volume", default, skip_serializing_if = "Option::is_none")]
107 pub volume: Option<i64>,
108 /// A flag indicating if the candlestick is complete. A complete candlestick is one whose ending time is not
109 /// in the future.
110 #[serde(rename = "complete", default, skip_serializing_if = "Option::is_none")]
111 pub complete: Option<bool>,
112}
113
114/// The price data (open, high, low, close) for the Candlestick representation.
115#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
116#[serde(rename_all = "camelCase")]
117pub struct CandlestickData {
118 /// The first (open) price in the time-range represented by the candlestick.
119 #[serde(rename = "o", default, skip_serializing_if = "Option::is_none")]
120 #[serde(deserialize_with = "crate::decimal_wire::optional_number_or_string")]
121 pub o: Option<Decimal>,
122 /// The highest price in the time-range represented by the candlestick.
123 #[serde(rename = "h", default, skip_serializing_if = "Option::is_none")]
124 #[serde(deserialize_with = "crate::decimal_wire::optional_number_or_string")]
125 pub h: Option<Decimal>,
126 /// The lowest price in the time-range represented by the candlestick.
127 #[serde(rename = "l", default, skip_serializing_if = "Option::is_none")]
128 #[serde(deserialize_with = "crate::decimal_wire::optional_number_or_string")]
129 pub l: Option<Decimal>,
130 /// The last (closing) price in the time-range represented by the candlestick.
131 #[serde(rename = "c", default, skip_serializing_if = "Option::is_none")]
132 #[serde(deserialize_with = "crate::decimal_wire::optional_number_or_string")]
133 pub c: Option<Decimal>,
134}
135
136/// Response containing instrument, granularity, and list of candles.
137#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
138#[serde(rename_all = "camelCase")]
139pub struct CandlestickResponse {
140 /// The instrument whose Prices are represented by the candlesticks.
141 #[serde(
142 rename = "instrument",
143 default,
144 skip_serializing_if = "Option::is_none"
145 )]
146 pub instrument: Option<InstrumentName>,
147 /// The granularity of the candlesticks provided.
148 #[serde(
149 rename = "granularity",
150 default,
151 skip_serializing_if = "Option::is_none"
152 )]
153 pub granularity: Option<CandlestickGranularity>,
154 /// The list of candlesticks that satisfy the request.
155 #[serde(rename = "candles", default, skip_serializing_if = "Option::is_none")]
156 pub candles: Option<Vec<Candlestick>>,
157}