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 PnL is 0 when using optimizer

    General Code/Help
    3
    6
    50
    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.
    • Eduardo Menges Mattje
      Eduardo Menges Mattje last edited by

      Hello again,

      Now that the data can properly be analised (see https://community.backtrader.com/topic/2484/error-with-mt5-csv-file-read), I tried to run the Optimizer provided by Backtest Rookies, but it yielded no results:

      Results: Ordered by Profit:
      Period: 14, PnL: 0.0
      Period: 15, PnL: 0.0
      Period: 16, PnL: 0.0
      Period: 17, PnL: 0.0
      Period: 18, PnL: 0.0
      Period: 19, PnL: 0.0
      Period: 20, PnL: 0.0
      

      This is code of the optimizer that I'm trying to run:

      import backtrader as bt
      from datetime import datetime
      
      
      class firstStrategy(bt.Strategy):
          params = (
              ('period', 21),
          )
      
          def __init__(self):
              self.startcash = self.broker.getvalue()
              self.rsi = bt.indicators.RSI_SMA(self.data.close, period=self.params.period)
      
          def next(self):
              if not self.position:
                  if self.rsi < 30:
                      self.buy(size=100)
              else:
                  if self.rsi > 70:
                      self.sell(size=100)
      
      
      if __name__ == '__main__':
          # Variable for our starting cash
          startcash = 10000
      
          # Create an instance of cerebro
          cerebro = bt.Cerebro(optreturn=False)
      
          # Add our strategy
          cerebro.optstrategy(firstStrategy, period=range(14, 21))
      
          data = bt.feeds.MT4CSVData(
              dataname='ETHUSD.csv',
              fromdate=datetime(2019, 5, 1, 0, 0),
              todate=datetime(2020, 5, 1, 0, 0),
              timeframe=bt.TimeFrame.Minutes,
              compression=120,
          )
      
          # Add the data to Cerebro
          cerebro.adddata(data)
      
          # Set our desired cash start
          cerebro.broker.setcash(startcash)
      
          # Run over everything
          opt_runs = cerebro.run()
      
          # Generate results list
          final_results_list = []
          for run in opt_runs:
              for strategy in run:
                  value = round(strategy.broker.get_value(), 2)
                  PnL = round(value - startcash, 2)
                  period = strategy.params.period
                  final_results_list.append([period, PnL])
      
          by_PnL = sorted(final_results_list, key=lambda x: x[1], reverse=True)
      
          print('Results: Ordered by Profit:')
          for result in by_PnL:
              print('Period: {}, PnL: {}'.format(result[0], result[1]))
      
      

      It can't be a problem with the data, because if I run the analyzer, it works. I've tried to change the code, but it only broke it even more. Does anyone have any lead?

      A 1 Reply Last reply Reply Quote 0
      • vladisld
        vladisld last edited by

        Try to increase your cash.

        Eduardo Menges Mattje 1 Reply Last reply Reply Quote 0
        • A
          ab_trader @Eduardo Menges Mattje last edited by

          @Eduardo-Menges-Mattje said in Strategy PnL is 0 when using optimizer:

          Backtest Rookies

          Are they asking for money to help with their scripts?

          1 Reply Last reply Reply Quote 0
          • Eduardo Menges Mattje
            Eduardo Menges Mattje @vladisld last edited by

            @vladisld Increasing the startcash to 100000 seems to work. Thanks again.

            @ab_trader No, the scripts and tutorials that they post are free. I originally used TradingView to test my strategies, but there is no optimizer in it. After some googling (yesterday), I found their site, and now I'm experimenting with Backtrader.

            A 1 Reply Last reply Reply Quote 0
            • A
              ab_trader @Eduardo Menges Mattje last edited by

              @Eduardo-Menges-Mattje I know that their scripts are free. The weird thing is that you ask question here, but not on their website. Which would be logical using their scripts.

              Eduardo Menges Mattje 1 Reply Last reply Reply Quote 0
              • Eduardo Menges Mattje
                Eduardo Menges Mattje @ab_trader last edited by

                @ab_trader Since they didn't reply comments from 2018 asking about errors, I figured that I could get better help from here.

                1 Reply Last reply Reply Quote 0
                • 1 / 1
                • First post
                  Last post
                Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
                $(document).ready(function () { app.coldLoad(); }); }