How To Backtest a Trading Strategy in Python using ONLY Vectorization / NO LOOPS

Поділитися
Вставка
  • Опубліковано 22 січ 2025

КОМЕНТАРІ • 55

  • @nishadseeraj7034
    @nishadseeraj7034 2 роки тому +4

    Really love your tutorials man! They are done so well and comprehensive. I pick up many useful coding tips and tricks not just for algotrading but for my actual data science day job. Always look forward to your stuff!

    • @Algovibes
      @Algovibes  2 роки тому +2

      Thank you my man! :-) Also working in the field and happy to read my stuff is useful to you.

  • @inspiration2292
    @inspiration2292 2 роки тому +1

    Much Love Mate!

    • @Algovibes
      @Algovibes  2 роки тому

      Thanks a lot. Beautiful name :-)

  • @sweealamak628
    @sweealamak628 2 роки тому +2

    Yup! I changed my code and use pandas for backtesting. No fear of overloading or even creating multiple dataframes now 👍

    • @Algovibes
      @Algovibes  2 роки тому

      Awesome. Thanks a lot for your comment :-)

  • @sz8558
    @sz8558 2 роки тому +1

    Excellent video thanks....Great to look inside the code before moving to looping methods..thank you

    • @Algovibes
      @Algovibes  2 роки тому

      Thanks a lot for watching mate!

  • @heshere
    @heshere 29 днів тому +1

    this is very helpful. Do you have any examples of vectorized backtesting on a long/short strategy and using profit take and stop losses?

    • @Algovibes
      @Algovibes  27 днів тому +1

      happy to read, thx mate. sure, just explore my Python for Finance playlist. There is tons of stuff like that :-)

  • @santhoshdasari7
    @santhoshdasari7 2 роки тому +3

    Thanks for sharing 🙏

    • @Algovibes
      @Algovibes  2 роки тому

      Thanks for watching buddy!

  • @lucianocas2019
    @lucianocas2019 2 роки тому +3

    raw_trades = filter_buys.T.idxmax()
    mark error in this line TypeError: reduction operation 'argmax' not allowed for this dtype

    • @saldonegativo982
      @saldonegativo982 2 роки тому +1

      raw_sells = filter_sells.T.astype(float).idxmax() works fine.

    • @Algovibes
      @Algovibes  2 роки тому

      Hi Luciano, just stick to the code in the video and you won't run into issues. Anyhow, did you solve the error?
      Thx Saldo for helping out others!

  • @tessalittle2411
    @tessalittle2411 Рік тому +1

    fascinating! maybe this is what I am looking for. Question if I were to look for the highest timeframe to which the K% of stochastic RSI is above 80 for a given range (lets say starting at 10D and doing a 12H interation till 1D). Then do the same but this time to find the highest time frame to which K% rsi stochastic below 20. Would vectoration be better? I will be wanting to keep this analysesfor historical back testing.

  • @arunavadatta7734
    @arunavadatta7734 Рік тому

    Can you make similar vectorized trading code for both long and short trade with flag/position system..so that multiple position will not take at the same time?

  • @twizzlemania3843
    @twizzlemania3843 2 роки тому

    super interesting content and concurrent to the channel 👍🔥

    • @Algovibes
      @Algovibes  2 роки тому

      Thanks a lot buddy :-)

  • @argjendfejzulahi4860
    @argjendfejzulahi4860 2 роки тому +1

    Great Video as always ! I would love to see a video on so-called "event-based-backtesting". I am kind of confused of how one could use a more complex backtesting engine to a live-stream of data. Why the distinction ? Is a vectorized Backtester not able to perform the task ?

    • @Algovibes
      @Algovibes  2 роки тому

      Thanks a lot Argjend! Also thanks for your suggestion.

  • @polkobra5455
    @polkobra5455 2 роки тому +1

    Hey algo,
    May I ask you a quick q? When you sell a coin using the Binance API, I receive them as AUD (i am from australia). How can I sell a quantity of a coin and have it as USDT? Or in other words, is there a client.create_order but for exchanges between coins?

    • @Algovibes
      @Algovibes  2 роки тому

      Good question. I usually just store my Cash as USDT and transfer it back as a major crypto to my other wallet. So I have no idea if that's possible.

  • @hugomenchaca7216
    @hugomenchaca7216 2 роки тому +1

    Other fantastic video, thanks a lot!. I also think that it's very important develop a vector strategy but not always it's easy to get do it, for example I'm not getting to filter the number positions/stocks maximal that we can have using only vectorization where the strategy is based on all stock SP500. It's a good suggest for a new video.

    • @Algovibes
      @Algovibes  2 роки тому

      Hugo, thanks a lot! Can you maybe elaborate a bit on your suggestion. Thanks.

    • @hugomenchaca7216
      @hugomenchaca7216 2 роки тому

      @@Algovibes Sure, this is the idea to develop without loops:
      - Over all stocks of SP500.
      - Maximal positions of the strategy for explample: 9
      - Each week check if there are any stocks that compile the buy conditions, for example: RSI >70 and sma(21) < price
      If there are positions free so buy the stocks until the max. (9) select order by max ROC.
      - Sell the positions after 10 weeks or if the RSI < 40.
      The estrategy is the less important, the key is to get do it with a vectorizacion method. The portfolio of the strategy never can to have more than 9 stocks.

  • @salemabualem
    @salemabualem 2 роки тому +1

    if i want to apply this to cryptos what do i need to do ?

    • @Algovibes
      @Algovibes  2 роки тому +1

      Interesting question! Just pull the price data for cryptos as I did in the videos in the cryptobot playlist and repeat the procedure in the video.

    • @salemabualem
      @salemabualem 2 роки тому +1

      @@Algovibes Thanks

  • @kaillra
    @kaillra 2 роки тому +2

    Hi when I do the Boolean indexing checkbuys[checkbuys_sum] it returns a dataframe of True, False, Nan instead of « 1 ». Do you know why?

    • @kaillra
      @kaillra 2 роки тому +1

      @@ЕвгенийКрасилов-о9о Thanks I will try that!

    • @kaillra
      @kaillra 2 роки тому +2

      @@ЕвгенийКрасилов-о9о It works :)

    • @mrryanwells
      @mrryanwells 2 роки тому

      yeah i had to "filter_buys = checkbuys.astype(int)[checkbuys_sum]" to get the index max to integers, but I'm dangerously low on knowledge

  • @Leo-cw6dr
    @Leo-cw6dr 2 роки тому +1

    Thank you for sharing such a great video. I have a question, I have seen many people use even-driven backtest instead of vectorized. Do you think only Vectorized backtesting will be enough to test an strategy?😢

    • @Algovibes
      @Algovibes  2 роки тому

      Welcome mate, Event-Driven Backtest is definitely important as well!

  • @newdata
    @newdata 2 роки тому +1

    dunno why df.loc[i:].index[int(e)] give out of IndexError: index 10 is out of bounds for axis 0 with size 10

    • @newdata
      @newdata 2 роки тому +1

      got to get rid of buydates towards end of df else out of range for above formula

    • @Algovibes
      @Algovibes  2 роки тому

      Thanks @newdata! Also for sharing the solution.

    • @promax.ai.marketing
      @promax.ai.marketing 11 місяців тому

      @@Algovibes how?

  • @jerrywang3225
    @jerrywang3225 2 роки тому +1

    Thanks for your video. The shifted rows and index are too complicated to understand. Why not just use backtesting?

    • @Algovibes
      @Algovibes  2 роки тому

      Shifting the rows is simply taking the next n days into one column to use vectorized operations. Ofc you can opt for Backtesting libraries but try to go over it again. Let me know if you have any understanding questions and I'll try to do my best helping you out.

  • @emperorpalpatine8418
    @emperorpalpatine8418 2 роки тому +1

    Thx for the vid. I there a possibility to share a strategy ? I’ve found a NCAV/MV strategy which can be implemented in Python and it might be interesting since it’s going to be a fundamental strategy.

    • @Algovibes
      @Algovibes  2 роки тому

      Welcome chancellor! Can you pass me a reference? Happy to take a look.

    • @emperorpalpatine8418
      @emperorpalpatine8418 2 роки тому

      @@Algovibes Ive posted the strategy reference, but for some reason it’s not here anymore

  • @ryu8777
    @ryu8777 2 роки тому +1

    Awesome

    • @Algovibes
      @Algovibes  2 роки тому

      Happy that you think so :-) Thanks for watching!

  • @Justin-mx5ye
    @Justin-mx5ye 2 роки тому +1

    What do you do for work man?

    • @Algovibes
      @Algovibes  2 роки тому +2

      For a living I am working in a Data Science related field.

  • @pythonplate7189
    @pythonplate7189 2 роки тому +2

    Dear reader , i like your video's a lot .
    Maybe you can make a video about a Websocket connection thats ping every 1 minute.
    And that the client automaticly reconect when its receive more than 1 minute nothing

    • @Algovibes
      @Algovibes  2 роки тому

      Hi buddy,
      already did that in a lot of videos in the cryptobot playlist. Be kindly invited to check it out! :-)

    • @pythonplate7189
      @pythonplate7189 2 роки тому

      Do you have a link from that video that autoconnect ?

  • @betahex3589
    @betahex3589 2 роки тому +1

    Why buy from the low, thats not realistic

    • @Algovibes
      @Algovibes  2 роки тому

      Hi BetaHex,
      please check out the linked video. Explained the strategy there. The Low is simply giving us the information if we would have hit the buy limit order on a specific day. We are not buying "from the Low".

  • @ybvco.79
    @ybvco.79 2 роки тому +1

    😀