Skip to main content

Alpaca API within backtrader

Project description


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.


$ pip3 install alpaca-backtrader-api


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

  from datetime import datetime
  import backtrader as bt
  import alpaca_backtrader_api

  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.signal_add(bt.SIGNAL_LONG, crossover)

  cerebro = bt.Cerebro()

  store = alpaca_backtrader_api.AlpacaStore(

  broker = store.getbroker()  # or just alpaca_backtrader_api.AlpacaBroker

  DataFactory = store.getdata() # or use alpaca_backtrader_api.AlpacaData
  data0 = DataFactory(dataname='AAPL', timeframe=bt.TimeFrame.TFrame("Days"))  # Supported timeframes: "Days"/"Minutes"

API Document

The HTTP API document is located in


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 'key_id' and 'secret_key'.

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.

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.

Project details

Download files

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

Files for alpaca-backtrader-api, version 0.1
Filename, size File type Python version Upload date Hashes
Filename, size alpaca_backtrader_api-0.1-py3-none-any.whl (15.0 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size alpaca-backtrader-api-0.1.tar.gz (14.6 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page