alpacahq/alpaca-backtrader-api

Is it possible to convert AlpacaData to pandas/numpy/CSV data?

doctorcolossus opened this issue · 1 comments

I adapted the following code from the README.md:

import alpaca_backtrader_api
import backtrader as bt
from datetime import datetime

ALPACA_API_KEY    = "..."
ALPACA_SECRET_KEY = "..."
ALPACA_PAPER      = True

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

data = store.getdata(dataname   = "AAPL",
                     historical = True,
                     fromdate   = datetime(2015, 1, 1),
                     timeframe  = bt.TimeFrame.Days)

The resulting data of type AlpacaData works fine with cerebro, but I'm struggling to figure out how it's possible to use directly. len(data) returns 0. My goal is simply to perform some general analyses like identifying local extrema to help in developing my strategy. I guess I could download the data once through alpaca-backtrader-api for backtesting, and then again through the Alpaca API for other analyses, but isn't there any way to just do it once to get an AlpacaData object (which I guess is a Backtrader data feed?), and then convert that to a numpy series, or a pandas DataFrame, or CSV?

Apologies if I'm missing something obvious. I'm new to both Backtrader and Alpaca. I wanted to first ask the Backtrader community, but unfortunately their forum is not letting me register because the CAPTCHA isn't loading.

Hi,
sorry for the late response.
as you said you could do it in 2 ways (probably best to use alpaca-trade-api to get raw data)
but if you want to get data exactly like backtrader your best option is to put a breakpoint/print in this file:
alpacastore.py function name: _t_candles. this is where we get the data from the broker and pass it to backtrader datafeed