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/

    Strategy output as input to another strategy

    General Code/Help
    2
    3
    83
    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.
    • G
      gten last edited by

      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?

      1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators last edited by

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

        G 1 Reply Last reply Reply Quote 0
        • G
          gten @backtrader last edited by

          @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.

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