extract output_csv_line

This commit is contained in:
Paul Campbell 2024-03-31 18:26:15 +01:00
parent 45841db4ed
commit 15d919e62c

View file

@ -118,18 +118,7 @@ async fn fetch_closing_data(
}
}
#[async_std::main]
async fn main() -> std::io::Result<()> {
let opts = Opts::parse();
let from: DateTime<Utc> = opts.from.parse().expect("Couldn't parse 'from' date");
let to = Utc::now();
// 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).await?;
if !closes.is_empty() {
// min/max of the period. unwrap() because those are Option types
fn output_csv_line(closes: Vec<f64>, from: DateTime<Utc>, symbol: &str) {
let period_max: f64 = max(&closes).unwrap();
let period_min: f64 = min(&closes).unwrap();
let last_price = *closes.last().unwrap_or(&0.0);
@ -148,6 +137,20 @@ async fn main() -> std::io::Result<()> {
sma.last().unwrap_or(&0.0)
);
}
#[async_std::main]
async fn main() -> std::io::Result<()> {
let opts = Opts::parse();
let from: DateTime<Utc> = opts.from.parse().expect("Couldn't parse 'from' date");
let to = Utc::now();
// 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).await?;
if !closes.is_empty() {
output_csv_line(closes, from, symbol);
}
}
Ok(())
}