Skip to content

Async stock market technical analysis tool with real-time streaming of S&P 500 indicators

License

serle/async_streams

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

7 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

async_streams

Async stock market technical analysis tool with real-time streaming of S&P 500 indicators

License: MIT Rust


Overview

async_streams is a high-performance async Rust application that fetches real-time stock market data from Yahoo Finance and calculates technical indicators for S&P 500 companies. The tool streams analysis data continuously, making it ideal for monitoring market trends and building trading strategies.

Key Features

  • πŸ“Š Real-time Data Streaming - Continuous updates every 30 seconds for S&P 500 stocks
  • πŸ“ˆ Technical Indicators - SMA (Simple Moving Average), Min/Max prices, price differences
  • ⚑ Async Performance - Built with Tokio for concurrent API calls and efficient streaming
  • πŸ’Ύ CSV Output - Structured data export for analysis and visualization
  • πŸ”„ Automatic Retry - Resilient error handling with exponential backoff
  • 🎯 Flexible Configuration - Command-line interface for custom symbol lists and date ranges

Installation

Prerequisites

  • Rust 2021 edition or later
  • Internet connection for Yahoo Finance API access

Build from Source

git clone https://github.com/serle/async_streams.git
cd async_streams
cargo build --release

Usage

Real-time Streaming Mode

Stream technical indicators for S&P 500 stocks (requires sp500.txt file with symbols):

cargo run --release

Output appears both on console and in data.csv:

period start,symbol,price,change %,min,max,30d avg
2025-01-22T10:30:00-05:00,AAPL,$150.25,2.15%,$145.00,$152.50,$148.33

Single Analysis Mode

Analyze specific symbols over a date range:

# Analyze Apple stock for the last 2 weeks (default)
cargo run --release -- --symbols AAPL

# Multiple symbols
cargo run --release -- --symbols "AAPL,MSFT,GOOG,TSLA"

# Custom date range
cargo run --release -- --symbols "AAPL,MSFT" \
  --from "2025-01-01 00:00:00 UTC" \
  --to "2025-01-31 23:59:59 UTC"

Technical Indicators

The tool calculates the following metrics for each stock:

Indicator Description
Price Current/latest closing price
Change % Percentage change from first to last price in period
Period Min Lowest price in the analysis period
Period Max Highest price in the analysis period
30d SMA 30-day Simple Moving Average

Custom Indicators

The system uses a trait-based architecture for easy extensibility:

#[async_trait]
pub trait AsyncStockSignal {
    type SignalType;

    async fn calculate(&self, data: &[f64]) -> Option<Self::SignalType>;
}

Available implementations:

  • MinPrice - Find minimum price in series
  • MaxPrice - Find maximum price in series
  • PriceDifference - Calculate absolute and relative price changes
  • WindowedSMA - Calculate simple moving average over window

Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Yahoo Finance   β”‚
β”‚      API        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
    β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚  Data Fetcher       β”‚
    β”‚  (async/await)      β”‚
    β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
    β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚ Signal Calculators  β”‚
    β”‚  - MinPrice         β”‚
    β”‚  - MaxPrice         β”‚
    β”‚  - PriceDifference  β”‚
    β”‚  - WindowedSMA      β”‚
    β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
    β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚  CSV Writer         β”‚
    β”‚  (data.csv)         β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Async Streaming Flow

  1. Load S&P 500 symbol list from sp500.txt
  2. Every 30 seconds, spawn async tasks for each symbol
  3. Fetch closing prices from Yahoo Finance API
  4. Calculate technical indicators concurrently
  5. Stream results to console and CSV file
  6. Automatic retry on transient failures (up to 5 attempts)

Configuration

S&P 500 Symbol List

Create a sp500.txt file with comma-separated stock symbols:

AAPL, MSFT, GOOGL, AMZN, META, TSLA, NVDA, JPM, V, WMT

Command-line Options

Options:
  -s, --symbols <SYMBOLS>    Comma-separated stock symbols (default: AAPL,MSFT,UBER,GOOG)
  -f, --from <FROM>          Start date (default: 2 weeks ago)
  -t, --to <TO>              End date (default: now)
  -h, --help                 Print help
  -V, --version              Print version

Testing

Run the comprehensive test suite:

# Run all tests
cargo test

# Test with Yahoo Finance API (requires internet)
cargo test -- --ignored

# Specific test
cargo test test_windowed_sma_calculate

Test coverage includes:

  • βœ… Technical indicator calculations
  • βœ… Yahoo Finance API integration
  • βœ… Date range queries
  • βœ… Error handling and edge cases
  • βœ… Streaming signal generation

Dependencies

  • tokio - Async runtime for concurrent operations
  • tokio-stream - Stream utilities for interval-based updates
  • yahoo_finance_api - Yahoo Finance data provider
  • chrono - Date/time handling
  • clap - Command-line argument parsing
  • async-trait - Async trait support
  • async-recursion - Recursive async functions for retry logic

Performance

  • Concurrent API Calls - Fetches data for multiple symbols in parallel
  • Efficient Streaming - Minimal memory footprint with async iterators
  • Rate Limiting - 30-second intervals to respect API limits
  • Resilient Retry - Exponential backoff for failed requests

Output Format

CSV Structure

period start,symbol,price,change %,min,max,30d avg
2025-01-22T10:30:00-05:00,AAPL,$150.25,2.15%,$145.00,$152.50,$148.33
2025-01-22T10:30:00-05:00,MSFT,$380.75,1.85%,$375.20,$385.00,$379.50

Console Output

Real-time updates with colored formatting showing symbol, price movements, and technical indicators.


Use Cases

  • πŸ“Š Algorithmic Trading - Feed data into trading algorithms
  • πŸ“ˆ Market Monitoring - Track S&P 500 performance in real-time
  • πŸ” Technical Analysis - Identify trends using moving averages
  • πŸ“‰ Risk Management - Monitor price volatility and ranges
  • πŸ“± Alert Systems - Build notification systems for price movements

License

This project is licensed under the MIT License - see the LICENSE file for details.


Author

Serle Shuman - serle.shuman@gmail.com


Acknowledgments

  • Built with Rust πŸ¦€ for performance and reliability
  • Powered by Yahoo Finance API
  • Async runtime provided by Tokio

About

Async stock market technical analysis tool with real-time streaming of S&P 500 indicators

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages