QuickFix / N如何最好地处理多个FIX版本

时间:2015-04-30 12:54:49

标签: c# quickfix fix-protocol

我连接到几个全部使用FXI4.2的API,但现在我希望连接到使用其自己版本的FIX4.4的另一个。

我有一个路由器应用程序向各种API发送命令,看起来我需要复制我的所有方法(例如OnMessage(),NewSingleOrder等)以应对2个FIX协议。

有没有更聪明的方法来避免这种重复?

主持人:我知道现在有点开放,但是一旦得到一些初步反馈,我会添加一些代码片段。

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);
    }
}

问题:FIX44会接受以前所有的FIX吗?

如何使这个FIX版本不可知?

        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();
             }
        }

1 个答案:

答案 0 :(得分:3)

问题是来自不同版本的FIX消息类型除了它们的基类之外没有任何关系 - 在最低级别,所有FIX消息都派生自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; }
    }
}