alpacahq / alpacahq/alpaca-backtrader-api
Error when fetch the history data with minute timeframe.
- Langage dominant
- Python
- Étoiles
- 702
- Forks
- 160
- Métriques de merge des PR
- Aucune PR mergée en 30 j
Description
Hi,
I encounter this error when I upgrade to version alpaca-backtrader-api-0.14.1. It can be reproduced using the code of strategy_readme_sample.py . The only change is backtest and the timeframe of minute bars.
File "/Users/xxx/lib/python3.8/site-packages/alpaca_backtrader_api/alpacastore.py", line 397, in _t_candles
cdl = self.get_aggs_from_alpaca(dataname,
File "/Users/xxx/lib/python3.8/site-packages/alpaca_backtrader_api/alpacastore.py", line 586, in get_aggs_from_alpaca
response = _iterate_api_calls()
File "/Users/xxx/lib/python3.8/site-packages/alpaca_backtrader_api/alpacastore.py", line 522, in _iterate_api_calls
r = self.oapi.get_bars(dataname,
File "/Users/xxx/lib/python3.8/site-packages/alpaca_trade_api/rest.py", line 651, in get_bars
bars = list(self.get_bars_iter(symbol,
File "/Users/xxx/lib/python3.8/site-packages/alpaca_trade_api/rest.py", line 637, in get_bars_iter
for bar in bars:
File "/Users/xxx/lib/python3.8/site-packages/alpaca_trade_api/rest.py", line 564, in _data_get_v2
for item in items:
TypeError: 'NoneType' object is not iterable
The code:
```
import alpaca_backtrader_api
import backtrader as bt
from datetime import datetime
ALPACA_API_KEY = ""
ALPACA_SECRET_KEY = ""
IS_BACKTEST = True
IS_LIVE = False
symbol = "AAPL"
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)
if __name__ == '__main__':
cerebro = bt.Cerebro()
cerebro.addstrategy(SmaCross)
store = alpaca_backtrader_api.AlpacaStore(
key_id=ALPACA_API_KEY,
secret_key=ALPACA_SECRET_KEY,
paper=not IS_LIVE,
)
DataFactory = store.getdata # or use alpaca_backtrader_api.AlpacaData
if IS_BACKTEST:
data0 = DataFactory(dataname=symbol, historical=True,
fromdate=datetime(2021, 8, 12),
timeframe=bt.TimeFrame.Minutes,
data_feed='iex')
else:
data0 = DataFactory(dataname=symbol,
historical=False,
timeframe=bt.TimeFrame.Days,
data_feed='iex')
# or just alpaca_backtrader_api.AlpacaBroker()
broker = store.getbroker()
cerebro.setbroker(broker)
cerebro.adddata(data0)
print('Starting Portfolio Value: {}'.format(cerebro.broker.getvalue()))
cerebro.run()
print('Final Portfolio Value: {}'.format(cerebro.broker.getvalue()))
cerebro.plot()
```
I found that the earliest_sample in _iterate_api_calls() is some time in the day of fromdate(start) like 2021-08-12 04:03:00 which is never earlier than the start date time 2021-08-12 00:00:00. So the got_all is till false when the data fetch is done. It works for the days timeframe but failed on minutes timeframe. I am not sure if that is the root cause, and just post my finding.
Guide de contribution
Aucun guide de contribution indexé pour ce dépôt
Évaluation
Cette issue n'a pas encore été évaluée.