Interactive Brokers TWS API + TradingView Charts Python Tutorial (Updated)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome back in this video I'm going to walk you through the creation of a trading application using the interactive brokers twws API so the twws API is also known as the trader workstation API and this is the official uh API supported by interactive brokers not a rapper or thirdparty Library um they document it and it's open source on the website and What it lets you do is write uh computer programs that interact with Trader workstation so you can interactively program your uh brokerage account uh with interactive brokers and so you can do this in a variety of programming languages including uh python Java C++ C and Visual Basic um on this channel I typically discuss uh python even though I know Java and C++ and am proficient in a variety of languages I find that Python's very flexible um and it's very most importantly very easy to teach and for people to pick up and there's a lot of beginner programmers that watch this channel also I want to use what is easiest if there's high demand for another language like Java or C++ let me know and I can probably make that happen so what are we going to build so um there's all these Concepts involved like creating a client um checking your account uh callback functions uh building uh building order objects contract objects creating a client connecting soet programming many different concepts that uh we're going to discuss in this tutorial that I have outlined outlined on the website at hackingthe markets.com and you can follow along I have all of the source code uh on here right and you can see this is many many many pages I took a long time uh writing this and so uh yeah so that's all available for free on the website uh however I do have a link to interactive brokers if you decide to follow along and want to sign up it does support uh the channel so what are we building let's go ahead and run this so I don't want to take too long to start the coding bit but what I like to do is show you the end result like what we're trying to build and then I'm going to go back and remove delete everything start from scratch uninstall everything and show you how to build it up uh from scratch and provide you all the code so I'm going to run this final project you can see I'm running a Python program here you see it's fetching some data and it's going to launch a little application here so what we are building is this desktop application here so I've made a lot of web applications on this channel but this is a desktop one so you can see I can expand handed out you see a trading view uh lightweight chart uh right here and that's provided by this Library called a trading view lightweight charts in Python so there's no JavaScript or HTML involved in this app uh this guy Lewis NW did a great job of creating an open source Library I've created a video on this before however this one uh he's made a lot of updates since then because I made that video in July of last year since then you can see there's been many many releases of this so it's a lot more powerful so I want to go over some of the new functionalities so I can update that video but also provide an update to my Interactive Broker uh tutorial to use the official API uh what are the features of this what are we building so I have it outlined uh here so if you look here the features we're building are um so I'm going to walk you through how to use all the different functionality in the API and in using that API I don't want it just be like oh here's code I want to make it actually produce something that's Visual and something that you can actually use I find that to be a little bit more motivating as a project rather than just talking about the concepts so to build up something like this uh let me talk about the different uh features and then we'll get into coding this so uh number one you'll notice there's data on here so where does this data come from so what I'm going to do is first uh we're going to walk through how to connect to Trader workstation so we need to create a client so the first step is going to be uh to create a client so I'm just giving an overview of what we're building uh so the part one we're going to build a client so this a python client it's going to start a new thread and connect it's going to make a socket connection to a specific port and so we're going to do Network programming in this tutorial rather than like a rest API like we've done with alpaca and other uh Services now there's some complexity here so I'm going to try to talk about uh some of the various uh differences involved so we're going to connect to our local Trader workstation on a specific Port um and then once we're connected we'll be able to send commands to interactive brokers and get responses back so um Step One creating a client step two we need some data so to once we can connect we have this persistent connection to interactive brokers so we can send a request for historical data you see I'm sending a request to uh for a contract so a contract is just a tradable instrument and so I'm doing uh this for stock right now so this is going to be for tsmc so the Taiwan semiconductor uh we're going to get some 5minute data and then once we get that data back interactor broke is going to call me back and I'll be able to render that data um on this chart so we're going to talk about uh sending commands and then implementing callbacks and then handling the response and so what's kind of cool about a network program like this vers is a rest API um let's say we want to place a limit order uh if I did this with a rest API and sent like a web request that doesn't really maintain any state it's stateless so I send a request out there saying oh uh buy this stock at 150 or whatever um how do I know when that's filled right once I send the request it's gone I have to turn around and pull the server and say uh tell me about this order ID is it done yet is it done yet is it done yet and it may never even be finished right whereas with interactive brokers here I have a persistent uh connection over this socket and I can uh while that connection's open I just send the command and then it just notifies me back in real time uh when that order is filled um so I'm going to show you how to uh I'm going to walk you through the process of sending uh commands and requests and processing the callbacks uh in the client uh next like I said we're going to be rendering a chart so we'll create a a chart object and display uh this chart and then a bunch of these new widgets on the screen so what can we do um well first of all we can change time frames so this is called the switcher widget so it's kind of like we're building a desktop application here but the low-level details are handled by the lightweight charts uh library that Lewis has made um so what I can do is Click 15 here and what that'll do you'll see how it's interacting with the Python program so if I click 15 it's going to make another call for uh historical data you see a pandas data frame down there so I'm going to click 1 hour you'll see it makes another call calls it back and then we rerender that data so I want to walk through how to send a command get stuff back process it present it in a new way so that's called the uh switcher right there so the switcher Widget the next thing I'm going to show you how to do is um I can do command F and I can switch to Microsoft for instance um so we have these different uh shortcut keys so I can switch between stocks and then shows me a legend or a watermark I mean on the chart right here uh the other thing we have is a screenshot ability um and drawing tools and so let's say I have have Taiwan semiconductor pulled up right here and I am on a different time frame here so I'm going to switch to 15 minutes you'll notice there's a horizontal line on here and so since we get this data back in a pandis data frame uh we can perform calculations with pandis TA or just regular pandis functions and I'm just going to get the highest high here and I can programmatically draw a line here so you can see I have a hor horizontal line and not only that uh he has provided a way to imp ment call back functions whenever certain events happen on this chart and so I can drag this line around so let's say um I want to uh zoom out a little bit more here and let's just say now I'm interested in um this particular high so I can drag this down and if you look at my code at the bottom there you'll see it says horizontal line moved so there's a call back function whenever I move this line and then I what that means is I can track the state of this application and maybe track the state of the price and say uh keep track of the price data and when the price crosses above this line wherever I've set it I can say place an order or take some other type of action right so that's uh horizontal lines and a callbacks um the other thing is now we have these drawing tools available and so I can draw a trend line for instance right here and so if I click right here I can draw this trend line and I can move this down right here for instance right uh there's also um you can just draw arbitrary horizontal lines here at will and there's also these Ray lines which means you start it at a point and it just projects out uh into the future so that happens if you double click there what we also have is the ability to take screenshots of the state of the chart and so there's a we Implement a little button in the top bar I click screenshot and you're going to notice in my code in this directory you'll see it'll save another screenshot when I click it there you go and it captures that frame and we have this drawing exactly like we configured it and that's nice for some people that like to keep a record of their charts or maybe you keep a little journal or publish these things somewhere uh you can record all this information and how it looked at the time the other feature we're going to implement is hotkeys so uh there's now a functionality that lets you specify hotkeys now this is something I typically do but a lot of people have emailed me saying oh I use hotkeys for orders like they're day trader types that watch charts all day um so what I could do here since I'm connected to my brokerage account and I can Implement hotkeys here so what I'm going to do here is I'm going to hit shift and O right now and you'll see it's a logging that an order was submitted and you see in the actual workstation uh this is actually submitting buy orders now the market just closed so these are not getting filled right now but they would be if it was open I can also hit shift p here and I can programmatically execute a sell order so you see now each time I click that I hit that uh key uh it executes a sell order now how does this look in the code well um you'll see I have shift plus o and actually creates an Interactive Broker order object and programmatically submits buys and sells right here um the other thing uh we can do is on the right side you see we have uh some symbols listed here on the right and you'll notice uh we have this uh control of the layout and so what I've done here is um fired up a chart and you'll see on my chart object there are uh there's a width and you can have uh an inner width here and so you can see this uh chart here the chart uh side takes up 60% of the screen and then I have this table widget on the right taking up 40% of the screen and then what I'm doing is implementing an Interactive Broker Market scanner call and so in the documentation uh you can call different uh you can configure different scanner subscriptions and let's say um I want to scan the market for uh yeah top percentage gainers so you want to see which stocks went up up by the most uh on the current day you can scan for all of those um you can also look at Option activity for instance most active right and then so what we do is implement the scanner and so when this uh fires up scans the market for these candidates and then we're going to render all those rows in this table and then I can click on any one of these for instance right and and get the data and have that be the focus so um if you're only interested in trading tring uh certain types of stocks that meet certain conditions so I've seen some papers where or strategies where um someone you know they only want to trade stocks that are over 1 billion market cap over $5 a share over $10 a share uh with this much volume um you can Implement these scanners and then you know have this list show up dynamically as soon as you fire up this application easily switch between watch them draw your lines take screenshots whatever you want to do and programmatically uh submit orders um and then finally what do I want to show yeah so I showed the ability to programmatically uh have lines on the chart and also you can see there's a little moving average right there as well so we can perform any calculations on these pandas data frames and then render these on the chart you can also see the volume data at the bottom there all right let's go ahead and get started here so um I'm going by the article here and I've linked to many of the uh tools I'm using first of all you need uh python obviously uh I'm kind of going with the assumption that you already know how to do some basic Python Programming otherwise it's going to be a very difficult uh tutorial for you uh secondly I use Visual Studio code as my IDE so I would just download this for your operating system maybe you already use this maybe you have something else but I'm using VSS code for this which is just a free uh code editor which you can download and install uh so I have that installed and it's running here so what I did is just create an empty folder here on my uh desktop and then I can open that right there and then you see I have nothing in here and so I can create a file called uh app.py for instance just to get something started so my visual studio code is set up to use my uh python interpreter and so if you installed python um I also add a couple of VSS code extensions and so here on the left is extensions and you'll see I have a number of extensions installed and so I install the python extension which gives you Python language support and some of the auto completion and debugging type of features and I also install this uh py Lance uh plugin right here so py Lance and so what's nice if you have those installed you can easily uh get some hints and navigation in your code so I'll bring up the finished application and when I look at this you'll see you know if I'm importing a package that I'm unfamiliar with like the interactive broker's uh contract object here uh if I hold command and click on this you'll see I can dive into the python source code uh for that and study the original source code see what the uh function signatures look like and get some details um likewise if I'm hovering over like um uh if I'm extending a class like Epper or eclient and I hover over this right here historical data you can see that I'm actually overwriting a method in the parent class and you you can see it kind of shows me the the different parameters it it expects otherwise you know who can memorize all of this stuff it's very complicated library and so these hints are very important so that's why I recommend uh installing these extensions even though technically yeah you know it's optional uh the next thing we need is the uh twws API itself so this is the official uh API library that we're going to be using and so when I click that you'll notice it brings me to a little uh uh license agreement here and I just say uh I agree and you download the appropriate version for your operating system and so if you download it for when I'm using Mac OSX so I download this one over here uh but for Windows it looks like they actually have an installer and so uh yeah you'll need to install that for Windows um I'm more familiar with the Mac uh setup process and so I'm going to click this right here and you'll see it actually downloads uh a zip file right there and so I'm going to show this in my uh finder and I'm going to copy so behind the scenes I'm copying this over to my uh folder where my project is and so let's go to my uh empty folder um that we're working on so what I'm going to do is I'm going back to my uh folder here where I have my trading application um and I'm going to go inside of my finder and just paste that zip file so I'm just going to work with it in the same directory and then just import it from there so I'm pasting it I mean I have finder in another window so you see I paste that in there and so now that zip file is is right there um so if I go to my terminal here and list that directory you'll see I that tiws API zip and I can just type unzip and unzip that right and you'll see this unzip that folder and you'll see the directory IB jts right there and so that's the interactive uh broker uh API right there all those libraries okay next we need to also install the trader workstation which is right here uh you can Google it or you can click the link from there um I'm not using the IB KR desktop so they have a nice new user interface for this but it doesn't yet support uh API functionality so uh we can't use this yet but it is a nice uh client uh I'm using the trader workstation here not the desktop and so you can download this for your operating system uh and install it so there's the twws software um so you can download the twws latest for your operating system and install it and I am going to do that now and I have the Apple silicon so I'm on an M uh 2 processor here on a MacBook Pro and I'm going to install it and so I'm installing Trader workstation like so so I'm trying to do this a step by step uh so you see it's it's going to uh walk me through it and I have two monitors here so I keep having to drag this over so I'm installing that that right there and so I'm going to run the trader workstation so you'll notice when this comes up we have a couple different modes here so paper trading mode and live trading mode and this is assuming that you have an interactive broker's account there's a link below if you want to sign up for one so there's paper trading which lets you uh run all this stuff and trade practice uh with no RIS VI so I recommend doing that since it's a tutorial and uh I don't want anyone to risk their hard earn Capital while they're learning this stuff should someday in the future that you decide oh I'm comfortable risking money um that's up to you there is a live trading mode as well so what we'll do is start in live uh paper trading mode and then also I will execute a few on my own account in live trading mode just to show how we can easily swap between uh different ports uh to change uh which which uh mode we're using so I'm going to log into mine I'm not giving you my password and all that so I'm going to do that off the camera so now I'm logged in here into my paper trading account and the first thing I want to do is allow access to my paper trading account so even though no money is at risk here uh it's still secure by default saying oh I don't want to let external programs connect to Trader workstation so what I can do is I'm going to go to my file menu Global configuration I can go to API right here and then settings and then I can say enable ActiveX and socket client so we're building a client that's going to connect to Trader workstation over a socket you'll see socket Port here so this is Port 7497 that I'm using by default and so the port I'm using for paper trading mode is 7497 and in live trading mode we're going to use 7496 so we need to log into our live trading account to uh Set uh the port on that side as well um there's also this create AP API message log file to add some logging for different uh API requests so I hit okay and then let's see if we can actually connect to it so to do that we have the interactive brokers API source code right here um and we have an empty app.py so we don't really have a lot going on so let's go ahead and change that so I'm going to scroll down and set up my coding environment so we already installed vs code we set up a project folder and let's talk about this virtual environment here so I'm using a Python 3 everyone should be on Python 3 at this point I used to talk about python 2 vers 3 but python 2 is like decade old at this point so you should I I don't think anyone's on python 2 anymore um so you should have a Python 3 command so I I have the Python 3 command some people have it where you just type Python and so I'm in the terminal um on nvs code so I've opened a terminal and so what I'm going to do first is create a virtual environment which is like an isolated environment for this project in particular because I make many different types of projects on this channel and personally and you know many of them have different versions of those packages and I don't want those to conflict so you create a little isolated environment of for your projects and so I'm going to type Python 3-m venv venv and that's going to create a virtual environment in a directory called a v uh VV so you'll see it pops up a little directory called VMV and my VSS code is smart enough to go oh you created a new environment do you want to select it for the workspace folder and I'm going click yes and so now it knows about this environment um I can activate a virtual environment by typing do VNV uh Bin activate so if you look in here you'll see a bin activate and so you if you see VMV in front of here it means uh when you're running this you're using the python version uh that's in the virtual environment so this is like an isolated instance of python and so you see when it runs now you'll see it's actually running python from that directory so VNV bin uh so there I could be running python 3.11 in this directory and then some other project I have can be running python 3.9 for instance and have a different set of packages and so not all of my uh projects are going to use the Interactive Broker API so when I install this package I want to I don't want to necessarily install it globally I don't want to install lightweight charts globally I want to uh isolate my projects right uh so I have a virtual environment here um app.py yeah so I've run app.py with my play button and I verified it's using my virtual environment python uh right there uh I want to install some dependencies and so I use the PIP command for that so I'm going to pip install uh lightweight charts and so that is the uh charting package we mentioned you see it's installing a whole bunch of stuff right there um I also need to install uh the Interactive Broker API so I downloaded it uh but you'll notice I have this uh uh setup. Pi that I need to run and so that's in my uh Interactive Broker source code so I download the source code for inter active Brokers API um so I'm going to go into that directory into the source directory and I'm using the python client and here if I list inside that directory you see there's a setup Pi I type python setup Pi install and you see it installs the Interactive Broker API just like that and then how do we know this works well I import let's try importing some uh classes and packages so I'm going to import a client and a wrapper um from the Interactive Broker package so I'm going to uh copy this right here and let's try to import it and see if it knows about it so I run that and you see there was no error so it means it clearly uh knows about about it uh it looks like my pyant says it isn't quite sure but what I can do is uh hover over it and I click this quick fix and I'll say add that to the path and now it seems to know about it and so now if I hovered over it you see how it picks up uh the documentation here and then it also picks up those classes and so now I can actually probably navigate so if I hit command click on the client you can see I can actually dive into the source code and like navigate into all these different uh packages and so that's how I know that this is configured uh properly now the next thing we want to do is let's look at this interactive brokers API source code a bit so um there's documentation if you Google around there's a couple different sources of documentation there's the old documentation which is here you can click whatever language so if I click p Pyon and I want to learn about order objects for instance you can click here and learn a bit um so this is the old documentation there's a newer one on ibkr campus.com and so I can look at it here this one looks a little bit more modern and and cleaned up a bit but I find that sometimes I need to dive into the source code to see the exact uh details so if I go in here and click uh command click on client or just browse in the source here you'll see there's a directory for each client so if I want to see the C++ client source code for instance I could click on that and actually oh here's what the C++ code looks like um but for the python client I can I can go in here um let's see let's find the client and so look here's uh the eclient here and you can see all of the different uh options available here so if I instantiate a new client uh you can see all the different member variables the different uh methods that are available um and so forth so if I search for like the word connect for instance we're going to use the connect function and if I don't know about the connect connect function I can look directly in the source code of this and see oh the connect function expects a host name a port number and a client ID my host name is my Local Host um port number we talked about that in our configuration and the client ID we pick that ourselves and I'll talk about that uh in a moment so just so you know you can browse the source code uh for uh orders or any of these other uh libraries and classes that we're going to use and see how they all work and sometimes looking at the documentation and the dock strings inside of there uh give you you know a little more detail on on what exactly is going on and I find that uh very helpful so um like for instance if you look at the order object you know there's so many options here it's like ridiculous so you see the like oh if you want to know what the trailing stop parameter are you can see uh those are in there and there's this it has a lot of power but with that you know not every single thing is perfectly documented um so that's that um and then the other thing so that's the client code under Source here right so there's the client code uh under Source but then there's also samples and so there's actually some samples uh in that little bundle we downloaded and so if you want to see C++ uh code samples or python code samples they in here and there's a lot of helpful information in here we're going to write this code but also it's helpful to like look at Contract samples for instance and you know I don't trade every type of contract um if you want to trade a European stocks there's like BMW and that's how you'd actually construct uh those objects and then you know if you're trading options and you want to know how do I create uh a put option at a strike price of 105 uh for this particular symbol how to do that bonds m UT funds there's so many different types of contracts that I will not go over on this channel futures um and so forth some some people are trading cocoa now and orange juice and stuff like that um that that those things are going crazy right now so uh oh yeah different option combinations etc etc uh so lots of good examples in there and also what's useful is the order samples so there's many many different uh order types so it's not just your standard uh market and limit order you can see Market order very simple right there but uh you can also uh see that there's many different uh order types uh available so if I do like uh stop limit for instance uh Market Market on close so if I want to place an order right when the Market opens or right when the market closes uh those order types are all available if I want to place a trailing stop order uh I can specify that as well as like a trailing percent um so on and so forth so that's some code examples and the actual client code itself and getting familiar with that source code uh could be very useful all right so enough about the Interactive Broker uh sample code uh let's talk about the code that actually goes into creating uh this application that we're building uh so the way it's going to work is we are going to build this uh program up so what I did um so I I wrote this uh program all the way at the beginning but what I did is divide it up into chunks that way you know we could talk about a piece of it at a time add another function add another section of functionality and so forth and so each of these Snippets you can you can paste uh copy paste and we'll talk about it and then you can see what I've added at each step and what each piece does because if it's just one big wall of code then you know it's just too much all at once so uh what we want to do is start up with one concept at a time uh so here the first first thing I have here is creating an interactive broker's client so let's uh take this code right here and put it in our app.py so we're already importing the appropriate libraries here uh so we don't uh need those uh that extra code uh so we import eclient and E wrapper so uh what is eclient and E wrapper well eclient lets us uh send a request or message to interactive brokers um and then the server gives us response and that is sent back to us via Epper now not the biggest fan of these names but uh you'll get used to it um next thing we do is import thread because we're going to run uh this connection in its own uh thread because we're going to also have this desktop guey application that is running separately the next thing we do is create a new interactive broker's client so here I have it called the IB client class I could have call it the PTL client or whatever you want to call your client or you could call it a trading app I've seen uh people call it a variety of things so you might call it app or something like that right so you define your class here and so this is just hopefully you're familiar somewhat with objectoriented uh programming but here this is a class so this is just a collection of uh functions and member variables um and a class can inherit from uh parent classes and so what this does is extend and uh two different classes so it inherits from eclient uh which is our client class and then Epper which is our uh raer class so one of them has the function so if I dive into eclient and I have like request historical data this has all the different functions for sending messages to Interactive Broker so request historical data uh place order so forth so we're sending command outward from our client outward to the server now this is just sending information that way but we need to receive information back from the server and so what happens is um Interactive Broker uh e wrapper here uh implements these uh callback functions and so if I dive into Epper here you'll see this has a historical data function as well um and what that does is um when uh I request historical data for instance um when it's ready and comes back it calls a fun function called uh historical data there's also a function called uh error for instance and so if the server returns an error to us it'll call a function called error with an error code an error message and so forth so what we can do here is when we extend these classes we can Implement either methods that are uh in Epper so we'll implement the Callback functions that are part of Epper but we can also uh call any uh functions or methods that are part of the client class and so that includes the function called a connect right here and so what you'll see here is when I instantiate a new client so down here I create my own client object I create an instance of my IB client here and then I give it a few parameters so the host name is uh or the host is 127.0.0.1 that's just Local Host because uh this uh Interactive Broker Trader workstation instance is running on my laptop right now but technically I might have an interactive broker's Gateway and have it deployed onto Some Cloud Server which um I showed in some other videos so I'm connecting locally and then here I'm using Port 7497 because I'm in paper trading mode and uh in my configuration in uh the workstation here under API settings I have the socket port at 7497 and so what I'm saying is create this socket connection uh to my local host to uh Trader workstation and communicate uh on Port 7497 and then the other parameter is a client ID so technically uh you might want to have multiple clients that are identified by a unique ID so here I just have one client and I can just put the number one here um but technically you could give it client number seven for instance um but you can have like multiple clients with the same ID so when I instantiate this when you you instantiate any class the Constructor is the init method so when you create a new client it automatically calls this function we initialize the a client uh parent class uh right here um and then we call connect so eclient has a function called connect and this handles all the details of creating a connection Interactive Broker so you don't need to worry about all this this is in the uh IB API source code there's connect disconnect um so forth now so connect is an example of a function that lives on eclient because we're sending a message to connect but the response uh could be um from the Epper so the Epper sends us uh messages back by a calling methods that it implements so there's the error function and so what we do if we want to listen for uh any errors that occur we can make a function called error ourselves and then we can specify what happens when the wrapper class uh calls this error function and so what I'm saying here is I want to print the message out and so um let me comment out this code for a second and what I'm going to do is just print um message and I'm going to print uh code okay and so we're going to see if everything's proper uh when I run this we should create a new instance of the client which will establish a connection it'll run in it own thread um and then we should get a call back uh with any messages that we receive when we connect and so if be sure your Trader workstation is running and you're logged in because that's how it's authenticated and I click play and you see I receive a number of uh messages back and so it says okay okay okay so that all looks good I'm printing a code and I'm uh printing uh a message here so Market uh data connection and it it lists all that out because I'm just printing it out like that um so that's all good and and those messages even though this is called the error function all those messages about my connection are coming back um now you'll notice even though this is called the error function this is just all the output coming back from the server um and so what I have here is for these different codes for connecting I just print the message so I'm going to say if the code is like 2104 and some of these there's a list of all these uh codes on Interactive Broker documentation and so I'm just printing the message as is but there are sometimes where you want to you you get a genuine error and you want to print the message and so you can say oh if it's one of these codes just print print the the the message from the server um otherwise you know you can handle specific messages and type error in front of them and format them in a particular way so that's step one making you sure you can uh actually connect and in the tutorial here I talk talk a little bit more uh about the details of oh what are the concepts we cover here creating a class uh inheritance so we inherit from Epper and eclient uh here uh the Constructor objects and so forth um and then the Callback which is this error function right here um so we get notified uh from uh interactive brokers when uh a command is processed um and then it it calls a call back and and let me uh I think this request historical data when we add that this will make more sense uh so the next thing we want to do is now that we can connect let's send a command uh to uh interactive brokers and so what are we going to add here so you'll see right here in order to get some data um we already have a client here and we're connected so now what we're going to do is take this little uh bit of code here under the client and let's put this uh down low and I'm going to put it uh right here and I am going to put right here um and you'll notice I have a time. sleep here and so what I need to do is import time so I have that and so I need to import anything uh that uh we access so I'm accessing I'm creating a new contract object so I need to make sure I import a contract right there so I'm going to add a contract right here and let me see if this runs so I run that you see uh nothing is happening yet so what we're doing is uh creating a new contract and requesting historical data but where's it at okay so what does a contract object look like well um I imported contract I create a new instance of contract uh so a contract needs some type of symbol um a security type so uh in this case uh this is of type stock but if it was options I believe it's opt uh Futures fut and so forth so there's a type of instrument uh an exchange you can just use Smart here and it will figure it out for you but if you have a specific exchange for the instrument you're trading uh you would fill that in uh currency I'm using US dollar um and so what I'm doing is calling request historical data if I hover over that you'll see I can see all of the different uh details of this function signature and what you need to pass in and so you need to pass a contract object um a request ID um a an end time duration string bar size and so forth so you can request on different time uh time frames etc etc uh and so I'm giving it a request ID um I'm passing it my contract object that I just instantiated um the next parameter is the end time I'm just going to say up till the present so I can just leave that blank I'm going to get 30 days of data on the 5 minute uh time frame and then you can see there's five more parameters here so there's a whole lot of parameters to these and so that's why I said it was helpful to jump into this and you can see some details of what uh each of those parameters are so you'll see like format date uh use rth for instance what is that so let's look down here uh or what to show so you can um show the trades the bid and ask uh use rth return all data available during the quest or only uh regular trading hours so rth is regular trading hours you might want to return pre-market data for instance um the date format uh you have those options as well uh so we request uh historical data um but we need a way to know to get informed when that uh data has been requested so that's where we get into implementing these e wrapper methods and so if I jump into Epper you'll see there is a function called historical data and there's one called historical data end and so these functions you can say what you want to happen when the data comes back so this is called every single time a bar of data uh comes back so we get a bar data object that has the date open high low close and volume um and then at the end after we received all of the bars it has another callback function to say oh I've gotten all the data that you asked for so the full 30 days um and so what we can do is add those functions to our client here and when we add those interactive brokers will call them all right and so if I add these two functions an implementation of them um and put them right here inside of my class what I'm saying is print the bar each bar as it comes back and then print a message when you've received all the data so if I run that you'll see look at that a bunch of bar data starts coming in I prints them bar by bar and then at the very end you see so there's one call for each bar and then at the very end it calls historical data n saying end of data so I got everything back so that's how we get our raw data now how do we start uh creating a chart to visualize that data so we're going to add on and create a simple empty canvas for the chart to start and then I'm also going to start creating some constants here you notice uh previously I coded um some values here like Taiwan semiconductor and this host name but you might want to change from a paper trading to live trading mode so you'll want some constants here um so we'll gradually refactor this code as we go and so what I'll do here first of all let's create a variable for the initial symbol and the default uh host and the default client ID and let's just put these at the top uh before our class uh let's just just create some default values there and then so that'll be our default symbol when we start but then we'll let them change it afterwards okay and so now when I connect here instead of hardcoding uh the Local Host I can do um default host or you can just call it host um and then if the host changes you can just change it in one place at the top um that's our port number and then we also have a default client ID now the port is interesting cuz sometimes we want 7496 for live trading and sometimes we want 7497 so what I'm going to do is create a little flag so if I want to change from paper trading to live trading then I can set live trading to true so right now I have it false and so what I'm going to say um is by default I'm going to set my trading port to my paper trading Port right here 7497 but if live trading is true then I'll set my tra trading Port equal to the live trading Port which is 7496 and then down here um I can just set this to trading port and then switch back and forth by just changing uh that one flag all right uh the next thing we need is we need to import the actual uh chart library that we're going to use and so we're using lightweight charts and so I am going to import that right here and then at the bottom here we're going to actually uh create an instance of a chart and so down here we're going to create an instance of chart and so here I'm going to go down um and right after uh my connection I'm going to say oh create me a new chart and make it 1,000 pixels wide I'm going to show a toolbox and make the width of the chart uh 60% uh of the screen or of the 1,000 pixels so that's my uh chart object and I'm going to have this chart show the other thing you need to do we need this if name equals main when we uh run this program so I will add that or we will get an error um so we're creating a client connection we're creating a chart um and then we're going to uh show the chart and so I'm going to do uh chart. show right at the end there okay so I'll run this guy and let's see if we can a chart box to come up so I connect I request some data and I show a chart and now it's completely empty and so what do we want to do with the chart we want to take the data the historical data that we retrieve and render it on this chart so to do that uh we need to figure out how to do that uh in lightweight charts so to refactor a little bit more what we're going to do is take this since we're going to be switching around time frames and symbols we don't want to just uh uh do a default contract object and request it here we want to put this inside of a function and so what I want to do is have a uh function called get bar data and then have this code live inside of there that way you can send a different symbol in a different time frame and all of that logic will be encapsulated in this one function called get bar data and we'll just call it whenever the uh application starts so let's go ahead and do that now so I'm going to take uh this code here so I'll make a function called get bar data and then this accepts a symbol in a time frame just like that and then what we do is take this uh contract and historical data here and this get bar data function will be responsible uh for f for calling request historical uh data uh what else did I add I added a watermark and so this chart uh library has the option for a watermark and just like that and also I need to actually call the get bar data function so uh we Define get bar data which instantiates a new contract object for whatever symbol is passed to the function so I'm going to swap out a TSM and I'm going to use whatever symbol comes in as an input and I'm going to swap out this 5 minutes and have it use whatever uh time frame uh is passed in and then down here I'm going to go ahead and call get bar data with the initial symbol which is TSM and the initial time frame which is 5 minutes and so I could even have initial a time frame right there if I wanted to and Define that up here so initial time frame equals 5 minuts okay and then I run this and this should still work let's make sure that still works works you see I have my Watermark now that says TSM I have my drawing tools but still no rendering yet so what we did first is just make sure our chart package Works um and how did I know how how that works well it has examples on their page so we instantiate a new chart object this on the GitHub page for that uh I know how to show a chart and it looks like I need to set a data frame of price data in this format in order to actually display a chart data so uh let's take the data that comes back from interactive brokers and convert that to a pandas data frame and then and then do just like it says in the documentation which is set the data equal to that data frame and so if you look here um what I have um the first uh call back I have was historical data that just prints a bar and then uh historical data n which just prints when it's done but what we want to do is collect all those bars into a a data frame format and so here the next piece here I had to code was uh rendering that historical chart so what I do here inside historical data we expand that out not only print it let's go ahead and uh take that bar data so you see all those bars this is actually an object so it has a date which is a Unix timestamp we have an open high low and close of price we got volume uh and so forth and so what I'm going to do is collect all of that data uh into a iary and so each bar will have its own dictionary with date open high low close and volume and I'm going to format it this way uh just like that and then once it's done I'm going to put it in a queue which is I'm going to talk about shortly so I'm going to take this code here and go to my historical data function and just like that you'll see I it highlights a couple problems I don't have date time imported so I'm going to import a date time and you'll also notice uh I'm going to be using a q so here at the top I also create a new uh CU here okay and so I'm going to do this at the top now what is this uh queue that I'm creating well we need the uh desktop uh user interface to be able to interact with interactive brokers so when I uh when these uh when this thread is running with Interactive Broker ERS uh that connection running what I need to do is somehow get that data back to my chart for rendering and so what I do is when that data comes back what I'm going to do is just drop it in this Quee and then I'm going to be uh have my chart look at that queue and say is there any new data there and if there is uh collect it uh and into a data frame and then set it on the chart and so I'm going to uh import a q here so you'll see here at the top uh I just import the standard uh Q package here so I'm going to import uh q and then here you see I actually put the data that comes back from historical data in the queue and then what I want to do is after I've received all the data in historical data end I want to just update the chart so I want to update the rendering of the chart and say render um all the data that's available in the queue and so in historical data end this is my call back for when I have all the data I'm calling a function called update chart now in that chart how do I Define that this is how I read information uh from that q and so what I did is create a function so uh it's getting longer now there's more uh details uh this has this IB client I want to make sure the only code in here is the code for uh receiving information connecting to interactive brokers in this client and also uh call backs that are called by interactive brokers now anything that's like specific to rendering on the chart I'm putting outside of that class so I just have an update chart uh function right here uh just like that and what I'm doing is saying oh historical data I got it back call update chart there's new data available in the que so what I'm doing in this function is um retrieving the price data from this q and so what I'm doing in a loop here is saying uh check this queue get all the information available in it all the bar data append it to a list until it's empty and then once that's done then I'm going to go ahead and create a pandas data frame out of those bars and then if I have a non-empty data frame I'm going to set the data on the chart equal to that data frame you see I have an little underline here saying uh PD is not defined that's pandas and so I need to uh import pandas as PD and I'm going to stop this and let's make sure I have pandas installed I usually have that installed but if you don't type pip install pandas and now let me run that and you'll see what happened there is not only did we print the bars we also uh consume the queue uh put all those bars into a pandas data frame in this format so data open high low close volume the format that expects from this chart and then I just set the data on that chart equal to this Panda's data frame and there you go we have a nice looking uh chart right here that zoomable and pable and we also uh by default get our uh drawing tools just like that so making some making some progress making some progress all right so the next bit we have here is on symbol and time frame switching so you see we started with an initial symbol of a Taiwan semiconductor here and in our get bar data we accept a symbol in a time frame but in our current interface we don't really provide a way to uh switch between symbols and switch between time frames because we don't have the toolbar up here yet so how do we add a toolbar with different buttons so that we can add functionality to our application here so I'm going to close this and so the next thing we're going to talk about is is this uh switcher functionality so if I go over to the uh lightweight charts Library you'll see there is a switcher here where you can add a text box here for the symbol and you can add a switcher to switch between uh time frames um and so he has some labeled 1 minute 5 minute 30 minute here um this the examples in in his code are based on like a CSV file and I think he has a polygon IO example and so it's reading from a static file typically um and so what I want to do is make these parameters match what interactive broker's API expects and so you'll see I uh in the code we have shared here um in the switcher uh you see I have the time frame for five mins so there's a space and like an S at the end so that's that's one difference and so what I'm going to do here is add this little bit to the toolbar so you'll see we're adding uh two things to the top bar here we're going to add a text box with the symbol and we're going to also add a switcher which is the time frame switcher and you'll also see that I'm going to have a call back function so anytime you switch between these elements it's going to call this function on time frame selection and so if I go down here to my uh chart object so I'm going to go down here to where I instantiate the chart I'm going to add this bit so this is my chart object I'm going to add this text box with the initial Sy symbol of TSM and I'm going to add this switcher with a default and I'm going to do default equal to you can do 5 minutes or I added that initial time frame variable uh I didn't put that on the blog post but um I put it here uh and I have a function called on time frame selection that is expected and so I haven't defined this function yet so here you'll see I have defined the function and what I do is just specify what I want to do when I change a time frame so every time I click on one of these it's going to call uh on time frame selection and so let me Define that function here so I'll put this after my get bar data function I'll put this and what this does is get a reference to the chart and so if I were to change a time frames I'm going to get bar data again and so I'm going to call get bar data and instead of passing the initial symbol I'm going to get the value of the Sy symbol um that's currently set in my uh toolbar there and then I'm also going to uh pass the value of the time frame that's in the switcher so if I run this now let's see what happens let's see if we get that so you'll see um it added TSM there because that's my initial symbol so now when I click a different time frame I click 15 minutes you'll see it calls get bar data gets that data renders it call 1 hour gets new data so that's an hourly time frame bar right there so it calls a get bar data right there uh so yeah so the call back calls get bar data and then that in turn uh builds the contract for whatever symbol is currently selected request more historical data uh sends it right back sets a new Watermark and you see this is nice and dynamic now how do I switch symbols you see this doesn't do anything yet so I need to add the search bar functionality and so here we'll also have another callback function and so you see I have that on search right there so what you can do here is add an event and so we're going to have a search event and this is also part of lightweight charts uh so here I'm going to have a chart events. search and we add a search event and so what this is is just a callback function called on search so anytime a search is made it's going to call this function with a reference to the chart and the string that was searched for and so we can implement this function so after on time frame selection so we'll have an event for Anytime the time frame changes and event that calls a function Whenever there is a new search so if they search a new symbol obviously you got to get bar data again for whatever uh string they search for and then set the time frame value exactly the same as it was and then we set the symbol uh in in the top bar okay so I'm going to run this again and you'll see it load Tai 1 semiconductor again if I hit uh command F you'll see it brings up a search box and then I'm going to type Apple AAP and so when I click enter it's going to call my uh callback function on search it's going to get a reference to the Chart so that we can access the top bar and and set it to the new string and it's also going to get this search string okay and so once I get that search string I'm going to say oh take app get bar data data for app and then since it's already on the 5 minute it's just going to pass what's already there so the reason I get this reference to the Chart object is so I can access what the currently selected values are so it just keeps the time frame the same here I'm not changing that yet so I click enter you see uh I set the top bar symbol now to Apple and that's what makes it display there and then we also get in the legend and we get the new Apple data right here which looks looks good all right so we're making some good progress now what is next let's add this hot key ordering so let's actually uh place an order when a specific key is pressed and we can do other interesting things here like Contin continuously uh update These Bars request new data and when some condition is met uh in the price action we might want to uh place an order not doing it on this video I've done that in a lot of videos um so what I'm going to do is implement this hotkey functionality and so if you go here um uh you see he's added hotkeys and so you need to dive into the documentation uh which is pretty well documented here and so I've dug through this and documented how to uh implement this with an actual a brokerage account most of these examples are just driven by CSV files so if I click on events here um you see he has a a place by order but all he does is print here but what I wanted to do is actually make it place in order and so we're going to specify a a hotkey here you can specify one or more values for that hotkey uh so shift X he's saying Place cell order and then shift 1 2 or three he's saying controlling the quantity uh that he's going to buy um so I just use shift o and shift p and so in this example here um I'm implementing a hotkey um down here and so if you take these two items here um I'm specifying a two chart hotkeys so I'm going to add this as part of my chart set set up and then you see I have uh a function that's called so when you hit shift and O it's going to call a function called place order which I've not defined yet when you uh click uh shift p it's going to call place order so what we need to do is add a place order function so I got a place order right here and I'm going to talk you through uh this bit right here so this handles when a user uh clicks a hotkey combination so I'll put this on my on search right there okay and what are we going to do here um place order we're going to capture the key that they pressed so they press press shift we know and we're going to process whether that key is o or P or one two or three and so forth so when they place the order what do we need to know we need to know the symbol so that we can build our contract so we already talked about uh building a contract object and so we're going to dynamically build one based on what's currently being viewed on the chart so we uh build our contract and then we need to build an order object uh you see this is underline that means I have not imported order uh yet and so I'm going to go up here to the top and make sure I import uh order and so you see I imported contract I'm also going to uh import order right there and if I go back down you should see uh this should be fixed which is good order type is just Market here if we wanted we can make a box come up um and let you specify parameters for our limit order but I wanted this to be a really fast so I just want to place an order and have it filled I'm just having a quantity of one you could customize this uh if you would like maybe again you might want another search box that uh a text box where you place some of these settings uh before you do your hotkeys the other thing an order object needs is an order ID and so you'll see um I have this place order and I need a client order ID and in order to get a new order ID and track the order uh we need to uh call this client. request IDs here and that's just part of the API it'll assign an order ID and later when that order is filled we'll get a call back to the order filled event and we can check on particular order IDs okay uh so if the key was O I'm going to set the action on the order object equal to buy and if the key is p I'm setting the action to sell and then I just call place order which is part of the IV client I give it an order ID I give it a contract so I'm ordering uh whatever symbol is specified in this contract and then I'm specifying this order object and the reason an order is an object is because I might have another type besides Market let's say I want a market on close order Market on open order a stop limit order and so forth okay uh so I'm going to place that order and if I dive in you can see what place order looks like you can see um there's many different uh types of orders here and and you can browse through the uh source code this actually uh quite long so yeah let me go ahead and kill that and rerun this and see how it goes so I'm going to press uh shift and O and I'm going to look at my uh paper trading account here and I'm going to shift and O and let's see if it places another you see I've I've queued up a lot of these already while testing this I'm going to see if I can get another buy order to appear above this sell order right here so I'm going to do shift and O and you'll see uh object has no attribute order ID so let's see what went wrong uh so if I go to my example source code here you see that we need a call back function here so when you call request IDs what it's going to do is uh the Epper class is going to call next valid ID and it's going to get you a new order ID but you need to set that here so self. order ID so you need to process uh the call back when I request a new ID here okay so what I'm going to do is add this to my client class uh right here and I'll have an order ID and then you'll see um down here where I check the client's order ID now I actually have it because that's part of the client object and so um I'm requesting the ID and then I'm going to set it on the client when it it when it's received and then now I know I actually have it and I can place the order so I got to kill that and I'm going to start it up just like that and I'm going to do shift o now and let me show my account again shift o and let's see next valid ID is 23 you see a buy order when in right there and I'm going to do another one like that and you'll see it's getting I'm outside of Trad hours it's like 8:00 p.m. over here so the Market's long been closed but you see it's queuing up these buy orders uh but if the market was open right now those would immediately fill so you see I'm using hotkeys in our own little app here to uh Place orders in a different desktop application and these things are able to communicate and now I can do shift p and I should see a cell appear here at the top of this que here and there you go there cell goes in right right there so that is the hotkey functionality the next thing I want to do is add the uh take screenshot button here and so I have that documented right here and so you'll see down here in my chart configuration all I do is add another button you see our application's getting pretty long now um and so you see I'm adding a chart top bar button so we have a switcher we have a button and we have a uh a watermark we have a lot of different widgets that we're adding to this application so I'm going to implement this uh top bar button and I'm going to give it a label so let's go to my chart show here and at the bottom I can just put another thing on the top bar I'm going to add a type button and I'm going to give it a label of screenshot so that that's what it shows on the screen and then this also has a call back function so this says when I click this button call this function so we have on button click we're calling a function we have on certain hotkey presses call a function uh and we have uh on search we're calling a function uh we have a switcher so we have all these callback functions so we say oh our desktop application listen for these keyboard events and events from the user and then when they happen call some function trigger some custom code and so that's a take screenshot function and you see it's not defined yet so we need to Define that and so it is simple I got it right here Handler for screenshot button and again we can put that as another function here and then so what we want to do is call just chart. screenshot and that gets us a reference to an image and so what I did is I wanted to name these files and give them a timestamp and so you can control um so what what I'm doing is opening a new file and writing that image to a file and we need to decide how to name that file in Python and so what I'm doing is using an F string here and saying uh I just want to get a timestamp so that they have unique names so if I would have called it just screenshot.png uh stocks that Ma match certain criteria so let's say uh top percent gainers or stocks that have a price above $5 things like that uh so let's let's scan the market and display those in a table on the right hand side and so there is now a bit of table functionality here and so if you look there's a table and under table here you see you just need to call create table and then you can format that table and add new row programmatically and so what I'm going to do is Implement a scanner subscription in interactive brokers so I'm going to scan and then when that scan data comes back for each new stock that comes back we're just going to add a new row and we're going to just render that uh on the right side of the screen and so now if I go over uh to uh my example code here uh on Market scanners you see I'm now importing scanner subscription so I'm going to add this at the top so we're going to use the client's uh scanner subscription and then if you look in the trader workstation API there's examples of many different types of Market scanners so if I click python here you'll see like top percent gain most active and so forth I'm going to use this hot by volume uh right here and so I'm going to add that so I have that code written hot by volume and so I have a function called do scan here and so at the very beginning when the program starts up that's when we're going to do our scan you could add a button to do different types of scans if you want to I'm just going to call a function called uh do scan once so I'm going to put it uh let's put it right here do scan and then our implementation of uh do scan so here's our implementation of do scan and we'll put that right above here um next to take screenshot okay we'll do uh do scan uh you'll see when you call do scan it's going to create a new scanner subscription uh each uh type of scan has a scan code I'm having a input parameter called scan code so you see when I call do scan I'm passing in that scan code for hot by volume I could easily change that to percent uh gainers so if I go back to this function you'll see a tag value is undefined and so what we need to do is import uh this tag value uh all those tag values are uh are um explained in the uh interactive brokers API documentation there's a big uh list of all them I won't go through all of them now but so if I uh go in the interactive brokers uh source code and type hot by volume here so I'm going to search this directory uh you can probably find a list of them so um there's some sample ones uh right there hot by volume uh percent gainers so the same way we had contract samples and order samples that we discussed at the beginning there's also a lot of samples of all the different types of uh scanners right here and there's also a function you can call to uh request all the different types of of scan codes here and there's a list yeah somewhere in here where you can uh list them all out so uh I've entered a scan code uh hot by volume and then I can uh filter on volume as well and so this list of tag values that's what that that's for um and so what we do is uh request a scan pass our scanner subscription object um and then once that comes back we're going to want to display uh that scan so I'm going to say uh request uh the uh scanner data and then we want to uh display it and so I have an implementation called a display scan here and all that does is that's what's going to render that information when it comes back so display scan right here um I have this function so uh one bit of code handles the event where uh we ask for the scanner uh the scan to occur and then the next uh function here is what to do uh with the results and then we should have one more function here inside of our interactive broker's client this is the uh e wrapper function called scanner data so the same way we request historical data uh and then get that data back and put it in a queue for consumption we need uh something to receive uh the scanner data that we received back and so I have an implementation of the scanner data callback and we're going to add that to the interactive broker's uh client so if I go back all the way you see uh this is the point where you want to start breaking this up into uh separate files um but I'm just going to keep it in one file for Simplicity now uh so I implemented scanner data and you can see uh I'm just printing out the different contracts that come back U I'm collecting them into a dictionary like we did with historical data and then I'm putting all that data into a Quee and then on display scan here you'll see where uh once again I uh read the que and then add new rows for each symbol that comes back so yeah little bit of complexity there I'm not sure how well I explain that but ultimately you're just uh creating a scanner subscription object you're requesting scanner data you're implementing a call back to uh process all that scanner data uh when it comes back and I'm saying oh when I have all the scanner data back uh render it on on this desktop app here so if I run this now uh you should see there you go uh the results of the scan comes back it creates a new row for each of those on the table all right so the video is probably over an hour long I'm going to go ahead and uh wrap this up you can tell I'm moving a bit uh quicker now so to wrap it up I want to just add a couple lines on here I mentioned the uh horizontal line functionality here and so what we want to do is add a horizontal line to our chart and so what I'm going to do is go back into our update chart function uh that displays um the bar data so when we consume that bar data uh not since we have this data frame here we can go ahead and add some lines to this chart so I'm going to add a horizontal line at the high of the data frame um and then I'm also going to add just a simple moving average uh line and so to do that I can just uh calculate a rolling mean and so I'm just going to use use the 50 period moving average of the close here and so what I can do is create a new line on the chart um using functionality of lightweight charts I'm going to give it a name so sma50 and then I'm going to set that uh line equal to some series of data which is just uh the date uh and the close here and then I'm going to display those lines on the chart so horizontal line and a 50 ma line so uh yeah horizontal line here uh so where we have our chart set for our data frame so we have our data frame right here in our update chart function that we added earlier uh so we have our data frame let's go ahead and calculate the high of that data frame uh the max High there and then um what we can also do is when that horizontal line is dragged we can specify a callback function for when I I move that horizontal line up and down and that's why I have an on horizontal line move right there and I can capture a where that line was moved to and so I'm going to add a little function here for when the horizontal line is moved and so let me run this one real quick and let's see if we get that for Taiwan semiconductor uh so if I zoom out here you should see it at a horizontal line where is that high I think that high occurred back here yeah uh let me go on the hour time frame might be able to see a little easier yeah there you go there was that Spike to 158 back there and so we dra we have a horizontal line right there and then now I can drag that horizontal line down and we should see our callback function fire just like that horizontal line move to 148 uh so that's great now let's add uh to finish this off let's go ahead and add a moving average line so I'm going to add that uh SMA uh 50 line so SMA 50 um and so what I'm doing here is uh I have a list of lines so we might want to add all kinds of lines to this chart so you'll see um every time we update the chart I want to clear out our list of lines CU if they switch symbols we need to clear all the lines off the chart uh if they switch time frames we we need to recalculate all this stuff and so what I did here at the top uh you should see yeah I have a list for keeping track of any indicator lines uh at the top here and I'm just initializing that um as empty and then on my update chart um I need to be able to exess access that current lines variable and so and and I'm going to change it so I can do a global here of current lines and then in current lines there uh in our update chart uh I can say um for any lines that are currently in that list if there was anything in there before we need to clear it out and then we need to add any new lines that we want to calculate so uh I clear indicator lines here um so where's my yeah my data frame there so I'm going to clear any indicator lines that are on the chart already uh Reet current lines to empty and then I'm going to create a new line uh and append it and show it on the chart and so I am going to do this so create a line on my chart label it SMA 50 and then set that line equal to a new data frame that's just the date and my close line or and I just set that line to some series of data so a pandas data frame of just a date and a rolling mean that's calculated on my data frame and ien that new line to my current lines uh list right there and yeah let's run this and see if we wrap this up all right and so there you go we see a moving average line on there now with a Crosshair and we can see it following the chart around we should have our a horizontal line up there at the top we have all of our drawing tools here that we can add to our chart uh we have our screenshot button we have our scanner uh we can switch uh when we click a row which I didn't uh mention we have a a new row uh function here uh so I have an on row click function right here so when you click a row uh it changes the symbol in our top bar and we fetch the bar data for whatever it's click so that's one of the little point I didn't make uh yeah so click LW here you SW you see it switches the chart to LW fetches new data and puts that label uh up here and so yeah we covered a lot of different bits of functionality in this tutorial and many different uh programming Concepts and over time I I'll probably add some follow-up video to elaborate on these a bit more I originally had this written as like a seven-part series or something but then I was taking so long to write it that I had never put anything out so I'm trying not to overthink everything and just get get it out there and Sh share the source code yeah so that's it for this video um I'll be making a lot more of these so see you in the next one bye
Info
Channel: Part Time Larry
Views: 36,891
Rating: undefined out of 5
Keywords: interactive brokers api, python tutorial, algotrading, python for finance, tws api
Id: ZEtsLuXdC-g
Channel Id: undefined
Length: 81min 39sec (4899 seconds)
Published: Sun May 12 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.