For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

Different results running strategy than optimization results

  • I have a very simple MA strategy but when I run the strategy I get a different result than what I see through the optimizer using the same values:

      # Initialize the MAs
      def __init__(self):
          self.sma_slow = bt.indicators.SimpleMovingAverage(
              self.datas[0], period=self.params.maslow)
          self.sma_fast = bt.indicators.SimpleMovingAverage(
              self.datas[0], period=self.params.mafast)
        # Buy if fast cross above slow
        def next(self):
            if not self.position:
                if self.sma_fast[0] > self.sma_slow[0]:
                    self.order =
                if self.sma_fast[0] < self.sma_slow[0]:
                    self.order = self.sell()

    I ran the optimizer

    strats = cerebro.optstrategy(
        mafast=range(11, 100),

    and logged the results

        def stop(self):
            self.log('(MA Fast %2d Slow %2d) Ending Value %.2f' %
                     (self.params.mafast, self.params.maslow,

    I got the best result as
    2000-12-29, (MA Fast 54 Slow 1) Ending Value 1305.80

    so I ran the strategy

    cerebro.addstrategy(TestStrategy, mafast = 1, maslow = 54, printlog = True)

    But I only get a final value of 595.40

    print('Final Portfolio Value: %.2f' %
    Final Portfolio Value: 595.40

    What could be causing the difference of 1305.80 during the optimization run and 595.40 during the individual strategy run?

  • @sfkiwi according to your output from optimization mafast=54 and maslow=1. Then you run your strategy with mafast = 1, maslow = 54. Maybe this can cause a difference.

Log in to reply