tvdata_rs/history/mod.rs
1mod fetch;
2mod request;
3
4use request::HistorySeriesMap;
5
6use crate::client::TradingViewClient;
7use crate::error::Result;
8use crate::scanner::Ticker;
9
10pub use request::{
11 Adjustment, Bar, HistoryBatchRequest, HistoryRequest, HistorySeries, Interval, TradingSession,
12};
13
14impl TradingViewClient {
15 /// Downloads multiple OHLCV history series with bounded concurrency.
16 ///
17 /// # Examples
18 ///
19 /// ```no_run
20 /// use tvdata_rs::{HistoryBatchRequest, Interval, Result, TradingViewClient};
21 ///
22 /// #[tokio::main]
23 /// async fn main() -> Result<()> {
24 /// let client = TradingViewClient::builder().build()?;
25 /// let request = HistoryBatchRequest::new(["NASDAQ:AAPL", "NASDAQ:MSFT"], Interval::Day1, 30);
26 /// let series = client.history_batch(&request).await?;
27 ///
28 /// println!("series: {}", series.len());
29 /// Ok(())
30 /// }
31 /// ```
32 pub async fn history_batch(&self, request: &HistoryBatchRequest) -> Result<Vec<HistorySeries>> {
33 fetch::fetch_history_batch_with(
34 request.to_requests(),
35 request.concurrency,
36 |request| async move { self.history(&request).await },
37 )
38 .await
39 }
40
41 /// Downloads the maximum history currently available for multiple symbols.
42 ///
43 /// The crate keeps requesting older bars over the chart websocket until
44 /// TradingView stops returning new history.
45 ///
46 /// # Examples
47 ///
48 /// ```no_run
49 /// use tvdata_rs::{Interval, Result, TradingViewClient};
50 ///
51 /// #[tokio::main]
52 /// async fn main() -> Result<()> {
53 /// let client = TradingViewClient::builder().build()?;
54 /// let series = client
55 /// .download_history_max(["NASDAQ:AAPL", "NASDAQ:MSFT"], Interval::Day1)
56 /// .await?;
57 ///
58 /// println!("series: {}", series.len());
59 /// Ok(())
60 /// }
61 /// ```
62 pub async fn download_history_max<I, T>(
63 &self,
64 symbols: I,
65 interval: Interval,
66 ) -> Result<Vec<HistorySeries>>
67 where
68 I: IntoIterator<Item = T>,
69 T: Into<Ticker>,
70 {
71 let request = HistoryBatchRequest::max(symbols, interval);
72 self.history_batch(&request).await
73 }
74
75 /// Convenience wrapper around [`TradingViewClient::history_batch`] for a list of symbols.
76 pub async fn download_history<I, T>(
77 &self,
78 symbols: I,
79 interval: Interval,
80 bars: u32,
81 ) -> Result<Vec<HistorySeries>>
82 where
83 I: IntoIterator<Item = T>,
84 T: Into<Ticker>,
85 {
86 let request = HistoryBatchRequest::new(symbols, interval, bars);
87 self.history_batch(&request).await
88 }
89
90 /// Downloads multiple history series and returns them keyed by symbol.
91 pub async fn download_history_map<I, T>(
92 &self,
93 symbols: I,
94 interval: Interval,
95 bars: u32,
96 ) -> Result<HistorySeriesMap>
97 where
98 I: IntoIterator<Item = T>,
99 T: Into<Ticker>,
100 {
101 let series = self.download_history(symbols, interval, bars).await?;
102 Ok(series
103 .into_iter()
104 .map(|series| (series.symbol.clone(), series))
105 .collect())
106 }
107
108 /// Downloads the maximum history available and returns it keyed by symbol.
109 pub async fn download_history_map_max<I, T>(
110 &self,
111 symbols: I,
112 interval: Interval,
113 ) -> Result<HistorySeriesMap>
114 where
115 I: IntoIterator<Item = T>,
116 T: Into<Ticker>,
117 {
118 let series = self.download_history_max(symbols, interval).await?;
119 Ok(series
120 .into_iter()
121 .map(|series| (series.symbol.clone(), series))
122 .collect())
123 }
124}
125
126pub(crate) use fetch::fetch_history;