Trading API
Alpaca offers brokerage services for equities and crypto. Equity trading is commission free while crypto trading fees are tiered. Alpaca-py allows you to place orders and manage your positions on your Alpaca brokerage account.
Paper Trading
Alpaca offers a paper trading sandbox environment so you can test out the API or paper trade your strategy before you go live. The paper trading environment is free to use. You can learn more about paper trading here.
To use paper trading, you will need to set the paper parameter to True when instantiating the TradingClient. Make sure the keys you are providing correspond to a paper account.
Example
from alpaca.trading.client import TradingClient
# paper=True enables paper trading
trading_client = TradingClient('api-key', 'secret-key', paper=True)
Retrieving Account Details
You can access details about your brokerage account like how much buying power you have, whether you’ve been flagged by as a pattern day trader, your total equity. You can learn more about the Account model here.
Example
from alpaca.trading.client import TradingClient
trading_client = TradingClient('api-key', 'secret-key')
account = trading_client.get_account()
Assets
The assets API serves a list of assets available on Alpaca for trading and data consumption. It is important to note that not all assets are tradable on Alpaca, and those assets will be marked with tradable=False
. You can learn more about Assets, click here.
Getting All Assets
Retrieves a list of assets that matches the search parameters. If there is not any search parameters provided, a list of all available assets will be returned. Search parameters for assets are defined by the GetAssetsRequest
model, which allows filtering by AssetStatus
, AssetClass
, and AssetExchange
.
Example
from alpaca.trading.client import TradingClient
from alpaca.trading.requests import GetAssetsRequest
from alpaca.trading.enums import AssetClass
trading_client = TradingClient('api-key', 'secret-key')
# search for crypto assets
search_params = GetAssetsRequest(asset_class=AssetClass.CRYPTO)
assets = trading_client.get_all_assets(search_params)
Orders
The orders API allows you to submit orders and then manage those orders. You can customize your order with various order types, order time in forces or by creating multi-leg orders. You can learn more about order types at Alpaca here.
Creating an Order
To create on order on Alpaca-py you must use an OrderRequest
object. There are different OrderRequest
objects based on the type of order you want to make. For market orders, there is MarketOrderRequest
, limit orders have LimitOrderRequest
, stop orders StopOrderRequest
, and trailing stop orders have TrailingStopOrderRequest
. Each order type have their own required parameters for a successful order.
Hint
For stocks, the notional parameter can only be used with Market orders. For crypto, the notional parameter can be used with any order type.
Market Order
A market order is an order to buy or sell a stock at the best available price. Generally, this type of order will be executed immediately. However, the price at which a market order will be executed is not guaranteed.
Market orders allow the trade of fractional shares for stocks. Fractional shares must be denoted either with a non-integer qty
value or with the use of the notional
parameter. The notional
parameter allows you to denote the amount you wish to trade in units of the quote currency. For example, instead of trading 1 share of SPY, we can trade $200 of SPY. notional
orders are inherently fractional orders.
Example
from alpaca.trading.client import TradingClient
from alpaca.trading.requests import MarketOrderRequest
from alpaca.trading.enums import OrderSide, TimeInForce
trading_client = TradingClient('api-key', 'secret-key', paper=True)
# preparing orders
market_order_data = MarketOrderRequest(
symbol="SPY",
qty=0.023,
side=OrderSide.BUY,
time_in_force=TimeInForce.DAY
)
# Market order
market_order = trading_client.submit_order(
order_data=market_order_data
)
Limit Order
A limit order is an order to buy or sell a stock at a specific price or better. You can use the LimitOrderRequest
model to prepare your order details.
Example
from alpaca.trading.client import TradingClient
from alpaca.trading.requests import LimitOrderRequest
from alpaca.trading.enums import OrderSide, TimeInForce
trading_client = TradingClient('api-key', 'secret-key', paper=True)
limit_order_data = LimitOrderRequest(
symbol="BTC/USD",
limit_price=17000,
notional=4000,
side=OrderSide.SELL,
time_in_force=TimeInForce.FOK
)
# Limit order
limit_order = trading_client.submit_order(
order_data=limit_order_data
)
Getting All Orders
We can attempt to cancel all open orders with this method. The method takes no parameters and returns a list of CancelOrderResponse
objects. The cancellation of an order is not guaranteed. The CancelOrderResponse
objects contain information about the cancel status of each attempted order cancellation.
Example
from alpaca.trading.client import TradingClient
trading_client = TradingClient('api-key', 'secret-key', paper=True)
# attempt to cancel all open orders
cancel_statuses = trading_client.cancel_orders()
Positions
The positions endpoints lets you track and manage open positions in your portfolio. You can learn more about the Position model here.
Getting All Positions
This method requires no parameters and returns all open positions in your portfolio. It will return a list of Position objects.
Example
from alpaca.trading.client import TradingClient
trading_client = TradingClient('api-key', 'secret-key')
trading_client.get_all_positions()
Close All Positions
This method closes all your open positions. If you set the cancel_orders
parameter to True, the method will also cancel all open orders, preventing you from entering into a new position.
Example
from alpaca.trading.client import TradingClient
trading_client = TradingClient('api-key', 'secret-key')
# closes all position AND also cancels all open orders
trading_client.close_all_positions(cancel_orders=True)
Streaming Trade Updates
There is also a TradingStream
websocket client which allows you to stream order updates. Whenever an order is submitted, filled, cancelled, etc, you will receive a response on the client.
Example
from alpaca.trading.stream import TradingStream
trading_stream = TradingStream('api-key', 'secret-key', paper=True)
async def update_handler(data):
# trade updates will arrive in our async handler
print(data)
# subscribe to trade updates and supply the handler as a parameter
trading_stream.subscribe_trade_updates(update_handler)
# start our websocket streaming
trading_stream.run()
Updated 19 days ago