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/

    How to get the first date of a datafeed?

    Indicators/Strategies/Analyzers
    2
    4
    122
    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.
    • DOM LEE
      DOM LEE last edited by

      In def init of a strategy, I need to get external data depend on the first day of data-feed. Is it possible to to that?

      # Create a Stratey
      class TestStrategy(bt.Strategy):
          params = (('maperiod', 60), )
      
          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
              self.sma = bt.indicators.SimpleMovingAverage(
                  self.datas[0], period=self.params.maperiod)
      
              # my question is here
              first_date = ?????
      
              my_ind_data = mydb.get_my_data("news_hit").loc[start_day:]
              news_ind = bt.indicators.SimpleMovingAverage(my_ind_data,period=5)
      
      
      
      DOM LEE 1 Reply Last reply Reply Quote 0
      • DOM LEE
        DOM LEE @DOM LEE last edited by

        I try this , but fail

        
        # Create a Stratey
        class TestStrategy(bt.Strategy):
            params = (('maperiod', 60), )
        
            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
                self.sma = bt.indicators.SimpleMovingAverage(
                    self.datas[0], period=self.params.maperiod)
        
                # my question is here
                first_date = self.datas[0][0]
        
                my_ind_data = mydb.get_my_data("news_hit").loc[first_data:]
                news_ind = bt.indicators.SimpleMovingAverage(my_ind_data,period=5)
        
        
        
        
        1 Reply Last reply Reply Quote 0
        • vladisld
          vladisld last edited by

          The data bars can not be accessed (examined) during the __init__ method since the bars were not yet loaded and the data feed was not yet started. You may try to access the fromdate data parameter (self.datas[0].p.fromdate) if such was supplied. However, this is also not a good option since firstly such a parameter may not be actually supplied, and secondly, the actual dates in the data feed may not exactly match the fromdate parameter.

          As for the actual reason for your question - IMHO it is better to just define a custom data feed for your database ( if such does not already exist ) and use it instead of (or in addition to) the data feed you already supply to Cerebro engine.

          DOM LEE 1 Reply Last reply Reply Quote 1
          • DOM LEE
            DOM LEE @vladisld last edited by

            @vladisld Thanks for you advice. I thought about define custom data feed, but there are thousands of stocks when I run the strategy, and they share a same "my_ind_data" line(and "my_ind_data" is not the only custom data). so that will increase memory usage obviously.

            1 Reply Last reply Reply Quote 0
            • 1 / 1
            • First post
              Last post
            Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
            $(document).ready(function () { app.coldLoad(); }); }