Skip to main content

The new official Python SDK for the https://alpaca.markets/ API

Project description

AlpacaPy

Downloads

Dev setup

This project is managed via poetry so setup should be just running poetry install.

This repo is using pre-commit to setup some checks to happen at commit time to keep the repo clean. To set these up after you've run poetry install just run poetry run pre-commit install to have pre-commit setup these hooks

Note: AlpacaPy is in the very early stages of alpha development and is not production ready. Currently AlpacaPy interfaces with only the Market Data API, however the other APIs are coming soon.

Basic Example Use Cases

Requesting Historical Market Data

To retrieve historical market data, you’ll need to instantiate a historical data client with your API keys. There are many different methods that allow you to access various stock and crypto data. To see the full range of data types available, read the API reference for market data.

In this example, we will query daily bar data for Apple Inc (AAPL) between January 1st 2021 and December 31st 2021. Then we will convert the data to a dataframe and print it out to the console.

from alpaca.data.historical import HistoricalDataClient
from alpaca.common.time import TimeFrame

API_KEY = 'api-key'
SECRET_KEY = 'secret-key'

client = HistoricalDataClient(API_KEY, SECRET_KEY)

bars = client.get_bars("AAPL", TimeFrame.Day, "2021-01-01", "2021-12-31")

print(bars.df)

#             open      high      low   close     volume  trade_count        vwap
# timestamp
# 2021-01-04  133.56  133.6116  126.760  129.41  143302687      1310228  129.732580
# 2021-01-05  128.98  131.7400  128.430  131.01   97667342       707584  130.717944
# 2021-01-06  127.53  131.0499  126.382  126.60  155104120      1202580  128.350036
# 2021-01-07  128.38  131.6300  127.860  130.92  109581117       718363  130.153889
# 2021-01-08  132.50  132.6300  130.230  132.05  105158675       800071  131.565744
# ...            ...       ...      ...     ...        ...          ...         ...
# 2021-12-27  177.10  180.4200  177.070  180.33   74912939       629431  179.056944
# 2021-12-28  180.20  181.3300  178.530  179.29   79103863       631316  179.707003
# 2021-12-29  179.30  180.6300  178.140  179.38   62325973       491576  179.455692
# 2021-12-30  179.59  180.5700  178.090  178.20   59770632       498613  179.374495
# 2021-12-31  178.00  179.2300  177.260  177.57   64038680       451478  177.800285

# [252 rows x 7 columns]

Subscribing to Live Market Data

Live market data is available for both crypto and stocks via websocket interfaces. Keep in mind live stock data is only available during market hours on trading days, whereas live crypto data is available 24/7. In this example, we will subscribe to live quote data for Bitcoin (BTCUSD). To do so, first we will need to create an instance of the CryptoDataStream client with our API keys. Then we can create an asynchronous callback method to handle our live data as it is available.

from alpaca.data.live import CryptoDataStream

API_KEY = 'api-key'
SECRET_KEY = 'secret-key'

client = CryptoDataStream(API_KEY, SECRET_KEY)


# handler function will receive data as the data arrives
async def handler(data):
    print(data)


# subscribe to quote data for BTCUSD
client.subscribe_quotes(handler, "BTCUSD")

# start websocket client
client.run()

Project details


Release history Release notifications | RSS feed

This version

0.2.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

alpaca-py-0.2.0.tar.gz (69.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

alpaca_py-0.2.0-py3-none-any.whl (79.9 kB view details)

Uploaded Python 3

File details

Details for the file alpaca-py-0.2.0.tar.gz.

File metadata

  • Download URL: alpaca-py-0.2.0.tar.gz
  • Upload date:
  • Size: 69.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.9.2 Darwin/21.3.0

File hashes

Hashes for alpaca-py-0.2.0.tar.gz
Algorithm Hash digest
SHA256 63dc4465139de62c8e2e0bb6dca7cae6a02330cc24a6e80c246017e213ccb307
MD5 eac483dfb2087340b42f40cb20de4158
BLAKE2b-256 026c066a92ebf126798628ae9b2b253e63ac812e715690cc78d18f71858a3d2f

See more details on using hashes here.

File details

Details for the file alpaca_py-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: alpaca_py-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 79.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.9.2 Darwin/21.3.0

File hashes

Hashes for alpaca_py-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 09664212985dd230cdf338addc455c4aab5239d1a7d720c441fc1723bdd0ec9d
MD5 d365a9b3e5334314fb55f07fcb37bca2
BLAKE2b-256 3a2d21b77dd852deb2f53238cb34a0429eb9c8e2acdc51bb145ae9496b614c6b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page