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/

    PercentRank

    General Code/Help
    2
    12
    92
    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.
    • A
      AlfredSisley last edited by

      Hi All,

      Looking for clarification regarding the PercentRank indicator.

      Specifically, looking to understand if it's possible to get the PercentRank of another indicator.

      Would be used in a multi instrument strategy, something like:

      def __init__(self):
      
              self.o = dict() 
              self.holding = dict() 
              self.ema = []
              self.percentrank = []
      
              for i, d in enumerate(self.datas):   
                  self.ema.append(bt.ind.EMA(self.datas[i],period=2))
                  self.percentrank.append(bt.ind.EMA(self.datas[i],period=2),200)
      

      The error I've been getting has been the following:

      TypeError: __init__() takes 1 positional argument but 2 were given
      

      The documentation states that 2 arguments are required (data + lookback period).

      Is this possible? If so, how would I go about doing this?

      Thanks in advance.

      Alfred Sisley

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

        Could you clarify what you are trying to do? Your error is most likely coming from

        self.percentrank.append(bt.ind.EMA(self.datas[i],period=2),200)
        

        You have defined self.percentrank as a list and you are trying to append two items to it, where only one is allowed.

        I'm not sure where you are going with this. Provide some more information and we'll try to help you out.

        1 Reply Last reply Reply Quote 1
        • A
          AlfredSisley last edited by

          The error is coming from that line 100%.

          Trying to get the percentile of any indicator value over the last x periods.

          Was originally trying to see if there was a way to somehow access all indicator values (value at each time period) in the data and apply percentile to see on any given day where it stands relative to history.

          Would prefer the later but would settle for the former.

          Let me know if you need further explanation.

          Thanks!

          Alfred Sisley

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

            For one indicator comparing againt itself and going back n periods.

            # In the strategy class...
             def __init__(self):
                    self.prank = bt.ind.PctRank(period=5)
            
            # results in with 0.00 the bottom and 0..80 the top rank: 
            2005-05-17T00:00:00, o 12.26	h 12.28	l 11.98	c 12.21	v 61633100	prank 0.40
            2005-05-18T00:00:00, o 12.21	h 12.43	l 12.13	c 12.33	v 44940000	prank 0.40
            2005-05-19T00:00:00, o 12.36	h 12.64	l 12.35	c 12.43	v 40463700	prank 0.80
            2005-05-20T00:00:00, o 12.45	h 12.64	l 12.40	c 12.55	v 25230900	prank 0.80
            2005-05-23T00:00:00, o 12.62	h 12.77	l 12.60	c 12.70	v 47390500	prank 0.80
            2005-05-24T00:00:00, o 12.66	h 12.86	l 12.64	c 12.80	v 43792600	prank 0.80
            2005-05-25T00:00:00, o 12.72	h 12.79	l 12.61	c 12.75	v 32280900	prank 0.60
            2005-05-26T00:00:00, o 12.82	h 12.98	l 12.77	c 12.92	v 31827400	prank 0.80
            2005-05-27T00:00:00, o 12.86	h 12.90	l 12.79	c 12.85	v 21594400	prank 0.60
            2005-05-31T00:00:00, o 12.77	h 12.87	l 12.69	c 12.80	v 28423900	prank 0.20
            2005-06-01T00:00:00, o 12.79	h 12.97	l 12.77	c 12.89	v 33329000	prank 0.60
            

            @AlfredSisley said in PercentRank:

            Was originally trying to see if there was a way to somehow access all indicator values (value at each time period) in the data and apply percentile to see on any given day where it stands relative to history.

            Could you clarify this maybe with an example?

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

              Absolutely.

              For example, let's say I had the ROC indicator for my instruments which would look something like this in a multi-instrument strategy:

              
              def __init__(self):
                  
                      self.roc = []
              
                      
                      for i, d in enumerate(self.datas):   
                          self.roc.append(bt.ind.ROC(self.datas[i], period=self.params.period)) 
                          
              

              Where percentrank would come in is when I would want the ROC output compared to historical output. This way I can assess the indicator from a historical perspective.

              My initial thought was to do something like this:

              
              def __init__(self):
                  
                      self.roc = []
                      self.percentrank = []
                      
                      for i, d in enumerate(self.datas):   
                          self.roc.append(bt.ind.ROC(self.datas[i], period=self.params.period))
                          self.percentrank.append(ROC_INDICATOR_ABOVE, period=PERIOD_FOR_TICKER)
              

              Notice above I've tried adding (in partial pseudo code) percentrank to the ROC indicator for the period of the entire dataset of a ticker.

              In essence, this is just trying to take an indicator of an indicator.

              Let me know if you need more information.

              Appreciate the help!

              Alfred Sisley

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

                @AlfredSisley

                How about this if I understand you correctly.

                def __init__(self):
                    
                        self.roc = {}
                        self.percentrank = {}
                        
                        for i, d in enumerate(self.datas):   
                              roc = bt.ind.ROC(self.datas[i], period=self.p.period
                              prank = bt.ind.PctRank(roc, period=self.p.period)
                             
                              self.roc[d] =roc
                              self.percentrank[d] = prank
                

                Then when you are in next, you can access the values like:

                for d in self.datas:
                    # then your prank value per data
                    whatever_list_or_var = self.percentrank[d]
                
                1 Reply Last reply Reply Quote 1
                • A
                  AlfredSisley last edited by

                  This is exactly what I was looking for.

                  Thank you very much.

                  Alfred Sisley

                  1 Reply Last reply Reply Quote 1
                  • A
                    AlfredSisley last edited by

                    @run-out

                    One quick follow up question. Is there a way to access all historical close values in the init for each ticker?

                    Thanks

                    Alfred Sisley

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

                      @AlfredSisley said in PercentRank:

                      One quick follow up question. Is there a way to access all historical close values in the init for each ticker?

                      For what purpose?

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

                        Looking to somehow use PctRank to get bottom percentile of historical ROC data (historical = entire data set).

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

                          If I understand you correctly you want to calculate data ahead of the back test using the entire data set. If you were to use this data to trade you have look ahead bias. Backtrader doesn't do this.

                          Unless I understand you wrong.

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

                            @run-out You are correct. Needed to go back to the drawing board as a result. Thank you.

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