{"id":18843274,"url":"https://github.com/alpacahq/alpaca-backtrader-api","last_synced_at":"2025-05-14T15:00:25.497Z","repository":{"id":33223647,"uuid":"155041481","full_name":"alpacahq/alpaca-backtrader-api","owner":"alpacahq","description":"Alpaca Trading API integrated with backtrader ","archived":false,"fork":false,"pushed_at":"2024-12-09T12:23:46.000Z","size":230,"stargazers_count":645,"open_issues_count":54,"forks_count":148,"subscribers_count":31,"default_branch":"master","last_synced_at":"2025-04-12T18:47:32.665Z","etag":null,"topics":["algotrading","alpaca","backtesting","backtrader","finance","hacktoberfest","python","quants","stock-market","stock-trading","trading","trading-algorithms","trading-bot"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/alpaca-backtrader-api/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alpacahq.png","metadata":{"files":{"readme":"README.md","changelog":"changes.txt","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-10-28T06:19:00.000Z","updated_at":"2025-04-02T08:59:51.000Z","dependencies_parsed_at":"2024-05-16T01:05:28.814Z","dependency_job_id":"fa2a3ab7-dd32-45e1-a43b-724cc1076364","html_url":"https://github.com/alpacahq/alpaca-backtrader-api","commit_stats":{"total_commits":207,"total_committers":16,"mean_commits":12.9375,"dds":"0.38647342995169087","last_synced_commit":"475bb54b8b16506092eb97df5a7cb1d05f0b86be"},"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alpacahq%2Falpaca-backtrader-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alpacahq%2Falpaca-backtrader-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alpacahq%2Falpaca-backtrader-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alpacahq%2Falpaca-backtrader-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alpacahq","download_url":"https://codeload.github.com/alpacahq/alpaca-backtrader-api/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254168074,"owners_count":22026078,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algotrading","alpaca","backtesting","backtrader","finance","hacktoberfest","python","quants","stock-market","stock-trading","trading","trading-algorithms","trading-bot"],"created_at":"2024-11-08T02:57:23.796Z","updated_at":"2025-05-14T15:00:23.848Z","avatar_url":"https://github.com/alpacahq.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![PyPI version](https://badge.fury.io/py/alpaca-backtrader-api.svg)](https://badge.fury.io/py/alpaca-backtrader-api)\n[![CircleCI](https://circleci.com/gh/alpacahq/alpaca-backtrader-api.svg?style=shield)](https://circleci.com/gh/alpacahq/alpaca-backtrader-api)\n[![Updates](https://pyup.io/repos/github/alpacahq/alpaca-backtrader-api/shield.svg)](https://pyup.io/repos/github/alpacahq/alpaca-backtrader-api/)\n[![Python 3](https://pyup.io/repos/github/alpacahq/alpaca-backtrader-api/python-3-shield.svg)](https://pyup.io/repos/github/alpacahq/alpaca-backtrader-api/)\n\n# alpaca-backtrader-api\n\n`alpaca-backtrader-api` is a python library for the Alpaca trade API\nwithin `backtrader` framework.\nIt allows rapid trading algo development easily, with support for the\nboth REST and streaming interfaces. For details of each API behavior,\nplease see the online API document.\n\nNote this module supports only python version 3.5 and above, due to\nthe underlying library `alpaca-trade-api`.\n\n## Install\n\n```bash\n$ pip3 install alpaca-backtrader-api\n```\n\n## Example\n\n#### These examples only work if you have a funded brokerage account or another means of accessing Polygon data.\n\nyou can find example strategies in the [samples](https://github.com/alpacahq/alpaca-backtrader-api/tree/master/sample) folder. \n\nremember to add you credentials.\n\nyou can toggle between backtesting and paper trading by changing `ALPACA_PAPER`\n\n#### a strategy looks like this:\n\nIn order to call Alpaca's trade API, you need to obtain API key pairs.\nReplace \u003ckey_id\u003e and \u003csecret_key\u003e with what you get from the web console.\n\n```python\nimport alpaca_backtrader_api\nimport backtrader as bt\nfrom datetime import datetime\n\nALPACA_API_KEY = \u003ckey_id\u003e\nALPACA_SECRET_KEY = \u003csecret_key\u003e\nALPACA_PAPER = True\n\n\nclass SmaCross(bt.SignalStrategy):\n  def __init__(self):\n    sma1, sma2 = bt.ind.SMA(period=10), bt.ind.SMA(period=30)\n    crossover = bt.ind.CrossOver(sma1, sma2)\n    self.signal_add(bt.SIGNAL_LONG, crossover)\n\n\ncerebro = bt.Cerebro()\ncerebro.addstrategy(SmaCross)\n\nstore = alpaca_backtrader_api.AlpacaStore(\n    key_id=ALPACA_API_KEY,\n    secret_key=ALPACA_SECRET_KEY,\n    paper=ALPACA_PAPER\n)\n\nif not ALPACA_PAPER:\n  broker = store.getbroker()  # or just alpaca_backtrader_api.AlpacaBroker()\n  cerebro.setbroker(broker)\n\nDataFactory = store.getdata  # or use alpaca_backtrader_api.AlpacaData\ndata0 = DataFactory(dataname='AAPL', historical=True, fromdate=datetime(\n    2015, 1, 1), timeframe=bt.TimeFrame.Days)\ncerebro.adddata(data0)\n\nprint('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())\ncerebro.run()\nprint('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())\ncerebro.plot()\n```\n\n## API Document\n\nThe HTTP API document is located in https://docs.alpaca.markets/\n\n## Authentication\n\nThe Alpaca API requires API key ID and secret key, which you can obtain from the\nweb console after you sign in.  You can set them in the AlpacaStore constructor,\nusing 'key_id' and 'secret_key'.\n\n## Paper/Live mode\n\nThe 'paper' parameter is default to False, which allows live trading.\nIf you set it to True, then you are in the paper trading mode.\n\n## Running Multiple Strategies/Datas\nThere's a way to execute an algorithm with multiple datas or/and execute more than one algorithm.\u003cbr\u003e\nThe websocket connection is limited to 1 connection per account. Alpaca backtrader opens a websocket connection for each data you define.\u003cbr\u003e\nFor that exact purpose this ![project](https://github.com/shlomikushchi/alpaca-proxy-agent)  was created\u003cbr\u003e\nThe steps to execute this are:\n* Run the Alpaca Proxy Agent as described in the project's README\n* 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`)\n* execute your algorithm. it will connect to the servers through the proxy agent allowing you to execute multiple datas/strategies\n\n## Support and Contribution\n\nFor technical issues particular to this module, please report the\nissue on this GitHub repository. Any API issues can be reported through\nAlpaca's customer support.\n\nNew features, as well as bug fixes, by sending pull request is always\nwelcomed.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falpacahq%2Falpaca-backtrader-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falpacahq%2Falpaca-backtrader-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falpacahq%2Falpaca-backtrader-api/lists"}