Trailing Stop Backtest For Algorithmic Trading in Python

Поделиться
HTML-код
  • Опубликовано: 16 июн 2024
  • This video tests the Stop Loss VS Trailing Stop methods for Algorithmic Trading using python. Backtests are applied to one trading strategy showing the differences between these methods.
    #tradingbots #pythoncoding #algorithmictrading #tradingstrategy
    🍓 If you want to follow structured courses with more details and practice exercises check my "About" page for Discount Coupons on my Udemy courses covering: Python basics, Object Oriented Programming and Data Analysis with NumPy and Pandas, ... more courses are on the way drop me a message if you have a particular interesting topic! Good luck!
    and the link for the code:
    drive.google.com/file/d/1Lpgj...
    00:00 Trading Stop Loss Introduction
    02:47 Stop Loss And Trail Stop Python Code
    07:28 Backtesting Strategy In Python
    10:00 Backtesting Results Analysis

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

  • @gdydbjsiuxvsnsjdx
    @gdydbjsiuxvsnsjdx 2 года назад +5

    Excellent content.. was looking for exactly this.. however, one variation that I am looking for is to break-even stops. For example, after the price reaches 1R, the SL could be moved to the entry point (or break even point) and then the SL starts trailing from there forward.
    Thanks anyway for the video.. keep this coming!

    • @CodeTradingCafe
      @CodeTradingCafe  2 года назад +3

      Hi thank you for your comment, actually yes I see what you mean, there are different variations of trade management, it's worth backtesting some of them.
      Good luck, see you around I guess

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

    great video ! i really enjoyed this study of tsl tp strategies!

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

      Thank you, are you interested in coding as well?

  • @a.winath4024
    @a.winath4024 2 года назад +1

    Thank you so much friend!

  • @1cBoris
    @1cBoris Год назад +2

    Hi, sorry for my english. Thanks for your videos, they are amazing. If we get the current price from the order book, for analysis and real trading, the stop loss will work much earlier than at the end of the candle. The candle looks like one beautiful movement, and there is a lot of volatility hidden inside.

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

      You are right, one way of avoiding this is to put SL outside the candle range, but you have to test the system again to make sure the odds are on your side.
      Thank you for sharing your ideas, good luck!

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

    Awesome video, thanks so much for sharing

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

    You are best . I wanted continue ❤❤❤

  • @LIL_MISSA
    @LIL_MISSA 8 дней назад

    Keep up the good work man, i love your codes and explanations ❤

    • @CodeTradingCafe
      @CodeTradingCafe  8 дней назад

      Glad you like them! Thank you for your support, much appreciated!

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

    Thanks a lot for sharing knowledge, code and video, very very usefull all your videos

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

      Thank you for your support, it makes a difference

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

    Great video. Did you tried vectorb for backtesting instead?

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

      I did have a look at it, to be honest I wasn't happy with their documentation, it was too time consuming to guess their functions so I simply moved to backtesting py

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

    Bravooo👌

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

    I just intalled Python to learn and be able to practice all this nice algoritms. Thanks for help people to learn.

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

      Good luck, remember learn slow but regular work don't get discouraged if it takes time

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

    Thanks to share.

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

    Can you please clarify what these atr_f and ratio_f stand for and what 0.2 and 1 mean at 11:30. When I try this params on my algo (I am testing it on 1min timeframe) I get instantly taken out of the position. Thank you for the awesome work!

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

      The first is a ratio variable to define SL from ATR so SL distance is atrf * ATR, and the second is the TP SL ratio. Hope this helps, good luck!

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

    I like the idea of using ATR to adjust the stoploss at the end of every bar ... but instead of using ATR you should try a realtime volatility pulled in from somewhere else, maybe options market. ATR is too slow. Thoughts?

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

      I guess something like a custom function checking the ATR and the lowest/highest points of few previous candles then providing something around these values would be an improvement. But if trying to get the info from elsewhere the algo can as well keep an eye on the different correlated currencies...

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

      @@CodeTradingCafe ATR is telling you what the volatility was, not what it is. $EVZ might be better, and that is based on 1 month currency options.

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

      Will check it out hopefully it's reliable

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

    Hi is there anyway to backtest trailing and stop loss using pct( percentage of profit/loss), rather than ATR? and is there indicator or mathematic way to identify low volatility of the market and sideway market? Thank you in advance.

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

      Hi definitely you can use percentage distance instead of ATR, it's relatively simple. Regarding identifying low volatility you can use ATR it's a good volatility indicator or check this video ruclips.net/video/phy57DZOIwc/видео.html you can detect narrow channels for low volatility. I hope this helps!

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

    thanks, great video knowlage, can you share the code

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

      done it's in the description, enjoy coding

  • @jeffb9967
    @jeffb9967 3 месяца назад +1

    hello and thank you for your great videos. Could you please tell me if these stop loss's work with crypto trading, in particular, ccxt and kucoin exchange, and will it work with any exchange?

    • @CodeTradingCafe
      @CodeTradingCafe  3 месяца назад

      Hi, thank you for your support, in principle yes any technical chart is the same, so the same SL TP principles can be applied.

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

    Thank you for your very interesting and exciting video. I have question, if it writes that file is not found and the problem have to be in the second cell, where is code: df = pd.read_csv("EURUSD_Candlestick_1_D_ASK_05.05.2003-30.06.2021.csv"), what i have to do with it?

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

      Hi you can put the file in the same folder as the python file to.be found. Or try including the path in the code l.

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

      @@CodeTradingCafe Will I pissed you off if I ask you for code you are writing about? I am really lost.

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

      @@FootfunI no worries, just copy your error message here I will try to help. If it's unfound file try rechecking the name of the file and the path.

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

      @@CodeTradingCafe
      FileNotFoundError
      Input In [2], in ()
      1 import pandas as pd
      ----> 2 df = pd.read_csv("EURUSD_Candlestick_4_Hour_ASK_05.05.2003-16.10.2021.csv")
      3 df.columns=['time', 'open', 'high', 'low', 'close', 'volume']
      4 #Check if NA values are in data
      File ~\anaconda3\lib\site-packages\pandas\util\_decorators.py:311, in deprecate_nonkeyword_arguments..decorate..wrapper(*args, **kwargs)
      305 if len(args) > num_allow_args:
      306 warnings.warn(
      307 msg.format(arguments=arguments),
      308 FutureWarning,
      309 stacklevel=stacklevel,
      310 )
      --> 311 return func(*args, **kwargs)

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

      @@FootfunI yes it can't find the file you can put both python code file and data file in the same folder or include the path from the read_csv function

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

    How to run auto trade on any Brocker?

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

      Hi, it doesn't work for any broker because each broker has its own API, so the best way is first choose the broker and then learn their API... Now this wad the easy part... Now comes finding a winning strategy to automate :) this is a lifetime teaser

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

    hey man , why dont you show your sharpe ratio?

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

      Never thought about it to be honest, but also I am afraid not everyone is familiar with it so it's worth a video on its own...

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

      @@CodeTradingCafe Anybody watching these videos knows what it is, i think.

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

      @@CodeTradingCafe It would be good to show Sharpe in your videos. It's the only way we can compare your strategy to others because you don't show your vol either.

  • @Dr.jayfrancis
    @Dr.jayfrancis 2 года назад +1

    Some day, I'll understand these advanced algos.. but not now xD

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

      Keep up the good work it will take some time