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/

    backtest with tick data, get period datas

    General Code/Help
    2
    4
    3647
    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
      ct last edited by

      Hi,

      I have tick data in csv file. I want to calculate last n (x minutes, hourly, daily) period avg from this tick data. How I can backtest my strategy and get period value list?

      Best

      ct

      time,last
      2016-02-01 17:44:59,90.75
      2016-02-01 17:44:59,90.75
      2016-02-01 17:44:59,90.75
      2016-02-01 17:44:59,90.75
      2016-02-01 17:44:59,90.75
      2016-02-01 17:44:59,90.72
      2016-02-01 17:44:58,90.75
      2016-02-01 17:44:58,90.75
      2016-02-01 17:44:58,90.75
      
      1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators last edited by

        You probably first need to reverse those ticks because lower ticks have an old timestamp.

        Tick Data Resampling is covered for example in this Blog - Tick Data and Resampling

        Since you only have last, the most obvious choice when passing that to the generic csv parser is:

            open=1,
            high=1,
            low=1,
            high=1,
        

        to make sure the data has something that looks normal. You may also want to set volume and openinterest to -1 since you have none.

        The generic csv data feed is documented with the others here: Docs - Data Feeds Reference

        1 Reply Last reply Reply Quote 0
        • C
          ct last edited by

          I converted to ohlc tick manually and started cerebro with resampledata. You can see at below. I can't understand, how I can work on period datas in strategy next or other function?

          I expected a property on strategy class for periods calculating on the fly for every tick. Next function calling for every tick am I right?

          Thank you for quick response and interest

          Best

          ct

          Datetime,Open,High,Low,Close,Volume,OpenInterest
          2016-02-01 17:44:59,90.75,90.75,90.75,90.75,1.0,0
          2016-02-01 17:44:59,90.75,90.75,90.75,90.75,2.0,0
          2016-02-01 17:44:59,90.75,90.75,90.75,90.75,1.0,0
          2016-02-01 17:44:59,90.75,90.75,90.75,90.75,40.0,0
          2016-02-01 17:44:59,90.75,90.75,90.75,90.75,2.0,0
          2016-02-01 17:44:59,90.72,90.72,90.72,90.72,2.0,0
          2016-02-01 17:44:58,90.75,90.75,90.75,90.75,4.0,0
          2016-02-01 17:44:58,90.75,90.75,90.75,90.75,4.0,0
          2016-02-01 17:44:58,90.75,90.75,90.75,90.75,1.0,0
          
          
          
          class TestStrategy(bt.Strategy):
          
              def log(self, txt, dt=None):
                  ''' Logging function fot this strategy'''
                  dt = dt or self.datas[0].datetime.date(0)
                  print('%s, %s' % (dt.isoformat(), txt))
          
              def __init__(self):
                  # Keep a reference to the "close" line in the data[0] dataseries
                  self.dataclose = self.datas[0].close
          
              def next(self):
                  # Simply log the closing price of the series from the reference
                  self.log('Close, %.2f' % self.dataclose[0])
          
          if __name__ == '__main__':
              # Create a cerebro entity
              cerebro = bt.Cerebro()
          
              # Add a strategy
              cerebro.addstrategy(TestStrategy)
              # Load the Data
              datapath = 'csv/tickdata.csv'
          
              data = btfeeds.BacktraderCSVData(
                  dataname=datapath,
                  timeframe=bt.TimeFrame.Ticks,
                  compression=1,  # 1 is the default
                  rtbar=True,  # use RealTimeBars
              )
          
              # Add the Data Feed to Cerebro
              cerebro.resampledata(data, timeframe=bt.TimeFrame.Days, compression=20)
          
              # Set our desired cash start
              cerebro.broker.setcash(100000.0)
           
          
          1 Reply Last reply Reply Quote 0
          • B
            backtrader administrators last edited by

            first need to reverse those ticks because lower ticks have an old timestamp.

            Your ticks are in the wrong order. The resampling process can only work forward. That's the reason your ticks are not being resampled.

            The more recent timestamps are at the top of the file and most of the world (Yahoo being an exception) works with files which have the more recent timestamps at the bottom.

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