FIX:How to send ,messages using FIX ,quickfixj

笑着哭i 提交于 2020-03-05 16:06:30

问题


# 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.

Also onCreate and ToApp are being called when I try to run the code.

I want to know whether i have sent the message successfully ?

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

I don't see ToAdmin and FromAdmin being executed in my code. 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.

Please advise whats going wrong in this?


回答1:


OK your settings file looks ok and you're saying it works, but I don't think you need GATEWAY and TRANSPORT headings

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.

The default QuickFIX automatically logs on to the Target in your settings file, and if the logon is accepted then it begins to heartbeat. From your comments it sounds like this is happening.

So what's going wrong is your sendLogonRequest is not necessary. Also, if you do send "extra" logons then the target FIX engine will probably reject or ignore them. 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.

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


来源:https://stackoverflow.com/questions/42745874/fixhow-to-send-messages-using-fix-quickfixj

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!