Alpa.Crypto.Trading (AlpaEx v1.0.3)

View Source

Cryptocurrency trading operations for the Alpaca Trading API.

Alpaca supports commission-free crypto trading for select pairs.

Supported Pairs

Common pairs include BTC/USD, ETH/USD, and others. Use Alpa.assets(asset_class: "crypto") to get the full list.

Notes

  • Crypto trades 24/7
  • Fractional trading is supported
  • Order types: market, limit
  • Time in force: gtc, ioc, fok

Summary

Functions

Get a specific crypto asset.

Get all available crypto assets.

Buy crypto with a market order.

Buy crypto by dollar amount.

Place a crypto order.

Get a specific crypto position.

Get all crypto positions.

Sell crypto with a market order.

Sell crypto by dollar amount.

Functions

asset(symbol, opts \\ [])

@spec asset(
  String.t(),
  keyword()
) :: {:ok, Alpa.Models.Asset.t()} | {:error, Alpa.Error.t()}

Get a specific crypto asset.

Examples

iex> Alpa.Crypto.Trading.asset("BTC/USD")
{:ok, %Alpa.Models.Asset{...}}

assets(opts \\ [])

@spec assets(keyword()) :: {:ok, [Alpa.Models.Asset.t()]} | {:error, Alpa.Error.t()}

Get all available crypto assets.

Examples

iex> Alpa.Crypto.Trading.assets()
{:ok, [%Alpa.Models.Asset{class: :crypto, symbol: "BTC/USD", ...}]}

buy(symbol, qty, opts \\ [])

@spec buy(String.t(), String.t(), keyword()) ::
  {:ok, Alpa.Models.Order.t()} | {:error, Alpa.Error.t()}

Buy crypto with a market order.

Examples

# Buy 0.01 BTC
iex> Alpa.Crypto.Trading.buy("BTC/USD", "0.01")
{:ok, %Alpa.Models.Order{...}}

buy_notional(symbol, amount, opts \\ [])

@spec buy_notional(String.t(), String.t(), keyword()) ::
  {:ok, Alpa.Models.Order.t()} | {:error, Alpa.Error.t()}

Buy crypto by dollar amount.

Examples

# Buy $50 worth of ETH
iex> Alpa.Crypto.Trading.buy_notional("ETH/USD", "50")
{:ok, %Alpa.Models.Order{...}}

place_order(params)

@spec place_order(keyword()) ::
  {:ok, Alpa.Models.Order.t()} | {:error, Alpa.Error.t()}

Place a crypto order.

Required Parameters

  • :symbol - Crypto pair symbol (e.g., "BTC/USD")
  • :side - "buy" or "sell"
  • :type - "market" or "limit"
  • :time_in_force - "gtc", "ioc", or "fok"

Quantity (one required)

  • :qty - Quantity in base currency (e.g., 0.5 for 0.5 BTC)
  • :notional - Dollar amount to trade

Optional Parameters

  • :limit_price - Required for limit orders
  • :client_order_id - Custom order ID

Examples

# Buy $100 worth of Bitcoin
iex> Alpa.Crypto.Trading.place_order(
...>   symbol: "BTC/USD",
...>   notional: "100",
...>   side: "buy",
...>   type: "market",
...>   time_in_force: "gtc"
...> )
{:ok, %Alpa.Models.Order{...}}

# Limit order to buy 0.1 BTC at $40,000
iex> Alpa.Crypto.Trading.place_order(
...>   symbol: "BTC/USD",
...>   qty: "0.1",
...>   side: "buy",
...>   type: "limit",
...>   limit_price: "40000",
...>   time_in_force: "gtc"
...> )
{:ok, %Alpa.Models.Order{...}}

position(symbol, opts \\ [])

@spec position(
  String.t(),
  keyword()
) :: {:ok, Alpa.Models.Position.t()} | {:error, Alpa.Error.t()}

Get a specific crypto position.

Examples

iex> Alpa.Crypto.Trading.position("BTC/USD")
{:ok, %Alpa.Models.Position{...}}

positions(opts \\ [])

@spec positions(keyword()) ::
  {:ok, [Alpa.Models.Position.t()]} | {:error, Alpa.Error.t()}

Get all crypto positions.

Examples

iex> Alpa.Crypto.Trading.positions()
{:ok, [%Alpa.Models.Position{...}]}

sell(symbol, qty, opts \\ [])

@spec sell(String.t(), String.t(), keyword()) ::
  {:ok, Alpa.Models.Order.t()} | {:error, Alpa.Error.t()}

Sell crypto with a market order.

Examples

iex> Alpa.Crypto.Trading.sell("BTC/USD", "0.01")
{:ok, %Alpa.Models.Order{...}}

sell_notional(symbol, amount, opts \\ [])

@spec sell_notional(String.t(), String.t(), keyword()) ::
  {:ok, Alpa.Models.Order.t()} | {:error, Alpa.Error.t()}

Sell crypto by dollar amount.

Examples

iex> Alpa.Crypto.Trading.sell_notional("ETH/USD", "50")
{:ok, %Alpa.Models.Order{...}}