Interactive Brokers Client Web Portal API with IBeam

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome back to another video today i have some more content specifically for users of interactive brokers i'm going to be talking about ibeam which is an authentication and maintenance tool used for the interactive brokers client portal web api gateway and that's quite a lot what is that and why do i care i already did a video on interactive brokers and if you'll remember in that one i covered this trader workstation api which is one way of interacting with interactive brokers right and so we wrote some programs to interact with tws or the trader workstation nr and in order to use that you'll remember uh the method of programming that it's a little bit different it's not your typical rest api where you make a web request to some server with python and get a json response back you'll remember we actually created this socket connection there's this e-client socket and e-wrapper class and then you create this a socket connection to this desktop application that's running on your local machine at all times and uh yeah so you just write python code and establish this connection and there's callback functions and threads and so forth and so that model of programming is a little unfamiliar unfamiliar to a lot of people so it can be a bit harder to use and so what i was curious about is if interactive brokers had an alternative to this like how do we program this if we don't want to write this type of program what if we just want to use a regular old web request in order to to make trades in order to get historical data in order to get option data uh from interactive brokers and when looking this up if you've looked this up before uh there's this interactive brokers client portal uh web api which again um one downside to interactive brokers is it's not super friendly to use if you read through this it mentions uh java 8 update 192 gateways compatible with this version of open jdk 11 download this zip file blah blah blah so it doesn't seem quite intuitive still and so fortunately for us there are people in the open source community such as voice i don't i don't know this person but i think they've done a great job on this and so the aim of this i-beam project is to make it a little bit easier to use this client portal web api because it's actually a bit confusing to figure out how to use it and so if you look through here it gives a number of benefits number one it has a continuous headless run of the gateway and no physical display required and what's nice about this is that we can actually deploy this to a remote server and so in the next video i'm going to show you how to put this on lynode and execute some web api requests from a server server that lives up in the cloud and it's also containerized using docker so you don't need to worry about all of these dependencies it should be very plug and play and so what i'm going to do real quick here is just show you how to get this set up and running and so this tutorial is going to assume you have a docker running already so make sure you have docker so go to docker.com and click on get started and download a docker for your your desktop and get that up and running and once you do that you should have just a docker command that you can run on your command line right so i have docker installed and i have the docker command and once i have that installed what i should be able to do is pull this i-beam image so i'm going to pull that image now so i'll do docker pull voice slash i-beam and this should pull down the image and all the dependencies so you see it's downloading this docker image and once this is done i should be able to run this container locally on my machine and not have to worry about any of those java dependencies or how to run this headless gateway and all that stuff it should just do that all for me and so let's go ahead and let this finish downloading it does take a little while it looks like it's a pretty large image here alright so it looks like it's done so i'm going to type docker images and you'll see that i have this docker image voice i-beam right here so you see that exists looks like it was about a gigabyte so in order to use this you need to run this docker container and you need to pass it your interactive broker's credentials so that you can actually authenticate you can pass those credentials on the command line as these command line arguments so you can pass in these environment variables or you can create an environment a file here with all of your information and so i prefer to use this environment file so i'm going to go into a new project directory real quick and so i'll just call this ibm test and so i'll go into ibm test just like that and so i'm at the command line and i'm just going to create a new file here i'm going to use vi but use you can use visual studio code or whatever editor and so i'm just going to do n.list so i have a new file called env.list and i need to provide my account credentials so i'm going to say ibeam account equals and i'm not going to show you my password obviously because there's billions of dollars in in my interactive brokers account and i don't want you to take them from me i'm just trying to teach you how to use this thing so uh ibm account and then ibm password and i am going to enter in my real stuff so this will just be your password and your account and i'm going to enter mine off of camera and save it to a file and after you're done inside of this directory you should just have a file called env.list and you should have your credentials inside of that file so now that my real credentials are in that file i'm just going to take this docker run command so i'm going to do docker run environment file and dot list so this file is already in my direct my directory and then i'm going to say dash p and you can tell it what port you want to run it on looks like by default they just use 5000 so port 5000 and you're going to actually be able to uh do two-factor authentication from a local host running on your machine and i'll show you that real quick so i'm going to do voice i-beam so what i'm doing is actually running a docker container now and so you see it runs and it says open localhost 5000 to log in and so what i'm going to do here is actually log in so i'm going to type in my username and password and my phone is also going to get a two-factor authentication message all right so now that i've entered my two-factor authentication and clicked login it actually displays client login succeeded and i'm still on localhost here so what you'll see here in the terminal now is the gateway is running and authenticated that means i'm ready to use the client web portal api just like that and that wasn't too bad right and so we can either write a python program to make web requests against local host 5000 or we can use insomnia or postman or another rest client to just do this graphically so i'm going to show you how to do it graphically in the next video i'll deploy this to a server and we'll use python in order to execute these api requests so you see it's authenticated you see i i failed the first time one note is be careful about failing a login too many times if you mess up your two-factor auth or forget your password interactive brokers will actually lock you out of your web portal account and i actually had to call them on the phone and that took like i had to wait on hold for like 30 minutes so i hate calling in for stuff so be careful not to lock yourself out of your account or you're gonna have to wait a little bit so i'm good to go there and so what i'm gonna do is use a program i've used on this channel before and it's called insomnia so if you do insomnia rest client you can use this and what this allows you to do is to make web requests using a graphical user interface but we can also do it with python i'm going to show you the python equivalent afterwards i just want to quickly show you how to test this out and some people like postman so you can use postman if you like using that one as well so if you want to use insomnia like i do you can just click get started and download that it's absolutely free so i'm going to start insomnia on my local machine and this gives me this nice graphical tool here and i'm gonna create a new uh request collect collection so i'm just gonna call this ibm test and so this is just a collection of api requests that we can try out and the first thing i'm going to do is say new request and let's just see if we can get some market data so i'm going to name this request market data and this will just be our test of whether we can get market data okay so i'm going to go to the client portal api and let's see oh yeah one quick thing you can do before we even get to insomnia is just make sure your api requests work from the command line and so to test this what they did is use the curl command which if you have that installed you can use curl to make an api request from the command line you'll notice this parameter dash k here which means don't verify ssl so i'll do a curl dash dash help here and you may not have curl on your machine depending on what machine you're running so you can install curl figure out how to install that on your machine there's a windows installation and but you don't even need curl at all this is just one way to test it so they're making a get request to an api endpoint just to make sure it returns a response and they're doing a dash k here and this dash k part is very important so this is not verifying the ssl certificate so if you're going to use insomnia and not verify ssl what you need to do is click this gear in the top right hand corner and on request response you want to uncheck validate certificates in order to interact with https logo host 5000 okay so make sure you uncheck that box or you get uh some type of i think you get an ssl error i can't remember what the error message is so that's what that dash k is is doing and that's how i replicated that using insomnia all right let's go ahead and look at the client portal web api documentation to see if we can figure out how it works so i'm going to click this interactive brokers documentation client web portal api and you can see a browse available endpoints so let's see what is available to us all right so when you're using the interactive brokers api you care about the contract id so that's the the thing you're going to trade so let's see if we can get some information about a particular symbol and all the contracts that are available for that symbol so i'm going to go to contracts and this one says security stocks by symbol and so you see there's an endpoint here and so what this is expecting is a get request to localhost 5000 right and so i'm going to do is make a get request right i can do get post put patch delete i'm going to do a get request to localhost 5000 vo1 api trs rv stock so i'm just copying this endpoint and let's see what parameters it requires in order to get the stocks by symbol so i'm going to pass it query parameters so query query parameters means we do a question mark like that and so we're including the parameters in the url and the parameter is named symbols so i'm going to do question mark symbols equals and how does it want the symbols a list of upper sensitive symbols separated by comma and so let's say i care about apple for example and i've been interested in western digital stock lately because i heard a bunch of people are buying up hard drives lately and there might be a shortage there let's say i'm interested in western digital stock so wdc so symbols equals apple comma wdc i'm going to click send and just like that you see that i made an api request to the interactive brokers client portal web api and we have apple here and we have a western digital in this json response and you see we have this con id this contract id and let's say i'm interested in this one 13 681 which is for western digital corporation so 13681 and let's say i want to find information about that particular contract let's say i'm interested in market data so i'm going to click market data here and let's say i want market data history and this assumes you with if you have interactive brokers you probably have some type of a market data subscription that lets you fetch all this historical data i have that enabled on my account and so make sure you have that if you don't already if you want to use this i i'm assuming if you're using interactive brokers you have all this stuff already you have an account and all of that okay so i'm going to use this market data history endpoint so i'm going to click new request market data history so i'm calling that one market data history and actually this one is called a contract get stock contract so i'm just naming these requests just to make it easy to find and what you can do is make a with insomnia is create this collection of api requests that you're interested in test them out see the responses and kind of experiment with this and try it out before you actually write python code this lets you just see what the data format looks like see what the requests look like make sure everything works okay so we got a stock contract now we want this market data history i'm going to paste this market data history here and what does it want query parameters once again i'm going to do question con id equals and what contract id do i want uh we will have 13 681 here so i'm going to do con id equals 13 681 i'm going to send that over just like that you see i have uh this contract for western digital corporation so i believe this is just the common stock and you see under data here we have a list of open high low close values and volume and time stamp and so if i checked this time stamp here let's see what it's for so we can go to unix timestamp here for instance plug in this time stamp hit convert and you see i'm recording this on sunday right now so this last traded on friday so this is 6 30 a.m my time pacific time so this is like the first minute of the day uh for western digital stock right and if i got the second one let's see if this is the minute time frame so i can go where am i going here i'll convert it you see that's 631 and if i scroll down here we have lots of minute data right and if i got the very last one and let's see what we got so i have this and paste that in and you see that's for 12 59 which is the last minute the market was open so this is the regular hours for the market and we have all this price data that we're fetching from interactive brokers so good to go we're already making a couple of api requests let's see if we can uh place an order so to do that let's go back to our client portal api and click on order and let's click on place order and so you'll see this one's a little different in that it's a post request so we're not getting data from the server we're posting an order so we need to create a new request so i'm going to do new request and i'm going to say that this is place order and instead of a get request i'm going to do a post request and this post request requires my account id and then it's also going to require me to post a json structured payload that looks like this right so i'm gonna do post instead of no body i'm gonna click a json as the type of request and i'm gonna click uh okay and so in here i'm gonna put some json payload right and so i'm gonna take this url here and this is localhost 5000 v1 api iserver account slash and i need to replace this with my actual account id and so uh figure out what your account id is if you don't know your account id luckily there is a request you can make here that will pull in your account id and i don't want to show you my account id on camera so i'm going to get i'm going to make another request called get account and i'm going to get my own account id and somehow blur it out all right so in between here i'm gonna put my account which is like u123 and so for my json request body here you'll see uh it requires a number of things so i'm gonna send it a contract id so i'm going to do con id like that and since we mentioned that we were interested in contract id 13681 i'm going to put that 13681 right there we need a security type so i'm going to do a sec type okay and i'm doing a stock so i think i can just do a stock right there and let me just click send to see what happens it says unknown order type so clearly i need an order type so there's order type so i'll put order type and order type can be market or limit so let's just do a market order which is mkt i'll try send it again order size 0 is not value valid so we probably need a side and we need a quantity so let's do a quantity and let's just get one share and side uh buy okay and so i'll submit that time and force null time and force so tif is time and force so a good tool canceled or expire at the end of the day so i'm just going to say good till cancel so i'll do tif gtc just like that and click submit and there you go so i actually submit an order it looks like it gave me an order id and it says market order confirmation inside of this message here and yeah so i assume that i'll go through it's a sunday right now so i'm not actually placing that order it's just in the queue right there and so that's pretty much it for this video i just wanted to get you up and running with ibeam using the docker container setup and this environment file and show you how to use insomnia here to make a view web request to this client portal api to show you how it works and make sure you know how to look through this documentation and figure out how to send it a post request with an application application json body and also how to do a get request where you have some query parameters and using all those things together now that you're authenticated against interactive brokers and you have this container running you should be able to test all these out and play around with it with insomnia and figure out how it works before translating it into a python code using the python request library stay tuned for the next video i'm going to see if i can take this docker container and put it on lynnode which is a virtual private server company i like to use it's like five dollars a month so i like to use this and i'm gonna see if i can get interactive brokers running on a remote server that way we don't have to be running this on our laptop at all times and maybe we can build some type of trading bot or system on top of a interactive brokers running in the cloud that's authenticated at all times so thanks a lot for watching and stay tuned for the next video thanks
Info
Channel: Part Time Larry
Views: 6,419
Rating: undefined out of 5
Keywords: interactive brokers api, rest api, tutorial, client portal web api, ibeam, docker, trading bot, algotrading, ib rest api
Id: O1OhiiCx6Ho
Channel Id: undefined
Length: 19min 40sec (1180 seconds)
Published: Sun May 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.