Interactive Brokers Trading

Ooooo, I am tipping my hand. Ha! One of the things that annoys me is how much work it takes to keep eyes on my portfolio.  I would like to automate some of this effort.  After a little research (and I do mean a little), I have decided to give Interactive Brokers a shot. I am not sure what the bill is going to look like relative to my other brokerage accounts, and that will probably be a follow-up article.

A crazy wall of horizontally and vertically stacked old books with a rustic blue wood door in the center. The words "Support MindFuel Blog on Patreon are shamelessly plastered over this fine image

It takes a little effort to figure this stuff out. If you are like me and going for the C# flavor, you need:

  • An IDE (e.g. Visual Studio Community Edition, not VS Code)
  • .NET 4.5 or higher (not Core)
    • Note: I had an issue with Visual Studio not being able to target .NET 4.5.  I tried installing it a couple different ways including using “multi-targeting” but no love.  To get around this, I took Visual Studio up on the option to “upgrade the project to 4.6.1”, which allowed me to load the projects and build the sample code.
  • An IB Account (preferably with the mobile phone IB Key application for authentication)
  • Download either the TWS Desktop App, or the IB Gateway App.  If you don’t know which one, use TWS because it has a GUI.  Your code should work with IB Gateway later, should you decide to progress to that.
  • Download the latest API, and install it to our C:\ drive (there is a warning about this drive being required)
  • Links are not included – they appear to change over time, so just recommend your favorite search engine to drill into the latest gadgetry.
  • And a healthy dose of caution: Make sure you are in paper trading mode, or ready to use REAL money when you run any code on this trading platform.

With all that setup and ready to rumble, I found a couple handy URLs.  First is this overview URL: How to do various goodies, and then, within that, I have started with this initial market data tutorial.

One thing I noticed is that the IBApi namespace is actually built from the CSharpApi project. I noticed other switcheroos between file names and class names. Stay vigilant!

Next thing I noticed was that the tutorial started talking about an EWrapper implementation (to save us newbies from having to implement the EWrapper interface our darn selves). The tutorial says to look for the EWrapperImpl class, which is in C:\[your path to]\twsapi\samples\CSharp\Testbed but I also found it was implemented in: C:\[your path to]\twsapi\samples\CSharp\IBSampleApp\backend\IBClient.cs.  The first is for a console app, the second seems more appropriate for a back end library. Just saying that to remind myself it exists later.

After writing the initial code and running it, I still couldn’t connect to IB. I was getting a “connection refused” exception. Luckily, I found this article and realized, in my global settings the “Allow ActiveX and Socket Clients” checkbox was not checked.  After checking that, I was able to connect.

Of course, I got a big fat error about not having the required market data subscription. Maybe I will write a post when I figure that out.

Happy PAPER trading – be careful out there!!

Leave a Comment

Your email address will not be published. Required fields are marked *