Introduction to CCXT with Alpaca


CCXT (CryptoCurrency eXchange Trading) is an open source library that lets you connect to dozens of cryptocurrency brokerages. These brokerages can be very different from each other, with different asset offerings and API services, often times making it difficult to transition from one brokerage to another. CCXT aims to make the transition easier by implementing the same interface across all brokerages on its platform. It does this by implementing a set of methods (e.g. createOrder, cancelOrder, etc.) for each brokerage by using the broker’s API endpoints to fulfill the requirements of the method.
Alpaca’s crypto services are in the process of being integrated with CCXT. You can follow integration at PR #10625. If you already use CCXT or are interested in exploring CCXT’s interface, you'll be able to connect to your Alpaca brokerage account using your API keys. For now, we’ve released our own light version of CCXT that you can use to get started. This light version of CCXT named Alpaca CCXT only contains Alpaca Exchange and will allow you to learn about CCXT while it's being integrated.
Getting Started with CCXT
Installation
We'll use Alpaca CCXT, a light version of CCXT that contains only Alpaca Exchange. Once Alpaca is fully integrated with CCXT, you will be able to use the CCXT library to trade with Alpaca.
Use the following command to install Alpaca CCXT:
pip install alpaca-ccxt
Connecting Your Alpaca Brokerage Account
To start using your Alpaca brokerage account with CCXT, create an instance of the Alpaca exchange and supply it with your API keys.
import alpaca_ccxt as ccxt
API_KEY = "YOUR_ALPACA_API_KEY"
API_SECRET = "YOUR ALPACA_API_SECRET"
alpaca = ccxt.alpaca(({
apiKey: API_KEY ,
secret: API_SECRET,
})
// If we want to use paper api keys, enable sandbox mode
alpaca.setSandboxMode(true);
Accessing Data
Market Data
Use the fetch_markets()
method to retrieve all tradable securities and their properties.
markets = alpaca.fetch_markets()
You can also retrieve the latest market data for a specific ticker using fetch_ticker()
.
ticker = alpaca.fetch_ticker("BTCUSD")
Retrieve the latest trades using fetch_trades()
. You can supply a starting timestamp and a data limit to control the data you retrieve. If the since parameter is left out, you will receive the latest trades, and if the limit parameter is left out, there will be no cap on the number of trades received.
trades = alpaca.fetch_trades("BTCUSD", since=1637657082, limit=50)
Account Data
You can also retrieve data about your account currency balances using the fetch_balance()
.
This method will show how much of each currency (USD, BTC, ETH, etc) you own.
balances = alpaca.fetch_balance()
Creating and Managing Orders
Creating a New Order
CCXT’s method for creating a new order is very similar to Alpaca’s method. To submit an order, you will need to provide a symbol, order type, order side, quantity, and price if the order type is ‘limit’. The method will return details about the order, including its order id, which you will need if you want to manage the order.
order = alpaca.create_order("BTCUSD", "market", "buy", 0.01)
Canceling an Order
Use an existing order’s id to submit a request to cancel that order. Bear in mind that it isn't always possible to cancel an order. If the order is already filled, or if there is already a cancel request pending, the cancel request will not be successful.
order_id = order.id;
alpaca.cancel_order(order_id);
Accessing Open Orders
When managing orders, it’s helpful to know details about your open orders. This way you can easily cancel certain orders.
open_orders = alpaca.fetch_open_orders()
Conclusion
CCXT offers a great way to access Alpaca’s APIs. You can use it to transition between Alpaca and other crypto trading providers. Note that our crypto is in beta and as new updates come out, new functionalities may become available through CCXT. We’ll update this post as we add new features to Alpaca on CCXT.
This article is for educational and informational purposes only. All screenshots are for illustrative purposes only.
Cryptocurrency is highly speculative in nature, involves a high degree of risks, such as volatile market price swings, market manipulation, flash crashes, and cybersecurity risks. Cryptocurrency is not regulated or is lightly regulated in most countries. Cryptocurrency trading can lead to large, immediate and permanent loss of financial value. You should have appropriate knowledge and experience before engaging in cryptocurrency trading. For additional information please click here.
Securities brokerage services are provided by Alpaca Securities LLC ("Alpaca Securities"), member FINRA/SIPC, a wholly-owned subsidiary of AlpacaDB, Inc. Technology and services are offered by AlpacaDB, Inc.
Cryptocurrency services are provided by Alpaca Crypto LLC ("Alpaca Crypto"), a wholly-owned subsidiary of AlpacaDB, Inc. Alpaca Crypto is not a member of SIPC or FINRA. Cryptocurrencies are not stocks and your cryptocurrency investments are not protected by either FDIC or SIPC.
This is not an offer, solicitation of an offer, or advice to buy or sell securities or cryptocurrencies, or open a brokerage account or cryptocurrency account in any jurisdiction where Alpaca Securities or Alpaca Crypto respectively, are not registered.
Alpaca Learn | Developer-First API for Crypto and Stocks Newsletter
Join the newsletter to receive the latest updates in your inbox.