SkillAgentSearch skills...

Alpaca Trade Api Python

Python client for Alpaca's trade API

Install / Use

npx skills add alpacahq/alpaca-trade-api-python

Installs into whichever agent you are using.

README

PyPI version CircleCI Updates Python 3

Deprecation Notice

A new python SDK, Alpaca-py, is available. This SDK will be the primary python SDK starting in 2023. We recommend moving over your code to use the new SDK. Keep in mind, we will be maintaining this repo as usual until the end of 2022.

alpaca-trade-api-python

alpaca-trade-api-python is a python library for the Alpaca Commission Free Trading API. It allows rapid trading algo development easily, with support for both REST and streaming data interfaces. For details of each API behavior, please see the online API document.

Note that this package supports only python version 3.7 and above.

Install

We support python>=3.7. If you want to work with python 3.6, please note that these package dropped support for python <3.7 for the following versions:

pandas >= 1.2.0
numpy >= 1.20.0
scipy >= 1.6.0

The solution - manually install these packages before installing alpaca-trade-api. e.g:

pip install pandas==1.1.5 numpy==1.19.4 scipy==1.5.4

Also note that we do not limit the version of the websockets library, but we advise using

websockets>=9.0

Installing using pip

$ pip3 install alpaca-trade-api

API Keys

To use this package you first need to obtain an API key. Go here to signup

Services

These services are provided by Alpaca:

The free services are limited, please check the docs to see the differences between paid/free services.

Alpaca Environment Variables

The Alpaca SDK will check the environment for a number of variables that can be used rather than hard-coding these into your scripts.<br> Alternatively you could pass the credentials directly to the SDK instances.

| Environment | default | Description | | -------------------------------- | -------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- | | APCA_API_KEY_ID=<key_id> | | Your API Key | | APCA_API_SECRET_KEY=<secret_key> | | Your API Secret Key | | APCA_API_BASE_URL=url | https://api.alpaca.markets (for live) | Specify the URL for API calls, Default is live, you must specify <br/>https://paper-api.alpaca.markets to switch to paper endpoint! | | APCA_API_DATA_URL=url | https://data.alpaca.markets | Endpoint for data API | | APCA_RETRY_MAX=3 | 3 | The number of subsequent API calls to retry on timeouts | | APCA_RETRY_WAIT=3 | 3 | seconds to wait between each retry attempt | | APCA_RETRY_CODES=429,504 | 429,504 | comma-separated HTTP status code for which retry is attempted | | DATA_PROXY_WS | | When using the alpaca-proxy-agent you need to set this environment variable as described here |

Working with Data

Historic Data

You could get one of these historic data types:

  • Bars
  • Quotes
  • Trades

You now have 2 pythonic ways to retrieve historical data.<br> One using the traditional rest module and the other is to use the experimental asyncio module added lately.<br> Let's have a look at both:<br>

The first thing to understand is the new data polling mechanism. You could query up to 10000 items, and the API is using a pagination mechanism to provide you with the data.<br> You now have 2 options:

  • Working with data as it is received with a generator. (meaning it's faster but you need to process each item alone)
  • Wait for the entire data to be received, and then work with it as a list or dataframe. We provide you with both options to choose from.

Bars

option 1: wait for the data

from alpaca_trade_api.rest import REST, TimeFrame
api = REST()

api.get_bars("AAPL", TimeFrame.Hour, "2021-06-08", "2021-06-08", adjustment='raw').df

                              open      high       low     close    volume
timestamp
2021-06-08 08:00:00+00:00  126.100  126.3000  125.9600  126.3000     42107
2021-06-08 09:00:00+00:00  126.270  126.4000  126.2200  126.3800     21095
2021-06-08 10:00:00+00:00  126.380  126.6000  125.8400  126.4900     54743
2021-06-08 11:00:00+00:00  126.440  126.8700  126.4000  126.8500    206460
2021-06-08 12:00:00+00:00  126.821  126.9500  126.7000  126.9300    385164
2021-06-08 13:00:00+00:00  126.920  128.4600  126.4485  127.0250  18407398
2021-06-08 14:00:00+00:00  127.020  127.6400  126.7800  127.1350  13446961
2021-06-08 15:00:00+00:00  127.140  127.4700  126.2101  126.6100  10444099
2021-06-08 16:00:00+00:00  126.610  126.8400  126.5300  126.8250   5289556
2021-06-08 17:00:00+00:00  126.820  126.9300  126.4300  126.7072   4813459
2021-06-08 18:00:00+00:00  126.709  127.3183  126.6700  127.2850   5338455
2021-06-08 19:00:00+00:00  127.290  127.4200  126.6800  126.7400   9817083
2021-06-08 20:00:00+00:00  126.740  126.8500  126.5400  126.6600   5525520
2021-06-08 21:00:00+00:00  126.690  126.8500  126.6500  126.6600    156333
2021-06-08 22:00:00+00:00  126.690  126.7400  126.6600  126.7300     49252
2021-06-08 23:00:00+00:00  126.725  126.7600  126.6400  126.6400     41430

option 2: iterate over bars

def process_bar(bar):
    # process bar
    print(bar)

bar_iter = api.get_bars_iter("AAPL", TimeFrame.Hour, "2021-06-08", "2021-06-08", adjustment='raw')
for bar in bar_iter:
    process_bar(bar)

Alternatively, you can decide on your custom timeframes by using the TimeFrame constructor:

from alpaca_trade_api.rest import REST, TimeFrame, TimeFrameUnit

api = REST()
api.get_bars("AAPL", TimeFrame(45, TimeFrameUnit.Minute), "2021-06-08", "2021-06-08", adjustment='raw').df

                               open      high       low     close    volume  trade_count        vwap
timestamp
2021-06-08 07:30:00+00:00  126.1000  126.1600  125.9600  126.0600     20951          304  126.049447
2021-06-08 08:15:00+00:00  126.0500  126.3000  126.0500  126.3000     21181          349  126.231904
2021-06-08 09:00:00+00:00  126.2700  126.3200  126.2200  126.2800     15955          308  126.284120
2021-06-08 09:45:00+00:00  126.2900  126.4000  125.9000  125.9000     30179          582  126.196877
2021-06-08 10:30:00+00:00  125.9000  126.7500  125.8400  126.7500    105380         1376  126.530863
2021-06-08 11:15:00+00:00  126.7300  126.8500  126.5600  126.8300    129721         1760  126.738041
2021-06-08 12:00:00+00:00  126.4101  126.9500  126.3999  126.8300    418107         3615  126.771889
2021-06-08 12:45:00+00:00  126.8500  126.9400  126.6000  126.6200    428614         5526  126.802825
2021-06-08 13:30:00+00:00  126.6200  128.4600  126.4485  127.4150  23065023       171263  127.425797
2021-06-08 14:15:00+00:00  127.4177  127.6400  126.9300  127.1350   8535068        65753  127.342337
2021-06-08 15:00:00+00:00  127.1400  127.4700  126.2101  126.7101   8447696        64616  126.789316
2021-06-08 15:45:00+00:00  126.7200  126.8200  126.5300  126.6788   5084147        38366  126.712110
2021-06-08 16:30:00+00:00  126.6799  126.8400  126.5950  126.5950   3205870        26614  126.718837
2021-06-08 17:15:00+00:00  126.5950  126.9300  126.4300  126.7010   3908283        31922  126.665727
2021-06-08 18:00:00+00:00  126.7072  127.0900  126.6700  127.0600   3923056        29114  126.939887
2021-06-08 18:45:00+00:00  127.0500  127.4200  127.0000  127.0050   5051682        38235  127.214157
2021-06-08 19:30:00+00:00  127.0150  127.0782  126.6800  126.7800  11665598        47146  126.813182
2021-06-08 20:15:00+00:00  126.7700  126.7900  126.5400  126.6600     83725         1973  126.679259
2021-06-08 21:00:00+00:00  126.6900  126.8500 

Related Skills

View on GitHub
GitHub Stars1.9k
CategoryDevelopment
Updated10d ago
Forks553

Languages

Python

Security Score

100/100

Audited on Jul 29, 2026

No findings