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/

    Grab every 4th row in data

    General Code/Help
    3
    9
    151
    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.
    • M
      mudassar031 last edited by

      I want to grab every 4th row in data without resampling in backtrader as i have done in python here is code

      ts = TimeSeries(key='av_api_key', output_format='pandas')
      data, meta_data = ts.get_intraday(symbol='MSFT', interval='60min', outputsize='full')
      # Convert the index to datetime.
      data.index = pd.to_datetime(data.index)
      data.columns = ['Open', 'High', 'Low', 'Close', 'Volume']
      # Grabs every 4th row
      data = data.iloc[::4, :]
      
      M 1 Reply Last reply Reply Quote 0
      • M
        mudassar031 @mudassar031 last edited by

        @mudassar031
        if we have minutes of data and use resampling with compression=60 so backtrader give us data after averaging 60 mins actually i want to skip data in between and want to grab specific row as i have explained above example Thanks

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

          @mudassar031 said in Grab every 4th row in data:

          I want to grab every 4th row in data

          What does it mean to 'grab' every fourth row. What will you be doing with it? It might be easier to just have a counter in next and only consider every fourth row?

          RunBacktest.com

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

            @run-out
            can you please give me an code example to consider every fourth row in next function?

            1 Reply Last reply Reply Quote 0
            • A
              ab_trader last edited by

              Try this

              def next(self):
                  if len(self.data) % 4 != 0:
                      return
              
                  # operations you want to make on every 4th bar
              
              • If my answer helped, hit reputation up arrow at lower right corner of the post.
              • Python Debugging With Pdb
              • New to python and bt - check this out
              M 3 Replies Last reply Reply Quote 2
              • M
                mudassar031 @ab_trader last edited by

                @ab_trader
                Thanks

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

                  @ab_trader
                  i have minutes of data and i want to fetch 4 hours our data but it always executing one minutes on data i want only to display row after 240th row

                       if len(self.data) % 240 != 0:
                  
                          self.log(
                              'Open: %.2f '
                              'High: %.2f '
                              'Low: %.2f '
                              'Close: %.2f '
                              % (
                                  self.data.open[0],
                                  self.data.high[0],
                                  self.data.low[0],
                                  self.data.close[0]
                              ))
                  
                  M 1 Reply Last reply Reply Quote 0
                  • M
                    mudassar031 @ab_trader last edited by

                    @ab_trader
                    another question i am using 4 hours of resampledata

                     data0 = DataFactory(
                            dataname=args.symbol or "MSFT",
                            timeframe=bt.TimeFrame.TFrame("Minutes"),
                            fromdate=pd.Timestamp('2020-07-27'),
                            todate=pd.Timestamp('2020-07-29'),
                            compression=1,
                            historical=True)
                        cerebro.adddata(data0)
                    
                        cerebro.resampledata(data0, timeframe=bt.TimeFrame.Minutes, compression=60 * 4)
                    

                    and i am fetching 4 hours resample data in def init(self):
                    like this

                    self.data4h = self.datas[1]  # 240 minute resampled data
                    

                    its giving me error

                    File "4hresample.py", line 74, in __init__
                        self.data4h = self.datas[1]  # 240 minute resampled data
                    IndexError: list index out of range
                    
                    1 Reply Last reply Reply Quote 0
                    • M
                      mudassar031 @mudassar031 last edited by

                      @mudassar031
                      I have resolved it actually
                      this must be

                      if len(self.data) % 240 == 0:
                      

                      Thanks

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