Amibroker is an excellent tool for back testing and can also be set up for automated trading with Interactive Brokers.

The basic infrastructure is to connect Amibroker to the Interactive Brokers Trader Workstation software using the Amibroker IBController plugin as a buffer.

The IBController is able to send and receive information from TWS and take commands from Amibroker allowing it to send and cancel orders to your Interactive Brokers account.

Data can either be imported real time into Amibroker direct from Interactive Brokers (with a limit of 100 symbols) or via a third party program (like eSignal or IQFeed). More details are shown on the Amibroker auto-trading page.

auto trading with amibroker and interactive brokers

Interactive Brokers TWS with API orders

Interactive Brokers TWS with API orders

One of the drawbacks of automated trading with Amibroker is having the applications running on your desktop. Doing so can lead to difficulties such as lack of memory, server outage, power disruption etc.

Ideally, automated trading infrastructure should have its own server and be cloud based.

When it comes to trading with end of day data, with providers such as Norgate Premium Data, a slightly different approach can be implemented.

Semi Automated Trading With End Of Day Data

Before proceeding, please remember that auto-trading is extremely risky. Always test your auto-trading system with the IB paper trader first and make sure the orders have been accepted and the system is working correctly. Power outages, internet failure and other problems can interfere with the connection between TWS and the IB Controller.

Auto Trading Code

The following code can be used to send end of day orders directly from Amibroker to Interactive Brokers.

The idea is that you run an exploration on your database after the market is closed. The IBController then transmits your orders to TWS:

Symbol = Name();
PercentageOfAvailableFunds = 10; //% of Available Funds

if (LastValue(Buy) || LastValue(Sell)) {
ibc = GetTradingInterface( "IB" );
if (ibc.IsConnected() ) {
pos = ibc.GetPositionSize(symbol);
if (LastValue(Sell)) {
if (pos > 0) {
orderID = ibc.PlaceOrder( symbol, "Sell", pos, "LMT", LastValue(C), 0, "GTC", True );
_TRACE("Sell "+symbol+", orderID "+orderID);
else if (LastValue(Buy)) {
if (pos == 0) {
CashBalance = StrToNum(ibc.GetAccountValue("CashBalance"));
Quantity =  CashBalance * PercentageOfAvailableFunds/100/LastValue(C);
_TRACE("CashBalance "+CashBalance+", Closing price "+LastValue(C)+", Quantity "+Quantity);
Quantity = Max(1, round(Quantity));
orderID = ibc.PlaceOrder( symbol, "Buy", Quantity, "LMT", LastValue(C), 0, "GTC", True );
_TRACE("Buy "+symbol+", orderID "+orderID);
else {
_TRACE("Order for "+symbol+" already exists!");
else {
Error("IB Controller Interface is not connected");

if (LastValue(Buy)) {


What Happens

Paste this code underneath your trading system rules to run an exploration on your Amibroker database and gather your buy and sell orders.

The IB controller will open and read the available funds from your TWS account (cash balance field). The controller will then read the buy and sell orders from your Amibroker exploration and transmit those orders to your TWS.

In this example, the position size for each buy order is 10% of available funds and the entry price will be entered as a limit order with the limit price being yesterday’s close. These settings can be easily altered.

Where a sell order is required, the IB controller first checks to see if a position is open. If a sell order is required it then transmits a sell limit order, again based on yesterday’s close price.

Note: entry and exit prices can be easily switched to market orders.

TWS Setup

For the above code to work correctly it is also necessary to make sure your TWS is configured correctly.

  • In API Settings make sure Enable ActiveX and Socket Clients is ticked.
  • Make sure Read-Only API is unticked
  • In precautions, make sure all bypass options are ticked
  • Make sure Port Number in IB Controller matches socket port in TWS API settings. Mine was 7497


Auto-Trading Steps

So, the basic steps are as follows:

  1. Make sure TWS is open and configured properly
  2. When the market is closed update your Amibroker database with new quotes (this is done once a day for example with the Norgate Data Updater)
  3. Paste the auto trading code underneath your trading system rules
  4. Run an exploration on your watchlist
  5. IB Controller should now open and start communicating with TWS
  6. Check the connection is OK
  7. Check all pending and submitted orders in IBController and TWS
  8. Do one last check of all tabs in IBController and TWS to make sure everything is OK
  9. Once orders are submitted you can log off and come back the next day to check for new buy and sell orders.

using the ib controller with amibroker

Round Up

You now have some code that you can paste at the bottom of your trading system rules that will send limit orders directly to Interactive Brokers via the IBController plugin.

Obviously this should only be done when you have a trading strategy you’re confident with.

The only manual intervention needed is to update your quotes and run the exploration which should be done on a daily basis when the market is closed. If you desire you may be able to automate this process using Windows Task Scheduler.

This is quite a useful solution for automating end of day trading and will help you get to grips with the auto-trading setup in Amibroker.

Just remember the high risks of automated trading and make sure to start off on the IB Paper Trader.


Tags: ,

Leave a Reply

%d bloggers like this: