Codes to pull data from TWS Interactive Broker - C#

Quote from hftvol:

Out of curiosity, care to show me an example of IB code that is purely based on string passing? I guess with "internally" you mean the core IB API? I have not seen what you suggest so would be curious. Not that I spent too much time on it, I do not execute much aside FX through IB and even that goes through FIX.

But I am interested in what you said about the string passing aspect of the IB API.

THanks

Yes at the core it is field decoding and encoding with conversion from to strings. There is a lot of code in their API (here C++) but you can focus on folders "Shared", "SocketClient" and review some files for example

..shared\EClientSocketBaseImpl.h, IBString.h

here are some code snippets of 1 decoder and encoder showing the action on double using atof (STL), snprintf. Once the encoding/decoding is done appropriate method/callback is called based on message id and API user can process it in appropriate overrided function in their implementation. It would be unlikely that any other mechanism would be used considering that sockets send/receive chars.

template<>
void EClientSocketBase::EncodeField<double>(std::ostream& os, double doubleValue)
{
char str[128];

snprintf(str, sizeof(str), "%.10g", doubleValue);

EncodeField<const char*>(os, str);
}


bool EClientSocketBase::DecodeField(double& doubleValue, const char*& ptr, const char* endPtr)
{
if( !CheckOffset(ptr, endPtr))
return false;
const char* fieldBeg = ptr;
const char* fieldEnd = FindFieldEnd(fieldBeg, endPtr);
if( !fieldEnd)
return false;
doubleValue = atof(fieldBeg);
ptr = ++fieldEnd;
return true;
}
 
thanks for the reference.

Quote from vicirek:

Yes at the core it is field decoding and encoding with conversion from to strings. There is a lot of code in their API (here C++) but you can focus on folders "Shared", "SocketClient" and review some files for example

..shared\EClientSocketBaseImpl.h, IBString.h

here are some code snippets of 1 decoder and encoder showing the action on double using atof (STL), snprintf. Once the encoding/decoding is done appropriate method/callback is called based on message id and API user can process it in appropriate overrided function in their implementation. It would be unlikely that any other mechanism would be used considering that sockets send/receive chars.

template<>
void EClientSocketBase::EncodeField<double>(std::ostream& os, double doubleValue)
{
char str[128];

snprintf(str, sizeof(str), "%.10g", doubleValue);

EncodeField<const char*>(os, str);
}


bool EClientSocketBase::DecodeField(double& doubleValue, const char*& ptr, const char* endPtr)
{
if( !CheckOffset(ptr, endPtr))
return false;
const char* fieldBeg = ptr;
const char* fieldEnd = FindFieldEnd(fieldBeg, endPtr);
if( !fieldEnd)
return false;
doubleValue = atof(fieldBeg);
ptr = ++fieldEnd;
return true;
}
 
Back
Top