Walk Forward Optimization in Python with Backtesting.py

Поделиться
HTML-код
  • Опубликовано: 22 дек 2024

Комментарии • 44

  • @pethiraj
    @pethiraj Год назад +1

    Amazing content and with this high quality content, wish you the very best and soon million subscribers!

  • @VaidaBogdan
    @VaidaBogdan Год назад

    Loved the video, exactly what I needed. Would love to get access to your notebooks too.

  • @a7medmostafaabuhatab
    @a7medmostafaabuhatab 11 месяцев назад +1

    I have a question, in the walk forward function, why bt.optimize not bt_training.optimize!

  • @richardchu1593
    @richardchu1593 Год назад +2

    Any chance we can add a Monte Carlo and MAE/MFE analysis please?

  • @dgarciagud
    @dgarciagud 2 года назад +2

    Are going to going to make it available somewhere?

  • @samyoe
    @samyoe Год назад

    I use an ML model that splits the data into training and validation sets within the strategy itself. So I just got rid of the validation backtest within the walk_forward function, and it worked fine.

  •  Год назад

    Thanks for your work. Is any way get the code you show in the video?

  • @VaidaBogdan
    @VaidaBogdan Год назад

    I'm curious of your view regarding what would be a very good backtesting methodology. E.g. kfold validation, walk-forward optimization (anchored or not)... there are multiple ways to look at it and you've mentioned on your website that you're into the hard stuff... I'm curios of your view on what a default / go-to method would do.
    For context: I started out splitting 60-20-20 and quickly realized I couldn't do that because I would, for example, train on a sideways market, validate on an upside and test on an upside. Then used kfold and split the timerange into 5 folds, each with its own training and testing set. That worked well enough but on higher timeframes I couldn't get enough trades to validate the results. This is my default framework but I sometimes use a walk-forward that is anchored... but I wanted to know how more experienced quants do it. If the 5 fold approach is better or worse than an unanchored walk forward or than an anchored one or if there is another way to look at this that is escaping me.

    • @Yash21000
      @Yash21000 Год назад

      Hey I'm also trying to understand this topic. Can I please connect with you and possibly recieve some guidance?

  • @FabioArcari-n3l
    @FabioArcari-n3l Год назад

    Hi, is there a way to download the .py you're explaining? Thanks

  • @christolabuschagne4382
    @christolabuschagne4382 Год назад +1

    I've been binge watching your stuff for a few days now. This is a great video with a solid idea. My only problem is that my strategy allows for multiple trades to be opened, but as soon as the week ends, it closes all my open trades, and this causes my performance to tank a bit. If I run it over full data set I get very good results, but slicing it like this really kills my return %. Not sure if something can be done about it though...

    • @ChadThackray
      @ChadThackray  Год назад

      Generally if the strategy does significantly worse in walk forward, you've probably over-fit.
      Would recommend doing some forward testing / paper trading to get a better idea of what's going on. Especially if you're trading on shorter timeframes. Importantly you should not make any changes to the strategy whilst forward testing, or you'll risk more over-fitting.

    • @christolabuschagne4382
      @christolabuschagne4382 Год назад +1

      I've been fixing my code and strategy. Currently optimising for two month, walking forward for a week. Also updated your code to get current equity from the walk forward week for the next week. In your experience, if walk forward does well, how does it compare with real life?

  • @aarondelarosa3146
    @aarondelarosa3146 Год назад

    Hey Chad. I can't install backtesting on terminal. What do I do? I'm using Jupyter Notebook VSC (Visual Studio Code).

    • @ChadThackray
      @ChadThackray  Год назад

      Just install it however you normally install python packages

    • @aarondelarosa3146
      @aarondelarosa3146 Год назад

      It seems that backtesting file contains a virus. It was blocked by antivirus. @@ChadThackray

  • @hwtang217
    @hwtang217 Год назад

    Nice content, Chad. I am new for back testing , I have no idea how to rewrite the walk forward if using different time frame with 2 moving average crossover strategy with optimisation. Thanks.

    • @ChadThackray
      @ChadThackray  Год назад

      Perhaps try some of my other videos or general python tutorials until you're ready

    • @hwtang217
      @hwtang217 Год назад

      @@ChadThackray Now I am using the max time frame of long ma for warmup_bars

  • @MrClimateCriminal
    @MrClimateCriminal Год назад

    how do we download your notebook?

  • @samueltamayogaviria3377
    @samueltamayogaviria3377 2 года назад

    Hello Chad. Thanks for your videos, they have served very well for my formation as an algorithmic trader. I have a question, if I am not calculating indicators for other timeframes, do I still need the "warmup_bars"?

    • @ChadThackray
      @ChadThackray  2 года назад +1

      Depends on the indicators you use. Most require some kind of warmup time

  • @xramonz
    @xramonz Год назад

    I don't understand how to choose parameters after such optimizations. For example we have bunch of results which is obtained with different strategy parameters. What would we choose for further trading in real market? Which parameters are best?

    • @AccOunt-cb9tc
      @AccOunt-cb9tc Год назад +1

      You walk forward to the current time so that the training data ends at the current date and then use the strategy parameters from that last test in your live strategy for as long as your validation data window was

    • @VaidaBogdan
      @VaidaBogdan Год назад

      this is the way

    • @a7medmostafaabuhatab
      @a7medmostafaabuhatab 11 месяцев назад

      I think the most repetitive parameters through all the validation processes with good results should be selected for the live trading

  • @SaraStemPro
    @SaraStemPro 2 года назад

    Thank you so much for the information, it's very useful. Could you use the split data from sklearn?

    • @SaraStemPro
      @SaraStemPro 2 года назад

      I mean with cross-validation

  • @re_di_roma_is_back2388
    @re_di_roma_is_back2388 Год назад

    Hi. My Jupiter says "No module named backtesting"

    • @VaidaBogdan
      @VaidaBogdan Год назад

      Run "pip install backtesting" inside or outside the jupyter notebook

  • @OlufemiJeromeAjakaye
    @OlufemiJeromeAjakaye 6 месяцев назад

    Your view platform not yet clear for the viewers to see clear.....adjusted d zoom up.

  • @aarondelarosa3146
    @aarondelarosa3146 8 месяцев назад

    Hey Chad. Can you share your code?

  • @suriyashopping407
    @suriyashopping407 Год назад +1

    source code please

  • @swastika4860
    @swastika4860 2 года назад

    Thanks

  • @dgarciagud
    @dgarciagud 2 года назад

    Very nice code

  • @hendrikvanbrantegem7526
    @hendrikvanbrantegem7526 Год назад

    cool things

  • @kanaya1991
    @kanaya1991 2 года назад

    How can used metatrader data here,l?😁

    • @ChadThackray
      @ChadThackray  2 года назад

      I've never used metatrader but as long as you've got it in a pandas dataframe it'll work fine

    • @kanaya1991
      @kanaya1991 2 года назад

      @@ChadThackray Ok thx!

  • @AccOunt-cb9tc
    @AccOunt-cb9tc Год назад

    I had a problem with the optimized parameters for the backtests on the validation sets of data all being the same when printing them from the _strategy in the stats results.
    When you tested it in your video you only checked a single _strategy result to see if it was different from the defaults, but not if they were different from each other so I don't know if you had the same problem, I think I copied your code exactly in a new jupiter notebook and still had the same problem, anyone else have this problem?
    edit:
    I fixed it like this but I don't know what the original problem was.
    def walk_forward(strategy, data_full, warmup_bars, lookback_bars, validation_bars, cash=10_000_000, commission=0.002):
    stats_master = []
    params_master = []

    for i in range(lookback_bars + warmup_bars, len(data_full) - validation_bars, validation_bars):
    training_data = data_full.iloc[i - lookback_bars - warmup_bars:i]
    validation_data = data_full.iloc[i - warmup_bars:i + validation_bars]

    bt_training = Backtest(training_data, strategy, cash=cash, commission=commission)
    stats_training = bt_training.optimize(
    small_threshold=list(np.arange(0, 1, 0.1)),
    large_threshold=list(np.arange(1, 3, 0.2)),
    maximize="Equity Final [$]"
    )

    optimized_strategy = stats_training._strategy
    strategy.small_threshold = optimized_strategy.small_threshold
    strategy.large_threshold = optimized_strategy.large_threshold

    bt_validation = Backtest(validation_data, strategy, cash=cash, commission=commission)
    stats_validation = bt_validation.run()

    stats_master.append(stats_validation)
    params_master.append({
    "small_threshold": optimized_strategy.small_threshold,
    "large_threshold": optimized_strategy.large_threshold
    })

    return stats_master, params_master
    lookback_bars = 28*400
    validation_bars = 7*400
    warmup_bars = 16*24
    stats, params = walk_forward(
    MomentumStrategy,
    data,
    lookback_bars=lookback_bars,
    validation_bars=validation_bars,
    warmup_bars=warmup_bars
    )
    stats
    params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7], params[8], params[9], params[10]

    • @mehmetakifsaloglu8717
      @mehmetakifsaloglu8717 Год назад

      i've same problem :( i couldnt find any solution

    • @ThatMetalChannel
      @ThatMetalChannel Год назад

      yeah I had the same problem and tried different indicators, sometimes it would change just 1 value but I never figured it out