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写作平台,涵盖小说、学术、社交媒体等多场景。提供续写、改写、润色等功能,助力创作者高效优化写作流程。界面简洁,功能全面,适合各类写作者提升内容品质和工作效率。

AI辅助写作AI工具蛙蛙写作AI写作工具学术助手办公助手营销助手AI助手
Trae

Trae

字节跳动发布的AI编程神器IDE

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

AI工具TraeAI IDE协作生产力转型热门
问小白

问小白

全能AI智能助手,随时解答生活与工作的多样问题

问小白,由元石科技研发的AI智能助手,快速准确地解答各种生活和工作问题,包括但不限于搜索、规划和社交互动,帮助用户在日常生活中提高效率,轻松管理个人事务。

热门AI助手AI对话AI工具聊天机器人
Transly

Transly

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

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

讯飞智文

讯飞智文

一键生成PPT和Word,让学习生活更轻松

讯飞智文是一个利用 AI 技术的项目,能够帮助用户生成 PPT 以及各类文档。无论是商业领域的市场分析报告、年度目标制定,还是学生群体的职业生涯规划、实习避坑指南,亦或是活动策划、旅游攻略等内容,它都能提供支持,帮助用户精准表达,轻松呈现各种信息。

AI办公办公工具AI工具讯飞智文AI在线生成PPTAI撰写助手多语种文档生成AI自动配图热门
讯飞星火

讯飞星火

深度推理能力全新升级,全面对标OpenAI o1

科大讯飞的星火大模型,支持语言理解、知识问答和文本创作等多功能,适用于多种文件和业务场景,提升办公和日常生活的效率。讯飞星火是一个提供丰富智能服务的平台,涵盖科技资讯、图像创作、写作辅助、编程解答、科研文献解读等功能,能为不同需求的用户提供便捷高效的帮助,助力用户轻松获取信息、解决问题,满足多样化使用场景。

热门AI开发模型训练AI工具讯飞星火大模型智能问答内容创作多语种支持智慧生活
Spark-TTS

Spark-TTS

一种基于大语言模型的高效单流解耦语音令牌文本到语音合成模型

Spark-TTS 是一个基于 PyTorch 的开源文本到语音合成项目,由多个知名机构联合参与。该项目提供了高效的 LLM(大语言模型)驱动的语音合成方案,支持语音克隆和语音创建功能,可通过命令行界面(CLI)和 Web UI 两种方式使用。用户可以根据需求调整语音的性别、音高、速度等参数,生成高质量的语音。该项目适用于多种场景,如有声读物制作、智能语音助手开发等。

咔片PPT

咔片PPT

AI助力,做PPT更简单!

咔片是一款轻量化在线演示设计工具,借助 AI 技术,实现从内容生成到智能设计的一站式 PPT 制作服务。支持多种文档格式导入生成 PPT,提供海量模板、智能美化、素材替换等功能,适用于销售、教师、学生等各类人群,能高效制作出高品质 PPT,满足不同场景演示需求。

讯飞绘文

讯飞绘文

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

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

热门AI辅助写作AI工具讯飞绘文内容运营AI创作个性化文章多平台分发AI助手
材料星

材料星

专业的AI公文写作平台,公文写作神器

AI 材料星,专业的 AI 公文写作辅助平台,为体制内工作人员提供高效的公文写作解决方案。拥有海量公文文库、9 大核心 AI 功能,支持 30 + 文稿类型生成,助力快速完成领导讲话、工作总结、述职报告等材料,提升办公效率,是体制打工人的得力写作神器。

下拉加载更多