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/

    Using backtrader to filter and plot symbols

    General Discussion
    3
    9
    256
    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.
    • Alireza Mastery
      Alireza Mastery last edited by

      Hello

      is it possible to use backtrader to filter symbols based on a certain condition?
      for example finding symbols that are above 20-day moving average and plot those symbols with that moving average

      thank you

      tianjixuetu 1 Reply Last reply Reply Quote 0
      • tianjixuetu
        tianjixuetu @Alireza Mastery last edited by

        @Alireza-Mastery you can do it。it is just a for-loop all the symbols.

        1 Reply Last reply Reply Quote 1
        • Alireza Mastery
          Alireza Mastery last edited by

          thank you for your answer
          can you elaborate where the for loop should be used?
          how the result can be extracted for a certain condition?

          1 Reply Last reply Reply Quote 0
          • Alireza Mastery
            Alireza Mastery last edited by

            @tianjixuetu sorry forgot to mention!

            1 Reply Last reply Reply Quote 0
            • Alireza Mastery
              Alireza Mastery last edited by

              for example in this code:

              import backtrader as bt
              
              
              class TestStrategy(bt.Strategy):
                  params = (
                      ('ma_period' , 20) ,
                  )
              
                  def __init__(self):
                      self.inds = dict()
                      for i , d in enumerate(self.datas):
                          self.inds[d] = dict()
                          self.inds[d]['EMA'] = bt.indicators.ExponentialMovingAverage(self.datas[i] , period=self.params.ma_period)
              
              
              class analyze:
              
                  def __init__(self , strategy):
                      self.strategy = strategy
                      self.cerebro = bt.Cerebro(stdstats=False)
              
                      # Add a strategy:
                      self.cerebro.addstrategy(strategy)
              
                      # load data:
                      # (function to load data)
              
                      self.cerebro.run()
              
              
              if __name__ == '__main__':
                  test = analyze(strategy=TestStrategy)
              
              1 Reply Last reply Reply Quote 0
              • Alireza Mastery
                Alireza Mastery last edited by

                the solution I came up with is this:

                import backtrader as bt
                
                
                class TestStrategy(bt.Strategy):
                    params = (
                        ('ma_period' , 20) ,
                    )
                
                    def __init__(self):
                        self.result = []
                        self.inds = dict()
                        for i , d in enumerate(self.datas):
                            self.inds[d] = dict()
                            self.inds[d]['EMA'] = bt.indicators.ExponentialMovingAverage(self.datas[i] , period=self.params.ma_period)
                
                
                
                class analyze:
                
                    def __init__(self , strategy):
                        self.strategy = strategy
                        self.cerebro = bt.Cerebro(stdstats=False)
                
                        # Add a strategy:
                        self.cerebro.addstrategy(strategy)
                
                        # load data:
                        # (function to load data)
                
                        self.cerebro.run()
                
                
                if __name__ == '__main__':
                    test = analyze(strategy=TestStrategy)
                
                1 Reply Last reply Reply Quote 0
                • Alireza Mastery
                  Alireza Mastery last edited by

                  the solution I came up with is this:

                  import backtrader as bt
                  
                  
                  class TestStrategy(bt.Strategy):
                      params = (
                          ('ma_period' , 20) ,
                      )
                  
                      def __init__(self):
                          self.result = []
                          self.inds = dict()
                          for i , d in enumerate(self.datas):
                              self.inds[d] = dict()
                              self.inds[d]['EMA'] = bt.indicators.ExponentialMovingAverage(self.datas[i] , period=self.params.ma_period)
                  
                      def next_open(self):
                          if len(self.data) == self.data.buflen():
                              for i , d in enumerate(self.datas):
                                  if self.inds[d]['EMA'][0] < self.datas[i].close[0]:
                                      self.result.append(self.datas[i]._name)
                  
                      def stop(self):
                          print(self.result)
                  
                  class analyze:
                  
                      def __init__(self , strategy):
                          self.strategy = strategy
                          self.cerebro = bt.Cerebro(stdstats=False)
                  
                          # Add a strategy:
                          self.cerebro.addstrategy(strategy)
                  
                          # load data:
                          # (function to load data)
                  
                          self.cerebro.run()
                  
                  
                  if __name__ == '__main__':
                      test = analyze(strategy=TestStrategy)
                  
                  
                  
                  1 Reply Last reply Reply Quote 0
                  • Alireza Mastery
                    Alireza Mastery last edited by

                    by the way, how a post can be edited in this forum? :)

                    A 1 Reply Last reply Reply Quote 0
                    • A
                      ab_trader @Alireza Mastery last edited by

                      @Alireza-Mastery editing is disabled

                      • If my answer helped, hit reputation up arrow at lower right corner of the post.
                      • Python Debugging With Pdb
                      • New to python and bt - check this out
                      1 Reply Last reply Reply Quote 0
                      • 1 / 1
                      • First post
                        Last post
                      Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors