Skip to main content

PyPI version CircleCI

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

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.

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()
cerebro.addstrategy(SmaCross)

store = alpaca_backtrader_api.AlpacaStore(
    key_id='',
    secret_key='',
    paper=True
)

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

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

cerebro.run()
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 '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.

Metadata

Release files for alpaca-backtrader-api 0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for alpaca-backtrader-api 0.2
File Size Uploaded
alpaca-backtrader-api-0.2.tar.gz 14.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for alpaca-backtrader-api 0.2
File Interpreter ABI Platform
alpaca_backtrader_api-0.2-py3-none-any.whl Python 3 none any Details

Total release size: 29.8 kB

Release files / alpaca-backtrader-api-0.2.tar.gz

Download URL alpaca-backtrader-api-0.2.tar.gz
Size 14.8 kB
Tags Source
SHA-256 checksum
How to use checksums
db65ca1a1684fc34e169a5d1a96327fb5fdaf68010e3156a9f7c0e7111ee3cdb
BLAKE2b-256 checksum
How to use checksums
7a65779185eb764392dbebd86e7ddaeaefcd1eb7a8a4a240815cacaca214d213
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.6

Release files / alpaca_backtrader_api-0.2-py3-none-any.whl

Download URL alpaca_backtrader_api-0.2-py3-none-any.whl
Size 15.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
62371041594acfd3cbf49b1f8e86be763d5bd799858120cff309b002a3d7d747
BLAKE2b-256 checksum
How to use checksums
4c49bf880e0f66f98fb8519bdecba3d57679960c2d33c523e7dbc4997f795508
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.6

Release history Release notifications | RSS feed

0.14.1

2 release files

0.14.0

2 release files

0.12.0

2 release files

0.11.1

2 release files

0.9.5

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.7.1

1 release file

0.7

1 release file

0.6

1 release file

0.5

1 release file

0.4

1 release file

0.3

2 release files

This release

0.2 This release

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page