Introduction to Algorithmic Trading with Python: Create a Mean Reverting Trading Algorithm

Поделиться
HTML-код
  • Опубликовано: 15 ноя 2024

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

  • @mrityunjaypathak8792
    @mrityunjaypathak8792 7 месяцев назад

    You saved my time. Was looking for this for my project ! Thank you Matt.

  • @HisyamHilmiOmar-jx7mx
    @HisyamHilmiOmar-jx7mx 6 месяцев назад

    Hi, great tutorial! Just one quick question, for the value of the moving average, what does 21 stand for, is it the length of the moving average or the actual value of the moving average? Can you elaborate further on how you derived the value of 21 for the moving average?

    • @MattMacarty
      @MattMacarty  5 месяцев назад

      21 is the number of periods in the moving average. This is the number of trading days in a month.

  • @nj-bz8pv
    @nj-bz8pv 2 года назад

    Thanks man! I’m a long time trader but beginner “quant” and python user. This is very helpful

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

    Your approach to algorithmic trading is novel. I've never seen an approach like this. I 've become aware of a problem with most indicators used by traders to generate Buy and Sell signals. One such indicator is the RSI. So, what I did was to add a column to your gld dataframe. I then ran a correlation between the gld['ratio] column and the gld['RSI'] column. I got the following correlation: 0.9120, which indicates that the ratio is highly correlated with the RSI. That, my friend, is the problem with most (all?) indicators that are based on price. In short, they all seem to be highly correlated. Using more than one indicator is oversampling, or superfluous. One doesn't really appear to be much better than another as far as I can tell. I'm curious, however, if there are any strategies that work better than others, relatively consistently. I'd also like to know if there are indicators that aren't correlated with price movement. I'm interested in any feedback, but if someone feels the need to attack me, please, keep it to yourself.

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

      Haha, yes I am just showing an example of a mean reversion approach using traditional statistics rather than technical indicator; I'm not making any claim that this is a winning system - it's far too simplistic, although at some point in history it may have worked just like that for a while. In general I think if anyone knows what works they aren't going to share it, since these edges tend to get traded away when they are known. This means lots of trial and error. And probably a combination signals. On the other hand highly correlated isn't the same as collinearity. The question might be when the two systems diverge which one does better?

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

    Thank you very much for sharing your Python codes.

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

      You are welcome. Glad it helped

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

    It would be great if you make another video about how to look at this strategy from linear regression point of view

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

      Ok, let me see what I can do.

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

    Outstanding! Thank you for the tutorial, very clean code.

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

    Superb tutorial, thanks! One question, how can we incorporate the cost of each transaction in the case of the strategy version? Would that mean its final return is likely to be smaller than that of buy and hold? Thanks

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

      Thanks. You could incorporate transaction costs by just subtracting the cost at every change in direction. Most transaction costs are pretty small at this pint but they will impact return.

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

      @@MattMacarty thanks!

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

    Hallo I have 3 question here
    1. Why do we use np.exp at the back?
    2. why do we need to shift the position to 1 ahead?

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

      Hi. I am calculating an instantaneous rate of return with np.exp. You shift the data to avoid look-ahead bias.

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

      @@MattMacarty may you please elaborate more about what do you mean by instantaneous rate of return , why haven't u taken the return as Close (n) -close (n-1)

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

    a small correction while u were interpreting the graph : percentile 50% is the median not the mean

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

    Amazing content !!

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

    Could you please suggest a solution for ( since not working pdr.get_data_yahoo) while I am executing the code for GLD?

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

      Can you tell me what the error message you are getting is?

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

    Can you please show us how to write the for loop to backtest a portfolio stock, for example 10 stocks. I can understand the code for backtesting one instrument, but can't figure how to to write the code for testing 10 stocks in one go. Thanks a lot.

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

      Let e see what I can do. There are some pretty good back testing libraries out there.

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

      @@MattMacarty please show us. It's a joy to see you do it. You made it look so interesting and easy for us to understand. You are one of, if not the best Python tutorial guy on RUclips on this type of backtesting Pythob demo.

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

    May I know why you use close price instead of adjusted close price?

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

      Adjusted close is there for splits. So use it if the security has split recently. Close and adjusted close eventually converge.

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

    Thank you very much man for this upload ! It's perfect. Can you make the same video with exit point, neutral point and calculate fees ? Thnx :-)

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

      Thanks. I would probably take advantage of one of the backtesting packages out there for some of this. Try bt which is about the easiest to use and has pretty good documentation.

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

      Ok I will try bt, but its not so easy. Thnx

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

    Can i use this on cryptocurrency?

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

      You should probably test anything you want to try on historical data.

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

    Thank you Sir

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

    Hello Matt, I hope you are good some questions: this strategy does not consider commissions right and that's why it outperforms buy and hold? what is the difference between a strategy of moving averages signals and this ratio? and if we wanted to add some commissions how would we do it?
    Thank you so much!

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

      Right, it doesn't consider commissions for either the strategy or the buy and hold. If you want to consider commssion you would probably having a starting capital amount and a commision amount, then you can add a column to your dataframe to track your ending value and compare it to the same for buy and hold.

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

    Thank you Sir