alpacahq
alpaca-backtrader-api
Python

Alpaca Trading API integrated with backtrader

Last updated Jul 8, 2026
698
Stars
159
Forks
56
Issues
0
Stars/day
Attention Score
96
Language breakdown
No language data available.
Files click to expand
README

PyPI version CircleCI Updates Python 3

alpaca-backtrader-api

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

Note this module supports only python version 3.5 and above, due to the underlying library alpaca-trade-api.

Install

$ pip3 install alpaca-backtrader-api

Example

These examples only work if you have a funded brokerage account or another means of accessing Polygon data.

you can find example strategies in the samples folder.

remember to add you credentials.

you can toggle between backtesting and paper trading by changing ALPACA_PAPER

a strategy looks like this:

In order to call Alpaca's trade API, you need to obtain API key pairs. Replace id> and key> with what you get from the web console.

import alpacabacktraderapi
import backtrader as bt
from datetime import datetime

ALPACAAPIKEY = <key_id> ALPACASECRETKEY = <secret_key> ALPACA_PAPER = True

class SmaCross(bt.SignalStrategy): def init(self): sma1, sma2 = bt.ind.SMA(period=10), bt.ind.SMA(period=30) crossover = bt.ind.CrossOver(sma1, sma2) self.signaladd(bt.SIGNALLONG, crossover)

cerebro = bt.Cerebro() cerebro.addstrategy(SmaCross)

store = alpacabacktraderapi.AlpacaStore( keyid=ALPACAAPI_KEY, secretkey=ALPACASECRET_KEY, paper=ALPACA_PAPER )

if not ALPACA_PAPER: broker = store.getbroker() # or just alpacabacktraderapi.AlpacaBroker() cerebro.setbroker(broker)

DataFactory = store.getdata # or use alpacabacktraderapi.AlpacaData data0 = DataFactory(dataname='AAPL', historical=True, fromdate=datetime( 2015, 1, 1), timeframe=bt.TimeFrame.Days) cerebro.adddata(data0)

print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue()) cerebro.run() print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue()) cerebro.plot()

API Document

The HTTP API document is located in https://docs.alpaca.markets/

Authentication

The Alpaca API requires API key ID and secret key, which you can obtain from the web console after you sign in. You can set them in the AlpacaStore constructor, using 'keyid' and 'secretkey'.

Paper/Live mode

The 'paper' parameter is default to False, which allows live trading. If you set it to True, then you are in the paper trading mode.

Running Multiple Strategies/Datas

There's a way to execute an algorithm with multiple datas or/and execute more than one algorithm.
The websocket connection is limited to 1 connection per account. Alpaca backtrader opens a websocket connection for each data you define.
For that exact purpose this project was created
The steps to execute this are:
  • Run the Alpaca Proxy Agent as described in the project's README
  • Define this env variable: DATAPROXYWS to be the address of the proxy agent. (e.g: DATAPROXYWS=ws://192.168.99.100:8765)
  • execute your algorithm. it will connect to the servers through the proxy agent allowing you to execute multiple datas/strategies

Support and Contribution

For technical issues particular to this module, please report the issue on this GitHub repository. Any API issues can be reported through Alpaca's customer support.

New features, as well as bug fixes, by sending pull request is always welcomed.

🔗 More in this category

© 2026 GitRepoTrend · alpacahq/alpaca-backtrader-api · Updated daily from GitHub