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"
dependencies = [
"futures-core",
"futures-io",
"futures-task",
"memchr",
"pin-project-lite",
"pin-utils",
"slab",
]
[[package]]

View file

@ -9,5 +9,5 @@ version = "0.1.0"
[dependencies]
chrono = { version = "0.4", features = ["serde"] }
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"] }

View file

@ -20,7 +20,6 @@ struct Opts {
/// A trait to provide a common interface for all signal calculations.
///
trait AsyncStockSignal {
///
/// 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.
///
fn fetch_closing_data(
async fn fetch_closing_data(
symbol: &str,
beginning: &DateTime<Utc>,
end: &DateTime<Utc>,
@ -106,6 +105,7 @@ fn fetch_closing_data(
let response = provider
.get_quote_history(symbol, *beginning, *end)
.await
.map_err(|_| Error::from(ErrorKind::InvalidData))?;
let mut quotes = response
.quotes()
@ -127,14 +127,14 @@ async fn main() -> std::io::Result<()> {
// a simple way to output a CSV header
println!("period start,symbol,price,change %,min,max,30d avg");
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() {
// min/max of the period. unwrap() because those are Option types
let period_max: f64 = max(&closes).unwrap();
let period_min: f64 = min(&closes).unwrap();
let last_price = *closes.last().unwrap_or(&0.0);
let (_, pct_change) = price_diff(&closes).unwrap_or((0.0, 0.0));
let sma = n_window_sma(30, &closes).unwrap_or_default();
// min/max of the period. unwrap() because those are Option types
let period_max: f64 = max(&closes).unwrap();
let period_min: f64 = min(&closes).unwrap();
let last_price = *closes.last().unwrap_or(&0.0);
let (_, pct_change) = price_diff(&closes).unwrap_or((0.0, 0.0));
let sma = n_window_sma(30, &closes).unwrap_or_default();
// a simple way to output CSV data
println!(