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 optimization never finishes

    General Code/Help
    2
    2
    173
    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.
    • amit.kehat
      amit.kehat last edited by

      Re: Optstrategy

      HI,

      When trying to use the optstrategy method, it never finish... run forever. No error is observed.
      My code:

      from datetime import datetime
      import backtrader as bt
      import matplotlib.pyplot as plt
      %matplotlib inline
      
      class SmaCross(bt.Strategy):
          
          params = (('fast', 10),
                    ('slow', 30)
                   )
          
          def __init__(self):
              self.sma_fast = bt.ind.SMA(period = self.params.fast)
              self.sma_slow = bt.ind.SMA(period = self.params.slow)
              self.crossover = bt.ind.CrossOver(self.sma_fast, self.sma_slow)
      
      
          def start(self): #runs after the init in the beginning
              pass
              
          def next(self): #runs in each iteration
              if self.position.size == 0:
                  if self.sma_fast > self.sma_slow:
                      self.buy()
              elif self.position.size != 0:
                  if self.sma_fast < self.sma_slow:
                      self.sell()
          
          def stop(self): #runs at the end
              pass
      
      contract_history = pd.read_csv('APPL-hourly.csv')
      contract_history['dt'] = pd.to_datetime(contract_history['date'])
      contract_history = contract_history.set_index('dt')
      data_pd = bt.feeds.PandasData(dataname = contract_history, fromdate = datetime(2020, 11, 11),
                                      todate = datetime(2020,12,1))
      
              
      cerebro = bt.Cerebro()
      cerebro.adddata(data_pd)
      cerebro.addsizer(bt.sizers.PercentSizer, percents = 90)
      
      cerebro.optstrategy(SmaCross, fast = (5, 10), slow = (20, 25))
      cerebro.run()
      
      
      
      

      Does someone maybe know how to resolve this issue?

      Thanks,
      Amit

      1 Reply Last reply Reply Quote 0
      • run-out
        run-out last edited by

        @amit-kehat said in Strategy optimization never finishes:

        Does someone maybe know how to resolve this issue?
        Thanks,
        Amit

        I just ran this using daily data and it's fine. You may wish to add in timeframe and compression. Also, there are no outputs in your code. Perhaps this is why you are not seeing anything?

        
        
        timeframe (default: TimeFrame.Days)
        
        Potential values: Ticks, Seconds, Minutes, Days, Weeks, Months and Years
        
        compression (default: 1)
        
        Number of actual bars per bar. Informative. Only effective in Data Resampling/Replaying.
        
        

        RunBacktest.com

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