tastytrade 0.4.2

Library for trading through tastytrade's 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
/******************************************************************************
   Author: Joaquín Béjar García
   Email: jb@taunais.com
   Date: 31/8/25
******************************************************************************/

//! Bulk download of option symbols.
//!
//! Split into stages that can be reasoned about separately: discovery finds
//! the underlyings, retrieval fetches their chains, and transformation turns a
//! chain into symbol entries. Only the retrieval stage does I/O, only the
//! transformation stage is pure, and the report says which parts of the answer
//! are missing rather than leaving that in the logs.

use crate::api::query::PageRequest;
use crate::prelude::{SymbolEntry, TastyTradeConfig};
use crate::types::instrument::{FuturesNestedOptionChain, NestedOptionChain};
use crate::types::instrument_filter::ActiveEquityFilter;
use crate::utils::parse::expiration_instant;
use crate::{InstrumentType, TastyResult, TastyTrade, TastyTradeError};
use chrono::{DateTime, Utc};
use futures_util::StreamExt;
use futures_util::stream;
use std::collections::HashSet;
use tracing::{debug, info, warn};

/// The venue this crate labels downloaded symbols with.
const EXCHANGE: &str = "TASTYTRADE";

/// Items per page when walking a listing.
///
/// The value `list_active_equities` used to hard-code before the page size
/// became the caller's decision.
const LISTING_PAGE_SIZE: u32 = 1000;

/// Future products that do not carry option chains.
///
/// Asking for them costs a round trip and answers nothing, so they are skipped
/// rather than retried. Interest-rate futures, mostly.
const PRODUCTS_WITHOUT_OPTIONS: &[&str] = &["GE", "ZQ", "ZT", "ZF", "ZN", "ZB", "UB"];

/// Limits and concurrency for a download.
///
/// Every knob that used to be an undocumented environment variable or a
/// literal buried in a loop. `MAX_EQUITIES` and `MAX_FUTURE_PRODUCTS` were
/// read from the environment by a function nothing documented, so a caller had
/// no way to discover them and a library had no business reading them.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DownloadLimits {
    /// How many pages of active equities to walk before stopping.
    pub max_equity_pages: usize,
    /// How many equities to fetch chains for.
    pub max_equities: usize,
    /// How many future products to fetch chains for.
    pub max_future_products: usize,
    /// How many chain requests may be in flight at once.
    ///
    /// The old code was strictly sequential, which is slow, but unbounded
    /// concurrency against a broker is a good way to be rate limited or
    /// blocked.
    pub concurrency: usize,
}

impl Default for DownloadLimits {
    fn default() -> Self {
        Self {
            max_equity_pages: 5,
            max_equities: 100,
            max_future_products: 50,
            concurrency: 8,
        }
    }
}

/// One underlying whose chain could not be retrieved.
///
/// Named so a caller can retry exactly what failed instead of the whole run.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DownloadFailure {
    /// The underlying symbol or product code that failed.
    pub underlying: String,
    /// Why, as text. Never contains a credential or an account.
    pub reason: String,
    /// Whether asking again could plausibly work.
    ///
    /// Carried rather than left for the caller to infer from `reason`:
    /// deciding what to retry by matching on prose is how the old code
    /// classified failures, and it was wrong the moment the venue reworded
    /// anything.
    pub retryable: bool,
}

impl DownloadFailure {
    fn from_error(underlying: String, error: &TastyTradeError) -> Self {
        Self {
            underlying,
            reason: error.to_string(),
            retryable: error.is_retryable(),
        }
    }
}

/// How complete a download is.
///
/// The old function logged failures and returned a plain `Vec`, so a caller
/// could not tell a complete answer from one missing half its underlyings —
/// and a short list looks exactly like a quiet market.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DownloadOutcome {
    /// Every underlying that was asked for answered.
    Complete,
    /// Some underlyings failed. The symbols present are still usable.
    Partial {
        /// What failed and why.
        failures: Vec<DownloadFailure>,
    },
}

/// The symbols and how much of the picture they are.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DownloadReport {
    /// The symbols, deduplicated and sorted.
    pub symbols: Vec<SymbolEntry>,
    /// Whether anything is missing.
    pub outcome: DownloadOutcome,
    /// How many underlyings were asked about.
    pub underlyings_requested: usize,
}

impl DownloadReport {
    /// Whether every underlying answered.
    pub fn is_complete(&self) -> bool {
        matches!(self.outcome, DownloadOutcome::Complete)
    }

    /// What failed, empty on a complete run.
    pub fn failures(&self) -> &[DownloadFailure] {
        match &self.outcome {
            DownloadOutcome::Complete => &[],
            DownloadOutcome::Partial { failures } => failures,
        }
    }
}

/// Downloads every equity-option and future-option symbol, with defaults.
///
/// # Errors
///
/// Fails only when the download could not start: missing credentials, a
/// refused login, or no underlyings discovered at all. An underlying whose
/// chain fails is recorded in the report rather than failing the run.
pub async fn download_options_report() -> TastyResult<DownloadReport> {
    let config = TastyTradeConfig::new();
    download_options_symbols_with(&config, &DownloadLimits::default()).await
}

/// Downloads every option symbol, discarding the report.
///
/// # Errors
///
/// As [`download_options_report`].
#[deprecated(
    since = "0.4.0",
    note = "use download_options_report: this signature cannot tell a complete \
            answer from one missing half its underlyings"
)]
pub async fn download_options_symbols() -> Result<Vec<SymbolEntry>, Box<dyn std::error::Error>> {
    // Kept signature-compatible on purpose. The report is the improvement, but
    // a caller who only wants the symbols should not have to be broken to keep
    // getting them.
    Ok(download_options_report().await?.symbols)
}

/// Downloads every equity-option and future-option symbol.
///
/// The configuration is taken rather than read from the environment, so a
/// caller decides which account and which environment this runs against.
pub async fn download_options_symbols_with(
    config: &TastyTradeConfig,
    limits: &DownloadLimits,
) -> TastyResult<DownloadReport> {
    // connect already refuses missing credentials without a network call, and
    // says which variables to set.
    let tasty = TastyTrade::connect(config).await?;
    let now = Utc::now();

    // The two sources are independent, so one failing must not discard the
    // other's work or stop it from running at all.
    let mut failures = Vec::new();

    let equities = match discover_equities(&tasty, limits).await {
        Ok(found) => found,
        Err(e) => {
            failures.push(DownloadFailure::from_error(
                "equity discovery".to_string(),
                &e,
            ));
            Vec::new()
        }
    };
    let products = match discover_future_products(&tasty, limits).await {
        Ok(found) => found,
        Err(e) => {
            failures.push(DownloadFailure::from_error(
                "future product discovery".to_string(),
                &e,
            ));
            Vec::new()
        }
    };

    // Both failing is not a partial answer, it is no answer.
    if equities.is_empty() && products.is_empty() {
        return Err(TastyTradeError::Unknown(
            "no equity or future underlyings were discovered; check connectivity and that the \
             account can see instruments"
                .to_string(),
        ));
    }

    let requested = equities.len() + products.len();
    info!(
        "Downloading option chains for {} equities and {} future products",
        equities.len(),
        products.len()
    );

    let mut symbols = Vec::new();

    let (equity_symbols, equity_failures) =
        fetch_equity_chains(&tasty, &equities, limits, now).await;
    symbols.extend(equity_symbols);
    failures.extend(equity_failures);

    let (future_symbols, future_failures) =
        fetch_future_chains(&tasty, &products, limits, now).await;
    symbols.extend(future_symbols);
    failures.extend(future_failures);

    // Deduplicated by identity, which for a SymbolEntry is symbol plus epic,
    // then sorted. Concurrent retrieval means arrival order is not stable, and
    // a bulk download that produces a different file every run is one nobody
    // can diff.
    let unique: HashSet<SymbolEntry> = symbols.into_iter().collect();
    let mut symbols: Vec<SymbolEntry> = unique.into_iter().collect();
    symbols.sort_unstable_by(|a, b| {
        a.symbol
            .cmp(&b.symbol)
            .then_with(|| a.epic.cmp(&b.epic))
            .then_with(|| a.expiry.cmp(&b.expiry))
    });

    // Sorted for the same reason the symbols are: buffer_unordered decides the
    // order failures arrive in, and a report that differs between identical
    // runs is not the deterministic report this claims to be.
    failures.sort_unstable_by(|a, b| {
        a.underlying
            .cmp(&b.underlying)
            .then_with(|| a.reason.cmp(&b.reason))
    });

    if failures.is_empty() {
        info!("Downloaded {} unique symbols", symbols.len());
    } else {
        warn!(
            "Downloaded {} unique symbols; {} of {} underlyings failed",
            symbols.len(),
            failures.len(),
            requested
        );
    }

    Ok(DownloadReport {
        symbols,
        outcome: if failures.is_empty() {
            DownloadOutcome::Complete
        } else {
            DownloadOutcome::Partial { failures }
        },
        underlyings_requested: requested,
    })
}

/// The wire form of a page index, or nothing when it has no wire form.
///
/// `max_equity_pages` is a `usize` and the venue's `page-offset` is a `u32`, so
/// a limit above `u32::MAX` runs out of representable offsets before it runs
/// out of iterations. Saturating there is worse than stopping: every remaining
/// iteration would ask for the same last page, so the walk would keep issuing
/// authenticated requests that return the same items, and a misconfigured limit
/// would look like a very slow download rather than a mistake.
fn page_offset(page: usize) -> Option<u32> {
    let offset = u32::try_from(page).ok();
    if offset.is_none() {
        warn!("stopping the listing walk at page {page}: the venue's page offset is a u32");
    }
    offset
}

/// Walks the active-equities pages up to the configured limit.
async fn discover_equities(
    tasty: &TastyTrade,
    limits: &DownloadLimits,
) -> TastyResult<Vec<crate::types::instrument::EquityInstrument>> {
    let mut found = Vec::new();

    for page in 0..limits.max_equity_pages {
        // The page size the walk has always used. Explicit now rather than
        // buried in the endpoint method, because it is a decision about this
        // downloader and not about the endpoint.
        let Some(offset) = page_offset(page) else {
            break;
        };
        let request = PageRequest::new()
            .with_per_page(LISTING_PAGE_SIZE)
            .with_page_offset(offset);
        let filter = ActiveEquityFilter::new().with_page(request);
        let paginated = tasty.list_active_equities(&filter).await?;
        let pagination = &paginated.pagination;
        debug!(
            "active equities page {}/{}: {} items",
            pagination.page_offset, pagination.total_pages, pagination.current_item_count
        );

        // Read before the items move out of `paginated`.
        let has_more = paginated.has_more();
        found.extend(paginated.items);

        if !has_more {
            break;
        }
    }

    if found.len() > limits.max_equities {
        info!(
            "Limiting to {} of {} equities",
            limits.max_equities,
            found.len()
        );
        found.truncate(limits.max_equities);
    }

    Ok(found)
}

/// Lists future products worth asking about.
async fn discover_future_products(
    tasty: &TastyTrade,
    limits: &DownloadLimits,
) -> TastyResult<Vec<crate::types::instrument::FutureProduct>> {
    // Future products paginate now, so this walks pages instead of assuming
    // one response is the whole catalogue. Bounded by the same page budget as
    // equities: a downloader that walks until the venue says stop is a
    // downloader that hangs when the venue is wrong about that.
    let mut products: Vec<crate::types::instrument::FutureProduct> = Vec::new();

    for page in 0..limits.max_equity_pages {
        let Some(offset) = page_offset(page) else {
            break;
        };
        let request = PageRequest::new()
            .with_per_page(LISTING_PAGE_SIZE)
            .with_page_offset(offset);
        let paginated = tasty.list_future_products(&request).await?;
        let pagination = &paginated.pagination;
        debug!(
            "future products page {}/{}: {} items",
            pagination.page_offset, pagination.total_pages, pagination.current_item_count
        );

        // Read before the items move out of `paginated`.
        let has_more = paginated.has_more();
        products.extend(
            paginated
                .items
                .into_iter()
                .filter(|product| !PRODUCTS_WITHOUT_OPTIONS.contains(&product.code.as_str())),
        );

        if !has_more {
            break;
        }
    }

    if products.len() > limits.max_future_products {
        info!(
            "Limiting to {} of {} future products",
            limits.max_future_products,
            products.len()
        );
        products.truncate(limits.max_future_products);
    }

    Ok(products)
}

/// Fetches equity chains with bounded concurrency.
async fn fetch_equity_chains(
    tasty: &TastyTrade,
    equities: &[crate::types::instrument::EquityInstrument],
    limits: &DownloadLimits,
    last_update: DateTime<Utc>,
) -> (Vec<SymbolEntry>, Vec<DownloadFailure>) {
    let results = stream::iter(equities.iter().map(|equity| async move {
        let symbol = equity.symbol.clone();
        (
            symbol.0.clone(),
            tasty.list_nested_option_chains(symbol).await,
        )
    }))
    .buffer_unordered(limits.concurrency.max(1))
    .collect::<Vec<_>>()
    .await;

    let mut symbols = Vec::new();
    let mut failures = Vec::new();

    for (underlying, result) in results {
        match result {
            Ok(chains) => {
                for chain in &chains {
                    symbols.extend(equity_chain_to_symbols(chain, last_update));
                }
            }
            Err(e) => failures.push(DownloadFailure::from_error(underlying, &e)),
        }
    }

    (symbols, failures)
}

/// Fetches future-option chains with bounded concurrency.
async fn fetch_future_chains(
    tasty: &TastyTrade,
    products: &[crate::types::instrument::FutureProduct],
    limits: &DownloadLimits,
    last_update: DateTime<Utc>,
) -> (Vec<SymbolEntry>, Vec<DownloadFailure>) {
    let results = stream::iter(products.iter().map(|product| async move {
        (
            product.code.clone(),
            tasty.list_nested_futures_option_chains(&product.code).await,
        )
    }))
    .buffer_unordered(limits.concurrency.max(1))
    .collect::<Vec<_>>()
    .await;

    let mut symbols = Vec::new();
    let mut failures = Vec::new();

    for (underlying, result) in results {
        match result {
            Ok(chains) => {
                for chain in &chains {
                    symbols.extend(futures_chain_to_symbols(chain, last_update));
                }
            }
            Err(e) => failures.push(DownloadFailure::from_error(underlying, &e)),
        }
    }

    (symbols, failures)
}

/// Turns one equity chain into symbol entries.
///
/// Pure: no I/O, no logging, no clock. This is the part worth testing, and it
/// used to be four levels deep inside a function that also did all three.
fn equity_chain_to_symbols(
    chain: &NestedOptionChain,
    last_update: DateTime<Utc>,
) -> Vec<SymbolEntry> {
    let mut symbols = Vec::new();

    for expiration in &chain.expirations {
        let expiry = expiration_instant(expiration.expiration_date);

        for strike in &expiration.strikes {
            for (side, symbol) in [("Call", &strike.call), ("Put", &strike.put)] {
                symbols.push(SymbolEntry {
                    symbol: symbol.0.clone(),
                    epic: symbol.0.clone(),
                    name: format!(
                        "{} {} ${} {}",
                        chain.underlying_symbol.0,
                        side,
                        strike.strike_price,
                        expiration.expiration_date
                    ),
                    instrument_type: InstrumentType::EquityOption,
                    exchange: EXCHANGE.to_string(),
                    expiry,
                    last_update,
                });
            }
        }
    }

    symbols
}

/// Turns one nested futures chain into symbol entries. Pure, as above.
fn futures_chain_to_symbols(
    chain: &FuturesNestedOptionChain,
    last_update: DateTime<Utc>,
) -> Vec<SymbolEntry> {
    let mut symbols = Vec::new();

    for option_chain in &chain.option_chains {
        for expiration in &option_chain.expirations {
            let expiry = expiration_instant(expiration.expiration_date);

            for strike in &expiration.strikes {
                for (side, symbol) in [("Call", &strike.call), ("Put", &strike.put)] {
                    symbols.push(SymbolEntry {
                        symbol: symbol.clone(),
                        epic: symbol.clone(),
                        name: format!(
                            "{} Future {} ${} {}",
                            option_chain.underlying_symbol,
                            side,
                            strike.strike_price,
                            expiration.expiration_date
                        ),
                        instrument_type: InstrumentType::FutureOption,
                        exchange: EXCHANGE.to_string(),
                        expiry,
                        last_update,
                    });
                }
            }
        }
    }

    symbols
}

#[cfg(test)]
mod tests {
    use super::*;

    fn at(seconds: i64) -> DateTime<Utc> {
        DateTime::from_timestamp(seconds, 0).expect("a valid timestamp")
    }

    #[test]
    fn a_complete_report_has_no_failures() {
        let report = DownloadReport {
            symbols: Vec::new(),
            outcome: DownloadOutcome::Complete,
            underlyings_requested: 3,
        };

        assert!(report.is_complete());
        assert!(report.failures().is_empty());
    }

    /// The old function logged failures and returned a plain Vec, so a caller
    /// could not tell a complete answer from one missing half its underlyings.
    /// A short list looks exactly like a quiet market.
    #[test]
    fn a_partial_report_names_what_is_missing() {
        let report = DownloadReport {
            symbols: Vec::new(),
            outcome: DownloadOutcome::Partial {
                failures: vec![DownloadFailure {
                    underlying: "AAPL".to_string(),
                    reason: "HTTP 503".to_string(),
                    retryable: true,
                }],
            },
            underlyings_requested: 2,
        };

        assert!(!report.is_complete());
        assert_eq!(report.failures().len(), 1);
        assert_eq!(report.failures()[0].underlying, "AAPL");
        assert!(
            report.failures()[0].retryable,
            "a caller must not have to parse the reason to decide what to retry"
        );
    }

    /// The retry verdict comes from the error's own classification, not from
    /// matching on its prose, which is how the old code got it wrong.
    #[test]
    fn a_failure_carries_the_errors_own_retry_verdict() {
        let transient = DownloadFailure::from_error(
            "AAPL".to_string(),
            &TastyTradeError::Connection("refused".to_string()),
        );
        let fatal = DownloadFailure::from_error(
            "MSFT".to_string(),
            &TastyTradeError::Auth("rejected".to_string()),
        );

        assert!(transient.retryable);
        assert!(!fatal.retryable, "a rejected credential does not improve");
    }

    /// buffer_unordered decides the order failures arrive in, so a report that
    /// differs between identical runs would not be the deterministic report
    /// this claims to be.
    #[test]
    fn failures_are_ordered_deterministically() {
        let mut failures = [
            DownloadFailure {
                underlying: "MSFT".to_string(),
                reason: "b".to_string(),
                retryable: true,
            },
            DownloadFailure {
                underlying: "AAPL".to_string(),
                reason: "a".to_string(),
                retryable: false,
            },
        ];
        failures.sort_unstable_by(|a, b| {
            a.underlying
                .cmp(&b.underlying)
                .then_with(|| a.reason.cmp(&b.reason))
        });

        assert_eq!(failures[0].underlying, "AAPL");
    }

    /// Interest-rate futures carry no option chains, so asking costs a round
    /// trip and answers nothing.
    #[test]
    fn products_without_options_are_known_by_code() {
        for code in ["GE", "ZN", "UB"] {
            assert!(
                PRODUCTS_WITHOUT_OPTIONS.contains(&code),
                "{code} should be skipped"
            );
        }
        assert!(!PRODUCTS_WITHOUT_OPTIONS.contains(&"ES"));
    }

    /// Limits are a documented type now rather than environment variables a
    /// caller had no way to discover.
    #[test]
    fn the_default_limits_are_bounded_on_every_axis() {
        let limits = DownloadLimits::default();

        assert!(limits.max_equity_pages > 0);
        assert!(limits.max_equities > 0);
        assert!(limits.max_future_products > 0);
        assert!(
            limits.concurrency > 1,
            "the point of the refactor is that it is not sequential"
        );
    }

    /// Concurrent retrieval means arrival order is not stable, so a bulk
    /// download that produced a different file every run would be one nobody
    /// can diff.
    #[test]
    fn identical_symbols_from_different_sources_collapse_to_one() {
        let entry = |symbol: &str, expiry| SymbolEntry {
            symbol: symbol.to_string(),
            epic: symbol.to_string(),
            name: format!("{symbol} option"),
            instrument_type: InstrumentType::EquityOption,
            exchange: EXCHANGE.to_string(),
            expiry,
            last_update: at(0),
        };

        let unique: HashSet<SymbolEntry> = vec![
            entry("AAPL 250919C00100000", at(10)),
            entry("AAPL 250919C00100000", at(10)),
            entry("MSFT 250919P00300000", at(20)),
        ]
        .into_iter()
        .collect();

        assert_eq!(unique.len(), 2, "identity is symbol plus epic");
    }
}