Technical analysis in Rust: SMA and EMA from Alpha Vantage prices

Technical analysis looks at past prices and volume to find trends. Two of the most basic indicators are the simple moving average (SMA) and the exponential moving average (EMA), which smooth the daily closing price so the trend is easier to see.
This post fetches daily closes for a stock from the Alpha Vantage API and computes a 14-day SMA and EMA in plain Rust.
Project setup
Section titled “Project setup”cargo new technical_analysis_rustcd technical_analysis_rust[dependencies]reqwest = "0.13"tokio = { version = "1", features = ["full"] }serde = { version = "1", features = ["derive"] }serde_json = "1"Get a free API key and put it in an environment variable rather than in the source:
export ALPHAVANTAGE_API_KEY=your_key_hereFetching daily prices
Section titled “Fetching daily prices”The TIME_SERIES_DAILY endpoint returns JSON where the prices sit under a "Time Series (Daily)" key, as a map from date to an object with string fields like "4. close". With the default outputsize=compact, you get the latest 100 trading days; outputsize=full requires a premium key.
use std::collections::BTreeMap;use std::error::Error;
use serde::Deserialize;
#[derive(Deserialize, Debug)]struct DailyResponse { #[serde(rename = "Time Series (Daily)")] time_series: BTreeMap<String, DailyData>,}
#[derive(Deserialize, Debug)]struct DailyData { #[serde(rename = "4. close")] close: String,}
async fn fetch_daily(symbol: &str, api_key: &str) -> Result<DailyResponse, Box<dyn Error>> { let url = format!( "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol={symbol}&apikey={api_key}" );
let body = reqwest::get(&url).await?.error_for_status()?.text().await?;
// On a bad key or when you hit the rate limit, Alpha Vantage still returns // HTTP 200, but with a message instead of the time series. let parsed = serde_json::from_str(&body) .map_err(|e| format!("unexpected response ({e}): {body}"))?; Ok(parsed)}Two details matter here:
- The map is a
BTreeMap, not aHashMap. AHashMapiterates in arbitrary order, so the prices would come out shuffled and every moving average would be meaningless. The dates areYYYY-MM-DDstrings, so aBTreeMapkeeps them in chronological order. - Only
closeis declared. serde ignores the other fields (open, high, low, volume) unless you add them to the struct.
Moving averages
Section titled “Moving averages”The SMA is the plain average of the last period closes. The EMA weights recent prices more heavily using a multiplier of 2 / (period + 1). A common convention, used here, is to start the EMA from the SMA of the first period prices.
fn simple_moving_average(prices: &[f64], period: usize) -> Vec<f64> { assert!(period > 0, "period must be at least 1"); prices .windows(period) .map(|window| window.iter().sum::<f64>() / period as f64) .collect()}
fn exponential_moving_average(prices: &[f64], period: usize) -> Vec<f64> { assert!(period > 0, "period must be at least 1"); if prices.len() < period { return Vec::new(); }
let multiplier = 2.0 / (period as f64 + 1.0); let mut current = prices[..period].iter().sum::<f64>() / period as f64; let mut ema = vec![current];
for &price in &prices[period..] { current = (price - current) * multiplier + current; ema.push(current); }
ema}Both functions return prices.len() - period + 1 values, and value i belongs to the day at index i + period - 1. windows also means the last window, ending on the most recent day, is included.
Putting it together
Section titled “Putting it together”#[tokio::main]async fn main() -> Result<(), Box<dyn Error>> { let api_key = std::env::var("ALPHAVANTAGE_API_KEY")?; let symbol = "MSFT"; let period = 14;
let data = fetch_daily(symbol, &api_key).await?;
let dates: Vec<&String> = data.time_series.keys().collect(); let closes: Vec<f64> = data .time_series .values() .map(|day| day.close.parse::<f64>()) .collect::<Result<_, _>>()?;
let sma = simple_moving_average(&closes, period); let ema = exponential_moving_average(&closes, period);
// Print the five most recent days let offset = period - 1; for i in sma.len().saturating_sub(5)..sma.len() { println!( "{} close {:>8.2} SMA({period}) {:>8.2} EMA({period}) {:>8.2}", dates[i + offset], closes[i + offset], sma[i], ema[i], ); }
Ok(())}All three snippets go in src/main.rs. Run it with cargo run.
Things to watch for
Section titled “Things to watch for”- Rate limits. The free key has a small daily request quota. While developing, save the JSON response to a file and parse that, instead of calling the API on every run.
- Unadjusted prices.
TIME_SERIES_DAILYreturns raw prices, so a stock split looks like a sudden crash and drags the averages down forperioddays. The split-adjusted endpoint (TIME_SERIES_DAILY_ADJUSTED) is premium-only. f64is fine for indicators, not for money. Moving averages are approximations anyway. If you move on to tracking positions or P&L, use a decimal type such asrust_decimal.- Existing crates. The
tacrate implements SMA, EMA, RSI, MACD and others as streaming indicators, though its latest release (0.5.0) dates from 2021. Writing SMA and EMA yourself, as here, is a good way to check you understand what the library computes.
Further reading
Section titled “Further reading”© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)