How to OPTIMIZE Technical Indicators in a Trading Strategy with Python?

แชร์
ฝัง
  • เผยแพร่เมื่อ 2 มิ.ย. 2024
  • In this video I am going over Optimizing the parameters of a Trading Strategy with the example of taking technical indicators (simple moving average). First with one parameter (Close price above SMA), then with a two (SMA_1 above SMA_2)
    Get the Notebook/Source code by becoming a Tier-3 Channel member:
    / @algovibes
    Want more content like that? Please be so kind supporting the channel by hitting like, subscribe and leaving a comment. Thanks a lot!
    How are stock returns calculated:
    • How To Calculate Stock...
    00:00 - 01:22 Strategy/Cartesian Product/Disclaimer
    01:22 - 02:48 Data & Data manipulations
    02:48 - 13:40 One Parameter Optimization
    13:40 - 21:54 Two Parameter Optimization
    Disclaimer:
    This video is not an investment advice and is for educational, informational or entertainment purposes only!
    #python #trading #optimization

ความคิดเห็น • 57

  • @Estrav.Krastvich
    @Estrav.Krastvich ปีที่แล้ว +3

    Probably one of the most practical algotrading/backtesting content on the Web. Most of which is just pure water.

    • @Algovibes
      @Algovibes  ปีที่แล้ว

      Thanks a lot mate! Be kindly invited to check out my other stuff.

  • @pethiraj
    @pethiraj 11 หลายเดือนก่อน +3

    Excellent information and this is the only video on YT with clear method on how to optimize and validate our strategy. Thanks for taking time and sharing your knowledge. Greatly Appreciated and it benefited a lot! Thanks!

    • @Algovibes
      @Algovibes  11 หลายเดือนก่อน

      Thank you very much!

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

    this is so much fun to find combinations of SMAs which doe not make much sense but works very well.

  • @gianfrancosilvestri2155
    @gianfrancosilvestri2155 ปีที่แล้ว +3

    Incredible Algovibes! Never met so clear explantions and so concise code in the same tutorial!!

    • @Algovibes
      @Algovibes  ปีที่แล้ว

      Very happy to read! Thanks a lot for watching and leaving a comment :-)

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

    This is really great. Very clean and concise code. Well done.

    • @Algovibes
      @Algovibes  ปีที่แล้ว

      Thanks buddy! Be invited to check out my other stuff :-)

  • @fudgenuggets405
    @fudgenuggets405 ปีที่แล้ว +3

    This is top notch stuff. Although the total profit of the strategy may not beat buy and hold, the advantage is you are not in the position full time. When you are out of the position, your capital could be invested in another asset if it is in a state that expects to outperform AAPL at that particular time... or you could stay in cash and sleep easier at night.

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

      Agreed! Be invited to check out my newest series. I am going more into details there.

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

    Great. It will be nice to expand as stated. Thanks.

    • @Algovibes
      @Algovibes  ปีที่แล้ว

      Thanks for your feedback, highly appreciated!

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

    I'd love to see more of these.

    • @Algovibes
      @Algovibes  ปีที่แล้ว

      Thx for your feedback Dennis! Appreciate it.

    • @user-qd7ev9dl9j
      @user-qd7ev9dl9j ปีที่แล้ว

      Hi Dennis Carver, I can guide you how to invest in cryptocurrency. So you can leave me your contact. Regards

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

    very nice approach and great explanation of multiple fundamental concepts. you keep producing interesting content, thanks

    • @Algovibes
      @Algovibes  ปีที่แล้ว

      Thanks a lot mate. Appreciate your comment!

  • @bryan-9742
    @bryan-9742 ปีที่แล้ว +2

    very interesting!

    • @Algovibes
      @Algovibes  ปีที่แล้ว

      Thanks for your feedback Bryan!

  • @Niko-ie6dh
    @Niko-ie6dh ปีที่แล้ว +1

    does the cross join and then loop over the dataframe way have a performance benefit over the nested loop? Intuitively I would have iterated just over x and y in a nested for loop

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

    you can optimise if you filter where SMA_1 and SMA_2 have inverted values.
    ex. SMA_1=10 and SMA_2=15 vs SMA_1=15 and SMA_2=10

    • @ageens
      @ageens ปีที่แล้ว

      ahh, I just threw away all values where sma_1 > sma_2

  • @muggzzzzz
    @muggzzzzz ปีที่แล้ว +3

    Once upon a time I was testing my grid trading strategy, and I got my balance to be 100x as much as initial balance in a matter of one year. But if I move the starting point of a testing literally by one day, i got busted almost immediately.
    So you shouldn't be obsessed with optimization. It should be able to keep being on the flow no matter of the 'financial weather'.

    • @vetiarvind
      @vetiarvind ปีที่แล้ว +3

      Good point, overfitting isn't a good idea always.

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

      Referring to that at the end of the video. For a bulletproof optimization you need a train test split. I have covered something very similar here:
      th-cam.com/video/mabCIr-a4HA/w-d-xo.html

    • @muggzzzzz
      @muggzzzzz ปีที่แล้ว

      @@Algovibes I'm sorry for commenting before watching the full video.

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

    From a coding perspective, have you ever tried list comprehension for the conditional looping logic? I'm taking an online python course and they suggest "pythonic" way of coding. Their recommendation is centered around dealing with very large datasets. Just wondering whether you think there's a material benefit switching styles for this specific case of trading labels.

    • @Algovibes
      @Algovibes  ปีที่แล้ว

      Wouldn't make sense here at all. But yes I am a big time user of list comprehensions :-)

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

      @@Algovibes Thanks for replying. Will keep a look out for instances where you use list comprehension. 👍 Lots to learn 🫡

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

    Would a nested for loop work the same way as the cartesian product method, in the case of optimizing for 2 parameters?

    • @Algovibes
      @Algovibes  ปีที่แล้ว

      Also possible to solve this with a Nested loop but I highly recommend using the cartesian product/cross join here.

  • @eliotharreau7627
    @eliotharreau7627 3 หลายเดือนก่อน

    Hi, how to make the same cartesien product with tree paramaters x y z , because the merge function only accept two parameters ? Thank you

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

    What is the probabilty that optimized parametrs will bring profite in the future trading? The probability is very low, because you a looking for a best parametr using past data, but not checking, how this parametrs works on the future data! It is much more interesting to listen about forward testing, not about backtesting!

    • @Algovibes
      @Algovibes  ปีที่แล้ว

      You repeated exactly what I said in the video. I have content on that as well - Just watch all my videos and you will get a detailed and holistic view.
      Let me know if you need anything!

    • @fudgenuggets405
      @fudgenuggets405 ปีที่แล้ว

      If you have access to the "future data" then you don't need a trading strategy. Unfortunately google doesn't provide any links to future stock prices.

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

    Could we please have a video about support and resistance level detection in python for bitcoin?

    • @Algovibes
      @Algovibes  ปีที่แล้ว

      Cool suggestion! Thanks a lot

  • @Leonhard-Euler
    @Leonhard-Euler ปีที่แล้ว +1

    One point:
    You should check the period for sma_1 is less than that for sma_2

    • @Algovibes
      @Algovibes  ปีที่แล้ว

      Not necessarily as I wanted to test both sides but definitely an option.

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

    Thanks Bro, How to detect support and resistance using python?

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

      Interesting topic. Thanks for the suggestion!

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

      ​@@Algovibes Thanks for reply. Please make the code as simple as you can😇

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

    Does it need to outperform the underlying vehicle to be viable?

    • @Algovibes
      @Algovibes  ปีที่แล้ว

      No definitely not. I am just referring to the interesting result that no matter which values you are passing you never outperform the asset itself over the long term (at least Apple). Interestingly this is not true for assets with a rather bad performance (e.g. MMM).
      There is much more to be considered: Exposure Time, Volatility, Max Drawdown, turnover and trading costs.

    • @bigphab7205
      @bigphab7205 ปีที่แล้ว

      @@Algovibes just curious. Cuz I always hear people talking about whether it does or doesn't. Drawdown is another term that lacks intuitive-ness for me too. Thanks.

    • @Master_of_Chess_Shorts
      @Master_of_Chess_Shorts ปีที่แล้ว

      @@Algovibes would be fun to use volume as an indicator to determine how likely your trade would go through. For Apple it might not be an issue but this is another dimension of a full optimisation strategy.

    • @fudgenuggets405
      @fudgenuggets405 ปีที่แล้ว

      @@Algovibes this type of strategy should work much better for cyclical stocks like energy companies.

    • @fudgenuggets405
      @fudgenuggets405 ปีที่แล้ว

      I took algovibes' code and did a quick test on XOM to test this idea. To save time I only examined sma's from 10 to 50. Buy and hold had profit of 1.664 and the sma strategy produced a profit of 2.180 so it can (not necessarily will) work better on cyclical stocks.

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

    try vectorbt

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

    don't take AAPL, bet something wavy instead, like efs - TLT, HYG, JNK. SPY is not "wavy" but still a tiny bit outperform "buy and hold". Need something volatile but horizontal to outperform growing monsters.

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

      poor GDX goes like chupa-chups. I do not see sense for n > 75-100 (edit: well 100... )

    • @Algovibes
      @Algovibes  ปีที่แล้ว

      @@ageens 😄

    • @fudgenuggets405
      @fudgenuggets405 ปีที่แล้ว

      Energy stocks are perfect for this type of strategy.

  • @ko-prometheus
    @ko-prometheus ปีที่แล้ว +2

    ☢☢☢☢☢

    • @Algovibes
      @Algovibes  ปีที่แล้ว

      Thx for watching Konstantin! :)

  • @blasdelezo8396
    @blasdelezo8396 6 หลายเดือนก่อน

    Its better to use final = final[final.sma_1 < final.sma_2]