VectorBT: Hyperfast Quantitative Analysis vs. Backtrader

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome back today i'm going to be discussing vector bt hyper-fast quantitative analysis it describes itself on its website as the ultimate python library for technical analysis and back testing at speed and scale sounds great sounds amazing who wouldn't want that in fact several of you have already commented on the youtube channel saying please do a video on vector bt and maybe compare it to some other back testing frameworks that i've discussed on this channel including backtrader and pi algo trade so i thought on this video i will do exactly that i'm going to start off with a quick overview of all of the source code and make a couple more videos showing more in-depth how to use this particular library we're going to go over how to generate a back test like the one shown here where you can similar to backtrader uh program in a strategy that shows all your buy and sell signals so this green arrow here is where you would buy and this red arrow is where you would have sold according to the strategy i've programmed and it also shows your returns on each one of these trades and compares your performance against whatever benchmark you're trying to compare against for instance buying and holding the s p 500 or buying and holding bitcoin seeing if your strategy would have done better or whether you're better off just sitting there doing nothing at all so uh this is pretty cool lets you generate these nice web-based visualizations it also lets you do this hyper parameter optimization uh some people might say this is overfitting where you can run many different combinations of your strategy parameters so for instance if you're doing this moving average crossover why not just try all of them see which one is the best you can generate this heat map to show which areas or which different combinations would have performed the best on historical data in case that's of interest to you you can also do things like label generation for ml models so for instance this one called xlb is labeling a local extrema so you can see this like a local peak and a bottom and there's another peak there's another bottom and there's another peak in yellow so maybe you want to run all of these labels on many different charts and feed it into some other system that you have and also there's a bunch of other labels that you can generate i ran this one on zillow stock to show these up trends and down trends and you can customize all these with different parameters saying you know maybe highlight all the 30 drawdowns for instance i'll also show you how to use this vector vt data updater so you see i have a class extending vbt data updater and what this does is allows you to programmatically fetch new data on a schedule so let's say you want to fetch data hourly or minutely or maybe every 15 seconds you can fetch new data and program your strategy into this data updater maybe you want to place a trade or send yourself some type of alert when a certain condition happens on price or volume and one other nice thing that's built in is telegram messaging so maybe when you receive this signal on new data that comes in you want to send yourself a little telegram message i'll show you how to use that and since everyone likes to create little bots i'll program a bot in an upcoming video and we'll deploy it to lenode so if anyone has any requests on a particular strategy to program in i'm taking requests now and i'll make it on this channel and show it to you i'll probably upload that like thursday or friday so let me know by tuesday or wednesday what you want to see and i'll program that in so let's go ahead and get started by jumping into the documentation all right so i'm on the vector bt website at vectorbt.dev there's a link to the documentation or we can go to the github page and browse the source code i'm going to click on documentation and we're going to look at how to install this thing so this is the package it describes some of the benefits of using it so we can just install it with pip as usual so i have an empty visual studio code editor here and i have a script i just made called backtest.pi i'm going to run it just to get a command line and i don't have it doing anything yet so i can just do pip install as usual so i'm going to do pip3 install vector bt and that will install the vector bt package and let us use it okay so now that that's done we're all set up so the first thing i want to do is briefly touch on vector bt versus backtrader everyone's always asking me what's the best back testing framework first of all i don't think it's pi algo trade i briefly tried that but i just don't think that's an answer there's not a ton of examples there's not a ton of documentation there and it wasn't clear to me how to do everything we want to do at least backtrader there's a lot of documentation there's a lot of examples and a lot of tutorials on how to use it vector bt here which describes a few benefits that i think are worthwhile the first thing in the vector bt documentation it discusses is performance so if you look at the first paragraph it says it was implemented in order to address common uh performance shortcomings of backtesting library so if you've seen in these videos where people make their own backtesting framework you'll notice they just loop through a lot of data and order using python lists for instance and a lot of them are really slow if you actually try to run it for large amounts of data or very short time frame data like minute data like a lot of you want to do so maybe you want to run some strategy for every minute of data for the past 20 years and you run one of these and you run it on every single instrument it'll be really slow in fact if you look at my backtrader tutorial i believe it was yeah the stock trading app tutorial part 19 where i back tested opening range breakout with backtrader what i did was run this orb back test on every stock in the nasdaq 100 so 100 stocks and i did it for three different values of the opening range and i posted the source code you'll see where i did this opt strategy so i optimized the strategy for the number of opening bars at 15 minutes 30 minutes and 60 minutes and i ran through all of them and i think this actually took hours i can't remember the exact amount of time it took but i had to let it run for a long time it's not like i could just show it in real time on the video so vector bt claims to solve some of these performance bottlenecks by using these optimize number num numpy arrays with this thing called numba if you go to number.pidata.org number makes python code fast it's an open source just-in-time compiler that translates a subset of python and numpy code into fast machine code so there's these all these optimizations that are happening under the hood here and so it claims that uses these highly optimized numpy arrays and it also overhauls the traditional object-oriented approach and so if you look at the source code for my opening range breakout strategy what you do in backtrader is you extend this backtrader strategy class so i have this class called opening range breakout and so there's this object here where i define a number of parameters and you see how there's this next function and what you do with backtrader as you're basically playing the tape in order in this list and you're saying next bar next bar next bar if this do that if this do this other thing notify order place an order check on the order status and so basically you're right you're filling in this long next function over and over again so a vector bt says it's able to process these arrays and run all these operations all at once somehow so that runs in a more highly efficient manner so i haven't personally benchmarked the a similar strategy on both of these to see whether this is the case but it looks like they have some benchmarks here showing uh using vector bt versus just a regular pandas data frame for instance and it looks like it is much much faster here and it's definitely much faster than backtrader which i think just uses pure like python classes and python data structures so this uses these very highly optimized numpy arrays the second comparison i'd like to do between vector bt and bactrator is the community and documentation so let's say you adopt one of these open source back testing library and you're relying on on it for your own trading your own strategies and you run into a problem how likely is it you're going to be able to google it and find an answer how likely is it you're going to be able to look in the documentation and figure out how to do it and if you reach out for help is anyone going to help you if you submit a pull request or a bug report in the github repository how likely is it anyone's going to reply to you and i'll say vector bt does very well in this department i found this documentation very well organized there's a lot of these jupiter notebooks here where you can run through a variety of examples there's actually a dashboard example here using plotly and there's a few articles that the author of the library has written himself and before i forget let's thank the author oleg palakkaw here who has generously written many many thousands of lines of code to implement this library that we will rely on so thanks for that if you click on getting help here looks like there's a private chat you can go to stack overflow or the github discussions here and what i've noticed here that's pretty nice is this uh library is very actively maintained so like one thing i like to look at how many stars forks people are watching it so how active is it and how many commits are there recently because there's a lot of just dead projects out there including some of the repositories that i've put online so it's very difficult to maintain an open source project when lots of people are asking for things all the time and you'll see the author committed code just three days ago you see there's commits in august here so this library is very actively maintained if you look at the pull requests here there's a lot of close pull requests and not that many of them open meaning contributions are accepted and merged into the source code pretty frequently on the other hand backtrader while it has great documentation and i will say the backtrader documentation is quite good there's documentation on each feature of the bactrader library and another nice thing of it about it is the community so there's a forum here with tons of posts where people have asked each other questions and gotten answers and uh here's the source code here and so one downside i've noticed on backtrader recently when i first started discussing it it was very actively maintained but if you look recently uh the commit frequency is getting less and less so you see back in july 2020 there's only been a couple commits in the last year and i think that's because the author who replied to many many posts on the forums by the way and did a great job he's a bit i find him a little condescending at times if you if you look at his post when people ask questions uh however i could see where it gets irritating to answer the same questions over and over again so he kept pointing people to the documentation um so good support here but also this this project is kind of considered complete i think some members of the community have forked it and maintained like back trader 2 or something but it's not quite as actively maintained by the original author which means over time there's not going to be as many new features added whereas vector bt i think is a little more actively maintained right now and there's a lot more commits and activity uh occurring right now so i have a lot of confidence that this is being actively maintained i also mentioned pi algo traded in the past um i want to say the documentation here covers some basics like doing the very basic things but if you actually dive in it appears to be very well documented but if you dive in more and more beyond these simple examples here i think there's actually not that much there and you'll you'll have some trouble figuring out how to how to do new things with pi i would trade and there's not like a forum or much activity in github on that right now so as of right now i'm not really considering using pi i'll go trade much in the future the third thing i want to talk about is ease of use how easy is it to get up and running write a back test maybe do some live trading using that back test well the easiest way to figure out ease of use is to look at a couple code examples for the same simple strategy between each framework and so if you look an example here on backtest rookies this website shows a simple rsi strategy using backtrader and we've written something like this before on the channel with backtrader if you're familiar with object oriented programming it's pretty straightforward but also that it's a little bit confusing how it works at first but it if you look here what they do is they create a strategy and you need to extend the backtrader strategy class you run some indicator in the constructor so this is the rsi indicator and so this indicator is running and you give it a period that you're running it on and then you just need to override this next function and so you just go through all the data in order and you're saying if you're not in the position if the rsi is less than 30 by 100 units else if the rsi is greater than 70 then you're gonna sell 100 units so you're just buying the oversold condition and selling uh the overbought condition just a basic strategy not a very good strategy by the way um but so the way backtrader works um before you run this class what you want to do is instantiate this cerebro object which is like the brain cerebros like the brain of the program you set a certain amount of starting cache you need to add the strategy to this cerebro object you need to provide it a data feed so they're using quandl here as their data feed and they're using apple uh between the time frame of 20 for the year 2016 and they're just adding that data feed to cerebro they're running the strategy on that data feed they're setting an initial amount of cash and then they're calling this run to run it and they're just showing the final value after running the strategy so not that many lines of code and then you can also plot that strategy using a candlestick chart so that's not not too complicated now let's look at what the equivalent looks like in vector bt and i'll go ahead and just write this code real quick just to show you a quick example so this is an example on the documentation of vector bt and so you see the first thing you do is import vector bt as vbt and then you just need to get some price data so it looks like they use yahoo finance to get bitcoin prices and so i'll do that real quick so we have a simple uh empty script here and i'm just going to import vector bt or vector bt as vbt and then i'm going to do bitcoin price equals vbt.yfdata.download so we're using the yahoo finance data that's built into vectorbt and so this wraps a number of data provider libraries which is kind of cool so if i jump into here you'll see there's yf data here so you'll see some other places you can get data from so let me quickly talk about that so we have yahoo finance data we also see it imports binance as well so we have a binance client which is kind of cool if we want to use binance as a data source they have a synthetic data source here if we want to generate fake data to test our application we also have this geometric brownian motion so that extends synthetic data so we can generate all this random data if we want to test that out and then also we can use yahoo finance data so that's good for pulling in stock data right and it can also pull in a crypto data but not as detailed of crypto crypto data as other providers and then you look there's also a binance data class here which is cool because it includes python binance which we've used for our binance trading bot and you can also see they include ccxt data so i've also done a video on ccxt the cryptocurrency exchange trading library which allows us to use multiple cryptocurrency exchange providers like kraken and uh coinbase and a lot of other different providers ftx and we can even get a one-minute time frame data from these data providers so if you want to do focus on crypto this is probably a good way to go instead of using yahoo finance i don't think i don't think yahoo finance provides really detailed crypto data but but maybe it does or it probably only provides it under on the top tokens so you can probably get more obscure tokens using something like ccxt or binance okay so we got our data provider here and so i'm getting the bitcoin price and i'll just take off the start and end i'm just going to get all of it and get the closing prices and actually i'll get all of the prices not just the close and let's see what that looks like so i get the bitcoin price and i print it like that and let's see what the data structure looks like so just with that one line of code right there we're able to download yahoo finance data and it gives us this yf data object and so if we want to get the actual data and show it i need to call.get here so i'm going to print btc price dot get and let's look at that data structure okay and that should show us a pandas data frame so if i do get you see our we have our typical uh date open hilo close dividends stock splits shows us the number of rows and columns and it gets us all the bitcoin data that's available so yahoo finance only has this back to 2014 when bitcoin was only 465 us dollars a share now at 62 000 so buy and hold just there is pretty dang good okay and so when we want to run our strategy we don't want to run it on this entire data frame we might just want to run it on the open or the closed price so in this case i can do get and i can do dot get close and that will give me the closing price so if i print the closing price like that you should just see one column here so we have just the date and just the close so i'm going to assign that to a variable and so i'm going to do closing prices equals btc price dot get close and then also let's just print a btc price.get and i'll put the source code up so that's our closing uh prices right there and then we need to run some type of strategy on those closing prices so you can see here they're running you know your typical fast m a slo-ma and running that for uh looks like a 10-day moving average and a 20-day moving average and they're naming them fast and slow uh the strategy we just showed on this backtest rookies is an rsi strategy so to reproduce the strategy that we just saw let's use the rsi indicator that's built into vector bt so i'm going to do vbt.rsi.run and i'm going to run the rsi indicator on closing prices just like this so i'll do closing prices now one thing that's kind of cool is i can jump into the source code if you have the python extension installed in your visual studio code i'm going to jump into rsi here and this is the rsi that's built in to vector bt so you see this indicator factory is instantiated and applies rsi to that series of data and you can pass a variety of parameters like the window for instance and so i'm going to jump into this indicator factory so you can see when i jump into rsi i go into this basic dot pi so this is organized into data so our data had yahoo finance data it had binance data and ccxt data so this package is kind of cool in that it bundles in a lot of best of breed libraries that we discuss on this channel right i've discussed what yahoo finance i've discussed uh ccxt and python binance packages on this channel we've already used them it also bundles a variety of technical indicator libraries so if you look in indicator under factories here you'll see on from here if i type from underscore you'll see there's from ta lib so you can actually run any indicator that's built into ta lib right and you can run anything that's built into pandas ta so this is from pandas ta and i think there's also from ta so there's this python ta as well and so what you're able to do is call any indicator that's built into any of these other indicator libraries and it provides a wrapper and a nice abstract way of accessing all of these right and so what i'm doing is just calling the basic rsi indicator here but you'll also see you know there's b bands for instance for bollinger bands you'd probably find stochastic here any any other moving average or whatever else you can think of it's probably built in here and if it's not built in right here you can probably use the name of one of the functions that are built into pandas ta which has like hundreds of them right so what we'll do here is apply the rsi indicator to the closing prices and then we just want to calculate our entries and our exits so what we can do here is set entries equal to rsi.rsi below 30 and then our exits equals rsi.rsi above 70. so we'll entry enter if rsi goes below 30 that oversold threshold a lot of people use and exit when rsi is overbought at 70. right so if i print my entries and i print my exits you should see some true and false values coming through saying whether we should enter or not whether we should exit or not and you can see there's my entries they're all false right now and it says exit true in the last few days so it's saying i should be selling my bitcoin and that's why this is not a great strategy because you don't want to sell your bitcoin right so i'm gonna look at the bitcoin price on trading view here uh so we'll do btc usd under crypto yeah so let's look at that and why is it saying to exit so if i look at relative strength index and i plot this on the graph you'll see that bitcoin is currently overbought right because it was like 40 000 a couple weeks ago right and then let me look at the daily chart here and i have too much crap on my chart right here now hold on let me close this uh stochastic here okay yeah you see you can see uh bitcoin was at like 40 000 about a few weeks ago right and then you can see it quickly moved up and you can see it's an overbought territory so according to this simple rsi strategy you should be selling right now bold strategy let's see how that plays out for us so one thing you'll notice here is there's multiple truths in a row it's because bitcoin is overbought here for multiple days in a row multiple closes right so since these are all consecutively true we really just wanted to exit this one time when it first crossed over into overbought territory and so what we're going to do here is set a crossover equals true and then also when it first becomes oversold we're going to enter and where first becomes overbought we're going to exit right and so if i run that that'll only do true on the very first time when it crosses over the threshold okay so now we have our list of entries and exits all right now that we have our entries and exits we need to apply these entries and exits to our closing prices and actually run a strategy and so to do that i can create a new portfolio variable here and i can use vbt.portfol and i can do from signals so you can see whenever i type dot here my auto completion shows me a variety of methods so there's a from holding so we can see our performance when we just hold we can do from orders to see enter in a number of discrete orders and see the results and you can also do this from signals so i'm going to see the portfolio from signals the performance from signals and then i'm just going to pass it my closing prices and then my entries and my exits and i also need to pass it some initial cash i'm going to do init cash equals and we'll just do ten thousand dollars here right and then so i should be able to just run portfolio.show no portfolio.plot.show to show the results of this overbought oversold strategy okay so i'm going to run that and it should fire up a web browser for me and let's see how it goes and it runs just like that and just with how many lines of code do we have it's like 10 lines of code really i put a bunch of spaces in there and printed out a lot of stuff so you'll see just how easy this is actually so if i were to condense this down a bit i'm not saying compress all your code i'm just showing you that this is like 10 lines of code right and we can space it out a little bit to generate this back test of this overbought oversold strategy you don't have to know anything about object-oriented programmer programming really and initialize the constructor and all this stuff or cerebro object just 10 lines of function calls and you're able to get this nice visualization of buying and selling and see the performance so there you have it this is our performance look at us with bitcoin trying to time it and buy and sell it and there's a buy and sell buy and sell buy and sell let's look at this one so uh this is our oversold we sold here we missed this entire run up here and then we waited until it was oversold right here we bought but then we sold really quickly and you can see we bought again and then we sold at a loss so you can see all these losing trades where we lost 40 percent 20 23 25 on bitcoin one of the best maybe the best performing asset of the decade and then you can see the cumulative returns here this here isn't our strategy this is the returns from buying and holding so that's the benchmark the actual return we got from our strategy is down here it looks like we didn't make any money maybe we even lost money so you can see this rsi thing that people talk about all over the internet all over youtube turns out it's a horrible strategy and it's not going to make you any money at all so it's almost like we should have done the exact opposite yeah i should do the opposite i should if every instinct you have is wrong then the opposite would have to be right yes i will do the opposite i used to sit here and do nothing and regret it for the rest of the day so now i will do the opposite and i will do something so let's try this out let's go ahead since that's completely wrong let's see what happens if we uh buy when the rsi is above 70. so let's make the rsi above 70r entry and rsi below 30 our exit exits and let's just reverse it and do the costanza strategy and see what happens so i'm gonna run that so it turns out uh bitcoin goes parabolic right and so you want to actually enter when bitcoin is overbought right so you buy when it's overbought you catch this entire move right because it's never uh oversold right and then when it's oversold then you then you exit right and then you it proceeds up really quickly it's overbought there you enter again then it's oversold right so it turns out actually buying the overbought condition actually makes you more money in this particular case so uh yeah i think that's it for now i think this video has run a bit long i touched on vector bt versus backtrader made a few comparisons on the ease of use the speed uh the usability the the community and the documentation so far i'm liking this vector bt library quite a bit and so in the next video i will dive into some more of the functionality and depth i'll use this data updater i'll talk about telegram notifications uh do a new strategy fetch some data in real time do some buys and sells and also deploy this onto lynnode let me know if there's something specific you want to see otherwise see you in the next video thanks
Info
Channel: Part Time Larry
Views: 11,793
Rating: undefined out of 5
Keywords: vectorbt, tutorial, backtesting, python, vectorbt vs backtrader, backtest, rsi, indicator, strategy, algotrading, bitcoin, crypto, stock
Id: 9rpMzng_aw0
Channel Id: undefined
Length: 27min 49sec (1669 seconds)
Published: Tue Oct 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.