Building your trading bot, step 1

If you are going to build your own trading bot, the first thing to do is connect to a brokerage through their API (application programming interface). Without being able to establish this connection, your bot will be worthless. So you need to make sure you are able to get this plumbing working first, or you’ll be wasting your time. I have tested out many brokerage APIs (if they even have one), so I will give my synopsis here:

TD Ameritrade: In my opinion, this is currently the best brokerage for bot trading and the one I currently use. Their API is very deep and full featured. It does have a few quirks (like an ALL_OR_NONE flag, which I wanted to use to prevent partial fills, isn’t usable), but I can work around those things. My one concern with TD Ameritrade is that Schwab (who recently acquired them) has not confirmed whether they will keep the API around in the future. If they ever ditch it, I will have to switch brokers.

eTrade: From a pure functionality standpoint, this was the next best API I found. The problem is that they have their authentication setup so that you would manually have to re-authenticate your bot every day in order to keep the bot connecting. This is okay if you plan to run your bot and be around to monitor it. However, that does not pass my test of being able to go on vacation for a week and trust that your bot will keep making money for you passively. eTrade does have a way to bypass this problem if you get approved as a vendor, but that involves setting up a demo where you have to show your application to their technical team and get their blessing. No thanks.

Tradestation: I had high hopes for Tradestation, as they initially seemed to meet all my requirements. They have an API that handles option trading well, and they are good about letting you establish an authentication where you don’t have to manually re-authenticate daily. However, after digging in I discovered that their API really doesn’t provide a usable order history. My entire bot revolves around the order history. While it would be possible to create a bot with this API, if anything gets messed up in your own data storage of your position history, there isn’t a good way to recover from that. No order history is a non-starter for me.

Interactive Brokers: I’ll be honest, I do have a bias against IB. Their pricing model has always felt off putting to me. And last I checked, their API is not a true API because you have to be running the IB software on your computer as sort of a gateway in order to make it work. This doesn’t bode well if you want to run your bot on a server, where you don’t have to rely on your own computer to always be up and running. So this is not an option for me, though others may be able to get it to work for them.

Once you have settled on a broker, you should decide on a programming language to use and write a simple piece of code that can authenticate to the API and retrieve data about your account.

I chose to use TypeScript for several reasons. First off, it is simply object oriented JavaScript and I think JavaScript is the most useful language to be proficient with, given that it can be found everywhere (browser and server side). Also, most APIs return data in the JSON data format, which is derived from the JavaScript syntax, and JSON goes hand in hand with the JavaScript language. I didn’t want go all the way to using Java, because while I’ve worked with Java for almost 20 years, I can write code much faster and cleaner with TypeScript. Plus, the compiling an deploying of Java code creates a lot more overhead.

Most examples of trading bots that you can find on the internet will be written in Python. I wouldn’t discourage anyone from using Python, if they are set on it. My reason for not using Python is that I find TypeScript code to be a lot cleaner and more readable. Not to mention, I already know JavaScript extremely well.

I won’t get too deep into the technical details of writing the connection code in this post, as most brokers have some good examples on their API site. For the most part, you will create a simple web page that allows you to authenticate with your broker and get a token that you can store. Once you have that token, your bot code can use it to authenticate with the API and make calls to get data and submit trades.

In subsequent posts, I will give some examples.

4 thoughts on “Building your trading bot, step 1

Leave a comment