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/

    GenericCSVData not feeding data?

    General Code/Help
    2
    5
    740
    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.
    • Brian Lin
      Brian Lin last edited by Brian Lin

      Hello, I am just working on the Quickstart tutorial, except I am trying to use the GenericCSVData to parse per minute data:

      Here is a sample of the csv file:

      3/12/2018 16:00:00,97.025,97.07,96.76,96.77,2661032,1
      3/12/2018 15:59:00,96.9871,97.03,96.98,97.03,141136,2
      3/12/2018 15:58:00,96.95,96.99,96.93,96.99,132356,3
      3/12/2018 15:57:00,96.93,96.95,96.9,96.94,111733,4
      3/12/2018 15:56:00,96.91,96.98,96.9,96.93,132513,5
      3/12/2018 15:55:00,96.99,96.99,96.89,96.91,128721,6
      3/12/2018 15:54:00,96.9844,97.01,96.965,96.99,93297,7
      3/12/2018 15:53:00,97.01,97.025,96.975,96.9881,62685,8
      3/12/2018 15:52:00,97.01,97.04,96.98,97.005,106785,9
      3/12/2018 15:51:00,97.08,97.11,97.01,97.02,168636,10
      3/12/2018 15:50:00,97.18,97.21,96.95,97.08,145091,11

      My code to read the data is as follows:

      data = bt.feeds.GenericCSVData(
      dataname = 'intraday_1min_MSFT_1.csv',
      openinterest = -1,
      fromdate=datetime.datetime(2018, 2, 27),
      todate=datetime.datetime(2018, 3, 12),
      dtformat=('%m/%d/%Y %H:%M:%S'),
      headers = False,
      timeframe = bt.TimeFrame.Minutes,
      )

      It simply outputs:
      Starting Portfolio Value: 1000000.00
      Final Portfolio Value: 1000000.00

      It doesn't show any portfolio actions(buying/selling)

      This is my strategy code(which I just copied from quickstart)

      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 self.dataclose[0] < self.dataclose[-1]:
              # current close less than previous close
      
              if self.dataclose[-1] < self.dataclose[-2]:
                  # previous close less than the previous close
      
                  # BUY, BUY, BUY!!! (with all possible default parameters)
                  self.log('BUY CREATE, %.2f' % self.dataclose[0])
                  self.buy()
      

      I noticed something interesting, when I commented out the fromdate and todate lines in the GenericCSVData, it does output trades, but does so in backwards date order.

      B 1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators @Brian Lin last edited by

        @brian-lin said in GenericCSVData not feeding data?:

        3/12/2018 16:00:00,97.025,97.07,96.76,96.77,2661032,1
        3/12/2018 15:59:00,96.9871,97.03,96.98,97.03,141136,2
        3/12/2018 15:58:00,96.95,96.99,96.93,96.99,132356,3

        @brian-lin said in GenericCSVData not feeding data?:

        I noticed something interesting, when I commented out the fromdate and todate lines in the GenericCSVData, it does output trades, but does so in backwards date order.

        Isn't the data in backwards order?

        1 Reply Last reply Reply Quote 0
        • Brian Lin
          Brian Lin last edited by Brian Lin

          Should the data be fed in from oldest to newest? I wasn't sure about that.

          I did try and feed it in in reversed order, and it printed in proper order, however I still got a strange result.

          My strategy code is the same as you can see. It only creates buy orders, and not sells. However at the end of my code execution I end up with more money in my account than I started with

          Eg:

          I started with 1,000,000
          .
          .
          .
          2018-03-12, Close, 97.00
          2018-03-12, BUY CREATE, 97.00
          2018-03-12, Close, 96.99
          2018-03-12, BUY CREATE, 96.99
          2018-03-12, Close, 96.99
          2018-03-12, Close, 96.91
          2018-03-12, Close, 96.93
          2018-03-12, Close, 96.94
          2018-03-12, Close, 96.99
          2018-03-12, Close, 97.03
          2018-03-12, Close, 96.77
          Final Portfolio Value: 1002221.66
          Final Portfolio Value: 1002221.66

          B 1 Reply Last reply Reply Quote 0
          • B
            backtrader administrators @Brian Lin last edited by

            @brian-lin said in GenericCSVData not feeding data?:

            Should the data be fed in from oldest to newest? I wasn't sure about that.

            If you want to trade forward, it seems pretty obvious you should be feeding the data forwards. If data were coming from a socket, with a bar each 10 seconds, how should the platform know which data goes first and which second?

            @brian-lin said in GenericCSVData not feeding data?:

            My strategy code is the same as you can see. It only creates buy orders, and not sells. However at the end of my code execution I end up with more money in my account than I started with

            You have a greater value. It isn't cash.

            Let me suggest that you get used with the basics of trading and basics like portfolio value, before you go any further.

            Brian Lin 1 Reply Last reply Reply Quote 0
            • Brian Lin
              Brian Lin @backtrader last edited by

              You have a greater value. It isn't cash.

              I missed that totally. Thanks for the help.

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