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",

编辑推荐精选

堆友

堆友

多风格AI绘画神器

堆友平台由阿里巴巴设计团队创建,作为一款AI驱动的设计工具,专为设计师提供一站式增长服务。功能覆盖海量3D素材、AI绘画、实时渲染以及专业抠图,显著提升设计品质和效率。平台不仅提供工具,还是一个促进创意交流和个人发展的空间,界面友好,适合所有级别的设计师和创意工作者。

图像生成AI工具AI反应堆AI工具箱AI绘画GOAI艺术字堆友相机AI图像热门
码上飞

码上飞

零代码AI应用开发平台

零代码AI应用开发平台,用户只需一句话简单描述需求,AI能自动生成小程序、APP或H5网页应用,无需编写代码。

Vora

Vora

免费创建高清无水印Sora视频

Vora是一个免费创建高清无水印Sora视频的AI工具

Refly.AI

Refly.AI

最适合小白的AI自动化工作流平台

无需编码,轻松生成可复用、可变现的AI自动化工作流

酷表ChatExcel

酷表ChatExcel

大模型驱动的Excel数据处理工具

基于大模型交互的表格处理系统,允许用户通过对话方式完成数据整理和可视化分析。系统采用机器学习算法解析用户指令,自动执行排序、公式计算和数据透视等操作,支持多种文件格式导入导出。数据处理响应速度保持在0.8秒以内,支持超过100万行数据的即时分析。

AI工具酷表ChatExcelAI智能客服AI营销产品使用教程
TRAE编程

TRAE编程

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

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

AI工具TraeAI IDE协作生产力转型热门
AIWritePaper论文写作

AIWritePaper论文写作

AI论文写作指导平台

AIWritePaper论文写作是一站式AI论文写作辅助工具,简化了选题、文献检索至论文撰写的整个过程。通过简单设定,平台可快速生成高质量论文大纲和全文,配合图表、参考文献等一应俱全,同时提供开题报告和答辩PPT等增值服务,保障数据安全,有效提升写作效率和论文质量。

AI辅助写作AI工具AI论文工具论文写作智能生成大纲数据安全AI助手热门
博思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法律顾问。

下拉加载更多