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

How to check the legality of parameters before running the test?

  • I backtest a double moving average strategy and want to optimize the two parameter, shortma and longma with the following code

    strategies = cerebro.optstrategy(DoubleMA,
                       shortma=range(5,30,5),longma=range(15, 50, 1)

    Obviously, shortma shall be small than longma, otherwise the strategy is meaningless. How should i add some code to check the legality of parameters in the __init__ , once the parameters is invalid, the strategy go the stop .

  • administrators

    This is covered by raising a exception during __init__. See Docs - Exceptions

    import backtrader as bt
    class Strategy(bt.Strategy):
        def __init__(self):
            if something_goes_wrong():
                raise bt.StrategySkipError

Log in to reply