Testing your AB-IBc-TWS Communication

Besides demonstrating the basics of Automated Trading (AT), the code below may function as a diagnostic tool during AT code development. It often happens that things suddenly stop working, and no orders are transmitted. When this happens, and before you start looking for bugs in your code, you can run this code to verify that your interfacing to the TWS is functional.

For orders to be transmitted to the market you must have entered your “Unlock Code” for the IB Controller in the Unlock window that pops up when you click Files -> Enter Unlock Code. You can obtain your code electronically by following the link to the IBc User Agreement. When you have signed and submitted the User Agreement the Unlock Code will be emailed to you within seconds.

The test code below can be executed from an Indicator window and will test your AB->TWS connection by placing orders from the Param window to your eDemo or Paper Trading account:

Order and TWS Status is displayed in the Title:

If you are using IB?s eDemo, orders may be processed slowly enough for you to observe how the orders are processed.

The code below illustrates several basic but very important aspects of Automated Trading, and it is important to fully understand this code before trying more complex programs. The most important concept to understand is that of the Order ID. The IBc returns a unique OrderID for each order placed. This OrderID can subsequently be used to modify, transmit, cancel, and get status for the order. For any AT system to function properly, OrderIDs must be tracked meticulously at all times. Using an expired OrderID, a non-existing one, or one for an order that is already filled, for example, will lead to API errors.

SetChartOptions2chartWrapTitle );
RequestTimedRefresh);
BuyOrderTrigger ParamTrigger"Place Buy order on TWS""BUY" );
SellOrderTrigger ParamTrigger"Place Sell order on TWS""SELL" );
CancelOrderTrigger ParamTrigger"Cancel order on TWS""CANCEL" );
TransmitTrigger ParamTrigger"Transmit Order""TRANSMIT" );
ibc GetTradingInterface"IB" );
OrderID StaticVarGetText"OrderID" );
ORderStatus ibc.GetStatusORderIDTrue );

if ( BuyOrderTrigger )
{
    OrderID ibc.ModifyOrderOrderIDName(), "Buy"100"MKT"00"Day"TransmitTrigger );
    StaticVarSetText"OrderID"OrderID );
}

if ( SellOrderTrigger )
{
    OrderID ibc.ModifyOrderOrderIDName(), "Sell"100"MKT"00"Day"TransmitTrigger );
    StaticVarSetText"OrderID"OrderID );
}
else
    if ( CancelOrderTrigger )
    {
        OrderID StaticVarGetText"OrderID" );
        ibc.CancelOrderORderID );
    }
    else
        if ( TransmitTrigger )
        {
            if ( ibc.IsOrderPendingORderID ) )
                ibc.TransmitORderID );
        }

OrderID StaticVarGetText"OrderID" );

LastTWSMsg ibc.getLastErrorORderID );
Title "\n" +
        "           OrderID: " ORderID "\n" +
        "Last TWS Error Msg: " LastTWSMsg "\n" +
        "Order Status at IB: " ORderStatus "\n" +
        " TWS Position Size: " NumToStribc.GetPositionSizeName() ), 1.0False );

Edited by Al Venosa

Comments are closed.