Alpa.Stream.MarketData (AlpaEx v1.0.3)

View Source

WebSocket stream for real-time market data from the Alpaca Market Data API.

This module provides a GenServer-based WebSocket client that streams real-time trades, quotes, and bars for subscribed symbols.

Usage

# Start the stream
{:ok, pid} = Alpa.Stream.MarketData.start_link(
  callback: fn event -> IO.inspect(event, label: "Market Data") end
)

# Subscribe to symbols
Alpa.Stream.MarketData.subscribe(pid, trades: ["AAPL", "MSFT"])
Alpa.Stream.MarketData.subscribe(pid, quotes: ["AAPL"], bars: ["SPY"])

# Unsubscribe
Alpa.Stream.MarketData.unsubscribe(pid, trades: ["MSFT"])

# Stop the stream
Alpa.Stream.MarketData.stop(pid)

Event Types

Events have a :type field indicating the data type:

  • :trade - Real-time trade data
  • :quote - Real-time quote (NBBO) data
  • :bar - Real-time minute bar data

Feeds

Available feeds:

  • "iex" - IEX exchange data (free)
  • "sip" - All US exchanges (requires subscription)

Summary

Functions

Returns the current connection status.

Start the market data WebSocket stream.

Stop the market data stream.

Subscribe to market data for symbols.

Unsubscribe from market data for symbols.

Types

callback()

@type callback() :: (map() -> any()) | {module(), atom(), list()}

subscription_type()

@type subscription_type() :: :trades | :quotes | :bars

Functions

connection_status(pid)

@spec connection_status(pid()) :: :connected | :disconnected | :connecting

Returns the current connection status.

Possible values: :connected, :disconnected, :connecting

start_link(opts)

@spec start_link(keyword()) :: {:ok, pid()} | {:error, term()}

Start the market data WebSocket stream.

Options

  • :callback - Required. Function or MFA tuple to handle market data events
  • :feed - Data feed: "iex" (default) or "sip"
  • :api_key - API key (uses config if not provided)
  • :api_secret - API secret (uses config if not provided)
  • :name - Optional GenServer name for the process

Examples

{:ok, pid} = Alpa.Stream.MarketData.start_link(
  callback: fn event -> process_event(event) end,
  feed: "iex"
)

stop(pid)

@spec stop(pid()) :: :ok

Stop the market data stream.

subscribe(pid, subscriptions)

@spec subscribe(
  pid(),
  keyword()
) :: :ok

Subscribe to market data for symbols.

Options

  • :trades - List of symbols to subscribe for trades
  • :quotes - List of symbols to subscribe for quotes
  • :bars - List of symbols to subscribe for minute bars

Examples

Alpa.Stream.MarketData.subscribe(pid, trades: ["AAPL", "MSFT"], quotes: ["AAPL"])

unsubscribe(pid, subscriptions)

@spec unsubscribe(
  pid(),
  keyword()
) :: :ok

Unsubscribe from market data for symbols.

Examples

Alpa.Stream.MarketData.unsubscribe(pid, trades: ["MSFT"])