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/

    Optstrategy

    General Code/Help
    2
    6
    2845
    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.
    • Y
      yjy last edited by

      I'm trying to use cerebro.optstrategy for some optimization, but it doesn't seem to be working.

      I have started with cerebro.optstrategy(myStrat, a=5, b=10) to try a single combination. Then I say cerebro.run().

      It works with cerebro.addstrategy but when I run the optstrategy, the run doesn't stop and I don't know why... Hope you can help.

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

        That information is simply too little to work with. A minimum working snippet is always helpful.

        1 Reply Last reply Reply Quote 0
        • Y
          yjy last edited by

          Sorry, I didn't have access to a computer at the time I was writing. Here is what I was trying to do. Assuming I was running the sample code given by backtrader front page:

          from datetime import datetime
          import backtrader as bt
              
          class SmaCross(bt.SignalStrategy):
                  params = (('pfast', 10), ('pslow', 30),)
                  def __init__(self):
                      sma1, sma2 = bt.ind.SMA(period=self.p.pfast), bt.ind.SMA(period=self.p.pslow)
                      self.signal_add(bt.SIGNAL_LONG, bt.ind.CrossOver(sma1, sma2))
              
          cerebro = bt.Cerebro()
          
          data = bt.feeds.YahooFinanceData(dataname='YHOO', fromdate=datetime(2011, 1, 1), todate=datetime(2012, 12, 31))
          
          cerebro.adddata(data)
          
          cerebro.addstrategy(SmaCross)
          cerebro.run()
          

          Now, assuming that I want to adjust various pfast and pslow value using the optstrategy function.

          When I replace

          cerebro.addstrategy(SmaCross)
          cerebro.run()
          

          with

          cerebro.optstrategy(SmaCross, pfast=(10,20,30), pslow=(60, 80, 100))
          cerebro.run()
          

          the code just keeps running for a long time without printing out anything. I'm just wondering if I'm missing something. Thanks.

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

            @yjy said in Optstrategy:

            the code just keeps running for a long time without printing out anything

            That's what happens during an optimization, things take longer. In any case the code contains no output statements (print) for example. The real question would what's the meaning of long time.

            Since you use YahooFinanceData you have to take into account that the servers for the original data are down and only the newest version (a few days old) can re-download from the alternative service (which may or may not be there in the future)

            1 Reply Last reply Reply Quote 0
            • Y
              yjy last edited by

              Thank you for the quick response. Buy long time, I actually meant it never really stopped running. And in my real code, I do have lots of print functions which I was able to see using the original code

              cerebro.addstrategy(SmaCross)
              cerebro.run()
              

              Of course, I may just have a really slow computer.

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

                One of the things about a snippet which reports a problem .. it has to actually produce the problem. And obviously the one above isn't the real one.

                In any case

                1. Printing to stdout during parallel processing makes no sense, because the output will get intermingled, rather sooner than later.
                2. If you are running under Windows, the output to stdout is buffered and it takes a large amount of output to produce a printout.
                1 Reply Last reply Reply Quote 0
                • 1 / 1
                • First post
                  Last post
                Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors