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

Strategy PnL is 0 when using optimizer

  • Hello again,

    Now that the data can properly be analised (see, 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.rsi = bt.indicators.RSI_SMA(, period=self.params.period)
        def next(self):
            if not self.position:
                if self.rsi < 30:
                if self.rsi > 70:
    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(
            fromdate=datetime(2019, 5, 1, 0, 0),
            todate=datetime(2020, 5, 1, 0, 0),
        # Add the data to Cerebro
        # Set our desired cash start
        # Run over everything
        opt_runs =
        # Generate results list
        final_results_list = []
        for run in opt_runs:
            for strategy in run:
                value = round(, 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?

  • Try to increase your cash.

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

    Backtest Rookies

    Are they asking for money to help with their scripts?

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

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

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

Log in to reply