Monday, January 27, 2020

Intraday Trading: Why You Need Trading API and The Pros and Cons of API Trading


Intraday trading is the question I get asked most and here I would like to address some of the main challenges.

Trading API allows traders the ability to build their own trading applications and consist of many portions; market data (real-time & historical), order and trade management, portfolio and account status updates.  In this post, I am going to talk about market data access API.

If you do quant trading (for non-direct exchange access traders), especially intraday, either structured time series or unstructured market microstructure analysis, you will at the very least need to use API provided by your broker for market data. Why?  Protecting your "trade secret" of course!

Although during the trading methodology analysis phase (back-test + optimization) to come up with profitable & robust trading strategy, you can do without API (just download or buy market data from legitimate source), how are you going to implement it during active market?  How and where are you going to get the, say 5-minute OHLCV bar or bid/ask/last done price and volume in real-time so that your strategy can analyse and then provide real-time trading signals?

To solve this problem, you have 2 choices:

1    1.       Pull method (for structured/fixed time-series only).

You could either manually (less complicated) or setup a fixed interval timer (more error handling routines needed) to call the API that provides the time series data that you needed in real-time.  The pro for this method is that it is relatively easy to implement.  The cons are that there will be a few seconds delay between you initiate the call and until you receive the data completely.  You will also be subjected by your API provider on throttling rules (too many complicated rules to discuss in this post) to avoid over stressing their servers.  Imagine a runaway program calling the API every second or worst!

For practitioners, there is another very important thing of concern; the availability of in-progress bar (see image below) and the delay (from the time it happened until you receive it).  Your strategy will have to differentiate between completed bars and in-progress bar.  If the API doesn’t provide you in-progress bar, then you could have a big problem, prices could run wild during the in-progress x-minute bar and you would miss your trade setups and signals depending on your trading strategy.

The best solution, with almost no delay, for time series data is to use the stream method explain below, however I have yet to come across any API that streams x-minute interval OHLCV in real-time, with in-progress and completed bar solution!  That would be on top of my wish list.



 2.        Push/stream method (for tick data, well, almost).

This is the best method in theory but not in practice.  If the API provides you with real-time every tick data, plus the changes of bid/ask, then this is the best method to use.  However, in practice, there is just too much data to process and push over internet!  So, most API only provide a snapshot of the real-time data, for Interactive Brokers at the moment it is a 250ms snapshot.  The pro for this method in theory is that you could build your own time-series (see image below) in almost real-time (rarely more than 1 second delay) and have your own trading strategy that choose any interval that no one compete with.  You could also do your own unstructured time analysis for your own HFT strategy.

However, in practice, not only it is much harder to implement and much more computer resource intensive, due to the snapshot, you would miss some of the prices done at and the order flow.  That means you cannot trust almost every setups and signals generated as the OHLC prices you captured and processed may not be accurate!  Another problem is that when the market is hot, the servers will push/stream the market data much slower (sometimes up to a few seconds) than when the market is calm.  Furthermore, how do you recover when the servers are disconnected then reconnect due to various reasons?

The last problem is a current problem.  Prices done by internal matching (internalization) doesn’t seem to send through the API!!!  Sometimes that would mean a few ticks missing or even seems like trade through the best bid/offer. I have personally observed trades done up to 6-7 ticks away!



To sum it up, like it or hate it, you will have to implement your intraday trading system using the trading API provided by your broker one day.  Some of the pros and cons of pull or push methods are provided above, no perfect solution exists, just choose one that fits your trading strategy, skill set and resources.  For me, I run both at the same time for each trading strategy (on different instances of Excel, both DDE & RTD).

Cheers and Happy trading!