I connect to several APIs that all use FXI4.2 but now I wish to connect to another that uses its own version of FIX4.4.
I have an router app that send orders to the various APIs and it would appear that I need to duplicate all my methods (e.g. the OnMessage(), NewSingleOrder etc) to cope with the 2 FIX protocols.
Is there a smarter way to do this to avoid this duplication?
Moderators: I know this is a little open now, but I will add some code snippets once I get some initial feedback.
public void OnMessage(QuickFix.FIX42.MarketDataIncrementalRefresh message, SessionID sessionID)
{
int count = message.NoMDEntries.getValue();
QuickFix.FIX42.MarketDataSnapshotFullRefresh.NoMDEntriesGroup repeatingMDItem = new QuickFix.FIX42.MarketDataSnapshotFullRefresh.NoMDEntriesGroup();
DateTime sourceDT = DateTime.ParseExact(message.Header.GetField(52), "yyyyMMdd-HH:mm:ss.fff", ci);
DateTime dt = TimeZoneInfo.ConvertTimeToUtc(sourceDT, utcZone);
DateTime nowUTC = TimeZoneInfo.ConvertTime(DateTime.UtcNow, utcZone, utcZone);
TimeSpan diffToUK = nowUTC - dt;
for (int i = 1; i <= count; i++)
{
message.GetGroup(i, repeatingMDItem);
String symbol = repeatingMDItem.GetField(55);
int tickBandNoDecPlaces = int.Parse(repeatingMDItem.GetField(5071));
masterForm.MDATA.AddData(symbol, tickBandNoDecPlaces, sourceDT);
}
}
Question: Will FIX44 accept all previous FIX?
How can I make this agnostic about which FIX version?
public void OnMessage(QuickFix.FIX42.MarketDataSnapshotFullRefresh message, SessionID sessionID)
{
OnMessageAgnostic(message, sessionID);
}
public void OnMessage(QuickFix.FIX44.MarketDataSnapshotFullRefresh message, SessionID sessionID)
{
OnMessageAgnostic(message, sessionID);
}
public int FixVersion(QuickFix.Message message)
{
switch (message.GetString(8)) // BeginString
{
case Values.BeginString_FIX42:
return 42;
case Values.BeginString_FIX44:
return 44;
default:
throw new NotSupportedException("This version of FIX is unsupported");
}
}
public void OnMessageAgnostic(QuickFix.Message message, SessionID sessionID)
{
int count;
if (FixVersion(message)==44)
{
count = ((QuickFix.FIX44.MarketDataSnapshotFullRefresh)message).NoMDEntries.getValue();
}
}
The problem is that is that FIX message types from different versions don't have any relationship except for their base class - at the lowest level, all FIX messages derive from Message
. You need to take the information you need from a message, package it in such a way that it's version-agnostic (as far as is possible), and then write code against those version-agnostic data structures.
I suggest that you let the message cracker do the initial filtering for you, if you're OK to let it handle that, and then feed the message to a handler that can deal with that particular type of message:
public void OnMessage(QuickFix.FIX42.MarketDataIncrementalRefresh message, SessionID sessionID)
{
this.marketDataIncrementalRefreshHandler.Handle(message);
}
public void OnMessage(QuickFix.FIX44.MarketDataIncrementalRefresh message, SessionID sessionID)
{
this.marketDataIncrementalRefreshHandler.Handle(message);
}
... elsewhere ...
public interface FixMessageHandler
{
void Handle(Message msg);
}
public class MarketDataIncrementalRefreshHandler : FixMessageHandler
{
public void Handle(Message msg)
{
DateTime sourceDT = DateTime.ParseExact(message.Header.GetField(52), "yyyyMMdd-HH:mm:ss.fff", ci);
DateTime dt = TimeZoneInfo.ConvertTimeToUtc(sourceDT, utcZone);
DateTime nowUTC = TimeZoneInfo.ConvertTime(DateTime.UtcNow, utcZone, utcZone);
TimeSpan diffToUK = nowUTC - dt;
var noMDEntriesGroups = this.GetAllNoMDEntries(msg)
foreach (var noMDEntriesGroup in noMDEntriesGroups)
{
masterForm.MDATA.AddData(
noMDEntriesGroup.Symbol,
noMDEntriesGroup.TickBandNoDecPlaces,
sourceDT);
}
}
private IEnumerable<NoMDEntriesGroup> GetAllNoMDEntries(Message msg)
{
switch (message.GetString(8)) // BeginString
{
case Values.BeginString_FIX42:
return this.GetAllNoMDEntries((QuickFix.FIX42.MarketDataSnapshotFullRefresh)msg);
case Values.BeginString_FIX44:
return this.GetAllNoMDEntries((QuickFix.FIX44.MarketDataSnapshotFullRefresh)msg);
default:
throw new NotSupportedException("This version of FIX is unsupported");
}
}
private IEnumerable<NoMDEntriesGroup> GetAllNoMDEntries(QuickFix.FIX42.MarketDataSnapshotFullRefresh msg)
{
int count = message.NoMDEntries.getValue();
QuickFix.FIX42.MarketDataSnapshotFullRefresh.NoMDEntriesGroup repeatingMDItem = new QuickFix.FIX42.MarketDataSnapshotFullRefresh.NoMDEntriesGroup();
for (int i = 1; i <= count; i++)
{
message.GetGroup(i, repeatingMDItem);
yield return new NoMDEntriesGroup
{
Symbol = repeatingMDItem.GetField(55),
TickBandNoDecPlaces = int.Parse(repeatingMDItem.GetField(5071)
};
}
}
private IEnumerable<NoMDEntriesGroup> GetAllNoMDEntries(QuickFix.FIX44.MarketDataSnapshotFullRefresh msg)
{
// Should be practically identical to the above version, with 4.4 subbed for 4.2
}
private class NoMDEntriesGroup
{
public string Symbol { get; set; }
public int TickBandNoDecPlaces { get; set; }
}
}