ctrader-connector 0.3.10

Client library for the cTrader Open API and the cTrader FIX 4.4 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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
//! Trendbars (candles) of a symbol: their history and the bar that is forming now.
//!
//! ```no_run
//! use std::time::Duration;
//!
//! use ctrader_connector::common::config::ConfigurationOpenApi;
//! use ctrader_connector::common::ids::SymbolId;
//! use ctrader_connector::common::time::{TimeRange, Timestamp};
//! use ctrader_connector::open_api::client::OpenApiClient;
//! use ctrader_connector::open_api::trendbars::{Period, TrendbarsParams};
//!
//! # async fn run() -> Result<(), ctrader_connector::common::errors::ConnectorError> {
//! let client = OpenApiClient::connect(ConfigurationOpenApi::from_env()?).await?;
//! client.authorise_application().await?;
//! let account = client.accounts().await?[0].id;
//! client.authorise_account(account).await?;
//! let symbol = SymbolId::new(1)?;
//! let day = TimeRange::ending_at(Timestamp::now(), Duration::from_secs(86_400));
//! let params = TrendbarsParams::new(account, symbol, Period::H1, day).count(10);
//! for bar in client.trendbars(&params).await? {
//!     println!("{} {} {} {} {}", bar.timestamp, bar.open, bar.high, bar.low, bar.close);
//! }
//! let mut live = client.subscribe_live_trendbars(account, symbol, Period::M1).await?;
//! println!("{:?}", live.next().await?);
//! live.unsubscribe().await?;
//! # Ok(())
//! # }
//! ```

use std::collections::BTreeMap;
use std::fmt;
use std::time::Duration;

use crate::common::decimals::Price;
use crate::common::errors::{ConnectorError, ValueError};
use crate::common::ids::{AccountId, SymbolId};
use crate::common::time::{TimeRange, Timestamp};
use crate::open_api::client::OpenApiClient;
use crate::open_api::connection::{ReplayKey, ReplayStage};
use crate::open_api::messages::{
    ProtoOaGetTrendbarsReq, ProtoOaSpotEvent, ProtoOaSubscribeLiveTrendbarReq, ProtoOaTrendbar,
    ProtoOaTrendbarPeriod, ProtoOaUnsubscribeLiveTrendbarReq,
};
use crate::open_api::quotes::wire_price;
use crate::open_api::stream::{Feed, starts_replay};

const MINUTE: u64 = 60;
const HOUR: u64 = 60 * MINUTE;
const DAY: u64 = 24 * HOUR;
const WEEK: u64 = 7 * DAY;

/// Length of time one trendbar covers.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Period {
    /// One minute.
    M1,
    /// Two minutes.
    M2,
    /// Three minutes.
    M3,
    /// Four minutes.
    M4,
    /// Five minutes.
    M5,
    /// Ten minutes.
    M10,
    /// Fifteen minutes.
    M15,
    /// Thirty minutes.
    M30,
    /// One hour.
    H1,
    /// Four hours.
    H4,
    /// Twelve hours.
    H12,
    /// One day.
    D1,
    /// One week.
    W1,
    /// One month.
    Mn1,
}

impl Period {
    /// Every period, shortest first.
    pub const ALL: [Self; 14] = [
        Self::M1,
        Self::M2,
        Self::M3,
        Self::M4,
        Self::M5,
        Self::M10,
        Self::M15,
        Self::M30,
        Self::H1,
        Self::H4,
        Self::H12,
        Self::D1,
        Self::W1,
        Self::Mn1,
    ];

    const fn wire(self) -> ProtoOaTrendbarPeriod {
        match self {
            Self::M1 => ProtoOaTrendbarPeriod::M1,
            Self::M2 => ProtoOaTrendbarPeriod::M2,
            Self::M3 => ProtoOaTrendbarPeriod::M3,
            Self::M4 => ProtoOaTrendbarPeriod::M4,
            Self::M5 => ProtoOaTrendbarPeriod::M5,
            Self::M10 => ProtoOaTrendbarPeriod::M10,
            Self::M15 => ProtoOaTrendbarPeriod::M15,
            Self::M30 => ProtoOaTrendbarPeriod::M30,
            Self::H1 => ProtoOaTrendbarPeriod::H1,
            Self::H4 => ProtoOaTrendbarPeriod::H4,
            Self::H12 => ProtoOaTrendbarPeriod::H12,
            Self::D1 => ProtoOaTrendbarPeriod::D1,
            Self::W1 => ProtoOaTrendbarPeriod::W1,
            Self::Mn1 => ProtoOaTrendbarPeriod::Mn1,
        }
    }

    /// Returns the nominal length of one bar; a month counts thirty days.
    #[must_use]
    pub const fn duration(self) -> Duration {
        Duration::from_secs(match self {
            Self::M1 => MINUTE,
            Self::M2 => 2 * MINUTE,
            Self::M3 => 3 * MINUTE,
            Self::M4 => 4 * MINUTE,
            Self::M5 => 5 * MINUTE,
            Self::M10 => 10 * MINUTE,
            Self::M15 => 15 * MINUTE,
            Self::M30 => 30 * MINUTE,
            Self::H1 => HOUR,
            Self::H4 => 4 * HOUR,
            Self::H12 => 12 * HOUR,
            Self::D1 => DAY,
            Self::W1 => WEEK,
            Self::Mn1 => 30 * DAY,
        })
    }

    /// Returns the longest span of time one request for bars of the period may cover.
    ///
    /// [`OpenApiClient::trendbars`] splits a longer range into requests of at most this span.
    #[must_use]
    pub const fn max_range(self) -> Duration {
        Duration::from_secs(match self {
            Self::M1 | Self::M2 | Self::M3 | Self::M4 | Self::M5 => 5 * WEEK,
            Self::M10 | Self::M15 | Self::M30 | Self::H1 => 35 * WEEK,
            Self::H4 | Self::H12 | Self::D1 => 366 * DAY,
            Self::W1 | Self::Mn1 => 1830 * DAY,
        })
    }
}

impl fmt::Display for Period {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.wire().as_str_name())
    }
}

const LOW: &str = "trendbar low";
const OPEN: &str = "trendbar open";
const HIGH: &str = "trendbar high";
const CLOSE: &str = "trendbar close";
const START: &str = "trendbar timestamp";
const VOLUME: &str = "trendbar volume";

/// Prices of a symbol over one period.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct Trendbar {
    /// Symbol the bar belongs to.
    pub symbol: SymbolId,
    /// Length of time the bar covers.
    pub period: Period,
    /// Moment the bar starts.
    pub timestamp: Timestamp,
    /// First price of the period.
    pub open: Price,
    /// Highest price of the period.
    pub high: Price,
    /// Lowest price of the period.
    pub low: Price,
    /// Last price of the period.
    pub close: Price,
    /// Number of price changes within the period.
    pub volume: u64,
}

impl Trendbar {
    fn parse(
        symbol: SymbolId,
        period: Period,
        bar: &ProtoOaTrendbar,
        last: Option<u64>,
    ) -> Result<Self, ValueError> {
        let low = bar.low.ok_or(ValueError::Missing { kind: LOW })?;
        let low = u64::try_from(low).map_err(|_| ValueError::OutOfRange { kind: LOW })?;
        let above = |delta: Option<u64>, kind: &'static str| {
            delta
                .ok_or(ValueError::Missing { kind })?
                .checked_add(low)
                .ok_or(ValueError::OutOfRange { kind })
        };
        let close = match (bar.delta_close, last) {
            (None, Some(last)) => last,
            (delta, _) => above(delta, CLOSE)?,
        };
        let minutes = bar
            .utc_timestamp_in_minutes
            .ok_or(ValueError::Missing { kind: START })?;
        Ok(Self {
            symbol,
            period,
            timestamp: Timestamp::from_minutes(minutes),
            open: wire_price(above(bar.delta_open, OPEN)?)?,
            high: wire_price(above(bar.delta_high, HIGH)?)?,
            low: wire_price(low)?,
            close: wire_price(close)?,
            volume: u64::try_from(bar.volume)
                .map_err(|_| ValueError::OutOfRange { kind: VOLUME })?,
        })
    }
}

/// Parameters of [`OpenApiClient::trendbars`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[must_use]
pub struct TrendbarsParams {
    account: AccountId,
    symbol: SymbolId,
    period: Period,
    range: TimeRange,
    count: Option<usize>,
}

impl TrendbarsParams {
    /// Asks for the bars of the symbol and period that overlap the range.
    pub const fn new(
        account: AccountId,
        symbol: SymbolId,
        period: Period,
        range: TimeRange,
    ) -> Self {
        Self {
            account,
            symbol,
            period,
            range,
            count: None,
        }
    }

    /// Limits the answer to the latest `count` bars of the range.
    pub const fn count(mut self, count: usize) -> Self {
        self.count = Some(count);
        self
    }

    /// Returns the account the bars are asked for.
    #[must_use]
    pub const fn account(&self) -> AccountId {
        self.account
    }

    /// Returns the symbol the bars belong to.
    #[must_use]
    pub const fn symbol(&self) -> SymbolId {
        self.symbol
    }

    /// Returns the length of time one bar covers.
    #[must_use]
    pub const fn period(&self) -> Period {
        self.period
    }

    /// Returns the range of time the bars overlap.
    #[must_use]
    pub const fn range(&self) -> TimeRange {
        self.range
    }

    /// Returns the largest number of bars in the answer, when one is set.
    #[must_use]
    pub const fn limit(&self) -> Option<usize> {
        self.count
    }
}

pub(crate) fn trendbar_key(account: AccountId, symbol: SymbolId, period: Period) -> ReplayKey {
    ReplayKey::new(
        ReplayStage::Subscription,
        format!("trendbars/{account}/{symbol}/{period}"),
    )
}

fn span_millis(span: Duration) -> i64 {
    i64::try_from(span.as_millis()).unwrap_or(i64::MAX)
}

/// The forming bar of one symbol and period, sent anew whenever a price changes it.
///
/// The subscription behind the stream is restored after a reconnection. The last bid the stream
/// has seen is forgotten the moment the loss of the connection is reported, so a bar from after
/// the loss never closes at a bid from before it.
///
/// A stream that is read too slowly loses events: the connection holds a limited number of
/// them, set with
/// [`ConnectionOptions::event_capacity`](crate::open_api::connection::ConnectionOptions::event_capacity).
/// The loss is reported once by [`TrendbarStream::next`] with [`ConnectorError::Lagged`] and
/// the last bid is forgotten, so no bar closes at a bid older than the loss. The stream stays
/// usable.
#[derive(Debug)]
pub struct TrendbarStream {
    client: OpenApiClient,
    feed: Feed,
    account: AccountId,
    symbol: SymbolId,
    period: Period,
    bid: Option<u64>,
}

impl TrendbarStream {
    /// Returns the account the bars belong to.
    #[must_use]
    pub const fn account(&self) -> AccountId {
        self.account
    }

    /// Returns the symbol the bars belong to.
    #[must_use]
    pub const fn symbol(&self) -> SymbolId {
        self.symbol
    }

    /// Returns the length of time one bar covers.
    #[must_use]
    pub const fn period(&self) -> Period {
        self.period
    }

    /// Waits for the next state of the forming bar.
    ///
    /// The server leaves the last price out of a live bar; it is taken from the bid that
    /// arrives with the bar or, failing that, from the last bid the stream has seen. A bar that
    /// arrives before any bid is passed over.
    ///
    /// # Errors
    ///
    /// Returns [`ConnectorError::Closed`] when the connection is shut down,
    /// [`ConnectorError::SessionEnded`] when the server ended the authorisation of the account,
    /// [`ConnectorError::Lagged`] when events were dropped because the stream was read too
    /// slowly, and [`ConnectorError::Value`] when the server sends a value out of range.
    pub async fn next(&mut self) -> Result<Trendbar, ConnectorError> {
        let period = i32::from(self.period.wire());
        loop {
            let event = match self.feed.next().await {
                Ok(event) => event,
                Err(error) => {
                    if matches!(error, ConnectorError::Lagged { .. }) {
                        self.bid = None;
                    }
                    return Err(error);
                }
            };
            if starts_replay(&event) {
                self.bid = None;
                continue;
            }
            let Some(spot) = event.decode::<ProtoOaSpotEvent>() else {
                continue;
            };
            if spot.ctid_trader_account_id != self.account.get()
                || spot.symbol_id != self.symbol.get()
            {
                continue;
            }
            self.bid = spot.bid.or(self.bid);
            let Some(bar) = spot.trendbar.iter().find(|bar| bar.period == Some(period)) else {
                continue;
            };
            if bar.delta_close.is_none() && self.bid.is_none() {
                continue;
            }
            return Ok(Trendbar::parse(self.symbol, self.period, bar, self.bid)?);
        }
    }

    /// Ends the subscription to the live bars; the spot subscription of the symbol stays.
    ///
    /// # Errors
    ///
    /// Fails like [`OpenApiClient::unsubscribe_live_trendbars`].
    pub async fn unsubscribe(self) -> Result<(), ConnectorError> {
        self.client
            .unsubscribe_live_trendbars(self.account, self.symbol, self.period)
            .await
    }
}

impl OpenApiClient {
    /// Fetches the bars of a symbol and period that overlap a range of time, oldest first.
    ///
    /// The server answers one request with a limited number of bars and accepts a limited span
    /// of time per period, see [`Period::max_range`]. A longer range is fetched with several
    /// requests, latest part first, each of which counts against the limit for historical data.
    /// The bar that is still forming is not part of the answer.
    ///
    /// # Errors
    ///
    /// Returns [`ConnectorError::Server`] when the server refuses a request,
    /// [`ConnectorError::Value`] when a bar lacks a field or carries a value out of range, and
    /// the errors of [`Connection::request`](crate::open_api::connection::Connection::request)
    /// otherwise.
    pub async fn trendbars(
        &self,
        params: &TrendbarsParams,
    ) -> Result<Vec<Trendbar>, ConnectorError> {
        let from = params.range.from().as_millis();
        let span = span_millis(params.period.max_range());
        let wanted = params.count.unwrap_or(usize::MAX);
        let mut cursor = params.range.to().as_millis();
        let mut bars = BTreeMap::new();
        while cursor >= from && bars.len() < wanted {
            let start = cursor.saturating_sub(span).max(from);
            let request = ProtoOaGetTrendbarsReq {
                payload_type: None,
                ctid_trader_account_id: params.account.get(),
                from_timestamp: start,
                to_timestamp: cursor,
                period: i32::from(params.period.wire()),
                symbol_id: params.symbol.get(),
                count: None,
            };
            let response = self.connection().request(&request).await?;
            let mut earliest = start;
            for (index, wire) in response.trendbar.iter().enumerate() {
                let bar = Trendbar::parse(params.symbol, params.period, wire, None)?;
                let opened = bar.timestamp.as_millis();
                earliest = if index == 0 {
                    opened
                } else {
                    earliest.min(opened)
                };
                bars.insert(bar.timestamp, bar);
            }
            cursor = earliest.min(cursor).saturating_sub(1);
        }
        let skipped = bars.len().saturating_sub(wanted);
        Ok(bars.into_values().skip(skipped).collect())
    }

    /// Subscribes to the forming bar of a symbol and period on an authorised account.
    ///
    /// The server sends live bars only for symbols whose spot prices are subscribed, so the
    /// spot subscription is requested first unless this client holds it already. Both
    /// subscriptions are restored after a reconnection, in that order.
    ///
    /// # Errors
    ///
    /// Returns [`ConnectorError::Server`] when the server refuses a subscription, and the
    /// errors of [`Connection::request`](crate::open_api::connection::Connection::request)
    /// otherwise.
    pub async fn subscribe_live_trendbars(
        &self,
        account: AccountId,
        symbol: SymbolId,
        period: Period,
    ) -> Result<TrendbarStream, ConnectorError> {
        let stream = TrendbarStream {
            client: self.clone(),
            feed: Feed::new(self.connection(), account),
            account,
            symbol,
            period,
            bid: None,
        };
        if !self.has_spots(account, symbol) {
            self.subscribe_spots(account, &[symbol]).await?;
        }
        let request = ProtoOaSubscribeLiveTrendbarReq {
            payload_type: None,
            ctid_trader_account_id: account.get(),
            period: i32::from(period.wire()),
            symbol_id: symbol.get(),
        };
        self.connection().request(&request).await?;
        self.connection()
            .register_replay(trendbar_key(account, symbol, period), &request)
            .await?;
        self.track_trendbars(account, symbol, period, true);
        Ok(stream)
    }

    /// Ends the subscription to the forming bar of a symbol and period.
    ///
    /// # Errors
    ///
    /// Returns [`ConnectorError::Server`] when the server refuses the request, and the errors
    /// of [`Connection::request`](crate::open_api::connection::Connection::request) otherwise.
    pub async fn unsubscribe_live_trendbars(
        &self,
        account: AccountId,
        symbol: SymbolId,
        period: Period,
    ) -> Result<(), ConnectorError> {
        let request = ProtoOaUnsubscribeLiveTrendbarReq {
            payload_type: None,
            ctid_trader_account_id: account.get(),
            period: i32::from(period.wire()),
            symbol_id: symbol.get(),
        };
        self.connection().request(&request).await?;
        self.connection()
            .remove_replay(trendbar_key(account, symbol, period))
            .await?;
        self.track_trendbars(account, symbol, period, false);
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use std::time::Duration;

    use super::{Period, Trendbar, TrendbarsParams, trendbar_key};
    use crate::common::errors::ValueError;
    use crate::common::ids::{AccountId, SymbolId};
    use crate::common::time::{TimeRange, Timestamp};
    use crate::open_api::messages::{ProtoOaTrendbar, ProtoOaTrendbarPeriod};

    fn symbol() -> SymbolId {
        SymbolId::new(1).expect("symbol id")
    }

    fn wire() -> ProtoOaTrendbar {
        ProtoOaTrendbar {
            volume: 42,
            period: None,
            low: Some(108_500),
            delta_open: Some(5),
            delta_close: Some(7),
            delta_high: Some(12),
            utc_timestamp_in_minutes: Some(3),
        }
    }

    fn parse(bar: &ProtoOaTrendbar, last: Option<u64>) -> Result<Trendbar, ValueError> {
        Trendbar::parse(symbol(), Period::M5, bar, last)
    }

    #[test]
    fn periods_map_to_the_wire_and_grow_in_length() {
        let names: Vec<String> = Period::ALL.iter().map(ToString::to_string).collect();
        assert_eq!(
            names,
            [
                "M1", "M2", "M3", "M4", "M5", "M10", "M15", "M30", "H1", "H4", "H12", "D1", "W1",
                "MN1"
            ]
        );
        for (index, period) in Period::ALL.iter().enumerate() {
            assert_eq!(
                ProtoOaTrendbarPeriod::try_from(i32::try_from(index + 1).expect("number")),
                Ok(period.wire())
            );
            assert!(period.max_range() > period.duration());
        }
        assert!(
            Period::ALL
                .windows(2)
                .all(|pair| pair[0].duration() < pair[1].duration() && pair[0] < pair[1])
        );
        assert!(
            Period::ALL
                .windows(2)
                .all(|pair| pair[0].max_range() <= pair[1].max_range())
        );
        assert_eq!(Period::M1.duration(), Duration::from_secs(60));
        assert_eq!(Period::Mn1.duration(), Duration::from_secs(2_592_000));
        assert_eq!(Period::M5.max_range(), Duration::from_millis(3_024_000_000));
        assert_eq!(
            Period::H1.max_range(),
            Duration::from_millis(21_168_000_000)
        );
        assert_eq!(
            Period::D1.max_range(),
            Duration::from_millis(31_622_400_000)
        );
        assert_eq!(
            Period::W1.max_range(),
            Duration::from_millis(158_112_000_000)
        );
    }

    #[test]
    fn bar_prices_are_the_low_plus_their_deltas() {
        let bar = parse(&wire(), None).expect("bar");
        assert_eq!(bar.symbol, symbol());
        assert_eq!(bar.period, Period::M5);
        assert_eq!(bar.timestamp.as_millis(), 180_000);
        let prices = [bar.open, bar.high, bar.low, bar.close].map(|price| price.to_string());
        assert_eq!(prices, ["1.08505", "1.08512", "1.08500", "1.08507"]);
        assert_eq!(bar.volume, 42);
        assert_eq!(parse(&wire(), Some(1)), Ok(bar));
    }

    #[test]
    fn a_live_bar_takes_its_close_from_the_last_price() {
        let live = ProtoOaTrendbar {
            delta_close: None,
            ..wire()
        };
        let bar = parse(&live, Some(108_509)).expect("bar");
        assert_eq!(bar.close.to_string(), "1.08509");
        assert_eq!(
            parse(&live, None),
            Err(ValueError::Missing {
                kind: "trendbar close"
            })
        );
    }

    #[test]
    fn bars_with_absent_or_impossible_fields_are_rejected() {
        let missing = |kind| Err(ValueError::Missing { kind });
        let out_of_range = |kind| Err(ValueError::OutOfRange { kind });
        let cases = [
            (
                ProtoOaTrendbar {
                    low: None,
                    ..wire()
                },
                missing("trendbar low"),
            ),
            (
                ProtoOaTrendbar {
                    delta_open: None,
                    ..wire()
                },
                missing("trendbar open"),
            ),
            (
                ProtoOaTrendbar {
                    delta_high: None,
                    ..wire()
                },
                missing("trendbar high"),
            ),
            (
                ProtoOaTrendbar {
                    utc_timestamp_in_minutes: None,
                    ..wire()
                },
                missing("trendbar timestamp"),
            ),
            (
                ProtoOaTrendbar {
                    low: Some(-1),
                    ..wire()
                },
                out_of_range("trendbar low"),
            ),
            (
                ProtoOaTrendbar {
                    delta_high: Some(u64::MAX),
                    ..wire()
                },
                out_of_range("trendbar high"),
            ),
            (
                ProtoOaTrendbar {
                    volume: -1,
                    ..wire()
                },
                out_of_range("trendbar volume"),
            ),
        ];
        for (bar, error) in cases {
            assert_eq!(parse(&bar, None), error);
        }
        assert_eq!(
            ValueError::Missing {
                kind: "trendbar low"
            }
            .to_string(),
            "trendbar low is missing"
        );
    }

    #[test]
    fn parameters_keep_what_they_were_built_from() {
        let account = AccountId::new(7).expect("account id");
        let at = |millis| Timestamp::from_millis(millis).expect("timestamp");
        let range = TimeRange::new(at(10), at(20)).expect("range");
        let params = TrendbarsParams::new(account, symbol(), Period::H4, range);
        assert_eq!(params.account(), account);
        assert_eq!(params.symbol(), symbol());
        assert_eq!(params.period(), Period::H4);
        assert_eq!(params.range(), range);
        assert_eq!(params.limit(), None);
        assert_eq!(params.count(3).limit(), Some(3));
        assert_eq!(
            trendbar_key(account, symbol(), Period::H4).id(),
            "trendbars/7/1/H4"
        );
    }
}