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

แชร์
ฝัง

ความคิดเห็น • 117

  • @swingtrader833
    @swingtrader833 2 ปีที่แล้ว +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  2 ปีที่แล้ว +1

      Thanks. Glad they are helping

  • @sudzbyte2215
    @sudzbyte2215 ปีที่แล้ว +1

    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 ปีที่แล้ว +2

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

    • @MattMacarty
      @MattMacarty  3 ปีที่แล้ว

      Thank you. Glad it helped

  • @PatrickBenitez6
    @PatrickBenitez6 5 หลายเดือนก่อน +6

    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  5 หลายเดือนก่อน

      Thank you. Glad it helped

  • @bsab9648
    @bsab9648 2 ปีที่แล้ว +1

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

    • @MattMacarty
      @MattMacarty  2 ปีที่แล้ว

      Glad it was helpful!

  • @stevensun1945
    @stevensun1945 2 ปีที่แล้ว

    the code is simple and beautiful. love it!

    • @MattMacarty
      @MattMacarty  2 ปีที่แล้ว

      Thanks. Glad it helped.

  • @sergeybure
    @sergeybure 10 หลายเดือนก่อน

    Informative tutorial. Thanks!

    • @MattMacarty
      @MattMacarty  10 หลายเดือนก่อน

      Glad it helped

  • @user-yd1fw9cm4d
    @user-yd1fw9cm4d 5 หลายเดือนก่อน

    This is awesome, thank you so much for sharing!

    • @MattMacarty
      @MattMacarty  5 หลายเดือนก่อน

      Thanks. Glad it helped

  • @bettinabautista4272
    @bettinabautista4272 2 ปีที่แล้ว

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

  • @juan-3350
    @juan-3350 2 ปีที่แล้ว +1

    thank you for this. learned a lot!

  • @chaddsmith3354
    @chaddsmith3354 ปีที่แล้ว +1

    Great Tutorial! Thank you!

  • @RenePatrique
    @RenePatrique 3 ปีที่แล้ว +1

    Great video! - :) - Thank you for this one! - Will you continue on the topic of trading system development with Python? - I'd love to see more of this. - Programming some MQL (MetaTrader) myself - but struggling to start to dive deeper into Python somehow. Videos like yours here do really help me. - Thank you for considering! - :) - Cheers!

    • @MattMacarty
      @MattMacarty  3 ปีที่แล้ว +3

      Thanks. yes I will put out a few more in the coming weeks

    • @RenePatrique
      @RenePatrique 3 ปีที่แล้ว +1

      @@MattMacarty Thank you! - :)

  • @chaddsmith3354
    @chaddsmith3354 ปีที่แล้ว

    Thank you for the video!

  • @zayb7861
    @zayb7861 ปีที่แล้ว +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.

  • @K-mk6pc
    @K-mk6pc 6 หลายเดือนก่อน

    Thanks for the Insight sir.

    • @MattMacarty
      @MattMacarty  6 หลายเดือนก่อน

      Glad it helped

  • @user-yj9pi9fo6t
    @user-yj9pi9fo6t หลายเดือนก่อน

    this is excellent work! many thanks

    • @MattMacarty
      @MattMacarty  หลายเดือนก่อน

      Glad it helped

  • @israelvtvita1104
    @israelvtvita1104 ปีที่แล้ว

    Everything works great. Thanks for the soft

  • @spiritverse4168
    @spiritverse4168 ปีที่แล้ว

    Thank you, boss.

  • @BLKDOLPHNDK
    @BLKDOLPHNDK 6 หลายเดือนก่อน

    Python beginner thanks every tip helps

    • @MattMacarty
      @MattMacarty  5 หลายเดือนก่อน

      Glad it helped

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

  • @aaronsarinanatoledo688
    @aaronsarinanatoledo688 2 ปีที่แล้ว

    Nice video! ... thanks!

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

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

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

  • @eddsoniko
    @eddsoniko 2 ปีที่แล้ว

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

    • @MattMacarty
      @MattMacarty  2 ปีที่แล้ว +1

      Thanks. Glad it helped

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

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

  • @munivoltarc
    @munivoltarc 11 หลายเดือนก่อน

    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  11 หลายเดือนก่อน

      Thanks. You mean something like this? th-cam.com/video/YjONqP78HHw/w-d-xo.html

  • @arnoldperalta5698
    @arnoldperalta5698 9 หลายเดือนก่อน

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

    • @MattMacarty
      @MattMacarty  9 หลายเดือนก่อน

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

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

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

  • @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 ปีที่แล้ว

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

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

  • @tomjohnson9518
    @tomjohnson9518 ปีที่แล้ว +2

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

    • @MattMacarty
      @MattMacarty  ปีที่แล้ว

      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.

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

  • @LeonardoZ-wq8vr
    @LeonardoZ-wq8vr หลายเดือนก่อน

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

    • @MattMacarty
      @MattMacarty  หลายเดือนก่อน

      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.

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

  • @AleksOMFG
    @AleksOMFG หลายเดือนก่อน

    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  หลายเดือนก่อน

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

    • @AleksOMFG
      @AleksOMFG หลายเดือนก่อน

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

  • @cybergrimreaper6669
    @cybergrimreaper6669 3 หลายเดือนก่อน

    can i use this to trade XAUUSD?

    • @MattMacarty
      @MattMacarty  3 หลายเดือนก่อน

      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 3 หลายเดือนก่อน

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

  • @NeerajKumar-gk9kz
    @NeerajKumar-gk9kz 5 หลายเดือนก่อน

    Plz make video trading in c+

    • @MattMacarty
      @MattMacarty  5 หลายเดือนก่อน

      Sorry. I don't know the language!

  • @garoxlee5670
    @garoxlee5670 2 ปีที่แล้ว

    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.

  • @munivoltarc
    @munivoltarc 16 วันที่ผ่านมา

    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  12 วันที่ผ่านมา +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 TH-cam.

  • @arindam899
    @arindam899 2 ปีที่แล้ว +1

    where can i get national stock exhange of india historical data

    • @shonitsinghal
      @shonitsinghal 2 ปีที่แล้ว

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

    • @MattMacarty
      @MattMacarty  2 ปีที่แล้ว

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

    • @aithon764
      @aithon764 2 ปีที่แล้ว

      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

  • @larrygranda6447
    @larrygranda6447 2 ปีที่แล้ว

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

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

  • @aspirantrade2697
    @aspirantrade2697 10 หลายเดือนก่อน

    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  10 หลายเดือนก่อน

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