Parsing TWS Error messages

Here is some simple code you can use to enable you to start developing your own error-parsing functions. In this example, error messages are hard-coded to facilitate code development, i.e. you don’t have to be live with IB to test it. I suggest that you inspect the IB list of error messages, copy the errors that are important to you, and add them to the hard-coded list. Since IB doesn’t list the error messages in the exact format you will receive them, you may have to place some orders with errors to see their exact format. When Applied to an Indicator, the code can be exercised by selecting the Error Message with the slider in the Param window. It will display the current error and parsing results in the Chart Title. Be sure to set Parameter properties to wrap the Title.


Param"Example Error Msg"0010);
switch ( )
case 0:
    TWSLastErrorMsg "ID=15071. Error 135. Can't find order with id =:15071";
case 1:
    TWSLastErrorMsg "ID=2094, Error 103, Duplicate ORder id";
case 2:
    TWSLastErrorMsg "ID=-1, Error 1102, Connectivity between IB AND TWS Has been restored - data MAintained";
case 3:
    TWSLastErrorMsg "ID=-1, Error 2100, New account data requested";
case 4:
    TWSLastErrorMsg "ID=-1, Error 1100, Connectivity between IB AND TWS has been lost";
case 5:
    TWSLastErrorMsg "Connection established OK, Next Order Id=2080";
case 6:
    TWSLastErrorMsg "ID=6125. Error 202. Order Canceled - reason:";
    TWSLastErrorMsg "";

function GetErrorCodeTWSLastErrorMsg )
    StrFindTWSLastErrorMsg" Error " );
    ErrStr StrMidTWSLastErrorMsg6);
    ErrNum    StrToNumErrStr );
    ErrStr    NumToStrErrNum1.0False );
    return ErrStr;

function ExtractErrorIDTWSLastErrorMsg )
    StrFindTWSLastErrorMsg"ID=" );
    IDStr StrMidTWSLastErrorMsg2);
    IDNum    StrToNumIDStr );
    IDStr    NumToStrIDNum1.0False );
    return IDStr;

TWSErrorCode GetErrorCodeTWSLastErrorMsg );
TWSErrorOrderID ExtractErrorIDTWSLastErrorMsg );
Title "\n" +
        "Extracting OrderIDs and Error codes from TWS Error Messages" "\n" +
        "Example Error Msg: " TWSLastErrorMsg "\n" +
        "        Error Code: " TWSErrorCode "\n" +
        " TWS Error OrderID: " TWSErrorOrderID;

Internet Resources for AT

To run Trade-Automation code, you should have the latest software versions of the AmiBroker-Pro and IB-Controller beta and have the AmiBroker Interactive Brokers Data Plugin selected for data source.

To use the AmiBroker Interactive Brokers Controller, you need an Interactive Brokers account. To place simulated orders you can use the TWS-Demo (Login: eDemo, Password: demouser), or, better, you can open a paper-trading account (free). The IB paper trading account is indispensable for any serious testing of your AT systems. The eDemo is very slow and much less adequate than using the TWS paper trading account.

DebugView is an essential tool to trace program execution and create logging files. You may consider creating a custom item in your AmiBroker Tools menu.

To deal with irritating TWS pop-ups common with earlier TWS versions, you may consider a product like PTFB Pro V3.1.0.0 by Technology Lighthouse (just one of many such products – this is not a special endorsement). Lately pop-ups seem to interfere much less, so you may not need this product if you use the most up-to-date version of TWS.

You should consider joining the Amibroker-at forum for discussions about AmiBroker-based Auto-Trading applications. To read about auto-trading in general, you should visit the Elite-Trader and AmiBroker-AT forums. You should visit the Interactive Broker forums to read about the TWS API and other AT topics. Be sure to create an icon for the AmiBroker ReadMeAT.html on your desktop, this file is located in your AmiBroker folder (default location). You might also want to have a copy of the API List of Error messages on your desktop; you will be referring to them frequently.

Edited by Al Venosa

Request Real-Time Topics here

You can suggest future topics for the Real-Time Auto-Trading category in a comment to this post.

RT vs EOD Trading

When migrating from end-of-day (EOD) to Real-Time (RT) trading, you will stumble upon many surprises, some beneficial and some that will be difficult to overcome. Trading is a very personal activity, and traders differ extensively on how they perceive the critical factors for success. The list below highlights a few areas where you should expect differences from your EOD experience.

1. Appearance of Bars. When you reduce the chart’s Time Frame (TF), the appearance of price-bars changes drastically. For example, in EOD you will rarely find sequential bars with unchanged prices; in the minute time frame, however, this may happen surprisingly often. Reducing the TF to 5 seconds exacerbates the problem even more. Many bars may be simple flat, horizontal lines where the OHLC prices are all equal. This could cause your indicators to drift and generate misleading signals. In addition, as you reduce the TF, the share volume may decline to the point where a bar might only represent a single trade, making it extremely difficult to get a fill at its price.

2. Data Padding. If you trade portfolios and use Foreign() the data you see may have been aligned and padded to your currently selected symbol (see: Automatic Analysis/Settings/check Pad and align all data to reference symbol). In this case the bar-prices you see are not real, have no volume and, of course, cannot be traded.

3. Gaps. When Indicators are used in RT, they do not correct for overnight gaps and intraday no-trading periods, which could cause problems with system performance.

4. Open vs. Close. In RT trading, the previous close is often equal or nearly equal to the current open since they are just one quote apart, while in EOD the entire trading system may be based on the difference between the previous closing price and the current open price.

5. Internet Delays. Internet delays impose much more significant effects in RT compared to EOD. The shorter time frames preclude speedy responses to price changes. Consequently, you will need to anticipate them and ensure your orders are placed if and when the expected move takes place.

6. Volume. Volume is a much more critical factor in RT trading. Many price changes cannot be traded because they might have resulted from just a single trade, which means that in these cases someone got filled before you did!

7. Commissions. When taking more frequent but smaller profits, which is a typical characteristic of RT trading, commissions play a larger role on performance. This means that the percentage of winning trades becomes more important than in EOD trading.

8. Order Execution. Efficient order placement is crucial to success in RT trading. Contrary to EOD trading, experiencing a few seconds or minutes delay can kill your system. Manually calculating order prices in RT becomes difficult if not impossible.

9. Data flaws. RT Data may arrive out of sequence, contain errors, and be corrected at a later time by the data provider. This means that after an EOD Backfill, your bars may look different. This often leads to highly misleading and overly optimistic profit performance by the backtester.

10. OHLC-Timing. In EOD, Open prices occur at the start of the daily-bar interval and Close Prices appear at the end of the bar. In real time (RT), however, the Open may precede the Close by just a fraction of a second, and the actual quotes might have arrived anywhere during the bar?s interval. This can become a problem when your system is timed by your system’s clock. For example, whereas the Backtester may easily enter a trade at the Open and exit at the Close on the same bar, you may not have enough time for this to occur in RT.

11. Data Stability. EOD data are stable, i.e., the OHLC prices never change once the bar has completed. For example, if your system triggers a Short signal when the High price crosses your LMT price, this signal can never disappear. Using RT data, however, the only price that remains stable for the duration of the bar is the Open price, while the HLC prices will constantly change until after the close of the trading bar. Barring data errors, the High and Low prices, by definition, can only go higher or lower, respectively, and cannot retrace to earlier values. However, the Close price will vary throughout the bar duration and may result in multiple signals when it crosses your LMT price multiple times. So, you should exercise care when developing entry or exit signals based on the closing price relative to other prices.

12. Trend Sensitivity. An EOD Trend-Following system is an example of a trend-sensitive trading system. Because RT trades are often much shorter in duration, RT trades have greater trend immunity because the trending component of stock prices diminishes at shorter time frames.

13. Breakouts. In smaller time-frames, RT Breakout systems will seldom give you your breakout price, and slippage will be proportionally greater as you reduce the timeframe. This is because the price tends to jump over your threshold and fill at a worse price.

14. Static Variables. You need to use Static Variables to carry system data from one pass to the next. There is simply no other way to pass parameters from one AFL execution to the next.

15. Random Price Movements. Price movements are subject to random price fluctuations introduced by slow trader response and Internet delays. Such changes are negligible in EOD trading, but as you work with shorter time frames in RT trading, you will gradually start trading basic price volatility that has very little to do with the direction of the market.

16. Sequential Processing. In EOD programming, tasks are usually completed within one pass through the code. However, in RT, programming tasks are completed in many small steps spread out over multiple bars, each step dependent on whether an RT external condition is met.

Edited by Al Venosa

Introduction to Real-Time Automated-Trading

Automated Trading of financial instruments is controversial and risky. Without exception, all code in this category is intended to demonstrate AFL programming techniques and not, under any circumstance, to place orders on your real-money trading account.

Code in this section has been developed using the latest versions of AmiBroker beta (4.96.0), the InteractiveBrokers Controller (1.1.1), and the TWS (873) as of this writing. If you experience execution problems, or AmiBroker gives you error messages, please verify that you are using the latest program versions before reporting bugs.

Unless stated otherwise, all programs are written to execute in an Indicator window. To run any of the examples, copy the code to the AFL editor and click Insert. To run programs that place orders on your paper trading or the eDemo account, the TWS must be open and connected to Interactive Brokers.

During initial testing, it is good practice to Insert the code in its own Pane under its own Chart-Tab, and activate only this window. This will prevent conflicts between static variables from different programs that may use the same name.

Most of the examples presented in this section are ready to copy, insert, and run. To give them this quality, peripheral code, like initializations, a trading system, and/or Title statement, had to be added. This makes the code a little longer but will give you a debugging start when applying the code and, at the same time, may expose you to some handy code snippets.

Edited by Al Venosa