For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

Live Data/Trading Not Respecting Timeframe/Compression

  • I am using the Alpaca paper account as my broker and testing a very basic scenario just to validate functionality. I have been unable to implement trades based on the 1 minute, 5 minute or 1 hour bar. It seems that the strategy just attempts to trade off the real-time second-by-second data instead of the 1/5/60 minute bars.

    Since I am using Alpaca I use the "alpaca_backtrader_api" python library along with the backtrader library. The following code produces the second-by-second "next" output.

    import config
    import alpaca_backtrader_api
    import backtrader as bt
    from datetime import datetime
    ALPACA_API_KEY = config.alpaca_paper_api_key_id
    ALPACA_SECRET_KEY = config.alpaca_paper_api_secret_key
    class EmptyStrategy(bt.Strategy):
        def __init__(self):
            print(f"Define Empty Strategy")
        def next(self):
            date_fmt = '%Y%m%d %H:%M:%S'
            date_str =
            print(f"{date_str}: O:{[0]:.2f} C:{[0]:.2f}")
    cerebro = bt.Cerebro()
    store = alpaca_backtrader_api.AlpacaStore(
    alpaca_data_kwargs = dict(
    broker = alpaca_backtrader_api.AlpacaBroker()
    # DataFactory = alpaca_backtrader_api.AlpacaData
    DataFactory = store.getdata
    data0 = DataFactory(**alpaca_data_kwargs)
    print(f"Running Cerebro...")

    Output is:

    Running Cerebro...
    Define Empty Strategy
    20201006 15:01:26: O:9.25 C:9.25
    20201006 15:01:28: O:9.22 C:9.22
    20201006 15:01:31: O:9.22 C:9.22
    20201006 15:01:34: O:9.22 C:9.22
    20201006 15:01:41: O:9.22 C:9.22
    20201006 15:01:44: O:9.22 C:9.22
    20201006 15:01:45: O:9.25 C:9.25
    20201006 15:01:47: O:9.25 C:9.25
    20201006 15:01:51: O:9.22 C:9.22
    20201006 15:01:52: O:9.27 C:9.27
    20201006 15:01:53: O:9.27 C:9.27

    The output shows the "next" method is getting called every second or so which does not make sense to me when I specified "timeframe=bt.TimeFrame.Minutes" and "compression=5". I would have expected to only see output every 5 minutes.

    When I ran an actual strategy it was clear to me trades were occurring based on the second-by-second data, which is not what I wanted.

    Am I doing something wrong in my example?

  • i would ask this question to alpaca data feed developers.

Log in to reply