March 16, 2008
Parsing TWS Error messages
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’t have to be live with IB to test it. I suggest that you inspect the IB list of error messages, copy the errors that are important to you, and add them to the hard-coded list. Since IB doesn’t 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.
S = Param( "Example Error Msg", 0, 0, 10, 1 ); switch ( S ) { case 0: TWSLastErrorMsg = "ID=15071. Error 135. Can't find order with id =:15071"; break; case 1: TWSLastErrorMsg = "ID=2094, Error 103, Duplicate ORder id"; break; case 2: TWSLastErrorMsg = "ID=-1, Error 1102, Connectivity between IB AND TWS Has been restored - data MAintained"; break; case 3: TWSLastErrorMsg = "ID=-1, Error 2100, New account data requested"; break; case 4: TWSLastErrorMsg = "ID=-1, Error 1100, Connectivity between IB AND TWS has been lost"; break; case 5: TWSLastErrorMsg = "Connection established OK, Next Order Id=2080"; break; case 6: TWSLastErrorMsg = "ID=6125. Error 202. Order Canceled - reason:"; break; default: TWSLastErrorMsg = ""; break; } function GetErrorCode( TWSLastErrorMsg ) { p = StrFind( TWSLastErrorMsg, " Error " ); ErrStr = StrMid( TWSLastErrorMsg, p + 6, 4 ); ErrNum = StrToNum( ErrStr ); ErrStr = NumToStr( ErrNum, 1.0, False ); return ErrStr; } function ExtractErrorID( TWSLastErrorMsg ) { p = StrFind( TWSLastErrorMsg, "ID=" ); IDStr = StrMid( TWSLastErrorMsg, p + 2, 5 ); IDNum = StrToNum( IDStr ); IDStr = NumToStr( IDNum, 1.0, False ); return IDStr; } TWSErrorCode = GetErrorCode( TWSLastErrorMsg ); TWSErrorOrderID = ExtractErrorID( TWSLastErrorMsg ); Title = "\n" + "Extracting OrderIDs and Error codes from TWS Error Messages" + "\n" + "Example Error Msg: " + TWSLastErrorMsg + "\n" + " Error Code: " + TWSErrorCode + "\n" + " TWS Error OrderID: " + TWSErrorOrderID;
Filed by Herman at 5:05 pm under Real-Time Auto-Trading
Comments Off on Parsing TWS Error messages