FIX:如何使用FIX,quickfixj发送消息

时间:2017-03-12 09:43:10

标签: fix-protocol

# Here is my settings.cfg file:
# Thank you.Here is what i tried.My settings.cfg file

[DEFAULT]
# Settings which apply to all the Sessions.
ConnectionType=initiator
LogonTimeout=30
ReconnectInterval=20
ResetOnLogon=Y
FileLogPath=C:\Work\QuickFIXJ\logs
FileStorePath=C:\Work\QuickFIXJ\logs

[SESSION]
# Settings specifically for one session
BeginString=FIX.4.4
SenderCompID=XYZ
TargetCompID=TYZ
TimeZone=Asia/Tokyo
StartTime=16:00:00
EndTime=13:30:00
HeartBtInt=60
SocketConnectPort=7200
SocketConnectHost=123.123.123.123
UseDataDictionary=Y
DataDictionary=C:\Work\QuickFIXJ\datadictionary\FIX44.xml

[GATEWAY]
Port=4444

[TRANSPORT]
Port=4444

然后

//initiator code:

public class TestQuickFixJConnectivity {
    public static void main(String[] args) {
        SocketInitiator socketInitiator = null;
        try {
            SessionSettings sessionSettings = new SessionSettings("C:\\Work\\QuickFixJ\\sessionSettings.txt");
            Application application = new TestApplicationImpl();
            FileStoreFactory fileStoreFactory = new FileStoreFactory(sessionSettings);
            FileLogFactory logFactory = new FileLogFactory(sessionSettings);
            MessageFactory messageFactory = new DefaultMessageFactory();
            socketInitiator = new SocketInitiator(application,
                    fileStoreFactory, sessionSettings, logFactory,
                    messageFactory);
            socketInitiator.start();
            SessionID sessionId = socketInitiator.getSessions().get(0);
            sendLogonRequest(sessionId);
            int i = 0;
            do {
                try {
                    Thread.sleep(1000);
                    System.out.println(socketInitiator.isLoggedOn());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                i++;
            } while ((!socketInitiator.isLoggedOn()) && (i < 30));
        } catch (ConfigError e) {
            e.printStackTrace();
        } catch (SessionNotFound e) {
            e.printStackTrace();
        } catch (Exception exp) {
            exp.printStackTrace();
        } finally {
            if (socketInitiator != null) {
                socketInitiator.stop(true);
            }
        }
    }

    private static void sendLogonRequest(SessionID sessionId) throws SessionNotFound {
        Logon logon = new Logon();
        Message msg=new Message();
        Header header = msg.getHeader();
        logon.set(new HeartBtInt(60));
        logon.set(new ResetSeqNumFlag(true));
        header.setField(new BeginString("FIX.4.4"));
        header.setField(new MsgType("AP"));
        header.setField(new SenderCompId("XYZ"));
        header.setField(new TagetCompId("TYZ"));
        header.setField(new ResetSeqNumFlag(true));
        //here i m setting all the fields in the csv report .
        msg.setField(705,new SortQty(""));
        // ....
        //below statement returning false
        boolean sent = Session.sendToTarget(msg, sessionId);
        System.out.println("Logon Message Sent : " + sent);
    }
}

请在下面找到我的观察结果:

在事件日志中,我看到创建的会话:以及我尝试发送的消息。此外,我可以看到发件人序列号在日志中递增。

Messages.log和header.logs为空,body.log包含我要发送的消息。

当我尝试运行代码时,也会调用onCreate和ToApp。

我想知道我是否已成功发送邮件?

另外boolean sent = Session.sendToTarget(msg, sessionId); is returning false

我没有看到在我的代码中执行ToAdmin和FromAdmin。我是否也需要编写接收器代码,或者只是启动器代码就可以了。我正在使用的DataDictionary设置了所有字段,但我不确定当我尝试执行代码时它是否被QuickFixJ使用。

请告知这里出了什么问题?

1 个答案:

答案 0 :(得分:1)

确定你的设置文件看起来不错,你说它有效,但我认为你不需要GATEWAY和TRANSPORT标题

至于你的代码,你需要做的就是设置默认的QuickFIX应用程序,FileStoreFactory,LogFactory,MessageFactory和Initiator。

默认的QuickFIX会自动在设置文件中登录到目标,如果登录被接受则会开始心跳。从您的评论中可以看出这种情况正在发生。

所以出现问题的是你的sendLogonRequest不是必需的。此外,如果您发送“额外”登录,则目标FIX引擎可能会拒绝或忽略它们。拒绝消息将在日志或文件存储中看到。

那么您可以使用QuickFIX API开始,只需将消息输出到您自己的日志中。

像这样的东西

public void fromApp(Message msg, SessionID s) throws UnsupportedMessageType, FieldNotFound, IncorrectTagValue
{
    log.debug(String.valueOf(LocalTime.now()) + " INITIATOR: FromApp " + msg.toString());
}
public void toApp(Message msg, SessionID s)
{
    log.info(String.valueOf(LocalTime.now()) + " INITIATOR: ToApp " + msg.toString());  
}   
public void fromAdmin(Message msg, SessionID s) throws FieldNotFound, IncorrectTagValue
{
    Log.trace("INITIATOR: FromAdmin " + msg.getClass() + " " + msg.toString());    
}   
public void onCreate(SessionID s) 
{
    log.info("INITIATOR: OnCreate " + s.toString());
}    
public void onLogout(SessionID s)
{
    log.error("INITIATOR: OnLogout " + s.toString());   
}   
public void onLogon(SessionID s)
{
    log.info("INITIATOR: OnLogon " + s.toString());
}    
public void toAdmin(Message msg, SessionID s)
{
    log.trace("INITIATOR: ToAdmin " + msg.getClass() + " " + msg.toString());
}

然后当你想发送信息时,试试这样的事情:

    QuoteReqID id = new QuoteReqID("1234");

    // Create Quote Request message
    QuoteRequest qr = new QuoteRequest(id);

    // Setup outgoing group and specify an index for each group tag
    NoRelatedSym g = new NoRelatedSym();

    String instrument = m.getString("EUR/USD");
    Symbol symbol = new Symbol(instrument);     
    g.setField(1, symbol);

    QuoteRequestType qt = new QuoteRequestType(102);
    g.setField(2, qt);

    OrderQty qty = new OrderQty("1000000");
    g.setField(3, qty);

    SettlType sType = new SettlType("B");
    g.setField(4, sType);

    SettlDate sDate = new SettlDate("20170315");
    g.setField(5, sDate);

    Account account = new Account("357647");
    g.setField(6, account); 

    // add group to request
    qr.addGroup(g);

    Session s = Session.lookupSession(i.getSessions().get(0));
    s.send(qr);