Backtrader Community

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    "sleep 3 seconds and retrying.." warning when connecting to Alpaca paper account and not hitting limit

    General Code/Help
    alpaca
    1
    1
    303
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • T
      talbot09 last edited by

      Algo runs fine when during backtest (is_live=False in code below), but get the warning

      WARNING:alpaca_trade_api.rest:sleep 3 seconds and retrying https://paper-api.alpaca.markets/v2/account 3 more time(s)...

      when trying to run algo on Alpaca paper account during live trading hours (is_live=True). I don't think I'm hitting anywhere near the limit of 200 requests/min for Alpaca. Read through the following 2 threads and solutions didn't help:

      1. https://community.backtrader.com/topic/2693/timeframe-days-works-but-timeframe-minutes-doesn-t/20?page=1
      2. https://forum.alpaca.markets/t/lagging-and-dropping-connections/714
      import alpaca_backtrader_api
      import backtrader as bt
      import pandas as pd
      from datetime import datetime
      from alpaca_strategies import MAcrossover
      
      from dotenv import load_dotenv
      import os
      load_dotenv()
      
      stock = 'AAPL'
      is_live = True
      
      api_key = os.getenv('API_KEY_ID')
      api_secret = os.getenv('API_SECRET')
      alpaca_paper = os.getenv('ALPACA_PAPER')
      
      cerebro = bt.Cerebro()
      cerebro.addstrategy(MAcrossover)
      
      cerebro.addsizer(bt.sizers.SizerFix, stake=10)
      
      store = alpaca_backtrader_api.AlpacaStore(
          key_id=api_key,
          secret_key=api_secret,
          paper=alpaca_paper
      )
      
      DataFactory = store.getdata  # or use alpaca_backtrader_api.AlpacaData
      
      if is_live:
          broker = store.getbroker()
          cerebro.setbroker(broker)
          data0 = DataFactory(
              dataname=stock,
              timeframe=bt.TimeFrame.TFrame("Minutes"),
          )
      else:
          cerebro.broker.setcash(27000)
          cerebro.broker.setcommission(commission=0.0)
          data0 = DataFactory(
              dataname=stock,
              timeframe=bt.TimeFrame.TFrame("Minutes"),
              fromdate=pd.Timestamp('2021-01-13'),
              todate=pd.Timestamp('2021-01-14'),
              historical=True)
      cerebro.adddata(data0)
      
      start_portfolio_value = cerebro.broker.getvalue()
      print('Starting Portfolio Value: %.2f' % start_portfolio_value)
      cerebro.run()
      end_portfolio_value = cerebro.broker.getvalue()
      pnl = end_portfolio_value - start_portfolio_value
      print('Final Portfolio Value: %.2f' % end_portfolio_value)
      print(f'PnL: {pnl:.2f}')
      cerebro.plot()
      

      Can also show code for strategy if that helps--I'm still learning and it's from a tutorial. Want to check if the front end has any errors first.

      1 Reply Last reply Reply Quote 0
      • 1 / 1
      • First post
        Last post
      Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors