Backtrader Live Trading with Interactive Brokers (Part 1)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome back in this video i'm going to show you how to use backtrader's live trading functionality so backtrader is a back testing framework for python that allows you to test your trading strategies i've done a number of tutorials on this in the past including this three-part tutorial on trading with backtrader as you can see you can plug in a strategy that you code yourself and test the outcome of a particular strategy with a simulated broker on historical data and i'll show you what your entry and exit points would have been and how your portfolio would have grown over time when using a particular trading strategy and if you'll remember i even took a strategy such as this one opening range breakout which this guy claims is a great strategy and back tested that and determined it actually wasn't a great strategy so i back tested this opening range breakout and walked you through the code here and showed you how to test for a variety of parameters so what we did in this back test is we went through all stocks in the nasdaq 100 and a ran opening range breakout strategy we coded that up and tested it with multiple parameters using an opening range of 5 minutes 15 minutes and 30 minutes and it turns out the best performing stock using this strategy over i think a year or two of time frame was amd stock but it turns out that just holding amd stock over that period of time actually performed better than even using this strategy so you might as well just bought amd and sat tight so uh even though we didn't find any real edge in the strategy you see the value of being able to verify someone's claim so often you'll hear someone say a particular strategy or indicator or approach to trading the markets is very effective well you know you don't just have to take their word for it you can actually code this up run the results and see what would happen because if it's really a good strategy it should show up in the data so backtrader is nice in that you can test your strategy ahead of time you can use the simulated broker and simulate what your account balance would be after running this strategy over some period of time on historical data but what i have not yet talked about is the fact that you can swap out this simulated broker back in with backtrader and plug in a real broker and have that same strategy class trade the real-life market so once you're satisfied with the results of your back test you can plug in a back-end such as the interactive broker's backend and have it trade your interactive brokers account using the code that you just wrote and so today what i'm going to do is show you how to do that so i'm going to connect backtrader to an interactive broker store so an interactive broker's backend here and have it connect to my trader workstation and actually trade using real life data with the exact same strategy a class that you would have written to backtest a strategy so before i get started interactive brokers has provided some special links just for my channel and i have them linked down below so for this video there will be a video like this and you can just click on this interactive brokers link and check out interactive brokers if you could do that for me i'd really appreciate it it helps support the channel so you can browse through here and check out all the features of interactive brokers so uh with that all said uh let's go ahead and get started with backtrader and discussing how to integrate it with interactive brokers so that we can trade based on real live data so to get started what i've done is fire up trader workstation tws and this is just the desktop software for interactive brokers and so the way this works is i have some python code running in backtrader and it opens up a socket connection to trader workstation and sends it the appropriate commands so that it can execute trades and it can also access all these data feeds coming from interactive brokers so the stock market which is what i usually trade is not open right now it's thursday night just got off work so i need something some kind of data to work with and i've never traded a forex before but nonetheless i'm going to try it out and i'm not going to really make any crazy strategy here what i really want to do is just demonstrate some data coming in to backtrader and so i'm going to use this uh japanese yen here since it's it since you can trade it right now so whatever time frame you're interested in you can feed in real time data from interactive brokers so this could be just tick data second data or you can aggregate by minute bars hourly bars and that sort of thing and so we're going to connect to this and just feed in some real time live data and we'll show you how to just set up the structure of backtrader live trading so that we can do this and how to connect it to interactive brokers and then i have tomorrow off on friday and so what i'll do is take the part from the data feed and then connect it to the stock market whenever the stock market's open tomorrow and we'll execute some trades against the stock market on friday which is july 16th and so that video will be the second part in the series so the first part is just setting up backtrader uh connecting it to interactive brokers and feeding in some data and we'll probably do a buy in a cell just a flip-flop between buying and selling just to show you the mechanics of how that works and then in the next video i'll connect it to a stock market data and fill out the strategy class a little bit more so let's go ahead and get started with coding some of this up so as usual i have visual studio code open and i have a new folder and now i have a file requirements text where i'm going to list all the python packages that are required for this tutorial and so we're going to install backtrader so i'll include that and then backtrader needs to integrate with interactive brokers so the way you do that uh for live trading is you actually need to install this ib pi and so uh there's actually a great tutorial on how to use this uh by the author of backtrader so he wrote this article there's a couple parts that are a little bit outdated but so i've updated it a little bit and i'll show you how i did it on this tutorial so you'll notice he installs this ib pi here from this zip file since it wasn't in the python package index but you can actually just do it like this do ib pi2 and then i'm going to create a new file here so we're going to create a couple of files i'm going to create one called 4x.pi and one called stock.pi and so we're going to start with 4x data even though i'm not a forex trader or anything since it's like 8 pm over here i just want to get some live data going so that i can show it to you and so in forex dot pi i'm going to go ahead and run this and get a command line going and this activates an environment that i have set up this virtual environment and so what i'm going to do is do pip 3 install dash r requirements text and let it install those packages and you see i've already installed these backtrader and ib pi 2 and so i have those packages now and so i can go ahead and get started with importing this stuff so i'm going to do import backtrader as bt so i'm in the 4x.pi now and so i'm importing backtrader and then i'm importing it as bt i'm going to create a function i'm just going to call it start and this is where i'm going to initialize backtrader i'm going to put a pass for now i'm just going to call the function start okay so i'm going to say print starting backtrader right and i don't really need to do this but uh starting backtrader just to make sure things are running without any errors okay and so i'm going to go back to backtrader here and so there's the quick start guide on the backtrader home page and you can see the first thing you need is this cerebro object here and so i'm going to take that and so i'm going to initialize that in my start function and so i'm saying cerebro equals backtrader.cerebro and as you remember i've discussed this um it's like the brain of the program so it means like the the control center of the program and so we take we initialize backtrader uh we have this little control center and then all we need to do is add a data feed for it to operate on and then we need to add a strategy class so a strategy to apply to that data feed and then we just need to run it and see the results and so now that i've instantiated this i'm going to go ahead and add a data feed so in the past we've added data feeds just using a csv file and i've also shown how to query a database a sql database and then feed that into a pandas data frame and provide that as a data source but in this particular case we want live data that comes from interactive brokers and that's why we had to install ib pi 2 so to get that live data feed we need the ability to talk to the interactive brokers trader workstation and so backtrader provides these stores and these stores allow you to have these uh plugable backends so you see how there's a store here if you look at the interactive brokers under live trading here so there's various stores that you can use and so this is the interactive broker store and so we can copy this bit out of the documentation and so we're going to do a ib store and so we can just do store equals bt.stores ib store and then default is running on localhost so we don't really need to specify the host here and the port here mine is running on 7497s so if i go up to file in tws and then under global configuration you'll see there's this general here and under uh api settings uh you can get your port number so mine's running on seven four nine seven okay so i'm gonna go back here i'm gonna do port 7497 and i don't think i need this client id here and then i'm just gonna do data equals ib.store.getdata and i need to specify you know what what instrument what contract i'm feeding in data for and so in this particular case for data name i'm not going to use this i'm going to use we said we're going to use this japanese yen so i'm going to do usd.jpy then we need to tell it the type of security or the type of contract and so i'm going to leave this in a comment on this code that i publish and so you'll notice there's these basic contracts so when you're dealing with interactive brokers you can deal with these fx pairs stocks indexes futures options and so these all have different uh security types here and so what i can do here for this fx one i can just do a security type is cash so let me do sec type equals cash and then i need an exchange and for exchange here it looks like it does this ideal pro so i'm going to take that as well so i'm going to take that do exchange equals ideal pro okay and then i'm going to give it a time frame for the data feed so i'm going to do a time frame equals backtrader.time frame and then what do we want to trade so we can give it days minutes microseconds ticks so i'm just going to do seconds here just for fun i don't trade on the second time frame but uh some people think it's interesting to see a whole lot of data come in okay and so this will get us a second data feed okay and so what we can do is we can resample this data so it'll give it a second by second but we can say we want the 15 second we want to aggregate that by 15 second bars at a time so what i can do is do a cerebro dot resample data okay and so i will give it my data and i say time frame equals and i'm going to do bt.time frame dot seconds and i'm going to say compression equals 15 and so what that means is the data will be aggravated aggregated into these 15 second bar so i'll get the open high low close and volume for every 15 second time period okay and then after that i can just do cerebro dot run so i'm just gonna run this i'm gonna add the data to cerebro and just run it and it says ib store is not defined that's because i changed the name to store so i'll change this to store i'm going to run that it says starting back trader sorry version 76 it connected to tws so you see how it runs this and just stops so that's just because we didn't do anything with the data right we added a data feed and ran it right but we don't have a strategy yet so the next thing we need to do is create a strategy class and the way you do that with backtrader is you just create a class right so this is an object oriented programming concept so you just have a class and you just call it whatever your strategy is so you can call it my strategy like that okay and then a class for those unfamiliar i hope most people are but if you're not a class is just a a template for an object it's just a collection of data and functions that are all related so this class represents a strategy right and so i'm going to do a bt.strategy and so what this uh this strategy class does is it extends this base backtrader strategy and this does a lot of work behind the scenes and so what i need to do is just extend this backtrader base strategy class and so if i were to look at this class the code inside right you see it has a whole lot of logic built in it knows how to read through data in order it knows how to process orders there's a lot of built-in functions in here a lot of work it does behind the scenes that i don't need to worry about it encapsulates a trading strategy right and what i all i need to do is override this and fill out a couple of functions that it's going to call for me and so i'm just going to create an initialization function so i'm going to do init like that so this is the constructor function so i can just print initializing strategy so that's all i'll do for now and then i need a function called next and what this next function does in a backtrader is every time there's a new piece of data it calls next and then i can access the data feed so i can access it a bar at a time i can reference previous bars and then here i can specify what i want to do with the next bar of data so i'm going to fill this out shortly so i'm just going to print next here next okay and so i have this strat this really basic strategy class defined and so what i can do now is i'll just do a cerebro down here add strategy and i'll just give it my strategy right so i'm adding a strategy and it's called my strategy right so let's do that and let's see if this runs and so when i run that again you see i got this initializing strategy and so it instantiated my strategy but it didn't call my next function for for whatever reason and why is that and uh yeah i i think i got the data wrong here so i think that's supposed to be lowercase so it should be set type all lowercase like that so if i run it now you'll see you see next a bunch of times right so it's doing it's getting the next bar next bar and just printing next but i don't want to print uh next over and over again so i'm going to stop that and so instead of printing next i'm going to do self.data dot close zero like this so i'm going to print the close okay so i'm going to comment that out and run it all right so when i run that you see i have actual price data coming in so these are closing prices for each uh 15 second candle and so let me go ahead and print a time stamp there and also let's print the open hi-lo and close and so what i'll do here is just do a print self dot data dot open zero and and the zero and the reason why it's the zeroth here right this is the most recent bar and so in backtrader i can print the previous bar by using negative one and the one before that by printing a negative two right and so i can access those by the index so each time next comes through it's like the tape is coming through and then the zeroth there is the most recent bar and then i can access any of the bars before that if i want to like compare those or run a moving average on the previous 20 or find the highest of the previous 10 or i can operate on any any bar that came before this but i can access the current one with the zeroth bar here and so what i'm going to do here is create a function called log data right and so i'm going to create this function called log data and what this will do is have this functionality that just prints the data okay and in that tutorial that i referenced earlier that was written by the author of backtrader what he did is he created a function called uh log data like that i use an underscore and he created a list and he just appended to that list the open high low close and volume and a time stamp here and you see how you access the zeroth element of each of those and so this uses dot format but since we have python 3.7 now we don't need dot format we'll we'll just use uh f strings so so what we'll do here is just log all this data so i'm going to do um so we'll we'll create a list first and then we'll join that list together into one line so i'm going to create a new list called ohl cv so open high low close volume and i'm going to create a new list and then i'm going to append to that list so i'll do append so i'll append the open there just like that and i'll do ohlcv dot append self.data.hi zero like that and then let's copy that a couple more times and we'll just grab the volume so we'll do that do that open high low close and then volume and then for this one we'll do the date which is self.data and it looks like it's uh date data.datetime.datetime 0. okay so we'll do that so i'll do ohlcvpnself.data.datetime.com date time i think we can just do datetime.datetime like that so let's try that and then we'll print a comma dot join ohlcv like that and let's see how that works okay and so what that should do after i get it working right so i have a start here i have a log data and then let me call log data from the next function so each time we get a new bar we're just going to print the open high low close volume and then the date okay and then i can do self.log data like that and then let's run that see how it goes it says item 0 expected string instance date time found so let me see if i can just cast that to a string like this and let's try it again i'm going to run it expected string instance float found and so let's go ahead and just convert all these to strings like that because we're just going to print these as strings okay and then let me try that one more time okay there we go so i run that and you see we have these 15 second bars coming in here so you have the 42nd minute 4215 4230 4245 and then yeah so you see we have a consistent stream of data coming in and you'll just see new bars come in over time so every 15 seconds you should see another bar so looks good we have price data that's coming in we have a real time feed from uh backtrader connected to interactive brokers which is exactly what we want you see we have another one coming in the 30 second mark there and this japanese yen it it's not very volatile it's not like the currency market is is changing really quickly so you see these are very small price movements that are happening here uh what you'll also notice is backtrader also fed us like a lot of prior data that's prior to the current minute so it's the 44th minute of the hour right now but you see how this data going back like 20 minutes or so or maybe more than that right and so it'll show you some old data first and then then it'll start feeding you live data after that and so since it's feeding you uh some older data as well what you want to do in this next function when you write your strategy you don't want your strategy to process this old data you want to wait till you get the live data like the new fresh data coming in in real time you don't want to run your strategy on these old bars and so to do that we're going to override one more function okay and so we're going to create a new variable for this class here so i'm going to say self dot data ready and so initially the data ready is going to be false and then we're going to override a method called notify data okay and that's going to accept a data and status and we want to check on the status of our data so initially this data is considered delayed data and then eventually it'll it'll become live data when it's when it's current and so let's print out just to show you what i mean and this cover this is covered in that tutorial that i mentioned but i'm going to log the data status just like that so i'm going to print data status and just a little arrow to mark this and i'm going to do data dot underscore get status name status okay and so we're going to get the status of the data and say and we're just going gonna just log that right so watch watch what happens when we run this what it looks like okay so you see at the very top there when i ran it and let me scroll back you see how i have tons it gave me a lot of data okay so when i run it the first time it says data status equals delayed right and so when it's status delayed it's this this old data right so it just shows me some of the previous history and then eventually it gets to the current minute so 47 minutes and then it becomes data status live so it's notifying me on the status of the data and once the data is live right these are the bars that i'm interested in processing okay so i have self data ready equals false and so i'm going to wait till the data status is live and then i'll say we're ready to start implementing our strategy okay so i'm going to say if status so i'm going to say if my status equals data.live like that i'm going to say self.data ready equals true okay and then my next function here what i'll say is if not self dot data ready then i'm just going to return so i'm not going to process any of these bars until i know i have this live data right so i'm saying don't don't issue buys and sells based on this stuff wait wait till there's new bars coming in okay so i'm going to return and then i'll just get out of this function until the data is ready and then i'm going to move on and start processing this data and so now that i have this live data in you see i have the 4815 48 the 48th minute 48 15 right i just need to implement some type of strategy on how i want to trade this data so react to this new data as it comes in so i'll get next this is the zeroth close this is the negative first that's the negative second right and then it keeps uh incrementing forward right and so what do i want to do so i'll implement an actual strategy in the next video i just wanted to show the data feed coming in and also just execute a buy here and so what i can do here is say if i'm not in a position so if not self.position i'm going to do self.buy and then else if self.position self.cell okay so if i don't have position i'm just going to buy one of the instrument and then otherwise if i do have position i'm just going to sell the position sell the position so let's alternate back and forth between buying and selling so i'm going to stop that and let's see if we can get backtrader to execute a cell in interactive brokers right so i have a data feed coming in and i made one mistake equals live i want equals equals so if status equals equals okay all right so i'm going to run that one more time and i'm going to say if i'm not in the position i'm going to buy and if i'm in the position i'm going to sell so i should be alternating back and forth between buying and selling so i have some live data and i should buy and nothing happened yet and why is that the reason nothing happened yet is i didn't actually hook up uh the broker to this so right now this is still running uh in a simulated broker mode so i'm getting the live data feed but i need to specify that i want my orders to go through the interactive brokers broker right and so on cerebro i set the data feed i also added a strategy but i didn't set a broker so before i run the strategy i can do a cerebro dot broker equals store so my store is the interactive broker store so i want to get and set interactive brokers as the broker to execute trades so i'm going to do a get broker like that okay so cerebral.broker equals store.getbroker so i'm going to run this one more time and let's see if we can get it running against interactive brokers and place an order so when that buy function runs i want to place an order in interactive brokers okay so the data is live now let's wait look at that did you see how he said order filled so you see how it's saying order filled right now so you see how it executed a buy there right for japanese yen and then it switched over to doing a cell okay and there's a toaster on my second monitor here that you didn't see so there's a little notification so i said order filled again you see a buy came in and let me let me scroll this over a little bit so you can see the data flowing in and you'll see how that gets notified right so there's there's data you should see the next 15 second bar and you see how that sell comes in right there and so you see this is buying and selling repeatedly new data is coming in and then i'm executing a buy using backtrader and then it's executing these buys and sells against uh interactive brokers and then it has this nice voice very satisfying it lets me know that buys or buys and sells are occurring and then there's also a little notification and on the bottom right it's not on the screen right now but you can't see it but there's there's a little notification on my screen happening as well letting me know these buys and sells are happening right and so yeah that seems to be working well and then also i can i can change my by size for instance by sending a size equals two right and so i can run this again and then you should be able to see it you see how it bought quantity two right there and it shows me the filler price right and yeah there you go so we have a back trader running right we connected to the interactive brokers uh store and we are connecting to tws running on port 7497 we have a second data coming in for japanese yen usd japanese yen here uh we have the 15 second time frame so we're resampling our data we're setting our broker to be the interactive brokers store we're adding a strategy here which doesn't really do much yet we're printing out each row of data as it comes so we're printing out this open high low and close and we're alternating between buying and selling depending on whether we're in a position or out of the position and so that's all i wanted to show for this video i know it's been kind of long already but we know how to set this up have real time data coming in and buying and selling and so in the next video tomorrow morning when it's friday and the market's open and i have free time i am going to set this up to trade stocks and so we'll expand on this next function a little more maybe check on the status of our orders and do a little bit more with this so uh stay tuned for the next video i will be uploading another one in about 24 to 48 hours after you see this and it'll be working with stock data so thanks a lot for watching check out the interactive brokers link below and stay tuned for the next video thanks
Info
Channel: Part Time Larry
Views: 9,377
Rating: 4.9721255 out of 5
Keywords: interactive brokers api, backtrader, live trading, backtest, algotrading, forex, algorithmic trading, automated trading, python
Id: ZOohpUDdqKQ
Channel Id: undefined
Length: 29min 35sec (1775 seconds)
Published: Thu Jul 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.