Introduction to Algorithmic Trading Using Python - How to Create & Test Trading Algorithm

Поділитися
Вставка
  • Опубліковано 13 жов 2024

КОМЕНТАРІ • 116

  • @PatrickBenitez6
    @PatrickBenitez6 10 місяців тому +8

    Exceptional tutorial! I spent the past couple days researching how to calculate proper cumulative returns based on your short,long, hold signal strategy and luckily the YT algo brought your video to my home screen. Keep up the great work!

    • @MattMacarty
      @MattMacarty  10 місяців тому

      Thank you. Glad it helped

  • @sudzbyte2215
    @sudzbyte2215 Рік тому +2

    Great vid, especially the reminder about shifting rows. Thanks!

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

      Glad it helped. Technically if you assume you update your models at the end of the day you could skip the row shift, but then you have to deal with price slip from close to open the next day.

  • @kingkong1040
    @kingkong1040 3 роки тому +3

    Excellent. I'm about to watch all the excel, python, and mysql videos on your channel. Great teacher.

  • @swingtrader833
    @swingtrader833 3 роки тому +4

    Thank you for this great tutorial! Liked and subscribed.
    I'm trying to learn python but trading tutorials that are any good are difficult to find, so your tutorials are very much appreciated.

    • @MattMacarty
      @MattMacarty  3 роки тому +1

      Thanks. Glad they are helping

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

    Really useful.Thank you for your sharing.Clear explanation and nice tutorial.

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

    Great Tutorial! Thank you!

  • @BLKDOLPHNDK
    @BLKDOLPHNDK 11 місяців тому

    Python beginner thanks every tip helps

  • @VictorPérez-i8s
    @VictorPérez-i8s 6 місяців тому

    this is excellent work! many thanks

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

    Great and Awesome video. Just created my first momentum strategy with python; and I look forward to creating more strategies using python. Very useful skill for the FinTech industry.

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

    Brilliant I'm falling off my chair. I tried TradeWindow etc now testing PlutoHQ Trading

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

    Informative tutorial. Thanks!

  • @SidhanthKumar-n5r
    @SidhanthKumar-n5r 10 місяців тому

    This is awesome, thank you so much for sharing!

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

    thanks for the vid! was looking for some help on testing system returns and this vid helped

  • @K-mk6pc
    @K-mk6pc 11 місяців тому

    Thanks for the Insight sir.

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

    Great video, thank you a lot !!!
    I have a question for you Math if you don't mind of course about the trading algorithm in cryptocurrencies, my algorithm works pretty well using technical analysis RSI and CCI but I want to improve it using machine learning for merging technical analysis and fundamental analysis what can I do here? what kind of machine learning models or algorithms do I have to look at?
    my second question: I took a look at the holt winter method forecasting but I think this method will not work cause of the randomness of cryptocurrencies to change over time, is it true what I just said ? or this is a method for extracting trends and seasonality in any kind of time series

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

      Lots of people do use and try to use ML for trading algorithms. Securities are notoriously difficult to forecast (or else we would all be rich). For sure traditional time series forecasting models are pretty much useless unless you are using the model as a trading signal rather than as a forecasting tool. I think a good place to start is with Python for Finance, Hilpisch.

  • @stevensun1945
    @stevensun1945 3 роки тому

    the code is simple and beautiful. love it!

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

    can you make a video of how pandas performs the actual buy or sell orders ?

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

      HI. That functionality will be implemented by your trading platform. You want to find one that has an API that will allow you to integrate custom algorithms. It's not necessary to use Python though. Thinkorswim used to have a custom scripting language that let you write and run alogos but I am not sure if it still does.

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

    Hi Matt, I am learning Python programming right now. Just started so to me everything is completely new. I would like to ask you, with all the knowledge you have, have you been able to trade using your own algorithm with Python? If so what were the results? I amorally motivated to learn this in order to be able to trade. I think is more precise then guessing using Candlestick reading. Nice content by the way, Thanks bro.

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

      Thanks for your comment. Sadly, no I don't have an algorithm to share with you. There are a couple of websites out there that have communities sharing algorithms though. Sites like Quantopian were great for people like you. I don't have one to recommend, but do a little research and I am sure you will find what you are looking for.

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

    Thank you for the video!

  • @juan-3350
    @juan-3350 3 роки тому +1

    thank you for this. learned a lot!

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

    Nice video, thanks.
    A quick question - Why would you use numpy log to calculate returns? I reckon there is a more accurate pct_change() function that can be used directly. Also, numpy log also needs a diff..
    Also, could you explain how taking natural log (numpy log) and diff together end up coming close to the pct_change function result, which seems more intuitive for returns calculation?
    Update: Okay, it turned out that I didn't know about log returns till now. I went and did my homework :)
    But should we use log returns here when investment and returns are actually for discrete periods (1,2,3...n days)?

    • @MattMacarty
      @MattMacarty  3 роки тому +4

      Glad it helped. You can use pandas pct_change function, but then you need to make other adjustments, so we typically use "instantaneous" rate of return. log, or LN change. This is used to make it easier to compare to other types of securities that compound interest continuously. It is also used to estimate volatility and in valuing options.

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

    wow simplemente gracias, he aprendido mucho con sus tutoriales, podría hacer un video sobre la gamma positiva y gamma negativa? saludos

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

    Hi, you explained very well, could you please make a video on price action kind of trading, not using lagging indicators, then it would be great to see your coding, and is it possible to do a multitime frame price action trading, if so please give the code.

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

      Thanks. You mean something like this? ua-cam.com/video/YjONqP78HHw/v-deo.html

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

    thankyou for the video @Matt Macarty, I didn't get the last part what is system return?

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

      With this simple strategy approach, you end up with a small loss over the test period (-6%).

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

    thank you for the video . i think the look ahead is not a problem here if you take a decision at the end of the day . right ?
    I am creating a complex AI model and i am generating features depending on SMA and EMA . It would be important for me to know what is the best practice in this regard . i am taking decision at the end of each 1 hour candle .

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

      Yes if you can trade at the end of the day you wouldn't worry about look ahead. In practice, I guess you could assume a buy on close order, however since your model can't be updated until at least the exact close, it seems a little difficult to implement.

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

      @@MattMacarty thanks , i am taking a decision two minutes before the closing of each hour in cryptos market . I guess i shall not worry about the look ahead .

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

    Everything works great. Thanks for the soft

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

    Sir, would like to ask. I thought if you're going to use the jupyter notebook, it doesn't need any internet and yet you can still use it . does using any API requires internet? complete newbie, still on learning process Sir.

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

      Some API's do require Internet. Typically if you are downloading data with an API is when you will need Internet.

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

    Wow, this video was a game-changer for me! It made creating my own trading bot a breeze. Highly recommend it!
    Also, check out "Algorithmic Trading Tutorial Python | Build Trading Algorithm from Scratch" by QuantInsti - another awesome video that helped me understand the fundamentals of algorithmic trading and build my own algorithm. Super helpful and definitely worth watching!
    Big thanks to both videos for giving me the knowledge and confidence to create my trading bot. Can't wait to see what it can do! 😄🚀

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

      Glad it helped. I have a few others that work with a broker: ua-cam.com/play/PLiPFKCVZWHKqtlvkq-V610SXMr2Ii9uAR.html

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

    great video, just wondering what AI technique did you use for this code

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

      Thanks. This one is pretty much straight forward technical indicators.

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

      @@MattMacarty ahh i see with machine learning used or neural networks at all? also what is the algorithm that is used for this? (sorry for so many questions)

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

      @@simonibra19 there's no neural networks here. This is a simple 21-9 MACD technical indicator strategy

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

    Thank you, boss.

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

    the short return is incorrect? if S_0 = 10 and S_1 = 11, based on your formula (LN(11) - LN(10))*(short signal -1) = -0.09531018, exp(-0.09531018) = 0.909090.
    whereas being short at 10 to 11, implies loss of (11 - 10)*(short signal -1) = -1, 9 dollars left implies 9 / 10 = 0.9 cumulative returns

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

      The return is based on the price movement for the day. If you are short and the price goes down, ignoring everything else, your position value increases by the amount the price decreased.

  • @aaronsarinanatoledo688
    @aaronsarinanatoledo688 3 роки тому

    Nice video! ... thanks!

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

    Hello Matt, thanks for the video. I have a question, Why would you substract the returns to the system returns when your are not holding the asset, I mean when signal = -1 it reduces the system returns, but if you sold your position , the future price movement doesn't affect. Are you considered instead of multiply by -1 do it by 0?..

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

      Hi so the model assumes you are always invested, either long or short. The -1 indicates a short position. But yes if you wanted to go flat you could replace -1 with 0.

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

    Hi Matt. Which software you use in order to program? I have TradeSation live data. How can i use? Thanks.

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

      I think you can use Python with many of the trading houses. I have Anaconda and a few other libraries that it doesn't come with.

  • @munivoltarc
    @munivoltarc 5 місяців тому

    Hi, try not to use price, lagging indicators, instead use price action strategy, will give accurate results, can you do this, many viewers are fed-up seeing these price lagging indicators usage in different channels of youtube.

    • @MattMacarty
      @MattMacarty  5 місяців тому +1

      This is for demonstrating how to write a simple back tester, not really interested in a strategy. Any strategy worth anything will not be found on UA-cam.

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

    Hi Sir. What is the setting of the blue line if I will use MA? 😊

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

      Do you mean you want the MA to be blue? You could use color = ‘b’

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

    Can you please explain the look ahead problem that we faced here!!

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

      So the data we are using implies that on the current day you know the closing price. This would not be possible until after the market has closed. I have seen it handled both ways, but you really can't get a signal until after the market closes and so it's unlikely that you would be able to act on any signal until the following day.

  • @eddierosario1537
    @eddierosario1537 Місяць тому

    I wonder if you can use this on penny stocks?

    • @MattMacarty
      @MattMacarty  Місяць тому

      I mean the video is just to demonstrate some concepts, but if there is price action it shouldn't matter if it's a penny stock or not

  • @NeerajKumar-gk9kz
    @NeerajKumar-gk9kz 10 місяців тому

    Plz make video trading in c+

    • @MattMacarty
      @MattMacarty  10 місяців тому

      Sorry. I don't know the language!

  • @LeonardoZ-wq8vr
    @LeonardoZ-wq8vr 6 місяців тому

    Can someone explain what's the difference between Buy/Hold (blue line) and System line (orange one) at 14:31?

    • @MattMacarty
      @MattMacarty  6 місяців тому

      So the blue line shows what would happen if you bought in 2016 and just held gold without any adjustments. The orange line is what would have happened if you tried to catch the swings in prices based on a simple moving average.

  • @LamNguyen-nm1id
    @LamNguyen-nm1id Рік тому

    i used a different set of data from 2021-02 to 2022-02 and upon comparing buy/hold with system, system outperforms buy/hold. what is your take on this?

    • @MattMacarty
      @MattMacarty  Рік тому +2

      I am sure at times a system can outperform. The question is will the same conditions prevail in the future? And then how do you know that conditions are no longer favorable for the system? Obviously people do make money with algorithmic trading. My examples are meant to show you how construct fairly simple strategies to give you an idea of "how to".

    • @LamNguyen-nm1id
      @LamNguyen-nm1id Рік тому

      @@MattMacarty that's very thoughtful thank you

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

    How can we get the Min and Max price dates from the 21 day's of previous High and low and to get the monthly high and low price and date and the difference between them in days and time/days ?

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

      I think probably the easiest way will be to start adding columns that track what you are looking for. If you just want to watch a max/min over a 21 day period, you can make a column something like df['21-max'].rolling(21).max(), etc.

  • @LamNguyen-nm1id
    @LamNguyen-nm1id Рік тому

    hi, what is the idea behind plotting the mark on 9-day when entry is 2 and plotting the mark on 21-day when entry is -2? are they just for display meaning it should mean no harm if i were to plot the other day around? thank you

    • @LamNguyen-nm1id
      @LamNguyen-nm1id Рік тому

      at the same time, what is the role of np.exp() here? i understand since the log return represent somewhat similar to pct_change, we should do cumprod() but i don't understand what np.exp is for, knowing that it is e**x for x to be the value of that index

    • @LamNguyen-nm1id
      @LamNguyen-nm1id Рік тому

      at that*

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

      Sure you can plot on either as long as it intersects at a cross. I use the 9-day so you don't have to reverse your logic.

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

      You can arrive at the same numbers using either pct change or exp. exp is also known as instanaeous rate of return and computationally it's somewhat easier to work with than pct change. Instanteaous rate of return is used as the standard particularly when dealing with derivatives.

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

    I have a problem in the Plot trades on time series. I can plot out all the things except the markers, it shows "ValueError: iLocation based boolean indexing cannot use an indexable as a mask"
    Please help

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

      Yes it sounds like you are trying to plot the wrong data. I would maybe creagte a separate Series based on your data mask and plot that.

  • @AleksOMFG
    @AleksOMFG 6 місяців тому

    I can't get the data ( TypeError: string indices must be integers, not 'str' ) I tryied the override of yfinance but it doesn't work neither.

    • @MattMacarty
      @MattMacarty  6 місяців тому

      Try using yfinance directly: yf.download(...)

    • @AleksOMFG
      @AleksOMFG 6 місяців тому

      @@MattMacarty Just finished doing a program with tkinker with it thank you, Im new in coding. 😂 Great tutorial keep it up!!!

  • @cybergrimreaper6669
    @cybergrimreaper6669 8 місяців тому

    can i use this to trade XAUUSD?

    • @MattMacarty
      @MattMacarty  8 місяців тому

      I would test to see how it does, and compare to various strategies. The video was meant to show people how to test strategies, not how to trade profitably

    • @cybergrimreaper6669
      @cybergrimreaper6669 8 місяців тому

      @@MattMacarty ok sir,thank you for all the great content

  • @arindam899
    @arindam899 3 роки тому +1

    where can i get national stock exhange of india historical data

    • @shonitsinghal
      @shonitsinghal 3 роки тому

      From their website or you can buy your broker API Data if you want to use the data comercialy.

    • @MattMacarty
      @MattMacarty  3 роки тому

      You might try twelvedata or alphavantage, freeium APIs for global data.

    • @aithon764
      @aithon764 3 роки тому

      yes you can get it.i can help u out

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

    Yahoo finance code doesn't work anymore?

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

      It should. Try pip install --upgrade pandas-datareader from the command line, then try running your code again. If that doesn't work try pip install yfinance. This will require you to modify the code to work with yfinance (yfinance.download)

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

      @@MattMacarty I figured it out in the end thanks tho

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

    How to Read a Binary File Like Metastock Files Using Python

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

      You can start with something like file = open([filename], 'rb'), then probably file.readlines(). And if each data point in a line is separated by a space or comma or anything you can you can split on the character.

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

      Thank you Mr.Macraty for reply, can you please explain it in a video?

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

    Hai..i have problem ..when i run my code there are some error like this
    NameError : name 'pdr' is not defined
    I already follow the instruction and also update to the anaconda prompt....but still can't.please help.

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

      Hi you can download the notebook from the link in the description. This is error means pdr has not been defined anywhere in your code. pdr is the alias I gave pandas_datareader in the first notebook cell. Maybe you have a typo?

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

      Ok sir...already get it...tq🙂