PyAlgoTrade Tutorial (Part 1) - Beating the S&P 500

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome back to another video today i'm going to be talking about pi algo trade it's a python algorithmic trading library with an emphasis on back testing now in this channel i've already covered backtrader so i've released a few tutorials on backtrader but not everyone likes backtrader some people find it confusing so i thought i'd explore a few alternatives and show you how they work in case you like one of these alternatives better so the first one i'm going to start off with is pi algo trade and i have the open source project pulled up here and you see it has a focus on back testing and you know just like any other back testing a library let's say you have some strategy some idea some hypothesis on how you want to trade the market and you want to see how well that strategy would have performed on historical data or let's say you got some idea from some other guru or someone that said you can do this thing to make lots of money in the stock market and you want to verify whether what they're saying has any validity to it you should be able to do this using a library like backtrader or pi algo trade so if you go on youtube you're watching this channel on youtube you'll find all these people uh saying oh you can make 200 a day or 200 in 15 minutes and you can scale this up and type whatever you want here so if i typed a thousand dollars a day you'll get this guy if you type two thousand dollars a day however much money you want to make in a day there's going to be someone that claims you ha you can follow this simple strategy to make two thousand dollars so this guy's really popular he says make two thousand dollars a day trading one minute three bar play and if you look on its instagram page it has like lamborghinis and things like that or you know you'll see this guy with this lambo right there and all that kind of stuff and posing with the house usually i'm very skeptical of you should probably be skeptical of anyone that posts pictures with their car and stuff because most of people i know with a lot of money you know they they don't make a big deal about it or they drive like a buick or a a prius or something right here in silicon valley maybe a tesla you know but those aren't even that expensive anymore so anyway so if someone makes a claim like this that you can just look at that three bar play strategy right there and make two thousand dollars a day day trading then that should be easily testable right we have all the price data from the past and we should be able to feed it into a back testing library and see whether that's true or not right we don't need to take someone's word for it we don't need anyone to sell us on it we should be able to just run the numbers and you'll see how i did that in the past if you watched my video on opening range breakout it had like a 15 minute strategy and i actually back tested it for um all 500 stocks i believe in the s p 500 maybe all qqq so i did every single stock and it turned out that amd was the one that uh performed the best with the uh opening range breakout strategy but it turned out just buying and holding amd stock uh would have done better during that time period so it actually didn't end up being that great so um yeah so let's talk about a strategy what strategy i tried to think of a strategy that we could backtest that's not too complicated that way i can show you a short tutorial on it and learn the library easily but i also didn't want to show a strategy that underperformed just buying and holding so i thought i'd show a simple strategy that outperformed buying and holding the s p 500 and you know that might not be that exciting to anyone it doesn't say you know make two thousand dollars in 15 minutes or anything like that but um if you've ever traded for a very long period of time and tried to perform the outperform the s p 500 it's actually not that easy especially when you take into account dividends all the taxes you pay for very short-term trading um yeah there's a lot of factors at play there's a lot of emotions involved so um yeah it's actually very good to outperform the s p 500 or the qqq for instance so the simplest strategy that is documented uh that i found was this one by steve burns he writes a lot of books on trading and he has this article here this is from october of 2019 a moving average strategy that crushes by and hold and this strategy is actually uh super simple all he did is over the past uh 20 years so from the dot-com crash until the present day uh or until when this article was written um he just had an end of the month strategy so at the end of every month um if the price was below the 200-day moving average for spy the s p 500 uh then you would sell and if it closed above the 200 day moving average then uh you could buy and enter the position and hold it until it closed below the 200 day moving average and note the only actions are taken at the end of the month so the last trading day of the month is when we determine whether to take a trade so if you scroll down to the bottom of the page he didn't just say that it outperformed he actually showed us the data on what trades were actually taken which you know you got to respect that and you look at it you can see the chart here of the sp y and you can also see this equity curve here to see the starting balance and how this portfolio of would have finished by using this strategy and then he also shows the data on the number of winning trades and the number of losing trades and you can see with this strategy he got a 370 percent return which is great most people would be very happy with that and the s p 500 by and hold uh only returned 210 which is pretty good too uh so um yeah but you didn't have to sit in front of your computer every day day trading all day you got your 370 and you only made 11 trades over the course of 20 years so you saved a lot of time that you could have allocated uh two other things so in addition to outperforming the s p 500 another great thing about this strategy is that you didn't have to have a huge drawdown so a lot of people will say you should just buy and hold the s p 500 and hold it for 20 years and you'll do great and it turns out that it's true however have you actually ever held the s p 500 for 20 years straight some people can do it i personally have not been able to do it so i've been trading for over a decade now and i did have a portfolio before the financial crisis and if you held all the way through this thing and just kept going without panicking at all congratulations i personally had trouble holding and taking a 55 percent drawdown and while uh early in your career like a lot of people they started trading in the past year so they've they've withstood a 10 dip or a little growth stock uh dip but they've haven't really experienced a 55 percent drawdown like that right or maybe they have but their portfolio is only 10 000 or something like that so i've been able to withstand on a five figure account i can withstand a pretty good draw down maybe 30 percent 20 i don't know you need to determine your risk threshold but let's pretend you've been working since then and you've grown your account from uh five figures to seven figures in the present day right and can you imagine you know you've saved up two million dollars you've worked for 20 years and seeing that a two million dollar account have a 55 percent drawdown so you lose you have a drawdown of over a million dollars can you really stomach that or do you need some rules upon which you might exit and so the nice thing about this strategy is instead of enduring a full 55 drawdown and just holding over the course of this strategy a twenty percent drawdown uh is the biggest drawdown you had to withstand so if you have a million dollars and you watch it go down to 800k you can probably deal with that i feel like i could deal with that but watching a million dollars go to 450 000 you're like ah and you know once once you get really late uh in your career and you've been working a long time there's a point which you might not be able to make all that money back maybe you had some luck that let you get there and so it's nice to use a long-term strategy like this and following the trend here of the simple 200-day moving average might be something that helps you stay in in it for the long haul and another beauty of this strategy is look how few trades you had to make right there was only 11 trades executed right and you didn't have a big drawdown and what's nice about having fewer trades i personally love not taking many trades i know everyone on this channel wants me to do a minute by minute or tick by tick day trading bot which is cool it's cool to see happen but also you know i live in california short-term capital gains taxes are a lot you know if you pay 30 or 40 percent uh taxes when you start getting bigger and bigger gains you really notice that you know it's it's it's very very noticeable when you have a huge taxable gain at the end of the year and what's cool about this is it is able to ride some very long-term trends and so if you look there was a trade here from april of 2003 pretty much the bottom of the dot-com crash and all the way to until a december 31st 2007. so you got this 70 percent return here you were in the trade over 1100 days so you paid long-term capital gains tax you got a dividends over the course of several years and you rode this entire trend here and you got out whenever the s p 500 closed below the 200-day moving average this entire 2008 financial crisis happened and you're just out out of the market right and you didn't get back in until may of 2009 so you didn't get this very bottom here but you got back in right here when this trend changed and it went above the 200-day moving average and you were back in so you dodged this so you navigated the financial crisis you navigated the dot-com crash where the nasdaq fell by nearly 80 percent so to put that in perspective now uh picture uh the s p 500 falling from 4 200 to like 900 you know that'd be that'd be crazy so i personally think this is a great way to start our discussion of pi algae trade number one because uh the strategy is pretty easy to back test it's pretty easy to code and follow so it makes a good learning example number two it actually out formed the s p 500 which is great you know buffett bet someone people a million dollars they could not perform it and he won the bet and number three um this was tested over the course of 20 years when you look at these people are just cherry-picking uh simple examples of a trade that already happened like yesterday you know one day or one very specific chart for a very short-term period whereas this strategy navigated a 20-year period with a whole variety of market conditions not only did it navigate the dot-com crash which wiped a lot of people out it navigated the financial crisis of 2008 which destroyed a lot of people and if you fast forward till last year this article was written before then it actually helped you navigate the covet crash maybe by luck i don't know but actually this helped dodge the majority of the drawdown that you would have gotten from the covet crash that happened in march of 2020 so what i've done is written this strategy using pi algo trade in python code and you see on the right here i've run the script and it showed me the dates that this strategy would have executed and why and i can see the different dates i can see where i'm buying and selling where the 200-day moving average is and i see all these dates i can see my final portfolio value and i'm also using a feature called analyzers which analyzes this strategy so i can see the number of trades taken the average profit max profit average return you can see our largest drawdown all these different stats about the strategy and also it produces a chart of the buy and sell points and so you see on here all these green arrows so this green arrow right here in 2003 it's where you enter the trade and the red arrow is where you exit the trade i also have the 200-day moving average plotted here and you also see the growth of the portfolio over time over the course of this 20 year period and you see it closely matches what is shown in steve byrne's article so a quick roadmap of what we're about to do i'm going to a back test just buying and holding the s p 500 which is a good way to just show you the basic structure of pi algae trade then second we're going to implement this 200 day moving average strategy and then number three i'm going to show you how to use these analyzers and plotting and maybe the optimizers as well that way we could even optimize the strategy if we would like so in the next video i'm going to jump right into coding these strategies using python and pi algo trade so thanks a lot for watching and see you in the next video
Info
Channel: Part Time Larry
Views: 10,322
Rating: undefined out of 5
Keywords: pyalgotrade, python, algotrading, algorithmic trading, outperform, spy, s&p 500, moving average, algorithm, automated trading
Id: JcHOOEhaDtU
Channel Id: undefined
Length: 13min 37sec (817 seconds)
Published: Thu Jun 17 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.