Optstrategy



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


  • administrators

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



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


  • administrators

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



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


  • administrators

    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.

Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.