Skip to main content

Alpaca API within backtrader

Project description

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 <key_id> and <secret_key> with what you get from the web console.

import alpaca_backtrader_api
import backtrader as bt
from datetime import datetime

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


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

store = alpaca_backtrader_api.AlpacaStore(
    key_id=ALPACA_API_KEY,
    secret_key=ALPACA_SECRET_KEY,
    paper=ALPACA_PAPER
)

if not ALPACA_PAPER:
  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', 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 '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.

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: DATA_PROXY_WS to be the address of the proxy agent. (e.g: DATA_PROXY_WS=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.

Project details


Download files

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

Source Distribution

alpaca-backtrader-api-0.15.0.tar.gz (21.7 kB view details)

Uploaded Source

Built Distribution

alpaca_backtrader_api-0.15.0-py3-none-any.whl (23.3 kB view details)

Uploaded Python 3

File details

Details for the file alpaca-backtrader-api-0.15.0.tar.gz.

File metadata

  • Download URL: alpaca-backtrader-api-0.15.0.tar.gz
  • Upload date:
  • Size: 21.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.6.6

File hashes

Hashes for alpaca-backtrader-api-0.15.0.tar.gz
Algorithm Hash digest
SHA256 7e8be201029e410b82fed81522980551658294a6b707ec6e98f1eb1f06a20e66
MD5 45b817f634de33a0806c8a851bc39f02
BLAKE2b-256 b387b4c7488c16d7a4b7387376cc337817fb663c69072f4cd12f7bd54777abd6

See more details on using hashes here.

File details

Details for the file alpaca_backtrader_api-0.15.0-py3-none-any.whl.

File metadata

  • Download URL: alpaca_backtrader_api-0.15.0-py3-none-any.whl
  • Upload date:
  • Size: 23.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.6.6

File hashes

Hashes for alpaca_backtrader_api-0.15.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f6aeac443bef837968ea04e6aad81e38432a2593520eddf2a46cecba76fd29e6
MD5 a2b01c1560252704363d3b97f91cf60c
BLAKE2b-256 9f196379b7bc5bd406168a051e267f3372d25b4d6b8204a5839f649acca06280

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page