Navigation

    Backtrader Community

    • Register
    • 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/

    TimeFrame.Days works but TimeFrame.Minutes doesn't

    General Code/Help
    2
    21
    977
    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.
    • Wayne Filkins
      Wayne Filkins last edited by

      When I try to paper trade live with any strategy using TimeFrame.Minutes I get:

      sleep 3 seconds and retrying https://api.alpaca.markets/v2/account 3 more time(s)...

      If I click the link it gives:

      {"code":40110000,"message":"access key verification failed : access key not found (Code = 40110000)"}

      Tried making new keycodes several times so it's not that...seems to just be when I use Minutes instead of Days.

      1 Reply Last reply Reply Quote 1
      • Wayne Filkins
        Wayne Filkins last edited by

        Has anyone else had this problem? Could someone at least try using Minutes instead of Days and lmk what happens? Make sure you do it during market hours though that's when the error comes up.

        1 Reply Last reply Reply Quote 0
        • M
          mudassar031 last edited by

          Here is code i have used it and its working perfectly also there resample it

          if not alpaca_paper:
            broker = store.getbroker()  # or just alpaca_backtrader_api.AlpacaBroker()
            cerebro.setbroker(broker)
          
          DataFactory = store.getdata  # or use alpaca_backtrader_api.AlpacaData
          data0 = DataFactory(
              dataname='AAPL',
              timeframe=bt.TimeFrame.TFrame("Minutes"),
              fromdate=pd.Timestamp('2018-11-15'),
              todate=pd.Timestamp('2018-11-17'),
              historical=True)
          cerebro.adddata(data0)
          #Resampler for 15 minutes
          cerebro.resampledata(data0,timeframe=bt.TimeFrame.Minutes,compression=15)
          
          
          M Wayne Filkins 2 Replies Last reply Reply Quote 1
          • M
            mudassar031 @mudassar031 last edited by

            @mudassar031
            full code example using alpaca api with TimeFrame.minutes and resample data

            import alpaca_backtrader_api
            import backtrader as bt
            import pandas as pd
            from datetime import datetime
            from strategies.tos_strategy import TOS
            
            from dotenv import load_dotenv
            import os
            load_dotenv()
            
            api_key = os.getenv('API_KEY_ID')
            api_secret = os.getenv('API_SECRET')
            alpaca_paper = os.getenv('ALPACA_PAPER')
            
            cerebro = bt.Cerebro()
            cerebro.addstrategy(TOS)
            
            cerebro.broker.setcash(100000)
            cerebro.broker.setcommission(commission=0.0)
            cerebro.addsizer(bt.sizers.PercentSizer, percents=20)
            
            store = alpaca_backtrader_api.AlpacaStore(
                key_id=api_key,
                secret_key=api_secret,
                paper=alpaca_paper
            )
            
            if not alpaca_paper:
              broker = store.getbroker()  # or just alpaca_backtrader_api.AlpacaBroker()
              cerebro.setbroker(broker)
            
            DataFactory = store.getdata  # or use alpaca_backtrader_api.AlpacaData
            data0 = DataFactory(
                dataname='AAPL',
                timeframe=bt.TimeFrame.TFrame("Minutes"),
                fromdate=pd.Timestamp('2018-11-15'),
                todate=pd.Timestamp('2018-11-17'),
                historical=True)
            cerebro.adddata(data0)
            
            #Resampler for 15 minutes
            cerebro.resampledata(data0,timeframe=bt.TimeFrame.Minutes,compression=15)
            
            print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
            cerebro.run()
            print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
            cerebro.plot()
            
            
            Wayne Filkins 3 Replies Last reply Reply Quote 1
            • Wayne Filkins
              Wayne Filkins @mudassar031 last edited by

              @mudassar031 Thank you! I will try it.

              1 Reply Last reply Reply Quote 0
              • Wayne Filkins
                Wayne Filkins @mudassar031 last edited by

                @mudassar031 I don't know what resampling is. I don't understand it. I googled it and it made no sense there either. Apparently that's what i'm missing though because I wasn't doing w/e it is lol.

                M 1 Reply Last reply Reply Quote 0
                • M
                  mudassar031 @Wayne Filkins last edited by

                  @Wayne-Filkins
                  resample data means if you have minute of data and you want to convert it into 15 minutes period or 30 minutes period etc...Screen Shot 2020-06-30 at 3.10.24 PM.png

                  Wayne Filkins 1 Reply Last reply Reply Quote 2
                  • Wayne Filkins
                    Wayne Filkins @mudassar031 last edited by

                    @mudassar031 Thanks, I was googling it and getting the statistics definition. I get it now :) I have literally done it before when working on machine learning (with stock candle data) just never knew the term for it haha

                    M 1 Reply Last reply Reply Quote 0
                    • M
                      mudassar031 @Wayne Filkins last edited by

                      @Wayne-Filkins
                      haha no worries you are welcome

                      1 Reply Last reply Reply Quote 0
                      • Wayne Filkins
                        Wayne Filkins @mudassar031 last edited by

                        @mudassar031 If that's supposed to be run live, why do you have historical=True and fromdate and todate? When I try to run it it just does a backtest.

                        1 Reply Last reply Reply Quote 0
                        • Wayne Filkins
                          Wayne Filkins @mudassar031 last edited by

                          @mudassar031 When I changed historical and got rid of todate it did run without the error though! That's a first for me using minutes.

                          1 Reply Last reply Reply Quote 0
                          • Wayne Filkins
                            Wayne Filkins last edited by

                            @mudassar031 said in TimeFrame.Days works but TimeFrame.Minutes doesn't:

                            if not alpaca_paper:
                            broker = store.getbroker() # or just alpaca_backtrader_api.AlpacaBroker()
                            cerebro.setbroker(broker)

                            this part confuses me:

                            if not alpaca_paper:
                              broker = store.getbroker()  # or just alpaca_backtrader_api.AlpacaBroker()
                              cerebro.setbroker(broker)
                            

                            Wouldn't it be if alpaca_paper is True? Because you use Alpaca Broker for live, not backtest right?

                            I can get it working if I do it this way, but it's not using my Alpaca paper trade account.

                            M 2 Replies Last reply Reply Quote 0
                            • M
                              mudassar031 @Wayne Filkins last edited by

                              @Wayne-Filkins
                              yes right ALPACA_PAPER=True in my case
                              right now i am using paper trade for back-testing

                              1 Reply Last reply Reply Quote 0
                              • M
                                mudassar031 @Wayne Filkins last edited by

                                @Wayne-Filkins
                                here is my .env file format

                                ALPACA_PAPER=True
                                API_KEY_ID=xxxxxxxxxxxxxxx
                                API_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxx
                                
                                1 Reply Last reply Reply Quote 0
                                • Wayne Filkins
                                  Wayne Filkins last edited by

                                  I'm getting the error when live paper trading (not backtesting)

                                  M 1 Reply Last reply Reply Quote 0
                                  • M
                                    mudassar031 @Wayne Filkins last edited by

                                    @Wayne-Filkins
                                    What is the error ?

                                    1 Reply Last reply Reply Quote 0
                                    • Wayne Filkins
                                      Wayne Filkins last edited by

                                      Original post we are commenting on lol

                                      M 1 Reply Last reply Reply Quote 0
                                      • M
                                        mudassar031 @Wayne Filkins last edited by

                                        @Wayne-Filkins
                                        i have't tried live trading yet
                                        please check this
                                        https://forum.alpaca.markets/t/lagging-and-dropping-connections/714/4

                                        Wayne Filkins 1 Reply Last reply Reply Quote 0
                                        • Wayne Filkins
                                          Wayne Filkins @mudassar031 last edited by

                                          I don't see how i'm exceeding 200 requests per minute. There must be something wrong with this thing. Does no one trade live with it?

                                          1 Reply Last reply Reply Quote 0
                                          • Wayne Filkins
                                            Wayne Filkins last edited by

                                            Nevermind, figured it out. At first I thought it was the date going too far back (for 1-minute data) but then deleted all alpaca stuff from my pc and reinstalled just alpaca-backtrader and now it's working. I think maybe it was somehow using some old keycodes from the alpaca api in a different folder or something...idk really. Got it working though I think!

                                            M 1 Reply Last reply Reply Quote 1
                                            • 1
                                            • 2
                                            • 1 / 2
                                            • First post
                                              Last post
                                            Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
                                            $(document).ready(function () { app.coldLoad(); }); }