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

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

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

  • @PatrickBenitez6
    @PatrickBenitez6 Год назад +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  11 месяцев назад

      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 года назад +4

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

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

    Python beginner thanks every tip helps

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

    Great Tutorial! Thank you!

  • @K-mk6pc
    @K-mk6pc Год назад

    Thanks for the Insight sir.

  • @VictorPérez-i8s
    @VictorPérez-i8s 8 месяцев назад

    this is excellent work! many thanks

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

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

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

    Informative tutorial. 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.

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

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

  • @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

  • @SidhanthKumar-n5r
    @SidhanthKumar-n5r 11 месяцев назад

    This is awesome, thank you so much for sharing!

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

      Thanks. Glad it helped

  • @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.

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

    the code is simple and beautiful. love it!

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

    Thank you for the video!

  • @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%).

  • @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? ruclips.net/video/YjONqP78HHw/видео.html

  • @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: ruclips.net/p/PLiPFKCVZWHKqtlvkq-V610SXMr2Ii9uAR

  • @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

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

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

  • @juan-3350
    @juan-3350 3 года назад +1

    thank you for this. learned a lot!

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

    Everything works great. Thanks for the soft

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

    Thank you, boss.

  • @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.

  • @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.

  • @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 .

  • @Josh-ov7td
    @Josh-ov7td 20 дней назад

    Im a bit confused on why the look ahead was profitable but the shifting forward 1 day to catch the close was not profitable. Is shifting forward different then a cross over of the 10 and 22 for this example?

    • @MattMacarty
      @MattMacarty  17 дней назад

      Sure you can try that. I think what we are basically seeing is that the entry and exit are critical in this strategy. Remember it's just for demonstration purposes

  • @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.

  • @NeerajKumar-gk9kz
    @NeerajKumar-gk9kz Год назад

    Plz make video trading in c+

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

      Sorry. I don't know the language!

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

    Nice video! ... thanks!

  • @eddierosario1537
    @eddierosario1537 3 месяца назад

    I wonder if you can use this on penny stocks?

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

      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

  • @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.

  • @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

  • @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’

  • @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.

  • @ayomideakinola943
    @ayomideakinola943 Месяц назад

    i can't import yfinance library in my jupyter notebook i always get error messages .....the command meant to see read panda data set to seem not to exist in my jupyter notebook modules thanks for the comrehensive videos anyways

    • @MattMacarty
      @MattMacarty  Месяц назад

      Try this in your notebook: !pip install yfinance

  • @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.

  • @garoxlee5670
    @garoxlee5670 3 года назад +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  3 года назад

      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.

  • @LeonardoZ-wq8vr
    @LeonardoZ-wq8vr 7 месяцев назад

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

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

      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.

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

    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  3 года назад +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.

  • @munivoltarc
    @munivoltarc 6 месяцев назад

    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  6 месяцев назад +5

      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 RUclips.

    • @titantrades-se8if
      @titantrades-se8if 21 день назад

      @@MattMacartyI actually have one. But unfortunately I don’t know how to code which could’ve helped me. Since I go to school and cant keep up to the market.

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

    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  3 года назад +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.

  • @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 7 месяцев назад

    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  7 месяцев назад

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

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

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

  • @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

  • @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.

  • @cybergrimreaper6669
    @cybergrimreaper6669 9 месяцев назад

    can i use this to trade XAUUSD?

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

      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 9 месяцев назад

      @@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🙂