{"id":617,"date":"2007-07-12T00:56:13","date_gmt":"2007-07-12T00:56:13","guid":{"rendered":"http:\/\/www.amibroker.org\/userkb\/2007\/07\/12\/testing-your-ab-ibc-tws-communication\/"},"modified":"2008-02-25T16:10:05","modified_gmt":"2008-02-25T16:10:05","slug":"testing-your-ab-ibc-tws-communication","status":"publish","type":"post","link":"http:\/\/www.amibroker.org\/editable_userkb\/2007\/07\/12\/testing-your-ab-ibc-tws-communication\/","title":{"rendered":"Testing your AB-IBc-TWS Communication"},"content":{"rendered":"

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. <\/p>\n

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<\/a>. When you have signed and submitted the User Agreement the Unlock Code will be emailed to you within seconds.<\/p>\n

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: <\/p>\n

<\/p>\n

Order and TWS Status is displayed in the Title: <\/p>\n

<\/p>\n

If you are using IB?s eDemo, orders may be processed slowly enough for you to observe how the orders are processed. <\/p>\n

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.<\/p>\n

SetChartOptions<\/span>( <\/span>2<\/span>, <\/span>chartWrapTitle <\/span>);\r<\/span>RequestTimedRefresh<\/span>( <\/span>1 <\/span>);\r<\/span>BuyOrderTrigger <\/span>= <\/span>ParamTrigger<\/span>( <\/span>"Place Buy order on TWS"<\/span>, <\/span>"BUY" <\/span>);\r<\/span>SellOrderTrigger <\/span>= <\/span>ParamTrigger<\/span>( <\/span>"Place Sell order on TWS"<\/span>, <\/span>"SELL" <\/span>);\r<\/span>CancelOrderTrigger <\/span>= <\/span>ParamTrigger<\/span>( <\/span>"Cancel order on TWS"<\/span>, <\/span>"CANCEL" <\/span>);\r<\/span>TransmitTrigger <\/span>= <\/span>ParamTrigger<\/span>( <\/span>"Transmit Order"<\/span>, <\/span>"TRANSMIT" <\/span>);\r<\/span>ibc <\/span>= <\/span>GetTradingInterface<\/span>( <\/span>"IB" <\/span>);\r<\/span>OrderID <\/span>= <\/span>StaticVarGetText<\/span>( <\/span>"OrderID" <\/span>);\r<\/span>ORderStatus <\/span>= <\/span>ibc<\/span>.<\/span>GetStatus<\/span>( <\/span>ORderID<\/span>, <\/span>True <\/span>);\r\rif ( <\/span>BuyOrderTrigger <\/span>)\r{\r    <\/span>OrderID <\/span>= <\/span>ibc<\/span>.<\/span>ModifyOrder<\/span>( <\/span>OrderID<\/span>, <\/span>Name<\/span>(), <\/span>"Buy"<\/span>, <\/span>100<\/span>, <\/span>"MKT"<\/span>, <\/span>0<\/span>, <\/span>0<\/span>, <\/span>"Day"<\/span>, <\/span>TransmitTrigger <\/span>);\r    <\/span>StaticVarSetText<\/span>( <\/span>"OrderID"<\/span>, <\/span>OrderID <\/span>);\r}\r\rif ( <\/span>SellOrderTrigger <\/span>)\r{\r    <\/span>OrderID <\/span>= <\/span>ibc<\/span>.<\/span>ModifyOrder<\/span>( <\/span>OrderID<\/span>, <\/span>Name<\/span>(), <\/span>"Sell"<\/span>, <\/span>100<\/span>, <\/span>"MKT"<\/span>, <\/span>0<\/span>, <\/span>0<\/span>, <\/span>"Day"<\/span>, <\/span>TransmitTrigger <\/span>);\r    <\/span>StaticVarSetText<\/span>( <\/span>"OrderID"<\/span>, <\/span>OrderID <\/span>);\r}\relse\r    if ( <\/span>CancelOrderTrigger <\/span>)\r    {\r        <\/span>OrderID <\/span>= <\/span>StaticVarGetText<\/span>( <\/span>"OrderID" <\/span>);\r        <\/span>ibc<\/span>.<\/span>CancelOrder<\/span>( <\/span>ORderID <\/span>);\r    }\r    else\r        if ( <\/span>TransmitTrigger <\/span>)\r        {\r            if ( <\/span>ibc<\/span>.<\/span>IsOrderPending<\/span>( <\/span>ORderID <\/span>) )\r                <\/span>ibc<\/span>.<\/span>Transmit<\/span>( <\/span>ORderID <\/span>);\r        }\r\r<\/span>OrderID <\/span>= <\/span>StaticVarGetText<\/span>( <\/span>"OrderID" <\/span>);\r\r<\/span>LastTWSMsg <\/span>= <\/span>ibc<\/span>.<\/span>getLastError<\/span>( <\/span>ORderID <\/span>);\r<\/span>Title <\/span>= <\/span>"\\n" <\/span>+\r        <\/span>"           OrderID: " <\/span>+ <\/span>ORderID <\/span>+ <\/span>"\\n" <\/span>+\r        <\/span>"Last TWS Error Msg: " <\/span>+ <\/span>LastTWSMsg <\/span>+ <\/span>"\\n" <\/span>+\r        <\/span>"Order Status at IB: " <\/span>+ <\/span>ORderStatus <\/span>+ <\/span>"\\n" <\/span>+\r        <\/span>" TWS Position Size: " <\/span>+ <\/span>NumToStr<\/span>( <\/span>ibc<\/span>.<\/span>GetPositionSize<\/span>( <\/span>Name<\/span>() ), <\/span>1.0<\/span>, <\/span>False <\/span>);<\/span><\/pre>\n

Edited by Al Venosa<\/p>\n","protected":false},"excerpt":{"rendered":"

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 […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[124],"tags":[],"_links":{"self":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts\/617"}],"collection":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/comments?post=617"}],"version-history":[{"count":0,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts\/617\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/media?parent=617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/categories?post=617"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/tags?post=617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}