Level 101: twsATSXL with IB TWS, AmiBroker

This QuickStart Guide shows you how to use twsATSXL, IB Trading API and AmiBroker 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:

  • IB Gateway connects twsATSXL and IB trade server;

    (Note: you should have completed this setup at Level 100.)
  • AmiBroker displays the trading signals (see below), and generates signals for twsATSXL;
  • twsATSXL executes the trading strategy using the above signals as one of input sources,
    then orders and positions are managed accordingly;


Please first complete QuickStart Guide “Level 100: Manual Trading with IB TWS” so that you have installed and configured IB TWS, IB Trading API and twsATAXL. And you have been able to manually send, modify and cancel orders.

Summary on The Trading Strategy

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

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

There is the third AFL file, ATSXL_MMAExpand_Explore_v5.1.afl, that is 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.

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:


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.

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 a full path and file name for outbound message
// NOTE: double back slash "\\" is used here
strFilePath01 = "C:\\twsATSXL\\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

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.

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.

Congratulation! You have completed this QuickStart Guide.