Alpa.Stream.TradeUpdates (AlpaEx v1.0.3)

View Source

WebSocket stream for real-time trade updates from the Alpaca Trading API.

This module provides a GenServer-based WebSocket client that streams order fills, partial fills, cancellations, and other trade events.

Usage

# Start the stream with a callback function
{:ok, pid} = Alpa.Stream.TradeUpdates.start_link(
  callback: fn event -> IO.inspect(event, label: "Trade Update") end
)

# Or use a module callback
{:ok, pid} = Alpa.Stream.TradeUpdates.start_link(
  callback: {MyModule, :handle_trade_update, []}
)

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

Events

Trade update events include:

  • new - Order has been received
  • fill - Order has been completely filled
  • partial_fill - Order has been partially filled
  • canceled - Order has been canceled
  • expired - Order has expired
  • replaced - Order has been replaced
  • rejected - Order has been rejected
  • pending_new - Order is pending acceptance
  • pending_cancel - Order cancellation is pending
  • pending_replace - Order replacement is pending

Summary

Functions

Returns the current connection status.

Start the trade updates WebSocket stream.

Stop the trade updates stream.

Types

callback()

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

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 trade updates WebSocket stream.

Options

  • :callback - Required. Function or MFA tuple to handle trade update events
  • :api_key - API key (uses config if not provided)
  • :api_secret - API secret (uses config if not provided)
  • :use_paper - Use paper trading endpoint (default: true)
  • :name - Optional GenServer name for the process

Examples

# With anonymous function
{:ok, pid} = Alpa.Stream.TradeUpdates.start_link(
  callback: fn event -> process_event(event) end
)

# With MFA tuple
{:ok, pid} = Alpa.Stream.TradeUpdates.start_link(
  callback: {MyHandler, :handle_event, [extra_arg]}
)

stop(pid)

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

Stop the trade updates stream.