Trailing Stop Backtest For Algorithmic Trading in Python

แชร์
ฝัง
  • เผยแพร่เมื่อ 13 มิ.ย. 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

  • @LIL_MISSA
    @LIL_MISSA 5 วันที่ผ่านมา

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

    • @CodeTradingCafe
      @CodeTradingCafe  5 วันที่ผ่านมา

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

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

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

  • @a.winath4024
    @a.winath4024 2 ปีที่แล้ว +1

    Thank you so much friend!

  • @mcdawgzy
    @mcdawgzy 5 หลายเดือนก่อน

    Awesome video, thanks so much for sharing

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

  • @sibiljas3628
    @sibiljas3628 2 ปีที่แล้ว +2

    You are best . I wanted continue ❤❤❤

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

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

  • @walduss7939
    @walduss7939 ปีที่แล้ว +1

    Thanks to share.

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

  • @aidakhalil1394
    @aidakhalil1394 2 ปีที่แล้ว +1

    Bravooo👌

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

  • @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 th-cam.com/video/phy57DZOIwc/w-d-xo.html you can detect narrow channels for low volatility. I hope this helps!

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

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

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

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