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/

    Unable to print close price correctly. [Beginner]

    General Code/Help
    1
    4
    33
    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.
    • R
      rajthakur last edited by

      Hi Team,
      I'm new to backtrader. I was trying a simple example that prints close price from my csv. But I'm unable to do so. Can you please help me to check what am I doing wrong?

      Here is my code:

      import backtrader as bt
      import backtrader.feeds as btfeeds
      
      class PrintClose(bt.Strategy):
      
          def __init__(self):
              self.dataclose = self.datas[0].close
      
          def log(self, txt, dt=None):
              dt = dt or self.datas[0].datetime.date(0)
              print(f'{dt} {txt}')  # Print date and close
      
          def next(self):
              self.log('Close: ', self.dataclose[0])
      
      
      if __name__ == '__main__':
          cerebro = bt.Cerebro()
          cerebro.addstrategy(PrintClose)
          datapath = './RELIANCE_formatted.csv'
      
          data = btfeeds.GenericCSVData(
                      dataname=datapath,
                      dtformat=('%Y-%m-%d %H:%M:%S%z'),
                      timestamp=0,
                      high=2,
                      low=3,
                      open=1,
                      close=4,
                      volume=5,
                      openinterest=-1
                  )
          cerebro.adddata(data)
          cerebro.run()
      

      Output:
      1992.9 Close:
      1989.1 Close:
      1988.8 Close:
      1992.75 Close:
      1989.7 Close:
      1991.55 Close:
      1986.7 Close:
      1979.15 Close:
      ... and so on .

      Here is a part of my csv :

      date,open,high,low,close,volume
      2016-07-04 09:15:00+05:30,483.4,490.35,483.4,489.1,950630
      2016-07-04 10:15:00+05:30,489.1,492.05,488.55,491.45,603618
      2016-07-04 11:15:00+05:30,491.7,491.95,489.4,489.8,514331
      2016-07-04 12:15:00+05:30,489.8,490.65,488.15,488.75,374728
      2016-07-04 13:15:00+05:30,488.85,489.55,488.25,488.75,314632
      2016-07-04 14:15:00+05:30,488.75,490.3,487.45,489.15,511010
      2016-07-04 15:15:00+05:30,489.1,489.25,487.95,488.55,323005
      

      Please help me out. I have been banging my head for hours on this. Thanks in Advance

      R 1 Reply Last reply Reply Quote 0
      • R
        rajthakur @rajthakur last edited by

        @backtrader @backtraderadmin Any help please sir ?

        R 1 Reply Last reply Reply Quote 0
        • R
          rajthakur @rajthakur last edited by

          @rajthakur I figured out the issue. Closing this thread.

          1 Reply Last reply Reply Quote 0
          • R
            rajthakur last edited by

            @rajthakur How do i close this discussion ?

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