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/

    A really dumb question that I can somehow not find the answer to

    General Code/Help
    4
    7
    32
    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.
    • K
      kfue last edited by

      I'm building a list in my init of my strategy like so

      for d in self.datas:
                  self.stoch_lst.append(bt.ind.Stochastic(d, period=14, period_dfast=3, safediv=True))
      

      Really simple and it's working.

      Then in my next how do I get the d period? I'm accessing the k value like so

      self.stoch_lst[i][0]
      

      and then using it without getting the 0 index for comparisons, but how would I get the d value from the stochastic? Or any other indicator that produces more than one line.

      I'm sure this is really simple but I can't find it on the docs and my google searches have came up empty somehow.
      Thanks.

      T 2 Replies Last reply Reply Quote 0
      • T
        techydoc @kfue last edited by

        @kfue here is a code snippet based on what I use to add multiple indicators to a list of stocks.

        import backtrader as bt
        import collections
        
        class St(bt.Strategy):
            def __init__(self):
        
                # to hold the indicators
                self.inds = collections.defaultdict(dict) 
        
               # Iterate over datafeeds and add any indicators for each datafeed
                for d in self.datas:
                   # Add an indicator and use a string to name which indicator
                    self.inds[d]['stoch'] = bt.ind.Stochastic(d, period=14, period_dfast=3, safediv=True)
                    # You can add mutiple indicators to a datafeed
                    self.inds[d]['movav'] = bt.ind.SMA(d, period=28)
            
                # to get the period for the stochastic indicator
                print(self.inds[self.data0]['stoch'].p.period)
        
        data = bt.feeds.BacktraderCSVData(dataname='./data/yourdata.csv')
        
        cerebro = bt.Cerebro()
        cerebro.adddata(data)
        
        cerebro.addstrategy(St)
        cerebro.run()
        
        
        1 Reply Last reply Reply Quote 0
        • T
          techydoc @kfue last edited by

          @kfue I think for you example instead of

          self.stoch_list[i][0]
          

          you could use

          self.stoch_list{i].p.period
          
          T K 2 Replies Last reply Reply Quote 0
          • T
            techydoc @techydoc last edited by

            @techydoc typo you { brackets should all be []

            (sux you cannot edit posts)

            1 Reply Last reply Reply Quote 0
            • K
              kfue @techydoc last edited by

              @techydoc That's almost what I want. In the stochastic it should contain a "slowD" and a "slowK" or something of that nature. A d and a k value. The k is the default, how do I get the k.

              Also, is this documented somewhere? There's this documentation but it doesn't contain any details of what these objects actually contain which is very frustrating. https://www.backtrader.com/docu/indautoref/

              A run-out 2 Replies Last reply Reply Quote 0
              • A
                ab_trader @kfue last edited by

                @kfue yep, it is documented exactly by the reference you have in your post. Stochastic has two lines percD and percK, which keeps the values the same way like data has lines open, close etc. And can be accessed the same way.

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

                  @kfue said in A really dumb question that I can somehow not find the answer to:

                  Also, is this documented somewhere? There's this documentation but it doesn't contain any details of what these objects actually contain which is very frustrating. https://www.backtrader.com/docu/indautoref/

                  It pretty well documented here. https://www.backtrader.com/docu/indautoref/#stochastic

                  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(); }); }