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/

    Optimization returns the same set of parameters, does not cycle through the parameter ranges

    General Code/Help
    1
    1
    28
    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.
    • A
      azzam last edited by

      Trying to run a simple strategy, but the optimizer doesn't seem to cycle through all parameter options. It returns the same set of parameters.

      This is my strategy

      class Momentum(BasicStrategy):
          params = (
              ('maperiod', 25),
              ('rsiperiod', 3),
          )
      
          def __init__(self):
              # Add a MovingAverageSimple indicator
      
              self.wma = bt.indicators.WMA(self.datas[1], period=self.p.maperiod)
              self.rsi = bt.indicators.RSI(self.datas[1], period=self.p.rsiperiod)
              self.slope = bt.indicators.RateOfChange100(self.wma, period=1)
              self.buy_signal = bt.And(self.rsi() < 35.0,  self.slope(-1) > 0.0)
              self.sell_signal = bt.And(self.rsi() > 65.0, self.slope(-1) < 0.0)
              self.order = None
          
          def next(self):
              if self.order:
                  return
              if self.buy_signal and self.position.size <= 0:
                  self.buy()
              if self.sell_signal and self.position.size >= 0:
                  self.sell()
      

      The optimization part

          cerebro.optstrategy(
              Momentum,
              maperiod=range(30, 50, 10),
              rsiperiod=range(3, 5, 2),
          )
      
          
          cerebro.addanalyzer(bt.analyzers.DrawDown, _name="drawdown")
          cerebro.addanalyzer(bt.analyzers.Returns, _name="returns")
       
          # clock the start of the process
          tic = time.perf_counter()
      
          # Run over everything
          results = cerebro.run()
      
          # clock the end of the process
          toc = time.perf_counter()
          # import pdb; pdb.set_trace()
          for run in results:
              for strat in run:
                  par_list = [[strat.params.maperiod,
                               strat.params.rsiperiod,
                               strat.analyzers.returns.get_analysis()['rnorm100'],
                               strat.analyzers.drawdown.get_analysis()['max']['drawdown'],
                               0
                               #x[0].analyzers.sharpe.get_analysis()['sharperatio']
                  ] for x in results]
          
          par_df = pd.DataFrame(par_list, columns=['maperiod',
                                                   'rsiperiod',
                                                   'return',
                                                   'dd',
                                                   'sharpe'])
          print(par_df)
      

      here is the output

        maperiod  rsiperiod    return        dd  sharpe
      0        40          3  0.000152  8.268218       0
      1        40          3  0.000152  8.268218       0
      Time used: -217.93248052499985
      
      

      First, it seems that I arbitrary get a single set of parameters, here for example it is (40, 3). If I change the parameter range, I get another pair that keeps repeating. Notice it is not the first of the range (which should be (30,3)) and note top of the range.

      Any help into this?

      1 Reply Last reply Reply Quote 0
      • 1 / 1
      • First post
        Last post
      Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors