Algorithmic Trading using Backtrader and Python [Part 1]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys this is costa and today i'm going to show you how you can use bug trader to test your algorithmic trading strategies using python i'll show you how you can install backtrader how we can set and default cache amount for our portfolio how to add data that we've downloaded in episode one and execute two basic strategies the first one will be to just log each line of our data and the second one will be buy and hold strategy essentially seeing how much money we could have made if we invest that if we invested in spy 20 years ago let's start first to install backtrader we have to do pb install back trader plotting mine here it says requirement already satisfied because i've done this before but it might take some time on your system depending on your configuration so let's start first let's import backtrader next we can initialize backtrader using bt.cerripro and basically this initializes backtrader using some default values for example the portfolio value will be set to 10 000 us dollars and we can check that by doing print f dot by using the get value function and then in order to run our strategy which in this case we don't have any we can do so we brought dot run and then we can print the final value of our portfolio let's run this and see what it returns as you can see we have a starting value of 10 000 and the final value is also 10 000 because here in this step we didn't make any buy or sell orders now let's try to set a cash amount that's different from the default so here we can do the repro dot broker dot set cache to let's say 100 000 and if we run this we can see that this change and now we have uh 100 000 dollars to start with next let's add some data to cerripro so if you if you see in on the previous episode we've downloaded the spy data sets for the past 20 years so here we can see the date open high low close adjusted closing volume and conveniently backtrader has a class that already knows how to parse this csv file and we can use that so here we can do data equals bt dot feeds and then we can say yahoo finance csv data and it takes three parameters first is a data name which will be the path that i'm running this from so for us will be episode 2 slash spy.csv and here it takes a from date equals to daytime dot date time and here we can say 2000 january 1st and that to date will be date time again dot daytime 2020 december 31st and we have to import date time as well so here we can do import date time and let's add these data to ceripro so we can we have to do the report dot add data and then pass our data let's run and see if everything works as expected we didn't get any errors so i'm assuming that this works as expected now let's move on and create a really simple strategy that will print each line of our um csv data so i'll move everything to a function so it's a bit more organized so here we can say the front and let's move everything under this function and here we can execute this everything still works as expected now let's add a really simple strategy that will print each line of our csv data in order for bacteria to understand that we have a strategy we have to in here we have to create a class and then inherit from bt dot strategy parent class so let's say class log data strategy and this has to inherit from bt strategy as we've said and this gives us some life cycle methods that we can use when the strategy is initialized or on each line of our data or at the end of our strategy and stuff like that so for the purposes of just logging our data we'll use the function that it gets called on every time that we have a new line in our csv file and for that we can use dev next which is the lifecycle method that gets called every time that we have a new row in our csv file so here we can do we can let's say let's get the closing price and the date so here we can do close equals self.datas zero dot close zero and then let's say date equals self dot data zero dot date time dot date zero and here we can use just print and print our data you can do f date and the closing price and the final step for this will be to add our strategy to backtrader so it knows which strategy to use so for that we can do bt dot at uh sorry we can do certipro dot add strategy and then give the class name of our strategy and if we run this hopefully yeah we'll see that it goes through all the lines in our csv and it prints the closing price so let's check for december 30. so it's it's the last day in our csv file so if we see here december 30 the closing price was 369.57 which is um each it uses this which it's within our csv file this is the adjusted close price but if you can see here we have more decimal places but here we just print only two decimal places that's it for a simple strategy now let's go ahead and create um just the buy and hold strategy let's delete this and let's say class by and hold again we have to inherit from bt to strategy class we need another function as well which is the constructor of our class just to just to get the initials portfolio valuation so here we can do self dot while start equals self.broker dot get cash which it will give us initial money that we have in our portfolio and we can also and we also need another variable called order exists which i will set to false for now and next let's use the next function and here we can say if not self dot order exists we have to make a buy order so let's say make a buy order and we have to determine the size so here i want to invest all the money of our portfolio so we can say int self.broker dot get cash divided by self dot data which will give us in units how much stock of spy we can buy and next we can do self dot by and give it give the size equal size and then we have to flip the switch so we don't make any other biostores so here we can say the order exists and let's add our strategy here and let's run and see what happens okay so we have an error yeah here i just miss to call the function so let's try again as you can see here we we've started with 100 000 and we've ended up with 380 173.5 which is a really good amount and last we can calculate the return on investment or roi for short so we can use another lifecycle method which is called stop which will only run once when our strategy stops or after the last line in our csv file so we can say roi equals self dot broker dot get value divided by self dot pulse start and this will give us the multiplier basically of our money and we can say print f roi equals roi let's see so here yeah as you can see we have a 3.8 which means that from the starting value if we multiply it by 3.8 then we get the final value and as a final step i would like to plot and see when the bars that happen and see and check like if everything seems okay so for just plotting we can do cherrypro dot threepro.plot and let's run this again here we can see the data from january 2000 all the way to december 2020 and as you can see here this green means that we had a bioster at this point when we started processing our data and we just hauled all the way through 2020 and our our portfolio evaluation is 380 173.5 that's all if you have any questions please leave them in the comments or if you have any issues running these please subscribe it means a lot for me and on the next episodes we'll see some more advanced algorithmic strategies and also if you have any questions please leave them in the comments down below and i'll try to post this code on github as well so you can um just copy then run it on your machines and and see the for yourselves thanks for watching see you on the next one bye
Info
Channel: Costa Piy
Views: 789
Rating: undefined out of 5
Keywords: backtrader, algotrading, stock market, automated trading, yahoo finance, algorithmic trading, python backtesting
Id: fbVlD-0Z6Uk
Channel Id: undefined
Length: 14min 13sec (853 seconds)
Published: Sun Jul 25 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.