twsATSXL for AmiBroker and IB TWS (Level 101)

This QuickStart Guide shows you how to use twsATSXL, AmiBroker and IB Trading API to automate a trading strategy that combines Guppy Multiple Moving Average (GMMA) and Bollinger Band (BB).

Have a look at the following summary on how they work together before we go into hairy details:

  • AmiBroker displays the trading signals (see below), and generates signals for twsATSXL;
  • IB Gateway connects twsATSXL and IB trade server;
  • twsATSXL executes the trading strategy using the above signals as one of input sources,
    then orders and positions are managed accordingly;

Manual trading that needs fast order entry? If you do manual/discretionary trading most time, this QuickStart Guide does help you walk through essential configurations until step Make Trade Manually with twsATSXL, upon there you are fully up to trade with twsATSXL that supports fast order entry..


Summary on The Trading Strategy


In this QuickStart Guide, two AFL files are used for Indicators to display the trading signals:

  1. ATSXL_GMMA_v2.afl
  2. ATSXL_MMAExpand_v5.1.afl

The third AFL file, ATSXL_MMAExpand_Explore_v5.1.afl, will be used for AmiBroker Exploration to create the trading signals.

The setup and use of these three AFL files are detailed in Configure AmiBroker and AFL. (in case you want to jump there)

Here are a few notes that may help you better understand the use of these indicators as part of our trading strategy:

  • Many parameters used by GMMA and Bollinger Band here can be tweaked (as you will see them as parameters)
  • GMMA's Investor lines are not shown (as you will see parameter "Show Investors" is set to No)
  • GMMA Trader lines are used to detect if expansion or compression pattern is formed, which creates entry and exit signals (hollow arrows in red or yellow) for ATSXL.

    ATSXL will read these signals, match them against your trading strategy (or trading plan), then manage the order and positions accordingly.
  • Bollinger Band here are simply used to create trailing stop signals (small hollow triangles in red or yellow) for ATSXL.

    ATSXL processes these signals similarly as it does with entry and exit signals.
  • You can later use any other indicators or algorithms to create entry, exit and trailing stop signals, i.e. the slowest Trader line could be a good candidate for trailing stop.
  • You can create any other signals, i.e. take profit, re-entry, and so on, and configure ATSXL to use these signals and manage your orders and positions by your design.

The screen shot shows how these indicators and signals look like:

AB_indicator.JPG

Summary on The System Requirements


Pleaese refer to the general descriptions on ATSXL' system requirements.

The following requirements are specific to this QuickStart Guide:

  • IB Account (paper account);
  • AmiBroker 5.0 or later;
    Earlier version should be OK.
  • Microsoft Excel 2003/2007/2010 (32 bit);
    Any one of editions installed is OK.
    32 bit limitation is due to IB TWS API.
  • Microsoft .Net Framework 2.0 or later;
    .Net 3.0 or 3.5 should be OK as they will also install .Net 2.0.

    Note: .Net Framework 4.0 or later can not used hence not required.

IB's Official API Reference Guide

Interactive Brokers (IB) has published an excellent API Reference Guide that provides official and most-updated information on its TWS based Trading API. You can find it useful because our sample Excel spreadsheet (twsATSXL.xls) here is built on top of IB's sample TwsActiveX.xls, directly utilizing many of its worksheets and functions.

You can access Interactive Brokers' API Reference Guide here.

The information from this API Reference Guide is relevant to our twsATSXL, specifically the chapter Overview, DDE for Excel, and Reference are helpful.

Important Tips:
#1 - If you would like to make the most use of twsATSXL, please spend some more time on charpter DDE for Excel, which provides detailed information on these worksheets: Account, Tickers, Basic Orders, Open Orders, Executions, Portfolio. These worksheets can be easily used for manual trading with twsATSXL.

#2 - In addition, for (Semi-)Automated Trading with twsATSXL, Basic Orders Worksheet is particularly important because your trading strategy (or tranding plan) will control orders on this worksheet.


Download and Install IB TWS

Follow instructions here from Interactive Brokers website.
Note that current IB TWS installation package includes IB Gateway.


Download and Install IB API

Following instrunctions here from Interactive Brokers website. After accept its agreement, choose "IB API for Windows", download and install this package.


Configure IB Gateway

(You may refer to Interactive Brokers' API Reference Guide here for more details if needed.)

You will get similar screen when you log onto IB Gateway successfully (with the same IB Account and Credentials):
IB_Gateway_connected_with_server.JPGClick Configure on the top of IB Gateway window and make sure the following highlighted fields have desired value:
IB_Gateway_config.JPG

Configure Microsoft Excel

Follow instructions here to configure Microsoft Excel.


Download and Install twsATSXL

Follow steps below to download and install twsATSXL:

  • Create a directory on any drive of your computer, i.e. D:\twsATSXL;
  • Download twsATSXL package from here and save it to the directory you just created;
  • Unzip this package to create a group of files and sub-directories under directory you just created, i.e. D:\twsATSXL;

Configure twsATSXL

Most ATSXL settings are presented on the CFG Worksheet.

The sample Excel spreadsheet file included with the package has all configrations set properly for you. So you do not worry about them at this moment, however you may need to change them later for your own trading strategy. Please refer to User Guide for details.


Configure and View twsATSXL Log Files

ATSXL produces the following three log files when it is running:

  • Primary log file - it keeps messages from ATSXL core components, including user message, warning or error if any.

    This file also keeps important messages from the following two log files, which provides a consolidated view on all events in sequence;
  • MSG log file - it keeps signals read from an external file, i.e. the signals generated from AmiBroker Exploration;
  • Trade log file - it keeps messages from Broker's trading API;

The location and file name can be configured on CFG Worksheet. Here are settings used in our sample twsATSXL spreadsheet:

Log_File = atsxl_core.log

MSG_Log_File = atsxl_msg.log

Trade_Log_File = atsxl_broker.log

You can view the file contents with any text editor or log monitoring software. Since these log files will be updated frequently by ATSXL, you should use a proper tool that:

  1. does not exclusively lock the file; and
  2. can move to the end of file easily (for your convenience);

We currently use BareTail and BareGrep from Bare Metal Software Pty Ltd. to view/tail the log files (using BareTail) and search certain contents (using BareGrep).


DISCLAIMER:
WWW.ATSXL.COM is not affiliated with the vendor of the above mentioned software. It is recommended here simply because the software meets our needs and we have used them for years.


Initial Check for Updates

For the first time, when you open a spreadsheet for ATSXL, and click [Load ATSXL] button on the CFG Worksheet, ATSXL will prompt you to check if there are bug fixes or updates available.

XL_2003_check_updates.JPG

Let's use twsATSXL.xls as an example:

Please save and exit all Excel spreadsheets, then run the batch script update_ATSXL.bat under your ATSXL directory (double click it) or run it in a Command window.

D:\twsATSXL>update_ATSXL.bat

The batch script will create a unique time stamped sub-directory (i.e. Backup_2013101309453012), copy all critical files along with your Excel spreadsheets into this directory, then start an automatic updater. You simply follow screen instructions from there.

A log file ATSXL_check_update.log will be updated with a summary of this update activity and the name of the backup directory if created.

If you prefer to not create a backup directory, you may run batch script update_ATSXL_NO_BACKUP.bat instead, which is a wrapper of update_ATSXL.bat. For example:

D:\twsATSXL>update_ATSXL_NO_BACKUP.bat

WARNING: It is not recommended to skip this backup step.


This backup created prior to any updates provides a way that you can roll back to previous version if something went wrong. You also get a chance to keep existing Excel spreadsheets with multiple versions that could be useful some time later. You can delete these backup directories when everything is confirmed OK.

You may run this check and update batch script at any time. ATSXL will remind you to run it once every three weeks to make sure you receive the most recent updates.


Load ATSXL Add-in into Excel

You now have the latest ATSXL after initial check and update from previous step.

Open twsATSXL.xls spreadsheet, and click Load ATSXL button on the CFG Worksheet, ATSXL Add-in will be loaded into Excel and creates a menu named twsATSXL on Excel's menu bar. Click the only sub-menu item About to display a small window with twsATSXL's version and website link.

ATSXL Add-in is loaded into twsATSXL.xls spreadsheet successfully if you see similar screen shown as below:

XL_2003_twsATSXL_about.JPG

Connect twsATSXL with IB Gateway

(You may refer to Interactive Brokers' API Reference Guide here for more details if needed.)

Click Connect to TWS on the General Worksheet to connect Excel with IB Gateway, you may feel a short while of freeze when it's trying to establish connection.

You will see similar screen with the buttons and fields highlighted as below. Note the values should match against what we have entered for IB Gateway.

XL_2003_connect_gateway.JPG
You will get similar screen when you connect twsATSXL.xls with IB Gateway successfully:
IB_Gateway_connected_with_client.JPG

Now it's time to pull account details to test connection:

Click the Request Account Updates button on the Account Worksheet. If it pulls account details out there then your twsATSXL is connected with IB trade server through IB Gateway successfully.


Make Trade Manually with twsATSXL


You can create an order on the Basic Orders Worksheet, click Place/Modify button to place a new order or modify an existing one, click Cancel button to cancel an existing order. Note that you have to click the actual row where the order is located before you click button to place (or modify or cancel) that order.

Important Tips:
#1 - If you would like to make the most use of twsATSXL, please refer to Interactive Brokers' API Reference Guide here, spend some more time specifically on charpter DDE for Excel, which provides detailed information on these worksheets: Account, Tickers, Basic Orders, Open Orders, Executions, Portfolio. These worksheets can be easily used for manual trading with twsATSXL.

#2 - In addition, for (Semi-)Automated Trading with twsATSXL, Basic Orders Worksheet is particularly important because your trading strategy (or tranding plan) will control orders on this worksheet.

Upon here, you have completed this QuickStart Guide if you do manual/discretionary trading most time. There are many possibilities for you to use current sample as a start point so that you can incorporate more calculations on Excel spreadsheet to aid your trading.

As you see, there are a few more steps ahead (as below) to start your adventure on Automated Trading with twsATSXL if you were ever interested in this idea…


Configure AmiBroker and AFL

Follow instructions here to configure AmiBroker and load sample AFL.


Test to Pull Signals Created by AmiBroker

Once you configured AmiBroker and ran the its Exploration successfully (as the above step shown). A signal file will be created under the designated folder. Such file will be used by twsATSXL.

Click Start Pull button on the MSG Worksheet to pull the signals presented in the file and further write them on the MSG Worksheet. Please check the file path used by both AmiBroker and twsATSXL, if you don't see any signals shown on the MSG Worksheet.

Note:
The following mentioned sub-directory msgout will be automatically created when you unzip the twsATSXL package, and you can modify the configurations below to use any other directory.

Suppose you have twsATSXL installed on D:\myATSXL, you should use the following code in AFL:

// define the full path and file name for message out
// NOTE: double back slash "\\" is used here
strFilePath01 = "D:\\myATSXL\\msgout\\atsxl_msgout_tws.txt";

You also need to check setting of MSG_Outbound_File on CFG worksheet to make sure twsATSXL can read the file. For example:

MSG_Outbound_File = msgout\atsxl_msgout_tws.txt

Note:
Here twsATSXL pulls the signals only once, which is useful for testing in some case. Continue on the following steps to configure twsATSXL for continuous run. Signals will be pulled periodically based on the specified frequency.


Test to Run The Strategy Once

Click Run It Once button on the CFG Worksheet to run the trading strategy (or trading plans) located on the CORE Worksheet.

Note:
Here twsATSXL runs the trading strategy only once, which is useful for testing in some case. Continue on the next step to configure twsATSXL for continuous run.


Start The Timer to Run The Strategy

Click Start Timer button on the CFG Worksheet to run the trading strategy (or trading plans) located on the CORE Worksheet, repeatedly according the timer setting. Once the timer is started, the text on the button becomes Stop Timer indicating you can click it once again to stop the timer

The frequency that the timer uses to trigger a certain task can be configured on CFG Worksheet. Here are settings used in our sample twsATSXL spreadsheet:

# Determine how often the Timer activates runTask

Timer_Freq = 0:01:03        # hh:mm:ss (0:00:00 to 23:59:59)

# Determine how often it pulls AB Messages (on the MSG sheet)
# NOTE: MSG_Pull_Freq must be shorter than Ticker_Proc_Freq

MSG_Pull_Freq = 0:00:55     # hh:mm:ss (0:00:00 to 23:59:59)

# Determine how often it processes Tickers (on the CORE sheet)

Ticker_Proc_Freq = 0:01:50  # hh:mm:ss (0:00:00 to 23:59:59)


Setup Monitoring on Log Files


We currently use logstash, a powerful open source log management tool, to monitor and scan ATSXL's log files, which ensures important alerts on trading activity and critical events can be detected and delivered via Email.

Of course, you can use your own program or script as long as it can monitor ATSXL's log files to meet your needs.

Upon here, you have successfully completed this QuickStart Guide: twsATSXL for AmiBroker and IB TWS (Level 101)

2010 - 2015 WWW.ATSXL.COM COPYRIGHT NOTICE