简体   繁体   English

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

[英]FIX:How to send ,messages using FIX ,quickfixj

# 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

and then 接着

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

Please find my observations below: 请在下面找到我的观察结果:

In the event logs I am seeing as Created session: and the message which I am trying to send. 在事件日志中,我看到的是“创建的会话:”和我要发送的消息。 Also I could see that the sender sequence number is getting incremented in the logs. 我也可以看到发送者序列号在日志中正在递增。

Messages.log and header.logs are blank and body.log has the message which i am trying to send. Messages.log和header.logs为空白,body.log具有我要发送的消息。

Also onCreate and ToApp are being called when I try to run the code. 当我尝试运行代码时,也会调用onCreate和ToApp。

I want to know whether i have sent the message successfully ? 我想知道我是否已经成功发送了消息?

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

I don't see ToAdmin and FromAdmin being executed in my code. 我没有在代码中看到ToAdmin和FromAdmin。 Also Do I need to write the acceptor code as well for the same, or just the initiator code will be fine. 我是否也需要同样编写接受者代码,否则仅发起者代码就可以了。 The DataDictionary which i am using has all the fields set, but I am not sure whether its being used by QuickFixJ when i try to execute the code. 我正在使用的DataDictionary已设置了所有字段,但是我不确定在尝试执行代码时QuickFixJ是否正在使用它。

Please advise whats going wrong in this? 请告知这是怎么回事?

OK your settings file looks ok and you're saying it works, but I don't think you need GATEWAY and TRANSPORT headings 好的,您的设置文件看起来不错,您说的是它可以工作,但是我认为您不需要GATEWAY和TRANSPORT标题

As for your code, all you need to do to start with is setup the default QuickFIX Application, FileStoreFactory, LogFactory, MessageFactory and Initiator which you have done. 对于您的代码,所有您需要做的就是设置默认的QuickFIX应用程序,FileStoreFactory,LogFactory,MessageFactory和Initiator。

The default QuickFIX automatically logs on to the Target in your settings file, and if the logon is accepted then it begins to heartbeat. 默认的QuickFIX将自动登录到您的设置文件中的目标,如果登录被接受,它将开始心跳。 From your comments it sounds like this is happening. 从您的评论看来,这正在发生。

So what's going wrong is your sendLogonRequest is not necessary. 所以出问题了,您的sendLogonRequest是不必要的。 Also, if you do send "extra" logons then the target FIX engine will probably reject or ignore them. 另外,如果您确实发送“额外”登录,则目标FIX引擎可能会拒绝或忽略它们。 The reject message would be seen in the logs or file store. 拒绝消息将在日志或文件存储中看到。

So then you have the QuickFIX API with which to start with you can simply output messages to your own log. 这样便有了QuickFIX API,您可以从中简单地将消息输出到自己的日志中。

Something like this 像这样

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

Then when you want to send a message, try something like this: 然后,当您要发送消息时,请尝试以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM