For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Strategy output as input to another strategy



  • Hi,
    I am a novice user with very limited knowledge of coding. Love the way all the examples are put up.
    I further want to understand the process where the output of one strategy can be input to another, in this example:

    from __future__ import (absolute_import, division, print_function,
                            unicode_literals)
    
    import argparse
    import datetime
    
    import backtrader as bt
    
    
    class Screener_SMA(bt.Analyzer):
        params = dict(period=10)
    
        def start(self):
            self.smas = {data: bt.indicators.SMA(data, period=self.p.period)
                         for data in self.datas}
    
        def stop(self):
            self.rets['over'] = list()
            self.rets['under'] = list()
    
            for data, sma in self.smas.items():
                node = data._name, data.close[0], sma[0]
                if data > sma:  # if data.close[0] > sma[0]
                    self.rets['over'].append(node)
                else:
                    self.rets['under'].append(node)
    
    
    DEFAULTTICKERS = ['YHOO', 'IBM', 'AAPL', 'TSLA', 'ORCL', 'NVDA']
    
    
    def run(args=None):
        args = parse_args(args)
        todate = datetime.date.today()
        # Get from date from period +X% for weekeends/bank/holidays: let's double
        fromdate = todate - datetime.timedelta(days=args.period * 2)
    
        cerebro = bt.Cerebro()
        for ticker in args.tickers.split(','):
            data = bt.feeds.YahooFinanceData(dataname=ticker,
                                             fromdate=fromdate, todate=todate)
            cerebro.adddata(data)
    
        cerebro.addanalyzer(Screener_SMA, period=args.period)
        cerebro.run(runonce=False, stdstats=False, writer=True)
    
    
    def parse_args(pargs=None):
    
        parser = argparse.ArgumentParser(
            formatter_class=argparse.ArgumentDefaultsHelpFormatter,
            description='SMA Stock Screener')
    
        parser.add_argument('--tickers', required=False, action='store',
                            default=','.join(DEFAULTTICKERS),
                            help='Yahoo Tickers to consider, COMMA separated')
    
        parser.add_argument('--period', required=False, action='store',
                            type=int, default=10,
                            help=('SMA period'))
    
        if pargs is not None:
            return parser.parse_args(pargs)
    
        return parser.parse_args()
    
    
    if __name__ == '__main__':
        run()
    

    I am looking for a case where, if ticker data is above SMA then display over/under, feed over tickers to sma cross 20 over 50 strategy and then display over/under, again feed over tickers to RSI/MACD strategy, etc. Final strategy will give the result of tickers that has come over for all strategies. A solution will be to implement strategy for each and feed output to next strategy, that way, at the end I will only the ones that are over for all strategy tests. Is there a way this can be done?


  • administrators

    Yes. Use a common communication channel and let the last strategy be the only one that trades using the messages from the communication channel.



  • @backtrader thanks, can you please help an example? or would be great if you can share the reference, if this is already mentioned in any previous instance.


Log in to reply
 

});