Trading strategy and Backtest with Python & SQL [MOMENTUM in the INDIAN stock market]

Поделиться
HTML-код
  • Опубликовано: 14 июн 2024
  • Please subscribe to the channel :-) It is free for you and is helping me a lot.
    Disclaimer: This video is no investment advice and is for educational and entertainment purposes only.
    Momentum is widely discussed in academia. Please check my other videos on Momentum for academic references.
    Momentum in the Dow Jones:
    • How to build a trading...
    Momentum in the S&P500:
    • Trading strategy and B...
    Videos on SQL & Python:
    • Create your own Financ...
    Short term reversal:
    • How To Build a Trading...
    0:00 - 01:46 Strategy explanation and prerequisites
    01:46 - 3:49 Online data request (yfinance)
    3:49 - 4:28 Create a SQL database
    4:28 - 06:56 Populating the SQL Database with Python
    06:56 - 10:44 Example of requesting data out of the SQL database
    10:44 - 16:44 Mass request of all SQL tables and all necessary data manipulations
    16:44 - 16:52 Final dataframe
    16:52 - 25:35 Momentum strategy with a single formation date
    25:35 - 27:54 Construct the momentum function
    27:54 - 34:46 Backtest the strategy

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

  • @jitkadyan
    @jitkadyan 3 года назад +13

    You are great and so supportive that you take care of all your subscriber like us from india , providing us knowledge that we are not able to get after spending our thousand of rupees. i m very thankful to you to take us our comment and suggestion in your consideration and providing us knowldge that is very very useful to in our journey of algo trading. Pls keep it going

    • @Algovibes
      @Algovibes  3 года назад +1

      Thanks a lot for your kind comment. Very happy my videos are adding value for you. Really appreciate it :-)

    • @SinthanaiNeram
      @SinthanaiNeram 3 года назад +3

      I tried building a system from scratch with real time capabilities to place order. I missed out on the strategies and hence had to stop the trading. I had a postgres based DB and Java based multi-threaded implementation which can listen to ticks and decide. I did historical back testing and faced 50:50 win rates. I should accept that all my attempts to find the strategy with parameters did not go well across scripts. now learning ML to implement deep learning on the data, system i have.

  • @lavanyahouse1372
    @lavanyahouse1372 3 года назад +1

    Thanks for this wonderful video, simple and easy to understand. Good work.

    • @Algovibes
      @Algovibes  3 года назад

      Thank you :-) Glad to hear that!

  • @pavankamat6985
    @pavankamat6985 2 года назад +4

    Thanks for making this video.
    I tried doing this kind of thing on my own long ago but instead I was trying to implement IBD's relative strength ranking with entire stock exchange's universe, and get 1-100 ranks. But eventually gave up as python was very new to me and I didn't have a lot of time to spend on it
    But this video has given me hope. I'll try once again. Thanks once again!

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

      Hey buddy, thanks for your comment :-)
      Sounds interesting! Let me know about the insights you got once you build it.
      Don't give up :-)

  • @npranavsubhraveti2513
    @npranavsubhraveti2513 3 года назад +1

    Great video, very helpful. Subscribed.

    • @Algovibes
      @Algovibes  3 года назад

      Thanks a lot for watching, your comment and your subscription. That's a huge support for me! Appreciate it :-)

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

    There are people here who are charging thousands of dollars to teach these things, you are doing a great service

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

      Thank you buddy for your positive words, really appreciate it :-)

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

    Thanks for sharing this vedio... 🙏🙏🙏...

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

      Most welcome! Thanks for watching :-)

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

    Thanks a lot. I’m pretty new to python. I just followed what you said. I’m getting proper output.

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

      Awesome. Thanks a lot for your feedback!

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

    Top content! You are The Man!

    • @Algovibes
      @Algovibes  3 года назад

      Thanks a lot for your support :-) It is an incredible help for me.

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

    Nice!

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

      Thanks for watching mate.

  • @adityashinde2878
    @adityashinde2878 3 года назад +4

    My man, you got me bro

    • @Algovibes
      @Algovibes  3 года назад +1

      Happy that I got you! :D Thanks for watching and your comment mate :-)

  • @sagarchoudhari9386
    @sagarchoudhari9386 3 года назад +1

    Very nice content

    • @Algovibes
      @Algovibes  3 года назад

      Thanks a lot for your support. Appreciate it and really helps me out! :-)

  • @santhosh6700
    @santhosh6700 3 года назад +1

    I LOVE PYTHON AND TRADING
    This channel have both...❤️

    • @Algovibes
      @Algovibes  3 года назад

      Haha, me too ❤️ Thanks for commenting mate. Appreciate it!

  • @jitkadyan
    @jitkadyan 3 года назад +3

    We will tried our best to aware other about your chennal where they can also benefit of your knowledge.

    • @Algovibes
      @Algovibes  3 года назад

      That would be such a huge help. Thanks a lot for your support! :-)

  • @FRANKWHITE1996
    @FRANKWHITE1996 3 года назад +1

    Im subscribing this guy 🙏

    • @Algovibes
      @Algovibes  3 года назад +1

      Means a lot to me. Thanks a bunch :-)

  • @nadeemahmedcfa3790
    @nadeemahmedcfa3790 3 года назад +1

    Just to be clear, the returns for 1 month holding period is what they say - They are for 1 month only. You need to multiply them by 12 to get annualized return. similarly, multiply 3 months return by 4, and 6 months return by 2. Then they can be compared.

  • @adarshv14
    @adarshv14 3 года назад +1

    Hey, Thank you for this content. Could you also make a strategy video (for eg: HeikenAshi, EMA Crossover etc) for Intraday trading on Nifty/Sensex based on top performers of a particular sector? Thanks in advance. Great content. Loving it. Already subscribed and keep up the great work.

    • @Algovibes
      @Algovibes  3 года назад +1

      Thank you very much for watching, your appreciative comment and subscribing. Also thanks for your suggestion! I cannot promise anything but I noted that. Be kindly invited to check out my other trading strategy videos. You will surely find something interesting for you and there will be more stuff on trading in the future :-)

  • @bryan-9742
    @bryan-9742 Год назад +1

    I'm not sure how that NAN work around is working. Very cool though! I think trying to use this on an imbalanced panel would be a great thing to think about.

  • @juuttuub
    @juuttuub 3 года назад +1

    Great one! Some additional tweak ideas. It would be great to see, what are the on each portfolio. Also, I think this structure would be good for the 52 week high strategy. In that strategy the problem might be that there are multiple stocks that are on 52 week high and one needs to randomly pick one or even better hold the stock if it is on the following ranking on top too

    • @Algovibes
      @Algovibes  3 года назад

      Thanks a lot for watching, your kind comment and your valuable suggestions! :-)
      I am sorry but I am honestly not getting the very first suggestion. What do you want to see?
      Regarding the 52 week high: I will definitely taking this into consideration, it in fact is an interesting thing to code but have some other things in the pipeline before. Thanks a lot for reminding me!

    • @juuttuub
      @juuttuub 3 года назад +1

      @@Algovibes Sorry, if my comment was imprecise. I meant, it would be nice to have the names of the stocks of every portfolio and may the result too. Example: 3month portfolio From january-march and the names. Then portfolio april-june and the names of the stocks. This information would be nice to be in easy to read format.

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

    Algovibes, one doubt on the formation day how many stocks we should buy ? Normally we can only buy stocks initially on the first month and we cannot short sell right as we are buying stocks for first time

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

      Hi buddy,
      can you elaborate on your problem?
      I don't really get it. Sorry and thanks in advance!

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

    Happy Deepawali @Algovibes

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

      Thank you buddy! For you as well ❤️

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

    Hello sir.. good job
    can I get technical data..from NSE Web site live market
    like RSI MACD ..etc

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

      Hi buddy,
      thanks a lot :-)
      Just as an alternative solution: Why wouldn't you calculate the technical indicators instead?
      I have a lot of content on that.
      E.g.
      ruclips.net/video/rYfe9Bg2GcY/видео.html
      or
      ruclips.net/video/r8pU-8l1KPU/видео.html

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

    Hi, thanks for sharing it but are you calculating 11 months return here instead of 12 month return?There are just 11 months between begin (28.02.10) and end date (31.01.11) in this code. What am I missing?
    Regards

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

      Thanks for watching mate. Yes that's intented. It's accounting for the short term reversal effect. Covered here:
      ruclips.net/video/xpP048vfNrg/видео.html

  • @adityashinde2878
    @adityashinde2878 3 года назад +1

    Can you bring more content for Indian markets ?

    • @Algovibes
      @Algovibes  3 года назад

      As I am personally interested in the Indian market there will be some more stuff. Maybe not India only but I might include Sensex components in other strategies in the future. Thanks for your suggestion :-)

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

    im not into codin and tech stuff what app do you use for coding bro?

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

      Using Jupyter Notebook here :-)
      If you wanna start out you are invited to check out my Python Introduction playlist.

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

    Hey great video but the data has inconsistencies and duplicate data also which can affect our results how to tackle that any idea?

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

      Can you elaborate (in the best case with timestamps)? Thx!

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

      Check for duplicates in dataframe after downloading the data with period ="max"
      Df.duplicates()

  • @Whchamprahulsingh
    @Whchamprahulsingh 3 года назад +1

    Thank you so much for this video.
    Can you make a strategy of long only where we check weekly performance. And of the universe of Nifty 500. We will check the momentum performance weekly and rebalance accordingly. We keep the performers and get rid of losers.
    Thank you in advance liked your video and you got a happy subscriber.

    • @Algovibes
      @Algovibes  3 года назад +1

      Thank you very much for watching and your comment! I noted your suggestion, but have some more topics in the pipeline beforehand. Thanks for your patience. Also thanks a bunch for your subscription - really appreciate it :-)

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

      Bruh, he gave you all this information. At least try it by yourself first.
      He has shown you everything in this video, you can literally make any kind of momentum strategy if you google other indicators and combine it with this.
      I'm trying Nifty 500, Smallcap 250 along with some more of my own criterias

  • @kumarlavanya6393
    @kumarlavanya6393 3 года назад +1

    Thanks for creating this wonderful videos. I am new to Python and getting no output for "line 35, tome 23:54 using the code "winnerret = mtlprices.loc[formation+MonthEnd(1),winners.index]/(mtlprices.loc[formation, winners.index]) - 1" what is wrong here. Also not getting any error. Can you please help?

    • @Algovibes
      @Algovibes  3 года назад

      Thanks for watching and your kind comment :-)
      Well there could be a couple of reasons for that.
      Did you use exactly my syntax or did you deviate at some point?

    • @kumarlavanya6393
      @kumarlavanya6393 3 года назад

      @@Algovibes I have followed the same syntax. No error and no result. I find it strange. Can you please email me the code to my email stockssan@gmail.com

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

      @@Algovibes i have the same problem , its the formation variable datatype, when you do formation+Monthend becomes a timestamp instead of a datetime.
      thats the problem, the only way i could work around it is to use formation as formation+MonthEnd(0) for this line to work.

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

    Hi , i need the python code for pulling the stocks below 10 /- from bse, nse on specified date. Please help me if you have any

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

      Hi mate, could you elaborate?

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

    I m finding error like MonthEnd is not defined can you guide me

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

      You gotta import it in the very beginning of your script.

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

    Great content bro.
    Can you please share the code so that we can do R&D

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

      Thank you! I didn't plan on publishing the code yet. Sorry!

  • @boohoo708
    @boohoo708 3 года назад +1

    take the best is a good strat…instead of take the worst…if you would do that for the momentum top 10 or top 5 of the most capitalized 200 stocks in the Nasdaq, you would have a killer strat…
    6 month ranking time and 3 Month holding time…no skipping month…if you backtest such a strat since 1997 you will get a 55% annual return in such a
    KillerStrat…if you forbid to buy when the NasdaqIndex is under the sma200, then you make it perfect for the gods of wallstreet….
    god bless you bro…thx for the code…i will try….

    • @Algovibes
      @Algovibes  3 года назад +1

      Hey mate, thanks for your kind comment, really appreciate it :-) And thanks for your input regarding the Nasdaq!

    • @boohoo708
      @boohoo708 3 года назад

      @@Algovibes i m sure, you will get a lot of new abonnements with that…but it should be possible to choose the number of stocks to buy…5 min until about max 20…short ist not so useful…
      then you should have a exponential regression, annualized for sorting…and maybe multiplied with the correlation of determination R2, to sort down the stocks with gaps and to much volatility…if you then will calculate the max drawdown and finally calculate the annualized Performances, then you will have the ultimative app for the next generation of success…
      if any holded stock falls under the sma100 it should be sold
      thx…bro…im impressed of your programming speed…i will donate you, if you have it ready…ericpauldion@gmail.com

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

    What if I want Niftybank 1 min data and Run strategy on it for backtesting??

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

      As answered below your other comment, please check this vid (cryptos tho):
      ruclips.net/video/HB1CLz0Z1NY/видео.html
      You need a good data source for 1m data on the Indian market. If you have one, let me know. Happy to cover something on Indian stocks soon again.

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

      @@Algovibes How do I get data for 1min of data for Banknifty Indices Symbol:^NSEBANK??

  • @sunnydhokane8988
    @sunnydhokane8988 3 года назад +1

    What are the prerequisites for this video

    • @Algovibes
      @Algovibes  3 года назад

      Thanks for your comment! :-)
      Prerequisites in general is a solid numpy or pandas knowledge and intermediate Python skills. Also you should have a basic knowledge on how financial markets are working. You can click through my Python for finance playlist, I think that would be helpful.
      Let me know if you need any more resources or tell me where you are struggling.

  • @nitin-goyal
    @nitin-goyal 2 года назад +1

    Bro, don't you have a github repo? Just asking! 😉

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

      Unfortunately I haven't.

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

    When trying this code. yf.download(tickersSensex[0]) returns a JSON decode error. Any suggestions on how to fix that? Does this API stil work?

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

      Note: I updated yfinance to 0.1.62 that got rid of the error but now I get the stocks as delisted. :(

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

      Last update: the date I was using was probably a holiday. Latest attempt after removing date range seems to work fine.

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

      Yea, I had some problems with the previous version as well (check my latest community post). Thank you very much for sharing your problem solving! :-)

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

    BSE stock exchange has lost its sheen in last 2 decades, it only handles less than 20% of total trading in india. NSE is the stock exchange which handles 80% trades of Indian market. Please design things around nse as it has emerged has largest options exchange in the world in recent years.

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

      Good to know, thanks a lot for sharing that!

  • @bryan-9742
    @bryan-9742 Год назад

    Using these datetime objects assumes your index will map appropriately to the index. Be careful with this in other strategies. Observe the error below:
    df_mom = yf.download(tickers, start = '2010-01-01')['Adj Close']
    # this will work on a momentum for an imbalanced panael as zeros converge to the middle
    df_mom = df_mom.fillna(0)
    Example
    formation = dt.datetime(2011, 2, 28)
    begin_measurement = formation - MonthEnd(12)
    end_measurement = formation - MonthEnd(1)
    price_end = df_mom.loc[end_measurement]
    price_end # output works to map this appropriately to the dataframe that contains the necessary values.
    price_12mbefore = df_mom.loc[begin_measurement]
    price_12mbefore
    KeyError: Timestamp('2010-02-28 00:00:00')
    This is a result because the datetime index does not exist in the dataframe that was ingested via Yf. Just be careful here as you may have gotten lucky that your Indian index contained the appropriate indexes.