alpaca-trade-api-python

alpaca-trade-api-python

Alpaca交易API Python库支持快速算法开发和实时数据访问

alpaca-trade-api-python是一个用于访问Alpaca交易API的Python库。该库支持REST和实时数据接口,方便开发者构建交易算法。它提供历史数据查询、实时数据流、账户和投资组合管理等功能,支持Python 3.7及以上版本。通过环境变量配置,用户可以轻松获取股票历史行情、报价和交易数据,并支持自定义时间框架和数据处理方式。

Alpaca交易APIPython SDK数据服务历史数据Github开源项目

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.

EnvironmentdefaultDescription
APCA_API_KEY_ID=<key_id>Your API Key
APCA_API_SECRET_KEY=<secret_key>Your API Secret Key
APCA_API_BASE_URL=urlhttps://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=urlhttps://data.alpaca.marketsEndpoint for data API
APCA_RETRY_MAX=33The number of subsequent API calls to retry on timeouts
APCA_RETRY_WAIT=33seconds to wait between each retry attempt
APCA_RETRY_CODES=429,504429,504comma-separated HTTP status code for which retry is attempted
DATA_PROXY_WSWhen 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 126.6700 126.7200 145153 769 126.746457 2021-06-08 21:45:00+00:00 126.7000 126.7400 126.6500 126.7100 38455 406 126.699544 2021-06-08 22:30:00+00:00 126.7100 126.7600 126.6700 126.7100 30822 222 126.713892 2021-06-08 23:15:00+00:00 126.7200 126.7600 126.6400 126.6400 32585 340 126.704131

Quotes

option 1: wait for the data

from alpaca_trade_api.rest import REST api = REST() api.get_quotes("AAPL", "2021-06-08", "2021-06-08", limit=10).df ask_exchange ask_price ask_size bid_exchange bid_price bid_size conditions timestamp 2021-06-08 08:00:00.070928640+00:00 P 143.00 1 0.00 0 [Y] 2021-06-08 08:00:00.070929408+00:00 P 143.00 1 P 102.51 1 [R] 2021-06-08 08:00:00.070976768+00:00 P 143.00 1 P 116.50 1 [R] 2021-06-08 08:00:00.070978816+00:00 P 143.00 1 P 118.18 1 [R] 2021-06-08 08:00:00.071020288+00:00 P 143.00 1 P 120.00 1 [R] 2021-06-08 08:00:00.071020544+00:00 P 134.18 1 P 120.00 1 [R] 2021-06-08 08:00:00.071021312+00:00 P 134.18 1 P 123.36 1 [R] 2021-06-08 08:00:00.071209984+00:00 P 131.11 1 P 123.36 1 [R] 2021-06-08 08:00:00.071248640+00:00 P 130.13 1 P 123.36 1 [R] 2021-06-08 08:00:00.071286016+00:00 P 129.80 1 P 123.36 1 [R]

option 2: iterate over quotes

def process_quote(quote): # process quote print(quote) quote_iter = api.get_quotes_iter("AAPL", "2021-06-08", "2021-06-08", limit=10) for quote in quote_iter: process_quote(quote)

Trades

option 1: wait for the data

from alpaca_trade_api.rest import REST api = REST() api.get_trades("AAPL",

编辑推荐精选

TRAE编程

TRAE编程

AI辅助编程,代码自动修复

Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。

AI工具TraeAI IDE协作生产力转型热门
博思AIPPT

博思AIPPT

AI一键生成PPT,就用博思AIPPT!

博思AIPPT,新一代的AI生成PPT平台,支持智能生成PPT、AI美化PPT、文本&链接生成PPT、导入Word/PDF/Markdown文档生成PPT等,内置海量精美PPT模板,涵盖商务、教育、科技等不同风格,同时针对每个页面提供多种版式,一键自适应切换,完美适配各种办公场景。

AI办公办公工具AI工具博思AIPPTAI生成PPT智能排版海量精品模板AI创作热门
潮际好麦

潮际好麦

AI赋能电商视觉革命,一站式智能商拍平台

潮际好麦深耕服装行业,是国内AI试衣效果最好的软件。使用先进AIGC能力为电商卖家批量提供优质的、低成本的商拍图。合作品牌有Shein、Lazada、安踏、百丽等65个国内外头部品牌,以及国内10万+淘宝、天猫、京东等主流平台的品牌商家,为卖家节省将近85%的出图成本,提升约3倍出图效率,让品牌能够快速上架。

iTerms

iTerms

企业专属的AI法律顾问

iTerms是法大大集团旗下法律子品牌,基于最先进的大语言模型(LLM)、专业的法律知识库和强大的智能体架构,帮助企业扫清合规障碍,筑牢风控防线,成为您企业专属的AI法律顾问。

SimilarWeb流量提升

SimilarWeb流量提升

稳定高效的流量提升解决方案,助力品牌曝光

稳定高效的流量提升解决方案,助力品牌曝光

Sora2视频免费生成

Sora2视频免费生成

最新版Sora2模型免费使用,一键生成无水印视频

最新版Sora2模型免费使用,一键生成无水印视频

Transly

Transly

实时语音翻译/同声传译工具

Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。

讯飞绘文

讯飞绘文

选题、配图、成文,一站式创作,让内容运营更高效

讯飞绘文,一个AI集成平台,支持写作、选题、配图、排版和发布。高效生成适用于各类媒体的定制内容,加速品牌传播,提升内容营销效果。

热门AI辅助写作AI工具讯飞绘文内容运营AI创作个性化文章多平台分发AI助手
商汤小浣熊

商汤小浣熊

最强AI数据分析助手

小浣熊家族Raccoon,您的AI智能助手,致力于通过先进的人工智能技术,为用户提供高效、便捷的智能服务。无论是日常咨询还是专业问题解答,小浣熊都能以快速、准确的响应满足您的需求,让您的生活更加智能便捷。

imini AI

imini AI

像人一样思考的AI智能体

imini 是一款超级AI智能体,能根据人类指令,自主思考、自主完成、并且交付结果的AI智能体。

下拉加载更多