{"id":1722,"date":"2008-03-16T17:05:37","date_gmt":"2008-03-16T17:05:37","guid":{"rendered":"http:\/\/www.amibroker.org\/userkb\/2008\/03\/16\/parsing-tws-error-messages\/"},"modified":"2009-02-20T22:53:17","modified_gmt":"2009-02-20T22:53:17","slug":"parsing-tws-error-messages","status":"publish","type":"post","link":"http:\/\/www.amibroker.org\/editable_userkb\/2008\/03\/16\/parsing-tws-error-messages\/","title":{"rendered":"Parsing TWS Error messages"},"content":{"rendered":"

Here is some simple code you can use to enable you to start developing your own error-parsing functions. In this example, error messages are hard-coded to facilitate code development, i.e. you don\u2019t have to be live with IB to test it. I suggest that you inspect the IB list of error messages<\/a>, copy the errors that are important to you, and add them to the hard-coded list. Since IB doesn\u2019t list the error messages in the exact format you will receive them, you may have to place some orders with errors to see their exact format. When Applied to an Indicator, the code can be exercised by selecting the Error Message with the slider in the Param window. It will display the current error and parsing results in the Chart Title. Be sure to set Parameter properties to wrap the Title.<\/p>\n

parsingerrors.png<\/a><\/p>\n

S <\/span>= <\/span>Param<\/span>( <\/span>"Example Error Msg"<\/span>, <\/span>0<\/span>, <\/span>0<\/span>, <\/span>10<\/span>, <\/span>1 <\/span>);\rswitch ( <\/span>S <\/span>)\r{\rcase <\/span>0<\/span>:\r    <\/span>TWSLastErrorMsg <\/span>= <\/span>"ID=15071. Error 135. Can't find order with id =:15071"<\/span>;\r    break;\rcase <\/span>1<\/span>:\r    <\/span>TWSLastErrorMsg <\/span>= <\/span>"ID=2094, Error 103, Duplicate ORder id"<\/span>;\r    break;\rcase <\/span>2<\/span>:\r    <\/span>TWSLastErrorMsg <\/span>= <\/span>"ID=-1, Error 1102, Connectivity between IB AND TWS Has been restored - data MAintained"<\/span>;\r    break;\rcase <\/span>3<\/span>:\r    <\/span>TWSLastErrorMsg <\/span>= <\/span>"ID=-1, Error 2100, New account data requested"<\/span>;\r    break;\rcase <\/span>4<\/span>:\r    <\/span>TWSLastErrorMsg <\/span>= <\/span>"ID=-1, Error 1100, Connectivity between IB AND TWS has been lost"<\/span>;\r    break;\rcase <\/span>5<\/span>:\r    <\/span>TWSLastErrorMsg <\/span>= <\/span>"Connection established OK, Next Order Id=2080"<\/span>;\r    break;\rcase <\/span>6<\/span>:\r    <\/span>TWSLastErrorMsg <\/span>= <\/span>"ID=6125. Error 202. Order Canceled - reason:"<\/span>;\r    break;\rdefault:\r    <\/span>TWSLastErrorMsg <\/span>= <\/span>""<\/span>;\r    break;\r}\r\rfunction <\/span>GetErrorCode<\/span>( <\/span>TWSLastErrorMsg <\/span>)\r{\r    <\/span>p <\/span>= <\/span>StrFind<\/span>( <\/span>TWSLastErrorMsg<\/span>, <\/span>" Error " <\/span>);\r    <\/span>ErrStr <\/span>= <\/span>StrMid<\/span>( <\/span>TWSLastErrorMsg<\/span>, <\/span>p <\/span>+ <\/span>6<\/span>, <\/span>4 <\/span>);\r    <\/span>ErrNum    <\/span>= <\/span>StrToNum<\/span>( <\/span>ErrStr <\/span>);\r    <\/span>ErrStr    <\/span>= <\/span>NumToStr<\/span>( <\/span>ErrNum<\/span>, <\/span>1.0<\/span>, <\/span>False <\/span>);\r    return <\/span>ErrStr<\/span>;\r}\r\rfunction <\/span>ExtractErrorID<\/span>( <\/span>TWSLastErrorMsg <\/span>)\r{\r    <\/span>p <\/span>= <\/span>StrFind<\/span>( <\/span>TWSLastErrorMsg<\/span>, <\/span>"ID=" <\/span>);\r    <\/span>IDStr <\/span>= <\/span>StrMid<\/span>( <\/span>TWSLastErrorMsg<\/span>, <\/span>p <\/span>+ <\/span>2<\/span>, <\/span>5 <\/span>);\r    <\/span>IDNum    <\/span>= <\/span>StrToNum<\/span>( <\/span>IDStr <\/span>);\r    <\/span>IDStr    <\/span>= <\/span>NumToStr<\/span>( <\/span>IDNum<\/span>, <\/span>1.0<\/span>, <\/span>False <\/span>);\r    return <\/span>IDStr<\/span>;\r}\r\r<\/span>TWSErrorCode <\/span>= <\/span>GetErrorCode<\/span>( <\/span>TWSLastErrorMsg <\/span>);\r<\/span>TWSErrorOrderID <\/span>= <\/span>ExtractErrorID<\/span>( <\/span>TWSLastErrorMsg <\/span>);\r<\/span>Title <\/span>= <\/span>"\\n" <\/span>+\r        <\/span>"Extracting OrderIDs and Error codes from TWS Error Messages" <\/span>+ <\/span>"\\n" <\/span>+\r        <\/span>"Example Error Msg: " <\/span>+ <\/span>TWSLastErrorMsg <\/span>+ <\/span>"\\n" <\/span>+\r        <\/span>"        Error Code: " <\/span>+ <\/span>TWSErrorCode <\/span>+ <\/span>"\\n" <\/span>+\r        <\/span>" TWS Error OrderID: " <\/span>+ <\/span>TWSErrorOrderID<\/span>;<\/span><\/pre>\n","protected":false},"excerpt":{"rendered":"

Here is some simple code you can use to enable you to start developing your own error-parsing functions. In this example, error messages are hard-coded to facilitate code development, i.e. you don\u2019t have to be live with IB to test it. I suggest that you inspect the IB list of error messages, copy the errors […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[70],"tags":[],"_links":{"self":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts\/1722"}],"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=1722"}],"version-history":[{"count":0,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts\/1722\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/media?parent=1722"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/categories?post=1722"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/tags?post=1722"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}