Alpaca API is a cutting-edge platform created by Alpaca Group, a US-based stock brokerage firm. It provides users with a robust infrastructure to develop algorithms for buying and selling stocks, cryptocurrencies, and other financial services. With Alpaca's API, users can send orders directly to Alpaca's servers to automate the trading process, bypassing traditional clients.
One of the key benefits of Alpaca is its supervision by the Securities and Exchange Commission (SEC) and the Financial Industry Regulatory Authority (FINRA), ensuring maximum security and protection of client's funds. Moreover, the Securities Investor Protection Corporation (SIPC) safeguards clients' funds and securities, utilizing the company's strategic backup plan in case of the broker's bankruptcy or inability to meet its obligations. Experience automated trading with peace of mind, only with Alpaca API.
If you're considering using the Alpaca Trading API, it's essential to weigh the pros and cons to decide if it's the right choice for you. To assist you in making an informed decision, we've assembled a table highlighting the advantages and disadvantages of using the Alpaca Trading API. This table provides a comprehensive overview of the benefits and drawbacks of Alpaca Trading API, allowing you to make an informed decision that suits your needs.
Pros | Cons |
---|---|
Commission-free. | Customer support is limited to email. |
Excellent charting tools. | Limited options for deposits and withdrawals. |
Allows the construction of an automated trading system. | Only US stocks and ETFs (Exchange Traded Funds). |
Operations are subject to supervision by both the SEC and FINRA. | Complex process to integrate third-party tools. |
Integration with different platforms. | Cell 2x5 |
If you're considering using Alpaca Trading API, you might also be interested in exploring other options. To help you compare and evaluate them, we've compiled a table that outlines the features and specifications of various trading APIs, including Alpaca. With this comparison, you can easily identify the strengths and limitations of each API, and make an informed decision on which one is right for you.
alpaca-trading | moomoo | webull | sofi-invest | robinhood | |
---|---|---|---|---|---|
Overall score | 4.7 | 4.7 | 4.8 | 4.7 | 4.5 |
Fees score | 4.8 | 4.7 | 4.8 | 5.0 | 4.6 |
Account opening score | 4.8 | 4.2 | 5.0 | 4.2 | 5.0 |
Deposit and withdrawal score | 1.8 | 1.9 | 1.9 | 2.2 | 1.8 |
Web platform score | 3.7 | 0.0 | 5.0 | 3.2 | 4.5 |
Markets and products score | 2.6 | 2.4 | 2.9 | 2.2 | 2.9 |
Founded in | 2015 | 2018 | 2017 | 2011 | 2013 |
Headquarter | US | US | US | US | US |
Investor protection | $500,000 | $500,000 | $500,000 | $500,000 | $500,000 |
Zero commission | yes | yes | yes | yes | yes |
Regulated by | sipc.org finra.org | sec.gov sipc.org finra.org | sec.gov sfc.hk | finra.org sec.gov | finra.org sec.gov |
Main services | CryptoΒ trading StockΒ trading | StockΒ trading | StockΒ trading CryptoΒ trading RecurringΒ Investment | StocksΒ trading | StocksΒ trading CashΒ Management |
Alpaca Trading API has gained popularity among developers and financial services providers due to its ease of use and flexible interface that enables the creation of automated trading systems. In this section, we showcase some success stories of apps and platforms that have integrated Alpaca Trading API to provide trading and financial services to their users. These examples highlight the diverse ways in which Alpaca Trading API can be utilized to create innovative and successful trading solutions.
algotraders.ai | This platform performs algorithmic trading, using the Alpaca API, to allow its users to automate their trading strategies. |
quantconnect.com | This is a backtesting and trading strategy development platform that uses the Alpaca API to provide real-time data and order execution for buying and selling. |
tradingview.com | This trading technical analysis platform uses the Alpaca API to allow users to trade directly from their charts and trading tools. |
In this section, we'll provide a step-by-step guide on how to get started with Alpaca API using NodeJS. We'll cover how to set up a paper-only account and a Broker API account, the two alternatives to interact and consume the Alpaca resources. Whether you're a beginner or an experienced developer, our guide will help you get started with Alpaca API quickly and easily.
Paper trading is a type of account that provides a real-time simulation environment where it's possible to test your code. You can reset and test your algorithm as much as you want using free, real-time market data.
To create a Paper Only Account and integrate it with an app itβs necessary to follow the next steps:
Register to the Alpaca platform through this URL: https://alpaca.markets/
After creating and verifying the account, you should log in and navigate to the operations dashboard.
Once in the dashboard, locate the API Keys tab and click on the 'Generate', 'New Keyβ or 'Regenerateβ buttons. Save these credentials as you won't be able to visualize them again.
After generating and saving the credentials, you will be able to integrate Alpaca's API with an application. To this end, Alpaca provides an SDK for Python, Javascript C# and Go.
npm install --save @alpacahq/alpaca-trade-api
After installing the library, it must be imported into the service where it will be used in order to create a new instance, for which the previously saved credentials must be provided.
import Alpaca from '@alpacahq/alpaca-trade-api';
const alpaca = new Alpaca({
paper: true,
secretKey: process.env.SECRET_KEY,
API: process.env.API_KEY,
});
At this point, Alpaca API resources can be consumed, and the information can be used according to the application logic. In this case, the resources have been associated with services exposed through a NodeJS API.
These services can be consumed in different ways, for example using Postman or in some cases directly from a web browser. For this example, the REST Client extension of Visual Studio Code is used, for which only a file with the http extension needs to be created within the application. From this file, all calls to the API can be made using the following structure.
@api = http://localhost:3000/api/trading
### get account info
GET {{api}}/account
The Javascript SDK provides a large number of functions that allow you to consume a wide variety of resources from the Alpaca API, which can be used to automate trading based on this information and the business logic that is implemented.
export const getAccount = async () => {
return await alpaca.getAccount();
};
{
"id": ee302827-4ced-4321-b5fb-71080392d828
"account_number": PA3717PJAYWN
"status": ACTIVE
"crypto_status": ACTIVE
"currency": USD
"buying_power": 1768290.404988
"regt_buying_power": 1768290.404988
"daytrading_buying_power": 0
"non_marginable_buying_power": 882145.2
"cash": 884145.202494
"accrued_fees": 0
"pending_transfer_out": None
"pending_transfer_in": 0
"portfolio_value": 910178.2209985875
"pattern_day_trader": False
"trading_blocked": False
"transfers_blocked": False
"account_blocked": False
"created_at": 2022-04-19 17:46:03.685850+00:00
"trade_suspended_by_user": False
"multiplier": 2
"shorting_enabled": True
"equity": 910178.2209985875
"last_equity": 907426.98
"long_market_value": 26033.0185045875
"short_market_value": 0
"initial_margin": 0
"maintenance_margin": 0
"last_maintenance_margin": 0
"sma": 868738.98
"daytrade_count": 0
}
export const getSymbolTrades = async (params: SymbolParams) => {
const { symbol, days } = params;
return await alpaca.getCryptoBars(
symbol,
{
start: getDateFrom(days),
end: new Date(),
timeframe: "1Day",
},
);
};
{
"symbol": "BTCUSD",
"timestamp": "2022-07-07T05:00:00Z",
"exchange": "CBSE",
"open": 20400.28,
"high": 20445.5,
"low": 20224.8,
"close": 20404.32,
"volume": 1029.86433895,
"tradecount": 43372,
"VWAP": 20350.5220346576
}
export const getPositionsStatus = async () => {
return await alpaca.getPositions();
};
{
"asset_id": 64bbff51 - 59d6 - 4b3c - 9351 - 13ad85e3c752
"symbol": BTCUSD
"exchange": FTXU
"asset_class": crypto
"avg_entry_price": 20983
"qty": 0.9975
"side": long
"market_value": 20928.5475
"cost_basis": 20930.5425
"unrealized_pl": -1.995
"unrealized_plpc": -0.0000953152552066
"unrealized_intraday_pl": -1.995
"unrealized_intraday_plpc": -0.0000953152552066
"current_price": 20981
"lastday_price": 19344
"change_today": 0.084625723738627
}
export const getAssets = async (params: AssetsParams) => {
return await alpaca.getAssets(params);
};
{
"id": "276e2673-764b-4ab6-a611-caf665ca6340",
"class": "crypto",
"exchange": "FTXU",
"symbol": "BTC/USD",
"name": "BTC/USD pair",
"status": "active",
"tradable": true,
"marginable": false,
"shortable": false,
"easy_to_borrow": false,
"fractionable": true,
"min_order_size": "0.0001",
"min_trade_increment": "0.0001",
"price_increment": "1"
}
export const createOrder = async (orderParams: any) => {
return await alpaca.createOrder(orderParams);
};
//example
{
"symbol": "BTC/USD",
"qty": 1,
"side": "buy",
"type": "market",
"time_in_force": "gtc"
}
{
"id": "5cdabade-aded-47be-9ef1-489695bfa39d",
"client_order_id": "8fb819c8-98c5-4609-861c-d9bd6bd05b7c",
"created_at": "2023-01-09T22:30:04.085727Z",
"updated_at": "2023-01-09T22:30:04.097252Z",
"submitted_at": "2023-01-09T22:30:04.085201Z",
"filled_at": "2023-01-09T22:30:04.08896Z",
"expired_at": null,
"canceled_at": null,
"failed_at": null,
"replaced_at": null,
"replaced_by": null,
"replaces": null,
"asset_id": "276e2673-764b-4ab6-a611-caf665ca6340",
"symbol": "BTC/USD",
"asset_class": "crypto",
"notional": null,
"qty": "1",
"filled_qty": "1",
"filled_avg_price": "17238.291649",
"order_class": "",
"order_type": "market",
"type": "market",
"side": "buy",
"time_in_force": "gtc",
"limit_price": null,
"stop_price": null,
"status": "filled",
"extended_hours": false,
"legs": null,
"trail_percent": null,
"trail_price": null,
"hwm": null,
"subtag": null,
"source": "access_key"
}
export const getOrders = async (params: any) => {
if (params.orderId) {
return await getOrderById(params.orderId);
}
return alpaca.getOrders(params);
};
[
{
"id": "5cdabade-aded-47be-9ef1-489695bfa39d",
"client_order_id": "8fb819c8-98c5-4609-861c-d9bd6bd05b7c",
"created_at": "2023-01-09T22:30:04.085727Z",
"updated_at": "2023-01-09T22:30:04.097252Z",
"submitted_at": "2023-01-09T22:30:04.085201Z",
"filled_at": "2023-01-09T22:30:04.08896Z",
"expired_at": null,
"canceled_at": null,
"failed_at": null,
"replaced_at": null,
"replaced_by": null,
"replaces": null,
"asset_id": "276e2673-764b-4ab6-a611-caf665ca6340",
"symbol": "BTC/USD",
"asset_class": "crypto",
"notional": null,
"qty": "1",
"filled_qty": "1",
"filled_avg_price": "17238.291649",
"order_class": "",
"order_type": "market",
"type": "market",
"side": "buy",
"time_in_force": "gtc",
"limit_price": null,
"stop_price": null,
"status": "filled",
"extended_hours": false,
"legs": null,
"trail_percent": null,
"trail_price": null,
"hwm": null,
"subtag": null,
"source": "access_key"
}
]
The Alpaca API provides other Real-Time options, which can be consumed in real-time through web sockets. These options allow you to subscribe to different types of resources and receive instant updates, enabling you to make timely decisions regarding market behavior. To use the Real-Time resources the Alpaca API has to offer you need to follow these steps:
const alpaca = new Alpaca({
paper: true,
});
const websocket = alpaca.crypto_stream_v2;
websocket.onConnect(() => {
websocket.subscribeForTrades(["AAPL"]);
});
websocket.onStateChange((status) => {
console.log("Crypto stream status:", status);
});
websocket.onError((err) => {
console.log("Error:", err);
});
websocket.onCryptoBar((bar) => {
console.log("Crypto Bar:", bar);
});
websocket.onCryptoDailyBar((bar) => {
console.log("Crypto Daily Bar:", bar);
});
websocket.onCryptoOrderbook((order) => {
console.log("Crypto Order Book:", order);
});
websocket.onCryptoQuote((quote) => {
console.log("Crypto Quote:", quote);
});
websocket.onCryptoTrade((trade) => {
console.log("Crypto Trade:", trade);
});
websocket.onCryptoUpdatedBar((bar) => {
console.log("Crypto updated bar:", bar);
});
// crypto_stream_v2
subscribe(symbols: {
trades?: Array<string>;
quotes?: Array<string>;
bars?: Array<string>;
updatedBars?: Array<string>;
dailyBars?: Array<string>;
orderbooks?: Array<string>;
}): void;
// data_stream_v2
subscribe(symbols: {
trades?: Array<string>;
quotes?: Array<string>;
bars?: Array<string>;
updatedBars?: Array<string>;
dailyBars?: Array<string>;
statuses?: Array<string>;
lulds?: Array<string>;
}): void;
In addition to these two WebSocket clients, there are also the news_stream and data_ws clients, which provide other types of real-time information.
The above functions are some of the many options provided by the SDK. Depending on the needs and logic of the application, you can choose to use the one that meets the requirements. These resources can be consulted in the API documentation.
The Broker API Account provided by Alpaca allows you to create trading applications with various tools for users. Alpaca provides an isolated environment with all the necessary tools and information to implement and test all the functionalities a broker app typically offers. This isolated testing environment has the same capabilities as the real environment, except for a few functionalities that require real money.
Depending on the type of application you want to build, there are three use cases for creating applications: Trading/investing app, Broker dealer (fully-disclosed, non-disclosed, omnibus), and Registered Investment Advisor (RIA). Depending on the case, specific API resources are used.
To create a Broker API Account and integrate it with an app itβs necessary to follow these steps:
const generateAuthToken = () => {
return Buffer.from(`${BROKER_API_KEY}:${BROKER_SECRET}`, 'utf8').toString('base64');
};
const callAPI = async (endpoint: string, method = 'get', params = {}, data = {}) => {
const response = await axios({
method,
url: `https://broker-api.sandbox.alpaca.markets/v1/${endpoint}`,
params,
data,
headers: {
'Authorization': `Basic ${generateAuthToken()}`,
'content-type': 'application/json',
}
});
if (!response.data) {
return response;
}
return response.data;
};
After having the base configuration to communicate with the API, and having chosen and explored the services to be used, the implementation begins. In this case, services were enforced for the creation and consultation of client users, and consultation of assets and commercial events.
This resource is accessible at the endpoint: https://broker-api.sandbox.alpaca.markets/v1/accounts
export const createNewAccount = async (params: UserInfo) => {
return callAPI('accounts', 'post', {}, params);
};
{
"contact": {
"email_address": "[email protected]",
"phone_number": "555-666-7788",
"street_address": ["20 N San Mateo Dr"],
"city": "San Mateo",
"state": "CA",
"postal_code": "94401",
"country": "USA"
},
"identity": {
"given_name": "John",
"family_name": "Doe",
"date_of_birth": "1990-01-01",
"tax_id": "666-55-4321",
"tax_id_type": "USA_SSN",
"country_of_citizenship": "USA",
"country_of_birth": "USA",
"country_of_tax_residence": "USA",
"funding_source": ["employment_income"]
},
"disclosures": {
"is_control_person": false,
"is_affiliated_exchange_or_finra": false,
"is_politically_exposed": false,
"immediate_family_exposed": false
},
"agreements": [
{
"agreement": "margin_agreement",
"signed_at": "2020-09-11T18:09:33Z",
"ip_address": "185.13.21.99"
},
{
"agreement": "account_agreement",
"signed_at": "2020-09-11T18:13:44Z",
"ip_address": "185.13.21.99"
},
{
"agreement": "customer_agreement",
"signed_at": "2020-09-11T18:13:44Z",
"ip_address": "185.13.21.99"
}
],
"documents": [
{
"document_type": "identity_verification",
"document_sub_type": "passport",
"content": "/9j/Cg==",
"mime_type": "image/jpeg"
}
],
"trusted_contact": {
"given_name": "Jane",
"family_name": "Doe",
"email_address": "[email protected]"
}
}
export const getAccounts = async (params: any) => {
let resource = 'accounts'
if (params.accountId) {
resource = `${resource}/${params.accountId}`;
}
return callAPI(resource);
};
[
{
"id": "fc1a5a44-6a9a-4919-9e4f-9e17edf9cc64",
"account_number": "961722725",
"status": "ACTIVE",
"crypto_status": "INACTIVE",
"kyc_results": {
"reject": {},
"accept": {},
"indeterminate": {},
"summary": "pass"
},
"currency": "USD",
"last_equity": "0",
"created_at": "2023-01-16T22:05:31.346989Z",
"account_type": "trading",
"enabled_assets": [
"us_equity"
]
}
]
export const getAssets = async (assetParams: AssetsParams) => {
const { symbol, status, asset_class, exchange } = assetParams;
let resource = 'assets';
const params = {};
if (symbol) {
resource = `${resource}/${symbol}`;
} else {
Object.assign(params, {
status: status || 'active',
asset_class: asset_class || 'crypto',
exchange: exchange || 'FTXU',
});
}
return callAPI(resource, 'get', params);
};
[
{
"id": "7595a8d2-68a6-46d7-910c-6b1958491f5c",
"class": "us_equity",
"exchange": "NYSE",
"symbol": "A",
"name": "Agilent Technologies Inc.",
"status": "active",
"tradable": true,
"marginable": true,
"shortable": true,
"easy_to_borrow": true,
"fractionable": true
}
]
export const getTradeEvents: Handler = async (req, res) => {
try {
const trades = await pullTradeEvents(req.query);
return res.json(trades);
} catch (error) {
return res.status(500).json(handleError(error));
}
};
{
"account_id":"c8f1ef5d-edc0-4f23-9ee4-378f19cb92a4",
"at":"2021-06-14T10:04:05.172241Z",
"event":"new",
"event_id":3014423,
"execution_id":"a829b698-da07-479f-91e6-f204c77573fc",
"order":{
"asset_class":"us_equity",
"asset_id":"b0b6dd9d-8b9b-48a9-ba46-b9d54906e415",
"canceled_at":null,
"client_order_id":"d6499947-dacd-4f85-a728-81734dc39c27",
"commission":"1.1",
"created_at":"2021-06-14T10:04:05.150255Z",
"expired_at":null,
"extended_hours":false,
"failed_at":null,
"filled_at":null,
"filled_avg_price":null,
"filled_qty":"0",
"hwm":null,
"id":"d507255a-b072-4fb7-96f3-9ad5a9c02b2a",
"legs":null,
"limit_price":null,
"notional":"10",
"order_class":"",
"order_type":"market",
"qty":null,
"replaced_at":null,
"replaced_by":null,
"replaces":null,
"side":"buy",
"status":"new",
"stop_price":null,
"submitted_at":"2021-06-14T10:04:05.0937Z",
"symbol":"AAPL",
"time_in_force":"day",
"trail_percent":null,
"trail_price":null,
"type":"market",
"updated_at":"2021-06-14T10:04:05.165088Z"
},
"timestamp": "2021-06-14T10:04:05.172241Z"
}
Launching a technological and financial solution into production involves significant responsibilities and risks, so it is essential to follow good security practices and comply with applicable regulations.
To deploy any application, different steps may be required, depending on the technology or business structure, but in general, the following points could be highlighted.
Alpaca API provides a tremendous amount of options to implement cryptocurrency trading applications. To go deeper into this world, you can check out the official documentation and the Alpaca community forums.
Alpaca has had impressive growth during the last couple of years thanks to various factors, including:
The possibilities of Alpaca API are almost endless, and whatever your fintech app is, it can surely help you with your objectives. But with great power comes great responsibility and it's important to know what the best options are for you so as not to get lost in that much information/possibilities. I encourage you to educate yourself in the Alpaca API or to hire a third party who knows the tech's ins and outs so you can have a more professional assessment.
You can get a comprehensive and honest picture of your project or product and its opportunities by jumping on a free discovery call with our team, which includes developers well-versed in this platform. Contact us here.
We hope this guide proved to be useful, any questions or comments are more than welcome. Thanks for reading!