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

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

我有一个路由器应用程序,可以将命令发送到各种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();
             }
        }

解决方法:

问题在于,来自不同版本的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; }
    }
}
上一篇:java-实施基于FIX协议的订单并支持止损和获利


下一篇:c#-Visual Studio上.NET的QUICKFIX 64位生成(.NET C DLL包装器)