July 12, 2007
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.
SetChartOptions( 2, chartWrapTitle ); RequestTimedRefresh( 1 ); 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.GetStatus( ORderID, True ); if ( BuyOrderTrigger ) { OrderID = ibc.ModifyOrder( OrderID, Name(), "Buy", 100, "MKT", 0, 0, "Day", TransmitTrigger ); StaticVarSetText( "OrderID", OrderID ); } if ( SellOrderTrigger ) { OrderID = ibc.ModifyOrder( OrderID, Name(), "Sell", 100, "MKT", 0, 0, "Day", TransmitTrigger ); StaticVarSetText( "OrderID", OrderID ); } else if ( CancelOrderTrigger ) { OrderID = StaticVarGetText( "OrderID" ); ibc.CancelOrder( ORderID ); } else if ( TransmitTrigger ) { if ( ibc.IsOrderPending( ORderID ) ) ibc.Transmit( ORderID ); } OrderID = StaticVarGetText( "OrderID" ); LastTWSMsg = ibc.getLastError( ORderID ); Title = "\n" + " OrderID: " + ORderID + "\n" + "Last TWS Error Msg: " + LastTWSMsg + "\n" + "Order Status at IB: " + ORderStatus + "\n" + " TWS Position Size: " + NumToStr( ibc.GetPositionSize( Name() ), 1.0, False );
Edited by Al Venosa
Filed by Herman at 12:56 am under System Automation
Comments Off on Testing your AB-IBc-TWS Communication