Monthly Archives: January 2011

quickfix logon support for username password

QuickFix is an open source FIX engine.
Ive been doing a little simulator along the lines of this (but using zeromq+quickfix) to become familiar with FIX.

The ASX supports the Username and Password fields for Logon messages. Quickfix normally takes care of Logon for you, but does not support Username and Password as session settings. Im only interested in FIX 4.4 (as thats the highest the ASX supports), so ill restrict my code to dealing with that.

Because we are modifying outgoing Logon messages, normally we would use a message cracker from our Application::toAdmin() override.
But there is a bug in quickfix (1.13.3) that means that cracking non const message references calls the wrong code. It ends up calling the onMessage for the message const ref.
ie it calls

void MQFeederApplication::onMessage( const FIX44::Logon& m, const FIX::SessionID& sessionID)

instead of

void MQFeederApplication::onMessage( FIX44::Logon& m, const FIX::SessionID& sessionID)

Rather than showing how to fix that bug and the code you will use ill just code the message type check right into toAdmin(). If you were doing a real program youd fix the bug and do it properly.

Firstly the obvious. We need to inject the settings class into our application so that we can pull the username and password from the config file.

class MQFeederApplication
	: public FIX::Application,
	private FIX::MessageCracker
{
public:
	MQFeederApplication(const FIX::SessionSettings&);
// ...
private:
	const FIX::SessionSettings& settings;
// ...
};

and pass it to our Applications ctor:

int main(){
		FIX::SessionSettings settings( "../mqfeed.cfg" );
		MQFeederApplication app(settings);
//...

Then in toAdmin we check the message type and pull the appropriate username and password from the settings

void MQFeederApplication::toAdmin( FIX::Message& message, const FIX::SessionID& sessionID)
{
	// when we send Logon make sure we add Username and Password if we have it configured for this session
	// I would have like to use a message cracker for this, but theres a bug in quickfix 1.13.3
	// wrt cracking non const messages
	const FIX::Dictionary& session_settings = settings.get(sessionID);
	if (FIX::MsgType_Logon == FIELD_GET_REF( message.getHeader(), MsgType)) {
		FIX44::Logon& logon_message = dynamic_cast<FIX44::Logon&>(message);
		if (session_settings.has("Username")) {
			FIX::Username username = session_settings.getString("Username");
			logon_message.set( username );
		}
		if (session_settings.has("Password")) {
			FIX::Password password = session_settings.getString("Password");
			logon_message.set( password );
		}
	}
}

So far working with quickfix has been pretty easy.
I was initially surprised by the use of little class wrappers for each field type, but it means the compiler can check that you arent trying to set fields that arent supported.
eg

		FIX44::Logon& logon_message = dynamic_cast<FIX44::Logon&>(message);
		logon_message.set(FIX::Symbol("GOOG"));

will result in a compile time error. neat.
If you do want to add custom fields you can either use Message::setField, or regenerate the code from a modified data dictionary.

C++ tricks: lazy mans operator<

Disclaimer: This may seem less interesting when its not 2am.

So Im writing this very simple class representing market data subscriptions.
Its really just an aggregate of some data.
Session id, symbol to subscribe to, and subscription request id.

It basically looks a little bit like

class Subscription {
   public:
      // ...
   private:
      FIX::SessionID session;
      std::string symbol;
      std::string req_id;
};

At some point I wanted to put these in an STL set.
This means I need a function that is a model of Strict Weak Ordering

All the members already have operator< but its still tedious to write a conforming func when theres more than a few members. Typically you end up with things like

inline bool lt_compare(const Subscription& a, const Subscription& b) {
   return a.get_session() < b.get_session()
      || a.get_session() == b.get_session() && a.get_symbol() < b.get_symbol()
      || a.get_session() == b.get_session() && a.get_symbol() == b.get_symbol() && a.get_req_id() < b.get_req_id();
}

Which isnt pretty and is easy to make a typo in. Adding more members just makes it uglier.

So I started thinking how Id do it in Python and came up with something like

def lt_compare(a,b):
   return (a.session,a.symbol,a.req_id) < (b.session,b.symbol,b.req_id)

This works because python’s tuples support __lt__ in the obvious manner.

I also recalled that std::pair provides operator<. Seeing as boost::tuple is an extension of std::pair I realized I could leverage that.

inline bool lt_compare (const Subscription& s1, const Subscription& s2) {
	return boost::make_tuple(s1.get_session_id(), s1.get_symbol(), s1.get_req_id())
		< boost::make_tuple(s2.get_session_id(), s2.get_symbol(), s2.get_req_id());
}

Quite close to the python version. IMO less prone to typos than the original C++ version.

Now for the problem: It does copy the members when it makes the tuple. This means its still not suitable for classes with heavy copy constructors. Like most versions of string.
We can fix that by wrapping all the arguments in boost::cref.

inline bool lt_compare (const Subscription& s1, const Subscription& s2) {
	return boost::make_tuple( boost::cref(s1.get_session_id()), boost::cref(s1.get_symbol()), boost::cref(s1.get_req_id()))
		< boost::make_tuple( boost::cref(s2.get_session_id()), boost::cref(s2.get_symbol()), boost::cref(s2.get_req_id()));
}

but it starts to look ugly again.
In the end I didnt use it anyway. I didnt need to store them in a set after all.

It should be pretty easy to write a template or macro thats even easier to use.
For 1 mins work i thought it was a neat trick.