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/

    Different results running strategy than optimization results

    General Code/Help
    2
    2
    520
    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.
    • S
      sfkiwi last edited by

      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 = self.buy()
              else:
                  if self.sma_fast[0] < self.sma_slow[0]:
                      self.order = self.sell()
      

      I ran the optimizer

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

      and logged the results

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

      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' % cerebro.broker.getvalue())
      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?

      A 1 Reply Last reply Reply Quote 0
      • A
        ab_trader @sfkiwi last edited by

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

        • If my answer helped, hit reputation up arrow at lower right corner of the post.
        • Python Debugging With Pdb
        • New to python and bt - check this out
        1 Reply Last reply Reply Quote 1
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors