Alpa.Stream.TradeUpdates (AlpaEx v1.0.3)
View SourceWebSocket 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 receivedfill- Order has been completely filledpartial_fill- Order has been partially filledcanceled- Order has been canceledexpired- Order has expiredreplaced- Order has been replacedrejected- Order has been rejectedpending_new- Order is pending acceptancepending_cancel- Order cancellation is pendingpending_replace- Order replacement is pending
Summary
Functions
Returns the current connection status.
Start the trade updates WebSocket stream.
Stop the trade updates stream.
Types
Functions
@spec connection_status(pid()) :: :connected | :disconnected | :connecting
Returns the current connection status.
Possible values: :connected, :disconnected, :connecting
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]}
)
@spec stop(pid()) :: :ok
Stop the trade updates stream.