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



  • 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



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



  • 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?



  • @tianjixuetu sorry forgot to mention!



  • 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)
    


  • 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)
    


  • 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)
    
    
    


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



  • @Alireza-Mastery editing is disabled


Log in to reply
 

});