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/

    Daily datafeed works BUT intraday doesn't

    General Code/Help
    3
    10
    1135
    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.
    • C
      chhrissi2909 last edited by

      Hello,

      I downloaded a daily data from yahoo and my intraday from tingoo.

      My Code looks like this:

      import os, sys, argparse
      import pandas as pd 
      import backtrader as bt 
      from Strategien.GoldenCross import GoldenCross
      import datetime
      from tiingo import TiingoClient
      
      cerebro = bt.Cerebro()
      cerebro.broker.setcash(100000)
      
      symbol = "AAPL"
      path = "/Users/me/Desktop/allgemein/Visual Studio/Stock Data/S&P500 Aktien 1H/" + symbol + ".csv"
      pathIndex = "/Users/me/Desktop/allgemein/Visual Studio/Stock Data/Index EOD Data/^GSPC.csv"
      # read csv, parse date, use dates col as index
      stock_prices = pd.read_csv(pathIndex, index_col=[0], parse_dates=[0])
      #stock_prices['date'] = pd.to_datetime(stock_prices['date']) #object to datetime
      #stock_prices.date = pd.to_datetime(stock_prices.date, format='%Y-%m-%dT%H:%M:%S.%fZ')
      
      print(stock_prices.dtypes)
      print(stock_prices)
      # create data feed pandas df
      
      feed = bt.feeds.PandasData(dataname=stock_prices)
      # add feed
      cerebro.adddata(feed)
      
      #cerebro.addstrategy(GoldenCross)
      cerebro.run()
      cerebro.plot(style='candle')
      

      If I use the pathIndex as feed, then I get plotted my chart with cerebro. But if I use the path of my tiingo data, which is an intraday data (1H data), than I get the output gives me an error called: 'numpy.int64' object has no attribute 'to_pydatetime'.
      I also have to mention, that my intraday format looks like this...

      dtype: object
                                date    close     high      low     open
      0     2017-01-03T15:00:00.000Z  115.450  115.815  115.400  115.600
      1     2017-01-03T16:00:00.000Z  115.370  115.670  115.135  115.450
      2     2017-01-03T17:00:00.000Z  115.470  115.525  115.270  115.365
      3     2017-01-03T18:00:00.000Z  115.235  115.495  115.235  115.475
      4     2017-01-03T19:00:00.000Z  115.435  115.445  115.160  115.235
      ...                        ...      ...      ...      ...      ...
      

      thank you for your help,
      best regards Christian

      1 Reply Last reply Reply Quote 0
      • run-out
        run-out last edited by

        Backtrader defaults to daily. Backtrader cannot tell your timeframe and compression. Try to add in the timeframe and compression in the adddata.

        cerebro.adddata(feed, timeframe=bt.Timeframe.Minutes, compression=60)
        

        timeframe (default: TimeFrame.Days)

        Potential values: Ticks, Seconds, Minutes, Days, Weeks, Months and Years

        compression (default: 1)

        Number of actual bars per bar. Informative. Only effective in Data Resampling/Replaying.

        RunBacktest.com

        run-out 1 Reply Last reply Reply Quote 0
        • run-out
          run-out @run-out last edited by

          @run-out I may have this wrong, you might have to use resample.

          RunBacktest.com

          C 1 Reply Last reply Reply Quote 0
          • C
            chhrissi2909 @run-out last edited by

            Hey @run-out thank you for your answer. I tried to use:

            cerebro.adddata(feed, timeframe=bt.Timeframe.Minutes, compression=60)
            

            Now the error sounds like:
            "TypeError: adddata() got an unexpected keyword argument 'timeframe'"

            1 Reply Last reply Reply Quote 0
            • run-out
              run-out last edited by

              @chhrissi2909 said in Daily datafeed works BUT intraday doesn't:

              'numpy.int64' object has no attribute 'to_pydatetime'

              Yeah, that first response was my mistake. I suspect your error is form the tingo date format going to pandas then to backtrader? I'm really not sure but maybe give this a try.

                  df = pd.read_csv("data/tiingo.csv", index_col=0, parse_dates=True, infer_datetime_format=True)
                  df.index.name = "datetime"
                  data = bt.feeds.PandasData(dataname=df)
                  cerebro.adddata(data)
                  cerebro.run()
              
              

              RunBacktest.com

              C 1 Reply Last reply Reply Quote 0
              • C
                chhrissi2909 @run-out last edited by

                @run-out I tired it, but this ist more or less my old code^^.. So it does not work.. Does anyone have an other idea? I think you first thinking of resample could be a good way, but I don't know how this works.

                Because you are completely right, the defaults value is for daily data (and this data also works for mer) but the intraday data doesn't..

                run-out 1 Reply Last reply Reply Quote 0
                • run-out
                  run-out @chhrissi2909 last edited by

                  @chhrissi2909 Could you paste all of your code in please including the new method and show your errors and output? Thanks.

                  RunBacktest.com

                  C 1 Reply Last reply Reply Quote 0
                  • C
                    chhrissi2909 @run-out last edited by

                    @run-out here the code:

                    import os, sys, argparse
                    import pandas as pd 
                    import backtrader as bt 
                    from Strategien.GoldenCross import GoldenCross
                    import datetime
                    from tiingo import TiingoClient
                    
                    cerebro = bt.Cerebro()
                    cerebro.broker.setcash(100000)
                    
                    symbol = "AAPL"
                    path = "/Users/me/Desktop/allgemein/Visual Studio/Stock Data/S&P500 Aktien 1H/" + symbol + ".csv"
                    pathIndex = "/Users/me/Desktop/allgemein/Visual Studio/Stock Data/Index EOD Data/^GSPC.csv"
                    
                    # read csv, parse date, use dates col as index
                    stock_prices = pd.read_csv(path, index_col=0, parse_dates=True, infer_datetime_format=True)
                    
                    ##print(stock_prices.dtypes)
                    print(stock_prices)
                    
                    # create data feed pandas df
                    
                    stock_prices.index.name = "datetime"
                    data = bt.feeds.PandasData(dataname=stock_prices)
                    cerebro.adddata(data)
                    cerebro.run()
                    cerebro.plot(style='candle')
                    
                    1 Reply Last reply Reply Quote 0
                    • C
                      chhrissi2909 last edited by

                      For everybody in the Future :D (I fixed the problem to my self):

                      import os, sys, argparse
                      import pandas as pd 
                      import backtrader as bt 
                      from Strategien.GoldenCross import GoldenCross
                      import datetime
                      from tiingo import TiingoClient
                      
                      cerebro = bt.Cerebro()
                      cerebro.broker.setcash(100000)
                      
                      symbol = "AAPL"
                      path = "/Users/me/Desktop/allgemein/Visual Studio/Stock Data/S&P500 Aktien 1H/" + symbol + ".csv"
                      pathIndex = "/Users/me/Desktop/allgemein/Visual Studio/Stock Data/Index EOD Data/^GSPC.csv"
                      
                      # read csv, parse date, use dates col as index
                      stock_prices = pd.read_csv(path, index_col=1, parse_dates=True, infer_datetime_format=True)
                      
                      # create data feed pandas df
                      
                      stock_prices.index.name = "datetime"
                      data = bt.feeds.PandasData(dataname=stock_prices)
                      cerebro.adddata(data)
                      cerebro.run()
                      cerebro.plot(volume=False)
                      cerebro.plot(style='candle')
                      

                      there are 2 key lines, the first is, that for intraday data, if there is no volume you have to tell that backtrader like this:

                      cerebro.plot(volume=False)
                      

                      And the second key line was how to read the csv:

                      stock_prices = pd.read_csv(path, index_col=1, parse_dates=True, infer_datetime_format=True)
                      
                      
                      Thanks for all your help :)
                      
                      DJ Deo 1 Reply Last reply Reply Quote 0
                      • DJ Deo
                        DJ Deo @chhrissi2909 last edited by

                        @chhrissi2909 Why is there no volume in intraday ? we do get it for each candle ! ?

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