remove blocking from yahoo_finance_api

This commit is contained in:
Paul Campbell 2024-03-10 16:49:56 +00:00
parent 0bd78e07e3
commit 7bfb2b915e
3 changed files with 10 additions and 13 deletions

View file

@ -423,12 +423,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8b7abd5d659d9b90c8cba917f6ec750a74e2dc23902ef9cd4cc8c8b22e6036a" checksum = "d8b7abd5d659d9b90c8cba917f6ec750a74e2dc23902ef9cd4cc8c8b22e6036a"
dependencies = [ dependencies = [
"futures-core", "futures-core",
"futures-io",
"futures-task", "futures-task",
"memchr",
"pin-project-lite", "pin-project-lite",
"pin-utils", "pin-utils",
"slab",
] ]
[[package]] [[package]]

View file

@ -9,5 +9,5 @@ version = "0.1.0"
[dependencies] [dependencies]
chrono = { version = "0.4", features = ["serde"] } chrono = { version = "0.4", features = ["serde"] }
clap = {version = "3.1.8", features = ["derive"]} clap = {version = "3.1.8", features = ["derive"]}
yahoo_finance_api = { version = "1.1", features = ["blocking"] } yahoo_finance_api = "1.1"
async-std = { version = "1.6", features = ["attributes", "tokio1"] } async-std = { version = "1.6", features = ["attributes", "tokio1"] }

View file

@ -20,7 +20,6 @@ struct Opts {
/// A trait to provide a common interface for all signal calculations. /// A trait to provide a common interface for all signal calculations.
/// ///
trait AsyncStockSignal { trait AsyncStockSignal {
/// ///
/// The signal's data type. /// The signal's data type.
/// ///
@ -97,7 +96,7 @@ fn min(series: &[f64]) -> Option<f64> {
/// ///
/// Retrieve data from a data source and extract the closing prices. Errors during download are mapped onto io::Errors as InvalidData. /// Retrieve data from a data source and extract the closing prices. Errors during download are mapped onto io::Errors as InvalidData.
/// ///
fn fetch_closing_data( async fn fetch_closing_data(
symbol: &str, symbol: &str,
beginning: &DateTime<Utc>, beginning: &DateTime<Utc>,
end: &DateTime<Utc>, end: &DateTime<Utc>,
@ -106,6 +105,7 @@ fn fetch_closing_data(
let response = provider let response = provider
.get_quote_history(symbol, *beginning, *end) .get_quote_history(symbol, *beginning, *end)
.await
.map_err(|_| Error::from(ErrorKind::InvalidData))?; .map_err(|_| Error::from(ErrorKind::InvalidData))?;
let mut quotes = response let mut quotes = response
.quotes() .quotes()
@ -127,14 +127,14 @@ async fn main() -> std::io::Result<()> {
// a simple way to output a CSV header // a simple way to output a CSV header
println!("period start,symbol,price,change %,min,max,30d avg"); println!("period start,symbol,price,change %,min,max,30d avg");
for symbol in opts.symbols.split(',') { for symbol in opts.symbols.split(',') {
let closes = fetch_closing_data(&symbol, &from, &to)?; let closes = fetch_closing_data(&symbol, &from, &to).await?;
if !closes.is_empty() { if !closes.is_empty() {
// min/max of the period. unwrap() because those are Option types // min/max of the period. unwrap() because those are Option types
let period_max: f64 = max(&closes).unwrap(); let period_max: f64 = max(&closes).unwrap();
let period_min: f64 = min(&closes).unwrap(); let period_min: f64 = min(&closes).unwrap();
let last_price = *closes.last().unwrap_or(&0.0); let last_price = *closes.last().unwrap_or(&0.0);
let (_, pct_change) = price_diff(&closes).unwrap_or((0.0, 0.0)); let (_, pct_change) = price_diff(&closes).unwrap_or((0.0, 0.0));
let sma = n_window_sma(30, &closes).unwrap_or_default(); let sma = n_window_sma(30, &closes).unwrap_or_default();
// a simple way to output CSV data // a simple way to output CSV data
println!( println!(